Skip to content

Commit

Permalink
Beta136 (#135)
Browse files Browse the repository at this point in the history
* beta1

* beta2

* beta3

* beta4

* beta5

* beta6

* beta7

* beta8

* beta9

* beta10

* beta11

* beta12

* beta13

* beta14

* beta15

* beta16

* beta16

* beta19

* beta20

* beta21

* beta22

* beta23

* beta24

* beta25

* beta27

* beta28

* beta29

* beta30

* beta31

* beta33

* beta34

* beta35

* beta36

* beta37

* beta38

* beta39

* beta40

* beta41

* beta42

* beta43

* beta44

* beta45

* beta45

* beta46

* beat48

* beta49

* beta50

* beta51

* beta52

* beta53

* beta54

* beta55

* beta57

* beta58

* beta59

* beta61

* beta62

* beta63

* beta63

* beta64

* beta65

* beta66

* beta67

* beta70

* beta71

* beta72

* beta72

* beta74

* beta75

* beta76

* beta77

* beta78

* beta79

* beta80

* beta81

* beta82

* beta83

* beta85

* beta86

* beta87

* beta88

* beta89

* beta90

* beta91

* beta92

* beta93

* beta94

* beta94

* beta96

* beta97

* beta98

* beta99

* beta100

* beta101

* beta102

* beta104

* beta105

* beta106

* beta107

* beta108

* beta109

* beta110

* beta111

* beta112

* beta113

* beta115

* beta116

* beta117

* beta118

* beta119

* beta120

* beta121

* beta122

* beta123

* beta124

* beta125

* beta126

* beta127

* beta128

* beta129

* beta130

* beta131

* beta132

* beta1333

* beta134

* beta135

* beta136
  • Loading branch information
Hoshinonyaruko authored Jun 3, 2024
1 parent 89745a2 commit 9473012
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 48 deletions.
65 changes: 52 additions & 13 deletions applogic/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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() {
Expand All @@ -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 处理基于关键词的退出逻辑。
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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)
}
}
Expand All @@ -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 {
// 获取系统历史,但不包括系统消息
Expand All @@ -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 ""
Expand Down
109 changes: 82 additions & 27 deletions applogic/gensokyo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
}
Expand All @@ -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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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() {
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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)
}
}

// 生成场景
Expand All @@ -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,
Expand Down
Loading

0 comments on commit 9473012

Please sign in to comment.