diff --git a/applogic/flow.go b/applogic/flow.go index d402ace..605ff7b 100644 --- a/applogic/flow.go +++ b/applogic/flow.go @@ -14,6 +14,10 @@ import ( // ApplyPromptChoiceQ 应用promptchoiceQ的逻辑,动态修改requestmsg func (app *App) ApplyPromptChoiceQ(promptstr string, requestmsg *string, message *structs.OnebotGroupMessage) { + userid := message.UserID + if config.GetGroupContext() && message.MessageType != "private" { + userid = message.GroupID + } // 检查是否启用了EnhancedQA if config.GetEnhancedQA(promptstr) { @@ -35,7 +39,7 @@ func (app *App) ApplyPromptChoiceQ(promptstr string, requestmsg *string, message } else { var ischange bool // 获取用户剧情存档中的当前状态 - CustomRecord, err := app.FetchCustomRecord(message.UserID) + CustomRecord, err := app.FetchCustomRecord(userid) if err != nil { fmt.Printf("app.FetchCustomRecord 出错: %s\n", err) return @@ -133,6 +137,11 @@ func (app *App) ApplyPromptChoiceQ(promptstr string, requestmsg *string, message // ApplyPromptCoverQ 应用promptCoverQ的逻辑,动态覆盖requestmsg func (app *App) ApplyPromptCoverQ(promptstr string, requestmsg *string, message *structs.OnebotGroupMessage) { + userid := message.UserID + if config.GetGroupContext() && message.MessageType != "private" { + userid = message.GroupID + } + // 检查是否启用了EnhancedQA if config.GetEnhancedQA(promptstr) { promptCover := config.GetPromptCoverQ(promptstr) @@ -142,7 +151,7 @@ func (app *App) ApplyPromptCoverQ(promptstr string, requestmsg *string, message } else { var ischange bool // 获取用户剧情存档中的当前状态 - CustomRecord, err := app.FetchCustomRecord(message.UserID) + CustomRecord, err := app.FetchCustomRecord(userid) if err != nil { fmt.Printf("app.FetchCustomRecord 出错: %s\n", err) return @@ -240,11 +249,16 @@ func (app *App) ApplyPromptCoverQ(promptstr string, requestmsg *string, message // ApplySwitchOnQ 应用switchOnQ的逻辑,动态修改promptstr func (app *App) ApplySwitchOnQ(promptstr *string, requestmsg *string, message *structs.OnebotGroupMessage) { + userid := message.UserID + if config.GetGroupContext() && message.MessageType != "private" { + userid = message.GroupID + } + // promptstr 随 switchOnQ 变化 promptstrChoices := config.GetSwitchOnQ(*promptstr) if len(promptstrChoices) != 0 { // 获取用户剧情存档中的当前状态 - CustomRecord, err := app.FetchCustomRecord(message.UserID) + CustomRecord, err := app.FetchCustomRecord(userid) if err != nil { fmt.Printf("app.FetchCustomRecord 出错: %s\n", err) return @@ -298,7 +312,7 @@ func (app *App) ApplySwitchOnQ(promptstr *string, requestmsg *string, message *s *promptstr = bestText // 获取新的信号长度 刷新持久化数据库 PromptMarksLength := config.GetPromptMarksLength(*promptstr) - app.InsertCustomTableRecord(message.UserID, *promptstr, PromptMarksLength) + app.InsertCustomTableRecord(userid, *promptstr, PromptMarksLength) fmt.Printf("enhancedChoices=true,根据关键词切换prompt为: %s,newPromptStrStat:%d\n", *promptstr, PromptMarksLength) // 故事模式规则 应用 PromptChoiceQ 这一次是为了,替换了分支后,再次用新的分支的promptstr处理一次,因为原先的promptstr是跳转前,要用跳转后的再替换一次 app.ApplyPromptChoiceQ(*promptstr, requestmsg, message) @@ -323,7 +337,7 @@ func (app *App) ApplySwitchOnQ(promptstr *string, requestmsg *string, message *s *promptstr = selectedText // 获取新的信号长度 刷新持久化数据库 PromptMarksLength := config.GetPromptMarksLength(*promptstr) - app.InsertCustomTableRecord(message.UserID, *promptstr, PromptMarksLength) + app.InsertCustomTableRecord(userid, *promptstr, PromptMarksLength) fmt.Printf("enhancedChoices=false,根据关键词切换prompt为: %s,newPromptStrStat:%d\n", *promptstr, PromptMarksLength) // 故事模式规则 应用 PromptChoiceQ 这一次是为了,替换了分支后,再次用新的分支的promptstr处理一次,因为原先的promptstr是跳转前,要用跳转后的再替换一次 app.ApplyPromptChoiceQ(*promptstr, requestmsg, message) @@ -341,8 +355,13 @@ func (app *App) ProcessExitChoicesQ(promptstr string, requestmsg *string, messag return } + userid := message.UserID + if config.GetGroupContext() && message.MessageType != "private" { + userid = message.GroupID + } + // 获取用户剧情存档中的当前状态 - CustomRecord, err := app.FetchCustomRecord(message.UserID) + CustomRecord, err := app.FetchCustomRecord(userid) if err != nil { fmt.Printf("app.FetchCustomRecord 出错: %s\n", err) return @@ -413,8 +432,13 @@ func (app *App) ProcessExitChoicesQ(promptstr string, requestmsg *string, messag // HandleExit 处理用户退出逻辑,包括发送消息和重置用户状态。 func (app *App) HandleExit(exitText string, message *structs.OnebotGroupMessage, selfid string) { + userid := message.UserID + if config.GetGroupContext() && message.MessageType != "private" { + userid = message.GroupID + } + fmt.Printf("处理重置操作on:%v", exitText) - app.migrateUserToNewContext(message.UserID) + app.migrateUserToNewContext(userid) RestoreResponse := config.GetRandomRestoreResponses() if message.RealMessageType == "group_private" || message.MessageType == "private" { if !config.GetUsePrivateSSE() { @@ -425,7 +449,7 @@ func (app *App) HandleExit(exitText string, message *structs.OnebotGroupMessage, } else { utils.SendGroupMessage(message.GroupID, message.UserID, RestoreResponse, selfid) } - app.deleteCustomRecord(message.UserID) + app.deleteCustomRecord(userid) } // ProcessExitChoicesA 处理基于关键词的退出逻辑。 @@ -435,8 +459,13 @@ func (app *App) ProcessExitChoicesA(promptstr string, response *string, message return } + userid := message.UserID + if config.GetGroupContext() && message.MessageType != "private" { + userid = message.GroupID + } + // 获取用户剧情存档中的当前状态 - CustomRecord, err := app.FetchCustomRecord(message.UserID) + CustomRecord, err := app.FetchCustomRecord(userid) if err != nil { fmt.Printf("app.FetchCustomRecord 出错: %s\n", err) return @@ -507,11 +536,16 @@ func (app *App) ProcessExitChoicesA(promptstr string, response *string, message // ApplySwitchOnA 应用switchOnA的逻辑,动态修改promptstr func (app *App) ApplySwitchOnA(promptstr *string, response *string, message *structs.OnebotGroupMessage) { + userid := message.UserID + if config.GetGroupContext() && message.MessageType != "private" { + userid = message.GroupID + } + // 获取与 switchOnA 相关的选择 promptstrChoices := config.GetSwitchOnA(*promptstr) if len(promptstrChoices) != 0 { // 获取用户剧情存档中的当前状态 - CustomRecord, err := app.FetchCustomRecord(message.UserID) + CustomRecord, err := app.FetchCustomRecord(userid) if err != nil { fmt.Printf("app.FetchCustomRecord 出错: %s\n", err) return @@ -557,7 +591,7 @@ func (app *App) ApplySwitchOnA(promptstr *string, response *string, message *str if bestMatchCount > 0 { *promptstr = bestText PromptMarksLength := config.GetPromptMarksLength(*promptstr) - app.InsertCustomTableRecord(message.UserID, *promptstr, PromptMarksLength) + app.InsertCustomTableRecord(userid, *promptstr, PromptMarksLength) fmt.Printf("enhancedChoices=true,根据关键词A切换prompt为: %s,newPromptStrStat:%d\n", *promptstr, PromptMarksLength) } } @@ -577,7 +611,7 @@ func (app *App) ApplySwitchOnA(promptstr *string, response *string, message *str selectedText := texts[rand.Intn(len(texts))] // 随机选择一个文本 *promptstr = selectedText PromptMarksLength := config.GetPromptMarksLength(*promptstr) - app.InsertCustomTableRecord(message.UserID, *promptstr, PromptMarksLength) + app.InsertCustomTableRecord(userid, *promptstr, PromptMarksLength) fmt.Printf("enhancedChoices=false,根据关键词A切换prompt为: %s,newPromptStrStat:%d\n", *promptstr, PromptMarksLength) } } @@ -588,6 +622,11 @@ func (app *App) ApplySwitchOnA(promptstr *string, response *string, message *str // ApplyPromptChoiceA 应用故事模式的情绪增强逻辑,并返回增强内容。 func (app *App) ApplyPromptChoiceA(promptstr string, response string, message *structs.OnebotGroupMessage) string { + userid := message.UserID + if config.GetGroupContext() && message.MessageType != "private" { + userid = message.GroupID + } + promptChoices := config.GetPromptChoicesA(promptstr) if len(promptChoices) == 0 { // 获取系统历史,但不包括系统消息 @@ -608,7 +647,7 @@ func (app *App) ApplyPromptChoiceA(promptstr string, response string, message *s } // 获取用户剧情存档中的当前状态 - CustomRecord, err := app.FetchCustomRecord(message.UserID) + CustomRecord, err := app.FetchCustomRecord(userid) if err != nil { fmt.Printf("app.FetchCustomRecord 出错: %s\n", err) return "" diff --git a/applogic/gensokyo.go b/applogic/gensokyo.go index a92b76e..953faa6 100644 --- a/applogic/gensokyo.go +++ b/applogic/gensokyo.go @@ -142,11 +142,19 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) { return } } - - // 从数据库读取用户的剧情存档 - CustomRecord, err := app.FetchCustomRecord(message.UserID) - if err != nil { - fmt.Printf("app.FetchCustomRecord 出错: %s\n", err) + var CustomRecord *structs.CustomRecord + if config.GetGroupContext() && message.MessageType != "private" { + // 从数据库读取用户的剧情存档 + CustomRecord, err = app.FetchCustomRecord(message.GroupID) + if err != nil { + fmt.Printf("app.FetchCustomRecord 出错: %s\n", err) + } + } else { + // 从数据库读取用户的剧情存档 + CustomRecord, err = app.FetchCustomRecord(message.UserID) + if err != nil { + fmt.Printf("app.FetchCustomRecord 出错: %s\n", err) + } } var promptstr string @@ -163,14 +171,27 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) { promptstr = CustomRecord.PromptStr fmt.Printf("刷新prompt参数: %s,newPromptStrStat:%d\n", promptstr, CustomRecord.PromptStrStat-1) newPromptStrStat := CustomRecord.PromptStrStat - 1 - err = app.InsertCustomTableRecord(message.UserID, promptstr, newPromptStrStat) - if err != nil { - fmt.Printf("app.InsertCustomTableRecord 出错: %s\n", err) + // 根据条件区分群和私聊 + if config.GetGroupContext() && message.MessageType != "private" { + err = app.InsertCustomTableRecord(message.GroupID, promptstr, newPromptStrStat) + if err != nil { + fmt.Printf("app.InsertCustomTableRecord 出错: %s\n", err) + } + } else { + err = app.InsertCustomTableRecord(message.UserID, promptstr, newPromptStrStat) + if err != nil { + fmt.Printf("app.InsertCustomTableRecord 出错: %s\n", err) + } } + } // MARK: 提示词之间 整体切换Q - app.ProcessPromptMarks(message.UserID, message.Message.(string), &promptstr) + if config.GetGroupContext() && message.MessageType != "private" { + app.ProcessPromptMarks(message.GroupID, message.Message.(string), &promptstr) + } else { + app.ProcessPromptMarks(message.UserID, message.Message.(string), &promptstr) + } // 提示词之间流转 达到信号量 markType := config.GetPromptMarkType(promptstr) @@ -189,7 +210,12 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) { } // 刷新新的提示词给用户目前的状态 新的场景应该从1开始 - app.InsertCustomTableRecord(message.UserID, newPromptStr, 1) + if config.GetGroupContext() && message.MessageType != "private" { + app.InsertCustomTableRecord(message.GroupID, newPromptStr, 1) + } else { + app.InsertCustomTableRecord(message.UserID, newPromptStr, 1) + } + fmt.Printf("流转prompt参数: %s,newPromptStrStat:%d\n", newPromptStr, 1) promptstr = newPromptStr } @@ -203,7 +229,12 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) { } // MARK: 提示词之间 整体切换Q 当用户没有存档时 - app.ProcessPromptMarks(message.UserID, message.Message.(string), &promptstr) + if config.GetGroupContext() && message.MessageType != "private" { + app.ProcessPromptMarks(message.GroupID, message.Message.(string), &promptstr) + } else { + app.ProcessPromptMarks(message.UserID, message.Message.(string), &promptstr) + } + var newstat int if config.GetPromptMarksLength(promptstr) > 1000 { newstat = config.GetPromptMarksLength(promptstr) @@ -212,7 +243,12 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) { } // 初始状态就是 1 设置了1000以上长度的是固有场景,不可切换 - err = app.InsertCustomTableRecord(message.UserID, promptstr, newstat) + if config.GetGroupContext() && message.MessageType != "private" { + err = app.InsertCustomTableRecord(message.GroupID, promptstr, newstat) + } else { + err = app.InsertCustomTableRecord(message.UserID, promptstr, newstat) + } + if err != nil { fmt.Printf("app.InsertCustomTableRecord 出错: %s\n", err) } @@ -297,7 +333,11 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) { //处理重置指令 if isResetCommand { fmtf.Println("处理重置操作") - app.migrateUserToNewContext(message.UserID) + if config.GetGroupContext() && message.MessageType != "private" { + app.migrateUserToNewContext(message.GroupID) + } else { + app.migrateUserToNewContext(message.UserID) + } RestoreResponse := config.GetRandomRestoreResponses() if message.RealMessageType == "group_private" || message.MessageType == "private" { if !config.GetUsePrivateSSE() { @@ -309,7 +349,11 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) { utils.SendGroupMessage(message.GroupID, message.UserID, RestoreResponse, selfid) } // 处理故事情节的重置 - app.deleteCustomRecord(message.UserID) + if config.GetGroupContext() && message.MessageType != "private" { + app.deleteCustomRecord(message.GroupID) + } else { + app.deleteCustomRecord(message.UserID) + } return } @@ -517,7 +561,7 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) { // 处理保存记忆 if ismemoryCommand { - app.handleSaveMemory(message, conversationID, parentMessageID) + app.handleSaveMemory(message, conversationID, parentMessageID) // 适配群 return } @@ -539,13 +583,13 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) { // 处理记忆列表 if ismemoryLoadCommand { - app.handleMemoryList(message) + app.handleMemoryList(message) // 适配群 return } // 新增处理载入记忆的逻辑 if isPrefixedMemoryLoadCommand { - app.handleLoadMemory(message, checkResetCommand) + app.handleLoadMemory(message, checkResetCommand) // 适配群 return } @@ -563,7 +607,7 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) { // 处理新对话 if isnewConversationCommand { - app.handleNewConversation(message, conversationID, parentMessageID) + app.handleNewConversation(message, conversationID, parentMessageID) // 适配群 return } @@ -597,21 +641,29 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) { } // 关键词退出部分ExitChoicesQ - app.ProcessExitChoicesQ(promptstr, &requestmsg, &message, selfid) + app.ProcessExitChoicesQ(promptstr, &requestmsg, &message, selfid) // 适配群 // 故事模式规则 应用 PromptChoiceQ - app.ApplyPromptChoiceQ(promptstr, &requestmsg, &message) + app.ApplyPromptChoiceQ(promptstr, &requestmsg, &message) // 适配群 // 故事模式规则 应用 PromptCoverQ - app.ApplyPromptCoverQ(promptstr, &requestmsg, &message) + app.ApplyPromptCoverQ(promptstr, &requestmsg, &message) // 适配群 // promptstr 随 switchOnQ 变化 切换Q - app.ApplySwitchOnQ(&promptstr, &requestmsg, &message) + app.ApplySwitchOnQ(&promptstr, &requestmsg, &message) // 适配群 // 从数据库读取用户的剧情存档 - CustomRecord, err := app.FetchCustomRecord(message.UserID) - if err != nil { - fmt.Printf("app.FetchCustomRecord 出错: %s\n", err) + var CustomRecord *structs.CustomRecord + if config.GetGroupContext() && message.MessageType != "private" { + CustomRecord, err = app.FetchCustomRecord(message.GroupID) + if err != nil { + fmt.Printf("app.FetchCustomRecord 出错: %s\n", err) + } + } else { + CustomRecord, err = app.FetchCustomRecord(message.UserID) + if err != nil { + fmt.Printf("app.FetchCustomRecord 出错: %s\n", err) + } } // 生成场景 @@ -620,8 +672,11 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) { PromptMarksLength := config.GetPromptMarksLength(promptstr) app.GetAndSendEnv(requestmsg, promptstr, message, selfid, CustomRecord.PromptStrStat, PromptMarksLength) } - - fmtf.Printf("实际请求conversation端点内容:[%v]%v\n", message.UserID, requestmsg) + if config.GetGroupContext() && message.MessageType != "private" { + fmtf.Printf("实际请求conversation端点内容:[%v]%v\n", message.GroupID, requestmsg) + } else { + fmtf.Printf("实际请求conversation端点内容:[%v]%v\n", message.UserID, requestmsg) + } requestBody, err := json.Marshal(map[string]interface{}{ "message": requestmsg, diff --git a/applogic/memory.go b/applogic/memory.go index 3979203..ce6ece5 100644 --- a/applogic/memory.go +++ b/applogic/memory.go @@ -84,8 +84,13 @@ func GetMemoryTitle(msg string) string { func (app *App) handleSaveMemory(msg structs.OnebotGroupMessage, ConversationID string, ParentMessageID string) { conversationTitle := "2024-5-19/18:26" // 默认标题,根据实际需求可能需要调整为动态生成的时间戳 + userid := msg.UserID + if config.GetGroupContext() && msg.MessageType != "private" { + userid = msg.GroupID + } + // 添加用户记忆 - err := app.AddUserMemory(msg.UserID, ConversationID, ParentMessageID, conversationTitle) + err := app.AddUserMemory(userid, ConversationID, ParentMessageID, conversationTitle) if err != nil { log.Printf("Error saving memory: %s", err) return @@ -104,7 +109,7 @@ func (app *App) handleSaveMemory(msg structs.OnebotGroupMessage, ConversationID newTitle := GetMemoryTitle(memoryTitle) // 获取最终的记忆标题 // 更新记忆标题 - err = app.updateConversationTitle(msg.UserID, ConversationID, ParentMessageID, newTitle) + err = app.updateConversationTitle(userid, ConversationID, ParentMessageID, newTitle) if err != nil { log.Printf("Error updating conversation title: %s", err) } @@ -124,7 +129,13 @@ func (app *App) handleSaveMemory(msg structs.OnebotGroupMessage, ConversationID // 获取记忆列表 func (app *App) handleMemoryList(msg structs.OnebotGroupMessage) { - memories, err := app.GetUserMemories(msg.UserID) + + userid := msg.UserID + if config.GetGroupContext() && msg.MessageType != "private" { + userid = msg.GroupID + } + + memories, err := app.GetUserMemories(userid) if err != nil { log.Printf("Error retrieving memories: %s", err) return @@ -166,6 +177,12 @@ func (app *App) handleMemoryList(msg structs.OnebotGroupMessage) { // 载入记忆 func (app *App) handleLoadMemory(msg structs.OnebotGroupMessage, checkResetCommand string) { + + userid := msg.UserID + if config.GetGroupContext() && msg.MessageType != "private" { + userid = msg.GroupID + } + // 从配置获取载入记忆指令 memoryLoadCommands := config.GetMemoryLoadCommand() @@ -178,7 +195,7 @@ func (app *App) handleLoadMemory(msg structs.OnebotGroupMessage, checkResetComma matchTerm := strings.TrimSpace(checkResetCommand) // 获取用户记忆 - memories, err := app.GetUserMemories(msg.UserID) + memories, err := app.GetUserMemories(userid) if err != nil { log.Printf("Error retrieving memories: %s", err) app.sendMemoryResponse(msg, "获取记忆失败") @@ -200,7 +217,7 @@ func (app *App) handleLoadMemory(msg structs.OnebotGroupMessage, checkResetComma } // 载入记忆 - err = app.updateUserContextPro(msg.UserID, matchedMemory.ConversationID, matchedMemory.ParentMessageID) + err = app.updateUserContextPro(userid, matchedMemory.ConversationID, matchedMemory.ParentMessageID) if err != nil { log.Printf("Error adding memory: %s", err) app.sendMemoryResponse(msg, "载入记忆失败") @@ -266,9 +283,14 @@ func formatHistory(history []structs.Message) string { func (app *App) handleNewConversation(msg structs.OnebotGroupMessage, conversationID string, parentMessageID string) { // 使用预定义的时间戳作为会话标题 conversationTitle := "2024-5-19/18:26" // 实际应用中应使用动态生成的时间戳 + userid := msg.UserID + + if config.GetGroupContext() && msg.MessageType != "private" { + userid = msg.GroupID + } // 添加用户记忆 - err := app.AddUserMemory(msg.UserID, conversationID, parentMessageID, conversationTitle) + err := app.AddUserMemory(userid, conversationID, parentMessageID, conversationTitle) if err != nil { log.Printf("Error saving memory: %s", err) return @@ -287,14 +309,14 @@ func (app *App) handleNewConversation(msg structs.OnebotGroupMessage, conversati newTitle := GetMemoryTitle(memoryTitle) // 获取最终的记忆标题 // 更新记忆标题 - err = app.updateConversationTitle(msg.UserID, conversationID, parentMessageID, newTitle) + err = app.updateConversationTitle(userid, conversationID, parentMessageID, newTitle) if err != nil { log.Printf("Error updating conversation title: %s", err) } }() // 迁移用户到新的上下文 - app.migrateUserToNewContext(msg.UserID) + app.migrateUserToNewContext(userid) // 获取并使用配置中指定的加载记忆指令 loadCommand := config.GetMemoryLoadCommand()