feat: Phase 1+2 架构进化 — 连续思考链/主动消息决策/情感状态机/离线自主思考 (86文件)
Phase 1 (基础设施): - ThinkChain 思考链连续性 + 差异化思考提示词 (persistent) - AutonomousToolPolicy 工具安全策略 (safe/unsafe/conditional) - MessageScheduler 自适应消息节奏 (Idle/Available/Busy) - SessionEnrichmentStore 渐进式上下文丰富 (5层) - ConversationBus 事件总线 + ResponseCache (dedup) - pkg/logger 统一日志 + 所有 handler 替换 fmt.Printf - NPE 守卫/链路优化/数据库表修复/Go workspace Phase 2 (人格交互): - EmotionState/EmotionTracker 情感状态机 (5种心情, 情绪衰减) - ProactiveGuard 主动消息多维决策 (静默时段/紧急度/频率/校验) - Gateway↔ai-core 在线状态感知链路 (presence notification) - 离线思考频率控制 + 重连问候 + 离线消息排队 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -4,7 +4,7 @@ import (
|
||||
"crypto/rand"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"github.com/yourname/cyrene-ai/pkg/logger"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -68,7 +68,7 @@ func (h *SessionHandler) Create(c *gin.Context) {
|
||||
|
||||
if h.useDB {
|
||||
if err := h.store.CreateSession(userID, req.SessionID, req.Title, req.IsMain); err != nil {
|
||||
log.Printf("[SessionHandler] 创建会话失败: %v", err)
|
||||
logger.Printf("[SessionHandler] 创建会话失败: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "创建会话失败", "errorType": "db_error"})
|
||||
return
|
||||
}
|
||||
@@ -106,7 +106,7 @@ func (h *SessionHandler) List(c *gin.Context) {
|
||||
if h.useDB {
|
||||
sessions, err := h.store.GetUserSessions(userID)
|
||||
if err != nil {
|
||||
log.Printf("[SessionHandler] 查询会话列表失败: %v", err)
|
||||
logger.Printf("[SessionHandler] 查询会话列表失败: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "查询会话失败", "errorType": "db_error"})
|
||||
return
|
||||
}
|
||||
@@ -139,7 +139,7 @@ func (h *SessionHandler) Get(c *gin.Context) {
|
||||
if h.useDB {
|
||||
session, err := h.store.GetSession(sessionID)
|
||||
if err != nil {
|
||||
log.Printf("[SessionHandler] 查询会话失败: %v", err)
|
||||
logger.Printf("[SessionHandler] 查询会话失败: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "查询会话失败", "errorType": "db_error"})
|
||||
return
|
||||
}
|
||||
@@ -186,7 +186,7 @@ func (h *SessionHandler) Delete(c *gin.Context) {
|
||||
// 所有权校验:先获取session再验证归属
|
||||
session, err := h.store.GetSession(sessionID)
|
||||
if err != nil {
|
||||
log.Printf("[SessionHandler] 查询会话失败: %v", err)
|
||||
logger.Printf("[SessionHandler] 查询会话失败: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "删除会话失败", "errorType": "db_error"})
|
||||
return
|
||||
}
|
||||
@@ -203,7 +203,7 @@ func (h *SessionHandler) Delete(c *gin.Context) {
|
||||
}
|
||||
|
||||
if err := h.store.DeleteSession(sessionID); err != nil {
|
||||
log.Printf("[SessionHandler] 删除会话失败: %v", err)
|
||||
logger.Printf("[SessionHandler] 删除会话失败: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "删除会话失败", "errorType": "db_error"})
|
||||
return
|
||||
}
|
||||
@@ -235,7 +235,7 @@ func (h *SessionHandler) DeleteAll(c *gin.Context) {
|
||||
|
||||
if h.useDB {
|
||||
if err := h.store.DeleteAllUserSessions(userID); err != nil {
|
||||
log.Printf("[SessionHandler] 删除用户所有会话失败: %v", err)
|
||||
logger.Printf("[SessionHandler] 删除用户所有会话失败: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "删除会话失败", "errorType": "db_error"})
|
||||
return
|
||||
}
|
||||
@@ -254,7 +254,7 @@ func (h *SessionHandler) GetMessages(c *gin.Context) {
|
||||
if h.useDB {
|
||||
session, err := h.store.GetSession(sessionID)
|
||||
if err != nil {
|
||||
log.Printf("[SessionHandler] 查询会话失败: %v", err)
|
||||
logger.Printf("[SessionHandler] 查询会话失败: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "查询消息失败", "errorType": "db_error"})
|
||||
return
|
||||
}
|
||||
@@ -302,7 +302,7 @@ func (h *SessionHandler) GetMessages(c *gin.Context) {
|
||||
if h.useDB {
|
||||
messages, err := h.store.GetMessages(sessionID, limit, offset)
|
||||
if err != nil {
|
||||
log.Printf("[SessionHandler] 查询消息失败: %v", err)
|
||||
logger.Printf("[SessionHandler] 查询消息失败: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "查询消息失败", "errorType": "db_error"})
|
||||
return
|
||||
}
|
||||
@@ -313,6 +313,7 @@ func (h *SessionHandler) GetMessages(c *gin.Context) {
|
||||
"id": m.ID,
|
||||
"session_id": m.SessionID,
|
||||
"role": m.Role,
|
||||
"msg_type": m.MsgType,
|
||||
"content": m.Content,
|
||||
"created_at": m.CreatedAt.UnixMilli(),
|
||||
})
|
||||
@@ -339,7 +340,7 @@ func (h *SessionHandler) ClearMessages(c *gin.Context) {
|
||||
// 所有权校验
|
||||
session, err := h.store.GetSession(sessionID)
|
||||
if err != nil {
|
||||
log.Printf("[SessionHandler] 查询会话失败: %v", err)
|
||||
logger.Printf("[SessionHandler] 查询会话失败: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "清空消息失败", "errorType": "db_error"})
|
||||
return
|
||||
}
|
||||
@@ -356,7 +357,7 @@ func (h *SessionHandler) ClearMessages(c *gin.Context) {
|
||||
}
|
||||
|
||||
if err := h.store.ClearSessionMessages(sessionID); err != nil {
|
||||
log.Printf("[SessionHandler] 清空消息失败: %v", err)
|
||||
logger.Printf("[SessionHandler] 清空消息失败: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "清空消息失败", "errorType": "db_error"})
|
||||
return
|
||||
}
|
||||
@@ -469,7 +470,7 @@ func (h *SessionHandler) SearchMessages(c *gin.Context) {
|
||||
|
||||
results, total, err := h.store.SearchMessages(userID, query, limit, offset)
|
||||
if err != nil {
|
||||
log.Printf("[SessionHandler] 搜索消息失败: %v", err)
|
||||
logger.Printf("[SessionHandler] 搜索消息失败: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "搜索失败", "errorType": "db_error"})
|
||||
return
|
||||
}
|
||||
@@ -531,7 +532,7 @@ func (h *SessionHandler) ExportSession(c *gin.Context) {
|
||||
// 获取会话信息
|
||||
session, err := h.store.GetSession(sessionID)
|
||||
if err != nil {
|
||||
log.Printf("[SessionHandler] 查询会话失败: %v", err)
|
||||
logger.Printf("[SessionHandler] 查询会话失败: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "查询会话失败", "errorType": "db_error"})
|
||||
return
|
||||
}
|
||||
@@ -552,7 +553,7 @@ func (h *SessionHandler) ExportSession(c *gin.Context) {
|
||||
// 获取所有消息 (不限制数量,导出全部)
|
||||
messages, err := h.store.GetMessages(sessionID, 0, 0)
|
||||
if err != nil {
|
||||
log.Printf("[SessionHandler] 查询消息失败: %v", err)
|
||||
logger.Printf("[SessionHandler] 查询消息失败: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "查询消息失败", "errorType": "db_error"})
|
||||
return
|
||||
}
|
||||
@@ -613,7 +614,7 @@ func (h *SessionHandler) exportJSON(c *gin.Context, session *store.Session, mess
|
||||
|
||||
jsonBytes, err := json.MarshalIndent(data, "", " ")
|
||||
if err != nil {
|
||||
log.Printf("[SessionHandler] JSON序列化失败: %v", err)
|
||||
logger.Printf("[SessionHandler] JSON序列化失败: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "导出失败", "errorType": "serialization_error"})
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user