forked from giik/genai-agent-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_call.go
More file actions
75 lines (68 loc) · 2.17 KB
/
Copy pathfunction_call.go
File metadata and controls
75 lines (68 loc) · 2.17 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
package main
import (
"errors"
"fmt"
"iter"
"maps"
"slices"
"google.golang.org/genai"
)
var (
expectedArgs = map[string]iter.Seq[string]{
"edit_file": maps.Keys(editFileTool.FunctionDeclarations[0].Parameters.Properties),
"read_file": maps.Keys(readFileTool.FunctionDeclarations[0].Parameters.Properties),
"list_files": maps.Keys(listFilesTool.FunctionDeclarations[0].Parameters.Properties),
}
)
func FunctionCall(call *genai.FunctionCall) *genai.FunctionResponse {
response := make(map[string]any)
var err error
// Check tool and argument names.
name := call.Name
received := maps.Keys(call.Args)
if expected, ok := expectedArgs[name]; !ok {
message := fmt.Sprintf("tool named '%s' is unknown", name)
err = errors.New(message)
response["error"] = message
} else if want, have := slices.Sorted(expected), slices.Sorted(received); !slices.Equal(want, have) {
message := fmt.Sprintf("for tool named '%s' want arguments named %v have arguments named %v",
name, slices.Collect(expected), slices.Collect(received))
err = errors.New(message)
response["error"] = message
} else if call.Name == "read_file" {
content, err := ReadFile(call.Args["path"].(string))
if err == nil {
response["output"] = content
} else {
response["error"] = err.Error()
}
} else if call.Name == "edit_file" {
err = EditFile(call.Args["path"].(string), call.Args["old_string"].(string), call.Args["new_string"].(string))
if err == nil {
response["output"] = "OK"
} else {
response["error"] = err.Error()
}
} else if call.Name == "list_files" {
files, err := ListFiles(call.Args["path"].(string))
if err == nil {
response["output"] = files // json encoding of the files list
} else {
response["error"] = err.Error()
}
} else {
message := fmt.Sprintf("tool '%s' is unknown", call.Name)
err = errors.New(message)
response["error"] = message
}
if _, hasOutput := response["output"]; hasOutput {
greenf("calling '%s' \u2714\n", call.Name)
} else if _, hasError := response["error"]; hasError {
redf("calling '%s' \u2718: %v\n", call.Name, err)
}
return &genai.FunctionResponse{
ID: call.ID,
Name: call.Name,
Response: response,
}
}