File size: 4,868 Bytes
8d3471e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package toolcall

import (
	"reflect"
	"testing"
)

func TestNormalizeParsedToolCallsForSchemasCoercesDeclaredStringFieldsRecursively(t *testing.T) {
	toolsRaw := []any{
		map[string]any{
			"type": "function",
			"function": map[string]any{
				"name": "TaskUpdate",
				"parameters": map[string]any{
					"type": "object",
					"properties": map[string]any{
						"taskId": map[string]any{"type": "string"},
						"payload": map[string]any{
							"type": "object",
							"properties": map[string]any{
								"content": map[string]any{"type": "string"},
								"tags": map[string]any{
									"type":  "array",
									"items": map[string]any{"type": "string"},
								},
								"count": map[string]any{"type": "number"},
							},
						},
					},
				},
			},
		},
	}
	calls := []ParsedToolCall{{
		Name: "TaskUpdate",
		Input: map[string]any{
			"taskId": 1,
			"payload": map[string]any{
				"content": map[string]any{"text": "hello"},
				"tags":    []any{1, true, map[string]any{"k": "v"}},
				"count":   2,
			},
		},
	}}

	got := NormalizeParsedToolCallsForSchemas(calls, toolsRaw)
	if len(got) != 1 {
		t.Fatalf("expected one normalized call, got %#v", got)
	}
	if got[0].Input["taskId"] != "1" {
		t.Fatalf("expected taskId coerced to string, got %#v", got[0].Input["taskId"])
	}
	payload, ok := got[0].Input["payload"].(map[string]any)
	if !ok {
		t.Fatalf("expected payload object, got %#v", got[0].Input["payload"])
	}
	if payload["content"] != `{"text":"hello"}` {
		t.Fatalf("expected nested content coerced to json string, got %#v", payload["content"])
	}
	if payload["count"] != 2 {
		t.Fatalf("expected non-string count unchanged, got %#v", payload["count"])
	}
	tags, ok := payload["tags"].([]any)
	if !ok {
		t.Fatalf("expected tags slice, got %#v", payload["tags"])
	}
	wantTags := []any{"1", "true", `{"k":"v"}`}
	if !reflect.DeepEqual(tags, wantTags) {
		t.Fatalf("unexpected normalized tags: got %#v want %#v", tags, wantTags)
	}
}

func TestNormalizeParsedToolCallsForSchemasSupportsDirectToolSchemaShape(t *testing.T) {
	toolsRaw := []any{
		map[string]any{
			"name": "Write",
			"input_schema": map[string]any{
				"type": "object",
				"properties": map[string]any{
					"content": map[string]any{"type": "string"},
				},
			},
		},
	}
	calls := []ParsedToolCall{{Name: "Write", Input: map[string]any{"content": []any{"a", 1}}}}
	got := NormalizeParsedToolCallsForSchemas(calls, toolsRaw)
	if got[0].Input["content"] != `["a",1]` {
		t.Fatalf("expected direct-schema content coerced to string, got %#v", got[0].Input["content"])
	}
}

func TestNormalizeParsedToolCallsForSchemasLeavesAmbiguousUnionUnchanged(t *testing.T) {
	toolsRaw := []any{
		map[string]any{
			"type": "function",
			"function": map[string]any{
				"name": "TaskUpdate",
				"parameters": map[string]any{
					"type": "object",
					"properties": map[string]any{
						"taskId": map[string]any{"type": []any{"string", "integer"}},
					},
				},
			},
		},
	}
	calls := []ParsedToolCall{{Name: "TaskUpdate", Input: map[string]any{"taskId": 1}}}
	got := NormalizeParsedToolCallsForSchemas(calls, toolsRaw)
	if got[0].Input["taskId"] != 1 {
		t.Fatalf("expected ambiguous union to stay unchanged, got %#v", got[0].Input["taskId"])
	}
}

func TestNormalizeParsedToolCallsForSchemasSupportsCamelCaseInputSchema(t *testing.T) {
	toolsRaw := []any{
		map[string]any{
			"name": "Write",
			"inputSchema": map[string]any{
				"type": "object",
				"properties": map[string]any{
					"content": map[string]any{"type": "string"},
				},
			},
		},
	}
	calls := []ParsedToolCall{{Name: "Write", Input: map[string]any{"content": map[string]any{"message": "hi"}}}}
	got := NormalizeParsedToolCallsForSchemas(calls, toolsRaw)
	if got[0].Input["content"] != `{"message":"hi"}` {
		t.Fatalf("expected camelCase inputSchema content coercion, got %#v", got[0].Input["content"])
	}
}

func TestNormalizeParsedToolCallsForSchemasPreservesArrayWhenSchemaSaysArray(t *testing.T) {
	toolsRaw := []any{
		map[string]any{
			"name": "todowrite",
			"inputSchema": map[string]any{
				"type": "object",
				"properties": map[string]any{
					"todos": map[string]any{
						"type": "array",
						"items": map[string]any{
							"type": "object",
							"properties": map[string]any{
								"content":  map[string]any{"type": "string"},
								"status":   map[string]any{"type": "string"},
								"priority": map[string]any{"type": "string"},
							},
						},
					},
				},
			},
		},
	}
	todos := []any{map[string]any{"content": "x", "status": "pending", "priority": "high"}}
	calls := []ParsedToolCall{{Name: "todowrite", Input: map[string]any{"todos": todos}}}
	got := NormalizeParsedToolCallsForSchemas(calls, toolsRaw)
	if !reflect.DeepEqual(got[0].Input["todos"], todos) {
		t.Fatalf("expected todos array preserved, got %#v want %#v", got[0].Input["todos"], todos)
	}
}