fix: 修复 AI 回复无法送达发送者 + 重复消息 + action角色泄露 + OS环境支持
广播逻辑重构: - AI 回复 (stream_start/response/stream_segments/multi_message/stream_end) 改用 broadcastToUser 发送给所有客户端 - 用户消息回显保持 broadcastToUserExcept 排除发送者 消息去重与角色修复: - CacheMessage(user) 移至回复生成后,避免本轮 LLM 调用出现重复用户消息 - action 角色消息在 DB 存储时映射为 assistant,DeepSeek 等模型不支持自定义角色 - stream_end defer 机制确保错误路径也会终止客户端思考指示器 OS 完整环境支持: - host 包重构为 HostBackend 接口 + Direct/WSL/Docker 三种后端 - 新增 os_exec/os_file/os_system 工具供 AI 在完整 Linux 环境中自由操作 其他: - 视觉模型注入 + 图片预处理后清空 Images 避免传给 Chat 模型 - 图片 URL 相对路径→绝对 URL 转换 - DevTools 链路追踪页面 + 重启修复 - 记忆搜索模糊匹配增强 - 后台思考定时调度支持 - 管理后台页面 (模型配置/用户管理等) - docs/api 更新广播机制说明 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -52,6 +52,7 @@ platform_configs.json
|
||||
# ========== 文档 (项目规范:docs/ 不纳入版本管理,docs/api/ 为例外) ==========
|
||||
docs/*
|
||||
!docs/api/
|
||||
!docs/deploy/
|
||||
|
||||
# ========== 调试临时文件 (项目规范:debug/cache/ 为临时脚本目录) ==========
|
||||
debug/cache/
|
||||
|
||||
@@ -76,3 +76,14 @@ ALLOWED_ORIGINS=http://localhost:5173,http://localhost:5199,http://localhost:300
|
||||
MEMORY_FILE_PATH=./data/memory
|
||||
VECTOR_DB_URL=http://localhost:6333
|
||||
VECTOR_DB_COLLECTION=cyrene_memories
|
||||
|
||||
# ========== 完整 OS 环境 (供 os_exec/os_file/os_system 工具) ==========
|
||||
# 后端选择: direct (默认,仅沙箱), wsl (WSL2 完整Linux), docker (Docker容器)
|
||||
HOST_EXEC_BACKEND=wsl
|
||||
WSL_DISTRO=Ubuntu-22.04
|
||||
# WSL 内自动创建的用户 (首次调用时自动创建,已存在则跳过)
|
||||
WSL_USER=cyrene
|
||||
WSL_USER_PASSWORD=cyrene
|
||||
SANDBOX_CONTAINER=cyrene-sandbox
|
||||
SANDBOX_IMAGE=ubuntu:22.04
|
||||
HOST_EXEC_MAX_TIMEOUT=300
|
||||
|
||||
+104
-7
@@ -133,6 +133,7 @@ func main() {
|
||||
adminUserID := "admin"
|
||||
adminSessionID := "admin-session-main"
|
||||
if cfg.DatabaseURL != "" {
|
||||
convStore.SetDatabaseURL(cfg.DatabaseURL)
|
||||
if err := convStore.LoadFromDB(cfg.DatabaseURL, adminSessionID, 50); err != nil {
|
||||
log.Printf("⚠ 从数据库恢复会话历史失败(不影响服务启动): %v", err)
|
||||
}
|
||||
@@ -151,13 +152,22 @@ func main() {
|
||||
log.Println("IoT 客户端未配置 (IOT_SERVICE_URL 和 IOT_DEBUG_SERVICE_URL 均为空)")
|
||||
}
|
||||
|
||||
// 初始化主机操控管理器 (Phase 6.2: 沙箱执行 + 文件系统隔离)
|
||||
// 初始化主机操控管理器 (沙箱执行 + 文件系统隔离)
|
||||
hostSandbox := host.NewSandbox(host.DefaultSandboxConfig())
|
||||
hostManager := host.NewManager(hostSandbox)
|
||||
directBackend := host.NewDirectBackend(hostSandbox)
|
||||
hostManager := host.NewManager(directBackend)
|
||||
dataDir := getEnv("DATA_DIR", "/tmp/cyrene_data")
|
||||
hostManager.SetAllowedDirs([]string{dataDir, os.TempDir(), "."})
|
||||
log.Printf("主机操控管理器已就绪: 沙箱执行 + 文件隔离 (数据目录=%s)", dataDir)
|
||||
|
||||
// 初始化完整OS环境管理器 (WSL/Docker,无沙箱限制,供 os_* 工具使用)
|
||||
osManager := createOSManager()
|
||||
if osManager != nil {
|
||||
log.Printf("完整OS环境管理器已就绪: backend=%s", osManager.BackendName())
|
||||
} else {
|
||||
log.Println("完整OS环境管理器未配置 (设置 HOST_EXEC_BACKEND=wsl 或 docker 以启用)")
|
||||
}
|
||||
|
||||
// 初始化 RAG 知识库 (Phase 6.6: 知识库 RAG 增强)
|
||||
knowledgeDir := getEnv("KNOWLEDGE_DIR", "./data/knowledge")
|
||||
ragEmbedder := rag.NewEmbedder(cfg.LLMBaseURL, cfg.LLMAPIKey, "text-embedding-3-small")
|
||||
@@ -167,6 +177,7 @@ func main() {
|
||||
|
||||
// 初始化工具注册中心 (使用共享插件模块)
|
||||
toolRegistry := plgManager.NewToolRegistry()
|
||||
var visionProvider llm.LLMProvider
|
||||
if getEnvBool("ENABLE_TOOLS", true) {
|
||||
// 11 个共享通用插件 — 注册其工具到统一注册中心
|
||||
registerPluginTools(toolRegistry, &pluginCalc.CalculatorPlugin{})
|
||||
@@ -198,7 +209,13 @@ func main() {
|
||||
toolRegistry.Register(wrapTool(tools.NewHostSystemTool(hostManager), "host_system", "Host System Info", "system"))
|
||||
}
|
||||
|
||||
var visionProvider llm.LLMProvider
|
||||
if osManager != nil {
|
||||
toolRegistry.Register(wrapTool(tools.NewOSExecTool(osManager), "os_exec", "OS Command Execution", "system"))
|
||||
toolRegistry.Register(wrapTool(tools.NewOSFileTool(osManager), "os_file", "OS File Operations", "system"))
|
||||
toolRegistry.Register(wrapTool(tools.NewOSSystemTool(osManager), "os_system", "OS System Info", "system"))
|
||||
}
|
||||
|
||||
visionProvider = nil
|
||||
if configLoader != nil && configLoader.HasConfig() {
|
||||
cfg := configLoader.GetConfig()
|
||||
if route, ok := cfg.Routing["vision"]; ok && len(route.FallbackChain) > 0 {
|
||||
@@ -300,7 +317,9 @@ func main() {
|
||||
// 注册子会话提供者
|
||||
subManager.Register(subsession.NewGeneralProvider(personaLoader))
|
||||
if memRetriever != nil {
|
||||
subManager.Register(subsession.NewMemoryProvider(memRetriever))
|
||||
memProvider := subsession.NewMemoryProvider(memRetriever)
|
||||
memProvider.SetFuzzySearch(memoryAdapter, memClient)
|
||||
subManager.Register(memProvider)
|
||||
}
|
||||
if iotClient != nil {
|
||||
subManager.Register(subsession.NewIoTProvider(iotClient, personaDir))
|
||||
@@ -322,6 +341,10 @@ func main() {
|
||||
memExtractor,
|
||||
)
|
||||
orch.SetToolRegistry(toolRegistry)
|
||||
if visionProvider != nil {
|
||||
orch.SetVisionProvider(visionProvider)
|
||||
log.Printf("对话编排器: 视觉模型已注入 (%s)", visionProvider.ModelName())
|
||||
}
|
||||
log.Println("对话编排器 v2.0 已就绪")
|
||||
_ = orch
|
||||
|
||||
@@ -428,6 +451,32 @@ func main() {
|
||||
json.NewEncoder(w).Encode(toolRegistry.GetCallStats())
|
||||
})
|
||||
|
||||
// OS 环境监控端点
|
||||
mux.HandleFunc("/api/v1/system/info", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
result := map[string]interface{}{
|
||||
"os_enabled": osManager != nil,
|
||||
}
|
||||
if osManager != nil {
|
||||
result["backend"] = osManager.BackendName()
|
||||
result["system"] = osManager.SystemInfo()
|
||||
if disk, err := osManager.DiskUsage("/"); err == nil {
|
||||
result["disk"] = disk
|
||||
}
|
||||
}
|
||||
if hostManager != nil {
|
||||
result["host"] = map[string]interface{}{
|
||||
"backend": hostManager.BackendName(),
|
||||
"system": hostManager.SystemInfo(),
|
||||
}
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(result)
|
||||
})
|
||||
|
||||
// 启动HTTP服务
|
||||
srv := &http.Server{
|
||||
Addr: ":" + cfg.Port,
|
||||
@@ -525,6 +574,42 @@ func getEnvBool(key string, fallback bool) bool {
|
||||
}
|
||||
}
|
||||
|
||||
// createOSManager 根据 HOST_EXEC_BACKEND 环境变量创建完整OS环境管理器。
|
||||
// 支持 "wsl" 和 "docker" 两种后端。返回 nil 表示未配置或配置无效。
|
||||
func createOSManager() *host.Manager {
|
||||
backend := strings.ToLower(os.Getenv("HOST_EXEC_BACKEND"))
|
||||
switch backend {
|
||||
case "wsl":
|
||||
distro := getEnv("WSL_DISTRO", "Ubuntu-22.04")
|
||||
username := getEnv("WSL_USER", "cyrene")
|
||||
password := os.Getenv("WSL_USER_PASSWORD")
|
||||
maxTimeout := time.Duration(getEnvInt("HOST_EXEC_MAX_TIMEOUT", 300)) * time.Second
|
||||
wslBackend := host.NewWSLBackend(distro, username, password, maxTimeout)
|
||||
return host.NewManager(wslBackend)
|
||||
case "docker":
|
||||
container := getEnv("SANDBOX_CONTAINER", "cyrene-sandbox")
|
||||
image := getEnv("SANDBOX_IMAGE", "ubuntu:22.04")
|
||||
maxTimeout := time.Duration(getEnvInt("HOST_EXEC_MAX_TIMEOUT", 300)) * time.Second
|
||||
dockerBackend := host.NewDockerBackend(container, image, maxTimeout)
|
||||
return host.NewManager(dockerBackend)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// getEnvInt 获取整数类型的环境变量
|
||||
func getEnvInt(key string, fallback int) int {
|
||||
v := os.Getenv(key)
|
||||
if v == "" {
|
||||
return fallback
|
||||
}
|
||||
n, err := strconv.Atoi(v)
|
||||
if err != nil || n <= 0 {
|
||||
return fallback
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// registerPluginTools 从插件实例注册其所有工具到注册中心
|
||||
func registerPluginTools(registry *plgManager.ToolRegistry, plugin plgSDK.Plugin) {
|
||||
for _, t := range plugin.Tools() {
|
||||
@@ -638,9 +723,6 @@ func handleChat(
|
||||
userNickname = cfg.AdminNickname
|
||||
}
|
||||
|
||||
// 0.1 缓存用户消息到会话历史
|
||||
ctxBuilder.CacheMessage(req.SessionID, model.RoleUser, req.Message)
|
||||
|
||||
// 1. 设置 SSE 响应头
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
w.Header().Set("Cache-Control", "no-cache")
|
||||
@@ -678,6 +760,19 @@ func handleChat(
|
||||
var fullContent string
|
||||
for event := range eventCh {
|
||||
switch event.Type {
|
||||
case model.StreamToolProgress:
|
||||
tp := event.ToolProgress
|
||||
progressData, _ := json.Marshal(map[string]interface{}{
|
||||
"type": "tool_progress",
|
||||
"tool_name": tp.ToolName,
|
||||
"status": tp.Status,
|
||||
"progress": tp.Progress,
|
||||
"message": tp.Message,
|
||||
"message_id": messageID,
|
||||
})
|
||||
fmt.Fprintf(w, "data: %s\n\n", progressData)
|
||||
flusher.Flush()
|
||||
|
||||
case model.StreamError:
|
||||
log.Printf("[chat] 流式错误: %v", event.Error)
|
||||
errData, _ := json.Marshal(map[string]string{"delta": "", "error": event.Error.Error()})
|
||||
@@ -729,6 +824,8 @@ func handleChat(
|
||||
}
|
||||
}
|
||||
|
||||
// 缓存用户消息到会话历史(在回复生成后,避免本轮 LLM 调用出现重复用户消息)
|
||||
ctxBuilder.CacheMessage(req.SessionID, model.RoleUser, req.Message)
|
||||
// 4. 对话完成后触发昔涟的自主思考(事件驱动,非定时)
|
||||
if thinker != nil {
|
||||
thinker.TriggerPostChatThink()
|
||||
|
||||
@@ -582,16 +582,7 @@ func (t *Thinker) performThink(triggerReason string) {
|
||||
return
|
||||
}
|
||||
|
||||
// 2. 检索相关记忆
|
||||
var memories []memory.MemoryEntry
|
||||
if t.memRetriever != nil {
|
||||
memories, err = t.memRetriever.Retrieve(ctx, t.adminUserID, "最近发生了什么 重要的事情 用户偏好 个人信息")
|
||||
if err != nil {
|
||||
log.Printf("[后台思考] 记忆检索失败: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 获取当前活跃会话的对话历史(优先活跃会话,回退到管理员主会话)
|
||||
// 2. 获取当前活跃会话的对话历史(优先活跃会话,回退到管理员主会话)
|
||||
var convHistory []model.LLMMessage
|
||||
if t.convStore != nil {
|
||||
t.mu.Lock()
|
||||
@@ -608,6 +599,37 @@ func (t *Thinker) performThink(triggerReason string) {
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 检索相关记忆(精确检索 + 模糊搜索)
|
||||
var memories []memory.MemoryEntry
|
||||
if t.memRetriever != nil {
|
||||
memories, err = t.memRetriever.Retrieve(ctx, t.adminUserID, "最近发生了什么 重要的事情 用户偏好 个人信息")
|
||||
if err != nil {
|
||||
log.Printf("[后台思考] 记忆检索失败: %v", err)
|
||||
}
|
||||
|
||||
// 模糊搜索:从对话历史提取话题,LLM 扩展关键词后语义搜索
|
||||
if t.memClient != nil && len(convHistory) > 0 {
|
||||
fuzzyQuery := lastUserMessage(convHistory)
|
||||
if fuzzyQuery == "" {
|
||||
fuzzyQuery = "最近对话 重要事件 用户状态"
|
||||
}
|
||||
fuzzyResults := t.fuzzyMemorySearch(ctx, t.adminUserID, fuzzyQuery)
|
||||
seen := make(map[string]bool)
|
||||
for _, m := range memories {
|
||||
seen[m.ID] = true
|
||||
}
|
||||
for _, m := range fuzzyResults {
|
||||
if !seen[m.ID] {
|
||||
seen[m.ID] = true
|
||||
memories = append(memories, m)
|
||||
}
|
||||
}
|
||||
if len(fuzzyResults) > 0 {
|
||||
log.Printf("[后台思考] 模糊搜索补充 %d 条记忆", len(fuzzyResults))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 查询 IoT 设备状态(每次都查询,无间隔限制)
|
||||
var deviceSummary string
|
||||
if t.iotClient != nil {
|
||||
@@ -750,10 +772,9 @@ func (t *Thinker) performThink(triggerReason string) {
|
||||
|
||||
log.Printf("[后台思考] 完成 (触发原因=%s, 内容长度=%d, 工具调用=%d次)", triggerReason, len(finalContent), totalToolCalls)
|
||||
|
||||
// 9. 周期性记忆维护(每 10 次思考触发一次)
|
||||
// 注:不再从思考结果中提取记忆——思考内容基于已有记忆生成,
|
||||
// 再次提取会造成"读取→思考→保存→再次读取"的重复循环。
|
||||
// 9. 记忆维护:机械合并(每10次) + LLM整理(每次)
|
||||
t.maybeMaintainMemories(currentCount)
|
||||
t.performMemoryConsolidation(ctx)
|
||||
}
|
||||
|
||||
// buildThinkingSystemPrompt 构建思考用的系统提示词
|
||||
@@ -1196,6 +1217,375 @@ func (t *Thinker) maybeMaintainMemories(thinkCount int) {
|
||||
}
|
||||
}
|
||||
|
||||
// consolidationAction is a parsed memory consolidation instruction from the LLM.
|
||||
type consolidationAction struct {
|
||||
Action string `json:"action"`
|
||||
IDs []string `json:"ids,omitempty"`
|
||||
ID string `json:"id,omitempty"`
|
||||
Content string `json:"content,omitempty"`
|
||||
Category string `json:"category,omitempty"`
|
||||
Importance int `json:"importance,omitempty"`
|
||||
Priority int `json:"priority,omitempty"`
|
||||
Keywords []string `json:"keywords,omitempty"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
}
|
||||
|
||||
// performMemoryConsolidation uses LLM to review and clean up the memory store.
|
||||
// It identifies duplicates, contradictions, outdated info, and low-quality memories,
|
||||
// then executes merge/delete/update actions.
|
||||
func (t *Thinker) performMemoryConsolidation(ctx context.Context) {
|
||||
if t.memClient == nil {
|
||||
return
|
||||
}
|
||||
|
||||
allMemories, err := t.memClient.Query(ctx, model.MemoryQuery{
|
||||
UserID: t.adminUserID,
|
||||
Limit: 200,
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("[记忆整理] 获取记忆失败: %v", err)
|
||||
return
|
||||
}
|
||||
if len(allMemories) < 5 {
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("[记忆整理] LLM 审查 %d 条记忆...", len(allMemories))
|
||||
|
||||
systemPrompt := t.buildConsolidationPrompt(allMemories)
|
||||
messages := []model.LLMMessage{
|
||||
{Role: model.RoleSystem, Content: systemPrompt},
|
||||
{Role: model.RoleUser, Content: "请审查以上记忆库,找出重复、矛盾、过时和低质量的记忆,输出 JSON 整理方案。如果没有需要整理的,输出空数组 []。"},
|
||||
}
|
||||
|
||||
resp, err := t.toolAdapter.Chat(ctx, messages)
|
||||
if err != nil {
|
||||
log.Printf("[记忆整理] LLM 调用失败: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
actions := parseConsolidationActions(resp.Content)
|
||||
if len(actions) == 0 {
|
||||
log.Printf("[记忆整理] 记忆库状态良好,无需整理")
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("[记忆整理] LLM 建议 %d 项操作", len(actions))
|
||||
executed := t.executeConsolidationActions(ctx, actions, allMemories)
|
||||
log.Printf("[记忆整理] 完成: 执行了 %d 项操作", executed)
|
||||
}
|
||||
|
||||
// buildConsolidationPrompt formats all memories as a structured list for LLM review.
|
||||
func (t *Thinker) buildConsolidationPrompt(memories []model.MemoryEntry) string {
|
||||
var sb strings.Builder
|
||||
sb.WriteString("你是记忆库管理助手。审查以下用户记忆,找出问题并输出 JSON 操作清单。\n\n")
|
||||
sb.WriteString("## 需要识别的问题\n")
|
||||
sb.WriteString("1. 重复记忆 — 多条记忆记录了相同信息 → merge 合并为一条\n")
|
||||
sb.WriteString("2. 矛盾记忆 — 两条记忆互相矛盾(如\"喜欢猫\"vs\"讨厌猫\")→ delete 删除过时的、update 修正错误的\n")
|
||||
sb.WriteString("3. 过时记忆 — 信息已被新记忆取代 → delete 或 update\n")
|
||||
sb.WriteString("4. 低质量记忆 — 太模糊、不完整、无实际信息量 → delete\n\n")
|
||||
sb.WriteString("## JSON 操作格式\n")
|
||||
sb.WriteString("```json\n[\n")
|
||||
sb.WriteString(" {\"action\":\"merge\", \"ids\":[\"id1\",\"id2\"], \"content\":\"合并后的内容\", \"category\":\"personal_info\", \"importance\":8, \"reason\":\"两条记录同一件事\"},\n")
|
||||
sb.WriteString(" {\"action\":\"delete\", \"id\":\"id3\", \"reason\":\"完全被 id1 覆盖\"},\n")
|
||||
sb.WriteString(" {\"action\":\"update\", \"id\":\"id4\", \"content\":\"修正后的内容\", \"importance\":7, \"reason\":\"纠正过时信息\"},\n")
|
||||
sb.WriteString(" {\"action\":\"create\", \"content\":\"需要补充的记忆\", \"category\":\"knowledge\", \"importance\":6, \"reason\":\"从已有记忆推断\"}\n")
|
||||
sb.WriteString("]\n```\n\n")
|
||||
sb.WriteString("## 规则\n")
|
||||
sb.WriteString("- 只输出 JSON 数组,可以用 ```json``` 包裹,不要输出其他解释文字\n")
|
||||
sb.WriteString("- 确实有问题才建议操作,不要强行找问题\n")
|
||||
sb.WriteString("- merge 时保留最重要的那条的 ID,合并内容应包含各条的关键信息\n")
|
||||
sb.WriteString("- 不确定时宁可保守(不操作)\n")
|
||||
sb.WriteString("- importance 范围 1-10,数字越大越重要\n")
|
||||
sb.WriteString("- category 可选: personal_info, user_preference, conversation, knowledge, event, task, relationship\n\n")
|
||||
sb.WriteString(fmt.Sprintf("## 当前记忆库 (%d 条)\n\n", len(memories)))
|
||||
|
||||
for i, m := range memories {
|
||||
sb.WriteString(fmt.Sprintf("%d. [%s] **%s** | cat=%s imp=%d pri=%d | src=%s\n",
|
||||
i+1, m.ID[:min(8, len(m.ID))], m.Content,
|
||||
m.Category, m.Importance, m.Priority, m.Source))
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// parseConsolidationActions extracts JSON actions from LLM response text.
|
||||
func parseConsolidationActions(text string) []consolidationAction {
|
||||
// Try to extract from ```json fences first
|
||||
jsonStr := text
|
||||
if idx := strings.Index(text, "```json"); idx >= 0 {
|
||||
start := idx + 7
|
||||
if end := strings.Index(text[start:], "```"); end >= 0 {
|
||||
jsonStr = text[start : start+end]
|
||||
}
|
||||
} else if idx := strings.Index(text, "```"); idx >= 0 {
|
||||
start := idx + 3
|
||||
if end := strings.Index(text[start:], "```"); end >= 0 {
|
||||
jsonStr = text[start : start+end]
|
||||
}
|
||||
}
|
||||
// Find the JSON array
|
||||
arrStart := strings.Index(jsonStr, "[")
|
||||
arrEnd := strings.LastIndex(jsonStr, "]")
|
||||
if arrStart < 0 || arrEnd <= arrStart {
|
||||
return nil
|
||||
}
|
||||
jsonStr = jsonStr[arrStart : arrEnd+1]
|
||||
|
||||
var actions []consolidationAction
|
||||
if err := json.Unmarshal([]byte(jsonStr), &actions); err != nil {
|
||||
log.Printf("[记忆整理] JSON 解析失败: %v", err)
|
||||
return nil
|
||||
}
|
||||
return actions
|
||||
}
|
||||
|
||||
// executeConsolidationActions runs the parsed consolidation actions against the memory store.
|
||||
func (t *Thinker) executeConsolidationActions(ctx context.Context, actions []consolidationAction, memories []model.MemoryEntry) int {
|
||||
// Index memories by their short ID prefix for lookup
|
||||
memByShortID := make(map[string]*model.MemoryEntry)
|
||||
for i := range memories {
|
||||
short := memories[i].ID[:min(8, len(memories[i].ID))]
|
||||
memByShortID[short] = &memories[i]
|
||||
}
|
||||
memByFullID := make(map[string]*model.MemoryEntry)
|
||||
for i := range memories {
|
||||
memByFullID[memories[i].ID] = &memories[i]
|
||||
}
|
||||
|
||||
executed := 0
|
||||
for _, a := range actions {
|
||||
switch a.Action {
|
||||
case "delete":
|
||||
id := resolveID(a.ID, memByShortID, memByFullID)
|
||||
if id == "" {
|
||||
log.Printf("[记忆整理] 跳过 delete: 找不到记忆 %s", a.ID)
|
||||
continue
|
||||
}
|
||||
if err := t.memClient.Delete(ctx, id); err != nil {
|
||||
log.Printf("[记忆整理] 删除 %s 失败: %v", a.ID, err)
|
||||
continue
|
||||
}
|
||||
log.Printf("[记忆整理] 已删除: %s (原因: %s)", a.ID, a.Reason)
|
||||
executed++
|
||||
|
||||
case "merge":
|
||||
if len(a.IDs) < 2 {
|
||||
continue
|
||||
}
|
||||
// Resolve all IDs, use first as the keeper
|
||||
var resolved []string
|
||||
for _, mid := range a.IDs {
|
||||
if rid := resolveID(mid, memByShortID, memByFullID); rid != "" {
|
||||
resolved = append(resolved, rid)
|
||||
}
|
||||
}
|
||||
if len(resolved) < 2 {
|
||||
continue
|
||||
}
|
||||
keeper := resolved[0]
|
||||
// Update the keeper with merged content
|
||||
cat := model.MemoryCategory(a.Category)
|
||||
if cat == "" {
|
||||
if m, ok := memByFullID[keeper]; ok {
|
||||
cat = m.Category
|
||||
}
|
||||
}
|
||||
imp := a.Importance
|
||||
if imp == 0 {
|
||||
if m, ok := memByFullID[keeper]; ok {
|
||||
imp = m.Importance + 1
|
||||
}
|
||||
}
|
||||
if imp > 10 {
|
||||
imp = 10
|
||||
}
|
||||
pri := a.Priority
|
||||
if pri == 0 {
|
||||
if m, ok := memByFullID[keeper]; ok {
|
||||
pri = int(m.Priority)
|
||||
}
|
||||
}
|
||||
if err := t.memClient.Update(ctx, &model.MemoryEntry{
|
||||
ID: keeper,
|
||||
Content: a.Content,
|
||||
Category: cat,
|
||||
Importance: imp,
|
||||
Priority: model.MemoryPriority(pri),
|
||||
Keywords: a.Keywords,
|
||||
Source: "consolidated",
|
||||
}); err != nil {
|
||||
log.Printf("[记忆整理] 更新合并目标 %s 失败: %v", keeper, err)
|
||||
continue
|
||||
}
|
||||
// Delete the discarded ones
|
||||
for _, did := range resolved[1:] {
|
||||
if err := t.memClient.Delete(ctx, did); err != nil {
|
||||
log.Printf("[记忆整理] 删除被合并记忆 %s 失败: %v", did, err)
|
||||
}
|
||||
}
|
||||
log.Printf("[记忆整理] 已合并: %v -> %s (原因: %s)", resolved, keeper, a.Reason)
|
||||
executed++
|
||||
|
||||
case "update":
|
||||
id := resolveID(a.ID, memByShortID, memByFullID)
|
||||
if id == "" {
|
||||
log.Printf("[记忆整理] 跳过 update: 找不到记忆 %s", a.ID)
|
||||
continue
|
||||
}
|
||||
existing := memByFullID[id]
|
||||
cat := model.MemoryCategory(a.Category)
|
||||
if cat == "" && existing != nil {
|
||||
cat = existing.Category
|
||||
}
|
||||
imp := a.Importance
|
||||
if imp == 0 && existing != nil {
|
||||
imp = existing.Importance
|
||||
}
|
||||
pri := a.Priority
|
||||
if pri == 0 && existing != nil {
|
||||
pri = int(existing.Priority)
|
||||
}
|
||||
if err := t.memClient.Update(ctx, &model.MemoryEntry{
|
||||
ID: id,
|
||||
Content: a.Content,
|
||||
Category: cat,
|
||||
Importance: imp,
|
||||
Priority: model.MemoryPriority(pri),
|
||||
Keywords: a.Keywords,
|
||||
Source: "consolidated",
|
||||
}); err != nil {
|
||||
log.Printf("[记忆整理] 更新 %s 失败: %v", id, err)
|
||||
continue
|
||||
}
|
||||
log.Printf("[记忆整理] 已更新: %s (原因: %s)", id, a.Reason)
|
||||
executed++
|
||||
|
||||
case "create":
|
||||
cat := model.MemoryCategory(a.Category)
|
||||
if cat == "" {
|
||||
cat = model.CategoryKnowledge
|
||||
}
|
||||
imp := a.Importance
|
||||
if imp == 0 {
|
||||
imp = 5
|
||||
}
|
||||
if err := t.memClient.Save(ctx, &model.MemoryEntry{
|
||||
UserID: t.adminUserID,
|
||||
Content: a.Content,
|
||||
Category: cat,
|
||||
Importance: imp,
|
||||
Priority: model.MemoryNormal,
|
||||
Keywords: a.Keywords,
|
||||
Source: "consolidation",
|
||||
}); err != nil {
|
||||
log.Printf("[记忆整理] 创建记忆失败: %v", err)
|
||||
continue
|
||||
}
|
||||
log.Printf("[记忆整理] 已创建: %s (原因: %s)", a.Content, a.Reason)
|
||||
executed++
|
||||
}
|
||||
}
|
||||
return executed
|
||||
}
|
||||
|
||||
// resolveID tries to match a short or full ID to an existing memory.
|
||||
func resolveID(id string, byShort, byFull map[string]*model.MemoryEntry) string {
|
||||
if _, ok := byFull[id]; ok {
|
||||
return id
|
||||
}
|
||||
if m, ok := byShort[id]; ok {
|
||||
return m.ID
|
||||
}
|
||||
// Try prefix match
|
||||
for fullID := range byFull {
|
||||
if strings.HasPrefix(fullID, id) {
|
||||
return fullID
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// fuzzyMemorySearch expands the query via LLM keyword extraction and performs semantic search.
|
||||
func (t *Thinker) fuzzyMemorySearch(ctx context.Context, userID, query string) []memory.MemoryEntry {
|
||||
if t.memClient == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
keywords := t.expandMemoryKeywords(ctx, query)
|
||||
if len(keywords) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Printf("[后台思考] 模糊记忆关键词: %v", keywords)
|
||||
|
||||
var allResults []memory.MemoryEntry
|
||||
seen := make(map[string]bool)
|
||||
|
||||
for _, kw := range keywords {
|
||||
results, err := t.memClient.QueryByText(ctx, userID, kw, "", 0, 5)
|
||||
if err != nil {
|
||||
log.Printf("[后台思考] 模糊搜索 '%s' 失败: %v", kw, err)
|
||||
continue
|
||||
}
|
||||
for _, m := range results {
|
||||
if !seen[m.ID] {
|
||||
seen[m.ID] = true
|
||||
allResults = append(allResults, m)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return allResults
|
||||
}
|
||||
|
||||
// expandMemoryKeywords uses LLM to generate fuzzy/related keywords for memory search.
|
||||
func (t *Thinker) expandMemoryKeywords(ctx context.Context, message string) []string {
|
||||
prompt := fmt.Sprintf(
|
||||
"从以下对话消息中提取 3-5 个可用于模糊搜索记忆的关键词。这些关键词应该是:\n"+
|
||||
"- 与话题相关的抽象概念\n- 同义词和相关词\n- 更宽泛或更具体的相关概念\n"+
|
||||
"- 不要包含消息中已经出现的原词\n\n"+
|
||||
"用户消息:「%s」\n\n"+
|
||||
"只输出 JSON 字符串数组,例如:[\"关键词1\",\"关键词2\"]", message)
|
||||
|
||||
resp, err := t.llmAdapter.Chat(ctx, []model.LLMMessage{
|
||||
{Role: model.RoleSystem, Content: "你是记忆搜索专家。输出 JSON 字符串数组。"},
|
||||
{Role: model.RoleUser, Content: prompt},
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("[后台思考] 关键词扩展失败: %v", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
text := strings.TrimSpace(resp.Content)
|
||||
if idx := strings.Index(text, "["); idx >= 0 {
|
||||
if end := strings.LastIndex(text, "]"); end > idx {
|
||||
text = text[idx : end+1]
|
||||
}
|
||||
}
|
||||
|
||||
var keywords []string
|
||||
if err := json.Unmarshal([]byte(text), &keywords); err != nil {
|
||||
log.Printf("[后台思考] 解析关键词 JSON 失败: %v (raw=%s)", err, resp.Content)
|
||||
return nil
|
||||
}
|
||||
|
||||
return keywords
|
||||
}
|
||||
|
||||
// lastUserMessage extracts the last user message from conversation history.
|
||||
func lastUserMessage(history []model.LLMMessage) string {
|
||||
for i := len(history) - 1; i >= 0; i-- {
|
||||
if history[i].Role == model.RoleUser {
|
||||
runes := []rune(history[i].Content)
|
||||
if len(runes) > 200 {
|
||||
return string(runes[:200])
|
||||
}
|
||||
return history[i].Content
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// formatDeviceContext 格式化设备状态为文本
|
||||
func formatDeviceContext(devices []tools.IoTDevice) string {
|
||||
if len(devices) == 0 {
|
||||
|
||||
@@ -0,0 +1,184 @@
|
||||
package background
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ScheduleRule defines a time-based interval rule.
|
||||
type ScheduleRule struct {
|
||||
Name string `json:"name"`
|
||||
Days []string `json:"days"`
|
||||
TimeRange string `json:"time_range"`
|
||||
Except []string `json:"except"`
|
||||
IntervalMinutes int `json:"interval_minutes"`
|
||||
}
|
||||
|
||||
// ThinkingScheduleConfig is the full schedule configuration.
|
||||
type ThinkingScheduleConfig struct {
|
||||
Version string `json:"version"`
|
||||
DefaultIntervalMinutes int `json:"default_interval_minutes"`
|
||||
Rules []ScheduleRule `json:"rules"`
|
||||
}
|
||||
|
||||
// ScheduleLoader loads the thinking schedule from a JSON file and calculates
|
||||
// the current interval based on time of day and day of week.
|
||||
type ScheduleLoader struct {
|
||||
mu sync.RWMutex
|
||||
path string
|
||||
config *ThinkingScheduleConfig
|
||||
}
|
||||
|
||||
// NewScheduleLoader creates a loader. Returns nil config if the file does not exist.
|
||||
func NewScheduleLoader(path string) (*ScheduleLoader, error) {
|
||||
l := &ScheduleLoader{path: path}
|
||||
if err := l.load(); err != nil {
|
||||
return l, err
|
||||
}
|
||||
return l, nil
|
||||
}
|
||||
|
||||
func (l *ScheduleLoader) load() error {
|
||||
data, err := os.ReadFile(l.path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
l.config = nil
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("read thinking schedule: %w", err)
|
||||
}
|
||||
if len(data) == 0 {
|
||||
l.config = nil
|
||||
return nil
|
||||
}
|
||||
var cfg ThinkingScheduleConfig
|
||||
if err := json.Unmarshal(data, &cfg); err != nil {
|
||||
l.config = nil
|
||||
return fmt.Errorf("parse thinking schedule: %w", err)
|
||||
}
|
||||
l.mu.Lock()
|
||||
l.config = &cfg
|
||||
l.mu.Unlock()
|
||||
log.Printf("[思考调度] 已加载配置文件: version=%s, default=%dmin, rules=%d", cfg.Version, cfg.DefaultIntervalMinutes, len(cfg.Rules))
|
||||
return nil
|
||||
}
|
||||
|
||||
// HasConfig returns true if a schedule config was loaded from file.
|
||||
func (l *ScheduleLoader) HasConfig() bool {
|
||||
l.mu.RLock()
|
||||
defer l.mu.RUnlock()
|
||||
return l.config != nil
|
||||
}
|
||||
|
||||
// GetInterval returns the thinking interval in minutes for the given time.
|
||||
// Returns 0 if no schedule is loaded (caller should use default).
|
||||
func (l *ScheduleLoader) GetInterval(now time.Time) int {
|
||||
l.mu.RLock()
|
||||
defer l.mu.RUnlock()
|
||||
|
||||
if l.config == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
weekday := strings.ToLower(now.Weekday().String()) // monday, tuesday, ...
|
||||
currentMinutes := now.Hour()*60 + now.Minute()
|
||||
|
||||
for _, rule := range l.config.Rules {
|
||||
if !matchDay(weekday, rule.Days) {
|
||||
continue
|
||||
}
|
||||
if !matchTimeRange(currentMinutes, rule.TimeRange) {
|
||||
continue
|
||||
}
|
||||
if matchExceptRange(currentMinutes, rule.Except) {
|
||||
continue
|
||||
}
|
||||
return rule.IntervalMinutes
|
||||
}
|
||||
|
||||
return l.config.DefaultIntervalMinutes
|
||||
}
|
||||
|
||||
// matchDay checks if the current weekday is in the rule's days list.
|
||||
func matchDay(currentDay string, days []string) bool {
|
||||
for _, d := range days {
|
||||
if strings.ToLower(d) == currentDay {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// matchTimeRange checks if currentMinutes (0-1439) falls within the time range.
|
||||
// Supports overnight ranges (e.g., 23:00-07:00 where start > end).
|
||||
func matchTimeRange(currentMinutes int, timeRange string) bool {
|
||||
start, end, ok := parseTimeRange(timeRange)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
if start <= end {
|
||||
return currentMinutes >= start && currentMinutes < end
|
||||
}
|
||||
// Overnight range
|
||||
return currentMinutes >= start || currentMinutes < end
|
||||
}
|
||||
|
||||
// matchExceptRange returns true if currentMinutes falls in any except range.
|
||||
func matchExceptRange(currentMinutes int, exceptRanges []string) bool {
|
||||
for _, er := range exceptRanges {
|
||||
start, end, ok := parseTimeRange(er)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if start <= end {
|
||||
if currentMinutes >= start && currentMinutes < end {
|
||||
return true
|
||||
}
|
||||
} else {
|
||||
if currentMinutes >= start || currentMinutes < end {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// parseTimeRange parses "HH:MM-HH:MM" into start and end minutes from midnight.
|
||||
func parseTimeRange(r string) (int, int, bool) {
|
||||
parts := strings.SplitN(r, "-", 2)
|
||||
if len(parts) != 2 {
|
||||
return 0, 0, false
|
||||
}
|
||||
start, ok := parseHM(strings.TrimSpace(parts[0]))
|
||||
if !ok {
|
||||
return 0, 0, false
|
||||
}
|
||||
end, ok := parseHM(strings.TrimSpace(parts[1]))
|
||||
if !ok {
|
||||
return 0, 0, false
|
||||
}
|
||||
return start, end, true
|
||||
}
|
||||
|
||||
// parseHM parses "HH:MM" into minutes from midnight.
|
||||
func parseHM(s string) (int, bool) {
|
||||
parts := strings.SplitN(s, ":", 2)
|
||||
if len(parts) != 2 {
|
||||
return 0, false
|
||||
}
|
||||
h, err := strconv.Atoi(parts[0])
|
||||
if err != nil || h < 0 || h > 23 {
|
||||
return 0, false
|
||||
}
|
||||
m, err := strconv.Atoi(parts[1])
|
||||
if err != nil || m < 0 || m > 59 {
|
||||
return 0, false
|
||||
}
|
||||
return h*60 + m, true
|
||||
}
|
||||
@@ -24,9 +24,10 @@ type IoTDeviceSummary interface {
|
||||
|
||||
// ConversationStore 会话历史存储接口
|
||||
type ConversationStore struct {
|
||||
mu sync.RWMutex
|
||||
messages map[string][]model.LLMMessage // key = sessionID
|
||||
maxHistory int
|
||||
mu sync.RWMutex
|
||||
messages map[string][]model.LLMMessage // key = sessionID
|
||||
maxHistory int
|
||||
databaseURL string // lazy-load from DB on cache miss
|
||||
}
|
||||
|
||||
// NewConversationStore 创建会话历史存储
|
||||
@@ -37,6 +38,13 @@ func NewConversationStore(maxHistory int) *ConversationStore {
|
||||
}
|
||||
}
|
||||
|
||||
// SetDatabaseURL sets the database URL for lazy-loading history on cache miss.
|
||||
func (cs *ConversationStore) SetDatabaseURL(url string) {
|
||||
cs.mu.Lock()
|
||||
defer cs.mu.Unlock()
|
||||
cs.databaseURL = url
|
||||
}
|
||||
|
||||
// AddMessage 添加消息到会话历史
|
||||
func (cs *ConversationStore) AddMessage(sessionID string, msg model.LLMMessage) {
|
||||
cs.mu.Lock()
|
||||
@@ -59,12 +67,23 @@ func (cs *ConversationStore) AddMessage(sessionID string, msg model.LLMMessage)
|
||||
cs.messages[sessionID] = msgs
|
||||
}
|
||||
|
||||
// GetHistory 获取会话历史
|
||||
// GetHistory 获取会话历史。
|
||||
// 如果内存缓存为空且配置了 databaseURL,会尝试从 DB 懒加载历史。
|
||||
func (cs *ConversationStore) GetHistory(sessionID string, limit int) []model.LLMMessage {
|
||||
cs.mu.RLock()
|
||||
defer cs.mu.RUnlock()
|
||||
|
||||
msgs := cs.messages[sessionID]
|
||||
dbURL := cs.databaseURL
|
||||
cs.mu.RUnlock()
|
||||
|
||||
if len(msgs) == 0 && dbURL != "" {
|
||||
// 懒加载:从 DB 恢复该会话的历史
|
||||
if err := cs.LoadFromDB(dbURL, sessionID, limit); err == nil {
|
||||
cs.mu.RLock()
|
||||
msgs = cs.messages[sessionID]
|
||||
cs.mu.RUnlock()
|
||||
}
|
||||
}
|
||||
|
||||
if len(msgs) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,204 @@
|
||||
package host
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// DirectBackend executes commands directly on the host via os/exec,
|
||||
// with command allowlist and directory restrictions for safety.
|
||||
type DirectBackend struct {
|
||||
sandbox *Sandbox
|
||||
allowedDirs []string
|
||||
}
|
||||
|
||||
// NewDirectBackend creates a host execution backend that runs commands
|
||||
// directly on the host machine with sandbox restrictions.
|
||||
func NewDirectBackend(sandbox *Sandbox) *DirectBackend {
|
||||
b := &DirectBackend{sandbox: sandbox}
|
||||
if sandbox != nil {
|
||||
b.allowedDirs = sandbox.cfg.AllowedDirs
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *DirectBackend) Name() string { return "direct" }
|
||||
|
||||
// SetAllowedDirs updates the directories accessible for file operations.
|
||||
func (b *DirectBackend) SetAllowedDirs(dirs []string) {
|
||||
b.allowedDirs = dirs
|
||||
if b.sandbox != nil {
|
||||
b.sandbox.cfg.AllowedDirs = dirs
|
||||
}
|
||||
}
|
||||
|
||||
// Exec runs a command in the sandbox.
|
||||
func (b *DirectBackend) Exec(ctx context.Context, command, workDir string, timeout time.Duration) (*ExecResult, error) {
|
||||
return b.sandbox.Exec(ctx, command, workDir, timeout)
|
||||
}
|
||||
|
||||
// ReadFile reads the contents of a file within allowed directories.
|
||||
func (b *DirectBackend) ReadFile(path string, maxBytes int) (string, error) {
|
||||
if maxBytes <= 0 {
|
||||
maxBytes = 1024 * 1024
|
||||
}
|
||||
if err := b.validatePath(path); err != nil {
|
||||
return "", err
|
||||
}
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot stat file: %w", err)
|
||||
}
|
||||
if info.IsDir() {
|
||||
return "", fmt.Errorf("path is a directory: %s", path)
|
||||
}
|
||||
if info.Size() > int64(maxBytes) {
|
||||
return "", fmt.Errorf("file too large: %d bytes (max %d)", info.Size(), maxBytes)
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot read file: %w", err)
|
||||
}
|
||||
if len(data) > maxBytes {
|
||||
data = data[:maxBytes]
|
||||
}
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
// WriteFile writes data to a file within allowed directories.
|
||||
func (b *DirectBackend) WriteFile(path, content string, maxBytes int) error {
|
||||
if maxBytes <= 0 {
|
||||
maxBytes = 1024 * 1024
|
||||
}
|
||||
if len(content) > maxBytes {
|
||||
return fmt.Errorf("content too large: %d bytes (max %d)", len(content), maxBytes)
|
||||
}
|
||||
if err := b.validatePath(path); err != nil {
|
||||
return err
|
||||
}
|
||||
dir := filepath.Dir(path)
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return fmt.Errorf("cannot create directory: %w", err)
|
||||
}
|
||||
return os.WriteFile(path, []byte(content), 0644)
|
||||
}
|
||||
|
||||
// ListDir lists directory contents within allowed directories.
|
||||
func (b *DirectBackend) ListDir(path string) ([]DirEntry, error) {
|
||||
if err := b.validatePath(path); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
entries, err := os.ReadDir(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot read directory: %w", err)
|
||||
}
|
||||
result := make([]DirEntry, 0, len(entries))
|
||||
for _, e := range entries {
|
||||
info, _ := e.Info()
|
||||
size := int64(0)
|
||||
modTime := time.Time{}
|
||||
if info != nil {
|
||||
size = info.Size()
|
||||
modTime = info.ModTime()
|
||||
}
|
||||
result = append(result, DirEntry{
|
||||
Name: e.Name(),
|
||||
IsDir: e.IsDir(),
|
||||
Size: size,
|
||||
ModTime: modTime.Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// SystemInfo returns basic system information.
|
||||
func (b *DirectBackend) SystemInfo() map[string]interface{} {
|
||||
hostname, _ := os.Hostname()
|
||||
wd, _ := os.Getwd()
|
||||
|
||||
info := map[string]interface{}{
|
||||
"hostname": hostname,
|
||||
"os": runtime.GOOS,
|
||||
"arch": runtime.GOARCH,
|
||||
"num_cpu": runtime.NumCPU(),
|
||||
"go_version": runtime.Version(),
|
||||
"work_dir": wd,
|
||||
"backend": "direct",
|
||||
}
|
||||
|
||||
if runtime.GOOS == "windows" {
|
||||
cmd := exec.Command("systeminfo")
|
||||
out, err := cmd.Output()
|
||||
if err == nil {
|
||||
lines := strings.Split(string(out), "\n")
|
||||
for _, line := range lines {
|
||||
line = strings.TrimSpace(line)
|
||||
if strings.Contains(line, "Total Physical Memory") {
|
||||
parts := strings.SplitN(line, ":", 2)
|
||||
if len(parts) == 2 {
|
||||
info["total_memory"] = strings.TrimSpace(parts[1])
|
||||
}
|
||||
}
|
||||
if strings.Contains(line, "OS Name") {
|
||||
parts := strings.SplitN(line, ":", 2)
|
||||
if len(parts) == 2 {
|
||||
info["os_name"] = strings.TrimSpace(parts[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if data, err := os.ReadFile("/proc/meminfo"); err == nil {
|
||||
for _, line := range strings.Split(string(data), "\n") {
|
||||
if strings.HasPrefix(line, "MemTotal:") {
|
||||
info["total_memory"] = strings.TrimSpace(strings.TrimPrefix(line, "MemTotal:"))
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return info
|
||||
}
|
||||
|
||||
// DiskUsage returns disk usage for the given path.
|
||||
func (b *DirectBackend) DiskUsage(path string) (map[string]interface{}, error) {
|
||||
if err := b.validatePath(path); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot stat path: %w", err)
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"path": path,
|
||||
"is_dir": info.IsDir(),
|
||||
"size": info.Size(),
|
||||
"mod_time": info.ModTime().Format(time.RFC3339),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (b *DirectBackend) validatePath(path string) error {
|
||||
absPath, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot resolve path: %w", err)
|
||||
}
|
||||
if len(b.allowedDirs) == 0 {
|
||||
return nil
|
||||
}
|
||||
for _, allowed := range b.allowedDirs {
|
||||
absAllowed, err := filepath.Abs(allowed)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(absPath, absAllowed+string(os.PathSeparator)) || absPath == absAllowed {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("path not in allowed directories: %s", path)
|
||||
}
|
||||
@@ -0,0 +1,274 @@
|
||||
package host
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// DockerBackend executes commands inside a Docker container,
|
||||
// providing a full Linux OS environment with container-level isolation.
|
||||
type DockerBackend struct {
|
||||
container string
|
||||
image string
|
||||
timeout time.Duration
|
||||
}
|
||||
|
||||
// NewDockerBackend creates a Docker backend that runs commands in the
|
||||
// specified container. If the container does not exist, it will be
|
||||
// created from the given image.
|
||||
func NewDockerBackend(container, image string, defaultTimeout time.Duration) *DockerBackend {
|
||||
if defaultTimeout <= 0 {
|
||||
defaultTimeout = 30 * time.Second
|
||||
}
|
||||
return &DockerBackend{
|
||||
container: container,
|
||||
image: image,
|
||||
timeout: defaultTimeout,
|
||||
}
|
||||
}
|
||||
|
||||
func (b *DockerBackend) Name() string { return "docker" }
|
||||
|
||||
// ensureContainer checks that the container exists and is running.
|
||||
// If it doesn't exist, it creates it from the configured image.
|
||||
func (b *DockerBackend) ensureContainer() error {
|
||||
// Check if container exists and is running
|
||||
check := exec.Command("docker", "inspect", "-f", "{{.State.Running}}", b.container)
|
||||
out, err := check.Output()
|
||||
if err == nil && strings.TrimSpace(string(out)) == "true" {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check if container exists but is stopped
|
||||
if err == nil && strings.TrimSpace(string(out)) == "false" {
|
||||
start := exec.Command("docker", "start", b.container)
|
||||
if out, err := start.CombinedOutput(); err != nil {
|
||||
return fmt.Errorf("cannot start container %s: %s — %w", b.container, string(out), err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Create and start a new container
|
||||
create := exec.Command("docker", "run", "-d", "--name", b.container,
|
||||
"--restart", "unless-stopped",
|
||||
b.image, "sleep", "infinity")
|
||||
if out, err := create.CombinedOutput(); err != nil {
|
||||
return fmt.Errorf("cannot create container %s from image %s: %s — %w",
|
||||
b.container, b.image, string(out), err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Exec runs a command inside the Docker container.
|
||||
func (b *DockerBackend) Exec(ctx context.Context, command, workDir string, timeout time.Duration) (*ExecResult, error) {
|
||||
if command == "" {
|
||||
return nil, fmt.Errorf("empty command")
|
||||
}
|
||||
|
||||
if err := b.ensureContainer(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if timeout <= 0 {
|
||||
timeout = b.timeout
|
||||
}
|
||||
|
||||
execCtx, cancel := context.WithTimeout(ctx, timeout)
|
||||
defer cancel()
|
||||
|
||||
// Build the shell command to run inside the container
|
||||
script := command
|
||||
if workDir != "" {
|
||||
script = fmt.Sprintf("cd %s && %s", shellEscapeDocker(workDir), command)
|
||||
}
|
||||
|
||||
cmd := exec.CommandContext(execCtx, "docker", "exec", b.container, "sh", "-c", script)
|
||||
|
||||
var stdout, stderr bytes.Buffer
|
||||
cmd.Stdout = &stdout
|
||||
cmd.Stderr = &stderr
|
||||
|
||||
start := time.Now()
|
||||
err := cmd.Run()
|
||||
elapsed := time.Since(start)
|
||||
|
||||
result := &ExecResult{
|
||||
Duration: elapsed.Round(time.Millisecond).String(),
|
||||
Stdout: stdout.String(),
|
||||
Stderr: stderr.String(),
|
||||
}
|
||||
|
||||
if execCtx.Err() == context.DeadlineExceeded {
|
||||
result.TimedOut = true
|
||||
result.ExitCode = -1
|
||||
return result, fmt.Errorf("command timed out after %s", timeout)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
if exitErr, ok := err.(*exec.ExitError); ok {
|
||||
result.ExitCode = exitErr.ExitCode()
|
||||
} else {
|
||||
result.ExitCode = -1
|
||||
}
|
||||
} else {
|
||||
result.ExitCode = 0
|
||||
}
|
||||
|
||||
return result, err
|
||||
}
|
||||
|
||||
// ReadFile reads a file from inside the container using cat.
|
||||
func (b *DockerBackend) ReadFile(path string, maxBytes int) (string, error) {
|
||||
if maxBytes <= 0 {
|
||||
maxBytes = 1024 * 1024
|
||||
}
|
||||
if err := b.ensureContainer(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
cmd := exec.CommandContext(ctx, "docker", "exec", b.container, "cat", path)
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot read file %s: %w", path, err)
|
||||
}
|
||||
if len(out) > maxBytes {
|
||||
out = out[:maxBytes]
|
||||
}
|
||||
return string(out), nil
|
||||
}
|
||||
|
||||
// WriteFile writes content to a file inside the container.
|
||||
func (b *DockerBackend) WriteFile(path, content string, maxBytes int) error {
|
||||
if maxBytes <= 0 {
|
||||
maxBytes = 1024 * 1024
|
||||
}
|
||||
if len(content) > maxBytes {
|
||||
return fmt.Errorf("content too large: %d bytes (max %d)", len(content), maxBytes)
|
||||
}
|
||||
if err := b.ensureContainer(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// Create parent directory and write file
|
||||
cmd := exec.CommandContext(ctx, "docker", "exec", "-i", b.container, "sh", "-c",
|
||||
fmt.Sprintf("mkdir -p $(dirname %s) && cat > %s", shellEscapeDocker(path), shellEscapeDocker(path)))
|
||||
cmd.Stdin = strings.NewReader(content)
|
||||
_, err := cmd.Output()
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot write file %s: %w", path, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListDir lists a directory inside the container.
|
||||
func (b *DockerBackend) ListDir(path string) ([]DirEntry, error) {
|
||||
if err := b.ensureContainer(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
cmd := exec.CommandContext(ctx, "docker", "exec", b.container, "sh", "-c",
|
||||
fmt.Sprintf("ls -la %s 2>/dev/null | tail -n +2 || echo ''", shellEscapeDocker(path)))
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot list dir %s: %w", path, err)
|
||||
}
|
||||
|
||||
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
|
||||
result := make([]DirEntry, 0, len(lines))
|
||||
for _, line := range lines {
|
||||
if line == "" || strings.HasPrefix(line, "total ") {
|
||||
continue
|
||||
}
|
||||
// Parse ls -la output: drwxr-xr-x 2 root root 4096 Jan 1 12:00 name
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) < 9 {
|
||||
continue
|
||||
}
|
||||
isDir := strings.HasPrefix(fields[0], "d")
|
||||
name := fields[len(fields)-1]
|
||||
if name == "." || name == ".." {
|
||||
continue
|
||||
}
|
||||
var size int64
|
||||
fmt.Sscanf(fields[4], "%d", &size)
|
||||
result = append(result, DirEntry{
|
||||
Name: name,
|
||||
IsDir: isDir,
|
||||
Size: size,
|
||||
})
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// SystemInfo returns system information from inside the container.
|
||||
func (b *DockerBackend) SystemInfo() map[string]interface{} {
|
||||
info := map[string]interface{}{
|
||||
"backend": "docker",
|
||||
"container": b.container,
|
||||
"image": b.image,
|
||||
}
|
||||
|
||||
if err := b.ensureContainer(); err != nil {
|
||||
info["error"] = err.Error()
|
||||
return info
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if out, err := exec.CommandContext(ctx, "docker", "exec", b.container, "uname", "-a").Output(); err == nil {
|
||||
info["uname"] = strings.TrimSpace(string(out))
|
||||
}
|
||||
if out, err := exec.CommandContext(ctx, "docker", "exec", b.container, "hostname").Output(); err == nil {
|
||||
info["hostname"] = strings.TrimSpace(string(out))
|
||||
}
|
||||
if out, err := exec.CommandContext(ctx, "docker", "exec", b.container, "free", "-h").Output(); err == nil {
|
||||
info["memory"] = strings.TrimSpace(string(out))
|
||||
}
|
||||
if out, err := exec.CommandContext(ctx, "docker", "exec", b.container, "df", "-h", "/").Output(); err == nil {
|
||||
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
|
||||
if len(lines) > 1 {
|
||||
info["disk"] = strings.TrimSpace(lines[1])
|
||||
}
|
||||
}
|
||||
return info
|
||||
}
|
||||
|
||||
// DiskUsage returns disk usage for a path inside the container.
|
||||
func (b *DockerBackend) DiskUsage(path string) (map[string]interface{}, error) {
|
||||
if err := b.ensureContainer(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
cmd := exec.CommandContext(ctx, "docker", "exec", b.container, "stat", path)
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot stat path %s: %w", path, err)
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"path": path,
|
||||
"stat": strings.TrimSpace(string(out)),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// shellEscapeDocker escapes a string for safe use in a shell command.
|
||||
func shellEscapeDocker(s string) string {
|
||||
escaped := strings.ReplaceAll(s, "'", "'\\''")
|
||||
return "'" + escaped + "'"
|
||||
}
|
||||
@@ -0,0 +1,323 @@
|
||||
package host
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// WSLBackend executes commands inside a WSL2 distribution,
|
||||
// providing a full Linux OS environment isolated from the Windows host.
|
||||
type WSLBackend struct {
|
||||
distro string
|
||||
username string
|
||||
password string
|
||||
timeout time.Duration
|
||||
|
||||
userEnsured bool
|
||||
}
|
||||
|
||||
// NewWSLBackend creates a WSL backend that runs commands in the
|
||||
// specified WSL distribution as the given user. On first use,
|
||||
// the user is automatically created with sudo privileges.
|
||||
func NewWSLBackend(distro, username, password string, defaultTimeout time.Duration) *WSLBackend {
|
||||
if defaultTimeout <= 0 {
|
||||
defaultTimeout = 30 * time.Second
|
||||
}
|
||||
if username == "" {
|
||||
username = "cyrene"
|
||||
}
|
||||
return &WSLBackend{
|
||||
distro: distro,
|
||||
username: username,
|
||||
password: password,
|
||||
timeout: defaultTimeout,
|
||||
}
|
||||
}
|
||||
|
||||
func (b *WSLBackend) Name() string { return "wsl" }
|
||||
|
||||
// ensureUser creates the configured user inside the WSL distro on first call.
|
||||
// The user gets sudo privileges and the configured password.
|
||||
func (b *WSLBackend) ensureUser() error {
|
||||
if b.userEnsured {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check if user already exists
|
||||
checkCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
checkCmd := exec.CommandContext(checkCtx, "wsl.exe", "-d", b.distro, "--", "id", b.username)
|
||||
if checkCmd.Run() == nil {
|
||||
b.userEnsured = true
|
||||
return nil
|
||||
}
|
||||
|
||||
// Create user with home directory, set password, add to sudo group
|
||||
// If password is empty, create user without password (sudo won't need it
|
||||
// if NOPASSWD is configured, but we still set a random one for safety)
|
||||
pwd := b.password
|
||||
if pwd == "" {
|
||||
pwd = "cyrene"
|
||||
}
|
||||
|
||||
// Escape single quotes in password for the shell echo command
|
||||
escapedPwd := strings.ReplaceAll(pwd, "'", "'\\''")
|
||||
script := fmt.Sprintf(
|
||||
"useradd -m -s /bin/bash %s && echo '%s:%s' | chpasswd && usermod -aG sudo %s",
|
||||
b.username, b.username, escapedPwd, b.username,
|
||||
)
|
||||
|
||||
createCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
createCmd := exec.CommandContext(createCtx, "wsl.exe", "-d", b.distro, "--", "bash", "-c", script)
|
||||
if out, err := createCmd.CombinedOutput(); err != nil {
|
||||
return fmt.Errorf("cannot create user %s: %s — %w", b.username, string(out), err)
|
||||
}
|
||||
|
||||
b.userEnsured = true
|
||||
return nil
|
||||
}
|
||||
|
||||
// Exec runs a command inside the WSL distribution via bash.
|
||||
func (b *WSLBackend) Exec(ctx context.Context, command, workDir string, timeout time.Duration) (*ExecResult, error) {
|
||||
if command == "" {
|
||||
return nil, fmt.Errorf("empty command")
|
||||
}
|
||||
|
||||
if err := b.ensureUser(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if timeout <= 0 {
|
||||
timeout = b.timeout
|
||||
}
|
||||
|
||||
execCtx, cancel := context.WithTimeout(ctx, timeout)
|
||||
defer cancel()
|
||||
|
||||
// Build the bash command to run inside WSL
|
||||
script := command
|
||||
if workDir != "" {
|
||||
wslPath := windowsToWSLPath(workDir)
|
||||
script = fmt.Sprintf("cd %s && %s", shellEscape(wslPath), command)
|
||||
}
|
||||
|
||||
cmd := exec.CommandContext(execCtx, "wsl.exe", "-d", b.distro, "--", "bash", "-c", script)
|
||||
|
||||
var stdout, stderr bytes.Buffer
|
||||
cmd.Stdout = &stdout
|
||||
cmd.Stderr = &stderr
|
||||
|
||||
start := time.Now()
|
||||
err := cmd.Run()
|
||||
elapsed := time.Since(start)
|
||||
|
||||
result := &ExecResult{
|
||||
Duration: elapsed.Round(time.Millisecond).String(),
|
||||
Stdout: stdout.String(),
|
||||
Stderr: stderr.String(),
|
||||
}
|
||||
|
||||
if execCtx.Err() == context.DeadlineExceeded {
|
||||
result.TimedOut = true
|
||||
result.ExitCode = -1
|
||||
return result, fmt.Errorf("command timed out after %s", timeout)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
if exitErr, ok := err.(*exec.ExitError); ok {
|
||||
result.ExitCode = exitErr.ExitCode()
|
||||
} else {
|
||||
result.ExitCode = -1
|
||||
}
|
||||
} else {
|
||||
result.ExitCode = 0
|
||||
}
|
||||
|
||||
return result, err
|
||||
}
|
||||
|
||||
// ReadFile reads a file from the WSL filesystem using cat.
|
||||
func (b *WSLBackend) ReadFile(path string, maxBytes int) (string, error) {
|
||||
if maxBytes <= 0 {
|
||||
maxBytes = 1024 * 1024
|
||||
}
|
||||
if err := b.ensureUser(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
wslPath := windowsToWSLPath(path)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
cmd := exec.CommandContext(ctx, "wsl.exe", "-d", b.distro, "--", "cat", wslPath)
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot read file %s: %w", path, err)
|
||||
}
|
||||
if len(out) > maxBytes {
|
||||
out = out[:maxBytes]
|
||||
}
|
||||
return string(out), nil
|
||||
}
|
||||
|
||||
// WriteFile writes content to a file in the WSL filesystem.
|
||||
func (b *WSLBackend) WriteFile(path, content string, maxBytes int) error {
|
||||
if maxBytes <= 0 {
|
||||
maxBytes = 1024 * 1024
|
||||
}
|
||||
if len(content) > maxBytes {
|
||||
return fmt.Errorf("content too large: %d bytes (max %d)", len(content), maxBytes)
|
||||
}
|
||||
if err := b.ensureUser(); err != nil {
|
||||
return err
|
||||
}
|
||||
wslPath := windowsToWSLPath(path)
|
||||
// Create parent directory first
|
||||
dir := filepath.Dir(wslPath)
|
||||
_ = exec.Command("wsl.exe", "-d", b.distro, "--", "mkdir", "-p", dir).Run()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
cmd := exec.CommandContext(ctx, "wsl.exe", "-d", b.distro, "--", "bash", "-c",
|
||||
fmt.Sprintf("cat > %s", shellEscape(wslPath)))
|
||||
cmd.Stdin = strings.NewReader(content)
|
||||
_, err := cmd.Output()
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot write file %s: %w", path, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListDir lists a directory in the WSL filesystem using ls.
|
||||
func (b *WSLBackend) ListDir(path string) ([]DirEntry, error) {
|
||||
if err := b.ensureUser(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
wslPath := windowsToWSLPath(path)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
cmd := exec.CommandContext(ctx, "wsl.exe", "-d", b.distro, "--", "bash", "-c",
|
||||
fmt.Sprintf("stat -c '%%n|%%F|%%s|%%Y' %s/* 2>/dev/null || echo ''", shellEscape(wslPath)))
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot list dir %s: %w", path, err)
|
||||
}
|
||||
|
||||
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
|
||||
result := make([]DirEntry, 0, len(lines))
|
||||
for _, line := range lines {
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
parts := strings.SplitN(line, "|", 4)
|
||||
if len(parts) < 4 {
|
||||
continue
|
||||
}
|
||||
var size int64
|
||||
fmt.Sscanf(parts[2], "%d", &size)
|
||||
var modTimeUnix int64
|
||||
fmt.Sscanf(parts[3], "%d", &modTimeUnix)
|
||||
modTime := time.Unix(modTimeUnix, 0).Format(time.RFC3339)
|
||||
isDir := strings.Contains(parts[1], "directory")
|
||||
result = append(result, DirEntry{
|
||||
Name: filepath.Base(parts[0]),
|
||||
IsDir: isDir,
|
||||
Size: size,
|
||||
ModTime: modTime,
|
||||
})
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// SystemInfo returns system information from inside the WSL distribution.
|
||||
func (b *WSLBackend) SystemInfo() map[string]interface{} {
|
||||
info := map[string]interface{}{
|
||||
"backend": "wsl",
|
||||
"distro": b.distro,
|
||||
}
|
||||
|
||||
if err := b.ensureUser(); err != nil {
|
||||
info["error"] = err.Error()
|
||||
return info
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// uname
|
||||
if out, err := exec.CommandContext(ctx, "wsl.exe", "-d", b.distro, "--", "uname", "-a").Output(); err == nil {
|
||||
info["uname"] = strings.TrimSpace(string(out))
|
||||
}
|
||||
|
||||
// hostname
|
||||
if out, err := exec.CommandContext(ctx, "wsl.exe", "-d", b.distro, "--", "hostname").Output(); err == nil {
|
||||
info["hostname"] = strings.TrimSpace(string(out))
|
||||
}
|
||||
|
||||
// memory info
|
||||
if out, err := exec.CommandContext(ctx, "wsl.exe", "-d", b.distro, "--", "free", "-h").Output(); err == nil {
|
||||
info["memory"] = strings.TrimSpace(string(out))
|
||||
}
|
||||
|
||||
// disk info
|
||||
if out, err := exec.CommandContext(ctx, "wsl.exe", "-d", b.distro, "--", "df", "-h", "/").Output(); err == nil {
|
||||
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
|
||||
if len(lines) > 1 {
|
||||
info["disk"] = strings.TrimSpace(lines[1])
|
||||
}
|
||||
}
|
||||
|
||||
return info
|
||||
}
|
||||
|
||||
// DiskUsage returns disk usage for a path inside WSL.
|
||||
func (b *WSLBackend) DiskUsage(path string) (map[string]interface{}, error) {
|
||||
wslPath := windowsToWSLPath(path)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
cmd := exec.CommandContext(ctx, "wsl.exe", "-d", b.distro, "--", "stat", wslPath)
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot stat path %s: %w", path, err)
|
||||
}
|
||||
|
||||
// Parse stat output minimally
|
||||
result := map[string]interface{}{
|
||||
"path": path,
|
||||
"wsl_path": wslPath,
|
||||
"stat": strings.TrimSpace(string(out)),
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// windowsToWSLPath converts a Windows path to its WSL equivalent.
|
||||
// C:\Users\foo → /mnt/c/Users/foo
|
||||
// If the path is already a WSL path (starts with /), return as-is.
|
||||
func windowsToWSLPath(path string) string {
|
||||
if strings.HasPrefix(path, "/") {
|
||||
return path // Already a Unix path
|
||||
}
|
||||
// Handle Windows drive letter: C:\... → /mnt/c/...
|
||||
if len(path) >= 2 && path[1] == ':' {
|
||||
drive := strings.ToLower(string(path[0]))
|
||||
rest := strings.TrimPrefix(path[2:], "\\")
|
||||
rest = strings.ReplaceAll(rest, "\\", "/")
|
||||
return fmt.Sprintf("/mnt/%s/%s", drive, rest)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
// shellEscape escapes a string for safe use in a shell command.
|
||||
func shellEscape(s string) string {
|
||||
// Use single quotes and escape any single quotes in the string
|
||||
escaped := strings.ReplaceAll(s, "'", "'\\''")
|
||||
return "'" + escaped + "'"
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
package host
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestWSLBackendIntegration(t *testing.T) {
|
||||
distro := os.Getenv("WSL_DISTRO")
|
||||
if distro == "" {
|
||||
t.Skip("WSL_DISTRO not set, skipping WSL integration test (set WSL_DISTRO=cyrene-wsl to run)")
|
||||
}
|
||||
|
||||
backend := NewWSLBackend(distro, "cyrene", "test123", 30*time.Second)
|
||||
mgr := NewManager(backend)
|
||||
ctx := context.Background()
|
||||
|
||||
// 1. Basic command
|
||||
t.Run("echo", func(t *testing.T) {
|
||||
r, err := mgr.Exec(ctx, "echo 'hello from WSL OS env'", "", 10*time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("exec failed: %v", err)
|
||||
}
|
||||
if r.ExitCode != 0 {
|
||||
t.Fatalf("exit=%d, stderr=%s", r.ExitCode, r.Stderr)
|
||||
}
|
||||
if !strings.Contains(r.Stdout, "hello from WSL OS env") {
|
||||
t.Fatalf("unexpected stdout: %s", r.Stdout)
|
||||
}
|
||||
t.Logf("echo OK: %s (duration=%s)", strings.TrimSpace(r.Stdout), r.Duration)
|
||||
})
|
||||
|
||||
// 2. Complex commands - package manager
|
||||
t.Run("apt", func(t *testing.T) {
|
||||
r, err := mgr.Exec(ctx, "apt --version 2>&1", "", 10*time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("exec failed: %v", err)
|
||||
}
|
||||
t.Logf("apt OK: %s", strings.TrimSpace(r.Stdout))
|
||||
})
|
||||
|
||||
// 3. Python (should be pre-installed on Ubuntu)
|
||||
t.Run("python", func(t *testing.T) {
|
||||
r, err := mgr.Exec(ctx, "python3 --version 2>&1", "", 10*time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("exec failed: %v", err)
|
||||
}
|
||||
t.Logf("python OK: %s", strings.TrimSpace(r.Stdout))
|
||||
})
|
||||
|
||||
// 4. Pipeline & shell features
|
||||
t.Run("pipeline", func(t *testing.T) {
|
||||
r, err := mgr.Exec(ctx, "echo 'a\nb\nc\nd' | wc -l", "", 10*time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("exec failed: %v", err)
|
||||
}
|
||||
if r.ExitCode != 0 {
|
||||
t.Fatalf("exit=%d", r.ExitCode)
|
||||
}
|
||||
t.Logf("pipeline OK: %s", strings.TrimSpace(r.Stdout))
|
||||
})
|
||||
|
||||
// 5. File write & read
|
||||
t.Run("file_rw", func(t *testing.T) {
|
||||
err := mgr.WriteFile("/tmp/cyrene-wsl-test.txt", "Hello from Cyrene OS!", 1024*1024)
|
||||
if err != nil {
|
||||
t.Fatalf("write failed: %v", err)
|
||||
}
|
||||
content, err := mgr.ReadFile("/tmp/cyrene-wsl-test.txt", 1024*1024)
|
||||
if err != nil {
|
||||
t.Fatalf("read failed: %v", err)
|
||||
}
|
||||
if content != "Hello from Cyrene OS!" {
|
||||
t.Fatalf("content mismatch: %q", content)
|
||||
}
|
||||
t.Logf("file r/w OK: %q", content)
|
||||
})
|
||||
|
||||
// 6. Directory listing
|
||||
t.Run("listdir", func(t *testing.T) {
|
||||
entries, err := mgr.ListDir("/etc")
|
||||
if err != nil {
|
||||
t.Fatalf("listdir failed: %v", err)
|
||||
}
|
||||
if len(entries) == 0 {
|
||||
t.Fatal("expected entries in /etc")
|
||||
}
|
||||
t.Logf("listdir OK: %d entries in /etc", len(entries))
|
||||
for _, e := range entries {
|
||||
if e.Name == "os-release" || e.Name == "hostname" {
|
||||
t.Logf(" - %s (isDir=%v, size=%d)", e.Name, e.IsDir, e.Size)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// 7. System info
|
||||
t.Run("sysinfo", func(t *testing.T) {
|
||||
info := mgr.SystemInfo()
|
||||
if info["backend"] != "wsl" {
|
||||
t.Fatalf("unexpected backend: %v", info["backend"])
|
||||
}
|
||||
if info["distro"] != distro {
|
||||
t.Fatalf("unexpected distro: %v", info["distro"])
|
||||
}
|
||||
t.Logf("sysinfo OK: backend=%v, distro=%v", info["backend"], info["distro"])
|
||||
if uname, ok := info["uname"]; ok {
|
||||
t.Logf(" uname: %v", uname)
|
||||
}
|
||||
if hostname, ok := info["hostname"]; ok {
|
||||
t.Logf(" hostname: %v", hostname)
|
||||
}
|
||||
if mem, ok := info["memory"]; ok {
|
||||
t.Logf(" memory: %v", mem)
|
||||
}
|
||||
})
|
||||
|
||||
// 8. workDir
|
||||
t.Run("workdir", func(t *testing.T) {
|
||||
r, err := mgr.Exec(ctx, "pwd", "/tmp", 10*time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("exec failed: %v", err)
|
||||
}
|
||||
if !strings.Contains(r.Stdout, "/tmp") {
|
||||
t.Fatalf("expected /tmp, got: %s", r.Stdout)
|
||||
}
|
||||
t.Logf("workdir OK: pwd=%s", strings.TrimSpace(r.Stdout))
|
||||
})
|
||||
|
||||
// 9. Timeout
|
||||
t.Run("timeout", func(t *testing.T) {
|
||||
r, err := mgr.Exec(ctx, "sleep 10", "", 1*time.Second)
|
||||
if err == nil {
|
||||
t.Fatal("expected timeout")
|
||||
}
|
||||
if !r.TimedOut {
|
||||
t.Fatal("expected TimedOut=true")
|
||||
}
|
||||
t.Logf("timeout OK: timed_out=%v", r.TimedOut)
|
||||
})
|
||||
}
|
||||
@@ -2,207 +2,72 @@ package host
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Manager provides controlled access to the host machine.
|
||||
// It wraps a Sandbox for command execution and adds file system
|
||||
// operations with path allow-list enforcement.
|
||||
// HostBackend defines the interface for command execution and file system
|
||||
// operations. Implementations include DirectBackend (host OS), WSLBackend
|
||||
// (Windows Subsystem for Linux), and DockerBackend (container).
|
||||
type HostBackend interface {
|
||||
Exec(ctx context.Context, command, workDir string, timeout time.Duration) (*ExecResult, error)
|
||||
ReadFile(path string, maxBytes int) (string, error)
|
||||
WriteFile(path, content string, maxBytes int) error
|
||||
ListDir(path string) ([]DirEntry, error)
|
||||
SystemInfo() map[string]interface{}
|
||||
DiskUsage(path string) (map[string]interface{}, error)
|
||||
Name() string
|
||||
}
|
||||
|
||||
// Manager provides controlled access to the host machine. It delegates
|
||||
// to a HostBackend implementation which may be direct, WSL, or Docker.
|
||||
type Manager struct {
|
||||
sandbox *Sandbox
|
||||
allowedDirs []string
|
||||
backend HostBackend
|
||||
}
|
||||
|
||||
// NewManager creates a new host Manager.
|
||||
func NewManager(sandbox *Sandbox) *Manager {
|
||||
m := &Manager{sandbox: sandbox}
|
||||
if sandbox != nil {
|
||||
m.allowedDirs = sandbox.cfg.AllowedDirs
|
||||
}
|
||||
return m
|
||||
// NewManager creates a new host Manager with the given backend.
|
||||
func NewManager(backend HostBackend) *Manager {
|
||||
return &Manager{backend: backend}
|
||||
}
|
||||
|
||||
// SetAllowedDirs updates the list of directories accessible for file operations.
|
||||
// SetAllowedDirs updates directory restrictions. Only effective for
|
||||
// DirectBackend; WSL and Docker backends are no-ops.
|
||||
func (m *Manager) SetAllowedDirs(dirs []string) {
|
||||
m.allowedDirs = dirs
|
||||
m.sandbox.cfg.AllowedDirs = dirs
|
||||
if db, ok := m.backend.(*DirectBackend); ok {
|
||||
db.SetAllowedDirs(dirs)
|
||||
}
|
||||
}
|
||||
|
||||
// Exec runs a command in the sandbox.
|
||||
// Exec runs a command via the configured backend.
|
||||
func (m *Manager) Exec(ctx context.Context, command, workDir string, timeout time.Duration) (*ExecResult, error) {
|
||||
return m.sandbox.Exec(ctx, command, workDir, timeout)
|
||||
return m.backend.Exec(ctx, command, workDir, timeout)
|
||||
}
|
||||
|
||||
// ReadFile reads the contents of a file within allowed directories.
|
||||
// ReadFile reads a file via the configured backend.
|
||||
func (m *Manager) ReadFile(path string, maxBytes int) (string, error) {
|
||||
if maxBytes <= 0 {
|
||||
maxBytes = 1024 * 1024
|
||||
}
|
||||
if err := m.validatePath(path); err != nil {
|
||||
return "", err
|
||||
}
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot stat file: %w", err)
|
||||
}
|
||||
if info.IsDir() {
|
||||
return "", fmt.Errorf("path is a directory: %s", path)
|
||||
}
|
||||
if info.Size() > int64(maxBytes) {
|
||||
return "", fmt.Errorf("file too large: %d bytes (max %d)", info.Size(), maxBytes)
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot read file: %w", err)
|
||||
}
|
||||
if len(data) > maxBytes {
|
||||
data = data[:maxBytes]
|
||||
}
|
||||
return string(data), nil
|
||||
return m.backend.ReadFile(path, maxBytes)
|
||||
}
|
||||
|
||||
// WriteFile writes data to a file within allowed directories.
|
||||
// WriteFile writes a file via the configured backend.
|
||||
func (m *Manager) WriteFile(path, content string, maxBytes int) error {
|
||||
if maxBytes <= 0 {
|
||||
maxBytes = 1024 * 1024
|
||||
}
|
||||
if len(content) > maxBytes {
|
||||
return fmt.Errorf("content too large: %d bytes (max %d)", len(content), maxBytes)
|
||||
}
|
||||
if err := m.validatePath(path); err != nil {
|
||||
return err
|
||||
}
|
||||
dir := filepath.Dir(path)
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return fmt.Errorf("cannot create directory: %w", err)
|
||||
}
|
||||
return os.WriteFile(path, []byte(content), 0644)
|
||||
return m.backend.WriteFile(path, content, maxBytes)
|
||||
}
|
||||
|
||||
// ListDir lists directory contents within allowed directories.
|
||||
// ListDir lists a directory via the configured backend.
|
||||
func (m *Manager) ListDir(path string) ([]DirEntry, error) {
|
||||
if err := m.validatePath(path); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
entries, err := os.ReadDir(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot read directory: %w", err)
|
||||
}
|
||||
result := make([]DirEntry, 0, len(entries))
|
||||
for _, e := range entries {
|
||||
info, _ := e.Info()
|
||||
size := int64(0)
|
||||
modTime := time.Time{}
|
||||
if info != nil {
|
||||
size = info.Size()
|
||||
modTime = info.ModTime()
|
||||
}
|
||||
result = append(result, DirEntry{
|
||||
Name: e.Name(),
|
||||
IsDir: e.IsDir(),
|
||||
Size: size,
|
||||
ModTime: modTime.Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
return result, nil
|
||||
return m.backend.ListDir(path)
|
||||
}
|
||||
|
||||
// DirEntry represents a filesystem directory entry.
|
||||
type DirEntry struct {
|
||||
Name string `json:"name"`
|
||||
IsDir bool `json:"is_dir"`
|
||||
Size int64 `json:"size"`
|
||||
ModTime string `json:"mod_time"`
|
||||
}
|
||||
|
||||
// SystemInfo returns basic system information.
|
||||
// SystemInfo returns system information from the configured backend.
|
||||
func (m *Manager) SystemInfo() map[string]interface{} {
|
||||
hostname, _ := os.Hostname()
|
||||
wd, _ := os.Getwd()
|
||||
|
||||
info := map[string]interface{}{
|
||||
"hostname": hostname,
|
||||
"os": runtime.GOOS,
|
||||
"arch": runtime.GOARCH,
|
||||
"num_cpu": runtime.NumCPU(),
|
||||
"go_version": runtime.Version(),
|
||||
"work_dir": wd,
|
||||
}
|
||||
|
||||
if runtime.GOOS == "windows" {
|
||||
cmd := exec.Command("systeminfo")
|
||||
out, err := cmd.Output()
|
||||
if err == nil {
|
||||
lines := strings.Split(string(out), "\n")
|
||||
for _, line := range lines {
|
||||
line = strings.TrimSpace(line)
|
||||
if strings.Contains(line, "Total Physical Memory") {
|
||||
parts := strings.SplitN(line, ":", 2)
|
||||
if len(parts) == 2 {
|
||||
info["total_memory"] = strings.TrimSpace(parts[1])
|
||||
}
|
||||
}
|
||||
if strings.Contains(line, "OS Name") {
|
||||
parts := strings.SplitN(line, ":", 2)
|
||||
if len(parts) == 2 {
|
||||
info["os_name"] = strings.TrimSpace(parts[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if data, err := os.ReadFile("/proc/meminfo"); err == nil {
|
||||
for _, line := range strings.Split(string(data), "\n") {
|
||||
if strings.HasPrefix(line, "MemTotal:") {
|
||||
info["total_memory"] = strings.TrimSpace(strings.TrimPrefix(line, "MemTotal:"))
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return info
|
||||
return m.backend.SystemInfo()
|
||||
}
|
||||
|
||||
// DiskUsage returns disk usage for the given path.
|
||||
// DiskUsage returns disk usage info from the configured backend.
|
||||
func (m *Manager) DiskUsage(path string) (map[string]interface{}, error) {
|
||||
if err := m.validatePath(path); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot stat path: %w", err)
|
||||
}
|
||||
result := map[string]interface{}{
|
||||
"path": path,
|
||||
"is_dir": info.IsDir(),
|
||||
"size": info.Size(),
|
||||
"mod_time": info.ModTime().Format(time.RFC3339),
|
||||
}
|
||||
return result, nil
|
||||
return m.backend.DiskUsage(path)
|
||||
}
|
||||
|
||||
func (m *Manager) validatePath(path string) error {
|
||||
absPath, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot resolve path: %w", err)
|
||||
}
|
||||
if len(m.allowedDirs) == 0 {
|
||||
return nil
|
||||
}
|
||||
for _, allowed := range m.allowedDirs {
|
||||
absAllowed, err := filepath.Abs(allowed)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(absPath, absAllowed+string(os.PathSeparator)) || absPath == absAllowed {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("path not in allowed directories: %s", path)
|
||||
// BackendName returns the name of the active backend.
|
||||
func (m *Manager) BackendName() string {
|
||||
return m.backend.Name()
|
||||
}
|
||||
|
||||
@@ -50,6 +50,14 @@ func NewSandbox(cfg SandboxConfig) *Sandbox {
|
||||
return &Sandbox{cfg: cfg}
|
||||
}
|
||||
|
||||
// DirEntry represents a filesystem directory entry.
|
||||
type DirEntry struct {
|
||||
Name string `json:"name"`
|
||||
IsDir bool `json:"is_dir"`
|
||||
Size int64 `json:"size"`
|
||||
ModTime string `json:"mod_time,omitempty"`
|
||||
}
|
||||
|
||||
// ExecResult holds the result of a sandboxed command execution.
|
||||
type ExecResult struct {
|
||||
Stdout string `json:"stdout"`
|
||||
|
||||
@@ -62,7 +62,7 @@ func TestManagerFileOps(t *testing.T) {
|
||||
tmpDir := os.TempDir()
|
||||
cfg.AllowedDirs = []string{tmpDir}
|
||||
sandbox := NewSandbox(cfg)
|
||||
mgr := NewManager(sandbox)
|
||||
mgr := NewManager(NewDirectBackend(sandbox))
|
||||
mgr.SetAllowedDirs([]string{tmpDir})
|
||||
|
||||
testPath := filepath.Join(tmpDir, "cyrene-test-file.txt")
|
||||
@@ -102,7 +102,7 @@ func TestManagerFileOps(t *testing.T) {
|
||||
func TestManagerSystemInfo(t *testing.T) {
|
||||
cfg := DefaultSandboxConfig()
|
||||
sandbox := NewSandbox(cfg)
|
||||
mgr := NewManager(sandbox)
|
||||
mgr := NewManager(NewDirectBackend(sandbox))
|
||||
|
||||
info := mgr.SystemInfo()
|
||||
if info["hostname"] == nil || info["hostname"] == "" {
|
||||
@@ -121,7 +121,7 @@ func TestPathValidation(t *testing.T) {
|
||||
cfg := DefaultSandboxConfig()
|
||||
cfg.AllowedDirs = []string{os.TempDir()}
|
||||
sandbox := NewSandbox(cfg)
|
||||
mgr := NewManager(sandbox)
|
||||
mgr := NewManager(NewDirectBackend(sandbox))
|
||||
mgr.SetAllowedDirs([]string{os.TempDir()})
|
||||
|
||||
// Should fail: access outside allowed dirs
|
||||
|
||||
@@ -4,15 +4,16 @@ import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"github.com/yourname/cyrene-ai/pkg/logger"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/yourname/cyrene-ai/ai-core/internal/model"
|
||||
"github.com/yourname/cyrene-ai/pkg/logger"
|
||||
)
|
||||
|
||||
// OpenAIConfig OpenAI适配器配置
|
||||
@@ -267,12 +268,13 @@ func (p *OpenAIProvider) doChat(ctx context.Context, messages []model.LLMMessage
|
||||
LogCall(r)
|
||||
}()
|
||||
|
||||
// 转换消息格式
|
||||
// 转换消息格式(先解析图片 URL 为 data URL)
|
||||
oaiMessages := make([]openAIMessage, len(messages))
|
||||
for i, msg := range messages {
|
||||
resolvedImages := p.resolveImages(msg.Images)
|
||||
oaiMsg := openAIMessage{
|
||||
Role: string(msg.Role),
|
||||
Content: buildContent(msg.Content, msg.Images),
|
||||
Content: buildContent(msg.Content, resolvedImages),
|
||||
Name: msg.Name,
|
||||
ToolCallID: msg.ToolCallID,
|
||||
ReasoningContent: msg.ReasoningContent,
|
||||
@@ -377,9 +379,10 @@ func (p *OpenAIProvider) doChat(ctx context.Context, messages []model.LLMMessage
|
||||
func (p *OpenAIProvider) doChatStream(ctx context.Context, messages []model.LLMMessage, modelName string, tools []OpenAITool) (*http.Response, error) {
|
||||
oaiMessages := make([]openAIMessage, len(messages))
|
||||
for i, msg := range messages {
|
||||
resolvedImages := p.resolveImages(msg.Images)
|
||||
oaiMsg := openAIMessage{
|
||||
Role: string(msg.Role),
|
||||
Content: buildContent(msg.Content, msg.Images),
|
||||
Content: buildContent(msg.Content, resolvedImages),
|
||||
Name: msg.Name,
|
||||
ToolCallID: msg.ToolCallID,
|
||||
ReasoningContent: msg.ReasoningContent,
|
||||
@@ -455,6 +458,67 @@ func contentString(v interface{}) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// resolveImages converts non-data URLs to base64 data URLs so external LLM APIs can access them.
|
||||
func (p *OpenAIProvider) resolveImages(images []string) []string {
|
||||
if len(images) == 0 {
|
||||
return images
|
||||
}
|
||||
resolved := make([]string, 0, len(images))
|
||||
for _, img := range images {
|
||||
if strings.HasPrefix(img, "data:") {
|
||||
resolved = append(resolved, img)
|
||||
continue
|
||||
}
|
||||
dataURL, err := p.downloadAsDataURL(img)
|
||||
if err != nil {
|
||||
logger.Printf("[openai] 图片下载失败, 保留原始 URL: %s, err=%v", img, err)
|
||||
resolved = append(resolved, img) // 保留原始 URL 作为 fallback
|
||||
continue
|
||||
}
|
||||
resolved = append(resolved, dataURL)
|
||||
}
|
||||
return resolved
|
||||
}
|
||||
|
||||
// downloadAsDataURL downloads an image from a URL and returns it as a base64 data URL.
|
||||
func (p *OpenAIProvider) downloadAsDataURL(url string) (string, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("创建请求失败: %w", err)
|
||||
}
|
||||
|
||||
resp, err := p.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("下载失败: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("HTTP %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
// 限制最大 20MB
|
||||
const maxSize = 20 * 1024 * 1024
|
||||
body, err := io.ReadAll(io.LimitReader(resp.Body, maxSize+1))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("读取失败: %w", err)
|
||||
}
|
||||
if len(body) > maxSize {
|
||||
return "", fmt.Errorf("图片过大: %d bytes", len(body))
|
||||
}
|
||||
|
||||
mimeType := resp.Header.Get("Content-Type")
|
||||
if mimeType == "" {
|
||||
mimeType = http.DetectContentType(body)
|
||||
}
|
||||
|
||||
b64 := base64.StdEncoding.EncodeToString(body)
|
||||
return fmt.Sprintf("data:%s;base64,%s", mimeType, b64), nil
|
||||
}
|
||||
|
||||
// buildContent converts text + optional images to API content format.
|
||||
// Returns a plain string if no images, or a multimodal array otherwise.
|
||||
func buildContent(text string, images []string) interface{} {
|
||||
|
||||
@@ -19,6 +19,7 @@ const (
|
||||
PurposeToolCalling ModelPurpose = "tool_calling"
|
||||
PurposeMemoryExtraction ModelPurpose = "memory_extraction"
|
||||
PurposeVision ModelPurpose = "vision"
|
||||
PurposeOCR ModelPurpose = "ocr"
|
||||
)
|
||||
|
||||
// ErrModelNotRequired is returned when an optional model is unavailable.
|
||||
|
||||
@@ -167,6 +167,31 @@ func (c *Client) GetByID(ctx context.Context, id string) (*model.MemoryEntry, er
|
||||
return &result.Memory, nil
|
||||
}
|
||||
|
||||
// Update 更新记忆
|
||||
func (c *Client) Update(ctx context.Context, entry *model.MemoryEntry) error {
|
||||
body, _ := json.Marshal(map[string]interface{}{
|
||||
"content": entry.Content,
|
||||
"summary": entry.Summary,
|
||||
"category": string(entry.Category),
|
||||
"priority": int(entry.Priority),
|
||||
"importance": entry.Importance,
|
||||
"keywords": entry.Keywords,
|
||||
"source": entry.Source,
|
||||
})
|
||||
|
||||
resp, err := c.doRequest(ctx, http.MethodPut, c.baseURL+"/api/v1/memories/"+entry.ID, body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("更新记忆失败: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode >= 300 {
|
||||
respBody, _ := io.ReadAll(resp.Body)
|
||||
return fmt.Errorf("更新记忆失败 (%d): %s", resp.StatusCode, string(respBody))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete 删除记忆
|
||||
func (c *Client) Delete(ctx context.Context, id string) error {
|
||||
resp, err := c.doRequest(ctx, http.MethodDelete, c.baseURL+"/api/v1/memories/"+id, nil)
|
||||
|
||||
@@ -8,6 +8,17 @@ type EnrichmentData struct {
|
||||
ThoughtOutline string
|
||||
IoTSummary string
|
||||
KnowledgeInfo string
|
||||
|
||||
// Pending tool results from async execution (keyed by tool call ID)
|
||||
PendingToolResults []PendingToolResult
|
||||
}
|
||||
|
||||
// PendingToolResult holds the result of a tool that completed asynchronously.
|
||||
type PendingToolResult struct {
|
||||
ToolCallID string `json:"tool_call_id"`
|
||||
ToolName string `json:"tool_name"`
|
||||
Result string `json:"result"`
|
||||
Success bool `json:"success"`
|
||||
}
|
||||
|
||||
// SessionEnrichmentStore is a thread-safe per-session cache for async
|
||||
@@ -25,8 +36,15 @@ func NewEnrichmentStore() *SessionEnrichmentStore {
|
||||
}
|
||||
}
|
||||
|
||||
// Get returns stored enrichment for a session and clears it (one-shot consumption).
|
||||
// Get returns stored enrichment for a session (does NOT clear; results may be reused).
|
||||
func (s *SessionEnrichmentStore) Get(sessionID string) *EnrichmentData {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return s.data[sessionID]
|
||||
}
|
||||
|
||||
// Pop returns stored enrichment for a session and clears it (one-shot consumption).
|
||||
func (s *SessionEnrichmentStore) Pop(sessionID string) *EnrichmentData {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
d, ok := s.data[sessionID]
|
||||
@@ -45,3 +63,32 @@ func (s *SessionEnrichmentStore) Store(sessionID string, d *EnrichmentData) {
|
||||
s.data[sessionID] = d
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
// AppendToolResult adds a completed tool result to the session's enrichment data.
|
||||
func (s *SessionEnrichmentStore) AppendToolResult(sessionID string, r PendingToolResult) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
d, ok := s.data[sessionID]
|
||||
if !ok {
|
||||
d = &EnrichmentData{}
|
||||
s.data[sessionID] = d
|
||||
}
|
||||
d.PendingToolResults = append(d.PendingToolResults, r)
|
||||
}
|
||||
|
||||
// ---- Global pending tool store (used by Synthesizer for async tool results) ----
|
||||
|
||||
var globalPendingToolStore *SessionEnrichmentStore
|
||||
var pendingToolStoreOnce sync.Once
|
||||
|
||||
// InitGlobalPendingToolStore initializes the singleton.
|
||||
func InitGlobalPendingToolStore() {
|
||||
pendingToolStoreOnce.Do(func() {
|
||||
globalPendingToolStore = NewEnrichmentStore()
|
||||
})
|
||||
}
|
||||
|
||||
// GetGlobalPendingToolStore returns the singleton, or nil if not initialized.
|
||||
func GetGlobalPendingToolStore() *SessionEnrichmentStore {
|
||||
return globalPendingToolStore
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ type Orchestrator struct {
|
||||
msgScheduler *scheduler.MessageScheduler
|
||||
emotionTracker *persona.EmotionTracker
|
||||
toolRegistry *plgManager.ToolRegistry
|
||||
visionProvider llm.LLMProvider // 视觉模型 (图片预处理/OCR)
|
||||
}
|
||||
|
||||
// SetResponseCache sets the response cache (optional, for Phase 0.2).
|
||||
@@ -71,6 +72,11 @@ func (o *Orchestrator) SetToolRegistry(tr *plgManager.ToolRegistry) {
|
||||
o.synthesizer.toolRegistry = tr
|
||||
}
|
||||
|
||||
// SetVisionProvider sets the vision model provider for image preprocessing.
|
||||
func (o *Orchestrator) SetVisionProvider(vp llm.LLMProvider) {
|
||||
o.visionProvider = vp
|
||||
}
|
||||
|
||||
// getBus returns the bus or a nop fallback.
|
||||
func (o *Orchestrator) getBus() bus.Bus {
|
||||
if o.eventBus == nil {
|
||||
@@ -149,7 +155,27 @@ func (o *Orchestrator) ProcessInput(
|
||||
UserID: params.UserID,
|
||||
})
|
||||
|
||||
// 1. 意图分析
|
||||
// 0.5 图片预处理: 使用视觉模型分析图片,将描述注入消息
|
||||
if len(params.Images) > 0 && o.visionProvider != nil {
|
||||
startTime := time.Now()
|
||||
augmented := o.preprocessImages(ctx, params.Message, params.Images)
|
||||
if augmented != params.Message {
|
||||
params.Message = augmented
|
||||
logger.Printf("[orchestrator] 图片预处理耗时: %v, 原消息=%d字, 增强后=%d字",
|
||||
time.Since(startTime), len([]rune(params.Message))-len([]rune(augmented))+len([]rune(params.Message)), len([]rune(augmented)))
|
||||
}
|
||||
// 预处理后清空原始图片,避免后续传给不支持多模态的 Chat 模型
|
||||
params.Images = nil
|
||||
} else if len(params.Images) > 0 {
|
||||
// 未配置 Vision 模型时,告知用户该模型不支持图片,并清空图片避免报错
|
||||
if params.Message == "" {
|
||||
params.Message = "(用户发送了一张图片,但当前未配置视觉模型,无法识别图片内容)"
|
||||
}
|
||||
logger.Printf("[orchestrator] 视觉模型未配置,丢弃 %d 张图片", len(params.Images))
|
||||
params.Images = nil
|
||||
}
|
||||
|
||||
// 1. 意图分析
|
||||
startTime := time.Now()
|
||||
intent, err := o.intentAnalyzer.Analyze(ctx, params.Message)
|
||||
if err != nil || intent == nil {
|
||||
@@ -247,17 +273,39 @@ func (o *Orchestrator) ProcessInput(
|
||||
resultCh = o.subManager.Dispatch(subCtx, intent, params.Message, createParams)
|
||||
}
|
||||
|
||||
// 3.5 确保全局工具结果存储已初始化
|
||||
InitGlobalPendingToolStore()
|
||||
|
||||
// 4. 加载上一轮异步完成的子会话富化结果
|
||||
var prevEnrichment *EnrichmentData
|
||||
if o.enrichmentStore != nil {
|
||||
prevEnrichment = o.enrichmentStore.Get(params.SessionID)
|
||||
if prevEnrichment != nil {
|
||||
logger.Printf("[orchestrator] 加载上一轮富化结果: memory=%t thought=%t iot=%t knowledge=%t",
|
||||
prevEnrichment.MemorySummary != "",
|
||||
prevEnrichment.ThoughtOutline != "",
|
||||
prevEnrichment.IoTSummary != "",
|
||||
prevEnrichment.KnowledgeInfo != "")
|
||||
prevEnrichment = o.enrichmentStore.Pop(params.SessionID)
|
||||
// Also merge any pending tool results from the global store
|
||||
if globalStore := GetGlobalPendingToolStore(); globalStore != nil {
|
||||
if toolData := globalStore.Pop(params.SessionID); toolData != nil && len(toolData.PendingToolResults) > 0 {
|
||||
if prevEnrichment == nil {
|
||||
prevEnrichment = &EnrichmentData{}
|
||||
}
|
||||
prevEnrichment.PendingToolResults = append(prevEnrichment.PendingToolResults, toolData.PendingToolResults...)
|
||||
logger.Printf("[orchestrator] 合并后台工具结果 %d 条", len(toolData.PendingToolResults))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Still check global store even if enrichmentStore is not set
|
||||
if globalStore := GetGlobalPendingToolStore(); globalStore != nil {
|
||||
if toolData := globalStore.Pop(params.SessionID); toolData != nil && len(toolData.PendingToolResults) > 0 {
|
||||
prevEnrichment = toolData
|
||||
logger.Printf("[orchestrator] 加载后台工具结果 %d 条", len(toolData.PendingToolResults))
|
||||
}
|
||||
}
|
||||
}
|
||||
if prevEnrichment != nil {
|
||||
logger.Printf("[orchestrator] 加载上一轮富化结果: memory=%t thought=%t iot=%t knowledge=%t tools=%d",
|
||||
prevEnrichment.MemorySummary != "",
|
||||
prevEnrichment.ThoughtOutline != "",
|
||||
prevEnrichment.IoTSummary != "",
|
||||
prevEnrichment.KnowledgeInfo != "",
|
||||
len(prevEnrichment.PendingToolResults))
|
||||
}
|
||||
|
||||
// 5. 先构建基础综合参数(不含子会话结果),开始合成
|
||||
@@ -284,6 +332,7 @@ func (o *Orchestrator) ProcessInput(
|
||||
synthParams.ThoughtOutline = prevEnrichment.ThoughtOutline
|
||||
synthParams.IoTSummary = prevEnrichment.IoTSummary
|
||||
synthParams.KnowledgeInfo = prevEnrichment.KnowledgeInfo
|
||||
synthParams.PendingToolResults = prevEnrichment.PendingToolResults
|
||||
}
|
||||
|
||||
// 异步收集子会话结果,存入 enrichmentStore 供下一轮使用
|
||||
@@ -324,7 +373,7 @@ func (o *Orchestrator) ProcessInput(
|
||||
}()
|
||||
|
||||
// 5. 调用 Synthesizer 流式生成最终回复
|
||||
chunkCh, err := o.synthesizer.Synthesize(ctx, synthParams)
|
||||
chunkCh, err := o.synthesizer.Synthesize(ctx, synthParams, eventCh)
|
||||
if err != nil {
|
||||
logger.Printf("[orchestrator] 综合器启动失败: %v", err)
|
||||
eventCh <- model.StreamEvent{
|
||||
@@ -601,6 +650,46 @@ func (o *Orchestrator) CacheMessage(sessionID string, role model.Role, content s
|
||||
}
|
||||
}
|
||||
|
||||
// preprocessImages uses the vision model to analyze images and augments the user message.
|
||||
// For standalone images (no text): generates a comprehensive description as the message.
|
||||
// For text+images: appends image descriptions as contextual annotations.
|
||||
func (o *Orchestrator) preprocessImages(ctx context.Context, message string, images []string) string {
|
||||
var prompt string
|
||||
if message == "" {
|
||||
prompt = "请详细描述这张图片的内容,包括场景、物体、人物、文字(如有)、颜色、氛围等所有视觉信息。"
|
||||
} else {
|
||||
prompt = fmt.Sprintf("用户的问题是:「%s」\n\n请根据用户的问题,分析这张图片中相关的视觉信息,帮助回答用户的问题。如果图片中有文字,请完整提取。", message)
|
||||
}
|
||||
|
||||
var descriptions []string
|
||||
for i, img := range images {
|
||||
resp, err := o.visionProvider.Chat(ctx, []model.LLMMessage{
|
||||
{Role: model.RoleUser, Content: prompt, Images: []string{img}},
|
||||
})
|
||||
if err != nil {
|
||||
logger.Printf("[orchestrator] 图片 %d 预处理失败: %v", i, err)
|
||||
continue
|
||||
}
|
||||
if resp.Content != "" {
|
||||
descriptions = append(descriptions, resp.Content)
|
||||
}
|
||||
}
|
||||
|
||||
if len(descriptions) == 0 {
|
||||
return message
|
||||
}
|
||||
|
||||
if message == "" {
|
||||
return strings.Join(descriptions, "\n\n")
|
||||
}
|
||||
|
||||
augmented := message
|
||||
for i, desc := range descriptions {
|
||||
augmented += fmt.Sprintf("\n\n[图片%d的视觉分析]: %s", i+1, desc)
|
||||
}
|
||||
return augmented
|
||||
}
|
||||
|
||||
// Ensure time, memory are used
|
||||
var _ = time.Now
|
||||
var _ = memory.NewRetriever
|
||||
|
||||
@@ -14,7 +14,7 @@ var codeBlockPattern = regexp.MustCompile("`{3}([^\n]*)\n([\\s\\S]*?)`{3}")
|
||||
var markdownPatterns = []*regexp.Regexp{
|
||||
regexp.MustCompile(`^#{1,6}\s`), // headings
|
||||
regexp.MustCompile(`\*\*[^*]+\*\*`), // bold
|
||||
regexp.MustCompile(`(?<!\*)\*[^*]+\*(?!\*)`), // italic (single *)
|
||||
regexp.MustCompile(`(?:^|[^*])\*([^*]+)\*(?:[^*]|$)`), // italic (*text*)
|
||||
regexp.MustCompile(`\[([^\]]+)\]\(([^\)]+)\)`), // links [text](url)
|
||||
regexp.MustCompile(`^[\-\*]\s`), // unordered list
|
||||
regexp.MustCompile(`^\d+\.\s`), // ordered list
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/yourname/cyrene-ai/ai-core/internal/llm"
|
||||
"github.com/yourname/cyrene-ai/ai-core/internal/model"
|
||||
@@ -30,23 +31,25 @@ func NewSynthesizer(llmAdapter *llm.Adapter, toolRegistry *plgManager.ToolRegist
|
||||
|
||||
// SynthesizeParams 综合参数
|
||||
type SynthesizeParams struct {
|
||||
UserID string
|
||||
SessionID string
|
||||
UserMessage string
|
||||
Images []string // 图片 base64 data URL (多模态)
|
||||
Nickname string
|
||||
PersonaPrompt string // 完整人格提示词
|
||||
DialogHistory []model.LLMMessage // 对话历史
|
||||
MemorySummary string // 记忆检索摘要
|
||||
ThoughtOutline string // 通用对话思考
|
||||
IoTSummary string // IoT 操作摘要
|
||||
DeviceContext string // 设备状态上下文
|
||||
KnowledgeInfo string // 知识库检索摘要
|
||||
Mode string // text / voice_assistant
|
||||
UserID string
|
||||
SessionID string
|
||||
UserMessage string
|
||||
Images []string // 图片 base64 data URL (多模态)
|
||||
Nickname string
|
||||
PersonaPrompt string // 完整人格提示词
|
||||
DialogHistory []model.LLMMessage // 对话历史
|
||||
MemorySummary string // 记忆检索摘要
|
||||
ThoughtOutline string // 通用对话思考
|
||||
IoTSummary string // IoT 操作摘要
|
||||
DeviceContext string // 设备状态上下文
|
||||
KnowledgeInfo string // 知识库检索摘要
|
||||
PendingToolResults []PendingToolResult // 上一轮异步完成的工具结果
|
||||
Mode string // text / voice_assistant
|
||||
}
|
||||
|
||||
// Synthesize 综合所有子会话结果,流式生成最终回复
|
||||
func (s *Synthesizer) Synthesize(ctx context.Context, params SynthesizeParams) (<-chan llm.StreamChunk, error) {
|
||||
// Synthesize 综合所有子会话结果,流式生成最终回复。
|
||||
// eventCh receives tool progress events; pass nil to suppress.
|
||||
func (s *Synthesizer) Synthesize(ctx context.Context, params SynthesizeParams, eventCh chan<- model.StreamEvent) (<-chan llm.StreamChunk, error) {
|
||||
messages := s.buildSynthesizeMessages(params)
|
||||
|
||||
logger.Printf("[synthesizer] 开始综合 (上下文 %d 条消息)", len(messages))
|
||||
@@ -62,7 +65,9 @@ func (s *Synthesizer) Synthesize(ctx context.Context, params SynthesizeParams) (
|
||||
return nil, err
|
||||
}
|
||||
|
||||
maxRounds := 5
|
||||
const toolDeadline = 8 * time.Second
|
||||
const maxRounds = 5
|
||||
|
||||
for round := 0; len(resp.ToolCalls) > 0 && round < maxRounds; round++ {
|
||||
logger.Printf("[synthesizer] LLM 请求 %d 个工具调用 (round=%d)", len(resp.ToolCalls), round)
|
||||
|
||||
@@ -80,7 +85,12 @@ func (s *Synthesizer) Synthesize(ctx context.Context, params SynthesizeParams) (
|
||||
args = make(map[string]interface{})
|
||||
}
|
||||
|
||||
result, execErr := s.toolRegistry.Execute(ctx, tc.Name, args)
|
||||
s.emitToolProgress(eventCh, tc.Name, "started", 0, "正在执行 "+tc.Name)
|
||||
|
||||
toolCtx, cancel := context.WithTimeout(ctx, toolDeadline)
|
||||
result, execErr := s.toolRegistry.Execute(toolCtx, tc.Name, args)
|
||||
cancel()
|
||||
|
||||
if execErr != nil {
|
||||
logger.Printf("[synthesizer] 工具 %s 执行失败: %v", tc.Name, execErr)
|
||||
}
|
||||
@@ -88,6 +98,19 @@ func (s *Synthesizer) Synthesize(ctx context.Context, params SynthesizeParams) (
|
||||
result = &plgSDK.ToolResult{ToolName: tc.Name, Success: false, Error: execErr.Error()}
|
||||
}
|
||||
|
||||
// Async fallback: if tool timed out, store for next turn
|
||||
if toolCtx.Err() == context.DeadlineExceeded {
|
||||
s.emitToolProgress(eventCh, tc.Name, "running", 0.5, tc.Name+" 执行时间较长,转入后台继续...")
|
||||
go s.executeAsyncAndStore(tc, args, params.SessionID, eventCh)
|
||||
result = &plgSDK.ToolResult{
|
||||
ToolName: tc.Name,
|
||||
Success: true,
|
||||
Output: fmt.Sprintf("[后台执行中] %s 正在后台运行,结果将在下一轮对话中返回。你可以继续聊天。", tc.Name),
|
||||
}
|
||||
} else {
|
||||
s.emitToolProgress(eventCh, tc.Name, "completed", 1.0, "")
|
||||
}
|
||||
|
||||
resultJSON, _ := json.Marshal(result)
|
||||
messages = append(messages, model.LLMMessage{
|
||||
Role: model.RoleTool,
|
||||
@@ -120,6 +143,51 @@ func (s *Synthesizer) Synthesize(ctx context.Context, params SynthesizeParams) (
|
||||
return ch, nil
|
||||
}
|
||||
|
||||
// emitToolProgress sends a StreamToolProgress event if eventCh is available.
|
||||
func (s *Synthesizer) emitToolProgress(eventCh chan<- model.StreamEvent, name, status string, progress float64, message string) {
|
||||
if eventCh == nil {
|
||||
return
|
||||
}
|
||||
select {
|
||||
case eventCh <- model.StreamEvent{
|
||||
Type: model.StreamToolProgress,
|
||||
ToolProgress: &model.ToolProgressInfo{
|
||||
ToolName: name,
|
||||
Status: status,
|
||||
Progress: progress,
|
||||
Message: message,
|
||||
},
|
||||
}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
// executeAsyncAndStore runs a tool in background and stores the result for the next turn.
|
||||
func (s *Synthesizer) executeAsyncAndStore(tc model.ToolCall, args map[string]interface{}, sessionID string, eventCh chan<- model.StreamEvent) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
|
||||
result, err := s.toolRegistry.Execute(ctx, tc.Name, args)
|
||||
if err != nil {
|
||||
logger.Printf("[synthesizer] 后台工具 %s 执行失败: %v", tc.Name, err)
|
||||
s.emitToolProgress(eventCh, tc.Name, "failed", 1.0, tc.Name+" 后台执行失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
s.emitToolProgress(eventCh, tc.Name, "completed", 1.0, tc.Name+" 后台执行完成")
|
||||
|
||||
resultJSON, _ := json.Marshal(result)
|
||||
store := GetGlobalPendingToolStore()
|
||||
if store != nil {
|
||||
store.AppendToolResult(sessionID, PendingToolResult{
|
||||
ToolCallID: tc.ID,
|
||||
ToolName: tc.Name,
|
||||
Result: string(resultJSON),
|
||||
Success: result != nil && result.Success,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// buildSynthesizeMessages 构建综合用的 LLM 消息列表
|
||||
func (s *Synthesizer) buildSynthesizeMessages(params SynthesizeParams) []model.LLMMessage {
|
||||
var messages []model.LLMMessage
|
||||
@@ -174,6 +242,23 @@ func (s *Synthesizer) buildSynthesizeMessages(params SynthesizeParams) []model.L
|
||||
})
|
||||
}
|
||||
|
||||
// 注入上一轮异步工具执行结果
|
||||
if len(params.PendingToolResults) > 0 {
|
||||
var sb strings.Builder
|
||||
sb.WriteString("【上一轮后台工具执行结果】\n")
|
||||
for _, ptr := range params.PendingToolResults {
|
||||
status := "成功"
|
||||
if !ptr.Success {
|
||||
status = "失败"
|
||||
}
|
||||
sb.WriteString(fmt.Sprintf("- %s (%s): %s\n", ptr.ToolName, status, ptr.Result))
|
||||
}
|
||||
messages = append(messages, model.LLMMessage{
|
||||
Role: model.RoleSystem,
|
||||
Content: sb.String(),
|
||||
})
|
||||
}
|
||||
|
||||
// 注入对话历史
|
||||
if len(params.DialogHistory) > 0 {
|
||||
messages = append(messages, params.DialogHistory...)
|
||||
|
||||
@@ -2,12 +2,15 @@ package subsession
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/yourname/cyrene-ai/pkg/logger"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/yourname/cyrene-ai/ai-core/internal/llm"
|
||||
"github.com/yourname/cyrene-ai/ai-core/internal/memory"
|
||||
"github.com/yourname/cyrene-ai/ai-core/internal/model"
|
||||
"github.com/yourname/cyrene-ai/pkg/logger"
|
||||
)
|
||||
|
||||
// MemoryRetriever 记忆检索接口
|
||||
@@ -16,9 +19,12 @@ type MemoryRetriever interface {
|
||||
}
|
||||
|
||||
// MemoryProvider 记忆检索子会话提供者
|
||||
// 职责:检索与当前对话相关的用户记忆,排序去重,返回结构化摘要
|
||||
// 职责:检索与当前对话相关的用户记忆,排序去重,返回结构化摘要。
|
||||
// 支持 LLM 驱动的模糊关键词扩展搜索。
|
||||
type MemoryProvider struct {
|
||||
retriever MemoryRetriever
|
||||
retriever MemoryRetriever
|
||||
llmAdapter *llm.Adapter
|
||||
memClient *memory.Client
|
||||
}
|
||||
|
||||
// NewMemoryProvider 创建记忆检索子会话提供者
|
||||
@@ -28,6 +34,12 @@ func NewMemoryProvider(retriever MemoryRetriever) *MemoryProvider {
|
||||
}
|
||||
}
|
||||
|
||||
// SetFuzzySearch enables LLM-driven fuzzy keyword expansion for broader memory retrieval.
|
||||
func (p *MemoryProvider) SetFuzzySearch(llmAdapter *llm.Adapter, memClient *memory.Client) {
|
||||
p.llmAdapter = llmAdapter
|
||||
p.memClient = memClient
|
||||
}
|
||||
|
||||
func (p *MemoryProvider) Type() model.SubSessionType {
|
||||
return model.SubSessionMemory
|
||||
}
|
||||
@@ -93,6 +105,7 @@ func (p *MemoryProvider) Execute(ctx context.Context, subCtx []model.LLMMessage)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// Phase 1: exact/keyword retrieval
|
||||
memories, err := p.retriever.Retrieve(ctx, userID, userMessage)
|
||||
if err != nil {
|
||||
logger.Printf("[memory-subsession] 记忆检索失败: %v", err)
|
||||
@@ -101,6 +114,20 @@ func (p *MemoryProvider) Execute(ctx context.Context, subCtx []model.LLMMessage)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
seen := make(map[string]bool)
|
||||
for _, m := range memories {
|
||||
seen[m.ID] = true
|
||||
}
|
||||
|
||||
// Phase 2: LLM-driven fuzzy keyword expansion + semantic search
|
||||
fuzzyMemories := p.fuzzySearch(ctx, userID, userMessage)
|
||||
for _, m := range fuzzyMemories {
|
||||
if !seen[m.ID] {
|
||||
seen[m.ID] = true
|
||||
memories = append(memories, m)
|
||||
}
|
||||
}
|
||||
|
||||
// 转换为 MemorySnippet
|
||||
snippets := make([]model.MemorySnippet, 0, len(memories))
|
||||
for _, m := range memories {
|
||||
@@ -117,7 +144,7 @@ func (p *MemoryProvider) Execute(ctx context.Context, subCtx []model.LLMMessage)
|
||||
if len(snippets) == 0 {
|
||||
result.Summary = "(没有找到相关记忆)"
|
||||
} else {
|
||||
result.Summary = fmt.Sprintf("检索到 %d 条相关记忆", len(snippets))
|
||||
result.Summary = fmt.Sprintf("检索到 %d 条相关记忆(含模糊匹配)", len(snippets))
|
||||
// 按重要性列出前几条
|
||||
topCount := len(snippets)
|
||||
if topCount > 3 {
|
||||
@@ -138,6 +165,74 @@ func (p *MemoryProvider) Execute(ctx context.Context, subCtx []model.LLMMessage)
|
||||
}
|
||||
|
||||
result.Memories = snippets
|
||||
logger.Printf("[memory-subsession] 完成: %s", result.Summary)
|
||||
logger.Printf("[memory-subsession] 完成: %s (精确=%d, 模糊=%d)", result.Summary, len(memories)-len(fuzzyMemories), len(fuzzyMemories))
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// fuzzySearch expands the user message into fuzzy keywords via LLM and performs semantic search.
|
||||
func (p *MemoryProvider) fuzzySearch(ctx context.Context, userID, userMessage string) []memory.MemoryEntry {
|
||||
if p.llmAdapter == nil || p.memClient == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
keywords := p.expandKeywords(ctx, userMessage)
|
||||
if len(keywords) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
logger.Printf("[memory-subsession] 模糊关键词: %v", keywords)
|
||||
|
||||
var allResults []memory.MemoryEntry
|
||||
seen := make(map[string]bool)
|
||||
|
||||
for _, kw := range keywords {
|
||||
results, err := p.memClient.QueryByText(ctx, userID, kw, "", 0, 5)
|
||||
if err != nil {
|
||||
logger.Printf("[memory-subsession] 模糊搜索 '%s' 失败: %v", kw, err)
|
||||
continue
|
||||
}
|
||||
for _, m := range results {
|
||||
if !seen[m.ID] {
|
||||
seen[m.ID] = true
|
||||
allResults = append(allResults, m)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return allResults
|
||||
}
|
||||
|
||||
// expandKeywords uses LLM to generate fuzzy/related search keywords from the user message.
|
||||
func (p *MemoryProvider) expandKeywords(ctx context.Context, message string) []string {
|
||||
prompt := fmt.Sprintf(
|
||||
"从以下对话消息中提取 3-5 个可用于模糊搜索记忆的关键词。这些关键词应该是:\n"+
|
||||
"- 与话题相关的抽象概念\n- 同义词和相关词\n- 更宽泛或更具体的相关概念\n"+
|
||||
"- 不要包含消息中已经出现的原词\n\n"+
|
||||
"用户消息:「%s」\n\n"+
|
||||
"只输出 JSON 字符串数组,例如:[\"关键词1\",\"关键词2\"]", message)
|
||||
|
||||
resp, err := p.llmAdapter.Chat(ctx, []model.LLMMessage{
|
||||
{Role: model.RoleSystem, Content: "你是记忆搜索专家。输出 JSON 字符串数组。"},
|
||||
{Role: model.RoleUser, Content: prompt},
|
||||
})
|
||||
if err != nil {
|
||||
logger.Printf("[memory-subsession] 关键词扩展失败: %v", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
text := strings.TrimSpace(resp.Content)
|
||||
// Extract JSON array
|
||||
if idx := strings.Index(text, "["); idx >= 0 {
|
||||
if end := strings.LastIndex(text, "]"); end > idx {
|
||||
text = text[idx : end+1]
|
||||
}
|
||||
}
|
||||
|
||||
var keywords []string
|
||||
if err := json.Unmarshal([]byte(text), &keywords); err != nil {
|
||||
logger.Printf("[memory-subsession] 解析关键词 JSON 失败: %v (raw=%s)", err, resp.Content)
|
||||
return nil
|
||||
}
|
||||
|
||||
return keywords
|
||||
}
|
||||
|
||||
@@ -0,0 +1,217 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/yourname/cyrene-ai/ai-core/internal/host"
|
||||
)
|
||||
|
||||
// OSExecTool allows the AI to execute arbitrary commands in a full OS
|
||||
// environment (WSL or Docker container). Unlike host_exec which runs in
|
||||
// a restricted sandbox, this provides unrestricted OS access.
|
||||
type OSExecTool struct {
|
||||
manager *host.Manager
|
||||
}
|
||||
|
||||
// NewOSExecTool creates a new OS exec tool for full OS command execution.
|
||||
func NewOSExecTool(manager *host.Manager) *OSExecTool {
|
||||
return &OSExecTool{manager: manager}
|
||||
}
|
||||
|
||||
func (t *OSExecTool) Definition() ToolDefinition {
|
||||
return ToolDefinition{
|
||||
Name: "os_exec",
|
||||
Description: "在完整的操作系统环境(WSL/Docker容器)中执行任意命令。适用于复杂操作:安装软件包、编译大型项目、运行脚本、管理服务等。拥有完整的Linux系统权限,无命令限制。日常简单操作请使用 host_exec。",
|
||||
Parameters: map[string]interface{}{
|
||||
"type": "object",
|
||||
"properties": map[string]interface{}{
|
||||
"command": map[string]interface{}{
|
||||
"type": "string",
|
||||
"description": "要执行的命令,例如 'pip install pandas && python analyze.py' 或 'apt-get update && apt-get install -y ffmpeg'",
|
||||
},
|
||||
"work_dir": map[string]interface{}{
|
||||
"type": "string",
|
||||
"description": "工作目录。不指定则使用默认目录。",
|
||||
},
|
||||
"timeout_sec": map[string]interface{}{
|
||||
"type": "integer",
|
||||
"description": "超时时间(秒),默认30秒,最大300秒。复杂任务请设置更长的超时。",
|
||||
},
|
||||
},
|
||||
"required": []string{"command"},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (t *OSExecTool) Execute(ctx context.Context, args map[string]interface{}) (*ToolResult, error) {
|
||||
cmd, _ := args["command"].(string)
|
||||
if cmd == "" {
|
||||
return &ToolResult{
|
||||
ToolName: "os_exec",
|
||||
Success: false,
|
||||
Error: "command 参数不能为空",
|
||||
}, nil
|
||||
}
|
||||
|
||||
workDir, _ := args["work_dir"].(string)
|
||||
timeoutSec := 60 // Default longer timeout for complex operations
|
||||
if v, ok := args["timeout_sec"].(float64); ok {
|
||||
timeoutSec = int(v)
|
||||
}
|
||||
timeout := time.Duration(timeoutSec) * time.Second
|
||||
|
||||
result, err := t.manager.Exec(ctx, cmd, workDir, timeout)
|
||||
if err != nil && result == nil {
|
||||
return &ToolResult{
|
||||
ToolName: "os_exec",
|
||||
Success: false,
|
||||
Error: err.Error(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
data, _ := json.Marshal(map[string]interface{}{
|
||||
"command": cmd,
|
||||
"backend": t.manager.BackendName(),
|
||||
"exit_code": result.ExitCode,
|
||||
"duration": result.Duration,
|
||||
"timed_out": result.TimedOut,
|
||||
"stdout": result.Stdout,
|
||||
"stderr": result.Stderr,
|
||||
})
|
||||
|
||||
success := result.ExitCode == 0 && !result.TimedOut
|
||||
return &ToolResult{
|
||||
ToolName: "os_exec",
|
||||
Success: success,
|
||||
Data: string(data),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// OSFileTool provides unrestricted file system access within the OS environment.
|
||||
type OSFileTool struct {
|
||||
manager *host.Manager
|
||||
}
|
||||
|
||||
// NewOSFileTool creates a new OS file tool for full OS file operations.
|
||||
func NewOSFileTool(manager *host.Manager) *OSFileTool {
|
||||
return &OSFileTool{manager: manager}
|
||||
}
|
||||
|
||||
func (t *OSFileTool) Definition() ToolDefinition {
|
||||
return ToolDefinition{
|
||||
Name: "os_file",
|
||||
Description: "在完整OS环境中读写文件。支持在整个文件系统中自由操作:读取/写入/列出文件,无目录限制。适用于批量文件处理、日志分析、配置文件管理等复杂文件操作。日常简单文件操作请使用 host_file。",
|
||||
Parameters: map[string]interface{}{
|
||||
"type": "object",
|
||||
"properties": map[string]interface{}{
|
||||
"action": map[string]interface{}{
|
||||
"type": "string",
|
||||
"description": "操作类型: read, write, list",
|
||||
"enum": []string{"read", "write", "list"},
|
||||
},
|
||||
"path": map[string]interface{}{
|
||||
"type": "string",
|
||||
"description": "文件或目录路径",
|
||||
},
|
||||
"content": map[string]interface{}{
|
||||
"type": "string",
|
||||
"description": "写入内容 (仅 write 操作需要)",
|
||||
},
|
||||
},
|
||||
"required": []string{"action", "path"},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (t *OSFileTool) Execute(ctx context.Context, args map[string]interface{}) (*ToolResult, error) {
|
||||
action, _ := args["action"].(string)
|
||||
path, _ := args["path"].(string)
|
||||
if action == "" || path == "" {
|
||||
return &ToolResult{
|
||||
ToolName: "os_file",
|
||||
Success: false,
|
||||
Error: "action 和 path 参数不能为空",
|
||||
}, nil
|
||||
}
|
||||
|
||||
switch action {
|
||||
case "read":
|
||||
content, err := t.manager.ReadFile(path, 1024*1024)
|
||||
if err != nil {
|
||||
return &ToolResult{ToolName: "os_file", Success: false, Error: err.Error()}, nil
|
||||
}
|
||||
data, _ := json.Marshal(map[string]interface{}{
|
||||
"path": path,
|
||||
"content": content,
|
||||
"size": len(content),
|
||||
})
|
||||
return &ToolResult{ToolName: "os_file", Success: true, Data: string(data)}, nil
|
||||
|
||||
case "write":
|
||||
content, _ := args["content"].(string)
|
||||
if err := t.manager.WriteFile(path, content, 1024*1024); err != nil {
|
||||
return &ToolResult{ToolName: "os_file", Success: false, Error: err.Error()}, nil
|
||||
}
|
||||
data, _ := json.Marshal(map[string]interface{}{
|
||||
"path": path,
|
||||
"written": len(content),
|
||||
"status": "ok",
|
||||
})
|
||||
return &ToolResult{ToolName: "os_file", Success: true, Data: string(data)}, nil
|
||||
|
||||
case "list":
|
||||
entries, err := t.manager.ListDir(path)
|
||||
if err != nil {
|
||||
return &ToolResult{ToolName: "os_file", Success: false, Error: err.Error()}, nil
|
||||
}
|
||||
data, _ := json.Marshal(map[string]interface{}{
|
||||
"path": path,
|
||||
"entries": entries,
|
||||
"count": len(entries),
|
||||
})
|
||||
return &ToolResult{ToolName: "os_file", Success: true, Data: string(data)}, nil
|
||||
|
||||
default:
|
||||
return &ToolResult{ToolName: "os_file", Success: false, Error: fmt.Sprintf("不支持的操作: %s", action)}, nil
|
||||
}
|
||||
}
|
||||
|
||||
// OSSystemTool provides OS-level system information.
|
||||
type OSSystemTool struct {
|
||||
manager *host.Manager
|
||||
}
|
||||
|
||||
// NewOSSystemTool creates a new OS system info tool.
|
||||
func NewOSSystemTool(manager *host.Manager) *OSSystemTool {
|
||||
return &OSSystemTool{manager: manager}
|
||||
}
|
||||
|
||||
func (t *OSSystemTool) Definition() ToolDefinition {
|
||||
return ToolDefinition{
|
||||
Name: "os_system",
|
||||
Description: "获取完整OS环境的系统信息,包括操作系统详情、CPU架构、内存使用、磁盘空间等。与 host_system 不同,此工具返回的是WSL/容器内的完整Linux系统信息。",
|
||||
Parameters: map[string]interface{}{
|
||||
"type": "object",
|
||||
"properties": map[string]interface{}{
|
||||
"query": map[string]interface{}{
|
||||
"type": "string",
|
||||
"description": "查询类型: info(完整信息), memory(内存), cpu(CPU), disk(磁盘)",
|
||||
"enum": []string{"info", "memory", "cpu", "disk"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (t *OSSystemTool) Execute(ctx context.Context, args map[string]interface{}) (*ToolResult, error) {
|
||||
info := t.manager.SystemInfo()
|
||||
data, _ := json.Marshal(info)
|
||||
return &ToolResult{
|
||||
ToolName: "os_system",
|
||||
Success: true,
|
||||
Data: string(data),
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/yourname/cyrene-ai/ai-core/internal/host"
|
||||
)
|
||||
|
||||
func TestOSExecToolWSL(t *testing.T) {
|
||||
distro := os.Getenv("WSL_DISTRO")
|
||||
if distro == "" {
|
||||
t.Skip("WSL_DISTRO not set, skipping OS tool integration test")
|
||||
}
|
||||
backend := host.NewWSLBackend(distro, "cyrene", "test123", 30e9)
|
||||
mgr := host.NewManager(backend)
|
||||
|
||||
// Test os_exec
|
||||
t.Run("os_exec", func(t *testing.T) {
|
||||
tool := NewOSExecTool(mgr)
|
||||
def := tool.Definition()
|
||||
if def.Name != "os_exec" {
|
||||
t.Fatalf("unexpected name: %s", def.Name)
|
||||
}
|
||||
result, err := tool.Execute(context.Background(), map[string]interface{}{
|
||||
"command": "echo 'os_exec works!' && uname -a",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("execute error: %v", err)
|
||||
}
|
||||
if !result.Success {
|
||||
t.Fatalf("exec failed: %s", result.Error)
|
||||
}
|
||||
if !strings.Contains(result.Data, "os_exec works!") {
|
||||
t.Fatalf("unexpected output: %s", result.Data)
|
||||
}
|
||||
t.Logf("os_exec OK: data len=%d", len(result.Data))
|
||||
})
|
||||
|
||||
// Test os_file
|
||||
t.Run("os_file", func(t *testing.T) {
|
||||
tool := NewOSFileTool(mgr)
|
||||
def := tool.Definition()
|
||||
if def.Name != "os_file" {
|
||||
t.Fatalf("unexpected name: %s", def.Name)
|
||||
}
|
||||
|
||||
// Write
|
||||
r, err := tool.Execute(context.Background(), map[string]interface{}{
|
||||
"action": "write",
|
||||
"path": "/tmp/cyrene-os-tool-test.txt",
|
||||
"content": "OS tool integration test",
|
||||
})
|
||||
if err != nil || !r.Success {
|
||||
t.Fatalf("os_file write failed: err=%v, errMsg=%s", err, r.Error)
|
||||
}
|
||||
|
||||
// Read
|
||||
r, err = tool.Execute(context.Background(), map[string]interface{}{
|
||||
"action": "read",
|
||||
"path": "/tmp/cyrene-os-tool-test.txt",
|
||||
})
|
||||
if err != nil || !r.Success {
|
||||
t.Fatalf("os_file read failed: err=%v, errMsg=%s", err, r.Error)
|
||||
}
|
||||
if !strings.Contains(r.Data, "OS tool integration test") {
|
||||
t.Fatalf("content mismatch: %s", r.Data)
|
||||
}
|
||||
|
||||
// List
|
||||
r, err = tool.Execute(context.Background(), map[string]interface{}{
|
||||
"action": "list",
|
||||
"path": "/tmp",
|
||||
})
|
||||
if err != nil || !r.Success {
|
||||
t.Fatalf("os_file list failed: err=%v, errMsg=%s", err, r.Error)
|
||||
}
|
||||
t.Logf("os_file OK: write+read+list all pass")
|
||||
})
|
||||
|
||||
// Test os_system
|
||||
t.Run("os_system", func(t *testing.T) {
|
||||
tool := NewOSSystemTool(mgr)
|
||||
def := tool.Definition()
|
||||
if def.Name != "os_system" {
|
||||
t.Fatalf("unexpected name: %s", def.Name)
|
||||
}
|
||||
result, err := tool.Execute(context.Background(), map[string]interface{}{})
|
||||
if err != nil {
|
||||
t.Fatalf("execute error: %v", err)
|
||||
}
|
||||
if !result.Success {
|
||||
t.Fatalf("os_system failed: %s", result.Error)
|
||||
}
|
||||
if !strings.Contains(result.Data, "wsl") {
|
||||
t.Fatalf("expected wsl backend info: %s", result.Data)
|
||||
}
|
||||
t.Logf("os_system OK: data len=%d", len(result.Data))
|
||||
})
|
||||
}
|
||||
@@ -13,7 +13,7 @@ func TestHostExecToolDefinition(t *testing.T) {
|
||||
cfg := host.DefaultSandboxConfig()
|
||||
cfg.AllowedDirs = []string{os.TempDir()}
|
||||
sandbox := host.NewSandbox(cfg)
|
||||
mgr := host.NewManager(sandbox)
|
||||
mgr := host.NewManager(host.NewDirectBackend(sandbox))
|
||||
|
||||
tool := NewHostExecTool(mgr)
|
||||
def := tool.Definition()
|
||||
@@ -40,7 +40,7 @@ func TestHostFileToolDefinition(t *testing.T) {
|
||||
tmpDir := os.TempDir()
|
||||
cfg.AllowedDirs = []string{tmpDir}
|
||||
sandbox := host.NewSandbox(cfg)
|
||||
mgr := host.NewManager(sandbox)
|
||||
mgr := host.NewManager(host.NewDirectBackend(sandbox))
|
||||
mgr.SetAllowedDirs([]string{tmpDir})
|
||||
|
||||
tool := NewHostFileTool(mgr)
|
||||
@@ -67,7 +67,7 @@ func TestHostFileToolDefinition(t *testing.T) {
|
||||
func TestHostSystemToolDefinition(t *testing.T) {
|
||||
cfg := host.DefaultSandboxConfig()
|
||||
sandbox := host.NewSandbox(cfg)
|
||||
mgr := host.NewManager(sandbox)
|
||||
mgr := host.NewManager(host.NewDirectBackend(sandbox))
|
||||
|
||||
tool := NewHostSystemTool(mgr)
|
||||
def := tool.Definition()
|
||||
|
||||
@@ -40,7 +40,7 @@ func TestEncodeImageToDataURL_InvalidPath(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestVisionToolDefinition(t *testing.T) {
|
||||
tool := NewVisionTool()
|
||||
tool := NewVisionTool(nil)
|
||||
def := tool.Definition()
|
||||
if def.Name != "vision_analyze" {
|
||||
t.Fatalf("unexpected tool name: %s", def.Name)
|
||||
@@ -68,7 +68,7 @@ func TestVisionToolExecute(t *testing.T) {
|
||||
}
|
||||
defer os.Remove(tmpPath)
|
||||
|
||||
tool := NewVisionTool()
|
||||
tool := NewVisionTool(nil)
|
||||
ctx := context.Background()
|
||||
result, err := tool.Execute(ctx, map[string]interface{}{
|
||||
"image_path": tmpPath,
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// ScheduleRule defines a time-based interval rule.
|
||||
type ScheduleRule struct {
|
||||
Name string `json:"name"`
|
||||
Days []string `json:"days"` // monday, tuesday, wednesday, thursday, friday, saturday, sunday
|
||||
TimeRange string `json:"time_range"` // "HH:MM-HH:MM"
|
||||
Except []string `json:"except"` // ["HH:MM-HH:MM", ...]
|
||||
IntervalMinutes int `json:"interval_minutes"`
|
||||
}
|
||||
|
||||
// ThinkingScheduleConfig is the full schedule configuration.
|
||||
type ThinkingScheduleConfig struct {
|
||||
Version string `json:"version"`
|
||||
DefaultIntervalMinutes int `json:"default_interval_minutes"`
|
||||
Rules []ScheduleRule `json:"rules"`
|
||||
}
|
||||
|
||||
// DefaultThinkingScheduleConfig returns the default schedule with two rules.
|
||||
func DefaultThinkingScheduleConfig() *ThinkingScheduleConfig {
|
||||
return &ThinkingScheduleConfig{
|
||||
Version: "1.0",
|
||||
DefaultIntervalMinutes: 5,
|
||||
Rules: []ScheduleRule{
|
||||
{
|
||||
Name: "night",
|
||||
Days: []string{"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"},
|
||||
TimeRange: "23:00-07:00",
|
||||
IntervalMinutes: 30,
|
||||
},
|
||||
{
|
||||
Name: "weekday_work",
|
||||
Days: []string{"monday", "tuesday", "wednesday", "thursday", "friday"},
|
||||
TimeRange: "09:00-17:00",
|
||||
Except: []string{"12:00-14:00", "15:00-15:30"},
|
||||
IntervalMinutes: 30,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// ThinkingScheduleStore persists the schedule config to a JSON file.
|
||||
type ThinkingScheduleStore struct {
|
||||
mu sync.RWMutex
|
||||
path string
|
||||
config *ThinkingScheduleConfig
|
||||
}
|
||||
|
||||
// NewThinkingScheduleStore creates a store, creating the file with defaults if it does not exist.
|
||||
func NewThinkingScheduleStore(path string) (*ThinkingScheduleStore, error) {
|
||||
s := &ThinkingScheduleStore{
|
||||
path: path,
|
||||
config: nil,
|
||||
}
|
||||
if err := s.load(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (s *ThinkingScheduleStore) load() error {
|
||||
data, err := os.ReadFile(s.path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
s.config = DefaultThinkingScheduleConfig()
|
||||
return s.save()
|
||||
}
|
||||
return fmt.Errorf("read thinking schedule file: %w", err)
|
||||
}
|
||||
if len(data) == 0 {
|
||||
s.config = DefaultThinkingScheduleConfig()
|
||||
return s.save()
|
||||
}
|
||||
var cfg ThinkingScheduleConfig
|
||||
if err := json.Unmarshal(data, &cfg); err != nil {
|
||||
return fmt.Errorf("parse thinking schedule: %w", err)
|
||||
}
|
||||
if cfg.Version == "" {
|
||||
cfg.Version = "1.0"
|
||||
}
|
||||
if cfg.DefaultIntervalMinutes <= 0 {
|
||||
cfg.DefaultIntervalMinutes = 5
|
||||
}
|
||||
if cfg.Rules == nil {
|
||||
cfg.Rules = []ScheduleRule{}
|
||||
}
|
||||
s.config = &cfg
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ThinkingScheduleStore) save() error {
|
||||
data, err := json.MarshalIndent(s.config, "", " ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal thinking schedule: %w", err)
|
||||
}
|
||||
tmpPath := s.path + ".tmp"
|
||||
if err := os.WriteFile(tmpPath, data, 0640); err != nil {
|
||||
return fmt.Errorf("write thinking schedule: %w", err)
|
||||
}
|
||||
return os.Rename(tmpPath, s.path)
|
||||
}
|
||||
|
||||
// GetConfig returns the current config (read-only).
|
||||
func (s *ThinkingScheduleStore) GetConfig() *ThinkingScheduleConfig {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return s.config
|
||||
}
|
||||
|
||||
// SetConfig validates and persists a new config.
|
||||
func (s *ThinkingScheduleStore) SetConfig(cfg *ThinkingScheduleConfig) error {
|
||||
if cfg == nil {
|
||||
return fmt.Errorf("配置不能为空")
|
||||
}
|
||||
if cfg.DefaultIntervalMinutes <= 0 {
|
||||
cfg.DefaultIntervalMinutes = 5
|
||||
}
|
||||
if cfg.Version == "" {
|
||||
cfg.Version = "1.0"
|
||||
}
|
||||
if cfg.Rules == nil {
|
||||
cfg.Rules = []ScheduleRule{}
|
||||
}
|
||||
for _, r := range cfg.Rules {
|
||||
if r.IntervalMinutes <= 0 {
|
||||
return fmt.Errorf("规则 %q 间隔分钟必须大于 0", r.Name)
|
||||
}
|
||||
if r.TimeRange == "" {
|
||||
return fmt.Errorf("规则 %q 缺少 time_range", r.Name)
|
||||
}
|
||||
}
|
||||
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.config = cfg
|
||||
return s.save()
|
||||
}
|
||||
|
||||
// HasConfig returns true if a config is loaded.
|
||||
func (s *ThinkingScheduleStore) HasConfig() bool {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return s.config != nil
|
||||
}
|
||||
@@ -152,7 +152,20 @@ func (h *ChatHandler) handleChatMessage(client *ws.Client, msg ws.ClientMessage)
|
||||
"mode": mode,
|
||||
}
|
||||
if len(msg.Attachments) > 0 {
|
||||
aiReq["attachments"] = msg.Attachments
|
||||
images := make([]string, 0, len(msg.Attachments))
|
||||
for _, att := range msg.Attachments {
|
||||
if att.Type == "image" && att.URL != "" {
|
||||
imgURL := att.URL
|
||||
// 将相对路径转换为绝对 URL,方便 AI-Core 访问
|
||||
if strings.HasPrefix(imgURL, "/") {
|
||||
imgURL = "http://127.0.0.1:" + h.cfg.Port + imgURL
|
||||
}
|
||||
images = append(images, imgURL)
|
||||
}
|
||||
}
|
||||
if len(images) > 0 {
|
||||
aiReq["images"] = images
|
||||
}
|
||||
}
|
||||
reqBody, err := json.Marshal(aiReq)
|
||||
if err != nil {
|
||||
@@ -187,8 +200,8 @@ func (h *ChatHandler) handleChatMessage(client *ws.Client, msg ws.ClientMessage)
|
||||
}
|
||||
h.hub.CacheMessage(client.UserID, client.SessionID, userMsg)
|
||||
|
||||
// 广播用户消息给同用户所有设备(跨端同步)
|
||||
h.broadcastToUser(client.UserID, ws.ServerMessage{
|
||||
// 广播用户消息给同用户其他设备(跨端同步,排除发送者自身)
|
||||
h.broadcastToUserExcept(client.UserID, client.ClientID, ws.ServerMessage{
|
||||
Type: "response",
|
||||
MessageID: userMsgID,
|
||||
Content: msg.Content,
|
||||
@@ -208,6 +221,21 @@ func (h *ChatHandler) handleChatMessage(client *ws.Client, msg ws.ClientMessage)
|
||||
|
||||
// streamResponse 调用 AI-Core SSE 流式接口并逐 delta 转发给客户端
|
||||
func (h *ChatHandler) streamResponse(client *ws.Client, mode string, reqBody []byte, userMsg string) {
|
||||
normalExit := false
|
||||
defer func() {
|
||||
if !normalExit {
|
||||
h.broadcastToUser(client.UserID, ws.ServerMessage{
|
||||
Type: "stream_end",
|
||||
MessageID: "msg_" + generateID(),
|
||||
SessionID: client.SessionID,
|
||||
Timestamp: time.Now().UnixMilli(),
|
||||
})
|
||||
if h.hub != nil {
|
||||
h.hub.UpdateSessionState(client.SessionID, "idle")
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
aiCoreURL := h.cfg.AICoreURL + "/api/v1/chat"
|
||||
httpReq, err := http.NewRequest("POST", aiCoreURL, bytes.NewReader(reqBody))
|
||||
if err != nil {
|
||||
@@ -309,7 +337,7 @@ func (h *ChatHandler) streamResponse(client *ws.Client, mode string, reqBody []b
|
||||
if chunk.Error != "" {
|
||||
logger.Printf("[chat] AI-Core 流式错误: %s", chunk.Error)
|
||||
h.hub.UpdateSessionState(client.SessionID, "error")
|
||||
client.SendMessage(ws.ServerMessage{
|
||||
h.broadcastToUser(client.UserID, ws.ServerMessage{
|
||||
Type: "error",
|
||||
MessageID: "msg_" + generateID(),
|
||||
Error: chunk.Error,
|
||||
@@ -338,9 +366,13 @@ func (h *ChatHandler) streamResponse(client *ws.Client, mode string, reqBody []b
|
||||
msgType = "action"
|
||||
}
|
||||
reviewMsgID := fmt.Sprintf("%s_r%d", msgID, i)
|
||||
// 持久化每条审查消息
|
||||
// 持久化每条审查消息 (action 角色映射为 assistant,LLM 模型不支持自定义角色)
|
||||
if h.sessionStore != nil && h.sessionStore.IsAvailable() {
|
||||
if err := h.sessionStore.AddMessage(client.SessionID, role, msgType, rm.Content, client.ClientID); err != nil {
|
||||
dbRole := role
|
||||
if dbRole == "action" {
|
||||
dbRole = "assistant"
|
||||
}
|
||||
if err := h.sessionStore.AddMessage(client.SessionID, dbRole, msgType, rm.Content, client.ClientID); err != nil {
|
||||
logger.Printf("[chat] 持久化审查消息失败: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -402,7 +434,7 @@ func (h *ChatHandler) streamResponse(client *ws.Client, mode string, reqBody []b
|
||||
if err := scanner.Err(); err != nil {
|
||||
logger.Printf("[chat] SSE 读取错误: %v", err)
|
||||
h.hub.UpdateSessionState(client.SessionID, "error")
|
||||
client.SendMessage(ws.ServerMessage{
|
||||
h.broadcastToUser(client.UserID, ws.ServerMessage{
|
||||
Type: "error",
|
||||
MessageID: "msg_" + generateID(),
|
||||
Error: fmt.Sprintf("流读取错误: %v", err),
|
||||
@@ -477,6 +509,7 @@ func (h *ChatHandler) streamResponse(client *ws.Client, mode string, reqBody []b
|
||||
h.hub.RecordMessage(client.SessionID, "assistant", recordText)
|
||||
|
||||
// 设置会话状态为 idle
|
||||
normalExit = true
|
||||
h.hub.UpdateSessionState(client.SessionID, "idle")
|
||||
}
|
||||
|
||||
@@ -766,7 +799,11 @@ func (h *ChatHandler) HandleProactiveMessage(c *gin.Context) {
|
||||
|
||||
// Persist to database so proactive messages survive restarts.
|
||||
if h.sessionStore != nil && h.sessionStore.IsAvailable() {
|
||||
if err := h.sessionStore.AddMessage(sessionID, role, msgType, seg.content, ""); err != nil {
|
||||
dbRole := role
|
||||
if dbRole == "action" {
|
||||
dbRole = "assistant"
|
||||
}
|
||||
if err := h.sessionStore.AddMessage(sessionID, dbRole, msgType, seg.content, ""); err != nil {
|
||||
logger.Printf("[proactive] 持久化消息失败: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -951,6 +988,17 @@ func (h *ChatHandler) broadcastToUser(userID string, msg ws.ServerMessage) {
|
||||
h.hub.SendToUser(userID, data)
|
||||
}
|
||||
|
||||
// broadcastToUserExcept sends a server message to ALL connected clients for a user,
|
||||
// excluding the specified clientID (the sender).
|
||||
func (h *ChatHandler) broadcastToUserExcept(userID, excludeClientID string, msg ws.ServerMessage) {
|
||||
data, err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
logger.Printf("[chat] 序列化广播消息失败: %v", err)
|
||||
return
|
||||
}
|
||||
h.hub.SendToUserExcept(userID, excludeClientID, data)
|
||||
}
|
||||
|
||||
// parseMultiMessage 检测并解析多消息格式
|
||||
// 如果文本包含空行分隔的多条消息,拆分为多条;否则返回单条
|
||||
func parseMultiMessage(text string) []proactiveSegment {
|
||||
|
||||
@@ -93,7 +93,16 @@ func (h *MemoryHandler) List(c *gin.Context) {
|
||||
userID = authUserID
|
||||
}
|
||||
|
||||
limit := c.Query("limit")
|
||||
offset := c.Query("offset")
|
||||
|
||||
url := fmt.Sprintf("%s/api/v1/memories?user_id=%s", h.memoryServiceURL, userID)
|
||||
if limit != "" {
|
||||
url += "&limit=" + limit
|
||||
}
|
||||
if offset != "" {
|
||||
url += "&offset=" + offset
|
||||
}
|
||||
|
||||
resp, err := h.client.Get(url)
|
||||
if err != nil {
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/yourname/cyrene-ai/gateway/internal/config"
|
||||
)
|
||||
|
||||
// ThinkingScheduleHandler handles CRUD for the thinking schedule config.
|
||||
type ThinkingScheduleHandler struct {
|
||||
store *config.ThinkingScheduleStore
|
||||
}
|
||||
|
||||
// NewThinkingScheduleHandler creates a new handler.
|
||||
func NewThinkingScheduleHandler(store *config.ThinkingScheduleStore) *ThinkingScheduleHandler {
|
||||
return &ThinkingScheduleHandler{store: store}
|
||||
}
|
||||
|
||||
// GetSchedule returns the current schedule config.
|
||||
// GET /api/v1/admin/thinking-schedule
|
||||
func (h *ThinkingScheduleHandler) GetSchedule(c *gin.Context) {
|
||||
cfg := h.store.GetConfig()
|
||||
if cfg == nil {
|
||||
cfg = config.DefaultThinkingScheduleConfig()
|
||||
}
|
||||
c.JSON(http.StatusOK, cfg)
|
||||
}
|
||||
|
||||
// SetSchedule replaces the entire schedule config.
|
||||
// PUT /api/v1/admin/thinking-schedule
|
||||
func (h *ThinkingScheduleHandler) SetSchedule(c *gin.Context) {
|
||||
var cfg config.ThinkingScheduleConfig
|
||||
if err := c.ShouldBindJSON(&cfg); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数无效: " + err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.store.SetConfig(&cfg); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"status": "saved"})
|
||||
}
|
||||
@@ -472,6 +472,25 @@ func (h *Hub) SendToUser(userID string, message []byte) {
|
||||
}
|
||||
}
|
||||
|
||||
// SendToUserExcept 向指定用户的所有连接发送消息,排除指定 clientID
|
||||
func (h *Hub) SendToUserExcept(userID, excludeClientID string, message []byte) {
|
||||
h.mu.RLock()
|
||||
defer h.mu.RUnlock()
|
||||
|
||||
if clients, ok := h.userClients[userID]; ok {
|
||||
for client := range clients {
|
||||
if client.ClientID == excludeClientID {
|
||||
continue
|
||||
}
|
||||
select {
|
||||
case client.Send <- message:
|
||||
default:
|
||||
// 跳过阻塞的客户端
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SendToSession 向指定会话的连接发送消息
|
||||
func (h *Hub) SendToSession(userID, sessionID string, message []byte) {
|
||||
h.mu.RLock()
|
||||
|
||||
@@ -64,9 +64,10 @@ func (h *MemoryHandler) listMemories(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
category := r.URL.Query().Get("category")
|
||||
limit := queryInt(r, "limit", 50)
|
||||
offset := queryInt(r, "offset", 0)
|
||||
minImportance := queryInt(r, "min_importance", 0)
|
||||
|
||||
memories, err := h.svc.ListMemories(r.Context(), userID, category, minImportance, limit)
|
||||
memories, err := h.svc.ListMemories(r.Context(), userID, category, minImportance, limit, offset)
|
||||
if err != nil {
|
||||
logger.Printf("[memory-handler] 列出记忆失败: %v", err)
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
|
||||
@@ -65,7 +65,7 @@ func (svc *MemoryService) GetMemory(ctx context.Context, id string) (*model.Memo
|
||||
}
|
||||
|
||||
// ListMemories 列出用户所有记忆
|
||||
func (svc *MemoryService) ListMemories(ctx context.Context, userID string, category string, minImportance int, limit int) ([]model.MemoryEntry, error) {
|
||||
func (svc *MemoryService) ListMemories(ctx context.Context, userID string, category string, minImportance int, limit int, offset int) ([]model.MemoryEntry, error) {
|
||||
if limit <= 0 {
|
||||
limit = 50
|
||||
}
|
||||
@@ -74,6 +74,7 @@ func (svc *MemoryService) ListMemories(ctx context.Context, userID string, categ
|
||||
UserID: userID,
|
||||
MinImportance: minImportance,
|
||||
Limit: limit,
|
||||
Offset: offset,
|
||||
}
|
||||
if category != "" {
|
||||
q.Category = model.MemoryCategory(category)
|
||||
|
||||
+601
-6
@@ -670,6 +670,76 @@ input[type="range"] { accent-color: var(--accent); padding: 0; }
|
||||
}
|
||||
.timeline-filter-tab:hover { background: var(--bg4); color: var(--text); }
|
||||
.timeline-filter-tab.active { background: var(--accent); color: #fff; border-color: var(--accent); font-weight: 600; }
|
||||
|
||||
/* ========== 全链路追踪面板样式 ========== */
|
||||
.trace-summary { display: flex; gap: 14px; margin-bottom: 16px; flex-wrap: wrap; }
|
||||
.trace-summary-item {
|
||||
display: flex; align-items: center; gap: 6px; padding: 6px 12px;
|
||||
background: var(--bg3); border-radius: var(--radius-sm); font-size: 12px;
|
||||
}
|
||||
.trace-summary-item .tsi-val { font-weight: 700; font-family: 'JetBrains Mono', monospace; }
|
||||
|
||||
.trace-timeline { position: relative; padding-left: 48px; }
|
||||
.trace-timeline::before {
|
||||
content: ''; position: absolute; left: 22px; top: 0; bottom: 0;
|
||||
width: 2px; background: var(--border2); border-radius: 1px;
|
||||
}
|
||||
.trace-hop {
|
||||
position: relative; margin-bottom: 4px; padding: 8px 12px;
|
||||
background: var(--bg2); border: 1px solid var(--border); border-radius: var(--radius-sm);
|
||||
display: flex; align-items: center; gap: 12px; cursor: pointer;
|
||||
transition: all .15s; font-size: 12px;
|
||||
}
|
||||
.trace-hop:hover { border-color: var(--accent); background: var(--bg3); }
|
||||
.trace-hop.error { border-left: 3px solid var(--red); }
|
||||
.trace-hop.success { border-left: 3px solid var(--green); }
|
||||
.trace-hop .hop-dot {
|
||||
position: absolute; left: -32px; top: 50%; transform: translateY(-50%);
|
||||
width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--border2);
|
||||
background: var(--bg2); z-index: 2;
|
||||
}
|
||||
.trace-hop .hop-dot.gateway { border-color: var(--blue); background: var(--blue-bg); }
|
||||
.trace-hop .hop-dot.ai-core { border-color: var(--accent); background: var(--accent-bg); }
|
||||
.trace-hop .hop-dot.voice-service { border-color: var(--yellow); background: var(--yellow-bg); }
|
||||
.trace-hop .hop-dot.memory-service { border-color: var(--orange); background: var(--orange-bg); }
|
||||
.trace-hop .hop-dot.error { border-color: var(--red); background: var(--red-bg); }
|
||||
|
||||
.trace-hop .hop-time {
|
||||
font-family: 'JetBrains Mono', monospace; font-size: 10px; color: var(--text3);
|
||||
min-width: 70px; flex-shrink: 0;
|
||||
}
|
||||
.trace-hop .hop-service {
|
||||
font-size: 10px; font-weight: 600; padding: 1px 8px; border-radius: 10px;
|
||||
flex-shrink: 0; text-transform: uppercase;
|
||||
}
|
||||
.trace-hop .hop-service.gateway { background: var(--blue-bg); color: var(--blue); }
|
||||
.trace-hop .hop-service.ai-core { background: var(--accent-bg); color: var(--accent); }
|
||||
.trace-hop .hop-service.voice-service { background: var(--yellow-bg); color: var(--yellow); }
|
||||
.trace-hop .hop-service.memory-service { background: var(--orange-bg); color: var(--orange); }
|
||||
.trace-hop .hop-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||
.trace-hop .hop-duration {
|
||||
font-family: 'JetBrains Mono', monospace; font-size: 11px; color: var(--text2);
|
||||
flex-shrink: 0;
|
||||
}
|
||||
.trace-hop .hop-status {
|
||||
font-size: 16px; flex-shrink: 0;
|
||||
}
|
||||
.trace-hop-detail {
|
||||
display: none; margin: -2px 0 8px 12px; padding: 10px 14px;
|
||||
background: var(--bg); border: 1px solid var(--border2); border-radius: var(--radius-sm);
|
||||
font-family: 'JetBrains Mono', monospace; font-size: 11px; color: var(--text2);
|
||||
white-space: pre-wrap; word-break: break-all; max-height: 200px; overflow-y: auto;
|
||||
}
|
||||
.trace-hop-detail.open { display: block; }
|
||||
|
||||
.trace-control-bar {
|
||||
display: flex; gap: 10px; align-items: center; margin-bottom: 14px; flex-wrap: wrap;
|
||||
}
|
||||
.trace-control-bar input {
|
||||
width: 280px; flex-shrink: 0;
|
||||
}
|
||||
.trace-empty { text-align: center; padding: 40px; color: var(--text2); }
|
||||
.trace-empty .icon { font-size: 48px; margin-bottom: 12px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
@@ -714,6 +784,14 @@ input[type="range"] { accent-color: var(--accent); padding: 0; }
|
||||
<span class="nav-icon">🗄️</span><span class="nav-label">数据库监看</span>
|
||||
<span class="nav-badge" id="db-badge" style="display:none">●</span>
|
||||
</button>
|
||||
<button class="nav-item" data-panel="vmMonitor">
|
||||
<span class="nav-icon">🖥</span><span class="nav-label">VM 监控</span>
|
||||
<span class="nav-badge" id="vm-badge" style="display:none">●</span>
|
||||
</button>
|
||||
<button class="nav-item" data-panel="trace">
|
||||
<span class="nav-icon">🔗</span><span class="nav-label">链路追踪</span>
|
||||
<span class="nav-badge" id="trace-badge" style="display:none">0</span>
|
||||
</button>
|
||||
</details>
|
||||
|
||||
<details class="nav-group">
|
||||
@@ -785,6 +863,8 @@ input[type="range"] { accent-color: var(--accent); padding: 0; }
|
||||
<div class="panel" id="panel-performance"></div>
|
||||
<!-- 数据库监看 -->
|
||||
<div class="panel" id="panel-database"></div>
|
||||
<!-- VM 监控 -->
|
||||
<div class="panel" id="panel-vmMonitor"></div>
|
||||
<!-- 工具调用记录 -->
|
||||
<div class="panel" id="panel-toolCalls"></div>
|
||||
<!-- 语音识别日志 -->
|
||||
@@ -801,6 +881,7 @@ input[type="range"] { accent-color: var(--accent); padding: 0; }
|
||||
<div class="panel" id="panel-modelConfig"></div>
|
||||
<div class="panel" id="panel-thinkingSchedule"></div>
|
||||
<div class="panel" id="panel-llmCalls"></div>
|
||||
<div class="panel" id="panel-trace"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -843,6 +924,10 @@ const STATE = {
|
||||
memoryFilterImportance: 0,
|
||||
memorySearchText: '',
|
||||
memoryPanelInitialized: false,
|
||||
memoryOffset: 0,
|
||||
memoryLimit: 50,
|
||||
memoryHasMore: true,
|
||||
memoryLoadingMore: false,
|
||||
// STT 语音识别日志面板状态
|
||||
sttLogs: [],
|
||||
sttAutoRefresh: null,
|
||||
@@ -853,6 +938,9 @@ const STATE = {
|
||||
timelineFilterType: 'all',
|
||||
timelineAutoRefresh: null,
|
||||
timelineLimit: 100,
|
||||
timelineOffset: 0,
|
||||
timelineHasMore: true,
|
||||
timelineLoadingMore: false,
|
||||
// 第三方聊天
|
||||
chatConfigsAutoRefresh: null,
|
||||
chatConfigs: [],
|
||||
@@ -1086,6 +1174,9 @@ function switchPanel(name) {
|
||||
history.replaceState(null, '', '#' + name);
|
||||
}
|
||||
|
||||
// 停止链路追踪定时器
|
||||
stopTraceAutoRefresh();
|
||||
|
||||
// 更新侧边栏
|
||||
document.querySelectorAll('.nav-item').forEach(b => b.classList.remove('active'));
|
||||
const navBtn = document.querySelector(`.nav-item[data-panel="${name}"]`);
|
||||
@@ -1101,6 +1192,8 @@ function switchPanel(name) {
|
||||
modelConfig: '🤖 模型配置管理',
|
||||
thinkingSchedule: '⏰ 思考调度配置',
|
||||
llmCalls: '📊 LLM 调用日志',
|
||||
vmMonitor: '🖥 VM 监控',
|
||||
trace: '🔗 全链路追踪',
|
||||
};
|
||||
document.getElementById('panel-title').textContent = titles[name] || name;
|
||||
|
||||
@@ -1120,6 +1213,7 @@ function switchPanel(name) {
|
||||
case 'iot': renderIoTPanel(); stopSessionsAutoRefresh(); stopDashboardAutoRefresh(); stopDbAutoRefresh(); startIoTRefresh(); stopTimelineAutoRefresh(); break;
|
||||
case 'performance': renderPerformancePanel(); stopSessionsAutoRefresh(); stopDashboardAutoRefresh(); stopDbAutoRefresh(); stopIoTRefresh(); stopTimelineAutoRefresh(); break;
|
||||
case 'database': renderDatabasePanel(); stopSessionsAutoRefresh(); stopDashboardAutoRefresh(); startDbAutoRefresh(); stopIoTRefresh(); stopTimelineAutoRefresh(); break;
|
||||
case 'vmMonitor': renderVMMonitorPanel(); stopSessionsAutoRefresh(); stopDashboardAutoRefresh(); stopDbAutoRefresh(); stopIoTRefresh(); stopTimelineAutoRefresh(); break;
|
||||
case 'toolCalls': renderToolCallsPanel(); stopSessionsAutoRefresh(); stopDashboardAutoRefresh(); stopDbAutoRefresh(); stopIoTRefresh(); stopTimelineAutoRefresh(); break;
|
||||
case 'stt': renderSTTPanel(); stopSessionsAutoRefresh(); stopDashboardAutoRefresh(); stopDbAutoRefresh(); stopIoTRefresh(); stopTimelineAutoRefresh(); break;
|
||||
case 'plugins': renderPluginsPanel(); stopSessionsAutoRefresh(); stopDashboardAutoRefresh(); stopDbAutoRefresh(); stopIoTRefresh(); stopTimelineAutoRefresh(); break;
|
||||
@@ -1130,6 +1224,7 @@ function switchPanel(name) {
|
||||
case 'modelConfig': renderModelConfigPanel(); stopSessionsAutoRefresh(); stopDashboardAutoRefresh(); stopDbAutoRefresh(); stopIoTRefresh(); stopTimelineAutoRefresh(); break;
|
||||
case 'thinkingSchedule': renderThinkingSchedulePanel(); stopSessionsAutoRefresh(); stopDashboardAutoRefresh(); stopDbAutoRefresh(); stopIoTRefresh(); stopTimelineAutoRefresh(); break;
|
||||
case 'llmCalls': renderLlmCallsPanel(); stopSessionsAutoRefresh(); stopDashboardAutoRefresh(); stopDbAutoRefresh(); stopIoTRefresh(); stopTimelineAutoRefresh(); break;
|
||||
case 'trace': renderTracePanel(); stopSessionsAutoRefresh(); stopDashboardAutoRefresh(); stopDbAutoRefresh(); stopIoTRefresh(); stopTimelineAutoRefresh(); break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1652,8 +1747,10 @@ async function loadMemories() {
|
||||
const userId = document.getElementById('mem-user-id').value.trim();
|
||||
if (!userId) { showToast('请输入用户ID', 'error'); return; }
|
||||
STATE.memoryUserId = userId;
|
||||
STATE.memoryOffset = 0;
|
||||
STATE.memoryHasMore = true;
|
||||
|
||||
const data = await api(`/api/memory/list?user_id=${encodeURIComponent(userId)}`);
|
||||
const data = await api(`/api/memory/list?user_id=${encodeURIComponent(userId)}&limit=${STATE.memoryLimit}&offset=0`);
|
||||
|
||||
if (data.error) {
|
||||
let hint = '';
|
||||
@@ -1678,9 +1775,46 @@ async function loadMemories() {
|
||||
else if (data.results) memories = data.results;
|
||||
|
||||
STATE.memoryCache = memories;
|
||||
STATE.memoryOffset = memories.length;
|
||||
STATE.memoryHasMore = memories.length >= STATE.memoryLimit;
|
||||
filterAndRenderMemories();
|
||||
}
|
||||
|
||||
async function loadMoreMemories() {
|
||||
if (STATE.memoryLoadingMore || !STATE.memoryHasMore) return;
|
||||
STATE.memoryLoadingMore = true;
|
||||
|
||||
const userId = STATE.memoryUserId;
|
||||
const data = await api(`/api/memory/list?user_id=${encodeURIComponent(userId)}&limit=${STATE.memoryLimit}&offset=${STATE.memoryOffset}`);
|
||||
|
||||
if (data.error) { STATE.memoryLoadingMore = false; return; }
|
||||
|
||||
let memories = [];
|
||||
if (Array.isArray(data)) memories = data;
|
||||
else if (data.memories) memories = data.memories;
|
||||
else if (data.results) memories = data.results;
|
||||
|
||||
if (memories.length > 0) {
|
||||
STATE.memoryCache = STATE.memoryCache.concat(memories);
|
||||
STATE.memoryOffset += memories.length;
|
||||
STATE.memoryHasMore = memories.length >= STATE.memoryLimit;
|
||||
renderStatsPanel();
|
||||
appendMemoryCards(memories);
|
||||
const countEl = document.getElementById('mem-result-count');
|
||||
if (countEl) countEl.textContent = '显示 ' + STATE.memoryCache.length + ' 条';
|
||||
} else {
|
||||
STATE.memoryHasMore = false;
|
||||
}
|
||||
STATE.memoryLoadingMore = false;
|
||||
}
|
||||
|
||||
function appendMemoryCards(memories) {
|
||||
var grid = document.getElementById('mem-cards-grid');
|
||||
if (!grid) return;
|
||||
var html = memories.map(function(m) { return renderMemoryCard(m); }).join('');
|
||||
grid.insertAdjacentHTML('beforeend', html);
|
||||
}
|
||||
|
||||
async function searchMemory() {
|
||||
const userId = document.getElementById('mem-user-id').value.trim();
|
||||
const q = STATE.memorySearchText || document.getElementById('mem-search-text')?.value?.trim() || '';
|
||||
@@ -1714,6 +1848,8 @@ async function searchMemory() {
|
||||
else if (data.results) memories = data.results;
|
||||
|
||||
STATE.memoryCache = memories;
|
||||
STATE.memoryOffset = memories.length;
|
||||
STATE.memoryHasMore = false; // search doesn't support pagination
|
||||
filterAndRenderMemories();
|
||||
}
|
||||
|
||||
@@ -3480,9 +3616,11 @@ async function renderTimelinePanel() {
|
||||
'自动刷新 (30s)</label>' +
|
||||
'<button class="btn btn-sm" onclick="renderTimelinePanel()" style="margin-left:8px">🔄 刷新</button>';
|
||||
|
||||
// 加载数据
|
||||
// 加载数据 (重置 offset)
|
||||
STATE.timelineOffset = 0;
|
||||
STATE.timelineHasMore = true;
|
||||
var userId = STATE.timelineUserId || 'admin';
|
||||
var data = await api('/api/memory-timeline?user_id=' + encodeURIComponent(userId) + '&limit=' + STATE.timelineLimit);
|
||||
var data = await api('/api/memory-timeline?user_id=' + encodeURIComponent(userId) + '&limit=' + STATE.timelineLimit + '&offset=0');
|
||||
|
||||
if (data.error) {
|
||||
container.innerHTML = '<div class="empty-state"><div class="icon">⚠️</div>' + escHtml(data.error) +
|
||||
@@ -3493,6 +3631,8 @@ async function renderTimelinePanel() {
|
||||
var timeline = data.timeline || [];
|
||||
var stats = data.stats || {};
|
||||
STATE.timelineData = timeline;
|
||||
STATE.timelineOffset = timeline.length;
|
||||
STATE.timelineHasMore = data.hasMore !== false;
|
||||
|
||||
// 筛选
|
||||
var filtered = timeline;
|
||||
@@ -3617,7 +3757,110 @@ async function renderTimelinePanel() {
|
||||
timelineHtml += '</div>';
|
||||
}
|
||||
|
||||
container.innerHTML = statsCardsHtml + filterHtml + timelineHtml;
|
||||
var loadMoreHtml = STATE.timelineHasMore
|
||||
? '<div id="timeline-load-more" style="text-align:center;padding:16px;color:var(--text2);cursor:pointer" onclick="loadMoreTimeline()">📜 滚动加载更多...</div>'
|
||||
: (timeline.length > 0 ? '<div style="text-align:center;padding:16px;color:var(--text3)">已加载全部条目</div>' : '');
|
||||
|
||||
container.innerHTML = statsCardsHtml + filterHtml + timelineHtml + loadMoreHtml;
|
||||
}
|
||||
|
||||
async function loadMoreTimeline() {
|
||||
if (STATE.timelineLoadingMore || !STATE.timelineHasMore) return;
|
||||
STATE.timelineLoadingMore = true;
|
||||
|
||||
var userId = STATE.timelineUserId || 'admin';
|
||||
var data = await api('/api/memory-timeline?user_id=' + encodeURIComponent(userId) + '&limit=' + STATE.timelineLimit + '&offset=' + STATE.timelineOffset);
|
||||
|
||||
if (data.error) { STATE.timelineLoadingMore = false; return; }
|
||||
|
||||
var newItems = data.timeline || [];
|
||||
if (newItems.length > 0) {
|
||||
STATE.timelineData = STATE.timelineData.concat(newItems);
|
||||
STATE.timelineOffset += newItems.length;
|
||||
STATE.timelineHasMore = data.hasMore !== false;
|
||||
appendTimelineItems(newItems);
|
||||
} else {
|
||||
STATE.timelineHasMore = false;
|
||||
}
|
||||
|
||||
var loadMoreEl = document.getElementById('timeline-load-more');
|
||||
if (loadMoreEl) {
|
||||
loadMoreEl.outerHTML = STATE.timelineHasMore
|
||||
? '<div id="timeline-load-more" style="text-align:center;padding:16px;color:var(--text2);cursor:pointer" onclick="loadMoreTimeline()">📜 滚动加载更多...</div>'
|
||||
: '<div style="text-align:center;padding:16px;color:var(--text3)">已加载全部条目</div>';
|
||||
}
|
||||
|
||||
STATE.timelineLoadingMore = false;
|
||||
}
|
||||
|
||||
function appendTimelineItems(items) {
|
||||
var container = document.querySelector('#panel-timeline .timeline-container');
|
||||
if (!container) return;
|
||||
|
||||
var html = '';
|
||||
var baseIdx = STATE.timelineData.length - items.length;
|
||||
for (var i = 0; i < items.length; i++) {
|
||||
var item = items[i];
|
||||
var itemId = 'tl-' + (baseIdx + i);
|
||||
var isMemory = item.type === 'memory';
|
||||
var dotIcon = isMemory ? '🧠' : '💭';
|
||||
var dotClass = isMemory ? 'memory' : 'thinking';
|
||||
var cardClass = isMemory ? 'memory-card' : 'thinking-card';
|
||||
var titleClass = isMemory ? 'memory' : 'thinking';
|
||||
var title = item.title || (isMemory ? '记忆' : '思考');
|
||||
var summary = '';
|
||||
if (isMemory) {
|
||||
summary = item.summary || item.content || '';
|
||||
if (summary.length > 200) summary = summary.substring(0, 197) + '...';
|
||||
} else {
|
||||
summary = item.summary || '';
|
||||
}
|
||||
var starsHtml = '';
|
||||
if (isMemory && item.importance) {
|
||||
starsHtml = '<span class="timeline-importance-stars">' + importanceToStarsTimeline(item.importance) + '</span>';
|
||||
}
|
||||
var catLabel = '';
|
||||
if (isMemory && item.category) {
|
||||
var cc = getCatColor(item.category);
|
||||
catLabel = '<span style="display:inline-block;padding:1px 8px;border-radius:10px;font-size:10px;font-weight:500;background:' + cc.bg + ';color:' + cc.text + ';">' + cc.icon + ' ' + cc.name + '</span>';
|
||||
}
|
||||
var toolCallLabel = '';
|
||||
if (!isMemory && item.tool_call_count > 0) {
|
||||
toolCallLabel = '<span style="display:inline-block;padding:1px 8px;border-radius:10px;font-size:10px;font-weight:500;background:var(--accent-bg);color:var(--accent);">🔧 ' + item.tool_call_count + ' 次工具调用</span>';
|
||||
}
|
||||
var triggerLabel = '';
|
||||
if (!isMemory) {
|
||||
var trigger = item.trigger || '定时';
|
||||
var triggerClass = trigger === '手动' ? 'manual' : 'scheduled';
|
||||
triggerLabel = '<span class="timeline-trigger-badge ' + triggerClass + '">' + (trigger === '手动' ? '👆 手动' : '⏰ 定时') + '</span>';
|
||||
}
|
||||
var sourceLabel = '';
|
||||
if (isMemory) {
|
||||
var srcText = item.source === 'thinking' ? '🤔 后台思考' : item.source === 'conversation' ? '💬 对话' : '📝 ' + (item.source || '未知');
|
||||
sourceLabel = '<span>' + srcText + '</span>';
|
||||
}
|
||||
html += '<div class="timeline-item" id="' + itemId + '">' +
|
||||
'<div class="timeline-dot ' + dotClass + '">' + dotIcon + '</div>' +
|
||||
'<div class="timeline-card ' + cardClass + '" onclick="toggleTimelineDetail(\'' + itemId + '\')">' +
|
||||
'<div class="timeline-card-header">' +
|
||||
'<span class="timeline-card-title ' + titleClass + '">' + escHtml(title) + '</span>' +
|
||||
'<div class="timeline-card-meta">' +
|
||||
starsHtml +
|
||||
'<span style="font-size:10px;white-space:nowrap;">' + formatTime(item.timestamp) + '</span>' +
|
||||
'</div>' +
|
||||
'</div>' +
|
||||
(summary ? '<div class="timeline-card-body">' + escHtml(summary) + '</div>' : '') +
|
||||
'<div class="timeline-card-footer">' +
|
||||
catLabel + toolCallLabel + triggerLabel + sourceLabel +
|
||||
(item.session_id ? '<span style="font-size:10px;color:var(--text3);max-width:140px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">💬 ' + escHtml((item.session_id || '').substring(0, 20)) + '</span>' : '') +
|
||||
'</div>' +
|
||||
'</div>' +
|
||||
'<div class="timeline-detail" id="' + itemId + '-detail">' +
|
||||
renderTimelineDetail(item) +
|
||||
'</div>' +
|
||||
'</div>';
|
||||
}
|
||||
container.insertAdjacentHTML('beforeend', html);
|
||||
}
|
||||
|
||||
function renderTimelineDetail(item) {
|
||||
@@ -5029,7 +5272,7 @@ function formatTokens(n) {
|
||||
// Listen for browser back/forward navigation.
|
||||
window.addEventListener('hashchange', function() {
|
||||
var hash = location.hash.replace('#', '');
|
||||
var validPanels = ['dashboard', 'memory', 'sessions', 'services', 'iot', 'performance', 'database', 'toolCalls', 'stt', 'thinking', 'timeline', 'chatPlatforms', 'clients', 'modelConfig', 'llmCalls', 'thinkingSchedule', 'plugins'];
|
||||
var validPanels = ['dashboard', 'memory', 'sessions', 'services', 'iot', 'performance', 'database', 'toolCalls', 'stt', 'thinking', 'timeline', 'chatPlatforms', 'clients', 'modelConfig', 'llmCalls', 'thinkingSchedule', 'plugins', 'trace'];
|
||||
if (hash && validPanels.indexOf(hash) >= 0 && hash !== STATE.activePanel) {
|
||||
switchPanel(hash);
|
||||
}
|
||||
@@ -5040,7 +5283,7 @@ refreshStatus();
|
||||
|
||||
// Restore last panel from URL hash, or default to dashboard.
|
||||
var initHash = location.hash.replace('#', '');
|
||||
var validPanels = ['dashboard', 'memory', 'sessions', 'services', 'iot', 'performance', 'database', 'toolCalls', 'stt', 'thinking', 'timeline', 'chatPlatforms', 'clients', 'modelConfig', 'llmCalls', 'thinkingSchedule', 'plugins'];
|
||||
var validPanels = ['dashboard', 'memory', 'sessions', 'services', 'iot', 'performance', 'database', 'toolCalls', 'stt', 'thinking', 'timeline', 'chatPlatforms', 'clients', 'modelConfig', 'llmCalls', 'thinkingSchedule', 'plugins', 'trace'];
|
||||
if (initHash && validPanels.indexOf(initHash) >= 0) {
|
||||
switchPanel(initHash);
|
||||
} else {
|
||||
@@ -5050,6 +5293,358 @@ if (initHash && validPanels.indexOf(initHash) >= 0) {
|
||||
|
||||
// 全局状态定时刷新
|
||||
STATE.statusInterval = setInterval(refreshStatus, 5000);
|
||||
|
||||
// 无限滚动: 监听面板容器滚动,触发热加载
|
||||
var panelContainer = document.getElementById('panel-container');
|
||||
if (panelContainer) {
|
||||
panelContainer.addEventListener('scroll', function() {
|
||||
var nearBottom = panelContainer.scrollTop + panelContainer.clientHeight >= panelContainer.scrollHeight - 200;
|
||||
if (!nearBottom) return;
|
||||
if (STATE.activePanel === 'memory' && STATE.memoryHasMore && !STATE.memoryLoadingMore) {
|
||||
loadMoreMemories();
|
||||
} else if (STATE.activePanel === 'timeline' && STATE.timelineHasMore && !STATE.timelineLoadingMore) {
|
||||
loadMoreTimeline();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ========== VM 监控面板 ==========
|
||||
async function renderVMMonitorPanel() {
|
||||
var container = document.getElementById('panel-vmMonitor');
|
||||
if (!container) return;
|
||||
|
||||
document.getElementById('panel-actions').innerHTML =
|
||||
'<button class="btn btn-sm" onclick="renderVMMonitorPanel()">🔄 刷新</button>';
|
||||
|
||||
container.innerHTML = '<div style="text-align:center;padding:40px;color:var(--text-muted)">加载中...</div>';
|
||||
|
||||
var statusData, logData;
|
||||
try {
|
||||
var r1 = await api('/api/vm-monitor/status');
|
||||
statusData = r1;
|
||||
var r2 = await api('/api/tool-calls?tool_name=os_exec&limit=10');
|
||||
logData = r2;
|
||||
var r3 = await api('/api/tool-calls?tool_name=os_file&limit=10');
|
||||
var fileLogs = r3.calls || [];
|
||||
var r4 = await api('/api/tool-calls?tool_name=os_system&limit=5');
|
||||
var sysLogs = r4.calls || [];
|
||||
} catch (e) {
|
||||
container.innerHTML = '<div class="empty-state"><div class="icon">⚠️</div>请求失败: ' + escHtml(String(e)) + '</div>';
|
||||
return;
|
||||
}
|
||||
|
||||
if (statusData.error) {
|
||||
container.innerHTML = '<div class="empty-state"><div class="icon">⚠️</div>' + escHtml(statusData.error) + '</div>';
|
||||
return;
|
||||
}
|
||||
|
||||
var osEnabled = statusData.os_enabled;
|
||||
var sys = statusData.system || {};
|
||||
var backend = statusData.backend || '—';
|
||||
var host = statusData.host || {};
|
||||
var disk = statusData.disk || {};
|
||||
|
||||
// Merge all OS tool logs
|
||||
var allCalls = (logData.calls || []).concat(fileLogs).concat(sysLogs);
|
||||
allCalls.sort(function(a, b) { return b.timestamp - a.timestamp; });
|
||||
allCalls = allCalls.slice(0, 20);
|
||||
|
||||
var badgeColor = osEnabled ? 'var(--green)' : 'var(--text-muted)';
|
||||
var badgeText = osEnabled ? '已启用 (' + backend + ')' : '未配置';
|
||||
|
||||
var uname = sys.uname || '—';
|
||||
var hostname = sys.hostname || '—';
|
||||
var memory = sys.memory || '—';
|
||||
var diskInfo = sys.disk || disk.stat || '—';
|
||||
var cpu = host.system ? (host.system.num_cpu || '—') : '—';
|
||||
var hostOS = host.system ? (host.system.os || '—') : '—';
|
||||
|
||||
// Build HTML
|
||||
var html =
|
||||
'<!-- 状态概览 -->' +
|
||||
'<div class="cards-grid cards-4" style="margin-bottom:16px">' +
|
||||
'<div class="stat-card blue"><div class="stat-value">' + escHtml(backend) + '</div><div class="stat-label">OS 后端</div></div>' +
|
||||
'<div class="stat-card green"><div class="stat-value" style="font-size:14px">' + escHtml(hostname) + '</div><div class="stat-label">主机名</div></div>' +
|
||||
'<div class="stat-card accent"><div class="stat-value">' + cpu + '</div><div class="stat-label">CPU 核数</div></div>' +
|
||||
'<div class="stat-card"><div class="stat-value" style="font-size:13px;color:' + badgeColor + '">' + escHtml(badgeText) + '</div><div class="stat-label">状态</div></div>' +
|
||||
'</div>';
|
||||
|
||||
// System info card
|
||||
html +=
|
||||
'<div class="cards-grid cards-2" style="margin-bottom:16px">' +
|
||||
'<div class="card">' +
|
||||
'<div class="card-header"><span class="card-title">🐧 系统详情</span></div>' +
|
||||
'<div style="padding:12px;font-size:12px;line-height:1.8;font-family:var(--mono)">' +
|
||||
'<div style="color:var(--text-muted);margin-bottom:4px">uname -a</div>' +
|
||||
'<pre style="margin:0 0 12px;white-space:pre-wrap;font-size:11px">' + escHtml(uname) + '</pre>' +
|
||||
'<div style="color:var(--text-muted);margin-bottom:4px">内存</div>' +
|
||||
'<pre style="margin:0 0 12px;white-space:pre-wrap;font-size:11px">' + escHtml(memory) + '</pre>' +
|
||||
'<div style="color:var(--text-muted);margin-bottom:4px">磁盘</div>' +
|
||||
'<pre style="margin:0;white-space:pre-wrap;font-size:11px">' + escHtml(String(diskInfo)) + '</pre>' +
|
||||
'</div>' +
|
||||
'</div>' +
|
||||
'<div class="card">' +
|
||||
'<div class="card-header"><span class="card-title">🖥 宿主机信息</span></div>' +
|
||||
'<div style="padding:12px;font-size:12px;line-height:1.8">' +
|
||||
'<div style="display:flex;justify-content:space-between;padding:4px 0;border-bottom:1px solid var(--border)"><span style="color:var(--text-muted)">OS</span><span>' + escHtml(hostOS) + '</span></div>' +
|
||||
'<div style="display:flex;justify-content:space-between;padding:4px 0;border-bottom:1px solid var(--border)"><span style="color:var(--text-muted)">架构</span><span>' + escHtml(String(host.system ? host.system.arch : '—')) + '</span></div>' +
|
||||
'<div style="display:flex;justify-content:space-between;padding:4px 0;border-bottom:1px solid var(--border)"><span style="color:var(--text-muted)">Go 版本</span><span>' + escHtml(String(host.system ? host.system.go_version : '—')) + '</span></div>' +
|
||||
'</div>' +
|
||||
'</div>' +
|
||||
'</div>';
|
||||
|
||||
// Recent OS tool calls
|
||||
html +=
|
||||
'<div class="card">' +
|
||||
'<div class="card-header"><span class="card-title">📋 近期 OS 工具调用</span><span style="font-size:11px;color:var(--text-muted)">最近 20 条 (os_exec / os_file / os_system)</span></div>';
|
||||
|
||||
if (allCalls.length === 0) {
|
||||
html += '<div style="text-align:center;padding:30px;color:var(--text-muted)">暂无 OS 工具调用记录</div>';
|
||||
} else {
|
||||
html +=
|
||||
'<div style="overflow-x:auto">' +
|
||||
'<table class="data-table" style="font-size:11px">' +
|
||||
'<thead><tr>' +
|
||||
'<th>时间</th><th>工具</th><th>状态</th><th>耗时</th><th>命令/操作</th>' +
|
||||
'</tr></thead><tbody>';
|
||||
|
||||
for (var i = 0; i < allCalls.length; i++) {
|
||||
var call = allCalls[i];
|
||||
var ts = new Date(call.timestamp / 1e6).toISOString().replace('T', ' ').slice(0, 19);
|
||||
var successIcon = call.success ? '✅' : '❌';
|
||||
var args = {};
|
||||
try { args = JSON.parse(call.arguments || '{}'); } catch(e) {}
|
||||
var summary = '';
|
||||
if (call.tool_name === 'os_exec') {
|
||||
summary = escHtml(args.command || call.tool_name).slice(0, 80);
|
||||
} else if (call.tool_name === 'os_file') {
|
||||
summary = escHtml((args.action || '') + ' ' + (args.path || '')).slice(0, 60);
|
||||
} else {
|
||||
summary = escHtml(call.tool_name);
|
||||
}
|
||||
var dur = call.duration_ms ? (call.duration_ms / 1000).toFixed(2) + 's' : '—';
|
||||
|
||||
html +=
|
||||
'<tr>' +
|
||||
'<td style="white-space:nowrap">' + escHtml(ts) + '</td>' +
|
||||
'<td><span class="badge badge-' + (call.tool_name === 'os_exec' ? 'running' : call.tool_name === 'os_file' ? 'starting' : 'info') + '">' + escHtml(call.tool_name) + '</span></td>' +
|
||||
'<td>' + successIcon + '</td>' +
|
||||
'<td>' + dur + '</td>' +
|
||||
'<td style="max-width:300px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap" title="' + escHtml(args.command || args.path || '') + '">' + summary + '</td>' +
|
||||
'</tr>';
|
||||
}
|
||||
|
||||
html += '</tbody></table></div>';
|
||||
}
|
||||
|
||||
html += '</div>';
|
||||
|
||||
container.innerHTML = html;
|
||||
}
|
||||
|
||||
// ========== 全链路追踪面板 ==========
|
||||
var traceAutoRefreshId = null;
|
||||
var traceMode = 'recent'; // 'recent' | 'session'
|
||||
var traceSessionId = '';
|
||||
|
||||
function renderTracePanel() {
|
||||
document.getElementById('panel-actions').innerHTML =
|
||||
'<button class="btn btn-sm" onclick="refreshTrace()" id="trace-refresh-btn">🔄 刷新</button>' +
|
||||
'<label style="font-size:11px;color:var(--text2);display:flex;align-items:center;gap:4px;cursor:pointer">' +
|
||||
'<input type="checkbox" id="trace-auto-refresh" onchange="toggleTraceAutoRefresh()"> 自动刷新 (5s)' +
|
||||
'</label>';
|
||||
|
||||
document.getElementById('panel-trace').innerHTML =
|
||||
'<div class="card">' +
|
||||
'<div class="card-header">' +
|
||||
'<span class="card-title">🔗 全链路消息追踪</span>' +
|
||||
'<span style="font-size:11px;color:var(--text2)">追踪 Client → Gateway → AI-Core → LLM 全链路</span>' +
|
||||
'</div>' +
|
||||
'<div class="trace-control-bar">' +
|
||||
'<button class="btn btn-sm ' + (traceMode === 'recent' ? 'btn-accent' : '') + '" onclick="switchTraceMode(\'recent\')">📋 最近活动</button>' +
|
||||
'<button class="btn btn-sm ' + (traceMode === 'session' ? 'btn-accent' : '') + '" onclick="switchTraceMode(\'session\')">🔍 会话追踪</button>' +
|
||||
'<span id="trace-session-input" style="display:' + (traceMode === 'session' ? 'inline' : 'none') + '">' +
|
||||
'<input type="text" id="trace-session-id" placeholder="输入 Session ID..." value="' + escHtml(traceSessionId) + '" style="width:260px" onkeydown="if(event.key===\'Enter\')refreshTrace()">' +
|
||||
'<button class="btn btn-sm btn-accent" onclick="refreshTrace()">追踪</button>' +
|
||||
'</span>' +
|
||||
'</div>' +
|
||||
'<div id="trace-stats" class="trace-summary"></div>' +
|
||||
'<div id="trace-content">' +
|
||||
'<div style="text-align:center;padding:20px;color:var(--text2);font-size:12px">加载中...</div>' +
|
||||
'</div>' +
|
||||
'</div>';
|
||||
|
||||
refreshTrace();
|
||||
}
|
||||
|
||||
function switchTraceMode(mode) {
|
||||
traceMode = mode;
|
||||
stopTraceAutoRefresh();
|
||||
document.getElementById('trace-auto-refresh').checked = false;
|
||||
renderTracePanel();
|
||||
}
|
||||
|
||||
function toggleTraceAutoRefresh() {
|
||||
var checked = document.getElementById('trace-auto-refresh').checked;
|
||||
if (checked) {
|
||||
startTraceAutoRefresh();
|
||||
} else {
|
||||
stopTraceAutoRefresh();
|
||||
}
|
||||
}
|
||||
|
||||
function startTraceAutoRefresh() {
|
||||
stopTraceAutoRefresh();
|
||||
traceAutoRefreshId = setInterval(function() {
|
||||
if (STATE.activePanel === 'trace') refreshTrace();
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
function stopTraceAutoRefresh() {
|
||||
if (traceAutoRefreshId) { clearInterval(traceAutoRefreshId); traceAutoRefreshId = null; }
|
||||
}
|
||||
|
||||
async function refreshTrace() {
|
||||
var contentEl = document.getElementById('trace-content');
|
||||
var statsEl = document.getElementById('trace-stats');
|
||||
var btn = document.getElementById('trace-refresh-btn');
|
||||
if (btn) btn.classList.add('spinning');
|
||||
|
||||
// 热更新:不清空现有内容,仅在按钮上显示加载状态
|
||||
|
||||
var url, data;
|
||||
if (traceMode === 'session') {
|
||||
var sid = document.getElementById('trace-session-id').value.trim();
|
||||
if (!sid) {
|
||||
contentEl.innerHTML = '<div class="trace-empty"><div class="icon">⚠️</div>请输入 Session ID 以追踪会话链路</div>';
|
||||
statsEl.innerHTML = '';
|
||||
if (btn) btn.classList.remove('spinning');
|
||||
return;
|
||||
}
|
||||
traceSessionId = sid;
|
||||
url = '/api/trace/session/' + encodeURIComponent(sid);
|
||||
} else {
|
||||
url = '/api/trace/recent?limit=50';
|
||||
}
|
||||
|
||||
try {
|
||||
data = await api(url);
|
||||
} catch(e) {
|
||||
data = { error: e.message };
|
||||
}
|
||||
|
||||
if (btn) btn.classList.remove('spinning');
|
||||
|
||||
if (!data) {
|
||||
contentEl.innerHTML = '<div class="trace-empty"><div class="icon">⚠️</div>无法获取链路数据 (服务未响应)</div>';
|
||||
statsEl.innerHTML = '';
|
||||
return;
|
||||
}
|
||||
|
||||
if (data.error) {
|
||||
contentEl.innerHTML = '<div class="trace-empty"><div class="icon">⚠️</div>' + escHtml(data.error) + '</div>';
|
||||
statsEl.innerHTML = '';
|
||||
return;
|
||||
}
|
||||
|
||||
var traces = data.traces || [];
|
||||
var stats = data.stats || {};
|
||||
|
||||
// 统计栏 — 热更新
|
||||
var servicesCount = (stats.services || []).length;
|
||||
var statsHtml =
|
||||
'<div class="trace-summary-item">📊 <span class="tsi-val">' + traces.length + '</span> 个追踪节点</div>' +
|
||||
'<div class="trace-summary-item">🖥 <span class="tsi-val">' + servicesCount + '</span> 个服务</div>' +
|
||||
(stats.errors > 0 ? '<div class="trace-summary-item" style="color:var(--red)">❌ <span class="tsi-val">' + stats.errors + '</span> 个错误</div>' : '<div class="trace-summary-item" style="color:var(--green)">✅ 全部成功</div>') +
|
||||
(stats.totalSpanMs > 0 ? '<div class="trace-summary-item">⏱ 总跨度 <span class="tsi-val">' + (stats.totalSpanMs / 1000).toFixed(2) + 's</span></div>' : '') +
|
||||
(stats.totalDurationMs > 0 ? '<div class="trace-summary-item">⏳ 总耗时 <span class="tsi-val">' + (stats.totalDurationMs / 1000).toFixed(2) + 's</span></div>' : '');
|
||||
|
||||
// Session info
|
||||
if (data.session) {
|
||||
var s = data.session;
|
||||
statsHtml +=
|
||||
'<div class="trace-summary-item">💬 Session: <code style="font-size:10px">' + escHtml((s.session_id || '').substring(0, 20)) + '...</code></div>' +
|
||||
'<div class="trace-summary-item">👤 User: ' + escHtml(s.user_id || '—') + '</div>';
|
||||
}
|
||||
statsEl.innerHTML = statsHtml;
|
||||
|
||||
// 更新侧边栏徽章
|
||||
var badge = document.getElementById('trace-badge');
|
||||
if (badge) {
|
||||
badge.textContent = traces.length;
|
||||
badge.style.display = traces.length > 0 ? 'inline-block' : 'none';
|
||||
}
|
||||
|
||||
if (traces.length === 0) {
|
||||
contentEl.innerHTML = '<div class="trace-empty"><div class="icon">📭</div>暂无链路追踪数据<br><span style="font-size:11px;color:var(--text3)">请确保服务正在运行且有消息活动</span></div>';
|
||||
return;
|
||||
}
|
||||
|
||||
// 保存当前展开状态,重建 DOM 后恢复
|
||||
var expandedHops = [];
|
||||
var existingHops = contentEl.querySelectorAll('.trace-hop-detail.open');
|
||||
for (var ei = 0; ei < existingHops.length; ei++) {
|
||||
// 用 hop 的 label+timestamp 做 key 来恢复
|
||||
var prevHop = existingHops[ei].previousElementSibling;
|
||||
if (prevHop) {
|
||||
var hopLabel = prevHop.querySelector('.hop-label');
|
||||
var hopTime = prevHop.querySelector('.hop-time');
|
||||
if (hopLabel && hopTime) {
|
||||
expandedHops.push(hopTime.textContent + '|' + hopLabel.textContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 渲染时间线
|
||||
var html = '<div class="trace-timeline">';
|
||||
|
||||
for (var i = 0; i < traces.length; i++) {
|
||||
var t = traces[i];
|
||||
var time = new Date(t.timestamp).toISOString().replace('T', ' ').slice(11, 19);
|
||||
var isError = t.status === 'error';
|
||||
var hopKey = time + '|' + t.label;
|
||||
var wasExpanded = expandedHops.indexOf(hopKey) >= 0;
|
||||
var gapHtml = '';
|
||||
|
||||
// 显示与上一跳的时间间隔
|
||||
if (i > 0) {
|
||||
var gap = t.ts - traces[i - 1].ts;
|
||||
if (gap > 50) {
|
||||
gapHtml = '<div style="text-align:center;padding:2px 0;font-size:10px;color:var(--text3);margin-left:48px">↓ ' + (gap > 1000 ? (gap / 1000).toFixed(2) + 's' : gap + 'ms') + '</div>';
|
||||
}
|
||||
}
|
||||
|
||||
html += gapHtml;
|
||||
html +=
|
||||
'<div class="trace-hop ' + (isError ? 'error' : 'success') + '" onclick="toggleTraceHop(this)" title="点击展开详情">' +
|
||||
'<div class="hop-dot ' + (isError ? 'error' : escHtml(t.service)) + '"></div>' +
|
||||
'<span class="hop-time">' + escHtml(time) + '</span>' +
|
||||
'<span class="hop-service ' + escHtml(t.service) + '">' + escHtml(t.service) + '</span>' +
|
||||
'<span class="hop-label">' + escHtml(t.label) + '</span>' +
|
||||
(t.durationMs > 0 ? '<span class="hop-duration">' + (t.durationMs >= 1000 ? (t.durationMs / 1000).toFixed(2) + 's' : t.durationMs + 'ms') + '</span>' : '') +
|
||||
'<span class="hop-status">' + (isError ? '❌' : '✅') + '</span>' +
|
||||
'</div>' +
|
||||
'<div class="trace-hop-detail' + (wasExpanded ? ' open' : '') + '">' +
|
||||
'<div><strong>时间:</strong> ' + escHtml(t.timestamp) + '</div>' +
|
||||
'<div><strong>服务:</strong> ' + escHtml(t.service) + '</div>' +
|
||||
'<div><strong>节点:</strong> ' + escHtml(t.hop) + '</div>' +
|
||||
'<div><strong>标签:</strong> ' + escHtml(t.label) + '</div>' +
|
||||
(t.durationMs > 0 ? '<div><strong>耗时:</strong> ' + (t.durationMs >= 1000 ? (t.durationMs / 1000).toFixed(2) + 's' : t.durationMs + 'ms') + '</div>' : '') +
|
||||
'<div><strong>状态:</strong> ' + (isError ? '❌ 失败' : '✅ 成功') + '</div>' +
|
||||
(t.detail ? '<div style="margin-top:6px"><strong>详情:</strong><br>' + escHtml(String(t.detail)) + '</div>' : '') +
|
||||
'</div>';
|
||||
}
|
||||
|
||||
html += '</div>';
|
||||
contentEl.innerHTML = html;
|
||||
}
|
||||
|
||||
function toggleTraceHop(el) {
|
||||
var detail = el.nextElementSibling;
|
||||
if (detail && detail.classList.contains('trace-hop-detail')) {
|
||||
detail.classList.toggle('open');
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
+267
-8
@@ -225,7 +225,8 @@ app.post('/api/devtools/restart', (_req, res) => {
|
||||
const child = spawn(process.execPath, [scriptPath, ...process.argv.slice(2)], {
|
||||
cwd: ROOT,
|
||||
detached: true,
|
||||
stdio: 'inherit',
|
||||
stdio: 'ignore',
|
||||
windowsHide: true,
|
||||
});
|
||||
child.unref();
|
||||
process.exit(0);
|
||||
@@ -324,10 +325,12 @@ app.get('/api/memory/search', async (req, res) => {
|
||||
});
|
||||
|
||||
app.get('/api/memory/list', async (req, res) => {
|
||||
const { user_id } = req.query;
|
||||
const { user_id, limit, offset } = req.query;
|
||||
if (!user_id) return res.status(400).json({ error: '缺少 user_id 参数' });
|
||||
const qs = new URLSearchParams({ user_id }).toString();
|
||||
const result = await proxyToGateway(`/api/v1/memory?${qs}`);
|
||||
const qs = new URLSearchParams({ user_id });
|
||||
if (limit) qs.set('limit', limit);
|
||||
if (offset) qs.set('offset', offset);
|
||||
const result = await proxyToGateway(`/api/v1/memory?${qs.toString()}`);
|
||||
res.status(result.status).json(result.body);
|
||||
});
|
||||
|
||||
@@ -868,6 +871,12 @@ app.get('/api/tool-calls/stats', async (_req, res) => {
|
||||
res.status(result.status).json(result.body);
|
||||
});
|
||||
|
||||
// ---- VM 监控 (OS 环境信息) ----
|
||||
app.get('/api/vm-monitor/status', async (_req, res) => {
|
||||
const result = await proxyToAICore('/api/v1/system/info');
|
||||
res.status(result.status).json(result.body);
|
||||
});
|
||||
|
||||
// ---- 插件管理代理 (转发到 plugin-manager) ----
|
||||
app.get('/api/plugins', async (_req, res) => {
|
||||
const result = await proxyToPluginManager('/api/v1/plugins');
|
||||
@@ -1124,6 +1133,251 @@ app.get('/api/llm-calls', async (req, res) => {
|
||||
res.status(result.status).json(result.body);
|
||||
});
|
||||
|
||||
// ---- 全链路追踪 ----
|
||||
|
||||
/**
|
||||
* 从日志文件中搜索包含指定关键词的最近行
|
||||
*/
|
||||
function searchLogFile(serviceId, keyword, maxLines = 200) {
|
||||
const filePath = logFile(serviceId);
|
||||
if (!fs.existsSync(filePath)) return [];
|
||||
try {
|
||||
const content = fs.readFileSync(filePath, 'utf-8');
|
||||
const allLines = content.split('\n').filter(Boolean);
|
||||
const recent = allLines.slice(-maxLines);
|
||||
const kwLower = keyword.toLowerCase();
|
||||
return recent
|
||||
.filter(line => line.toLowerCase().includes(kwLower))
|
||||
.map(line => line.trim());
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析日志时间戳 (支持常见格式)
|
||||
*/
|
||||
function parseLogTimestamp(line) {
|
||||
// 2024-01-01T12:00:00Z, 2024/01/01 12:00:00, [2024-01-01 12:00:00], 12:00:00
|
||||
const match = line.match(/(\d{4}[-/]\d{2}[-/]\d{2}[T ]\d{2}:\d{2}:\d{2})/);
|
||||
if (match) return new Date(match[1]).getTime();
|
||||
const timeMatch = line.match(/(\d{2}:\d{2}:\d{2})/);
|
||||
if (timeMatch) {
|
||||
const today = new Date();
|
||||
const [h, m, s] = timeMatch[1].split(':').map(Number);
|
||||
today.setHours(h, m, s, 0);
|
||||
return today.getTime();
|
||||
}
|
||||
return Date.now();
|
||||
}
|
||||
|
||||
// GET /api/trace/recent — 最近的全链路追踪数据
|
||||
app.get('/api/trace/recent', async (req, res) => {
|
||||
const limit = Math.min(parseInt(req.query.limit) || 30, 100);
|
||||
try {
|
||||
const [llmResult, toolResult, sessionsResult] = await Promise.all([
|
||||
proxyToAICore(`/api/v1/llm-calls?limit=${limit}`).catch(() => ({ status: 502, body: [] })),
|
||||
proxyToAICore(`/api/v1/tools/calls?limit=${limit}`).catch(() => ({ status: 502, body: { calls: [] } })),
|
||||
proxyToGateway('/api/v1/admin/sessions/active').catch(() => ({ status: 502, body: { users: {} } })),
|
||||
]);
|
||||
|
||||
const traces = [];
|
||||
|
||||
// LLM 调用 → 追踪节点
|
||||
const llmCalls = Array.isArray(llmResult.body) ? llmResult.body : [];
|
||||
for (const call of llmCalls) {
|
||||
const ts = call.time ? new Date(call.time).getTime() : Date.now();
|
||||
traces.push({
|
||||
id: `llm-${ts}-${Math.random().toString(36).slice(2, 6)}`,
|
||||
timestamp: new Date(ts).toISOString(),
|
||||
ts,
|
||||
service: 'ai-core',
|
||||
hop: 'llm_call',
|
||||
label: `LLM 调用: ${call.model || 'unknown'}`,
|
||||
status: call.success ? 'success' : 'error',
|
||||
durationMs: call.duration_ms || call.Duration || 0,
|
||||
detail: call.error || `${call.prompt_tokens || 0}+${call.completion_tokens || 0} tokens`,
|
||||
data: call,
|
||||
});
|
||||
}
|
||||
|
||||
// 工具调用
|
||||
const toolCalls = toolResult.body?.calls || (Array.isArray(toolResult.body) ? toolResult.body : []);
|
||||
for (const tc of toolCalls) {
|
||||
const ts = tc.time || tc.timestamp || tc.created_at;
|
||||
const tsNum = ts ? new Date(ts).getTime() : Date.now();
|
||||
traces.push({
|
||||
id: `tool-${tsNum}-${Math.random().toString(36).slice(2, 6)}`,
|
||||
timestamp: new Date(tsNum).toISOString(),
|
||||
ts: tsNum,
|
||||
service: 'ai-core',
|
||||
hop: 'tool_call',
|
||||
label: `工具调用: ${tc.tool_name || tc.name || 'unknown'}`,
|
||||
status: tc.error ? 'error' : 'success',
|
||||
durationMs: tc.duration_ms || tc.Duration || 0,
|
||||
detail: tc.error || tc.result?.substring?.(0, 100) || '',
|
||||
data: tc,
|
||||
});
|
||||
}
|
||||
|
||||
// 活跃会话
|
||||
const users = sessionsResult.body?.users || {};
|
||||
for (const [userID, sessions] of Object.entries(users)) {
|
||||
for (const s of sessions) {
|
||||
const ts = s.last_activity ? new Date(s.last_activity).getTime() : Date.now();
|
||||
traces.push({
|
||||
id: `session-${s.session_id || ''}`,
|
||||
timestamp: new Date(ts).toISOString(),
|
||||
ts,
|
||||
service: 'gateway',
|
||||
hop: 'session_active',
|
||||
label: `会话活跃: ${userID}`,
|
||||
status: 'success',
|
||||
durationMs: 0,
|
||||
detail: `Session: ${(s.session_id || '').substring(0, 16)}... State: ${s.state || 'idle'}`,
|
||||
data: { userID, sessionId: s.session_id, state: s.state },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 按时间倒序排列
|
||||
traces.sort((a, b) => b.ts - a.ts);
|
||||
const recent = traces.slice(0, limit);
|
||||
|
||||
// 统计摘要
|
||||
const services = [...new Set(recent.map(t => t.service))];
|
||||
const errors = recent.filter(t => t.status === 'error').length;
|
||||
const totalDuration = recent.reduce((sum, t) => sum + (t.durationMs || 0), 0);
|
||||
|
||||
res.json({
|
||||
timestamp: Date.now(),
|
||||
total: traces.length,
|
||||
shown: recent.length,
|
||||
stats: { services, errors, totalDurationMs: Math.round(totalDuration) },
|
||||
traces: recent,
|
||||
});
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: `获取链路追踪数据失败: ${err.message}` });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/trace/session/:sessionId — 特定会话的全链路追踪
|
||||
app.get('/api/trace/session/:sessionId', async (req, res) => {
|
||||
const { sessionId } = req.params;
|
||||
if (!sessionId) return res.status(400).json({ error: '缺少 sessionId' });
|
||||
|
||||
try {
|
||||
// 并行获取: 会话详情、LLM 调用、工具调用、日志搜索
|
||||
const [sessionResult, llmResult, toolResult] = await Promise.all([
|
||||
proxyToGateway(`/api/v1/admin/sessions/${sessionId}`).catch(() => ({ status: 502, body: null })),
|
||||
proxyToAICore('/api/v1/llm-calls?limit=500').catch(() => ({ status: 502, body: [] })),
|
||||
proxyToAICore('/api/v1/tools/calls?limit=200').catch(() => ({ status: 502, body: { calls: [] } })),
|
||||
]);
|
||||
|
||||
// 从日志文件中搜索 session 相关行
|
||||
const gatewayLogLines = searchLogFile('gateway', sessionId, 500);
|
||||
const aiCoreLogLines = searchLogFile('ai-core', sessionId, 500);
|
||||
|
||||
const traces = [];
|
||||
const sessionData = sessionResult.body;
|
||||
|
||||
// Gateway 日志 → 追踪节点
|
||||
for (const line of gatewayLogLines) {
|
||||
const ts = parseLogTimestamp(line);
|
||||
let hop = 'gateway_log';
|
||||
let label = 'Gateway 日志';
|
||||
if (line.includes('received') || line.includes('收到')) { hop = 'gateway_receive'; label = 'Gateway 接收消息'; }
|
||||
else if (line.includes('stream') || line.includes('流式')) { hop = 'gateway_stream'; label = 'Gateway 流式处理'; }
|
||||
else if (line.includes('send') || line.includes('发送') || line.includes('broadcast')) { hop = 'gateway_send'; label = 'Gateway 推送响应'; }
|
||||
else if (line.includes('error') || line.includes('错误')) { hop = 'gateway_error'; label = 'Gateway 错误'; }
|
||||
|
||||
traces.push({
|
||||
id: `gwlog-${ts}-${Math.random().toString(36).slice(2, 6)}`,
|
||||
timestamp: new Date(ts).toISOString(),
|
||||
ts,
|
||||
service: 'gateway',
|
||||
hop,
|
||||
label,
|
||||
status: hop === 'gateway_error' ? 'error' : 'success',
|
||||
durationMs: 0,
|
||||
detail: line.substring(0, 200),
|
||||
data: { raw: line },
|
||||
});
|
||||
}
|
||||
|
||||
// AI-Core 日志
|
||||
for (const line of aiCoreLogLines) {
|
||||
const ts = parseLogTimestamp(line);
|
||||
let hop = 'ai_core_log';
|
||||
let label = 'AI-Core 日志';
|
||||
if (line.includes('LLM') || line.includes('llm') || line.includes('chat')) { hop = 'ai_core_llm'; label = 'AI-Core LLM 处理'; }
|
||||
else if (line.includes('tool') || line.includes('Tool')) { hop = 'ai_core_tool'; label = 'AI-Core 工具调用'; }
|
||||
else if (line.includes('stream') || line.includes('SSE')) { hop = 'ai_core_stream'; label = 'AI-Core 流式输出'; }
|
||||
else if (line.includes('error') || line.includes('Error')) { hop = 'ai_core_error'; label = 'AI-Core 错误'; }
|
||||
|
||||
traces.push({
|
||||
id: `aclog-${ts}-${Math.random().toString(36).slice(2, 6)}`,
|
||||
timestamp: new Date(ts).toISOString(),
|
||||
ts,
|
||||
service: 'ai-core',
|
||||
hop,
|
||||
label,
|
||||
status: hop === 'ai_core_error' ? 'error' : 'success',
|
||||
durationMs: 0,
|
||||
detail: line.substring(0, 200),
|
||||
data: { raw: line },
|
||||
});
|
||||
}
|
||||
|
||||
// LLM 调用记录中如果有 session 相关信息也加入
|
||||
const llmCalls = Array.isArray(llmResult.body) ? llmResult.body : [];
|
||||
for (const call of llmCalls) {
|
||||
const ts = call.time ? new Date(call.time).getTime() : Date.now();
|
||||
traces.push({
|
||||
id: `llm-${ts}-${Math.random().toString(36).slice(2, 6)}`,
|
||||
timestamp: new Date(ts).toISOString(),
|
||||
ts,
|
||||
service: 'ai-core',
|
||||
hop: 'llm_call',
|
||||
label: `LLM: ${call.model || 'unknown'}`,
|
||||
status: call.success ? 'success' : 'error',
|
||||
durationMs: call.duration_ms || call.Duration || 0,
|
||||
detail: call.error || `${call.prompt_tokens || 0}→${call.completion_tokens || 0} tokens`,
|
||||
data: call,
|
||||
});
|
||||
}
|
||||
|
||||
// 按时间排序
|
||||
traces.sort((a, b) => a.ts - b.ts);
|
||||
|
||||
// 计算每一跳的耗时
|
||||
for (let i = 1; i < traces.length; i++) {
|
||||
const gap = traces[i].ts - traces[i - 1].ts;
|
||||
if (gap > 0 && !traces[i].durationMs) {
|
||||
traces[i]._gapFromPrev = gap;
|
||||
}
|
||||
}
|
||||
|
||||
const totalSpan = traces.length >= 2 ? traces[traces.length - 1].ts - traces[0].ts : 0;
|
||||
const errors = traces.filter(t => t.status === 'error').length;
|
||||
|
||||
res.json({
|
||||
timestamp: Date.now(),
|
||||
sessionId,
|
||||
session: sessionData,
|
||||
stats: {
|
||||
totalHops: traces.length,
|
||||
errors,
|
||||
totalSpanMs: totalSpan,
|
||||
services: [...new Set(traces.map(t => t.service))],
|
||||
},
|
||||
traces,
|
||||
});
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: `获取会话链路追踪失败: ${err.message}` });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* 代理请求到 Memory-Service
|
||||
* @param {string} path - Memory-Service API 路径
|
||||
@@ -1196,16 +1450,17 @@ app.get('/api/v1/thinking/:id', async (req, res) => {
|
||||
|
||||
// ---- 记忆时间线 (合并记忆 + 思考) ----
|
||||
app.get('/api/memory-timeline', async (req, res) => {
|
||||
const { user_id, limit } = req.query;
|
||||
const { user_id, limit, offset } = req.query;
|
||||
if (!user_id) {
|
||||
return res.status(400).json({ error: '缺少 user_id 参数' });
|
||||
}
|
||||
const maxItems = parseInt(limit) || 100;
|
||||
const pageOffset = parseInt(offset) || 0;
|
||||
|
||||
try {
|
||||
// 并行调用记忆和思考 API
|
||||
const memQs = new URLSearchParams({ user_id, limit: String(maxItems) }).toString();
|
||||
const thinkQs = new URLSearchParams({ user_id, limit: String(maxItems), offset: '0' }).toString();
|
||||
// 并行调用记忆和思考 API (带 offset)
|
||||
const memQs = new URLSearchParams({ user_id, limit: String(maxItems), offset: String(pageOffset) }).toString();
|
||||
const thinkQs = new URLSearchParams({ user_id, limit: String(maxItems), offset: String(pageOffset) }).toString();
|
||||
|
||||
const [memResult, thinkResult] = await Promise.all([
|
||||
proxyToMemoryService(`/api/v1/memories?${memQs}`),
|
||||
@@ -1285,6 +1540,9 @@ app.get('/api/memory-timeline', async (req, res) => {
|
||||
// 截取限制条数
|
||||
const result = timeline.slice(0, maxItems);
|
||||
|
||||
// 是否有更多数据 (两边都还有数据表示可能还有更多)
|
||||
const hasMore = memories.length >= maxItems || thinkingLogs.length >= maxItems;
|
||||
|
||||
// 统计摘要
|
||||
const stats = {
|
||||
total_memories: memories.length,
|
||||
@@ -1303,6 +1561,7 @@ app.get('/api/memory-timeline', async (req, res) => {
|
||||
timeline: result,
|
||||
stats,
|
||||
total: timeline.length,
|
||||
hasMore,
|
||||
user_id,
|
||||
});
|
||||
} catch (err) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# AI-Core Service API
|
||||
|
||||
**Base URL:** `http://<host>:8091` | **Auth:** 无(/internal 路由除外)
|
||||
**Base URL:** `http://<host>:8081` | **Auth:** 无(/internal 路由除外)
|
||||
|
||||
AI-Core 是 LLM 编排引擎,负责对话处理、意图分析、子会话调度、结果合成。所有对话和记忆端点通过 **Server-Sent Events (SSE)** 或 **JSON** 返回。
|
||||
|
||||
@@ -14,8 +14,11 @@ AI-Core 是 LLM 编排引擎,负责对话处理、意图分析、子会话调
|
||||
4. [POST /api/v1/memory — 添加记忆](#4-post-apiv1memory)
|
||||
5. [DELETE /api/v1/memory — 删除记忆](#5-delete-apiv1memory)
|
||||
6. [POST /api/v1/internal/presence — 在线状态](#6-post-apiv1internalpresence)
|
||||
7. [GET /api/v1/health — 健康检查](#7-get-apiv1health)
|
||||
8. [Model Selector 路由说明](#8-model-selector-路由说明)
|
||||
7. [GET /api/v1/system/info — OS 环境信息](#7-get-apiv1systeminfo)
|
||||
8. [GET /api/v1/tools/calls — 工具调用日志](#8-get-apiv1toolscalls)
|
||||
9. [GET /api/v1/llm-calls — LLM 调用日志](#9-get-apiv1llm-calls)
|
||||
10. [GET /api/v1/health — 健康检查](#10-get-apiv1health)
|
||||
11. [Model Selector 路由说明](#11-model-selector-路由说明)
|
||||
|
||||
---
|
||||
|
||||
@@ -29,11 +32,16 @@ AI-Core 是 LLM 编排引擎,负责对话处理、意图分析、子会话调
|
||||
|-----------|---------|------|------|
|
||||
| `user_id` | string | 是 | 用户 ID |
|
||||
| `session_id` | string | 是 | 会话 ID |
|
||||
| `message` | string | 是 | 用户消息文本 |
|
||||
| `images` | []string | 否 | base64 Data URL 数组 |
|
||||
| `message` | string | 是 | 用户消息文本(纯图片时可留空 `""`) |
|
||||
| `images` | []string | 否 | 图片 URL 数组(data URL 或 https URL) |
|
||||
| `mode` | string | 否 | 默认 `"text"` |
|
||||
| `nickname` | string | 否 | 用户昵称 |
|
||||
|
||||
> **图片预处理:** 当 `images` 非空时,AI-Core 使用视觉模型 (`routing.vision`) 自动分析图片:
|
||||
> - 纯图片 (`message=""`) → 生成场景描述作为用户消息
|
||||
> - 文字+图片 → 视觉分析追加到消息末尾
|
||||
> - 原始图片仍传递给下游 LLM(如模型支持多模态)
|
||||
|
||||
### SSE 事件
|
||||
|
||||
**delta** — 逐 token 发送
|
||||
@@ -64,6 +72,18 @@ AI-Core 是 LLM 编排引擎,负责对话处理、意图分析、子会话调
|
||||
}
|
||||
```
|
||||
|
||||
**tool_progress** — 工具执行进度
|
||||
```json
|
||||
{
|
||||
"type": "tool_progress",
|
||||
"tool_name": "host_exec",
|
||||
"status": "started|running|completed|failed",
|
||||
"progress": 0.5,
|
||||
"message": "正在执行 host_exec",
|
||||
"message_id": "string"
|
||||
}
|
||||
```
|
||||
|
||||
**done** — 流结束
|
||||
```json
|
||||
{ "message_id": "string", "mode": "string", "done": true }
|
||||
@@ -207,7 +227,65 @@ AI-Core 是 LLM 编排引擎,负责对话处理、意图分析、子会话调
|
||||
|
||||
---
|
||||
|
||||
## 7. GET /api/v1/health
|
||||
## 7. GET /api/v1/system/info
|
||||
|
||||
返回 OS 环境状态信息(WSL/Docker 后端状态、系统信息、磁盘使用)。
|
||||
|
||||
```json
|
||||
{
|
||||
"os_enabled": true,
|
||||
"backend": "wsl",
|
||||
"system": { "hostname": "...", "os": "linux", "arch": "amd64", "cpu_cores": 12, "total_memory": "16.0 GB" },
|
||||
"disk": { "path": "/", "total": "250.0 GB", "used": "120.0 GB", "free": "130.0 GB", "used_percent": 48.0 }
|
||||
}
|
||||
```
|
||||
|
||||
`os_enabled=false` 表示未配置 OS 后端。
|
||||
|
||||
---
|
||||
|
||||
## 8. GET /api/v1/tools/calls
|
||||
|
||||
工具调用日志(分页)。
|
||||
|
||||
| Query 参数 | 说明 |
|
||||
|-----------|------|
|
||||
| `tool_name` | 按工具名过滤 |
|
||||
| `limit` | 每页条数 (默认 50, 最大 500) |
|
||||
| `page` | 页码 (从 1 开始) |
|
||||
|
||||
```json
|
||||
{
|
||||
"calls": [ CallLogRecord ],
|
||||
"total": 100,
|
||||
"total_pages": 2,
|
||||
"page": 1,
|
||||
"limit": 50
|
||||
}
|
||||
```
|
||||
|
||||
### GET /api/v1/tools/calls/stats — 工具调用统计
|
||||
|
||||
返回各工具的调用次数、成功率等统计信息。
|
||||
|
||||
---
|
||||
|
||||
## 9. GET /api/v1/llm-calls
|
||||
|
||||
LLM 调用日志(调试用)。
|
||||
|
||||
`?limit=50` (默认 50, 最大 500)
|
||||
|
||||
```json
|
||||
{
|
||||
"calls": [ LLMCallRecord ],
|
||||
"total": 50
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 10. GET /api/v1/health
|
||||
|
||||
```json
|
||||
{
|
||||
@@ -219,7 +297,7 @@ AI-Core 是 LLM 编排引擎,负责对话处理、意图分析、子会话调
|
||||
|
||||
---
|
||||
|
||||
## 8. Model Selector 路由说明
|
||||
## 11. Model Selector 路由说明
|
||||
|
||||
AI-Core 使用基于用途的模型路由。模型配置从 `models.json` 加载,回退到 `.env` 环境变量。
|
||||
|
||||
@@ -232,6 +310,8 @@ AI-Core 使用基于用途的模型路由。模型配置从 `models.json` 加载
|
||||
| `intent_analysis` | `PurposeIntentAnalysis` | 意图分析 |
|
||||
| `tool_calling` | `PurposeToolCalling` | 工具调用 |
|
||||
| `memory_extraction` | `PurposeMemoryExtraction` | 记忆提取 |
|
||||
| `vision` | `PurposeVision` | 图片理解/视觉分析 (图片预处理、vision_analyze 工具) |
|
||||
| `ocr` | `PurposeOCR` | 专用 OCR 文字提取 |
|
||||
|
||||
### 路由策略
|
||||
|
||||
|
||||
+12
-1
@@ -170,7 +170,7 @@ ws://<gateway>/ws/chat?token=<jwt>&session_id=<optional>&client_id=<optional>&de
|
||||
"type": "message|voice_input|ping|history",
|
||||
"session_id": "string (可选)",
|
||||
"mode": "text|voice_msg|voice_assistant",
|
||||
"content": "string (message 类型必填)",
|
||||
"content": "string (纯图片消息可留空,文字+图片时填写提问内容)",
|
||||
"audio_data": "string (voice_input 类型必填, base64)",
|
||||
"attachments": [
|
||||
{
|
||||
@@ -250,6 +250,10 @@ ws://<gateway>/ws/chat?token=<jwt>&session_id=<optional>&client_id=<optional>&de
|
||||
| `device_update` | IoT 设备状态更新 |
|
||||
| `background_thinking` | 后台思考状态变更 |
|
||||
|
||||
> **广播机制**:服务端推送分为两类:
|
||||
> - **用户消息回显**(`type: "response"`, `role: "user"`):通过 `SendToUserExcept` 广播,排除发送者自身(发送者本地已渲染),仅同步到同用户的其他设备。
|
||||
> - **AI 回复消息**(`stream_start`、`stream_end`、`response`/`review`、`multi_message`、`stream_segments` 等 `role: "assistant"` 的消息):通过 `SendToUser` 广播给所有设备,包括发送者。
|
||||
|
||||
> **消息类型分类 (`msg_type`)**:所有 `response`、`multi_message`、`history_response`、`stream_chunk`、`thinking`、`tool_progress`、`system_info` 类型的服务端消息中,`msg_type` 字段均由后端自动分类填充,前端只需直接读取 `msg_type` 并据此渲染,无需解析消息内容来猜测类型。
|
||||
>
|
||||
> `msg_type` 可选值:
|
||||
@@ -482,6 +486,13 @@ Content-Type: `multipart/form-data`。字段 `file`。最大 20MB。
|
||||
|
||||
错误: 400 `{"error":"文件大小超过限制 (最大 20MB)","errorType":"file_too_large"}`, 400 `{"error":"不支持的文件类型: ...","errorType":"unsupported_type"}`
|
||||
|
||||
> **文件在 AI 对话中的传递链路**:客户端上传文件后获得的 `url` 为相对路径(如 `/api/v1/files/{id}/download`)。当用户消息携带 `attachments` 时:
|
||||
> 1. **Gateway** 在转发前将相对路径补全为绝对 URL(`http://127.0.0.1:{port}/api/v1/files/{id}/download`)
|
||||
> 2. **AI-Core** 的 LLM 适配器在调用外部模型 API 前,将非 `data:` 的图片 URL 下载并转为 base64 data URL
|
||||
> 3. 最终以多模态格式(`[{type: "text", text: "..."}, {type: "image_url", image_url: {url: "data:..."}}]`)传递给 LLM
|
||||
>
|
||||
> 即文件存储层对外部 LLM API 透明,无需暴露内网文件服务。
|
||||
|
||||
### GET /files — 列表
|
||||
|
||||
`?page=1&limit=20`
|
||||
|
||||
@@ -7,8 +7,12 @@ import { useAuth } from '@/hooks/useAuth';
|
||||
import { useChat } from '@/hooks/useChat';
|
||||
import { useSessionStore, isAdminUser } from '@/store/sessionStore';
|
||||
import { useChatStore } from '@/store/chatStore';
|
||||
import { usePageStore } from '@/store/pageStore';
|
||||
import { fetchMessages } from '@/api/sessions';
|
||||
import { registerServiceWorker } from '@/hooks/usePWA';
|
||||
import { ModelsAdminPage } from '@/components/admin/ModelsAdminPage';
|
||||
import { AdminDashboard } from '@/components/admin/AdminDashboard';
|
||||
import { ProfilePage } from '@/components/profile/ProfilePage';
|
||||
|
||||
/** URL Hash 工具 */
|
||||
const SESSION_HASH_PREFIX = 'session=';
|
||||
@@ -326,15 +330,42 @@ export default function App() {
|
||||
return (
|
||||
<ErrorBoundary>
|
||||
<AppLayout>
|
||||
<div className="flex flex-col h-full overflow-hidden">
|
||||
<div className="flex-1 min-h-0 overflow-hidden">
|
||||
<ChatContainer />
|
||||
</div>
|
||||
<div className="flex-shrink-0">
|
||||
<ChatInput onSend={send} />
|
||||
</div>
|
||||
</div>
|
||||
<PageRouter onSend={send} />
|
||||
</AppLayout>
|
||||
</ErrorBoundary>
|
||||
);
|
||||
}
|
||||
|
||||
type SendFn = (content: string, mode?: import('@/types/chat').ChatMode, attachments?: import('@/types/chat').MessageAttachment[]) => void;
|
||||
|
||||
function PageRouter({ onSend }: { onSend: SendFn }) {
|
||||
const currentPage = usePageStore((s) => s.currentPage);
|
||||
const isAdmin = isAdminUser(localStorage.getItem('user_id') || '');
|
||||
|
||||
switch (currentPage) {
|
||||
case 'admin-models':
|
||||
if (!isAdmin) return <ChatPage onSend={onSend} />;
|
||||
return <ModelsAdminPage />;
|
||||
case 'admin-dashboard':
|
||||
if (!isAdmin) return <ChatPage onSend={onSend} />;
|
||||
return <AdminDashboard />;
|
||||
case 'profile':
|
||||
return <ProfilePage />;
|
||||
case 'chat':
|
||||
default:
|
||||
return <ChatPage onSend={onSend} />;
|
||||
}
|
||||
}
|
||||
|
||||
function ChatPage({ onSend }: { onSend: SendFn }) {
|
||||
return (
|
||||
<div className="flex flex-col h-full overflow-hidden">
|
||||
<div className="flex-1 min-h-0 overflow-hidden">
|
||||
<ChatContainer />
|
||||
</div>
|
||||
<div className="flex-shrink-0">
|
||||
<ChatInput onSend={onSend} />
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
// Admin API — /admin/models/*, /admin/sessions/*, /admin/clients/*
|
||||
|
||||
import { request, type ApiResponse } from './client';
|
||||
import type {
|
||||
ModelProvider,
|
||||
ModelConfig,
|
||||
RoutingRule,
|
||||
SessionState,
|
||||
ClientInfo,
|
||||
HealthCheckResult,
|
||||
ModelListResponse,
|
||||
ProviderListResponse,
|
||||
AdminSessionsResponse,
|
||||
ActiveSessionsResponse,
|
||||
ClientListResponse,
|
||||
} from '@/types/admin';
|
||||
|
||||
// ========== Providers ==========
|
||||
|
||||
export function listProviders(): Promise<ApiResponse<ProviderListResponse>> {
|
||||
return request<ProviderListResponse>('/admin/models/providers');
|
||||
}
|
||||
|
||||
export function saveProvider(name: string, baseUrl: string, apiKey: string): Promise<ApiResponse<{ status: string; name: string }>> {
|
||||
return request(`/admin/models/providers/${encodeURIComponent(name)}`, {
|
||||
method: 'POST',
|
||||
body: { name, base_url: baseUrl, api_key: apiKey },
|
||||
});
|
||||
}
|
||||
|
||||
export function deleteProvider(name: string): Promise<ApiResponse<{ status: string; name: string }>> {
|
||||
return request(`/admin/models/providers/${encodeURIComponent(name)}`, { method: 'DELETE' });
|
||||
}
|
||||
|
||||
export function healthCheckProvider(provider: string): Promise<ApiResponse<HealthCheckResult>> {
|
||||
return request('/admin/models/health-check', { method: 'POST', body: { provider } });
|
||||
}
|
||||
|
||||
export function fetchProviderModels(name: string, url?: string): Promise<ApiResponse<{ models: Array<{ id: string; name?: string }> }>> {
|
||||
const query = url ? `?url=${encodeURIComponent(url)}` : '';
|
||||
return request(`/admin/models/fetch-models/${encodeURIComponent(name)}${query}`);
|
||||
}
|
||||
|
||||
// ========== Models ==========
|
||||
|
||||
export function listModels(): Promise<ApiResponse<ModelListResponse>> {
|
||||
return request<ModelListResponse>('/admin/models/models');
|
||||
}
|
||||
|
||||
export function saveModel(id: string, config: Partial<ModelConfig>): Promise<ApiResponse<{ status: string; id: string }>> {
|
||||
return request(`/admin/models/models/${encodeURIComponent(id)}`, {
|
||||
method: 'POST',
|
||||
body: { id, ...config },
|
||||
});
|
||||
}
|
||||
|
||||
export function deleteModel(id: string): Promise<ApiResponse<{ status: string; id: string }>> {
|
||||
return request(`/admin/models/models/${encodeURIComponent(id)}`, { method: 'DELETE' });
|
||||
}
|
||||
|
||||
// ========== Routing ==========
|
||||
|
||||
export function listRouting(): Promise<ApiResponse<{ rules: RoutingRule[] }>> {
|
||||
return request('/admin/models/routing');
|
||||
}
|
||||
|
||||
export function saveRouting(purpose: string, fallbackChain: string[], required?: boolean): Promise<ApiResponse<{ status: string; purpose: string }>> {
|
||||
return request(`/admin/models/routing/${encodeURIComponent(purpose)}`, {
|
||||
method: 'POST',
|
||||
body: { purpose, fallback_chain: fallbackChain, required },
|
||||
});
|
||||
}
|
||||
|
||||
export function deleteRouting(purpose: string): Promise<ApiResponse<{ status: string; purpose: string }>> {
|
||||
return request(`/admin/models/routing/${encodeURIComponent(purpose)}`, { method: 'DELETE' });
|
||||
}
|
||||
|
||||
// ========== Sessions ==========
|
||||
|
||||
export function listAdminSessions(): Promise<ApiResponse<AdminSessionsResponse>> {
|
||||
return request<AdminSessionsResponse>('/admin/sessions');
|
||||
}
|
||||
|
||||
export function listActiveSessions(): Promise<ApiResponse<ActiveSessionsResponse>> {
|
||||
return request<ActiveSessionsResponse>('/admin/sessions/active');
|
||||
}
|
||||
|
||||
export function getAdminSession(id: string): Promise<ApiResponse<SessionState>> {
|
||||
return request<SessionState>(`/admin/sessions/${encodeURIComponent(id)}`);
|
||||
}
|
||||
|
||||
// ========== Clients ==========
|
||||
|
||||
export function listClients(userId?: string): Promise<ApiResponse<ClientListResponse>> {
|
||||
const query = userId ? `?user_id=${encodeURIComponent(userId)}` : '';
|
||||
return request<ClientListResponse>(`/admin/clients${query}`);
|
||||
}
|
||||
|
||||
export function setClientNote(clientId: string, note: string): Promise<ApiResponse<{ status: string; client_id: string; note: string }>> {
|
||||
return request(`/admin/clients/${encodeURIComponent(clientId)}/note`, {
|
||||
method: 'PUT',
|
||||
body: { note },
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,222 @@
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import { listAdminSessions, listActiveSessions, listClients, setClientNote } from '@/api/admin';
|
||||
import type { SessionState, ClientInfo } from '@/types/admin';
|
||||
|
||||
type TabId = 'sessions' | 'clients';
|
||||
|
||||
export function AdminDashboard() {
|
||||
const [tab, setTab] = useState<TabId>('sessions');
|
||||
|
||||
return (
|
||||
<div className="h-full flex flex-col bg-[#FFFAF5] dark:bg-[#1a1a2e]">
|
||||
<div className="flex-shrink-0 px-6 py-4 border-b border-pink-100 dark:border-pink-900">
|
||||
<h2 className="text-lg font-semibold text-gray-800 dark:text-gray-200">管理仪表盘</h2>
|
||||
<p className="text-xs text-gray-400 mt-1">会话与客户端概览</p>
|
||||
</div>
|
||||
|
||||
<div className="flex-shrink-0 flex border-b border-pink-100 dark:border-pink-900 px-6">
|
||||
{([
|
||||
['sessions', '活跃会话'],
|
||||
['clients', '已知客户端'],
|
||||
] as [TabId, string][]).map(([id, label]) => (
|
||||
<button
|
||||
key={id}
|
||||
onClick={() => setTab(id)}
|
||||
className={`px-4 py-2.5 text-sm font-medium transition-colors border-b-2 -mb-px ${
|
||||
tab === id
|
||||
? 'text-pink-500 border-pink-500'
|
||||
: 'text-gray-400 border-transparent hover:text-gray-600'
|
||||
}`}
|
||||
>
|
||||
{label}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
|
||||
<div className="flex-1 overflow-y-auto p-6">
|
||||
{tab === 'sessions' && <SessionsTab />}
|
||||
{tab === 'clients' && <ClientsTab />}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ========== Sessions Tab ==========
|
||||
|
||||
function SessionsTab() {
|
||||
const [sessions, setSessions] = useState<SessionState[]>([]);
|
||||
const [activeUsers, setActiveUsers] = useState<Record<string, SessionState[]>>({});
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState('');
|
||||
|
||||
const load = useCallback(async () => {
|
||||
setLoading(true);
|
||||
setError('');
|
||||
const [allRes, activeRes] = await Promise.all([listAdminSessions(), listActiveSessions()]);
|
||||
if (allRes.data?.sessions) setSessions(allRes.data.sessions);
|
||||
else if (allRes.error) setError(allRes.error);
|
||||
if (activeRes.data?.users) setActiveUsers(activeRes.data.users);
|
||||
setLoading(false);
|
||||
}, []);
|
||||
|
||||
useEffect(() => { load(); }, [load]);
|
||||
|
||||
if (loading) return <div className="text-sm text-gray-400">加载中...</div>;
|
||||
|
||||
const totalMessages = sessions.reduce((sum, s) => sum + (s.message_count || 0), 0);
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
{error && (
|
||||
<div className="p-3 bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-lg text-sm text-red-500">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Stats */}
|
||||
<div className="grid grid-cols-3 gap-3">
|
||||
<div className="p-4 bg-white dark:bg-gray-800 rounded-xl border border-pink-100 dark:border-pink-900 text-center">
|
||||
<p className="text-2xl font-bold text-pink-500">{sessions.length}</p>
|
||||
<p className="text-xs text-gray-400 mt-1">总会话</p>
|
||||
</div>
|
||||
<div className="p-4 bg-white dark:bg-gray-800 rounded-xl border border-pink-100 dark:border-pink-900 text-center">
|
||||
<p className="text-2xl font-bold text-green-500">{Object.keys(activeUsers).length}</p>
|
||||
<p className="text-xs text-gray-400 mt-1">活跃用户</p>
|
||||
</div>
|
||||
<div className="p-4 bg-white dark:bg-gray-800 rounded-xl border border-pink-100 dark:border-pink-900 text-center">
|
||||
<p className="text-2xl font-bold text-blue-500">{totalMessages}</p>
|
||||
<p className="text-xs text-gray-400 mt-1">总消息</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* All Sessions */}
|
||||
<div>
|
||||
<h4 className="text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">所有会话 ({sessions.length})</h4>
|
||||
<div className="space-y-2">
|
||||
{sessions.slice(0, 50).map((s) => (
|
||||
<div key={s.session_id} className="p-3 bg-white dark:bg-gray-800 rounded-lg border border-pink-100 dark:border-pink-900 flex items-center justify-between">
|
||||
<div className="min-w-0 flex-1">
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="text-sm font-medium text-gray-700 dark:text-gray-300 truncate">
|
||||
{s.is_main ? '🏠 ' : ''}{s.title || '未命名'}
|
||||
</span>
|
||||
{s.is_main && <span className="px-1.5 py-0.5 text-[10px] bg-amber-100 text-amber-600 rounded">主对话</span>}
|
||||
</div>
|
||||
<p className="text-xs text-gray-400 mt-0.5">
|
||||
{s.user_id} · {s.message_count || 0} 条消息
|
||||
</p>
|
||||
</div>
|
||||
<span className="text-[10px] text-gray-400 ml-2 flex-shrink-0">
|
||||
{new Date(s.updated_at).toLocaleString('zh-CN')}
|
||||
</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ========== Clients Tab ==========
|
||||
|
||||
function ClientsTab() {
|
||||
const [clients, setClients] = useState<ClientInfo[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState('');
|
||||
const [editingNote, setEditingNote] = useState<{ id: string; note: string } | null>(null);
|
||||
|
||||
const load = useCallback(async () => {
|
||||
setLoading(true);
|
||||
setError('');
|
||||
const res = await listClients();
|
||||
if (res.data?.clients) setClients(res.data.clients);
|
||||
else if (res.error) setError(res.error);
|
||||
setLoading(false);
|
||||
}, []);
|
||||
|
||||
useEffect(() => { load(); }, [load]);
|
||||
|
||||
const handleSaveNote = async () => {
|
||||
if (!editingNote) return;
|
||||
const res = await setClientNote(editingNote.id, editingNote.note);
|
||||
if (res.error) setError(res.error);
|
||||
else { setEditingNote(null); await load(); }
|
||||
};
|
||||
|
||||
if (loading) return <div className="text-sm text-gray-400">加载中...</div>;
|
||||
|
||||
const onlineCount = clients.filter((c) => c.online).length;
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
{error && (
|
||||
<div className="p-3 bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-lg text-sm text-red-500">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="flex items-center gap-3">
|
||||
<span className="text-xs text-gray-400">{clients.length} 个客户端 ({onlineCount} 在线)</span>
|
||||
<button onClick={load} className="text-xs text-pink-500 hover:text-pink-600">刷新</button>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
{clients.map((c) => (
|
||||
<div key={c.client_id} className="p-4 bg-white dark:bg-gray-800 rounded-xl border border-pink-100 dark:border-pink-900">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex items-center gap-3 min-w-0 flex-1">
|
||||
<span className={`w-2.5 h-2.5 rounded-full flex-shrink-0 ${c.online ? 'bg-green-400' : 'bg-gray-300'}`} />
|
||||
<div className="min-w-0">
|
||||
<p className="text-sm font-medium text-gray-700 dark:text-gray-300 truncate">
|
||||
{c.device_name || c.client_id}
|
||||
</p>
|
||||
<p className="text-xs text-gray-400 mt-0.5 font-mono">{c.client_id}</p>
|
||||
{c.user_agent && (
|
||||
<p className="text-xs text-gray-400 truncate mt-0.5">{c.user_agent}</p>
|
||||
)}
|
||||
{c.note && (
|
||||
<p className="text-xs text-amber-500 mt-0.5">备注: {c.note}</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center gap-2 ml-3">
|
||||
{c.last_seen && (
|
||||
<span className="text-[10px] text-gray-400">{new Date(c.last_seen).toLocaleString('zh-CN')}</span>
|
||||
)}
|
||||
<button
|
||||
onClick={() => setEditingNote({ id: c.client_id, note: c.note || '' })}
|
||||
className="px-2 py-1 text-xs text-amber-500 hover:bg-amber-50 dark:hover:bg-amber-900/20 rounded transition-colors"
|
||||
>
|
||||
备注
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* Note Edit Modal */}
|
||||
{editingNote && (
|
||||
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/40">
|
||||
<div className="bg-white dark:bg-gray-800 rounded-2xl shadow-xl p-6 m-4 max-w-sm w-full border border-pink-100 dark:border-pink-700">
|
||||
<h3 className="text-lg font-semibold text-gray-800 dark:text-gray-200 mb-3">设置备注</h3>
|
||||
<p className="text-xs text-gray-400 mb-3 font-mono">{editingNote.id}</p>
|
||||
<input
|
||||
type="text" value={editingNote.note}
|
||||
onChange={(e) => setEditingNote({ ...editingNote, note: e.target.value })}
|
||||
className="w-full px-3 py-2 rounded-lg border border-pink-200 dark:border-pink-700 bg-white dark:bg-gray-700 text-sm"
|
||||
placeholder="输入备注..."
|
||||
onKeyDown={(e) => e.key === 'Enter' && handleSaveNote()}
|
||||
/>
|
||||
<div className="flex justify-end gap-3 mt-4">
|
||||
<button onClick={() => setEditingNote(null)}
|
||||
className="px-4 py-2 rounded-lg text-sm text-gray-500 hover:bg-gray-100 dark:hover:bg-gray-700">取消</button>
|
||||
<button onClick={handleSaveNote}
|
||||
className="px-4 py-2 rounded-lg text-sm bg-pink-400 hover:bg-pink-500 text-white font-medium">保存</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,484 @@
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import {
|
||||
listProviders, saveProvider, deleteProvider, healthCheckProvider, fetchProviderModels,
|
||||
listModels, saveModel, deleteModel,
|
||||
listRouting, saveRouting, deleteRouting,
|
||||
} from '@/api/admin';
|
||||
import type { ModelProvider, ModelConfig, RoutingRule } from '@/types/admin';
|
||||
|
||||
type TabId = 'providers' | 'models' | 'routing';
|
||||
|
||||
export function ModelsAdminPage() {
|
||||
const [tab, setTab] = useState<TabId>('providers');
|
||||
|
||||
return (
|
||||
<div className="h-full flex flex-col bg-[#FFFAF5] dark:bg-[#1a1a2e]">
|
||||
{/* Header */}
|
||||
<div className="flex-shrink-0 px-6 py-4 border-b border-pink-100 dark:border-pink-900">
|
||||
<h2 className="text-lg font-semibold text-gray-800 dark:text-gray-200">模型配置管理</h2>
|
||||
<p className="text-xs text-gray-400 mt-1">管理 LLM 供应商、模型和路由规则</p>
|
||||
</div>
|
||||
|
||||
{/* Tabs */}
|
||||
<div className="flex-shrink-0 flex border-b border-pink-100 dark:border-pink-900 px-6">
|
||||
{([
|
||||
['providers', '供应商'],
|
||||
['models', '模型'],
|
||||
['routing', '路由规则'],
|
||||
] as [TabId, string][]).map(([id, label]) => (
|
||||
<button
|
||||
key={id}
|
||||
onClick={() => setTab(id)}
|
||||
className={`px-4 py-2.5 text-sm font-medium transition-colors border-b-2 -mb-px ${
|
||||
tab === id
|
||||
? 'text-pink-500 border-pink-500'
|
||||
: 'text-gray-400 border-transparent hover:text-gray-600'
|
||||
}`}
|
||||
>
|
||||
{label}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* Content */}
|
||||
<div className="flex-1 overflow-y-auto p-6">
|
||||
{tab === 'providers' && <ProvidersTab />}
|
||||
{tab === 'models' && <ModelsTab />}
|
||||
{tab === 'routing' && <RoutingTab />}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ========== Providers Tab ==========
|
||||
|
||||
function ProvidersTab() {
|
||||
const [providers, setProviders] = useState<ModelProvider[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState('');
|
||||
const [editing, setEditing] = useState<{ name: string; base_url: string; api_key: string } | null>(null);
|
||||
const [showForm, setShowForm] = useState(false);
|
||||
const [formName, setFormName] = useState('');
|
||||
const [formUrl, setFormUrl] = useState('');
|
||||
const [formKey, setFormKey] = useState('');
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [healthMsg, setHealthMsg] = useState<Record<string, string>>({});
|
||||
|
||||
const load = useCallback(async () => {
|
||||
setLoading(true);
|
||||
setError('');
|
||||
const res = await listProviders();
|
||||
if (res.data?.providers) setProviders(res.data.providers);
|
||||
else if (res.error) setError(res.error);
|
||||
setLoading(false);
|
||||
}, []);
|
||||
|
||||
useEffect(() => { load(); }, [load]);
|
||||
|
||||
const handleSave = async () => {
|
||||
if (!formName) return;
|
||||
setSaving(true);
|
||||
const res = await saveProvider(formName, formUrl, formKey);
|
||||
if (res.error) setError(res.error);
|
||||
else {
|
||||
setShowForm(false);
|
||||
setFormName(''); setFormUrl(''); setFormKey('');
|
||||
await load();
|
||||
}
|
||||
setSaving(false);
|
||||
};
|
||||
|
||||
const handleDelete = async (name: string) => {
|
||||
if (!confirm(`确定要删除供应商 "${name}" 吗?`)) return;
|
||||
const res = await deleteProvider(name);
|
||||
if (res.error) setError(res.error);
|
||||
else await load();
|
||||
};
|
||||
|
||||
const handleHealth = async (provider: string) => {
|
||||
setHealthMsg((p) => ({ ...p, [provider]: '检测中...' }));
|
||||
const res = await healthCheckProvider(provider);
|
||||
setHealthMsg((p) => ({ ...p, [provider]: res.data?.message || res.error || '未知结果' }));
|
||||
};
|
||||
|
||||
const handleFetchModels = async (name: string, baseUrl: string) => {
|
||||
try {
|
||||
const res = await fetchProviderModels(name, baseUrl.replace(/\/+$/, '') + '/models');
|
||||
if (res.data) {
|
||||
alert(`获取到模型列表:\n${JSON.stringify(res.data, null, 2)}`);
|
||||
} else {
|
||||
alert('获取失败: ' + (res.error || '未知错误'));
|
||||
}
|
||||
} catch (err) {
|
||||
alert('获取失败: ' + (err instanceof Error ? err.message : '未知错误'));
|
||||
}
|
||||
};
|
||||
|
||||
if (loading) return <div className="text-sm text-gray-400">加载中...</div>;
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
{error && (
|
||||
<div className="p-3 bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-lg text-sm text-red-500">
|
||||
{error}
|
||||
<button onClick={() => setError('')} className="ml-2 text-red-400 hover:text-red-600">x</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-xs text-gray-400">{providers.length} 个供应商</span>
|
||||
<button
|
||||
onClick={() => { setShowForm(true); setEditing(null); setFormName(''); setFormUrl(''); setFormKey(''); }}
|
||||
className="px-3 py-1.5 text-xs font-medium bg-pink-400 hover:bg-pink-500 text-white rounded-lg transition-colors"
|
||||
>
|
||||
+ 添加供应商
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Add/Edit Form */}
|
||||
{(showForm || editing) && (
|
||||
<div className="p-4 bg-white dark:bg-gray-800 rounded-xl border border-pink-200 dark:border-pink-800 space-y-3">
|
||||
<h4 className="text-sm font-medium text-gray-700 dark:text-gray-300">
|
||||
{editing ? `编辑 ${editing.name}` : '新供应商'}
|
||||
</h4>
|
||||
<input
|
||||
type="text" placeholder="名称 (如 openai)" value={formName}
|
||||
onChange={(e) => setFormName(e.target.value)}
|
||||
className="w-full px-3 py-2 rounded-lg border border-pink-200 dark:border-pink-700 bg-white dark:bg-gray-700 text-sm"
|
||||
disabled={!!editing}
|
||||
/>
|
||||
<input
|
||||
type="text" placeholder="Base URL (如 https://api.openai.com/v1)" value={formUrl}
|
||||
onChange={(e) => setFormUrl(e.target.value)}
|
||||
className="w-full px-3 py-2 rounded-lg border border-pink-200 dark:border-pink-700 bg-white dark:bg-gray-700 text-sm"
|
||||
/>
|
||||
<input
|
||||
type="password" placeholder="API Key" value={formKey}
|
||||
onChange={(e) => setFormKey(e.target.value)}
|
||||
className="w-full px-3 py-2 rounded-lg border border-pink-200 dark:border-pink-700 bg-white dark:bg-gray-700 text-sm"
|
||||
/>
|
||||
<div className="flex gap-2 justify-end">
|
||||
<button onClick={() => { setShowForm(false); setEditing(null); }}
|
||||
className="px-3 py-1.5 text-xs text-gray-500 hover:bg-gray-100 dark:hover:bg-gray-700 rounded-lg">
|
||||
取消
|
||||
</button>
|
||||
<button onClick={handleSave} disabled={saving || !formName}
|
||||
className="px-3 py-1.5 text-xs font-medium bg-pink-400 hover:bg-pink-500 disabled:bg-pink-300 text-white rounded-lg">
|
||||
{saving ? '保存中...' : '保存'}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Provider List */}
|
||||
<div className="space-y-2">
|
||||
{providers.map((p) => (
|
||||
<div key={p.name} className="p-4 bg-white dark:bg-gray-800 rounded-xl border border-pink-100 dark:border-pink-900">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h5 className="text-sm font-medium text-gray-800 dark:text-gray-200">{p.name}</h5>
|
||||
<p className="text-xs text-gray-400 mt-0.5">{p.base_url}</p>
|
||||
<p className="text-xs text-gray-400">Key: {p.api_key ? p.api_key.slice(0, 8) + '...' : '(未设置)'}</p>
|
||||
</div>
|
||||
<div className="flex gap-1.5">
|
||||
<button onClick={() => handleFetchModels(p.name, p.base_url)}
|
||||
className="px-2 py-1 text-xs text-blue-500 hover:bg-blue-50 dark:hover:bg-blue-900/20 rounded transition-colors">
|
||||
获取模型
|
||||
</button>
|
||||
<button onClick={() => handleHealth(p.name)}
|
||||
className="px-2 py-1 text-xs text-green-500 hover:bg-green-50 dark:hover:bg-green-900/20 rounded transition-colors">
|
||||
检测
|
||||
</button>
|
||||
<button onClick={() => { setEditing({ name: p.name, base_url: p.base_url, api_key: p.api_key }); setFormName(p.name); setFormUrl(p.base_url); setFormKey(p.api_key); }}
|
||||
className="px-2 py-1 text-xs text-amber-500 hover:bg-amber-50 dark:hover:bg-amber-900/20 rounded transition-colors">
|
||||
编辑
|
||||
</button>
|
||||
<button onClick={() => handleDelete(p.name)}
|
||||
className="px-2 py-1 text-xs text-red-400 hover:bg-red-50 dark:hover:bg-red-900/20 rounded transition-colors">
|
||||
删除
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
{healthMsg[p.name] && (
|
||||
<p className="mt-2 text-xs text-gray-500 bg-gray-50 dark:bg-gray-900 rounded p-2">{healthMsg[p.name]}</p>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ========== Models Tab ==========
|
||||
|
||||
function ModelsTab() {
|
||||
const [models, setModels] = useState<ModelConfig[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState('');
|
||||
const [showForm, setShowForm] = useState(false);
|
||||
const [editId, setEditId] = useState('');
|
||||
const [form, setForm] = useState({ id: '', name: '', provider: '', description: '', paramsJson: '{}' });
|
||||
const [saving, setSaving] = useState(false);
|
||||
|
||||
const load = useCallback(async () => {
|
||||
setLoading(true);
|
||||
setError('');
|
||||
const res = await listModels();
|
||||
if (res.data?.models) setModels(res.data.models);
|
||||
else if (res.error) setError(res.error);
|
||||
setLoading(false);
|
||||
}, []);
|
||||
|
||||
useEffect(() => { load(); }, [load]);
|
||||
|
||||
const handleSave = async () => {
|
||||
if (!form.id || !form.provider) return;
|
||||
setSaving(true);
|
||||
let params: Record<string, unknown> | undefined;
|
||||
try { params = JSON.parse(form.paramsJson); } catch { params = undefined; }
|
||||
const res = await saveModel(form.id, {
|
||||
name: form.name, provider: form.provider, description: form.description, params,
|
||||
});
|
||||
if (res.error) setError(res.error);
|
||||
else { setShowForm(false); await load(); }
|
||||
setSaving(false);
|
||||
};
|
||||
|
||||
const handleEdit = (m: ModelConfig) => {
|
||||
setEditId(m.id);
|
||||
setForm({ id: m.id, name: m.name, provider: m.provider, description: m.description || '', paramsJson: JSON.stringify(m.params || {}, null, 2) });
|
||||
setShowForm(true);
|
||||
};
|
||||
|
||||
const handleDelete = async (id: string) => {
|
||||
if (!confirm(`确定要删除模型 "${id}" 吗?`)) return;
|
||||
const res = await deleteModel(id);
|
||||
if (res.error) setError(res.error);
|
||||
else await load();
|
||||
};
|
||||
|
||||
if (loading) return <div className="text-sm text-gray-400">加载中...</div>;
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
{error && (
|
||||
<div className="p-3 bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-lg text-sm text-red-500">
|
||||
{error}
|
||||
<button onClick={() => setError('')} className="ml-2 text-red-400 hover:text-red-600">x</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-xs text-gray-400">{models.length} 个模型</span>
|
||||
<button
|
||||
onClick={() => { setEditId(''); setForm({ id: '', name: '', provider: '', description: '', paramsJson: '{}' }); setShowForm(true); }}
|
||||
className="px-3 py-1.5 text-xs font-medium bg-pink-400 hover:bg-pink-500 text-white rounded-lg transition-colors"
|
||||
>
|
||||
+ 添加模型
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{showForm && (
|
||||
<div className="p-4 bg-white dark:bg-gray-800 rounded-xl border border-pink-200 dark:border-pink-800 space-y-3">
|
||||
<h4 className="text-sm font-medium text-gray-700 dark:text-gray-300">{editId ? `编辑 ${editId}` : '新模型'}</h4>
|
||||
<div className="grid grid-cols-2 gap-3">
|
||||
<div>
|
||||
<label className="text-xs text-gray-400">ID</label>
|
||||
<input type="text" placeholder="如 gpt-4o" value={form.id}
|
||||
onChange={(e) => setForm({ ...form, id: e.target.value })}
|
||||
disabled={!!editId}
|
||||
className="w-full px-3 py-2 rounded-lg border border-pink-200 dark:border-pink-700 bg-white dark:bg-gray-700 text-sm mt-1" />
|
||||
</div>
|
||||
<div>
|
||||
<label className="text-xs text-gray-400">名称</label>
|
||||
<input type="text" placeholder="显示名称" value={form.name}
|
||||
onChange={(e) => setForm({ ...form, name: e.target.value })}
|
||||
className="w-full px-3 py-2 rounded-lg border border-pink-200 dark:border-pink-700 bg-white dark:bg-gray-700 text-sm mt-1" />
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<label className="text-xs text-gray-400">供应商</label>
|
||||
<input type="text" placeholder="如 openai" value={form.provider}
|
||||
onChange={(e) => setForm({ ...form, provider: e.target.value })}
|
||||
className="w-full px-3 py-2 rounded-lg border border-pink-200 dark:border-pink-700 bg-white dark:bg-gray-700 text-sm mt-1" />
|
||||
</div>
|
||||
<div>
|
||||
<label className="text-xs text-gray-400">描述</label>
|
||||
<input type="text" value={form.description}
|
||||
onChange={(e) => setForm({ ...form, description: e.target.value })}
|
||||
className="w-full px-3 py-2 rounded-lg border border-pink-200 dark:border-pink-700 bg-white dark:bg-gray-700 text-sm mt-1" />
|
||||
</div>
|
||||
<div>
|
||||
<label className="text-xs text-gray-400">参数 (JSON)</label>
|
||||
<textarea rows={4} value={form.paramsJson}
|
||||
onChange={(e) => setForm({ ...form, paramsJson: e.target.value })}
|
||||
className="w-full px-3 py-2 rounded-lg border border-pink-200 dark:border-pink-700 bg-white dark:bg-gray-700 text-sm mt-1 font-mono" />
|
||||
</div>
|
||||
<div className="flex gap-2 justify-end">
|
||||
<button onClick={() => setShowForm(false)}
|
||||
className="px-3 py-1.5 text-xs text-gray-500 hover:bg-gray-100 dark:hover:bg-gray-700 rounded-lg">取消</button>
|
||||
<button onClick={handleSave} disabled={saving || !form.id}
|
||||
className="px-3 py-1.5 text-xs font-medium bg-pink-400 hover:bg-pink-500 disabled:bg-pink-300 text-white rounded-lg">
|
||||
{saving ? '保存中...' : '保存'}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="space-y-2">
|
||||
{models.map((m) => (
|
||||
<div key={m.id} className="p-4 bg-white dark:bg-gray-800 rounded-xl border border-pink-100 dark:border-pink-900">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex items-center gap-2">
|
||||
<h5 className="text-sm font-medium text-gray-800 dark:text-gray-200">{m.id}</h5>
|
||||
{m.enabled === false && (
|
||||
<span className="px-1.5 py-0.5 text-[10px] bg-red-100 text-red-500 rounded">禁用</span>
|
||||
)}
|
||||
{m.tags && m.tags.length > 0 && m.tags.map((t) => (
|
||||
<span key={t} className="px-1.5 py-0.5 text-[10px] bg-blue-100 text-blue-500 rounded">{t}</span>
|
||||
))}
|
||||
</div>
|
||||
<p className="text-xs text-gray-400 mt-0.5">
|
||||
{m.name} · {m.provider} · 优先级 {m.priority ?? '-'}
|
||||
{m.description ? ` · ${m.description}` : ''}
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex gap-1.5 ml-3">
|
||||
<button onClick={() => handleEdit(m)}
|
||||
className="px-2 py-1 text-xs text-amber-500 hover:bg-amber-50 dark:hover:bg-amber-900/20 rounded transition-colors">编辑</button>
|
||||
<button onClick={() => handleDelete(m.id)}
|
||||
className="px-2 py-1 text-xs text-red-400 hover:bg-red-50 dark:hover:bg-red-900/20 rounded transition-colors">删除</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ========== Routing Tab ==========
|
||||
|
||||
function RoutingTab() {
|
||||
const [rules, setRules] = useState<RoutingRule[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState('');
|
||||
const [showForm, setShowForm] = useState(false);
|
||||
const [formPurpose, setFormPurpose] = useState('');
|
||||
const [formChain, setFormChain] = useState('');
|
||||
const [formRequired, setFormRequired] = useState(false);
|
||||
const [saving, setSaving] = useState(false);
|
||||
|
||||
const load = useCallback(async () => {
|
||||
setLoading(true);
|
||||
setError('');
|
||||
const res = await listRouting();
|
||||
if (res.data) {
|
||||
// Response may wrap rules in an object or be the array directly
|
||||
const data = res.data as unknown as { rules?: RoutingRule[] } | RoutingRule[];
|
||||
if (Array.isArray(data)) setRules(data);
|
||||
else if (data?.rules) setRules(data.rules);
|
||||
} else if (res.error) setError(res.error);
|
||||
setLoading(false);
|
||||
}, []);
|
||||
|
||||
useEffect(() => { load(); }, [load]);
|
||||
|
||||
const handleSave = async () => {
|
||||
if (!formPurpose || !formChain.trim()) return;
|
||||
setSaving(true);
|
||||
const chain = formChain.split(',').map((s) => s.trim()).filter(Boolean);
|
||||
const res = await saveRouting(formPurpose, chain, formRequired);
|
||||
if (res.error) setError(res.error);
|
||||
else { setShowForm(false); setFormPurpose(''); setFormChain(''); await load(); }
|
||||
setSaving(false);
|
||||
};
|
||||
|
||||
const handleDelete = async (purpose: string) => {
|
||||
if (!confirm(`确定要删除路由规则 "${purpose}" 吗?`)) return;
|
||||
const res = await deleteRouting(purpose);
|
||||
if (res.error) setError(res.error);
|
||||
else await load();
|
||||
};
|
||||
|
||||
if (loading) return <div className="text-sm text-gray-400">加载中...</div>;
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
{error && (
|
||||
<div className="p-3 bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-lg text-sm text-red-500">
|
||||
{error}
|
||||
<button onClick={() => setError('')} className="ml-2 text-red-400 hover:text-red-600">x</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-xs text-gray-400">{rules.length} 条规则</span>
|
||||
<button
|
||||
onClick={() => { setShowForm(true); setFormPurpose(''); setFormChain(''); setFormRequired(false); }}
|
||||
className="px-3 py-1.5 text-xs font-medium bg-pink-400 hover:bg-pink-500 text-white rounded-lg transition-colors"
|
||||
>
|
||||
+ 添加规则
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{showForm && (
|
||||
<div className="p-4 bg-white dark:bg-gray-800 rounded-xl border border-pink-200 dark:border-pink-800 space-y-3">
|
||||
<h4 className="text-sm font-medium text-gray-700 dark:text-gray-300">新路由规则</h4>
|
||||
<div>
|
||||
<label className="text-xs text-gray-400">Purpose</label>
|
||||
<input type="text" placeholder="如 chat, deep_thinking, vision..." value={formPurpose}
|
||||
onChange={(e) => setFormPurpose(e.target.value)}
|
||||
className="w-full px-3 py-2 rounded-lg border border-pink-200 dark:border-pink-700 bg-white dark:bg-gray-700 text-sm mt-1" />
|
||||
</div>
|
||||
<div>
|
||||
<label className="text-xs text-gray-400">Fallback Chain (逗号分隔)</label>
|
||||
<input type="text" placeholder="如 gpt-4o, gpt-4o-mini, claude-sonnet-4-6" value={formChain}
|
||||
onChange={(e) => setFormChain(e.target.value)}
|
||||
className="w-full px-3 py-2 rounded-lg border border-pink-200 dark:border-pink-700 bg-white dark:bg-gray-700 text-sm mt-1" />
|
||||
</div>
|
||||
<label className="flex items-center gap-2 text-sm text-gray-600 dark:text-gray-400">
|
||||
<input type="checkbox" checked={formRequired} onChange={(e) => setFormRequired(e.target.checked)}
|
||||
className="rounded border-pink-300 text-pink-500 focus:ring-pink-400" />
|
||||
必须匹配
|
||||
</label>
|
||||
<div className="flex gap-2 justify-end">
|
||||
<button onClick={() => setShowForm(false)}
|
||||
className="px-3 py-1.5 text-xs text-gray-500 hover:bg-gray-100 dark:hover:bg-gray-700 rounded-lg">取消</button>
|
||||
<button onClick={handleSave} disabled={saving || !formPurpose}
|
||||
className="px-3 py-1.5 text-xs font-medium bg-pink-400 hover:bg-pink-500 disabled:bg-pink-300 text-white rounded-lg">
|
||||
{saving ? '保存中...' : '保存'}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="space-y-2">
|
||||
{rules.map((r) => (
|
||||
<div key={r.purpose} className="p-4 bg-white dark:bg-gray-800 rounded-xl border border-pink-100 dark:border-pink-900">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex items-center gap-2">
|
||||
<h5 className="text-sm font-medium text-gray-800 dark:text-gray-200 font-mono">{r.purpose}</h5>
|
||||
{r.required && (
|
||||
<span className="px-1.5 py-0.5 text-[10px] bg-amber-100 text-amber-600 rounded">必须</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex flex-wrap gap-1 mt-1.5">
|
||||
{r.fallback_chain.map((m, i) => (
|
||||
<span key={m} className="px-2 py-0.5 text-[10px] bg-pink-50 dark:bg-pink-900/20 text-pink-600 dark:text-pink-400 rounded-full">
|
||||
{i === 0 ? '★ ' : ''}{m}
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
<button onClick={() => handleDelete(r.purpose)}
|
||||
className="px-2 py-1 text-xs text-red-400 hover:bg-red-50 dark:hover:bg-red-900/20 rounded transition-colors ml-3">删除</button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -191,6 +191,51 @@ export function ChatInput({ onSend, disabled }: ChatInputProps) {
|
||||
});
|
||||
}, []);
|
||||
|
||||
// 拖拽上传
|
||||
const [isDragOver, setIsDragOver] = useState(false);
|
||||
const dragCounterRef = useRef(0);
|
||||
|
||||
const handleDragEnter = useCallback((e: React.DragEvent) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
dragCounterRef.current++;
|
||||
if (e.dataTransfer.items && e.dataTransfer.items.length > 0) {
|
||||
setIsDragOver(true);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const handleDragLeave = useCallback((e: React.DragEvent) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
dragCounterRef.current--;
|
||||
if (dragCounterRef.current <= 0) {
|
||||
dragCounterRef.current = 0;
|
||||
setIsDragOver(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const handleDragOver = useCallback((e: React.DragEvent) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
}, []);
|
||||
|
||||
const handleDrop = useCallback((e: React.DragEvent) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
setIsDragOver(false);
|
||||
dragCounterRef.current = 0;
|
||||
|
||||
const files = e.dataTransfer?.files;
|
||||
if (files) {
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
const file = files[i];
|
||||
if (file.type.startsWith('image/')) {
|
||||
addImageFile(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
}, [addImageFile]);
|
||||
|
||||
// 文件选择
|
||||
const handleFileSelect = useCallback(
|
||||
(e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
@@ -222,7 +267,22 @@ export function ChatInput({ onSend, disabled }: ChatInputProps) {
|
||||
}, [isListening, startListening, stopListening]);
|
||||
|
||||
return (
|
||||
<div className="border-t border-pink-100 dark:border-pink-900 bg-white/80 dark:bg-gray-900/80 backdrop-blur-sm px-4 py-3">
|
||||
<div
|
||||
className={`relative border-t border-pink-100 dark:border-pink-900 bg-white/80 dark:bg-gray-900/80 backdrop-blur-sm px-4 py-3 transition-colors ${isDragOver ? 'bg-pink-50/80 dark:bg-pink-900/20' : ''}`}
|
||||
onDragEnter={handleDragEnter}
|
||||
onDragLeave={handleDragLeave}
|
||||
onDragOver={handleDragOver}
|
||||
onDrop={handleDrop}
|
||||
>
|
||||
{/* 拖拽上传覆盖层 */}
|
||||
{isDragOver && (
|
||||
<div className="absolute inset-0 z-10 bg-pink-100/60 dark:bg-pink-900/40 border-2 border-dashed border-pink-400 rounded-lg flex items-center justify-center pointer-events-none">
|
||||
<div className="text-center">
|
||||
<span className="text-3xl">📷</span>
|
||||
<p className="text-sm text-pink-500 font-medium mt-1">释放以添加图片</p>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
<div className="flex flex-col gap-2 max-w-3xl mx-auto">
|
||||
{/* 昔涟正在输入指示器 */}
|
||||
{isTyping && (
|
||||
|
||||
@@ -3,7 +3,7 @@ import { CyreneAvatar } from '@/components/persona/CyreneAvatar';
|
||||
import { useAuthStore } from '@/store/authStore';
|
||||
import { useChatStore } from '@/store/chatStore';
|
||||
import { useSpeechSynthesis } from '@/hooks/useSpeechSynthesis';
|
||||
import type { MessageAttachment, MultiMessageItem, StreamSegment, MessageDisplayType } from '@/types/chat';
|
||||
import type { MessageAttachment, MultiMessageItem, StreamSegment, MessageDisplayType, ToolProgressInfo, ToolCall } from '@/types/chat';
|
||||
import { ImageLightbox } from './ImageLightbox';
|
||||
import { MarkdownRenderer } from './MarkdownRenderer';
|
||||
|
||||
@@ -18,6 +18,7 @@ interface MessageBubbleProps {
|
||||
streamSegments?: StreamSegment[];
|
||||
msgType?: MessageDisplayType;
|
||||
metadata?: Record<string, unknown>;
|
||||
audioUrl?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -155,6 +156,7 @@ export function MessageBubble({
|
||||
streamSegments,
|
||||
msgType,
|
||||
metadata,
|
||||
audioUrl,
|
||||
}: MessageBubbleProps) {
|
||||
const isUser = role === 'user';
|
||||
const isAction = role === 'action' || msgType === 'action';
|
||||
@@ -179,12 +181,32 @@ export function MessageBubble({
|
||||
);
|
||||
}
|
||||
|
||||
// 工具进度 — 紧凑进度行
|
||||
// 工具进度 — 带进度条的紧凑行
|
||||
if (isToolProgress) {
|
||||
const tp: ToolProgressInfo | undefined = metadata?.tool_progress as ToolProgressInfo | undefined;
|
||||
const progress = tp?.progress ?? 0;
|
||||
const toolName = tp?.tool_name ?? '';
|
||||
const status = tp?.status ?? 'running';
|
||||
const statusColor =
|
||||
status === 'completed' ? 'bg-green-400' :
|
||||
status === 'failed' ? 'bg-red-400' :
|
||||
status === 'started' ? 'bg-blue-400' :
|
||||
'bg-blue-400 animate-pulse';
|
||||
const progressText = status === 'completed' ? '完成' :
|
||||
status === 'failed' ? '失败' :
|
||||
`${Math.round(progress * 100)}%`;
|
||||
|
||||
return (
|
||||
<div className="flex items-center gap-2 mx-4 my-1 px-3 py-1 text-xs text-gray-400 dark:text-gray-500">
|
||||
<span className="inline-block w-2 h-2 rounded-full bg-blue-400 animate-pulse" />
|
||||
<span>{content}</span>
|
||||
<div className="flex items-center gap-2 mx-4 my-1 px-3 py-1.5 text-xs text-gray-500 dark:text-gray-400 bg-gray-50 dark:bg-gray-800/50 rounded-lg border border-gray-100 dark:border-gray-800">
|
||||
{toolName && <span className="font-medium text-gray-600 dark:text-gray-300 flex-shrink-0">{toolName}</span>}
|
||||
<div className="flex-1 h-1.5 bg-gray-200 dark:bg-gray-700 rounded-full overflow-hidden min-w-[60px]">
|
||||
<div
|
||||
className={`h-full rounded-full transition-all duration-300 ${statusColor}`}
|
||||
style={{ width: `${Math.max(progress * 100, 2)}%` }}
|
||||
/>
|
||||
</div>
|
||||
<span className="flex-shrink-0 text-[10px]">{progressText}</span>
|
||||
{content && <span className="text-gray-400 truncate hidden sm:inline">{content}</span>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -239,6 +261,7 @@ export function MessageBubble({
|
||||
);
|
||||
}
|
||||
|
||||
const toolCalls = (metadata?.tool_calls as ToolCall[] | undefined) ?? [];
|
||||
const [lightboxIndex, setLightboxIndex] = useState<number | null>(null);
|
||||
const time = new Date(timestamp).toLocaleTimeString('zh-CN', {
|
||||
hour: '2-digit',
|
||||
@@ -330,6 +353,20 @@ export function MessageBubble({
|
||||
</p>
|
||||
)}
|
||||
|
||||
{/* 音频播放器 */}
|
||||
{!isStreaming && audioUrl && (
|
||||
<div className="mt-2">
|
||||
<audio controls className="w-full max-w-[300px] h-8" src={audioUrl}>
|
||||
您的浏览器不支持音频播放
|
||||
</audio>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 工具调用信息 */}
|
||||
{!isStreaming && toolCalls.length > 0 && (
|
||||
<ToolCallsInfo toolCalls={toolCalls} />
|
||||
)}
|
||||
|
||||
{/* 图片附件网格 */}
|
||||
{!isStreaming && imageAttachments.length > 0 && (
|
||||
<div
|
||||
@@ -452,6 +489,48 @@ function ImageThumbnail({
|
||||
);
|
||||
}
|
||||
|
||||
/** 工具调用信息展示 */
|
||||
function ToolCallsInfo({ toolCalls }: { toolCalls: ToolCall[] }) {
|
||||
const [expanded, setExpanded] = useState(false);
|
||||
|
||||
if (!toolCalls || toolCalls.length === 0) return null;
|
||||
|
||||
return (
|
||||
<div className="mt-2 pt-2 border-t border-pink-100 dark:border-pink-800">
|
||||
<button
|
||||
onClick={() => setExpanded(!expanded)}
|
||||
className="flex items-center gap-1 text-[10px] text-gray-400 hover:text-pink-500 transition-colors"
|
||||
>
|
||||
<span>{expanded ? '▼' : '▶'}</span>
|
||||
<span>工具调用 ({toolCalls.length})</span>
|
||||
</button>
|
||||
{expanded && (
|
||||
<div className="mt-1.5 space-y-1.5">
|
||||
{toolCalls.map((tc, i) => (
|
||||
<div key={i} className="bg-gray-50 dark:bg-gray-900 rounded-lg p-2 text-xs">
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="font-medium text-pink-500">{tc.name}</span>
|
||||
{tc.result !== undefined && (
|
||||
<span className="text-[10px] text-green-500">
|
||||
{typeof tc.result === 'string' && tc.result.length > 60
|
||||
? tc.result.slice(0, 60) + '...'
|
||||
: JSON.stringify(tc.result).slice(0, 60)}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
{tc.arguments && Object.keys(tc.arguments).length > 0 && (
|
||||
<div className="mt-1 text-gray-400 font-mono break-all">
|
||||
{JSON.stringify(tc.arguments, null, 0).slice(0, 120)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/** 用户头像组件:管理员使用 Admin_Avatar.jpg,普通用户使用 Default_Avatar.png */
|
||||
function UserAvatar() {
|
||||
const [imgError, setImgError] = useState(false);
|
||||
|
||||
@@ -74,6 +74,7 @@ export function MessageList({
|
||||
attachments={msg.attachments}
|
||||
msgType={msg.msgType}
|
||||
metadata={msg.metadata}
|
||||
audioUrl={msg.audioUrl}
|
||||
/>
|
||||
))}
|
||||
<div ref={bottomRef} />
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { useRef, useEffect, useState } from 'react';
|
||||
import { useRef, useEffect, useState, useCallback } from 'react';
|
||||
import { CyreneAvatar } from '@/components/persona/CyreneAvatar';
|
||||
import { MoodIndicator } from '@/components/persona/MoodIndicator';
|
||||
import { useAuth } from '@/hooks/useAuth';
|
||||
import { usePWA } from '@/hooks/usePWA';
|
||||
import { useNotificationStore } from '@/store/notificationStore';
|
||||
import { useSessionStore } from '@/store/sessionStore';
|
||||
import { getToken } from '@/api/client';
|
||||
import { ReminderPanel } from '@/components/layout/ReminderPanel';
|
||||
import { BriefingPanel } from '@/components/layout/BriefingPanel';
|
||||
import { AutomationPanel } from '@/components/layout/AutomationPanel';
|
||||
@@ -60,12 +61,31 @@ export function Header({ onMenuClick, onSearchClick }: HeaderProps) {
|
||||
// PWA Hook
|
||||
const { isInstallable, isInstalled, hasUpdate, install, update } = usePWA();
|
||||
|
||||
// 下拉面板标签页切换:通知 / 提醒 / 简报
|
||||
// 下拉面板标签页切换
|
||||
const [dropdownTab, setDropdownTab] = useState<'notifications' | 'reminders' | 'briefing' | 'automation' | 'files' | 'knowledge'>('notifications');
|
||||
|
||||
// 获取当前用户 ID
|
||||
const userId = localStorage.getItem('user_id') || '';
|
||||
|
||||
// 健康检查指示器
|
||||
const [apiOnline, setApiOnline] = useState<boolean | null>(null);
|
||||
const checkHealth = useCallback(async () => {
|
||||
try {
|
||||
const API_BASE = import.meta.env.VITE_API_URL || '/api/v1';
|
||||
const resp = await fetch(`${API_BASE}/health`);
|
||||
const data = await resp.json();
|
||||
setApiOnline(data?.status === 'ok');
|
||||
} catch {
|
||||
setApiOnline(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
checkHealth();
|
||||
const interval = setInterval(checkHealth, 30000);
|
||||
return () => clearInterval(interval);
|
||||
}, [checkHealth]);
|
||||
|
||||
// 点击外部关闭下拉
|
||||
useEffect(() => {
|
||||
function handleClickOutside(e: MouseEvent) {
|
||||
@@ -102,9 +122,19 @@ export function Header({ onMenuClick, onSearchClick }: HeaderProps) {
|
||||
|
||||
<CyreneAvatar size="sm" />
|
||||
<div>
|
||||
<h1 className="text-base font-semibold text-pink-600 dark:text-pink-400">
|
||||
昔涟
|
||||
</h1>
|
||||
<div className="flex items-center gap-1.5">
|
||||
<h1 className="text-base font-semibold text-pink-600 dark:text-pink-400">
|
||||
昔涟
|
||||
</h1>
|
||||
{apiOnline !== null && (
|
||||
<span
|
||||
className={`w-1.5 h-1.5 rounded-full flex-shrink-0 ${
|
||||
apiOnline ? 'bg-green-400' : 'bg-red-400 animate-pulse'
|
||||
}`}
|
||||
title={apiOnline ? 'API 在线' : 'API 离线'}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
<MoodIndicator />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { useState, useCallback } from 'react';
|
||||
import { useSession } from '@/hooks/useSession';
|
||||
import { useSessionStore } from '@/store/sessionStore';
|
||||
import { usePageStore } from '@/store/pageStore';
|
||||
import { CyreneAvatar } from '@/components/persona/CyreneAvatar';
|
||||
import { exportSession, type ExportFormat } from '@/api/sessions';
|
||||
import type { Session } from '@/types/session';
|
||||
@@ -60,14 +61,15 @@ export function Sidebar({ onClose, onMemoryClick }: SidebarProps) {
|
||||
|
||||
const handleSelectSession = (id: string) => {
|
||||
setCurrentSession(id);
|
||||
usePageStore.getState().goToChat();
|
||||
if (onClose) onClose();
|
||||
};
|
||||
|
||||
const handleMainSession = async () => {
|
||||
// 找到主对话
|
||||
const mainSession = displaySessions.find((s) => s.is_main);
|
||||
if (mainSession) {
|
||||
setCurrentSession(mainSession.id);
|
||||
usePageStore.getState().goToChat();
|
||||
if (onClose) onClose();
|
||||
}
|
||||
};
|
||||
@@ -278,8 +280,25 @@ export function Sidebar({ onClose, onMemoryClick }: SidebarProps) {
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Admin 管理导航 */}
|
||||
{isAdmin && (
|
||||
<div className="px-3 pt-2 border-t border-pink-100 dark:border-pink-900 space-y-1">
|
||||
<p className="px-1 text-[10px] text-gray-400 uppercase tracking-wider">管理</p>
|
||||
<AdminNavButton page="admin-models" icon="🤖" label="模型配置" />
|
||||
<AdminNavButton page="admin-dashboard" icon="📊" label="仪表盘" />
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 底部:记忆管理 + 一键清空所有对话 */}
|
||||
<div className="p-3 border-t border-pink-100 dark:border-pink-900 space-y-2">
|
||||
{/* 个人资料 */}
|
||||
<button
|
||||
onClick={() => usePageStore.getState().setPage('profile')}
|
||||
className="w-full flex items-center justify-center gap-1.5 px-3 py-2 bg-blue-50 hover:bg-blue-100 dark:bg-blue-900/20 dark:hover:bg-blue-900/30 text-blue-500 hover:text-blue-600 rounded-xl text-xs font-medium transition-colors border border-blue-200 dark:border-blue-800"
|
||||
>
|
||||
<span>👤</span>
|
||||
<span>个人资料</span>
|
||||
</button>
|
||||
{onMemoryClick && (
|
||||
<button
|
||||
onClick={onMemoryClick}
|
||||
@@ -343,3 +362,24 @@ export function Sidebar({ onClose, onMemoryClick }: SidebarProps) {
|
||||
</aside>
|
||||
);
|
||||
}
|
||||
|
||||
/** Admin 导航按钮 */
|
||||
function AdminNavButton({ page, icon, label }: { page: string; icon: string; label: string }) {
|
||||
const currentPage = usePageStore((s) => s.currentPage);
|
||||
const setPage = usePageStore((s) => s.setPage);
|
||||
const isActive = currentPage === page;
|
||||
|
||||
return (
|
||||
<button
|
||||
onClick={() => setPage(page as 'admin-models' | 'admin-dashboard')}
|
||||
className={`w-full flex items-center gap-2 px-3 py-2 rounded-lg text-xs font-medium transition-colors ${
|
||||
isActive
|
||||
? 'bg-pink-50 dark:bg-pink-900/30 text-pink-600'
|
||||
: 'text-gray-500 hover:bg-gray-50 dark:hover:bg-gray-800 hover:text-gray-700 dark:hover:text-gray-300'
|
||||
}`}
|
||||
>
|
||||
<span>{icon}</span>
|
||||
<span>{label}</span>
|
||||
</button>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { request } from '@/api/client';
|
||||
import type { ProfileInfo } from '@/types/admin';
|
||||
import { useAuth } from '@/hooks/useAuth';
|
||||
import { usePageStore } from '@/store/pageStore';
|
||||
|
||||
export function ProfilePage() {
|
||||
const { logout } = useAuth();
|
||||
const goToChat = usePageStore((s) => s.goToChat);
|
||||
const [profile, setProfile] = useState<ProfileInfo | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState('');
|
||||
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
setLoading(true);
|
||||
const res = await request<ProfileInfo>('/profile');
|
||||
if (res.data) setProfile(res.data);
|
||||
else if (res.error) setError(res.error);
|
||||
setLoading(false);
|
||||
})();
|
||||
}, []);
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="h-full flex items-center justify-center bg-[#FFFAF5] dark:bg-[#1a1a2e]">
|
||||
<p className="text-sm text-gray-400">加载中...</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="h-full flex flex-col bg-[#FFFAF5] dark:bg-[#1a1a2e]">
|
||||
<div className="flex-shrink-0 px-6 py-4 border-b border-pink-100 dark:border-pink-900">
|
||||
<h2 className="text-lg font-semibold text-gray-800 dark:text-gray-200">个人资料</h2>
|
||||
<p className="text-xs text-gray-400 mt-1">账号信息与设置</p>
|
||||
</div>
|
||||
|
||||
<div className="flex-1 overflow-y-auto p-6">
|
||||
{error && (
|
||||
<div className="p-3 mb-4 bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-lg text-sm text-red-500">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{profile && (
|
||||
<div className="max-w-md space-y-4">
|
||||
{/* Avatar */}
|
||||
<div className="flex items-center gap-4 p-4 bg-white dark:bg-gray-800 rounded-xl border border-pink-100 dark:border-pink-900">
|
||||
<div className="w-16 h-16 rounded-full bg-pink-100 dark:bg-pink-900/30 flex items-center justify-center text-2xl">
|
||||
🌸
|
||||
</div>
|
||||
<div>
|
||||
<h3 className="text-lg font-semibold text-gray-800 dark:text-gray-200">
|
||||
{profile.nickname || profile.username}
|
||||
</h3>
|
||||
<p className="text-sm text-gray-400">@{profile.username}</p>
|
||||
{profile.is_admin && (
|
||||
<span className="inline-block mt-1 px-2 py-0.5 text-[10px] bg-pink-100 dark:bg-pink-900/30 text-pink-500 rounded-full">
|
||||
管理员
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Details */}
|
||||
<div className="p-4 bg-white dark:bg-gray-800 rounded-xl border border-pink-100 dark:border-pink-900 space-y-3">
|
||||
<DetailRow label="用户 ID" value={profile.user_id} />
|
||||
<DetailRow label="用户名" value={profile.username} />
|
||||
<DetailRow label="昵称" value={profile.nickname || '-'} />
|
||||
<DetailRow label="账号类型" value={profile.is_admin ? '管理员' : '普通用户'} />
|
||||
<DetailRow label="注册时间" value={new Date(profile.created_at).toLocaleString('zh-CN')} />
|
||||
</div>
|
||||
|
||||
{/* Actions */}
|
||||
<div className="flex gap-3">
|
||||
<button
|
||||
onClick={goToChat}
|
||||
className="flex-1 px-4 py-2.5 bg-pink-50 dark:bg-pink-900/20 hover:bg-pink-100 dark:hover:bg-pink-900/30 text-pink-500 rounded-xl text-sm font-medium transition-colors border border-pink-200 dark:border-pink-800"
|
||||
>
|
||||
返回聊天
|
||||
</button>
|
||||
<button
|
||||
onClick={logout}
|
||||
className="px-4 py-2.5 bg-red-50 dark:bg-red-900/20 hover:bg-red-100 dark:hover:bg-red-900/30 text-red-400 rounded-xl text-sm font-medium transition-colors border border-red-200 dark:border-red-800"
|
||||
>
|
||||
退出登录
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function DetailRow({ label, value }: { label: string; value: string }) {
|
||||
return (
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-xs text-gray-400">{label}</span>
|
||||
<span className="text-sm text-gray-700 dark:text-gray-300 font-mono">{value}</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -252,6 +252,9 @@ function handleServerMessage(msg: WSServerMessage) {
|
||||
timestamp: msg.timestamp,
|
||||
msgType: (msg.msg_type as MessageDisplayType) || undefined,
|
||||
client_info: msg.client_info,
|
||||
audioUrl: msg.full_audio_url,
|
||||
segments: msg.segments,
|
||||
metadata: msg.tool_calls ? { tool_calls: msg.tool_calls } : undefined,
|
||||
});
|
||||
}
|
||||
setTyping(false);
|
||||
@@ -349,8 +352,19 @@ function handleServerMessage(msg: WSServerMessage) {
|
||||
break;
|
||||
|
||||
case 'stream_segments':
|
||||
// 流式片段 — 语音合成辅助数据,不创建新消息气泡
|
||||
// response/multi_message 已负责创建聊天消息
|
||||
// 流式片段 — 更新最后一条助手消息的 segments 和 full_audio_url
|
||||
if (msg.segments || msg.full_audio_url) {
|
||||
const { messages: currentMsgs } = useChatStore.getState();
|
||||
if (currentMsgs.length > 0) {
|
||||
const lastMsg = currentMsgs[currentMsgs.length - 1];
|
||||
if (lastMsg.role === 'assistant') {
|
||||
const updated = { ...lastMsg };
|
||||
if (msg.segments) updated.segments = msg.segments;
|
||||
if (msg.full_audio_url) updated.audioUrl = msg.full_audio_url;
|
||||
useChatStore.getState().updateMessage(lastMsg.id, updated);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'device_update':
|
||||
@@ -430,6 +444,7 @@ function handleServerMessage(msg: WSServerMessage) {
|
||||
timestamp: msg.timestamp || Date.now(),
|
||||
msgType: 'tool_progress',
|
||||
isStreaming: false,
|
||||
metadata: { tool_progress: msg.tool_progress },
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -31,6 +31,7 @@ interface ChatStore {
|
||||
enqueueMessage: (message: Message) => void;
|
||||
/** 当前气泡逐字动画完成后调用:关闭 isStreaming,出队下一个 */
|
||||
onTypewriterDone: (messageId: string) => void;
|
||||
updateMessage: (id: string, updates: Partial<Message>) => void;
|
||||
setMessages: (messages: Message[]) => void;
|
||||
setTyping: (typing: boolean) => void;
|
||||
clearMessages: () => void;
|
||||
@@ -129,6 +130,11 @@ export const useChatStore = create<ChatStore>((set) => ({
|
||||
return { messages: msgs, isTyping: false, messageQueue: queue };
|
||||
}),
|
||||
|
||||
updateMessage: (id, updates) =>
|
||||
set((state) => ({
|
||||
messages: state.messages.map((m) => (m.id === id ? { ...m, ...updates } : m)),
|
||||
})),
|
||||
|
||||
setMessages: (messages) => set({ messages, isTyping: false }),
|
||||
|
||||
setTyping: (typing) => set({ isTyping: typing }),
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
// Admin 相关类型定义
|
||||
|
||||
/** 模型供应商 */
|
||||
export interface ModelProvider {
|
||||
name: string;
|
||||
base_url: string;
|
||||
api_key: string;
|
||||
}
|
||||
|
||||
/** 模型配置 */
|
||||
export interface ModelConfig {
|
||||
id: string;
|
||||
name: string;
|
||||
provider: string;
|
||||
description?: string;
|
||||
priority?: number;
|
||||
tags?: string[];
|
||||
params?: Record<string, unknown>;
|
||||
enabled?: boolean;
|
||||
updated_at?: string;
|
||||
}
|
||||
|
||||
/** 路由规则 */
|
||||
export interface RoutingRule {
|
||||
purpose: string;
|
||||
fallback_chain: string[];
|
||||
required?: boolean;
|
||||
}
|
||||
|
||||
/** 会话状态 */
|
||||
export interface SessionState {
|
||||
session_id: string;
|
||||
user_id: string;
|
||||
title: string;
|
||||
is_main: boolean;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
message_count: number;
|
||||
}
|
||||
|
||||
/** 客户端信息 */
|
||||
export interface ClientInfo {
|
||||
client_id: string;
|
||||
device_name?: string;
|
||||
user_agent?: string;
|
||||
note?: string;
|
||||
last_seen?: string;
|
||||
online?: boolean;
|
||||
}
|
||||
|
||||
/** 健康检查结果 */
|
||||
export interface HealthCheckResult {
|
||||
provider: string;
|
||||
message: string;
|
||||
}
|
||||
|
||||
/** Profile 信息 */
|
||||
export interface ProfileInfo {
|
||||
user_id: string;
|
||||
username: string;
|
||||
nickname?: string;
|
||||
is_admin: boolean;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
/** 模型列表响应 */
|
||||
export interface ModelListResponse {
|
||||
models: ModelConfig[];
|
||||
total: number;
|
||||
}
|
||||
|
||||
/** 供应商列表响应 */
|
||||
export interface ProviderListResponse {
|
||||
providers: ModelProvider[];
|
||||
total: number;
|
||||
}
|
||||
|
||||
/** 会话列表响应 (admin) */
|
||||
export interface AdminSessionsResponse {
|
||||
sessions: SessionState[];
|
||||
total: number;
|
||||
}
|
||||
|
||||
/** 按用户分组的活跃会话 */
|
||||
export interface ActiveSessionsResponse {
|
||||
users: Record<string, SessionState[]>;
|
||||
}
|
||||
|
||||
/** 客户端列表响应 */
|
||||
export interface ClientListResponse {
|
||||
clients: ClientInfo[];
|
||||
total: number;
|
||||
}
|
||||
Reference in New Issue
Block a user