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

Function streamingExample

examples/streaming/main.go:66–92  ·  view source on GitHub ↗

streamingExample 演示流式处理事件

(ctx context.Context, ag *agent.Agent)

Source from the content-addressed store, hash-verified

64
65// streamingExample 演示流式处理事件
66func streamingExample(ctx context.Context, ag *agent.Agent) {
67 // 使用 Recv 循环迭代事件流
68 reader := ag.Stream(ctx, "What is 2+2?")
69 for {
70 event, err := reader.Recv()
71 if err != nil {
72 if errors.Is(err, io.EOF) {
73 break
74 }
75 log.Printf("Error: %v", err)
76 break
77 }
78
79 // 实时处理每个事件
80 fmt.Printf("[Event %s] Author: %s, Content: %s\n",
81 event.ID[:8],
82 event.Author,
83 truncateContent(event.Content.Content, 50),
84 )
85
86 // 可以根据条件中断流
87 if event.Actions.Escalate {
88 fmt.Println("Escalation detected, stopping stream")
89 break
90 }
91 }
92}
93
94// collectExample 演示收集所有事件
95func collectExample(ctx context.Context, ag *agent.Agent) {

Callers 1

mainFunction · 0.85

Calls 4

truncateContentFunction · 0.85
RecvMethod · 0.80
PrintfMethod · 0.80
StreamMethod · 0.65

Tested by

no test coverage detected