-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtool_schema_test.go
More file actions
631 lines (577 loc) · 15.3 KB
/
tool_schema_test.go
File metadata and controls
631 lines (577 loc) · 15.3 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
package main
import (
"testing"
openai "github.com/sashabaranov/go-openai"
)
func TestParseToolSchema(t *testing.T) {
tests := []struct {
name string
input string
expectError bool
validate func(*testing.T, *ToolMeta)
}{
{
name: "Valid tool schema",
input: `{
"name": "get_city_info",
"description": "Get information about a city",
"parameters": {
"type": "object",
"properties": {
"city": { "type": "string" }
},
"required": ["city"]
}
}`,
expectError: false,
validate: func(t *testing.T, meta *ToolMeta) {
if meta.Name != "get_city_info" {
t.Errorf("expected name 'get_city_info', got '%s'", meta.Name)
}
if meta.Description != "Get information about a city" {
t.Errorf(
"expected description 'Get information about a city', got '%s'",
meta.Description,
)
}
if meta.Parameters == nil {
t.Error("expected parameters to be non-nil")
}
if meta.Parameters["type"] != "object" {
t.Errorf("expected parameters type 'object', got '%v'", meta.Parameters["type"])
}
},
},
{
name: "Minimal tool schema",
input: `{
"name": "simple_function",
"parameters": {}
}`,
expectError: false,
validate: func(t *testing.T, meta *ToolMeta) {
if meta.Name != "simple_function" {
t.Errorf("expected name 'simple_function', got '%s'", meta.Name)
}
if meta.Description != "" {
t.Errorf("expected empty description, got '%s'", meta.Description)
}
},
},
{
name: "Empty string",
input: "",
expectError: false,
validate: func(t *testing.T, meta *ToolMeta) {
if meta != nil {
t.Error("expected nil for empty input")
}
},
},
{
name: "Invalid JSON",
input: "not valid json",
expectError: true,
},
{
name: "Missing name field",
input: `{"description": "test", "parameters": {}}`,
expectError: true,
},
{
name: "Empty name field",
input: `{"name": "", "parameters": {}}`,
expectError: true,
},
{
name: "Malformed JSON",
input: `{"name": "test"`,
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
meta, err := ParseToolSchema(tt.input)
if tt.expectError && err == nil {
t.Error("expected error but got none")
}
if !tt.expectError && err != nil {
t.Errorf("unexpected error: %v", err)
}
if !tt.expectError && tt.validate != nil {
tt.validate(t, meta)
}
})
}
}
func TestToOpenAITool(t *testing.T) {
meta := &ToolMeta{
Name: "get_weather",
Description: "Get the current weather for a location",
Parameters: map[string]any{
"type": "object",
"properties": map[string]any{
"location": map[string]any{
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": map[string]any{
"type": "string",
"enum": []string{"celsius", "fahrenheit"},
},
},
"required": []string{"location"},
},
}
tool := meta.ToOpenAITool()
if tool.Type != openai.ToolTypeFunction {
t.Errorf("expected tool type '%s', got '%s'", openai.ToolTypeFunction, tool.Type)
}
if tool.Function == nil {
t.Fatal("expected function to be non-nil")
}
if tool.Function.Name != "get_weather" {
t.Errorf("expected function name 'get_weather', got '%s'", tool.Function.Name)
}
if tool.Function.Description != "Get the current weather for a location" {
t.Errorf(
"expected description 'Get the current weather for a location', got '%s'",
tool.Function.Description,
)
}
if tool.Function.Parameters == nil {
t.Error("expected parameters to be non-nil")
}
}
func TestToOpenAIToolMinimal(t *testing.T) {
meta := &ToolMeta{
Name: "minimal_function",
Parameters: map[string]any{},
}
tool := meta.ToOpenAITool()
if tool.Type != openai.ToolTypeFunction {
t.Errorf("expected tool type '%s', got '%s'", openai.ToolTypeFunction, tool.Type)
}
if tool.Function.Name != "minimal_function" {
t.Errorf("expected function name 'minimal_function', got '%s'", tool.Function.Name)
}
if tool.Function.Description != "" {
t.Errorf("expected empty description, got '%s'", tool.Function.Description)
}
}
func TestParseToolSchemaWithComplexParameters(t *testing.T) {
input := `{
"name": "analyze_code",
"description": "Analyze code and return structured results",
"parameters": {
"type": "object",
"properties": {
"score": {
"type": "integer",
"description": "Code quality score 1-10"
},
"issues": {
"type": "array",
"items": { "type": "string" },
"description": "List of issues found"
},
"metadata": {
"type": "object",
"properties": {
"language": { "type": "string" },
"lines": { "type": "integer" }
}
},
"is_valid": {
"type": "boolean"
}
},
"required": ["score", "issues"]
}
}`
meta, err := ParseToolSchema(input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if meta.Name != "analyze_code" {
t.Errorf("expected name 'analyze_code', got '%s'", meta.Name)
}
// Verify parameters structure
params := meta.Parameters
if params == nil {
t.Fatal("expected parameters to be non-nil")
}
props, ok := params["properties"].(map[string]any)
if !ok {
t.Fatal("expected properties to be a map")
}
// Check score property
score, ok := props["score"].(map[string]any)
if !ok {
t.Fatal("expected score property to be a map")
}
if score["type"] != "integer" {
t.Errorf("expected score type 'integer', got '%v'", score["type"])
}
// Check issues property (array)
issues, ok := props["issues"].(map[string]any)
if !ok {
t.Fatal("expected issues property to be a map")
}
if issues["type"] != "array" {
t.Errorf("expected issues type 'array', got '%v'", issues["type"])
}
}
// TestParseFunctionArguments tests parsing of function call arguments JSON
func TestParseFunctionArguments(t *testing.T) {
tests := []struct {
name string
jsonStr string
expected map[string]string
expectError bool
}{
{
name: "Simple string values",
jsonStr: `{"city": "Paris", "country": "France"}`,
expected: map[string]string{
"city": "Paris",
"country": "France",
},
},
{
name: "Integer value",
jsonStr: `{"score": 8, "name": "test"}`,
expected: map[string]string{
"score": "8",
"name": "test",
},
},
{
name: "Boolean value",
jsonStr: `{"is_valid": true, "is_error": false}`,
expected: map[string]string{
"is_valid": "true",
"is_error": "false",
},
},
{
name: "Array value",
jsonStr: `{"issues": ["bug1", "bug2"], "name": "review"}`,
expected: map[string]string{
"issues": `["bug1","bug2"]`,
"name": "review",
},
},
{
name: "Nested object value",
jsonStr: `{"metadata": {"lang": "go", "version": 1}, "name": "test"}`,
expected: map[string]string{
"metadata": `{"lang":"go","version":1}`,
"name": "test",
},
},
{
name: "Float value",
jsonStr: `{"temperature": 0.7, "name": "config"}`,
expected: map[string]string{
"temperature": "0.7",
"name": "config",
},
},
{
name: "Null value",
jsonStr: `{"optional": null, "name": "test"}`,
expected: map[string]string{
"optional": "null",
"name": "test",
},
},
{
name: "Empty object",
jsonStr: `{}`,
expected: map[string]string{},
},
{
name: "Empty string",
jsonStr: "",
expected: map[string]string{},
},
{
name: "Invalid JSON",
jsonStr: "not valid json",
expectError: true,
},
{
name: "Malformed JSON",
jsonStr: `{"key": "value"`,
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
output, err := ParseFunctionArguments(tt.jsonStr)
if tt.expectError {
if err == nil {
t.Error("expected error but got none")
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Verify each expected key-value pair
for key, expectedValue := range tt.expected {
actualValue, ok := output[key]
if !ok {
t.Errorf("expected key '%s' not found in output", key)
continue
}
if actualValue != expectedValue {
t.Errorf(
"for key '%s': expected '%s', got '%s'",
key,
expectedValue,
actualValue,
)
}
}
// Verify no extra keys
if len(output) != len(tt.expected) {
t.Errorf("expected %d keys, got %d", len(tt.expected), len(output))
}
})
}
}
func TestParseToolSchemaWithUnicodeCharacters(t *testing.T) {
input := `{
"name": "get_info",
"description": "取得資訊 - 获取信息",
"parameters": {
"type": "object",
"properties": {
"城市": { "type": "string", "description": "城市名稱" }
}
}
}`
meta, err := ParseToolSchema(input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if meta.Name != "get_info" {
t.Errorf("expected name 'get_info', got '%s'", meta.Name)
}
if meta.Description != "取得資訊 - 获取信息" {
t.Errorf("expected description with unicode, got '%s'", meta.Description)
}
}
func TestParseToolSchemaWithSpecialCharacters(t *testing.T) {
input := `{
"name": "handle_text",
"description": "Handle text with special chars: \"quotes\", \\backslash, \n newline",
"parameters": {
"type": "object",
"properties": {}
}
}`
meta, err := ParseToolSchema(input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if meta.Name != "handle_text" {
t.Errorf("expected name 'handle_text', got '%s'", meta.Name)
}
expectedDesc := "Handle text with special chars: \"quotes\", \\backslash, \n newline"
if meta.Description != expectedDesc {
t.Errorf("expected description '%s', got '%s'", expectedDesc, meta.Description)
}
}
// TestBuildOutputMap tests the BuildOutputMap function
func TestBuildOutputMap(t *testing.T) {
tests := []struct {
name string
rawResponse string
toolArgs map[string]string
expectedOutput map[string]string
expectedSkipped bool
}{
{
name: "No tool arguments - only raw response",
rawResponse: "Hello, world!",
toolArgs: nil,
expectedOutput: map[string]string{
"response": "Hello, world!",
},
expectedSkipped: false,
},
{
name: "Empty tool arguments",
rawResponse: "Test response",
toolArgs: map[string]string{},
expectedOutput: map[string]string{
"response": "Test response",
},
expectedSkipped: false,
},
{
name: "Tool arguments without reserved field",
rawResponse: `{"city":"Paris","country":"France"}`,
toolArgs: map[string]string{
"city": "Paris",
"country": "France",
},
expectedOutput: map[string]string{
"response": `{"city":"Paris","country":"France"}`,
"city": "Paris",
"country": "France",
},
expectedSkipped: false,
},
{
name: "Tool arguments with reserved 'response' field - should be skipped",
rawResponse: `{"response":"custom","city":"Tokyo"}`,
toolArgs: map[string]string{
"response": "custom",
"city": "Tokyo",
},
expectedOutput: map[string]string{
"response": `{"response":"custom","city":"Tokyo"}`,
"city": "Tokyo",
},
expectedSkipped: true,
},
{
name: "Only reserved 'response' field in tool args",
rawResponse: `{"response":"should be skipped"}`,
toolArgs: map[string]string{
"response": "should be skipped",
},
expectedOutput: map[string]string{
"response": `{"response":"should be skipped"}`,
},
expectedSkipped: true,
},
{
name: "Multiple fields with reserved field",
rawResponse: `{"response":"skip","name":"test","score":"10"}`,
toolArgs: map[string]string{
"response": "skip",
"name": "test",
"score": "10",
},
expectedOutput: map[string]string{
"response": `{"response":"skip","name":"test","score":"10"}`,
"name": "test",
"score": "10",
},
expectedSkipped: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
output, skipped := BuildOutputMap(tt.rawResponse, tt.toolArgs)
// Check if reserved field was skipped
if skipped != tt.expectedSkipped {
t.Errorf("expected skipped=%v, got skipped=%v", tt.expectedSkipped, skipped)
}
// Check output map length
if len(output) != len(tt.expectedOutput) {
t.Errorf("expected %d keys, got %d keys", len(tt.expectedOutput), len(output))
}
// Check each expected key-value pair
for key, expectedValue := range tt.expectedOutput {
actualValue, ok := output[key]
if !ok {
t.Errorf("expected key '%s' not found in output", key)
continue
}
if actualValue != expectedValue {
t.Errorf(
"for key '%s': expected '%s', got '%s'",
key,
expectedValue,
actualValue,
)
}
}
})
}
}
// TestBuildOutputMapResponseFieldAlwaysPresent verifies that the 'response' field
// is always present in the output, containing the raw LLM response
func TestBuildOutputMapResponseFieldAlwaysPresent(t *testing.T) {
testCases := []struct {
name string
rawResponse string
toolArgs map[string]string
}{
{
name: "With nil tool args",
rawResponse: "raw response content",
toolArgs: nil,
},
{
name: "With empty tool args",
rawResponse: "raw response content",
toolArgs: map[string]string{},
},
{
name: "With tool args",
rawResponse: `{"field":"value"}`,
toolArgs: map[string]string{"field": "value"},
},
{
name: "With tool args containing response field",
rawResponse: `{"response":"overwrite attempt"}`,
toolArgs: map[string]string{"response": "overwrite attempt"},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
output, _ := BuildOutputMap(tc.rawResponse, tc.toolArgs)
// Verify 'response' key always exists
responseValue, ok := output[ReservedOutputField]
if !ok {
t.Fatalf("expected '%s' key to always be present in output", ReservedOutputField)
}
// Verify 'response' value is the raw response (not overwritten by tool args)
if responseValue != tc.rawResponse {
t.Errorf(
"expected '%s' value to be '%s', got '%s'",
ReservedOutputField,
tc.rawResponse,
responseValue,
)
}
})
}
}
// TestBuildOutputMapReservedFieldSkipped verifies that when tool args contain
// a 'response' field, it is skipped and a warning indicator is returned
func TestBuildOutputMapReservedFieldSkipped(t *testing.T) {
rawResponse := `{"response":"should not override","data":"keep"}`
toolArgs := map[string]string{
"response": "should not override",
"data": "keep",
}
output, skipped := BuildOutputMap(rawResponse, toolArgs)
// Verify skipped flag is true
if !skipped {
t.Error("expected skipped to be true when tool args contain 'response' field")
}
// Verify 'response' in output is the raw response, not the tool arg value
if output[ReservedOutputField] != rawResponse {
t.Errorf(
"expected response to be raw response '%s', got '%s'",
rawResponse,
output[ReservedOutputField],
)
}
// Verify 'data' field is included
if output["data"] != "keep" {
t.Errorf("expected 'data' field to be 'keep', got '%s'", output["data"])
}
// Verify only 2 keys exist (response and data, not the tool's response)
if len(output) != 2 {
t.Errorf("expected 2 keys in output, got %d", len(output))
}
}