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:
LoveLosita
2026-03-07 15:25:40 +08:00
parent 204e78d1fe
commit 26c350f378
27 changed files with 2274 additions and 17 deletions

View File

@@ -3,11 +3,13 @@ package api
import (
"io"
"net/http"
"strings"
"github.com/LoveLosita/smartflow/backend/model"
"github.com/LoveLosita/smartflow/backend/respond"
"github.com/LoveLosita/smartflow/backend/service"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
type AgentHandler struct {
@@ -33,9 +35,18 @@ func (api *AgentHandler) ChatAgent(c *gin.Context) {
c.JSON(http.StatusBadRequest, respond.WrongParamType)
return
}
// 兼容:如果前端没传会话 ID后端兜底创建一个
conversationID := strings.TrimSpace(req.ConversationID)
if conversationID == "" {
conversationID = uuid.NewString()
}
// 把最终生效的会话 ID 回传给前端,方便后续继续同一会话
c.Writer.Header().Set("X-Conversation-ID", conversationID)
userID := c.GetInt("user_id") // 从上下文中获取用户 ID
// 3. 调用 Service 层的聊天方法,获取输出通道和错误通道
outChan, errChan := api.svc.AgentChat(c.Request.Context(), req.Message, req.Thinking, userID, req.ConversationID)
outChan, errChan := api.svc.AgentChat(c.Request.Context(), req.Message, req.Thinking, userID, conversationID)
// 4. 循环转发消息/错误
c.Stream(func(w io.Writer) bool {
select {