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:
@@ -4,22 +4,97 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/yourname/cyrene-ai/ai-core/internal/memory"
|
||||
"github.com/yourname/cyrene-ai/ai-core/internal/model"
|
||||
"github.com/yourname/cyrene-ai/ai-core/internal/persona"
|
||||
)
|
||||
|
||||
// IoTDeviceSummary IoT设备摘要接口(避免循环依赖)
|
||||
type IoTDeviceSummary interface {
|
||||
GetName() string
|
||||
GetType() string
|
||||
GetStatus() string
|
||||
}
|
||||
|
||||
// ConversationStore 会话历史存储接口
|
||||
type ConversationStore struct {
|
||||
mu sync.RWMutex
|
||||
messages map[string][]model.LLMMessage // key = sessionID
|
||||
maxHistory int
|
||||
}
|
||||
|
||||
// NewConversationStore 创建会话历史存储
|
||||
func NewConversationStore(maxHistory int) *ConversationStore {
|
||||
return &ConversationStore{
|
||||
messages: make(map[string][]model.LLMMessage),
|
||||
maxHistory: maxHistory,
|
||||
}
|
||||
}
|
||||
|
||||
// AddMessage 添加消息到会话历史
|
||||
func (cs *ConversationStore) AddMessage(sessionID string, msg model.LLMMessage) {
|
||||
cs.mu.Lock()
|
||||
defer cs.mu.Unlock()
|
||||
|
||||
msgs := cs.messages[sessionID]
|
||||
msgs = append(msgs, msg)
|
||||
|
||||
// 限制历史长度
|
||||
if len(msgs) > cs.maxHistory {
|
||||
// 保留 system 消息在开头,只裁剪 user/assistant 消息
|
||||
cutoff := len(msgs) - cs.maxHistory
|
||||
for cutoff < len(msgs) && msgs[cutoff].Role == model.RoleSystem {
|
||||
cutoff++
|
||||
}
|
||||
if cutoff > 0 {
|
||||
msgs = msgs[cutoff:]
|
||||
}
|
||||
}
|
||||
cs.messages[sessionID] = msgs
|
||||
}
|
||||
|
||||
// GetHistory 获取会话历史
|
||||
func (cs *ConversationStore) GetHistory(sessionID string, limit int) []model.LLMMessage {
|
||||
cs.mu.RLock()
|
||||
defer cs.mu.RUnlock()
|
||||
|
||||
msgs := cs.messages[sessionID]
|
||||
if len(msgs) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
start := 0
|
||||
if limit > 0 && len(msgs) > limit {
|
||||
start = len(msgs) - limit
|
||||
}
|
||||
|
||||
result := make([]model.LLMMessage, len(msgs[start:]))
|
||||
copy(result, msgs[start:])
|
||||
return result
|
||||
}
|
||||
|
||||
// Builder 对话上下文构建器
|
||||
type Builder struct{}
|
||||
type Builder struct {
|
||||
convStore *ConversationStore
|
||||
}
|
||||
|
||||
// NewBuilder 创建上下文构建器
|
||||
func NewBuilder(convStore *ConversationStore) *Builder {
|
||||
return &Builder{convStore: convStore}
|
||||
}
|
||||
|
||||
type BuildParams struct {
|
||||
UserID string
|
||||
SessionID string
|
||||
UserMessage string
|
||||
Persona *persona.PersonaConfig
|
||||
Memories []memory.MemoryEntry
|
||||
HistoryLimit int
|
||||
UserID string
|
||||
SessionID string
|
||||
UserMessage string
|
||||
Persona *persona.PersonaConfig
|
||||
Memories []memory.MemoryEntry
|
||||
HistoryLimit int
|
||||
DeviceContext string // 注入的设备状态文本
|
||||
PendingThoughts []string // 待注入的后台思考
|
||||
}
|
||||
|
||||
// Build 构建发送给LLM的完整消息列表
|
||||
@@ -28,9 +103,23 @@ func (b *Builder) Build(ctx context.Context, params BuildParams) ([]model.LLMMes
|
||||
|
||||
// 1. 系统消息 —— 昔涟的人格Prompt
|
||||
systemPrompt := params.Persona.BuildSystemPrompt(
|
||||
params.UserID, // 后续可替换为真实用户名
|
||||
1, // 初始好感度
|
||||
params.UserID,
|
||||
1,
|
||||
)
|
||||
|
||||
// 1.1 注入设备上下文到系统消息
|
||||
if params.DeviceContext != "" {
|
||||
systemPrompt += "\n\n" + params.DeviceContext
|
||||
}
|
||||
|
||||
// 1.2 注入后台思考到系统消息(不打扰地)
|
||||
if len(params.PendingThoughts) > 0 {
|
||||
systemPrompt += "\n\n【昔涟的内心思考(仅供你参考,不要直接复述,请自然地融入对话)】\n"
|
||||
for _, thought := range params.PendingThoughts {
|
||||
systemPrompt += fmt.Sprintf("- %s\n", thought)
|
||||
}
|
||||
}
|
||||
|
||||
messages = append(messages, model.LLMMessage{
|
||||
Role: "system",
|
||||
Content: systemPrompt,
|
||||
@@ -63,8 +152,99 @@ func (b *Builder) Build(ctx context.Context, params BuildParams) ([]model.LLMMes
|
||||
return messages, nil
|
||||
}
|
||||
|
||||
// loadHistory 加载会话历史 (MVP阶段返回空,后续对接数据库)
|
||||
// loadHistory 从 ConversationStore 加载会话历史
|
||||
func (b *Builder) loadHistory(_ context.Context, sessionID string, limit int) ([]model.LLMMessage, error) {
|
||||
log.Printf("[context] 加载会话 %s 历史 (限制 %d 条) - 暂未实现持久化", sessionID, limit)
|
||||
return nil, nil
|
||||
if b.convStore == nil {
|
||||
log.Printf("[context] 会话历史存储未初始化,跳过加载")
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
history := b.convStore.GetHistory(sessionID, limit)
|
||||
if len(history) == 0 {
|
||||
log.Printf("[context] 会话 %s 无历史记录", sessionID)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
log.Printf("[context] 加载会话 %s 历史 %d 条", sessionID, len(history))
|
||||
return history, nil
|
||||
}
|
||||
|
||||
// CacheMessage 缓存消息到会话历史(供chat handler在回复后调用)
|
||||
func (b *Builder) CacheMessage(sessionID string, role model.Role, content string) {
|
||||
if b.convStore == nil {
|
||||
return
|
||||
}
|
||||
b.convStore.AddMessage(sessionID, model.LLMMessage{
|
||||
Role: role,
|
||||
Content: content,
|
||||
})
|
||||
}
|
||||
|
||||
// InjectDeviceContext 将设备状态格式化为简洁的文本注入系统上下文
|
||||
func InjectDeviceContext(devices []DeviceInfo) string {
|
||||
if len(devices) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
var sb strings.Builder
|
||||
sb.WriteString("[当前IoT设备状态]\n")
|
||||
for _, d := range devices {
|
||||
switch d.Type {
|
||||
case "light":
|
||||
if d.Status == "on" {
|
||||
sb.WriteString(fmt.Sprintf("- %s: 开启 (亮度%d%%, %s)\n", d.Name, d.Brightness, d.Color))
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf("- %s: 关闭\n", d.Name))
|
||||
}
|
||||
case "ac":
|
||||
if d.Status == "on" {
|
||||
modeLabel := acModeLabel(d.Mode)
|
||||
sb.WriteString(fmt.Sprintf("- %s: 运行中 (%s%.0f°C)\n", d.Name, modeLabel, d.Temperature))
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf("- %s: 关闭\n", d.Name))
|
||||
}
|
||||
case "curtain":
|
||||
statusLabel := "已关闭"
|
||||
if d.Status == "open" {
|
||||
statusLabel = "已打开"
|
||||
}
|
||||
sb.WriteString(fmt.Sprintf("- %s: %s\n", d.Name, statusLabel))
|
||||
case "sensor":
|
||||
sb.WriteString(fmt.Sprintf("- %s: %.1f%s\n", d.Name, d.Value, d.Unit))
|
||||
case "lock":
|
||||
statusLabel := "已锁定"
|
||||
if d.Status == "unlocked" {
|
||||
statusLabel = "已解锁"
|
||||
}
|
||||
sb.WriteString(fmt.Sprintf("- %s: %s (电量%d%%)\n", d.Name, statusLabel, d.Battery))
|
||||
}
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// DeviceInfo 设备信息(避免循环依赖的简化结构体)
|
||||
type DeviceInfo struct {
|
||||
Name string
|
||||
Type string
|
||||
Status string
|
||||
Brightness int
|
||||
Color string
|
||||
Temperature float64
|
||||
Mode string
|
||||
Value float64
|
||||
Unit string
|
||||
Battery int
|
||||
}
|
||||
|
||||
func acModeLabel(mode string) string {
|
||||
switch mode {
|
||||
case "cool":
|
||||
return "制冷"
|
||||
case "heat":
|
||||
return "制热"
|
||||
case "auto":
|
||||
return "自动"
|
||||
default:
|
||||
return mode
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user