-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtool_schema.go
More file actions
100 lines (84 loc) · 2.71 KB
/
tool_schema.go
File metadata and controls
100 lines (84 loc) · 2.71 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
package main
import (
"encoding/json"
"fmt"
openai "github.com/sashabaranov/go-openai"
)
// ToolMeta represents the function schema structure for structured output
type ToolMeta struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters map[string]any `json:"parameters"`
}
// ParseToolSchema parses JSON string to ToolMeta
func ParseToolSchema(jsonStr string) (*ToolMeta, error) {
if jsonStr == "" {
return nil, nil
}
var meta ToolMeta
if err := json.Unmarshal([]byte(jsonStr), &meta); err != nil {
return nil, fmt.Errorf("failed to parse tool_schema JSON: %w", err)
}
if meta.Name == "" {
return nil, fmt.Errorf("tool_schema must have a 'name' field")
}
return &meta, nil
}
// ToOpenAITool converts ToolMeta to openai.Tool format
func (t *ToolMeta) ToOpenAITool() openai.Tool {
return openai.Tool{
Type: openai.ToolTypeFunction,
Function: &openai.FunctionDefinition{
Name: t.Name,
Description: t.Description,
Parameters: t.Parameters,
},
}
}
// ParseFunctionArguments parses function call arguments JSON string
// and converts it to a map[string]string for GitHub Actions output.
// String values are kept as-is, other types are marshaled back to JSON.
func ParseFunctionArguments(jsonStr string) (map[string]string, error) {
if jsonStr == "" {
return map[string]string{}, nil
}
var args map[string]any
if err := json.Unmarshal([]byte(jsonStr), &args); err != nil {
return nil, fmt.Errorf("failed to parse function arguments: %w", err)
}
output := make(map[string]string)
for key, value := range args {
switch v := value.(type) {
case string:
output[key] = v
default:
jsonBytes, err := json.Marshal(v)
if err != nil {
return nil, fmt.Errorf("failed to marshal value for key '%s': %w", key, err)
}
output[key] = string(jsonBytes)
}
}
return output, nil
}
// ReservedOutputField is the reserved field name for raw LLM response
const ReservedOutputField = "response"
// BuildOutputMap builds the final output map from the raw response and parsed tool arguments.
// It always includes the raw response under the "response" key.
// If tool arguments are provided, each field is added to the output, except for the
// reserved "response" field which will be skipped with a warning.
// Returns the output map and a boolean indicating if the reserved field was skipped.
func BuildOutputMap(rawResponse string, toolArgs map[string]string) (map[string]string, bool) {
output := map[string]string{
ReservedOutputField: rawResponse,
}
reservedFieldSkipped := false
for k, v := range toolArgs {
if k == ReservedOutputField {
reservedFieldSkipped = true
continue
}
output[k] = v
}
return output, reservedFieldSkipped
}