MCPcopy Create free account
hub / github.com/astercloud/aster / ExecuteAsync

Method ExecuteAsync

pkg/tools/builtin/task_executor.go:130–195  ·  view source on GitHub ↗

ExecuteAsync 异步执行任务

(ctx context.Context, agentType, prompt string, opts *TaskExecuteOptions)

Source from the content-addressed store, hash-verified

128
129// ExecuteAsync 异步执行任务
130func (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 {

Callers 1

Calls 3

CreateMethod · 0.65
ExecuteMethod · 0.65
ErrorMethod · 0.65

Tested by

no test coverage detected