ExecuteAsync 异步执行任务
(ctx context.Context, agentType, prompt string, opts *TaskExecuteOptions)
| 128 | |
| 129 | // ExecuteAsync 异步执行任务 |
| 130 | func (te *TaskExecutor) ExecuteAsync(ctx context.Context, agentType, prompt string, opts *TaskExecuteOptions) (*TaskExecutionHandle, error) { |
| 131 | te.mu.RLock() |
| 132 | factory := te.executorFactory |
| 133 | te.mu.RUnlock() |
| 134 | |
| 135 | if factory == nil { |
| 136 | return nil, errors.New("executor factory not configured") |
| 137 | } |
| 138 | |
| 139 | // 创建执行器 |
| 140 | executor, err := factory.Create(agentType) |
| 141 | if err != nil { |
| 142 | return nil, fmt.Errorf("failed to create executor: %w", err) |
| 143 | } |
| 144 | |
| 145 | taskID := fmt.Sprintf("task_%d", time.Now().UnixNano()) |
| 146 | execCtx, cancel := context.WithCancel(ctx) |
| 147 | |
| 148 | handle := &TaskExecutionHandle{ |
| 149 | TaskID: taskID, |
| 150 | AgentType: agentType, |
| 151 | Status: "pending", |
| 152 | StartTime: time.Now(), |
| 153 | CancelFunc: cancel, |
| 154 | } |
| 155 | |
| 156 | // 异步执行 |
| 157 | go func() { |
| 158 | handle.Status = "running" |
| 159 | |
| 160 | req := &types.SubAgentRequest{ |
| 161 | AgentType: agentType, |
| 162 | Task: prompt, |
| 163 | Context: opts.Context, |
| 164 | } |
| 165 | |
| 166 | if opts.Timeout > 0 { |
| 167 | req.Timeout = opts.Timeout |
| 168 | } |
| 169 | |
| 170 | result, err := executor.Execute(execCtx, req) |
| 171 | |
| 172 | now := time.Now() |
| 173 | handle.EndTime = &now |
| 174 | |
| 175 | if err != nil { |
| 176 | handle.Status = "failed" |
| 177 | handle.Result = &types.SubAgentResult{ |
| 178 | AgentType: agentType, |
| 179 | Success: false, |
| 180 | Error: err.Error(), |
| 181 | Duration: time.Since(handle.StartTime), |
| 182 | } |
| 183 | return |
| 184 | } |
| 185 | |
| 186 | handle.Result = result |
| 187 | if result.Success { |
no test coverage detected