Execute 执行子 Agent 任务
(ctx context.Context, req *types.SubAgentRequest)
| 199 | |
| 200 | // Execute 执行子 Agent 任务 |
| 201 | func (m *SubAgentManager) Execute(ctx context.Context, req *types.SubAgentRequest) (*types.SubAgentResult, error) { |
| 202 | // 获取规格 |
| 203 | spec, err := m.GetSpec(req.AgentType) |
| 204 | if err != nil { |
| 205 | return nil, err |
| 206 | } |
| 207 | |
| 208 | // 生成任务 ID |
| 209 | taskID := uuid.New().String() |
| 210 | |
| 211 | // 设置超时 |
| 212 | timeout := spec.Timeout |
| 213 | if req.Timeout > 0 { |
| 214 | timeout = req.Timeout |
| 215 | } |
| 216 | if timeout == 0 { |
| 217 | timeout = m.defaultTimeout |
| 218 | } |
| 219 | |
| 220 | ctx, cancel := context.WithTimeout(ctx, timeout) |
| 221 | defer cancel() |
| 222 | |
| 223 | subagentLog.Info(ctx, "starting subagent", map[string]any{ |
| 224 | "task_id": taskID, |
| 225 | "agent_type": req.AgentType, |
| 226 | "timeout": timeout.String(), |
| 227 | }) |
| 228 | |
| 229 | startTime := time.Now() |
| 230 | |
| 231 | // 创建子 Agent 配置 |
| 232 | agentConfig := m.buildAgentConfig(spec, req, taskID) |
| 233 | |
| 234 | // 创建子 Agent |
| 235 | agent, err := Create(ctx, agentConfig, m.deps) |
| 236 | if err != nil { |
| 237 | return &types.SubAgentResult{ |
| 238 | AgentType: req.AgentType, |
| 239 | Success: false, |
| 240 | Error: fmt.Sprintf("failed to create subagent: %v", err), |
| 241 | Duration: time.Since(startTime), |
| 242 | }, nil |
| 243 | } |
| 244 | defer func() { _ = agent.Close() }() // Best effort cleanup |
| 245 | |
| 246 | // 注册运行中的子 Agent |
| 247 | handle := &SubAgentHandle{ |
| 248 | ID: taskID, |
| 249 | AgentType: req.AgentType, |
| 250 | Agent: agent, |
| 251 | Request: req, |
| 252 | StartTime: startTime, |
| 253 | Status: "running", |
| 254 | CancelFunc: cancel, |
| 255 | ProgressChan: make(chan *types.SubAgentProgressEvent, 100), |
| 256 | ParentAgentID: req.ParentAgentID, |
| 257 | } |
| 258 | m.registerHandle(handle) |
nothing calls this directly
no test coverage detected