|
|
|
@@ -90,11 +90,20 @@ func (s *ConversationService) CreateConversation(userID uint, req *request.Creat
|
|
|
|
|
|
|
|
|
|
|
|
// 如果角色有开场白,创建开场白消息
|
|
|
|
// 如果角色有开场白,创建开场白消息
|
|
|
|
if character.FirstMes != "" {
|
|
|
|
if character.FirstMes != "" {
|
|
|
|
|
|
|
|
// 应用输出阶段正则脚本处理开场白
|
|
|
|
|
|
|
|
processedFirstMes := character.FirstMes
|
|
|
|
|
|
|
|
var regexService RegexScriptService
|
|
|
|
|
|
|
|
outputScripts, err := regexService.GetScriptsForPlacement(userID, 1, &character.ID, nil)
|
|
|
|
|
|
|
|
if err == nil && len(outputScripts) > 0 {
|
|
|
|
|
|
|
|
processedFirstMes = regexService.ExecuteScripts(outputScripts, processedFirstMes, "", character.Name)
|
|
|
|
|
|
|
|
global.GVA_LOG.Info(fmt.Sprintf("开场白应用正则脚本: 原始长度=%d, 处理后长度=%d", len(character.FirstMes), len(processedFirstMes)))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
firstMessage := app.Message{
|
|
|
|
firstMessage := app.Message{
|
|
|
|
ConversationID: conversation.ID,
|
|
|
|
ConversationID: conversation.ID,
|
|
|
|
Role: "assistant",
|
|
|
|
Role: "assistant",
|
|
|
|
Content: character.FirstMes,
|
|
|
|
Content: processedFirstMes,
|
|
|
|
TokenCount: len(character.FirstMes) / 4,
|
|
|
|
TokenCount: len(processedFirstMes) / 4,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = global.GVA_DB.Create(&firstMessage).Error
|
|
|
|
err = global.GVA_DB.Create(&firstMessage).Error
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
@@ -316,12 +325,27 @@ func (s *ConversationService) SendMessage(userID, conversationID uint, req *requ
|
|
|
|
return nil, errors.New("角色卡不存在")
|
|
|
|
return nil, errors.New("角色卡不存在")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 应用输入阶段的正则脚本 (Placement 0)
|
|
|
|
|
|
|
|
processedContent := req.Content
|
|
|
|
|
|
|
|
var regexService RegexScriptService
|
|
|
|
|
|
|
|
global.GVA_LOG.Info(fmt.Sprintf("查询输入阶段正则脚本: userID=%d, placement=0, charID=%d", userID, conversation.CharacterID))
|
|
|
|
|
|
|
|
inputScripts, err := regexService.GetScriptsForPlacement(userID, 0, &conversation.CharacterID, nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
global.GVA_LOG.Error(fmt.Sprintf("查询输入阶段正则脚本失败: %v", err))
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
global.GVA_LOG.Info(fmt.Sprintf("找到 %d 个输入阶段正则脚本", len(inputScripts)))
|
|
|
|
|
|
|
|
if len(inputScripts) > 0 {
|
|
|
|
|
|
|
|
processedContent = regexService.ExecuteScripts(inputScripts, processedContent, "", character.Name)
|
|
|
|
|
|
|
|
global.GVA_LOG.Info(fmt.Sprintf("应用了 %d 个输入阶段正则脚本,原文: %s, 处理后: %s", len(inputScripts), req.Content, processedContent))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 保存用户消息
|
|
|
|
// 保存用户消息
|
|
|
|
userMessage := app.Message{
|
|
|
|
userMessage := app.Message{
|
|
|
|
ConversationID: conversationID,
|
|
|
|
ConversationID: conversationID,
|
|
|
|
Role: "user",
|
|
|
|
Role: "user",
|
|
|
|
Content: req.Content,
|
|
|
|
Content: processedContent,
|
|
|
|
TokenCount: len(req.Content) / 4, // 简单估算
|
|
|
|
TokenCount: len(processedContent) / 4, // 简单估算
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = global.GVA_DB.Create(&userMessage).Error
|
|
|
|
err = global.GVA_DB.Create(&userMessage).Error
|
|
|
|
@@ -372,13 +396,23 @@ func (s *ConversationService) SendMessage(userID, conversationID uint, req *requ
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 应用显示阶段的正则脚本 (Placement 3)
|
|
|
|
|
|
|
|
displayContent := assistantMessage.Content
|
|
|
|
|
|
|
|
displayScripts, err := regexService.GetScriptsForPlacement(userID, 3, &conversation.CharacterID, nil)
|
|
|
|
|
|
|
|
if err == nil && len(displayScripts) > 0 {
|
|
|
|
|
|
|
|
displayContent = regexService.ExecuteScripts(displayScripts, displayContent, "", character.Name)
|
|
|
|
|
|
|
|
global.GVA_LOG.Info(fmt.Sprintf("应用了 %d 个显示阶段正则脚本", len(displayScripts)))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
resp := response.ToMessageResponse(&assistantMessage)
|
|
|
|
resp := response.ToMessageResponse(&assistantMessage)
|
|
|
|
|
|
|
|
resp.Content = displayContent // 使用处理后的显示内容
|
|
|
|
return &resp, nil
|
|
|
|
return &resp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// callAIService 调用 AI 服务
|
|
|
|
// callAIService 调用 AI 服务
|
|
|
|
func (s *ConversationService) callAIService(conversation app.Conversation, character app.AICharacter, messages []app.Message) (string, error) {
|
|
|
|
func (s *ConversationService) callAIService(conversation app.Conversation, character app.AICharacter, messages []app.Message) (string, error) {
|
|
|
|
// 获取 AI 配置
|
|
|
|
// 获取 AI 配置
|
|
|
|
|
|
|
|
|
|
|
|
var aiConfig app.AIConfig
|
|
|
|
var aiConfig app.AIConfig
|
|
|
|
var err error
|
|
|
|
var err error
|
|
|
|
|
|
|
|
|
|
|
|
@@ -521,6 +555,21 @@ func (s *ConversationService) callAIService(conversation app.Conversation, chara
|
|
|
|
}
|
|
|
|
}
|
|
|
|
global.GVA_LOG.Info(fmt.Sprintf("========== AI返回的完整内容 ==========\n%s\n==========================================", aiResponse))
|
|
|
|
global.GVA_LOG.Info(fmt.Sprintf("========== AI返回的完整内容 ==========\n%s\n==========================================", aiResponse))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 应用输出阶段的正则脚本 (Placement 1)
|
|
|
|
|
|
|
|
var regexService RegexScriptService
|
|
|
|
|
|
|
|
global.GVA_LOG.Info(fmt.Sprintf("查询输出阶段正则脚本: userID=%d, placement=1, charID=%d", conversation.UserID, conversation.CharacterID))
|
|
|
|
|
|
|
|
outputScripts, err := regexService.GetScriptsForPlacement(conversation.UserID, 1, &conversation.CharacterID, nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
global.GVA_LOG.Error(fmt.Sprintf("查询输出阶段正则脚本失败: %v", err))
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
global.GVA_LOG.Info(fmt.Sprintf("找到 %d 个输出阶段正则脚本", len(outputScripts)))
|
|
|
|
|
|
|
|
if len(outputScripts) > 0 {
|
|
|
|
|
|
|
|
originalResponse := aiResponse
|
|
|
|
|
|
|
|
aiResponse = regexService.ExecuteScripts(outputScripts, aiResponse, "", character.Name)
|
|
|
|
|
|
|
|
global.GVA_LOG.Info(fmt.Sprintf("应用了 %d 个输出阶段正则脚本,原文: %s, 处理后: %s", len(outputScripts), originalResponse, aiResponse))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return aiResponse, nil
|
|
|
|
return aiResponse, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -648,12 +697,27 @@ func (s *ConversationService) SendMessageStream(userID, conversationID uint, req
|
|
|
|
return errors.New("角色卡不存在")
|
|
|
|
return errors.New("角色卡不存在")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 应用输入阶段的正则脚本 (Placement 0)
|
|
|
|
|
|
|
|
processedContent := req.Content
|
|
|
|
|
|
|
|
var regexService RegexScriptService
|
|
|
|
|
|
|
|
global.GVA_LOG.Info(fmt.Sprintf("[流式传输] 查询输入阶段正则脚本: userID=%d, placement=0, charID=%d", userID, conversation.CharacterID))
|
|
|
|
|
|
|
|
inputScripts, err := regexService.GetScriptsForPlacement(userID, 0, &conversation.CharacterID, nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
global.GVA_LOG.Error(fmt.Sprintf("[流式传输] 查询输入阶段正则脚本失败: %v", err))
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
global.GVA_LOG.Info(fmt.Sprintf("[流式传输] 找到 %d 个输入阶段正则脚本", len(inputScripts)))
|
|
|
|
|
|
|
|
if len(inputScripts) > 0 {
|
|
|
|
|
|
|
|
processedContent = regexService.ExecuteScripts(inputScripts, processedContent, "", character.Name)
|
|
|
|
|
|
|
|
global.GVA_LOG.Info(fmt.Sprintf("[流式传输] 应用了 %d 个输入阶段正则脚本", len(inputScripts)))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 保存用户消息
|
|
|
|
// 保存用户消息
|
|
|
|
userMessage := app.Message{
|
|
|
|
userMessage := app.Message{
|
|
|
|
ConversationID: conversationID,
|
|
|
|
ConversationID: conversationID,
|
|
|
|
Role: "user",
|
|
|
|
Role: "user",
|
|
|
|
Content: req.Content,
|
|
|
|
Content: processedContent,
|
|
|
|
TokenCount: len(req.Content) / 4,
|
|
|
|
TokenCount: len(processedContent) / 4,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = global.GVA_DB.Create(&userMessage).Error
|
|
|
|
err = global.GVA_DB.Create(&userMessage).Error
|
|
|
|
@@ -768,6 +832,19 @@ func (s *ConversationService) SendMessageStream(userID, conversationID uint, req
|
|
|
|
// 打印AI返回的完整内容
|
|
|
|
// 打印AI返回的完整内容
|
|
|
|
global.GVA_LOG.Info(fmt.Sprintf("========== [流式传输] AI返回的完整内容 ==========\n%s\n==========================================", fullContent))
|
|
|
|
global.GVA_LOG.Info(fmt.Sprintf("========== [流式传输] AI返回的完整内容 ==========\n%s\n==========================================", fullContent))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 应用输出阶段的正则脚本 (Placement 1)
|
|
|
|
|
|
|
|
global.GVA_LOG.Info(fmt.Sprintf("[流式传输] 查询输出阶段正则脚本: userID=%d, placement=1, charID=%d", userID, conversation.CharacterID))
|
|
|
|
|
|
|
|
outputScripts, err := regexService.GetScriptsForPlacement(userID, 1, &conversation.CharacterID, nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
global.GVA_LOG.Error(fmt.Sprintf("[流式传输] 查询输出阶段正则脚本失败: %v", err))
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
global.GVA_LOG.Info(fmt.Sprintf("[流式传输] 找到 %d 个输出阶段正则脚本", len(outputScripts)))
|
|
|
|
|
|
|
|
if len(outputScripts) > 0 {
|
|
|
|
|
|
|
|
fullContent = regexService.ExecuteScripts(outputScripts, fullContent, "", character.Name)
|
|
|
|
|
|
|
|
global.GVA_LOG.Info(fmt.Sprintf("[流式传输] 应用了 %d 个输出阶段正则脚本", len(outputScripts)))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 保存 AI 回复
|
|
|
|
// 保存 AI 回复
|
|
|
|
assistantMessage := app.Message{
|
|
|
|
assistantMessage := app.Message{
|
|
|
|
ConversationID: conversationID,
|
|
|
|
ConversationID: conversationID,
|
|
|
|
|