-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
73 lines (59 loc) · 1.56 KB
/
main.go
File metadata and controls
73 lines (59 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"context"
"fmt"
"os"
"time"
codexsdk "github.com/ethpandaops/codex-agent-sdk-go"
)
func main() {
fmt.Println("=== Include Partial Messages Example ===")
fmt.Println("Streaming deltas will appear as they arrive, followed by the complete message.")
fmt.Println()
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
for msg, err := range codexsdk.Query(ctx, codexsdk.Text("Say 'Hello, streaming world!' one word at a time."),
codexsdk.WithIncludePartialMessages(true),
codexsdk.WithPermissionMode("bypassPermissions"),
) {
if err != nil {
fmt.Fprintf(os.Stderr, "Query failed: %v\n", err)
os.Exit(1)
}
switch m := msg.(type) {
case *codexsdk.StreamEvent:
event := m.Event
eventType, _ := event["type"].(string)
if eventType != "content_block_delta" {
continue
}
delta, ok := event["delta"].(map[string]any)
if !ok {
continue
}
switch delta["type"] {
case "text_delta":
text, _ := delta["text"].(string)
fmt.Print(text)
case "thinking_delta":
thinking, _ := delta["thinking"].(string)
fmt.Printf("[thinking] %s", thinking)
}
case *codexsdk.AssistantMessage:
fmt.Println()
fmt.Println()
fmt.Print("Complete: ")
for _, block := range m.Content {
if textBlock, ok := block.(*codexsdk.TextBlock); ok {
fmt.Print(textBlock.Text)
}
}
fmt.Println()
case *codexsdk.ResultMessage:
fmt.Println()
if m.Usage != nil {
fmt.Printf("Tokens: %d in / %d out\n", m.Usage.InputTokens, m.Usage.OutputTokens)
}
}
}
}