diff --git a/applogic/gensokyo.go b/applogic/gensokyo.go index 4db4ac4..a92b76e 100644 --- a/applogic/gensokyo.go +++ b/applogic/gensokyo.go @@ -492,8 +492,14 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) { } } - // 请求conversation api 增加当前用户上下文 - conversationID, parentMessageID, err := app.handleUserContext(message.UserID) + var conversationID, parentMessageID string + // 请求conversation api 增加当前群/用户上下文 + if config.GetGroupContext() && message.MessageType != "private" { + conversationID, parentMessageID, err = app.handleUserContext(message.GroupID) + } else { + conversationID, parentMessageID, err = app.handleUserContext(message.UserID) + } + // 使用map映射conversationID和uid gid的关系 StoreUserInfo(conversationID, message.UserID, message.GroupID, message.RealMessageType, message.MessageType) @@ -863,10 +869,18 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) { // 在SSE流结束后更新用户上下文 在这里调用gensokyo流式接口的最后一步 插推荐气泡 if lastMessageID != "" { fmtf.Printf("lastMessageID: %s\n", lastMessageID) - err := app.updateUserContext(message.UserID, lastMessageID) - if err != nil { - fmtf.Printf("Error updating user context: %v\n", err) + if config.GetGroupContext() && message.MessageType != "private" { + err := app.updateUserContext(message.GroupID, lastMessageID) + if err != nil { + fmtf.Printf("Error updating user context: %v\n", err) + } + } else { + err := app.updateUserContext(message.UserID, lastMessageID) + if err != nil { + fmtf.Printf("Error updating user context: %v\n", err) + } } + if message.RealMessageType == "group_private" || message.MessageType == "private" { if config.GetUsePrivateSSE() { @@ -960,10 +974,18 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) { // 更新用户上下文 if messageId, ok := responseData["messageId"].(string); ok { - err := app.updateUserContext(message.UserID, messageId) - if err != nil { - fmtf.Printf("Error updating user context: %v\n", err) + if config.GetGroupContext() && message.MessageType != "private" { + err := app.updateUserContext(message.GroupID, messageId) + if err != nil { + fmtf.Printf("Error updating user context: %v\n", err) + } + } else { + err := app.updateUserContext(message.UserID, messageId) + if err != nil { + fmtf.Printf("Error updating user context: %v\n", err) + } } + } } diff --git a/config/config.go b/config/config.go index 173c44c..1b9fff1 100644 --- a/config/config.go +++ b/config/config.go @@ -1122,6 +1122,16 @@ func GetNoContext() bool { return false } +// 获取GroupContext +func GetGroupContext() bool { + mu.Lock() + defer mu.Unlock() + if instance != nil { + return instance.Settings.GroupContext + } + return false +} + // 获取WithdrawCommand func GetWithdrawCommand() []string { mu.Lock() diff --git a/structs/struct.go b/structs/struct.go index e90d480..9fe5828 100644 --- a/structs/struct.go +++ b/structs/struct.go @@ -264,6 +264,7 @@ type Settings struct { UrlSendPics bool `yaml:"urlSendPics"` // 自己构造图床加速图片发送 MdPromptKeyboardAtGroup bool `yaml:"mdPromptKeyboardAtGroup"` // 群内使用md能力模拟PromptKeyboard GroupHintWords []string `yaml:"groupHintWords"` + GroupContext bool `yaml:"groupContext"` HunyuanType int `yaml:"hunyuanType"` MaxTokensHunyuan int `yaml:"maxTokensHunyuan"` diff --git a/template/config_template.go b/template/config_template.go index 051c26d..a8739a3 100644 --- a/template/config_template.go +++ b/template/config_template.go @@ -49,6 +49,7 @@ settings: hideExtraLogs : false #忽略流信息的log,提高性能 urlSendPics : false #自己构造图床加速图片发送.需配置公网ip+放通port+设置正确的selfPath groupHintWords : [] #当机器人位于群内时,需满足包含groupHintWords数组任意内容如[CQ:at,qq=2] 机器人的名字 等 + groupContext : false #群上下文 在智能体在群内时,以群为单位处理上下文. #Ws服务器配置 wsServerToken : "" #ws密钥 可以由onebotv11反向ws接入 diff --git a/utils/utils.go b/utils/utils.go index 981b1e4..85a59b9 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -981,11 +981,11 @@ func SendSSEPrivateMessage(userID int64, content string) { } if state == 20 { // 对最后一部分特殊处理 - RestoreResponses := config.GetRestoreCommand() + MemoryLoadCommand := config.GetMemoryLoadCommand() promptKeyboard := config.GetPromptkeyboard() - if len(RestoreResponses) > 0 { - selectedRestoreResponse := RestoreResponses[rand.Intn(len(RestoreResponses))] + if len(MemoryLoadCommand) > 0 { + selectedRestoreResponse := MemoryLoadCommand[rand.Intn(len(MemoryLoadCommand))] if len(promptKeyboard) > 0 { promptKeyboard[0] = selectedRestoreResponse }