-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
118 lines (89 loc) · 2.57 KB
/
main.go
File metadata and controls
118 lines (89 loc) · 2.57 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"context"
"fmt"
"log/slog"
"os"
"time"
codexsdk "github.com/ethpandaops/codex-agent-sdk-go"
)
// displayMessage standardizes message display across examples.
func displayMessage(msg codexsdk.Message) {
switch m := msg.(type) {
case *codexsdk.UserMessage:
for _, block := range m.Content.Blocks() {
if textBlock, ok := block.(*codexsdk.TextBlock); ok {
fmt.Printf("User: %s\n", textBlock.Text)
}
}
case *codexsdk.AssistantMessage:
for _, block := range m.Content {
if textBlock, ok := block.(*codexsdk.TextBlock); ok {
fmt.Printf("Codex: %s\n", textBlock.Text)
}
}
case *codexsdk.SystemMessage:
// Ignore system messages in display
case *codexsdk.ResultMessage:
fmt.Println("Result ended")
if m.Usage != nil {
fmt.Printf("Tokens: %d in / %d out\n", m.Usage.InputTokens, m.Usage.OutputTokens)
}
}
}
// basicExample demonstrates a simple question.
func basicExample() {
fmt.Println("=== Basic Example ===")
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
for msg, err := range codexsdk.Query(ctx, codexsdk.Text("What is 2 + 2?")) {
if err != nil {
fmt.Printf("Query failed: %v\n", err)
return
}
displayMessage(msg)
}
fmt.Println()
}
// withOptionsExample demonstrates using custom options.
func withOptionsExample() {
fmt.Println("=== With Options Example ===")
_ = slog.New(slog.NewTextHandler(os.Stderr, nil))
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
for msg, err := range codexsdk.Query(ctx, codexsdk.Text("Explain what Golang is in one sentence."),
codexsdk.WithSystemPrompt("You are a helpful assistant that explains things simply."),
) {
if err != nil {
fmt.Printf("Query failed: %v\n", err)
return
}
displayMessage(msg)
}
fmt.Println()
}
// withToolsExample demonstrates using allowed tools with cost reporting.
func withToolsExample() {
fmt.Println("=== With Tools Example ===")
_ = slog.New(slog.NewTextHandler(os.Stderr, nil))
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
for msg, err := range codexsdk.Query(ctx, codexsdk.Text("Create a file called hello.txt with 'Hello, World!' in it"),
codexsdk.WithAllowedTools("Read", "Write"),
codexsdk.WithSystemPrompt("You are a helpful file assistant."),
) {
if err != nil {
fmt.Printf("Query failed: %v\n", err)
return
}
displayMessage(msg)
}
fmt.Println()
}
func main() {
fmt.Println("Quick Start Examples")
fmt.Println()
basicExample()
withOptionsExample()
withToolsExample()
}