Version: 0.4.4.dev.260307
feat: 🚀 增强会话管理与缓存机制 * 会话 ID 空值兜底,若 `conversation_id` 为空时自动生成 UUID * 在响应头写入 `X-Conversation-ID`,供前端使用,保持同一会话状态 perf: ⚡ 会话状态缓存优化 * 当缓存未命中但 DB 已确认/创建会话后,调用 `SetConversationStatus` 回写 Redis * 缓存写回失败时记录日志,不中断聊天主流程,确保业务流畅性 fix: 🐛 修复历史消息顺序问题与编译错误 * 修复历史消息顺序问题,保证返回的 N 条历史消息按时间正序喂给模型 * 通过反转 `created_at desc` 查询结果的切片,确保模型输入顺序正确 * 修复 `fmt.Errorf` 参数不匹配问题,修正编译错误 * 整理 `agent-cache.go` 为标准 UTF-8 编码,避免 Go 编译报错 `invalid UTF-8 encoding` feat: 🛠️ 独立构建 MCP 服务器 * 使用 `Codex` 构建独立于后端的 MCP 服务器,简化与 Codex 的协作 * 通过该服务器方便 Codex 直接测试和查看 Redis 与 MySQL 中的数据
This commit is contained in:
@@ -3,12 +3,14 @@ package service
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"github.com/LoveLosita/smartflow/backend/agent"
|
||||
"github.com/LoveLosita/smartflow/backend/conv"
|
||||
"github.com/LoveLosita/smartflow/backend/dao"
|
||||
"github.com/LoveLosita/smartflow/backend/inits"
|
||||
"github.com/cloudwego/eino/schema"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type AgentService struct {
|
||||
@@ -25,10 +27,21 @@ func NewAgentService(aiHub *inits.AIHub, repo *dao.AgentDAO, agentRedis *dao.Age
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeConversationID(chatID string) string {
|
||||
trimmed := strings.TrimSpace(chatID)
|
||||
if trimmed == "" {
|
||||
return uuid.NewString()
|
||||
}
|
||||
return trimmed
|
||||
}
|
||||
|
||||
func (s *AgentService) AgentChat(ctx context.Context, userMessage string, ifThinking bool, userID int, chatID string) (<-chan string, <-chan error) {
|
||||
//1. 创建一个输出通道
|
||||
outChan := make(chan string, 5)
|
||||
errChan := make(chan error, 1)
|
||||
//补充:会话 ID 兜底,避免上层漏传
|
||||
chatID = normalizeConversationID(chatID)
|
||||
|
||||
//2. 先确保这个会话存在(如果不存在就创建一个新的)
|
||||
//先看看缓存里面有没有这个会话
|
||||
result, err := s.agentCache.GetConversationStatus(ctx, chatID)
|
||||
@@ -49,20 +62,23 @@ func (s *AgentService) AgentChat(ctx context.Context, userMessage string, ifThin
|
||||
}
|
||||
if !innerResult {
|
||||
//如果会话不存在,先创建一个新的会话
|
||||
_, err := s.repo.CreateNewChat(userID, chatID)
|
||||
if err != nil {
|
||||
if _, err = s.repo.CreateNewChat(userID, chatID); err != nil {
|
||||
errChan <- err
|
||||
close(outChan)
|
||||
close(errChan)
|
||||
return outChan, errChan
|
||||
}
|
||||
}
|
||||
//补充:把“会话存在”状态回写缓存,后续请求可直接命中
|
||||
if err = s.agentCache.SetConversationStatus(ctx, chatID); err != nil {
|
||||
//缓存回写失败不影响主流程
|
||||
log.Printf("failed to set conversation status cache for %s: %v", chatID, err)
|
||||
}
|
||||
}
|
||||
//能走到这里,要么缓存里有这个会话,要么数据库里有这个会话了
|
||||
//4. 提取出历史消息,构建上下文
|
||||
//先尝试从缓存里拿历史消息
|
||||
var chatHistory []*schema.Message
|
||||
chatHistory, err = s.agentCache.GetHistory(ctx, chatID)
|
||||
chatHistory, err := s.agentCache.GetHistory(ctx, chatID)
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
close(outChan)
|
||||
@@ -82,8 +98,7 @@ func (s *AgentService) AgentChat(ctx context.Context, userMessage string, ifThin
|
||||
//再转换成 Eino 的消息格式
|
||||
chatHistory = conv.ToEinoMessages(histories)
|
||||
//把历史消息放到缓存里,方便下次直接拿
|
||||
err = s.agentCache.BackfillHistory(ctx, chatID, chatHistory)
|
||||
if err != nil {
|
||||
if err = s.agentCache.BackfillHistory(ctx, chatID, chatHistory); err != nil {
|
||||
errChan <- err
|
||||
close(outChan)
|
||||
close(errChan)
|
||||
@@ -93,16 +108,18 @@ func (s *AgentService) AgentChat(ctx context.Context, userMessage string, ifThin
|
||||
//3. 将用户消息异步落缓存和库
|
||||
go func() {
|
||||
//这里先不管落库成功与否了,毕竟不想因为落库失败而影响用户的聊天体验
|
||||
_ = s.agentCache.PushMessage(ctx, chatID, &schema.Message{
|
||||
Role: "user",
|
||||
bg := context.Background()
|
||||
_ = s.agentCache.PushMessage(bg, chatID, &schema.Message{
|
||||
Role: schema.User,
|
||||
Content: userMessage,
|
||||
})
|
||||
_ = s.repo.SaveChatHistory(ctx, userID, chatID, "user", userMessage)
|
||||
_ = s.repo.SaveChatHistory(bg, userID, chatID, "user", userMessage)
|
||||
}()
|
||||
|
||||
//5. 启动一个 goroutine 来处理聊天逻辑
|
||||
go func() {
|
||||
defer close(outChan) // 确保在函数结束时关闭通道
|
||||
defer close(errChan)
|
||||
//3. 调用 StreamChat 函数进行流式聊天
|
||||
fullText, err := agent.StreamChat(ctx, s.AIHub.Worker, userMessage, ifThinking, chatHistory, outChan)
|
||||
if err != nil {
|
||||
@@ -111,13 +128,13 @@ func (s *AgentService) AgentChat(ctx context.Context, userMessage string, ifThin
|
||||
}
|
||||
//4. 将 AI 的回复异步落缓存和库
|
||||
go func() {
|
||||
_ = s.agentCache.PushMessage(ctx, chatID, &schema.Message{
|
||||
Role: "assistant",
|
||||
bg := context.Background()
|
||||
_ = s.agentCache.PushMessage(bg, chatID, &schema.Message{
|
||||
Role: schema.Assistant,
|
||||
Content: fullText,
|
||||
})
|
||||
err = s.repo.SaveChatHistory(context.Background(), userID, chatID, "assistant", fullText)
|
||||
if err != nil {
|
||||
log.Printf("Failed to save chat history to database: %v", err)
|
||||
if saveErr := s.repo.SaveChatHistory(bg, userID, chatID, "assistant", fullText); saveErr != nil {
|
||||
log.Printf("failed to save chat history to database: %v", saveErr)
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
Reference in New Issue
Block a user