feat(ai): 阶段 2 Ask|Agent 共用 aitools Runtime 真查库

- 侧栏 Ask|Agent 分段;Ask 仍走 /api/ai/chat,Agent 走 /api/ai/agent
- RunAgent 多轮 tool call → 进程内 aitools(与 MCP 同源只读/白名单/审计)
- 回复下步骤时间线;openai-chat tools(DeepSeek);agent-accept 冒烟
This commit is contained in:
logan committed 2026-10-15 11:51:39 +08:00
1 parent b8134c4503
commit cd319ce7bd
14 files changed
+1270 -182

No files matched your search

+63
View File
@@ -0,0 +1,63 @@
// opsxdb-agent-accept:阶段 2 冒烟 — 真 LLM(~/.opsxdb/ai.json)+ 真库 aitools。
// 用法:cd server && go run ./cmd/opsxdb-agent-accept/
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
"time"
"opsxdb/server/internal/ai"
"opsxdb/server/internal/aitools"
)
func main() {
llm, err := ai.DefaultToolChat()
if err != nil {
fmt.Println("SKIP: no ToolChat LLM configured:", err)
fmt.Println("提示:Ask|Agent 单测已覆盖 tool loop;配置 DeepSeek 后重跑本命令做真库冒烟。")
os.Exit(0)
}
rt, err := aitools.DefaultRuntime()
if err != nil {
fmt.Println("FAIL: runtime", err)
os.Exit(1)
}
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
defer cancel()
req := ai.AgentReq{
Prompt: "列出 demo 库有哪些表,再用 SELECT 查 users 表最多 3 行,简要中文回答。",
ConnectionID: "accept-mysql-demo",
Driver: "mysql",
Database: "demo",
MaxRounds: 8,
}
fmt.Println("RUN Agent:", req.Prompt)
res, err := ai.RunAgent(ctx, llm, rt, req)
b, _ := json.MarshalIndent(res, "", " ")
fmt.Println(string(b))
if err != nil {
fmt.Println("FAIL:", err)
os.Exit(1)
}
okTool := false
for _, s := range res.Steps {
fmt.Printf("STEP ok=%v tool=%s summary=%s ms=%d\n", s.OK, s.Tool, s.Summary, s.Ms)
if s.OK && (s.Tool == aitools.ToolListTables || s.Tool == aitools.ToolExecuteQuery || s.Tool == aitools.ToolGetSchemaContext) {
okTool = true
}
}
if !okTool {
fmt.Println("FAIL: Agent 未成功调用 list_tables / execute_query / get_schema_context")
os.Exit(1)
}
if strings.TrimSpace(res.Content) == "" {
fmt.Println("FAIL: empty content")
os.Exit(1)
}
fmt.Println("OK: agent smoke passed, rounds=", res.Rounds)
}