diff --git a/applogic/gensokyo.go b/applogic/gensokyo.go index 8357b7e..ab61747 100644 --- a/applogic/gensokyo.go +++ b/applogic/gensokyo.go @@ -691,6 +691,11 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) { fmtf.Printf("繁体转换简体失败:%v", err) } + // 是否超级安全 + if config.GetSuperSafe(promptstr) == 2 { + requestmsg = utils.FilterSimplifiedChinese(requestmsg) + } + // 替换in替换词规则 if config.GetSensitiveMode() { requestmsg = acnode.CheckWordIN(requestmsg) diff --git a/applogic/gensokyo_sp.go b/applogic/gensokyo_sp.go index e7184a4..40206ac 100644 --- a/applogic/gensokyo_sp.go +++ b/applogic/gensokyo_sp.go @@ -504,6 +504,11 @@ func (app *App) GensokyoHandlerSP(w http.ResponseWriter, r *http.Request) { fmtf.Printf("繁体转换简体失败:%v", err) } + // 是否超级安全 + if config.GetSuperSafe(promptstr) == 2 { + requestmsg = utils.FilterSimplifiedChinese(requestmsg) + } + // 替换in替换词规则 if config.GetSensitiveMode() { requestmsg = acnode.CheckWordIN(requestmsg) diff --git a/config/config.go b/config/config.go index fbe7fb6..4eb2045 100644 --- a/config/config.go +++ b/config/config.go @@ -3475,6 +3475,40 @@ func getNoEmojiInternal(options ...string) int { return NoEmoji } +// 获取NoEmoji +func GetSuperSafe(options ...string) int { + mu.Lock() + defer mu.Unlock() + return getSuperSafeInternal(options...) +} + +// 内部逻辑执行函数,不处理锁,可以安全地递归调用 +func getSuperSafeInternal(options ...string) int { + // 检查是否有参数传递进来,以及是否为空字符串 + if len(options) == 0 || options[0] == "" { + if instance != nil { + return instance.Settings.SuperSafe + } + return 0 + } + + // 使用传入的 basename + basename := options[0] + SuperSafeInterface, err := prompt.GetSettingFromFilename(basename, "SuperSafe") + if err != nil { + log.Println("Error retrieving getSuperSafeInternal:", err) + return getSuperSafeInternal() // 递归调用内部函数,不传递任何参数 + } + + SuperSafe, ok := SuperSafeInterface.(int) + if !ok || SuperSafe == 0 { // 检查是否断言失败或结果为空字符串 + log.Println("Type assertion failed or empty string for getSuperSafeInternal, fetching default") + return getSuperSafeInternal() // 递归调用内部函数,不传递任何参数 + } + + return SuperSafe +} + // ModelInterceptor模型覆盖 func GetModelInterceptor() bool { mu.Lock() @@ -3528,3 +3562,13 @@ func GetStringob11() bool { } return false } + +// 获取GroupNoKeyboard +func GetGroupNoKeyboard() bool { + mu.Lock() + defer mu.Unlock() + if instance != nil { + return instance.Settings.GroupNoKeyboard + } + return false +} diff --git a/structs/struct.go b/structs/struct.go index 301b2a0..e89f8bc 100644 --- a/structs/struct.go +++ b/structs/struct.go @@ -304,6 +304,7 @@ type Settings struct { UrlSendPics bool `yaml:"urlSendPics"` // 自己构造图床加速图片发送 MdPromptKeyboardAtGroup bool `yaml:"mdPromptKeyboardAtGroup"` // 群内使用md能力模拟PromptKeyboard MdPromptKeyboardAtGroupCmds []string `yaml:"mdPromptKeyboardAtGroupCMDs"` + GroupNoKeyboard bool `yaml:"groupNoKeyboard"` GroupHintWords []string `yaml:"groupHintWords"` GroupHintChance int `yaml:"groupHintChance"` GroupContext int `yaml:"groupContext"` // 0 false 1 false 2 true @@ -313,6 +314,7 @@ type Settings struct { GroupAddCardToQ int `yaml:"groupAddCardToQ"` SpecialNameToQ []ReplacementNamePair `yaml:"specialNameToQ"` NoEmoji int `yaml:"noEmoji"` // 0 false 1 false 2 true + SuperSafe int `yaml:"superSafe"` Stringob11 bool `yaml:"stringob11"` diff --git a/template/config_template.go b/template/config_template.go index bc65e88..8250c8e 100644 --- a/template/config_template.go +++ b/template/config_template.go @@ -64,6 +64,7 @@ settings: groupAddNicknameToQ : 0 #群上下文增加message.sender.nickname到上下文(昵称)让模型能知道发送者名字 0=默认 1=false 2=true groupAddCardToQ : 0 #群上下文增加message.sender.card到上下文(群名片)让模型能知道发送者名字 0=默认 1=false 2=true noEmoji : 0 #0=默认,正常发emoji 1=正常发emoji 2=不发任何emoji + superSafe : 0 #0=默认,1=正常,2=超级安全性 specialNameToQ: #开启groupAddNicknameToQ和groupAddCardToQ时有效,应用特殊规则,让模型对某个id产生特殊称谓 - id: 12345 @@ -87,6 +88,7 @@ settings: useAIPromptkeyboard : false #使用ai生成气泡. mdPromptKeyboardAtGroup : false #QQ智能体 群内mdPromptKeyboard mdPromptKeyboardAtGroupCMDs : [] #QQ智能体 固定指令 + groupNoKeyboard : false #群内不使用按钮 #语言过滤 allowedLanguages : ["cmn"] #根据自身安全实力,酌情过滤,cmn代表中文,小写字母,[]空数组代表不限制. /gensokyo api 可传参数skip_lang_check=true让某些信息跳过检查 diff --git a/utils/utils.go b/utils/utils.go index 0dd96a2..ed8908f 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -16,6 +16,7 @@ import ( "strings" "sync" "time" + "unicode" "github.com/abadojack/whatlanggo" "github.com/google/uuid" @@ -843,98 +844,111 @@ func SendGroupMessageMdPromptKeyboardSP(groupID string, userID string, message s mdContent = message } - fmt.Println(mdContent) - - // 构建Buttons - buttons := []structs.Button{} - // 添加promptkeyboard的按钮,每个按钮一行 - for i, label := range promptkeyboard { - buttons = append(buttons, structs.Button{ - ID: fmt.Sprintf("%d", i+1), - RenderData: structs.RenderData{ - Label: label, - VisitedLabel: label, - Style: 1, - }, - Action: structs.Action{ - Type: 2, - Permission: structs.Permission{ - Type: 2, - SpecifyRoleIDs: []string{"1", "2", "3"}, + var promptKeyboardMd structs.PromptKeyboardMarkdown + if !config.GetGroupNoKeyboard() { + // 构建Buttons + buttons := []structs.Button{} + // 添加promptkeyboard的按钮,每个按钮一行 + for i, label := range promptkeyboard { + buttons = append(buttons, structs.Button{ + ID: fmt.Sprintf("%d", i+1), + RenderData: structs.RenderData{ + Label: label, + VisitedLabel: label, + Style: 1, }, - Data: label, - UnsupportTips: "请升级新版手机QQ", - Enter: true, - Reply: true, - }, - }) - } + Action: structs.Action{ + Type: 2, + Permission: structs.Permission{ + Type: 2, + SpecifyRoleIDs: []string{"1", "2", "3"}, + }, + Data: label, + UnsupportTips: "请升级新版手机QQ", + Enter: true, + Reply: true, + }, + }) + } - // 添加"重置", "撤回", "重发"按钮,它们在一个单独的行 - rowWithThreeButtons := []structs.Button{} - labels := []string{"重置", "忽略", "记忆", "载入"} + // 添加"重置", "撤回", "重发"按钮,它们在一个单独的行 + rowWithThreeButtons := []structs.Button{} + labels := []string{"重置", "忽略", "记忆", "载入"} - for i, label := range labels { - actionType := 1 - if label == "载入" { - actionType = 2 // 设置特定的 ActionType - } + for i, label := range labels { + actionType := 1 + if label == "载入" { + actionType = 2 // 设置特定的 ActionType + } - button := structs.Button{ - ID: fmt.Sprintf("%d", i+4), // 确保ID不重复 - RenderData: structs.RenderData{ - Label: label, - VisitedLabel: label, - Style: 1, - }, - Action: structs.Action{ - Type: actionType, // 使用条件变量设置的 actionType - Permission: structs.Permission{ - Type: 2, - SpecifyRoleIDs: []string{"1", "2", "3"}, + button := structs.Button{ + ID: fmt.Sprintf("%d", i+4), // 确保ID不重复 + RenderData: structs.RenderData{ + Label: label, + VisitedLabel: label, + Style: 1, }, - Data: label, - UnsupportTips: "请升级新版手机QQ", - }, - } + Action: structs.Action{ + Type: actionType, // 使用条件变量设置的 actionType + Permission: structs.Permission{ + Type: 2, + SpecifyRoleIDs: []string{"1", "2", "3"}, + }, + Data: label, + UnsupportTips: "请升级新版手机QQ", + }, + } - rowWithThreeButtons = append(rowWithThreeButtons, button) - } + rowWithThreeButtons = append(rowWithThreeButtons, button) + } - // 构建完整的PromptKeyboardMarkdown对象 - var rows []structs.Row // 初始化一个空切片来存放行 + // 构建完整的PromptKeyboardMarkdown对象 + var rows []structs.Row // 初始化一个空切片来存放行 - // GetMemoryListMD==1 将buttons添加到rows - if config.GetMemoryListMD() == 1 { - // 遍历所有按钮,并每个按钮创建一行 - for _, button := range buttons { - row := structs.Row{ - Buttons: []structs.Button{button}, // 将当前按钮加入到新行中 + // GetMemoryListMD==1 将buttons添加到rows + if config.GetMemoryListMD() == 1 { + // 遍历所有按钮,并每个按钮创建一行 + for _, button := range buttons { + row := structs.Row{ + Buttons: []structs.Button{button}, // 将当前按钮加入到新行中 + } + rows = append(rows, row) // 将新行添加到行切片中 } - rows = append(rows, row) // 将新行添加到行切片中 } - } - // 添加特定的 rowWithThreeButtons 至 rows 数组的末尾 - row := structs.Row{ - Buttons: rowWithThreeButtons, // 将当前三个按钮放入 - } - rows = append(rows, row) + // 添加特定的 rowWithThreeButtons 至 rows 数组的末尾 + row := structs.Row{ + Buttons: rowWithThreeButtons, // 将当前三个按钮放入 + } + rows = append(rows, row) - // 构建 PromptKeyboardMarkdown 结构体 - promptKeyboardMd := structs.PromptKeyboardMarkdown{ - Markdown: structs.Markdown{ - Content: mdContent, - }, - Keyboard: structs.Keyboard{ - Content: structs.KeyboardContent{ - Rows: rows, // 使用动态创建的行数组 + // 构建 PromptKeyboardMarkdown 结构体 + promptKeyboardMd = structs.PromptKeyboardMarkdown{ + Markdown: structs.Markdown{ + Content: mdContent, }, - }, - Content: "keyboard", - MsgID: "123", - Timestamp: fmt.Sprintf("%d", time.Now().Unix()), - MsgType: 2, + Keyboard: structs.Keyboard{ + Content: structs.KeyboardContent{ + Rows: rows, // 使用动态创建的行数组 + }, + }, + Content: "keyboard", + MsgID: "123", + Timestamp: fmt.Sprintf("%d", time.Now().Unix()), + MsgType: 2, + } + + } else { + // 构建 PromptKeyboardMarkdown 结构体 + promptKeyboardMd = structs.PromptKeyboardMarkdown{ + Markdown: structs.Markdown{ + Content: mdContent, + }, + Content: "keyboard", + MsgID: "123", + Timestamp: fmt.Sprintf("%d", time.Now().Unix()), + MsgType: 2, + } } // 序列化成JSON @@ -3161,7 +3175,7 @@ func DeleteLatestMessageSP(messageType string, id string, userid string, selfid // 获取最新的有效消息ID messageID, valid := GetLatestValidMessageIDSP(userid) if !valid { - return fmt.Errorf("no valid message ID found for user/group/guild ID: %d", id) + return fmt.Errorf("no valid message ID found for user/group/guild ID: %s", id) } // 构造请求体 @@ -3280,3 +3294,14 @@ func RemoveEmojis(input string) string { return output } + +// FilterSimplifiedChinese filters the input string to keep only simplified Chinese characters +func FilterSimplifiedChinese(input string) string { + var result []rune + for _, char := range input { + if unicode.Is(unicode.Scripts["Han"], char) && char >= '\u4e00' && char <= '\u9fa5' { + result = append(result, char) + } + } + return string(result) +}