@ank1015/providers 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +93 -383
- package/dist/agent/conversation.d.ts +97 -0
- package/dist/agent/conversation.d.ts.map +1 -0
- package/dist/agent/conversation.js +328 -0
- package/dist/agent/conversation.js.map +1 -0
- package/dist/agent/runner.d.ts +37 -0
- package/dist/agent/runner.d.ts.map +1 -0
- package/dist/agent/runner.js +169 -0
- package/dist/agent/runner.js.map +1 -0
- package/dist/agent/tools/calculate.d.ts +15 -0
- package/dist/agent/tools/calculate.d.ts.map +1 -0
- package/dist/agent/tools/calculate.js +23 -0
- package/dist/agent/tools/calculate.js.map +1 -0
- package/dist/agent/tools/get-current-time.d.ts +15 -0
- package/dist/agent/tools/get-current-time.d.ts.map +1 -0
- package/dist/agent/tools/get-current-time.js +38 -0
- package/dist/agent/tools/get-current-time.js.map +1 -0
- package/dist/agent/tools/index.d.ts +3 -0
- package/dist/agent/tools/index.d.ts.map +1 -0
- package/dist/agent/tools/index.js +3 -0
- package/dist/agent/tools/index.js.map +1 -0
- package/dist/agent/types.d.ts +53 -31
- package/dist/agent/types.d.ts.map +1 -1
- package/dist/agent/types.js +1 -2
- package/dist/agent/utils.d.ts +14 -0
- package/dist/agent/utils.d.ts.map +1 -0
- package/dist/agent/utils.js +59 -0
- package/dist/agent/utils.js.map +1 -0
- package/dist/index.d.ts +16 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +16 -28
- package/dist/index.js.map +1 -1
- package/dist/llm.d.ts +15 -0
- package/dist/llm.d.ts.map +1 -0
- package/dist/llm.js +92 -0
- package/dist/llm.js.map +1 -0
- package/dist/models.d.ts +8 -1
- package/dist/models.d.ts.map +1 -1
- package/dist/models.generated.d.ts +25 -112
- package/dist/models.generated.d.ts.map +1 -1
- package/dist/models.generated.js +72 -227
- package/dist/models.generated.js.map +1 -1
- package/dist/models.js +30 -32
- package/dist/models.js.map +1 -1
- package/dist/providers/google/complete.d.ts +3 -0
- package/dist/providers/google/complete.d.ts.map +1 -0
- package/dist/providers/google/complete.js +53 -0
- package/dist/providers/google/complete.js.map +1 -0
- package/dist/providers/google/index.d.ts +6 -0
- package/dist/providers/google/index.d.ts.map +1 -0
- package/dist/providers/google/index.js +6 -0
- package/dist/providers/google/index.js.map +1 -0
- package/dist/providers/google/stream.d.ts +3 -0
- package/dist/providers/google/stream.d.ts.map +1 -0
- package/dist/providers/{google.js → google/stream.js} +67 -231
- package/dist/providers/google/stream.js.map +1 -0
- package/dist/providers/google/types.d.ts +8 -0
- package/dist/providers/google/types.d.ts.map +1 -0
- package/dist/providers/google/types.js +2 -0
- package/dist/providers/google/types.js.map +1 -0
- package/dist/providers/google/utils.d.ts +30 -0
- package/dist/providers/google/utils.d.ts.map +1 -0
- package/dist/providers/google/utils.js +354 -0
- package/dist/providers/google/utils.js.map +1 -0
- package/dist/providers/openai/complete.d.ts +3 -0
- package/dist/providers/openai/complete.d.ts.map +1 -0
- package/dist/providers/openai/complete.js +57 -0
- package/dist/providers/openai/complete.js.map +1 -0
- package/dist/providers/openai/index.d.ts +4 -0
- package/dist/providers/openai/index.d.ts.map +1 -0
- package/dist/providers/openai/index.js +4 -0
- package/dist/providers/openai/index.js.map +1 -0
- package/dist/providers/openai/stream.d.ts +3 -0
- package/dist/providers/openai/stream.d.ts.map +1 -0
- package/dist/providers/{openai.js → openai/stream.js} +74 -152
- package/dist/providers/openai/stream.js.map +1 -0
- package/dist/providers/openai/types.d.ts +8 -0
- package/dist/providers/openai/types.d.ts.map +1 -0
- package/dist/providers/openai/types.js +2 -0
- package/dist/providers/openai/types.js.map +1 -0
- package/dist/providers/openai/utils.d.ts +13 -0
- package/dist/providers/openai/utils.d.ts.map +1 -0
- package/dist/providers/openai/utils.js +285 -0
- package/dist/providers/openai/utils.js.map +1 -0
- package/dist/types.d.ts +95 -87
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -9
- package/dist/types.js.map +1 -1
- package/dist/utils/event-stream.d.ts +2 -2
- package/dist/utils/event-stream.d.ts.map +1 -1
- package/dist/utils/event-stream.js +2 -7
- package/dist/utils/event-stream.js.map +1 -1
- package/dist/utils/json-parse.js +3 -6
- package/dist/utils/json-parse.js.map +1 -1
- package/dist/utils/overflow.d.ts +51 -0
- package/dist/utils/overflow.d.ts.map +1 -0
- package/dist/utils/overflow.js +106 -0
- package/dist/utils/overflow.js.map +1 -0
- package/dist/utils/sanitize-unicode.js +1 -4
- package/dist/utils/sanitize-unicode.js.map +1 -1
- package/dist/utils/uuid.d.ts +6 -0
- package/dist/utils/uuid.d.ts.map +1 -0
- package/dist/utils/uuid.js +9 -0
- package/dist/utils/uuid.js.map +1 -0
- package/dist/utils/validation.d.ts +10 -3
- package/dist/utils/validation.d.ts.map +1 -1
- package/dist/utils/validation.js +20 -12
- package/dist/utils/validation.js.map +1 -1
- package/package.json +47 -8
- package/biome.json +0 -43
- package/dist/agent/agent-loop.d.ts +0 -5
- package/dist/agent/agent-loop.d.ts.map +0 -1
- package/dist/agent/agent-loop.js +0 -219
- package/dist/agent/agent-loop.js.map +0 -1
- package/dist/providers/convert.d.ts +0 -6
- package/dist/providers/convert.d.ts.map +0 -1
- package/dist/providers/convert.js +0 -207
- package/dist/providers/convert.js.map +0 -1
- package/dist/providers/google.d.ts +0 -26
- package/dist/providers/google.d.ts.map +0 -1
- package/dist/providers/google.js.map +0 -1
- package/dist/providers/openai.d.ts +0 -17
- package/dist/providers/openai.d.ts.map +0 -1
- package/dist/providers/openai.js.map +0 -1
- package/dist/stream.d.ts +0 -4
- package/dist/stream.d.ts.map +0 -1
- package/dist/stream.js +0 -40
- package/dist/stream.js.map +0 -1
- package/dist/test-google-agent-loop.d.ts +0 -2
- package/dist/test-google-agent-loop.d.ts.map +0 -1
- package/dist/test-google-agent-loop.js +0 -186
- package/dist/test-google-agent-loop.js.map +0 -1
- package/dist/test-google.d.ts +0 -2
- package/dist/test-google.d.ts.map +0 -1
- package/dist/test-google.js +0 -41
- package/dist/test-google.js.map +0 -1
- package/src/agent/agent-loop.ts +0 -275
- package/src/agent/types.ts +0 -80
- package/src/index.ts +0 -72
- package/src/models.generated.ts +0 -314
- package/src/models.ts +0 -45
- package/src/providers/convert.ts +0 -222
- package/src/providers/google.ts +0 -496
- package/src/providers/openai.ts +0 -437
- package/src/stream.ts +0 -60
- package/src/types.ts +0 -198
- package/src/utils/event-stream.ts +0 -60
- package/src/utils/json-parse.ts +0 -28
- package/src/utils/sanitize-unicode.ts +0 -25
- package/src/utils/validation.ts +0 -69
- package/test/core/agent-loop.test.ts +0 -958
- package/test/core/stream.test.ts +0 -409
- package/test/data/red-circle.png +0 -0
- package/test/data/superintelligentwill.pdf +0 -0
- package/test/edge-cases/general.test.ts +0 -565
- package/test/integration/e2e.test.ts +0 -530
- package/test/models/cost.test.ts +0 -499
- package/test/models/registry.test.ts +0 -298
- package/test/providers/convert.test.ts +0 -846
- package/test/providers/google-schema.test.ts +0 -666
- package/test/providers/google-stream.test.ts +0 -369
- package/test/providers/openai-stream.test.ts +0 -251
- package/test/utils/event-stream.test.ts +0 -289
- package/test/utils/json-parse.test.ts +0 -344
- package/test/utils/sanitize-unicode.test.ts +0 -329
- package/test/utils/validation.test.ts +0 -614
- package/tsconfig.json +0 -21
- package/vitest.config.ts +0 -9
|
@@ -1,614 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
|
2
|
-
import { Type } from '@sinclair/typebox';
|
|
3
|
-
import { validateToolArguments } from '../../src/utils/validation';
|
|
4
|
-
import { Tool } from '../../src/types';
|
|
5
|
-
|
|
6
|
-
describe('validateToolArguments', () => {
|
|
7
|
-
describe('Valid arguments', () => {
|
|
8
|
-
it('should pass validation for valid arguments', () => {
|
|
9
|
-
const tool: Tool = {
|
|
10
|
-
name: 'test',
|
|
11
|
-
description: 'Test tool',
|
|
12
|
-
parameters: Type.Object({
|
|
13
|
-
name: Type.String(),
|
|
14
|
-
age: Type.Number(),
|
|
15
|
-
}),
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
const toolCall = {
|
|
19
|
-
type: 'toolCall' as const,
|
|
20
|
-
name: 'test',
|
|
21
|
-
arguments: { name: 'John', age: 30 },
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
const result = validateToolArguments(tool, toolCall);
|
|
25
|
-
expect(result).toEqual({ name: 'John', age: 30 });
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
it('should pass validation for nested objects', () => {
|
|
29
|
-
const tool: Tool = {
|
|
30
|
-
name: 'test',
|
|
31
|
-
description: 'Test tool',
|
|
32
|
-
parameters: Type.Object({
|
|
33
|
-
user: Type.Object({
|
|
34
|
-
name: Type.String(),
|
|
35
|
-
email: Type.String(),
|
|
36
|
-
}),
|
|
37
|
-
count: Type.Number(),
|
|
38
|
-
}),
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
const toolCall = {
|
|
42
|
-
type: 'toolCall' as const,
|
|
43
|
-
name: 'test',
|
|
44
|
-
arguments: {
|
|
45
|
-
user: { name: 'John', email: 'john@example.com' },
|
|
46
|
-
count: 5,
|
|
47
|
-
},
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
const result = validateToolArguments(tool, toolCall);
|
|
51
|
-
expect(result).toEqual({
|
|
52
|
-
user: { name: 'John', email: 'john@example.com' },
|
|
53
|
-
count: 5,
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it('should pass validation for arrays', () => {
|
|
58
|
-
const tool: Tool = {
|
|
59
|
-
name: 'test',
|
|
60
|
-
description: 'Test tool',
|
|
61
|
-
parameters: Type.Object({
|
|
62
|
-
items: Type.Array(Type.String()),
|
|
63
|
-
numbers: Type.Array(Type.Number()),
|
|
64
|
-
}),
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
const toolCall = {
|
|
68
|
-
type: 'toolCall' as const,
|
|
69
|
-
name: 'test',
|
|
70
|
-
arguments: {
|
|
71
|
-
items: ['a', 'b', 'c'],
|
|
72
|
-
numbers: [1, 2, 3],
|
|
73
|
-
},
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
const result = validateToolArguments(tool, toolCall);
|
|
77
|
-
expect(result).toEqual({
|
|
78
|
-
items: ['a', 'b', 'c'],
|
|
79
|
-
numbers: [1, 2, 3],
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
it('should pass validation for optional fields', () => {
|
|
84
|
-
const tool: Tool = {
|
|
85
|
-
name: 'test',
|
|
86
|
-
description: 'Test tool',
|
|
87
|
-
parameters: Type.Object({
|
|
88
|
-
required: Type.String(),
|
|
89
|
-
optional: Type.Optional(Type.String()),
|
|
90
|
-
}),
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
const toolCall = {
|
|
94
|
-
type: 'toolCall' as const,
|
|
95
|
-
name: 'test',
|
|
96
|
-
arguments: { required: 'value' },
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
const result = validateToolArguments(tool, toolCall);
|
|
100
|
-
expect(result).toEqual({ required: 'value' });
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
it('should pass validation with optional fields present', () => {
|
|
104
|
-
const tool: Tool = {
|
|
105
|
-
name: 'test',
|
|
106
|
-
description: 'Test tool',
|
|
107
|
-
parameters: Type.Object({
|
|
108
|
-
required: Type.String(),
|
|
109
|
-
optional: Type.Optional(Type.String()),
|
|
110
|
-
}),
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
const toolCall = {
|
|
114
|
-
type: 'toolCall' as const,
|
|
115
|
-
name: 'test',
|
|
116
|
-
arguments: { required: 'value', optional: 'optional-value' },
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
const result = validateToolArguments(tool, toolCall);
|
|
120
|
-
expect(result).toEqual({ required: 'value', optional: 'optional-value' });
|
|
121
|
-
});
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
describe('Invalid arguments', () => {
|
|
125
|
-
it('should throw error for missing required field', () => {
|
|
126
|
-
const tool: Tool = {
|
|
127
|
-
name: 'test',
|
|
128
|
-
description: 'Test tool',
|
|
129
|
-
parameters: Type.Object({
|
|
130
|
-
name: Type.String(),
|
|
131
|
-
age: Type.Number(),
|
|
132
|
-
}),
|
|
133
|
-
};
|
|
134
|
-
|
|
135
|
-
const toolCall = {
|
|
136
|
-
type: 'toolCall' as const,
|
|
137
|
-
name: 'test',
|
|
138
|
-
arguments: { name: 'John' }, // missing age
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
expect(() => validateToolArguments(tool, toolCall)).toThrow();
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
it('should throw error for type mismatch (string vs number)', () => {
|
|
145
|
-
const tool: Tool = {
|
|
146
|
-
name: 'test',
|
|
147
|
-
description: 'Test tool',
|
|
148
|
-
parameters: Type.Object({
|
|
149
|
-
age: Type.Number(),
|
|
150
|
-
}),
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
const toolCall = {
|
|
154
|
-
type: 'toolCall' as const,
|
|
155
|
-
name: 'test',
|
|
156
|
-
arguments: { age: 'thirty' }, // should be number
|
|
157
|
-
};
|
|
158
|
-
|
|
159
|
-
expect(() => validateToolArguments(tool, toolCall)).toThrow();
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
it('should throw error for extra fields when additionalProperties is false', () => {
|
|
163
|
-
const tool: Tool = {
|
|
164
|
-
name: 'test',
|
|
165
|
-
description: 'Test tool',
|
|
166
|
-
parameters: Type.Object(
|
|
167
|
-
{
|
|
168
|
-
name: Type.String(),
|
|
169
|
-
},
|
|
170
|
-
{ additionalProperties: false }
|
|
171
|
-
),
|
|
172
|
-
};
|
|
173
|
-
|
|
174
|
-
const toolCall = {
|
|
175
|
-
type: 'toolCall' as const,
|
|
176
|
-
name: 'test',
|
|
177
|
-
arguments: { name: 'John', extra: 'field' },
|
|
178
|
-
};
|
|
179
|
-
|
|
180
|
-
expect(() => validateToolArguments(tool, toolCall)).toThrow();
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
it('should throw error for invalid nested object', () => {
|
|
184
|
-
const tool: Tool = {
|
|
185
|
-
name: 'test',
|
|
186
|
-
description: 'Test tool',
|
|
187
|
-
parameters: Type.Object({
|
|
188
|
-
user: Type.Object({
|
|
189
|
-
name: Type.String(),
|
|
190
|
-
age: Type.Number(),
|
|
191
|
-
}),
|
|
192
|
-
}),
|
|
193
|
-
};
|
|
194
|
-
|
|
195
|
-
const toolCall = {
|
|
196
|
-
type: 'toolCall' as const,
|
|
197
|
-
name: 'test',
|
|
198
|
-
arguments: {
|
|
199
|
-
user: { name: 'John' }, // missing age
|
|
200
|
-
},
|
|
201
|
-
};
|
|
202
|
-
|
|
203
|
-
expect(() => validateToolArguments(tool, toolCall)).toThrow();
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
it('should throw error for invalid array items', () => {
|
|
207
|
-
const tool: Tool = {
|
|
208
|
-
name: 'test',
|
|
209
|
-
description: 'Test tool',
|
|
210
|
-
parameters: Type.Object({
|
|
211
|
-
numbers: Type.Array(Type.Number()),
|
|
212
|
-
}),
|
|
213
|
-
};
|
|
214
|
-
|
|
215
|
-
const toolCall = {
|
|
216
|
-
type: 'toolCall' as const,
|
|
217
|
-
name: 'test',
|
|
218
|
-
arguments: {
|
|
219
|
-
numbers: [1, 2, 'three'], // should be all numbers
|
|
220
|
-
},
|
|
221
|
-
};
|
|
222
|
-
|
|
223
|
-
expect(() => validateToolArguments(tool, toolCall)).toThrow();
|
|
224
|
-
});
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
describe('Enum validation', () => {
|
|
228
|
-
it('should pass validation for valid enum value', () => {
|
|
229
|
-
const tool: Tool = {
|
|
230
|
-
name: 'test',
|
|
231
|
-
description: 'Test tool',
|
|
232
|
-
parameters: Type.Object({
|
|
233
|
-
status: Type.Union([
|
|
234
|
-
Type.Literal('active'),
|
|
235
|
-
Type.Literal('inactive'),
|
|
236
|
-
Type.Literal('pending'),
|
|
237
|
-
]),
|
|
238
|
-
}),
|
|
239
|
-
};
|
|
240
|
-
|
|
241
|
-
const toolCall = {
|
|
242
|
-
type: 'toolCall' as const,
|
|
243
|
-
name: 'test',
|
|
244
|
-
arguments: { status: 'active' },
|
|
245
|
-
};
|
|
246
|
-
|
|
247
|
-
const result = validateToolArguments(tool, toolCall);
|
|
248
|
-
expect(result).toEqual({ status: 'active' });
|
|
249
|
-
});
|
|
250
|
-
|
|
251
|
-
it('should throw error for invalid enum value', () => {
|
|
252
|
-
const tool: Tool = {
|
|
253
|
-
name: 'test',
|
|
254
|
-
description: 'Test tool',
|
|
255
|
-
parameters: Type.Object({
|
|
256
|
-
status: Type.Union([
|
|
257
|
-
Type.Literal('active'),
|
|
258
|
-
Type.Literal('inactive'),
|
|
259
|
-
Type.Literal('pending'),
|
|
260
|
-
]),
|
|
261
|
-
}),
|
|
262
|
-
};
|
|
263
|
-
|
|
264
|
-
const toolCall = {
|
|
265
|
-
type: 'toolCall' as const,
|
|
266
|
-
name: 'test',
|
|
267
|
-
arguments: { status: 'invalid' },
|
|
268
|
-
};
|
|
269
|
-
|
|
270
|
-
expect(() => validateToolArguments(tool, toolCall)).toThrow();
|
|
271
|
-
});
|
|
272
|
-
});
|
|
273
|
-
|
|
274
|
-
describe('Number constraints', () => {
|
|
275
|
-
it('should pass validation for number within range', () => {
|
|
276
|
-
const tool: Tool = {
|
|
277
|
-
name: 'test',
|
|
278
|
-
description: 'Test tool',
|
|
279
|
-
parameters: Type.Object({
|
|
280
|
-
age: Type.Number({ minimum: 0, maximum: 120 }),
|
|
281
|
-
}),
|
|
282
|
-
};
|
|
283
|
-
|
|
284
|
-
const toolCall = {
|
|
285
|
-
type: 'toolCall' as const,
|
|
286
|
-
name: 'test',
|
|
287
|
-
arguments: { age: 30 },
|
|
288
|
-
};
|
|
289
|
-
|
|
290
|
-
const result = validateToolArguments(tool, toolCall);
|
|
291
|
-
expect(result).toEqual({ age: 30 });
|
|
292
|
-
});
|
|
293
|
-
|
|
294
|
-
it('should throw error for number below minimum', () => {
|
|
295
|
-
const tool: Tool = {
|
|
296
|
-
name: 'test',
|
|
297
|
-
description: 'Test tool',
|
|
298
|
-
parameters: Type.Object({
|
|
299
|
-
age: Type.Number({ minimum: 0 }),
|
|
300
|
-
}),
|
|
301
|
-
};
|
|
302
|
-
|
|
303
|
-
const toolCall = {
|
|
304
|
-
type: 'toolCall' as const,
|
|
305
|
-
name: 'test',
|
|
306
|
-
arguments: { age: -5 },
|
|
307
|
-
};
|
|
308
|
-
|
|
309
|
-
expect(() => validateToolArguments(tool, toolCall)).toThrow();
|
|
310
|
-
});
|
|
311
|
-
|
|
312
|
-
it('should throw error for number above maximum', () => {
|
|
313
|
-
const tool: Tool = {
|
|
314
|
-
name: 'test',
|
|
315
|
-
description: 'Test tool',
|
|
316
|
-
parameters: Type.Object({
|
|
317
|
-
age: Type.Number({ maximum: 120 }),
|
|
318
|
-
}),
|
|
319
|
-
};
|
|
320
|
-
|
|
321
|
-
const toolCall = {
|
|
322
|
-
type: 'toolCall' as const,
|
|
323
|
-
name: 'test',
|
|
324
|
-
arguments: { age: 150 },
|
|
325
|
-
};
|
|
326
|
-
|
|
327
|
-
expect(() => validateToolArguments(tool, toolCall)).toThrow();
|
|
328
|
-
});
|
|
329
|
-
});
|
|
330
|
-
|
|
331
|
-
describe('String constraints', () => {
|
|
332
|
-
it('should pass validation for string within length constraints', () => {
|
|
333
|
-
const tool: Tool = {
|
|
334
|
-
name: 'test',
|
|
335
|
-
description: 'Test tool',
|
|
336
|
-
parameters: Type.Object({
|
|
337
|
-
name: Type.String({ minLength: 2, maxLength: 50 }),
|
|
338
|
-
}),
|
|
339
|
-
};
|
|
340
|
-
|
|
341
|
-
const toolCall = {
|
|
342
|
-
type: 'toolCall' as const,
|
|
343
|
-
name: 'test',
|
|
344
|
-
arguments: { name: 'John' },
|
|
345
|
-
};
|
|
346
|
-
|
|
347
|
-
const result = validateToolArguments(tool, toolCall);
|
|
348
|
-
expect(result).toEqual({ name: 'John' });
|
|
349
|
-
});
|
|
350
|
-
|
|
351
|
-
it('should throw error for string below minLength', () => {
|
|
352
|
-
const tool: Tool = {
|
|
353
|
-
name: 'test',
|
|
354
|
-
description: 'Test tool',
|
|
355
|
-
parameters: Type.Object({
|
|
356
|
-
name: Type.String({ minLength: 5 }),
|
|
357
|
-
}),
|
|
358
|
-
};
|
|
359
|
-
|
|
360
|
-
const toolCall = {
|
|
361
|
-
type: 'toolCall' as const,
|
|
362
|
-
name: 'test',
|
|
363
|
-
arguments: { name: 'Joe' },
|
|
364
|
-
};
|
|
365
|
-
|
|
366
|
-
expect(() => validateToolArguments(tool, toolCall)).toThrow();
|
|
367
|
-
});
|
|
368
|
-
|
|
369
|
-
it('should throw error for string above maxLength', () => {
|
|
370
|
-
const tool: Tool = {
|
|
371
|
-
name: 'test',
|
|
372
|
-
description: 'Test tool',
|
|
373
|
-
parameters: Type.Object({
|
|
374
|
-
name: Type.String({ maxLength: 5 }),
|
|
375
|
-
}),
|
|
376
|
-
};
|
|
377
|
-
|
|
378
|
-
const toolCall = {
|
|
379
|
-
type: 'toolCall' as const,
|
|
380
|
-
name: 'test',
|
|
381
|
-
arguments: { name: 'Jonathan' },
|
|
382
|
-
};
|
|
383
|
-
|
|
384
|
-
expect(() => validateToolArguments(tool, toolCall)).toThrow();
|
|
385
|
-
});
|
|
386
|
-
|
|
387
|
-
it('should pass validation for string matching pattern', () => {
|
|
388
|
-
const tool: Tool = {
|
|
389
|
-
name: 'test',
|
|
390
|
-
description: 'Test tool',
|
|
391
|
-
parameters: Type.Object({
|
|
392
|
-
email: Type.String({ pattern: '^[a-z]+@[a-z]+\\.[a-z]+$' }),
|
|
393
|
-
}),
|
|
394
|
-
};
|
|
395
|
-
|
|
396
|
-
const toolCall = {
|
|
397
|
-
type: 'toolCall' as const,
|
|
398
|
-
name: 'test',
|
|
399
|
-
arguments: { email: 'john@example.com' },
|
|
400
|
-
};
|
|
401
|
-
|
|
402
|
-
const result = validateToolArguments(tool, toolCall);
|
|
403
|
-
expect(result).toEqual({ email: 'john@example.com' });
|
|
404
|
-
});
|
|
405
|
-
|
|
406
|
-
it('should throw error for string not matching pattern', () => {
|
|
407
|
-
const tool: Tool = {
|
|
408
|
-
name: 'test',
|
|
409
|
-
description: 'Test tool',
|
|
410
|
-
parameters: Type.Object({
|
|
411
|
-
email: Type.String({ pattern: '^[a-z]+@[a-z]+\\.[a-z]+$' }),
|
|
412
|
-
}),
|
|
413
|
-
};
|
|
414
|
-
|
|
415
|
-
const toolCall = {
|
|
416
|
-
type: 'toolCall' as const,
|
|
417
|
-
name: 'test',
|
|
418
|
-
arguments: { email: 'invalid-email' },
|
|
419
|
-
};
|
|
420
|
-
|
|
421
|
-
expect(() => validateToolArguments(tool, toolCall)).toThrow();
|
|
422
|
-
});
|
|
423
|
-
});
|
|
424
|
-
|
|
425
|
-
describe('Complex schemas', () => {
|
|
426
|
-
it('should validate complex nested schema', () => {
|
|
427
|
-
const tool: Tool = {
|
|
428
|
-
name: 'test',
|
|
429
|
-
description: 'Test tool',
|
|
430
|
-
parameters: Type.Object({
|
|
431
|
-
user: Type.Object({
|
|
432
|
-
name: Type.String(),
|
|
433
|
-
age: Type.Number({ minimum: 0 }),
|
|
434
|
-
addresses: Type.Array(
|
|
435
|
-
Type.Object({
|
|
436
|
-
street: Type.String(),
|
|
437
|
-
city: Type.String(),
|
|
438
|
-
zipCode: Type.String({ pattern: '^[0-9]{5}$' }),
|
|
439
|
-
})
|
|
440
|
-
),
|
|
441
|
-
}),
|
|
442
|
-
metadata: Type.Optional(
|
|
443
|
-
Type.Object({
|
|
444
|
-
tags: Type.Array(Type.String()),
|
|
445
|
-
priority: Type.Number({ minimum: 1, maximum: 5 }),
|
|
446
|
-
})
|
|
447
|
-
),
|
|
448
|
-
}),
|
|
449
|
-
};
|
|
450
|
-
|
|
451
|
-
const toolCall = {
|
|
452
|
-
type: 'toolCall' as const,
|
|
453
|
-
name: 'test',
|
|
454
|
-
arguments: {
|
|
455
|
-
user: {
|
|
456
|
-
name: 'John',
|
|
457
|
-
age: 30,
|
|
458
|
-
addresses: [
|
|
459
|
-
{ street: '123 Main St', city: 'New York', zipCode: '10001' },
|
|
460
|
-
{ street: '456 Oak Ave', city: 'Boston', zipCode: '02101' },
|
|
461
|
-
],
|
|
462
|
-
},
|
|
463
|
-
metadata: {
|
|
464
|
-
tags: ['important', 'urgent'],
|
|
465
|
-
priority: 3,
|
|
466
|
-
},
|
|
467
|
-
},
|
|
468
|
-
};
|
|
469
|
-
|
|
470
|
-
const result = validateToolArguments(tool, toolCall);
|
|
471
|
-
expect((result as any).user.name).toBe('John');
|
|
472
|
-
expect((result as any).user.addresses).toHaveLength(2);
|
|
473
|
-
expect((result as any).metadata?.priority).toBe(3);
|
|
474
|
-
});
|
|
475
|
-
|
|
476
|
-
it('should throw error for invalid deeply nested field', () => {
|
|
477
|
-
const tool: Tool = {
|
|
478
|
-
name: 'test',
|
|
479
|
-
description: 'Test tool',
|
|
480
|
-
parameters: Type.Object({
|
|
481
|
-
user: Type.Object({
|
|
482
|
-
addresses: Type.Array(
|
|
483
|
-
Type.Object({
|
|
484
|
-
zipCode: Type.String({ pattern: '^[0-9]{5}$' }),
|
|
485
|
-
})
|
|
486
|
-
),
|
|
487
|
-
}),
|
|
488
|
-
}),
|
|
489
|
-
};
|
|
490
|
-
|
|
491
|
-
const toolCall = {
|
|
492
|
-
type: 'toolCall' as const,
|
|
493
|
-
name: 'test',
|
|
494
|
-
arguments: {
|
|
495
|
-
user: {
|
|
496
|
-
addresses: [{ zipCode: 'invalid' }], // should be 5 digits
|
|
497
|
-
},
|
|
498
|
-
},
|
|
499
|
-
};
|
|
500
|
-
|
|
501
|
-
expect(() => validateToolArguments(tool, toolCall)).toThrow();
|
|
502
|
-
});
|
|
503
|
-
});
|
|
504
|
-
|
|
505
|
-
describe('Error message formatting', () => {
|
|
506
|
-
it('should provide readable error message', () => {
|
|
507
|
-
const tool: Tool = {
|
|
508
|
-
name: 'calculator',
|
|
509
|
-
description: 'Calculate something',
|
|
510
|
-
parameters: Type.Object({
|
|
511
|
-
expression: Type.String(),
|
|
512
|
-
}),
|
|
513
|
-
};
|
|
514
|
-
|
|
515
|
-
const toolCall = {
|
|
516
|
-
type: 'toolCall' as const,
|
|
517
|
-
name: 'calculator',
|
|
518
|
-
arguments: { expression: 123 }, // should be string
|
|
519
|
-
};
|
|
520
|
-
|
|
521
|
-
try {
|
|
522
|
-
validateToolArguments(tool, toolCall);
|
|
523
|
-
expect.fail('Should have thrown error');
|
|
524
|
-
} catch (error) {
|
|
525
|
-
expect(error).toBeInstanceOf(Error);
|
|
526
|
-
expect((error as Error).message).toContain('calculator');
|
|
527
|
-
}
|
|
528
|
-
});
|
|
529
|
-
});
|
|
530
|
-
|
|
531
|
-
describe('Edge cases', () => {
|
|
532
|
-
it('should handle empty object schema', () => {
|
|
533
|
-
const tool: Tool = {
|
|
534
|
-
name: 'test',
|
|
535
|
-
description: 'Test tool',
|
|
536
|
-
parameters: Type.Object({}),
|
|
537
|
-
};
|
|
538
|
-
|
|
539
|
-
const toolCall = {
|
|
540
|
-
type: 'toolCall' as const,
|
|
541
|
-
name: 'test',
|
|
542
|
-
arguments: {},
|
|
543
|
-
};
|
|
544
|
-
|
|
545
|
-
const result = validateToolArguments(tool, toolCall);
|
|
546
|
-
expect(result).toEqual({});
|
|
547
|
-
});
|
|
548
|
-
|
|
549
|
-
it('should handle boolean fields', () => {
|
|
550
|
-
const tool: Tool = {
|
|
551
|
-
name: 'test',
|
|
552
|
-
description: 'Test tool',
|
|
553
|
-
parameters: Type.Object({
|
|
554
|
-
enabled: Type.Boolean(),
|
|
555
|
-
}),
|
|
556
|
-
};
|
|
557
|
-
|
|
558
|
-
const toolCall = {
|
|
559
|
-
type: 'toolCall' as const,
|
|
560
|
-
name: 'test',
|
|
561
|
-
arguments: { enabled: true },
|
|
562
|
-
};
|
|
563
|
-
|
|
564
|
-
const result = validateToolArguments(tool, toolCall);
|
|
565
|
-
expect(result).toEqual({ enabled: true });
|
|
566
|
-
});
|
|
567
|
-
|
|
568
|
-
it('should handle null values when allowed', () => {
|
|
569
|
-
const tool: Tool = {
|
|
570
|
-
name: 'test',
|
|
571
|
-
description: 'Test tool',
|
|
572
|
-
parameters: Type.Object({
|
|
573
|
-
value: Type.Union([Type.String(), Type.Null()]),
|
|
574
|
-
}),
|
|
575
|
-
};
|
|
576
|
-
|
|
577
|
-
const toolCall = {
|
|
578
|
-
type: 'toolCall' as const,
|
|
579
|
-
name: 'test',
|
|
580
|
-
arguments: { value: null },
|
|
581
|
-
};
|
|
582
|
-
|
|
583
|
-
const result = validateToolArguments(tool, toolCall);
|
|
584
|
-
expect(result).toEqual({ value: null });
|
|
585
|
-
});
|
|
586
|
-
|
|
587
|
-
it('should handle multiple levels of nested optional fields', () => {
|
|
588
|
-
const tool: Tool = {
|
|
589
|
-
name: 'test',
|
|
590
|
-
description: 'Test tool',
|
|
591
|
-
parameters: Type.Object({
|
|
592
|
-
level1: Type.Optional(
|
|
593
|
-
Type.Object({
|
|
594
|
-
level2: Type.Optional(
|
|
595
|
-
Type.Object({
|
|
596
|
-
value: Type.String(),
|
|
597
|
-
})
|
|
598
|
-
),
|
|
599
|
-
})
|
|
600
|
-
),
|
|
601
|
-
}),
|
|
602
|
-
};
|
|
603
|
-
|
|
604
|
-
const toolCall = {
|
|
605
|
-
type: 'toolCall' as const,
|
|
606
|
-
name: 'test',
|
|
607
|
-
arguments: {},
|
|
608
|
-
};
|
|
609
|
-
|
|
610
|
-
const result = validateToolArguments(tool, toolCall);
|
|
611
|
-
expect(result).toEqual({});
|
|
612
|
-
});
|
|
613
|
-
});
|
|
614
|
-
});
|
package/tsconfig.json
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2020",
|
|
4
|
-
"module": "CommonJS",
|
|
5
|
-
"lib": ["ES2020", "ES2022.Intl", "DOM"],
|
|
6
|
-
"moduleResolution": "node",
|
|
7
|
-
"resolveJsonModule": true,
|
|
8
|
-
"allowJs": true,
|
|
9
|
-
"strict": true,
|
|
10
|
-
"esModuleInterop": true,
|
|
11
|
-
"skipLibCheck": true,
|
|
12
|
-
"forceConsistentCasingInFileNames": true,
|
|
13
|
-
"declaration": true,
|
|
14
|
-
"declarationMap": true,
|
|
15
|
-
"sourceMap": true,
|
|
16
|
-
"outDir": "./dist",
|
|
17
|
-
"rootDir": "./src"
|
|
18
|
-
},
|
|
19
|
-
"include": ["src"],
|
|
20
|
-
"exclude": ["node_modules", "dist"]
|
|
21
|
-
}
|