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

Method StartAsync

pkg/tools/long_running.go:98–160  ·  view source on GitHub ↗

StartAsync 异步启动工具

(
	ctx context.Context,
	tool Tool,
	args map[string]any,
)

Source from the content-addressed store, hash-verified

96
97// StartAsync 异步启动工具
98func (e *LongRunningExecutor) StartAsync(
99 ctx context.Context,
100 tool Tool,
101 args map[string]any,
102) (string, error) {
103 // 1. 生成任务 ID
104 taskID := generateTaskID()
105
106 // 2. 创建任务状态
107 status := &TaskStatus{
108 TaskID: taskID,
109 State: TaskStatePending,
110 Progress: 0.0,
111 StartTime: time.Now(),
112 Metadata: make(map[string]any),
113 }
114 e.tasks.Store(taskID, status)
115
116 // 3. 创建可取消的 context
117 taskCtx, cancel := context.WithCancel(ctx)
118 e.cancels.Store(taskID, cancel)
119
120 // 4. 异步执行
121 go func() {
122 defer cancel()
123
124 // 更新状态为 Running
125 _ = e.updateState(taskID, TaskStateRunning)
126
127 // 执行工具(传递 nil ToolContext,因为 long-running 工具不需要它)
128 result, err := tool.Execute(taskCtx, args, nil)
129
130 // 更新最终状态
131 now := time.Now()
132 if err != nil {
133 if taskCtx.Err() == context.Canceled {
134 _ = e.updateStatus(taskID, func(s *TaskStatus) {
135 s.State = TaskStateCancelled
136 s.Error = errors.New("task canceled")
137 s.EndTime = &now
138 })
139 } else {
140 _ = e.updateStatus(taskID, func(s *TaskStatus) {
141 s.State = TaskStateFailed
142 s.Error = err
143 s.EndTime = &now
144 })
145 }
146 } else {
147 _ = e.updateStatus(taskID, func(s *TaskStatus) {
148 s.State = TaskStateCompleted
149 s.Progress = 1.0
150 s.Result = result
151 s.EndTime = &now
152 })
153 }
154
155 // 清理取消函数

Callers

nothing calls this directly

Calls 6

updateStateMethod · 0.95
updateStatusMethod · 0.95
cancelFunction · 0.85
generateTaskIDFunction · 0.70
ExecuteMethod · 0.65
DeleteMethod · 0.65

Tested by

no test coverage detected