173 lines
12 KiB
Go
173 lines
12 KiB
Go
// ai_runtime.go - AI 运行、工具能力和执行审计统一模型 // Logan.Li
|
|
package model
|
|
|
|
import "time"
|
|
|
|
// AIRun 统一记录 Agent CLI、告警分析、智能助手、MCP/技能调用发起的 AI 任务。 // Logan.Li
|
|
type AIRun struct {
|
|
ID uint `gorm:"primarykey" json:"id"`
|
|
RunID string `gorm:"size:64;uniqueIndex;not null" json:"run_id"` // 对外 trace/run 标识 // Logan.Li
|
|
ParentRunID string `gorm:"size:64;index" json:"parent_run_id"` // 子任务关联 // Logan.Li
|
|
UserID uint `gorm:"index" json:"user_id"` // 发起用户 // Logan.Li
|
|
Feature string `gorm:"size:64;index;not null" json:"feature"` // agent_cli/alert_analysis/assistant/mcp // Logan.Li
|
|
Mode string `gorm:"size:32;index" json:"mode"` // host/kubernetes/aiops/general // Logan.Li
|
|
Title string `gorm:"size:255" json:"title"` // 任务标题 // Logan.Li
|
|
Status string `gorm:"size:32;index;default:running" json:"status"` // running/done/error/aborted // Logan.Li
|
|
SubjectType string `gorm:"size:64;index" json:"subject_type"` // host/k8s_cluster/namespace/pod/aiops_module/mcp_server/skill // Logan.Li
|
|
SubjectID string `gorm:"size:128;index" json:"subject_id"` // 资源 ID 或稳定键 // Logan.Li
|
|
SubjectName string `gorm:"size:255" json:"subject_name"` // 资源快照名称 // Logan.Li
|
|
SubjectScope string `gorm:"size:255;index" json:"subject_scope"` // prod/ns/app 等范围键 // Logan.Li
|
|
Provider string `gorm:"size:50" json:"provider"` // LLM 提供商 // Logan.Li
|
|
Model string `gorm:"size:100" json:"model"` // 模型名 // Logan.Li
|
|
SafetyMode string `gorm:"size:32" json:"safety_mode"` // strict/balanced/permissive // Logan.Li
|
|
ContextPackID string `gorm:"size:64;index" json:"context_pack_id"` // 上下文包引用 // Logan.Li
|
|
PromptTokens int `json:"prompt_tokens"` // 输入 token // Logan.Li
|
|
CompletionTokens int `json:"completion_tokens"` // 输出 token // Logan.Li
|
|
TotalTokens int `json:"total_tokens"` // 总 token // Logan.Li
|
|
Rounds int `json:"rounds"` // Agent 轮次 // Logan.Li
|
|
Duration int64 `json:"duration"` // 毫秒 // Logan.Li
|
|
Error string `gorm:"type:text" json:"error,omitempty"` // 错误摘要 // Logan.Li
|
|
StartedAt time.Time `gorm:"index" json:"started_at"`
|
|
FinishedAt *time.Time `json:"finished_at,omitempty"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
func (AIRun) TableName() string { return "ai_runs" }
|
|
|
|
// AIRunMessage 统一 AI 对话消息,负责回放,不承载工具审计明细。 // Logan.Li
|
|
type AIRunMessage struct {
|
|
ID uint `gorm:"primarykey" json:"id"`
|
|
RunID string `gorm:"size:64;index;not null" json:"run_id"`
|
|
Round int `gorm:"index" json:"round"`
|
|
Seq int `gorm:"index" json:"seq"`
|
|
Role string `gorm:"size:32;index" json:"role"` // system/user/assistant/tool // Logan.Li
|
|
Type string `gorm:"size:32;index" json:"type"` // message/thought/tool_call/tool_result/done/error/status // Logan.Li
|
|
Content string `gorm:"type:text" json:"content"` // 脱敏内容 // Logan.Li
|
|
ToolName string `gorm:"size:128;index" json:"tool_name"` // 可选工具名 // Logan.Li
|
|
ToolCallID string `gorm:"size:128;index" json:"tool_call_id"` // LLM 工具调用 ID // Logan.Li
|
|
SpanID string `gorm:"size:64;index" json:"span_id"` // 关联 span // Logan.Li
|
|
Tokens int `json:"tokens"`
|
|
CreatedAt time.Time `gorm:"index" json:"created_at"`
|
|
}
|
|
|
|
func (AIRunMessage) TableName() string { return "ai_run_messages" }
|
|
|
|
// AIRunSpan 统一 AI trace span,记录 LLM、工具、guardrail、确认、审批等事件。 // Logan.Li
|
|
type AIRunSpan struct {
|
|
ID uint `gorm:"primarykey" json:"id"`
|
|
RunID string `gorm:"size:64;index;not null" json:"run_id"`
|
|
SpanID string `gorm:"size:64;uniqueIndex;not null" json:"span_id"`
|
|
ParentSpanID string `gorm:"size:64;index" json:"parent_span_id"`
|
|
Kind string `gorm:"size:32;index;not null" json:"kind"` // llm/tool/guardrail/confirm/approval/context // Logan.Li
|
|
Name string `gorm:"size:128;index" json:"name"` // 工具名或步骤名 // Logan.Li
|
|
CapabilityID uint `gorm:"index" json:"capability_id"` // 能力注册表 ID // Logan.Li
|
|
ToolSource string `gorm:"size:32;index" json:"tool_source"` // builtin/mcp/skill/shell/kubernetes/aiops // Logan.Li
|
|
Input string `gorm:"type:text" json:"input"` // 脱敏输入 // Logan.Li
|
|
Output string `gorm:"type:text" json:"output"` // 脱敏输出摘要 // Logan.Li
|
|
OutputSize int `json:"output_size"` // 脱敏后输出长度 // Logan.Li
|
|
Risk string `gorm:"size:32;index" json:"risk"` // safe/low/medium/high/critical // Logan.Li
|
|
Decision string `gorm:"size:32;index" json:"decision"` // auto/approved/rejected/denied/escalated // Logan.Li
|
|
Status string `gorm:"size:32;index" json:"status"` // running/success/error/skipped // Logan.Li
|
|
Error string `gorm:"type:text" json:"error"` // 错误摘要 // Logan.Li
|
|
PromptTokens int `json:"prompt_tokens"`
|
|
CompletionTokens int `json:"completion_tokens"`
|
|
TotalTokens int `json:"total_tokens"`
|
|
Duration int64 `json:"duration"` // 毫秒 // Logan.Li
|
|
StartedAt time.Time `gorm:"index" json:"started_at"`
|
|
EndedAt *time.Time `json:"ended_at,omitempty"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
}
|
|
|
|
func (AIRunSpan) TableName() string { return "ai_run_spans" }
|
|
|
|
// AICapability 注册 AIOps 内置工具、MCP 工具、Skill、Shell/K8s 工具的统一能力目录。 // Logan.Li
|
|
type AICapability struct {
|
|
ID uint `gorm:"primarykey" json:"id"`
|
|
Name string `gorm:"size:128;uniqueIndex;not null" json:"name"` // 稳定工具名 // Logan.Li
|
|
DisplayName string `gorm:"size:128" json:"display_name"`
|
|
Source string `gorm:"size:32;index;not null" json:"source"` // builtin/mcp/skill/shell/kubernetes/aiops // Logan.Li
|
|
Category string `gorm:"size:64;index" json:"category"` // asset/k8s/monitor/alert/change/security // Logan.Li
|
|
RiskLevel string `gorm:"size:32;index;default:safe" json:"risk_level"`
|
|
Schema string `gorm:"type:text" json:"schema"` // JSON Schema // Logan.Li
|
|
Config string `gorm:"type:text" json:"config"` // MCP server/skill 配置快照 // Logan.Li
|
|
Description string `gorm:"type:text" json:"description"`
|
|
Enabled bool `gorm:"default:true;index" json:"enabled"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
func (AICapability) TableName() string { return "ai_capabilities" }
|
|
|
|
// AICapabilityPolicy 描述工具能力在用户、角色、资源范围上的准入和审批策略。 // Logan.Li
|
|
type AICapabilityPolicy struct {
|
|
ID uint `gorm:"primarykey" json:"id"`
|
|
Name string `gorm:"size:128;not null" json:"name"`
|
|
CapabilityID uint `gorm:"index" json:"capability_id"`
|
|
SubjectType string `gorm:"size:32;index" json:"subject_type"` // user/role/all // Logan.Li
|
|
SubjectID string `gorm:"size:64;index" json:"subject_id"`
|
|
ResourceType string `gorm:"size:64;index" json:"resource_type"` // host/k8s_cluster/namespace/pod/aiops_module/mcp_server/skill // Logan.Li
|
|
ResourcePattern string `gorm:"size:255" json:"resource_pattern"` // 正则或通配符 // Logan.Li
|
|
Effect string `gorm:"size:16;index;default:allow" json:"effect"` // allow/deny // Logan.Li
|
|
RequireConfirm bool `gorm:"default:false" json:"require_confirm"`
|
|
RequireApproval bool `gorm:"default:false" json:"require_approval"`
|
|
MaxRisk string `gorm:"size:32;default:high" json:"max_risk"`
|
|
Enabled bool `gorm:"default:true;index" json:"enabled"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
func (AICapabilityPolicy) TableName() string { return "ai_capability_policies" }
|
|
|
|
// OpsSession 统一人机执行会话,覆盖 SSH/RDP/Pod/kubectl/Agent CLI。 // Logan.Li
|
|
type OpsSession struct {
|
|
ID uint `gorm:"primarykey" json:"id"`
|
|
SessionID string `gorm:"size:64;uniqueIndex;not null" json:"session_id"`
|
|
RunID string `gorm:"size:64;index" json:"run_id"` // AI 发起时关联 ai_runs // Logan.Li
|
|
UserID uint `gorm:"index" json:"user_id"`
|
|
SessionType string `gorm:"size:32;index;not null" json:"session_type"` // ssh/rdp/pod_exec/kubectl/agent_cli // Logan.Li
|
|
ActorType string `gorm:"size:32;index;default:user" json:"actor_type"` // user/ai/system // Logan.Li
|
|
TargetType string `gorm:"size:64;index" json:"target_type"` // host/k8s_cluster/pod // Logan.Li
|
|
TargetID string `gorm:"size:128;index" json:"target_id"`
|
|
TargetName string `gorm:"size:255" json:"target_name"`
|
|
ClientIP string `gorm:"size:64" json:"client_ip"`
|
|
Status string `gorm:"size:32;index" json:"status"` // active/closed/failed // Logan.Li
|
|
RecordRef string `gorm:"size:255" json:"record_ref"` // 录制对象存储或分片引用 // Logan.Li
|
|
RecordSize int `json:"record_size"`
|
|
Duration int64 `json:"duration"` // 秒 // Logan.Li
|
|
Error string `gorm:"type:text" json:"error,omitempty"`
|
|
StartedAt time.Time `gorm:"index" json:"started_at"`
|
|
EndedAt *time.Time `json:"ended_at,omitempty"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
func (OpsSession) TableName() string { return "ai_ops_sessions" }
|
|
|
|
// OpsCommand 统一命令/动作审计,覆盖人执行和 AI 工具执行。 // Logan.Li
|
|
type OpsCommand struct {
|
|
ID uint `gorm:"primarykey" json:"id"`
|
|
CommandID string `gorm:"size:64;uniqueIndex;not null" json:"command_id"`
|
|
SessionID string `gorm:"size:64;index" json:"session_id"`
|
|
RunID string `gorm:"size:64;index" json:"run_id"`
|
|
SpanID string `gorm:"size:64;index" json:"span_id"`
|
|
UserID uint `gorm:"index" json:"user_id"`
|
|
ActorType string `gorm:"size:32;index;default:user" json:"actor_type"` // user/ai/system // Logan.Li
|
|
TargetType string `gorm:"size:64;index" json:"target_type"`
|
|
TargetID string `gorm:"size:128;index" json:"target_id"`
|
|
CommandType string `gorm:"size:32;index" json:"command_type"` // shell/k8s_action/mcp/skill/api // Logan.Li
|
|
Command string `gorm:"type:text" json:"command"` // 脱敏命令或动作参数 // Logan.Li
|
|
Risk string `gorm:"size:32;index" json:"risk"`
|
|
Decision string `gorm:"size:32;index" json:"decision"`
|
|
Status string `gorm:"size:32;index" json:"status"`
|
|
ExitCode int `gorm:"default:-1" json:"exit_code"`
|
|
OutputRef string `gorm:"size:255" json:"output_ref"` // 大输出引用 // Logan.Li
|
|
OutputSize int `json:"output_size"`
|
|
Duration int64 `json:"duration"` // 毫秒 // Logan.Li
|
|
Error string `gorm:"type:text" json:"error"`
|
|
ExecutedAt time.Time `gorm:"index" json:"executed_at"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
}
|
|
|
|
func (OpsCommand) TableName() string { return "ai_ops_commands" }
|