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

Function filterExample

examples/streaming/main.go:109–129  ·  view source on GitHub ↗

filterExample 演示过滤事件

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

Source from the content-addressed store, hash-verified

107
108// filterExample 演示过滤事件
109func filterExample(ctx context.Context, ag *agent.Agent) {
110 // 只处理来自 assistant 的事件
111 reader := agent.StreamFilter(
112 ag.Stream(ctx, "Tell me a joke"),
113 func(event *session.Event) bool {
114 return event.Author == "assistant"
115 },
116 )
117
118 for {
119 event, err := reader.Recv()
120 if err != nil {
121 if errors.Is(err, io.EOF) {
122 break
123 }
124 log.Printf("Error: %v", err)
125 break
126 }
127 fmt.Printf("Assistant says: %s\n", event.Content.Content)
128 }
129}
130
131// lastEventExample 演示获取最后一个事件
132func lastEventExample(ctx context.Context, ag *agent.Agent) {

Callers 1

mainFunction · 0.85

Calls 4

StreamFilterFunction · 0.92
RecvMethod · 0.80
PrintfMethod · 0.80
StreamMethod · 0.65

Tested by

no test coverage detected