-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathmain.go
More file actions
181 lines (164 loc) · 5 KB
/
main.go
File metadata and controls
181 lines (164 loc) · 5 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Example 7: Custom Tools
//
// Shows how to define and use custom tools alongside built-in tools.
//
// Run: go run ./examples/07-custom-tools/
package main
import (
"context"
"encoding/json"
"fmt"
"math"
"os"
"strconv"
"strings"
"github.com/codeany-ai/open-agent-sdk-go/agent"
"github.com/codeany-ai/open-agent-sdk-go/types"
)
// weatherTool is a custom tool that returns weather data.
type weatherTool struct{}
func (t *weatherTool) Name() string { return "GetWeather" }
func (t *weatherTool) Description() string {
return "Get current weather for a city. Returns temperature and conditions."
}
func (t *weatherTool) InputSchema() types.ToolInputSchema {
return types.ToolInputSchema{
Type: "object",
Properties: map[string]interface{}{
"city": map[string]interface{}{
"type": "string",
"description": `City name (e.g., "Tokyo", "London")`,
},
},
Required: []string{"city"},
}
}
func (t *weatherTool) IsConcurrencySafe(input map[string]interface{}) bool { return true }
func (t *weatherTool) IsReadOnly(input map[string]interface{}) bool { return true }
func (t *weatherTool) Call(ctx context.Context, input map[string]interface{}, tCtx *types.ToolUseContext) (*types.ToolResult, error) {
city, _ := input["city"].(string)
temps := map[string]int{
"tokyo": 22, "london": 14, "beijing": 25, "new york": 18, "paris": 16,
}
temp, ok := temps[strings.ToLower(city)]
if !ok {
temp = 20
}
result := fmt.Sprintf("Weather in %s: %d°C, partly cloudy", city, temp)
return &types.ToolResult{
Content: []types.ContentBlock{{Type: types.ContentBlockText, Text: result}},
}, nil
}
// calculatorTool is a custom tool that evaluates simple math expressions.
type calculatorTool struct{}
func (t *calculatorTool) Name() string { return "Calculator" }
func (t *calculatorTool) Description() string {
return "Evaluate a simple mathematical expression with two numbers and an operator (+, -, *, /, **)."
}
func (t *calculatorTool) InputSchema() types.ToolInputSchema {
return types.ToolInputSchema{
Type: "object",
Properties: map[string]interface{}{
"expression": map[string]interface{}{
"type": "string",
"description": `Math expression (e.g., "2 ** 10", "42 * 17")`,
},
},
Required: []string{"expression"},
}
}
func (t *calculatorTool) IsConcurrencySafe(input map[string]interface{}) bool { return true }
func (t *calculatorTool) IsReadOnly(input map[string]interface{}) bool { return true }
func (t *calculatorTool) Call(ctx context.Context, input map[string]interface{}, tCtx *types.ToolUseContext) (*types.ToolResult, error) {
expr, _ := input["expression"].(string)
result, err := evalSimpleExpr(expr)
var text string
if err != nil {
text = fmt.Sprintf("Error: %v", err)
} else {
text = fmt.Sprintf("%s = %s", expr, result)
}
return &types.ToolResult{
Content: []types.ContentBlock{{Type: types.ContentBlockText, Text: text}},
}, nil
}
// evalSimpleExpr evaluates expressions like "2 ** 10", "42 * 17 + 3"
func evalSimpleExpr(expr string) (string, error) {
// Simple two-operand evaluation
for _, op := range []string{"**", "*", "/", "+", "-"} {
if idx := strings.Index(expr, op); idx > 0 {
left := strings.TrimSpace(expr[:idx])
right := strings.TrimSpace(expr[idx+len(op):])
a, err1 := strconv.ParseFloat(left, 64)
b, err2 := strconv.ParseFloat(right, 64)
if err1 != nil || err2 != nil {
continue
}
var result float64
switch op {
case "**":
result = math.Pow(a, b)
case "*":
result = a * b
case "/":
if b == 0 {
return "", fmt.Errorf("division by zero")
}
result = a / b
case "+":
result = a + b
case "-":
result = a - b
}
if result == float64(int64(result)) {
return fmt.Sprintf("%d", int64(result)), nil
}
return fmt.Sprintf("%.4f", result), nil
}
}
return "", fmt.Errorf("cannot evaluate: %s", expr)
}
func main() {
fmt.Println("--- Example 7: Custom Tools ---")
model := os.Getenv("CODEANY_MODEL")
if model == "" {
model = "sonnet-4-6"
}
a := agent.New(agent.Options{
Model: model,
MaxTurns: 10,
CustomTools: []types.Tool{
&weatherTool{},
&calculatorTool{},
},
})
defer a.Close()
ctx := context.Background()
a.Init(ctx)
fmt.Printf("Loaded tools with 2 custom tools (GetWeather, Calculator)\n\n")
events, errs := a.Query(ctx,
"What is the weather in Tokyo and London? Also calculate 2 ** 10 * 3. Be brief.",
)
for event := range events {
if event.Type == types.MessageTypeAssistant && event.Message != nil {
for _, block := range event.Message.Content {
switch block.Type {
case types.ContentBlockToolUse:
inputJSON, _ := json.Marshal(block.Input)
fmt.Printf("[%s] %s\n", block.Name, string(inputJSON))
case types.ContentBlockText:
if strings.TrimSpace(block.Text) != "" {
fmt.Printf("\n%s\n", block.Text)
}
}
}
}
if event.Type == types.MessageTypeResult {
fmt.Println("\n--- Done ---")
}
}
if err := <-errs; err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}