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

Method Execute

pkg/agent/subagent.go:201–307  ·  view source on GitHub ↗

Execute 执行子 Agent 任务

(ctx context.Context, req *types.SubAgentRequest)

Source from the content-addressed store, hash-verified

199
200// Execute 执行子 Agent 任务
201func (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)

Callers

nothing calls this directly

Calls 15

GetSpecMethod · 0.95
buildAgentConfigMethod · 0.95
registerHandleMethod · 0.95
unregisterHandleMethod · 0.95
monitorProgressMethod · 0.95
buildTaskMessageMethod · 0.95
cancelFunction · 0.85
CreateFunction · 0.85
InfoMethod · 0.65
CloseMethod · 0.65
SubscribeMethod · 0.65
UnsubscribeMethod · 0.65

Tested by

no test coverage detected