/ common / agent / types.go
types.go
  1  // Copyright (c) 2024-2026 Tencent Zhuque Lab. All rights reserved.
  2  //
  3  // Licensed under the Apache License, Version 2.0 (the "License");
  4  // you may not use this file except in compliance with the License.
  5  // You may obtain a copy of the License at
  6  //
  7  //     http://www.apache.org/licenses/LICENSE-2.0
  8  //
  9  // Unless required by applicable law or agreed to in writing, software
 10  // distributed under the License is distributed on an "AS IS" BASIS,
 11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  // See the License for the specific language governing permissions and
 13  // limitations under the License.
 14  //
 15  // Requirement: Any integration or derivative work must explicitly attribute
 16  // Tencent Zhuque Lab (https://github.com/Tencent/AI-Infra-Guard) in its
 17  // documentation or user interface, as detailed in the NOTICE file.
 18  
 19  package agent
 20  
 21  import "encoding/json"
 22  
 23  // Agent消息类型常量
 24  const (
 25  	// Agent -> Server 消息类型
 26  	AgentMsgTypeRegister = "register" // 注册消息
 27  
 28  	AgentMsgTypeResultUpdate = "resultUpdate" // 结果更新
 29  	AgentMsgTypeActionLog    = "actionLog"    // 插件日志
 30  	AgentMsgTypeToolUsed     = "toolUsed"     // 插件工作状态
 31  	AgentMsgTypeNewPlanStep  = "newPlanStep"  // 新建执行步骤
 32  	AgentMsgTypeStatusUpdate = "statusUpdate" // 更新步骤状态
 33  	AgentMsgTypePlanUpdate   = "planUpdate"   // 更新任务计划
 34  	AgentMsgTypeError        = "error"        // 更新任务计划
 35  
 36  	// Server -> Agent 消息类型
 37  	ServerMsgTypeRegisterResp = "register_ack" // 注册响应
 38  	ServerMsgTypeTaskAssign   = "task_assign"  // 任务分配
 39  	ServerMsgTypeTerminate    = "terminate"    // 终止任务
 40  )
 41  
 42  // 任务状态枚举
 43  const (
 44  	TaskStatusPending  = "pending"  // 等待中
 45  	TaskStatusRunning  = "running"  // 执行中
 46  	TaskStatusComplete = "complete" // 完成
 47  	TaskStatusFailed   = "failed"   // 失败
 48  )
 49  
 50  // 插件状态枚举
 51  const (
 52  	ToolStatusDoing = "doing" // 执行中
 53  	ToolStatusDone  = "done"  // 已完成
 54  )
 55  
 56  // Agent状态枚举
 57  const (
 58  	AgentStatusRunning   = "running"   // 执行中
 59  	AgentStatusCompleted = "completed" // 已完成
 60  	AgentStatusFailed    = "failed"    // 失败
 61  	AgentStatusIdle      = "idle"      // 空闲
 62  )
 63  
 64  type statusString string
 65  
 66  // 子任务状态枚举
 67  const (
 68  	SubTaskStatusTodo  statusString = "todo"  // 待办
 69  	SubTaskStatusDoing statusString = "doing" // 执行中
 70  	SubTaskStatusDone  statusString = "done"  // 已完成
 71  )
 72  
 73  // 任务类型
 74  const (
 75  	TaskTypeTestDemo           = "Test-Demo"
 76  	TaskTypeAIInfraScan        = "AI-Infra-Scan"
 77  	TaskTypeMcpScan            = "Mcp-Scan"
 78  	TaskTypeModelRedteamReport = "Model-Redteam-Report"
 79  	TaskTypeModelJailbreak     = "Model-Jailbreak"
 80  	TaskTypeAgentScan          = "Agent-Scan"
 81  )
 82  
 83  type AgentInfo struct {
 84  	ID           string   `json:"agent_id"` // Agent唯一标识
 85  	HostName     string   `json:"hostname"`
 86  	IP           string   `json:"ip"`           // Agent名称
 87  	Version      string   `json:"version"`      // Agent版本
 88  	Capabilities []string `json:"capabilities"` // Agent能力列表
 89  	Metadata     string   `json:"metadata"`     // 额外元数据
 90  }
 91  
 92  // AgentStatus Agent状态信息
 93  type AgentStatus struct {
 94  	Status    string  `json:"status"`     // 当前状态
 95  	Load      float64 `json:"load"`       // 负载指标 (0-1)
 96  	Memory    float64 `json:"memory"`     // 内存使用率
 97  	CPU       float64 `json:"cpu"`        // CPU使用率
 98  	TaskCount int     `json:"task_count"` // 当前任务数量
 99  	Timestamp int64   `json:"timestamp"`  // 时间戳
100  }
101  
102  type RequestData struct {
103  	Type    string      `json:"type"`
104  	Content interface{} `json:"content"`
105  }
106  
107  type ResponseData struct {
108  	Type    string          `json:"type"`
109  	Content json.RawMessage `json:"content"`
110  }
111  
112  type TerminateTaskRequest struct {
113  	SessionID string `json:"session_id"`
114  	Reason    string `json:"reason"`
115  }
116  
117  // Disconnect Agent断开
118  type Disconnect struct {
119  	AgentID string `json:"agent_id"`
120  	Reason  string `json:"reason"` // 断开原因
121  }
122  
123  // TaskRequest 任务请求
124  type TaskRequest struct {
125  	SessionId   string          `json:"sessionId"` // 任务ID
126  	TaskType    string          `json:"taskType"`  // 任务类型
127  	Params      json.RawMessage `json:"params"`    // 任务参数
128  	Timeout     int             `json:"timeout"`   // 超时时间(秒)
129  	Content     string          `json:"content"`
130  	Language    string          `json:"countryIsoCode"`
131  	Attachments []string        `json:"attachments"`
132  }
133  
134  // Event 事件结构
135  type Event struct {
136  	ID        string                 `json:"id"`        // 消息id
137  	Type      string                 `json:"type"`      // 消息类型
138  	Timestamp int64                  `json:"timestamp"` // 时间戳
139  	Result    map[string]interface{} `json:"result"`    // 任务结果
140  }
141  
142  // ResultUpdate 任务结果更新消息(前端格式)
143  type ResultUpdate struct {
144  	ID        string `json:"id"`        // 消息id
145  	Type      string `json:"type"`      // 消息类型,固定为"event"
146  	SessionId string `json:"sessionId"` // 任务的id
147  	Timestamp int64  `json:"timestamp"` // 时间戳
148  	Event     Event  `json:"event"`     // 事件数据
149  }
150  
151  // ==================== 插件日志相关结构 ====================
152  
153  // ActionLogEvent 插件日志事件结构
154  type ActionLogEvent struct {
155  	ID         string `json:"id"`         // 消息id
156  	Type       string `json:"type"`       // 消息类型,固定为"actionLog"
157  	Timestamp  int64  `json:"timestamp"`  // 时间戳
158  	ActionId   string `json:"actionId"`   // 插件id
159  	Tool       string `json:"tool"`       // 插件名
160  	PlanStepId string `json:"planStepId"` // 执行步骤id
161  	ActionLog  string `json:"actionLog"`  // 插件日志
162  }
163  
164  // ActionLogUpdate 插件日志更新消息(前端格式)
165  type ActionLogUpdate struct {
166  	ID        string         `json:"id"`        // 消息id
167  	Type      string         `json:"type"`      // 消息类型,固定为"event"
168  	SessionId string         `json:"sessionId"` // 任务的id
169  	Timestamp int64          `json:"timestamp"` // 时间戳
170  	Event     ActionLogEvent `json:"event"`     // 事件数据
171  }
172  
173  // ActionLogContent Agent发送给服务器的插件日志内容
174  type ActionLogContent struct {
175  	Type    string          `json:"type"`    // 固定为"actionLog"
176  	Content ActionLogUpdate `json:"content"` // 插件日志数据
177  }
178  
179  // ==================== 插件工作状态相关结构 ====================
180  
181  // ToolMessage 插件调用消息
182  type ToolMessage struct {
183  	Action string `json:"action"` // 调用插件的动作
184  	Param  string `json:"param"`  // 调用插件的参数
185  }
186  
187  // Tool 插件信息
188  type Tool struct {
189  	ToolId  string       `json:"toolId"`  // 插件id
190  	Tool    string       `json:"tool"`    // 插件名
191  	Status  statusString `json:"status"`  // 插件执行状态(doing,done)
192  	Brief   string       `json:"brief"`   // 插件简要描述
193  	Message ToolMessage  `json:"message"` // 插件调用描述
194  	Result  string       `json:"result"`  // 插件执行结果
195  }
196  
197  // ToolUsedEvent 插件工作状态事件结构
198  type ToolUsedEvent struct {
199  	ID          string `json:"id"`          // 消息id
200  	Type        string `json:"type"`        // 消息类型,固定为"toolUsed"
201  	Timestamp   int64  `json:"timestamp"`   // 时间戳
202  	Description string `json:"description"` // 描述信息
203  	PlanStepId  string `json:"planStepId"`  // 步骤id
204  	StatusId    string `json:"statusId"`    // 状态id
205  	Tools       []Tool `json:"tools"`       // 插件列表
206  }
207  
208  // ToolUsedUpdate 插件工作状态更新消息(前端格式)
209  type ToolUsedUpdate struct {
210  	ID        string        `json:"id"`        // 消息id
211  	Type      string        `json:"type"`      // 消息类型,固定为"event"
212  	SessionId string        `json:"sessionId"` // 任务的id
213  	Timestamp int64         `json:"timestamp"` // 时间戳
214  	Event     ToolUsedEvent `json:"event"`     // 事件数据
215  }
216  
217  // ToolUsedContent Agent发送给服务器的插件工作状态内容
218  type ToolUsedContent struct {
219  	Type    string         `json:"type"`    // 固定为"toolUsed"
220  	Content ToolUsedUpdate `json:"content"` // 插件工作状态数据
221  }
222  
223  // ==================== 新建执行步骤相关结构 ====================
224  
225  // NewPlanStepEvent 新建执行步骤事件结构
226  type NewPlanStepEvent struct {
227  	ID        string `json:"id"`        // 消息id
228  	Type      string `json:"type"`      // 消息类型,固定为"newPlanStep"
229  	Timestamp int64  `json:"timestamp"` // 时间戳
230  	StepId    string `json:"stepId"`    // 步骤id
231  	Title     string `json:"title"`     // 步骤名称
232  }
233  
234  // NewPlanStepUpdate 新建执行步骤更新消息(前端格式)
235  type NewPlanStepUpdate struct {
236  	ID        string           `json:"id"`        // 消息id
237  	Type      string           `json:"type"`      // 消息类型,固定为"event"
238  	SessionId string           `json:"sessionId"` // 任务的id
239  	Timestamp int64            `json:"timestamp"` // 时间戳
240  	Event     NewPlanStepEvent `json:"event"`     // 事件数据
241  }
242  
243  // NewPlanStepContent Agent发送给服务器的新建执行步骤内容
244  type NewPlanStepContent struct {
245  	Type    string            `json:"type"`    // 固定为"newPlanStep"
246  	Content NewPlanStepUpdate `json:"content"` // 新建执行步骤数据
247  }
248  
249  // ==================== 更新步骤状态相关结构 ====================
250  
251  // StatusUpdateEvent 更新步骤状态事件结构
252  type StatusUpdateEvent struct {
253  	ID          string `json:"id"`          // 消息id
254  	Type        string `json:"type"`        // 消息类型,固定为"statusUpdate"
255  	Timestamp   int64  `json:"timestamp"`   // 时间戳
256  	AgentStatus string `json:"agentStaus"`  // agent执行状态(注意:文档中是agentStaus,保持一致)
257  	Brief       string `json:"brief"`       // 简要描述
258  	Description string `json:"description"` // 状态描述
259  	NoRender    bool   `json:"noRender"`    // 是否不渲染
260  	PlanStepId  string `json:"planStepId"`  // 执行步骤id
261  }
262  
263  // StatusUpdateUpdate 更新步骤状态更新消息(前端格式)
264  type StatusUpdateUpdate struct {
265  	ID        string            `json:"id"`        // 消息id
266  	Type      string            `json:"type"`      // 消息类型,固定为"event"
267  	SessionId string            `json:"sessionId"` // 任务的id
268  	Timestamp int64             `json:"timestamp"` // 时间戳
269  	Event     StatusUpdateEvent `json:"event"`     // 事件数据
270  }
271  
272  // StatusUpdateContent Agent发送给服务器的更新步骤状态内容
273  type StatusUpdateContent struct {
274  	Type    string             `json:"type"`    // 固定为"statusUpdate"
275  	Content StatusUpdateUpdate `json:"content"` // 更新步骤状态数据
276  }
277  
278  // ==================== 更新任务计划相关结构 ====================
279  
280  // SubTask 子任务结构
281  type SubTask struct {
282  	Status    statusString `json:"status"`    // 子任务进度(todo,doing,done)
283  	Title     string       `json:"title"`     // 子任务名称
284  	StartedAt int64        `json:"startedAt"` // 子任务开始时间
285  	StepId    string       `json:"stepId"`    // 子任务id
286  }
287  
288  // PlanUpdateEvent 更新任务计划事件结构
289  type PlanUpdateEvent struct {
290  	ID        string    `json:"id"`        // 消息id
291  	Type      string    `json:"type"`      // 消息类型,固定为"planUpdate"
292  	Timestamp int64     `json:"timestamp"` // 时间戳
293  	Tasks     []SubTask `json:"tasks"`     // 子任务列表
294  }
295  
296  // PlanUpdateUpdate 更新任务计划更新消息(前端格式)
297  type PlanUpdateUpdate struct {
298  	ID        string          `json:"id"`        // 消息id
299  	Type      string          `json:"type"`      // 消息类型,固定为"event"
300  	SessionId string          `json:"sessionId"` // 任务的id
301  	Timestamp int64           `json:"timestamp"` // 时间戳
302  	Event     PlanUpdateEvent `json:"event"`     // 事件数据
303  }
304  
305  // PlanUpdateContent Agent发送给服务器的更新任务计划内容
306  type PlanUpdateContent struct {
307  	Type    string           `json:"type"`    // 固定为"planUpdate"
308  	Content PlanUpdateUpdate `json:"content"` // 更新任务计划数据
309  }
310  
311  // ErrorUpdateContent Agent发送给服务器的错误更新内容
312  type ErrorUpdateContent struct {
313  	Type    string      `json:"type"`    // 固定为"errorUpdate"
314  	Content ErrorUpdate `json:"content"` // 错误信息
315  }
316  
317  type ErrorEvent struct {
318  	Id        string `json:"id"`
319  	Type      string `json:"type"`
320  	Timestamp int64  `json:"timestamp"`
321  	Message   string `json:"message"`
322  }
323  
324  type ErrorUpdate struct {
325  	ID        string     `json:"id" validate:"required"`        // 事件ID - 必需
326  	Type      string     `json:"type" validate:"required"`      // 事件类型 - 必需
327  	SessionID string     `json:"sessionId" validate:"required"` // 会话ID - 必需
328  	Timestamp int64      `json:"timestamp" validate:"required"` // 时间戳 - 必需
329  	Event     ErrorEvent `json:"event"`                         // 事件数据 - 必需
330  }