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

Method Execute

pkg/tools/builtin/task_executor.go:61–127  ·  view source on GitHub ↗

Execute 执行任务(同步)

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

Source from the content-addressed store, hash-verified

59
60// Execute 执行任务(同步)
61func (te *TaskExecutor) Execute(ctx context.Context, agentType, prompt string, opts *TaskExecuteOptions) (*TaskExecution, error) {
62 te.mu.RLock()
63 factory := te.executorFactory
64 te.mu.RUnlock()
65
66 if factory == nil {
67 return nil, errors.New("executor factory not configured, subagent execution not available")
68 }
69
70 // 创建执行器
71 executor, err := factory.Create(agentType)
72 if err != nil {
73 return nil, fmt.Errorf("failed to create executor for %s: %w", agentType, err)
74 }
75
76 // 构建请求
77 req := &types.SubAgentRequest{
78 AgentType: agentType,
79 Task: prompt,
80 Context: opts.Context,
81 }
82
83 if opts.Timeout > 0 {
84 req.Timeout = opts.Timeout
85 }
86
87 // 执行
88 startTime := time.Now()
89 result, err := executor.Execute(ctx, req)
90
91 execution := &TaskExecution{
92 TaskID: fmt.Sprintf("task_%d", startTime.UnixNano()),
93 Subagent: agentType,
94 Model: opts.Model,
95 Status: "completed",
96 StartTime: startTime,
97 Duration: time.Since(startTime),
98 }
99
100 if err != nil {
101 execution.Status = "failed"
102 execution.Error = err.Error()
103 return execution, nil
104 }
105
106 if result != nil {
107 execution.Result = result.Output
108 if result.Success {
109 execution.Status = "completed"
110 } else {
111 execution.Status = "failed"
112 execution.Error = result.Error
113 }
114 execution.Metadata = map[string]any{
115 "tokens_used": result.TokensUsed,
116 "step_count": result.StepCount,
117 "artifacts": result.Artifacts,
118 }

Callers

nothing calls this directly

Calls 3

CreateMethod · 0.65
ExecuteMethod · 0.65
ErrorMethod · 0.65

Tested by

no test coverage detected