ExecuteAsync 异步执行子 Agent 任务
(ctx context.Context, req *types.SubAgentRequest)
| 308 | |
| 309 | // ExecuteAsync 异步执行子 Agent 任务 |
| 310 | func (m *SubAgentManager) ExecuteAsync(ctx context.Context, req *types.SubAgentRequest) (string, <-chan *types.SubAgentProgressEvent, error) { |
| 311 | // 获取规格 |
| 312 | spec, err := m.GetSpec(req.AgentType) |
| 313 | if err != nil { |
| 314 | return "", nil, err |
| 315 | } |
| 316 | |
| 317 | // 生成任务 ID |
| 318 | taskID := uuid.New().String() |
| 319 | |
| 320 | // 设置超时 |
| 321 | timeout := spec.Timeout |
| 322 | if req.Timeout > 0 { |
| 323 | timeout = req.Timeout |
| 324 | } |
| 325 | if timeout == 0 { |
| 326 | timeout = m.defaultTimeout |
| 327 | } |
| 328 | |
| 329 | execCtx, cancel := context.WithTimeout(context.Background(), timeout) |
| 330 | |
| 331 | // 创建进度通道 |
| 332 | progressChan := make(chan *types.SubAgentProgressEvent, 100) |
| 333 | |
| 334 | // 创建句柄 |
| 335 | handle := &SubAgentHandle{ |
| 336 | ID: taskID, |
| 337 | AgentType: req.AgentType, |
| 338 | Request: req, |
| 339 | StartTime: time.Now(), |
| 340 | Status: "starting", |
| 341 | CancelFunc: cancel, |
| 342 | ProgressChan: progressChan, |
| 343 | ParentAgentID: req.ParentAgentID, |
| 344 | } |
| 345 | m.registerHandle(handle) |
| 346 | |
| 347 | // 异步执行 |
| 348 | go func() { |
| 349 | defer close(progressChan) |
| 350 | defer cancel() |
| 351 | defer m.unregisterHandle(taskID) |
| 352 | |
| 353 | // 发送开始事件 |
| 354 | progressChan <- &types.SubAgentProgressEvent{ |
| 355 | AgentType: req.AgentType, |
| 356 | TaskID: taskID, |
| 357 | Phase: "started", |
| 358 | Progress: 0, |
| 359 | Message: "Subagent starting...", |
| 360 | } |
| 361 | |
| 362 | // 创建子 Agent |
| 363 | agentConfig := m.buildAgentConfig(spec, req, taskID) |
| 364 | agent, err := Create(execCtx, agentConfig, m.deps) |
| 365 | if err != nil { |
| 366 | handle.Status = "failed" |
| 367 | handle.Result = &types.SubAgentResult{ |
nothing calls this directly
no test coverage detected