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

Method ExecuteStream

pkg/workflow/router.go:196–392  ·  view source on GitHub ↗

ExecuteStream 流式执行 Router - 支持实时事件流

(ctx context.Context, input *StepInput, streamEvents bool)

Source from the content-addressed store, hash-verified

194
195// ExecuteStream 流式执行 Router - 支持实时事件流
196func (r *Router) ExecuteStream(ctx context.Context, input *StepInput, streamEvents bool) *stream.Reader[any] {
197 reader, writer := stream.Pipe[any](10)
198
199 go func() {
200 defer writer.Close()
201 startTime := time.Now()
202 routerID := r.id
203
204 // 调用选择器函数
205 stepsToExecute := r.selector(input)
206
207 // 发送 Router 开始事件
208 if streamEvents {
209 startEvent := &RouterEvent{
210 Type: RouterEventStarted,
211 RouterName: r.name,
212 SelectedSteps: getStepNames(stepsToExecute),
213 ExecutedSteps: 0,
214 Timestamp: startTime,
215 }
216 if writer.Send(startEvent, nil) {
217 return
218 }
219 }
220
221 if len(stepsToExecute) == 0 {
222 // 没有选中任何步骤
223 if streamEvents {
224 completeEvent := &RouterEvent{
225 Type: RouterEventCompleted,
226 RouterName: r.name,
227 SelectedSteps: []string{},
228 ExecutedSteps: 0,
229 Timestamp: time.Now(),
230 }
231 writer.Send(completeEvent, nil)
232 }
233
234 output := &StepOutput{
235 StepID: routerID,
236 StepName: r.name,
237 StepType: StepTypeRouter,
238 Content: fmt.Sprintf("Router %s: no steps selected", r.name),
239 StartTime: startTime,
240 EndTime: time.Now(),
241 NestedSteps: []*StepOutput{},
242 Metadata: map[string]any{
243 "selected_steps": 0,
244 },
245 Metrics: &StepMetrics{ExecutionTime: time.Since(startTime).Seconds()},
246 }
247 output.Duration = output.EndTime.Sub(output.StartTime).Seconds()
248 writer.Send(output, nil)
249 return
250 }
251
252 // 收集所有执行结果
253 allResults := make([]*StepOutput, 0, len(stepsToExecute))

Callers

nothing calls this directly

Implementers 1

StaticRouterpkg/router/router.go

Calls 7

getStepNamesFunction · 0.85
RecvMethod · 0.80
CloseMethod · 0.65
ExecuteMethod · 0.65
NameMethod · 0.65
ErrorMethod · 0.65
SendMethod · 0.45

Tested by

no test coverage detected