streamingExample 演示流式处理事件
(ctx context.Context, ag *agent.Agent)
| 64 | |
| 65 | // streamingExample 演示流式处理事件 |
| 66 | func 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 演示收集所有事件 |
| 95 | func collectExample(ctx context.Context, ag *agent.Agent) { |
no test coverage detected