feat: DevTools 数据库监看面板 + 隧道控制 + 多项 Bug 修复
**DevTools 新增功能 (Tasks 13-14):** - 首页仪表盘添加数据库实时监看卡片 (5端口状态 + 记忆数) - 侧边栏新增数据库面板,支持自动 5 秒刷新 - 数据库面板显示 PostgreSQL/Redis/Qdrant/MinIO/NATS 端口状态 - 隧道控制按钮 (启动/停止/重启/查看状态) - 新增 API 端点: GET /api/database/status, POST /api/tunnel/:action - 更新 docs/api-reference/ API 文档 **Bug 修复 (Task 15):** - 修复 pgrep -f 自匹配导致隧道状态误判 (添加 ^ssh 锚点) - devtools/src/index.js (dashboard + database/status) - scripts/tunnel.sh (is_tunnel_running + show_status) - 修复数据库面板缺少自动刷新定时器 - 修复侧边栏数据库徽章永远 display:none - 修复僵尸进程场景下按钮死锁问题 **其他改进:** - .gitignore 添加 backend/cmd, backend/iot-debug-service/main - 前端多项改进 (登录/注册/会话/流式动画等)
This commit is contained in:
@@ -92,7 +92,11 @@ func (h *SessionHandler) Delete(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "会话不存在"})
|
||||
c.JSON(http.StatusNotFound, gin.H{
|
||||
"error": "会话不存在",
|
||||
"errorType": "session_not_found",
|
||||
"hint": "会话可能已被删除,或 Gateway 重启后内存数据已清空",
|
||||
})
|
||||
}
|
||||
|
||||
// Get 获取单个会话信息
|
||||
@@ -107,7 +111,11 @@ func (h *SessionHandler) Get(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "会话不存在"})
|
||||
c.JSON(http.StatusNotFound, gin.H{
|
||||
"error": "会话不存在",
|
||||
"errorType": "session_not_found",
|
||||
"hint": "会话可能已被删除,或 Gateway 重启后内存数据已清空",
|
||||
})
|
||||
}
|
||||
|
||||
// GetMessages 获取会话的完整消息列表
|
||||
@@ -162,7 +170,11 @@ func (h *SessionHandler) GetSession(c *gin.Context) {
|
||||
|
||||
session := h.hub.GetSession(sessionID)
|
||||
if session == nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "会话不存在"})
|
||||
c.JSON(http.StatusNotFound, gin.H{
|
||||
"error": "会话不存在",
|
||||
"errorType": "session_not_found",
|
||||
"hint": "该会话可能已断开,或 Gateway 重启后内存数据已清空",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -204,6 +204,11 @@ func (h *Hub) GetActiveSessions() []*SessionState {
|
||||
h.mu.RLock()
|
||||
defer h.mu.RUnlock()
|
||||
|
||||
// 即使没有活跃连接也返回空列表而非 nil
|
||||
if h.sessions == nil || len(h.sessions) == 0 {
|
||||
return []*SessionState{}
|
||||
}
|
||||
|
||||
result := make([]*SessionState, 0, len(h.sessions))
|
||||
for _, s := range h.sessions {
|
||||
// 返回副本避免外部修改
|
||||
@@ -220,7 +225,13 @@ func (h *Hub) GetActiveSessionsByUser() map[string][]*SessionState {
|
||||
h.mu.RLock()
|
||||
defer h.mu.RUnlock()
|
||||
|
||||
// 即使没有活跃连接也返回空 map 而非 nil
|
||||
result := make(map[string][]*SessionState)
|
||||
|
||||
if h.sessions == nil {
|
||||
return result
|
||||
}
|
||||
|
||||
for _, s := range h.sessions {
|
||||
cp := *s
|
||||
cp.RecentMessages = nil
|
||||
@@ -314,12 +325,15 @@ func (h *Hub) RecordMessage(sessionID, role, content string) {
|
||||
|
||||
// ========== 对话缓存方法 ==========
|
||||
|
||||
const maxConversationCache = 50
|
||||
|
||||
// cacheKey 生成对话缓存 key
|
||||
func cacheKey(userID, sessionID string) string {
|
||||
return fmt.Sprintf("%s:%s", userID, sessionID)
|
||||
}
|
||||
|
||||
// CacheMessage 缓存单条消息到对话历史
|
||||
// 应对外暴露:由 gateway chat handler 在收到用户消息和 AI 回复时调用
|
||||
func (h *Hub) CacheMessage(userID, sessionID string, msg Message) {
|
||||
key := cacheKey(userID, sessionID)
|
||||
|
||||
@@ -332,6 +346,12 @@ func (h *Hub) CacheMessage(userID, sessionID string, msg Message) {
|
||||
messages = existing.([]Message)
|
||||
}
|
||||
messages = append(messages, msg)
|
||||
|
||||
// 限制缓存消息数量上限
|
||||
if len(messages) > maxConversationCache {
|
||||
messages = messages[len(messages)-maxConversationCache:]
|
||||
}
|
||||
|
||||
h.conversationCache.Store(key, messages)
|
||||
}
|
||||
|
||||
@@ -351,6 +371,20 @@ func (h *Hub) GetConversation(userID, sessionID string) []Message {
|
||||
return messages
|
||||
}
|
||||
|
||||
// GetSessionHistory 获取会话历史消息(限制条数)
|
||||
func (h *Hub) GetSessionHistory(userID, sessionID string, limit int) []Message {
|
||||
messages := h.GetConversation(userID, sessionID)
|
||||
if len(messages) == 0 {
|
||||
return []Message{}
|
||||
}
|
||||
|
||||
if limit > 0 && len(messages) > limit {
|
||||
start := len(messages) - limit
|
||||
return messages[start:]
|
||||
}
|
||||
return messages
|
||||
}
|
||||
|
||||
// DeleteConversation 删除对话缓存
|
||||
func (h *Hub) DeleteConversation(userID, sessionID string) {
|
||||
key := cacheKey(userID, sessionID)
|
||||
|
||||
Reference in New Issue
Block a user