Skip to content

Commit

Permalink
Merge a7af771 into 2b1b6f8
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoshinonyaruko authored Sep 6, 2024
2 parents 2b1b6f8 + a7af771 commit a76597a
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 80 deletions.
5 changes: 5 additions & 0 deletions applogic/gensokyo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions applogic/gensokyo_sp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
44 changes: 44 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions structs/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"`

Expand Down
2 changes: 2 additions & 0 deletions template/config_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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让某些信息跳过检查
Expand Down
185 changes: 105 additions & 80 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"strings"
"sync"
"time"
"unicode"

"github.com/abadojack/whatlanggo"
"github.com/google/uuid"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

// 构造请求体
Expand Down Expand Up @@ -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)
}

0 comments on commit a76597a

Please sign in to comment.