后端引擎 backend/pkg/agentcli/(纯引擎库,零业务依赖,剥离 CLI/TUI 外壳): - llm: OpenAI 兼容客户端,复用 model.LLMConfig - tools: ssh_execute(复用 pkg/remoteexec)+ analyze_output + 风险评估黑名单 - safety: 三级安全模式 + 五级风险 + WebSocket 异步确认 - prompt: 五层 System Prompt - core: ReAct 16 轮循环 + 自适应检查点 + 循环检测 - evolver: 四层记忆 + 进化循环,data/agentcli/ 文件存储 集成层: - controller/agent_cli.go: WebSocket 控制器(事件流推流 + 高危确认 + 会话持久化) - model/agent_cli.go: agent_cli_sessions/messages 表 + AutoMigrate - 路由 /ws/agent-cli + /api/v1/aiops/agent-cli/sessions - seed 权限 aiops:agent-cli / aiops:llm-config 前端: - AgentCLI.tsx: 对话流 + 工具块 + 高危确认 + 四模块 tips 面板 - LLMConfig.tsx: 大模型设置独立页 - api/agentCli.ts + Sidebar/App 菜单路由 文档: - docs/agent/ 11 篇(opsxcli 同名文档适配改写 + 00-README 映射总览) 其他: - CLAUDE.md 补充 agent-cli 模块说明 - 修正 .gitignore 根级二进制规则(前导 /,避免误伤 frontend/src/components/aiops/) Co-Authored-By: Claude <noreply@anthropic.com>
50 lines
2.8 KiB
Go
50 lines
2.8 KiB
Go
// agent_cli.go - Agent CLI 会话与消息持久化模型 // Logan.Li
|
|
// 记录 AI 运维 Agent 的每次任务(关联主机 + 用户)及其 ReAct 推理过程
|
|
// (思考/工具调用/输出/结果),供前端历史回放与审计。
|
|
package model
|
|
|
|
import "time"
|
|
|
|
// AgentCLISession Agent CLI 任务会话 // Logan.Li
|
|
type AgentCLISession struct {
|
|
ID uint `gorm:"primarykey" json:"id"`
|
|
UserID uint `gorm:"index" json:"user_id"` // 发起用户
|
|
ServerID uint `gorm:"index" json:"server_id"` // 目标主机
|
|
ServerName string `gorm:"size:128" json:"server_name"` // 主机名快照
|
|
ServerHost string `gorm:"size:64" json:"server_host"` // 主机 IP 快照
|
|
Title string `gorm:"size:255" json:"title"` // 任务标题(query 截断)
|
|
Status string `gorm:"size:32;default:running" json:"status"` // running/done/aborted/error
|
|
Provider string `gorm:"size:50" json:"provider"` // LLM 提供商
|
|
Model string `gorm:"size:100" json:"model"` // 模型名
|
|
Rounds int `json:"rounds"` // ReAct 轮次
|
|
TotalTokens int `json:"total_tokens"` // token 消耗
|
|
Duration int64 `json:"duration"` // 耗时(毫秒)
|
|
Error string `gorm:"type:text" json:"error,omitempty"` // 错误信息
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
// TableName 表名 // Logan.Li
|
|
func (AgentCLISession) TableName() string { return "agent_cli_sessions" }
|
|
|
|
// AgentCLIMessage Agent CLI 任务消息(ReAct 推理过程的每一步) // Logan.Li
|
|
type AgentCLIMessage struct {
|
|
ID uint `gorm:"primarykey" json:"id"`
|
|
SessionID uint `gorm:"index;not null" json:"session_id"`
|
|
Round int `json:"round"` // ReAct 轮次
|
|
Type string `gorm:"size:32" json:"type"` // thought/tool_call/tool_result/done/error/session
|
|
Role string `gorm:"size:32" json:"role"` // user/assistant/tool
|
|
Content string `gorm:"type:text" json:"content"` // 思考文本/最终答案
|
|
ToolName string `gorm:"size:64" json:"tool_name"` // 工具名
|
|
ToolArgs string `gorm:"type:text" json:"tool_args"` // 工具参数 JSON
|
|
ToolOutput string `gorm:"type:text" json:"tool_output"` // 工具输出
|
|
Risk string `gorm:"size:32" json:"risk"` // 风险等级
|
|
Success bool `json:"success"`
|
|
Duration string `gorm:"size:32" json:"duration"` // 工具耗时
|
|
Tokens int `json:"tokens"`
|
|
CreatedAt time.Time `gorm:"index" json:"created_at"`
|
|
}
|
|
|
|
// TableName 表名 // Logan.Li
|
|
func (AgentCLIMessage) TableName() string { return "agent_cli_messages" }
|