@strands-agents/sdk 0.2.2 → 0.4.0
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/README.md +43 -0
- package/dist/src/__fixtures__/mock-hook-provider.d.ts +3 -7
- package/dist/src/__fixtures__/mock-hook-provider.d.ts.map +1 -1
- package/dist/src/__fixtures__/mock-hook-provider.js +3 -9
- package/dist/src/__fixtures__/mock-hook-provider.js.map +1 -1
- package/dist/src/__fixtures__/mock-message-model.d.ts +8 -2
- package/dist/src/__fixtures__/mock-message-model.d.ts.map +1 -1
- package/dist/src/__fixtures__/mock-message-model.js +1 -0
- package/dist/src/__fixtures__/mock-message-model.js.map +1 -1
- package/dist/src/__fixtures__/mock-storage-provider.d.ts +37 -0
- package/dist/src/__fixtures__/mock-storage-provider.d.ts.map +1 -0
- package/dist/src/__fixtures__/mock-storage-provider.js +105 -0
- package/dist/src/__fixtures__/mock-storage-provider.js.map +1 -0
- package/dist/src/__fixtures__/slim-types.d.ts +50 -0
- package/dist/src/__fixtures__/slim-types.d.ts.map +1 -0
- package/dist/src/__fixtures__/slim-types.js +6 -0
- package/dist/src/__fixtures__/slim-types.js.map +1 -0
- package/dist/src/__fixtures__/tool-helpers.d.ts +8 -3
- package/dist/src/__fixtures__/tool-helpers.d.ts.map +1 -1
- package/dist/src/__fixtures__/tool-helpers.js +1 -1
- package/dist/src/__fixtures__/tool-helpers.js.map +1 -1
- package/dist/src/agent/__tests__/agent.hook.test.js +18 -18
- package/dist/src/agent/__tests__/agent.hook.test.js.map +1 -1
- package/dist/src/agent/__tests__/agent.test.js +124 -8
- package/dist/src/agent/__tests__/agent.test.js.map +1 -1
- package/dist/src/agent/__tests__/printer.test.js +5 -9
- package/dist/src/agent/__tests__/printer.test.js.map +1 -1
- package/dist/src/agent/__tests__/snapshot.test.d.ts +2 -0
- package/dist/src/agent/__tests__/snapshot.test.d.ts.map +1 -0
- package/dist/src/agent/__tests__/snapshot.test.js +249 -0
- package/dist/src/agent/__tests__/snapshot.test.js.map +1 -0
- package/dist/src/agent/__tests__/state.test.js +19 -0
- package/dist/src/agent/__tests__/state.test.js.map +1 -1
- package/dist/src/agent/agent.d.ts +19 -4
- package/dist/src/agent/agent.d.ts.map +1 -1
- package/dist/src/agent/agent.js +104 -34
- package/dist/src/agent/agent.js.map +1 -1
- package/dist/src/agent/printer.d.ts +4 -0
- package/dist/src/agent/printer.d.ts.map +1 -1
- package/dist/src/agent/printer.js +18 -6
- package/dist/src/agent/printer.js.map +1 -1
- package/dist/src/agent/snapshot.d.ts +132 -0
- package/dist/src/agent/snapshot.d.ts.map +1 -0
- package/dist/src/agent/snapshot.js +151 -0
- package/dist/src/agent/snapshot.js.map +1 -0
- package/dist/src/agent/state.d.ts +14 -1
- package/dist/src/agent/state.d.ts.map +1 -1
- package/dist/src/agent/state.js +21 -0
- package/dist/src/agent/state.js.map +1 -1
- package/dist/src/errors.d.ts +15 -0
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +18 -0
- package/dist/src/errors.js.map +1 -1
- package/dist/src/hooks/__tests__/events.test.js +102 -21
- package/dist/src/hooks/__tests__/events.test.js.map +1 -1
- package/dist/src/hooks/events.d.ts +156 -22
- package/dist/src/hooks/events.d.ts.map +1 -1
- package/dist/src/hooks/events.js +158 -18
- package/dist/src/hooks/events.js.map +1 -1
- package/dist/src/hooks/index.d.ts +12 -4
- package/dist/src/hooks/index.d.ts.map +1 -1
- package/dist/src/hooks/index.js +11 -3
- package/dist/src/hooks/index.js.map +1 -1
- package/dist/src/hooks/registry.d.ts +5 -5
- package/dist/src/hooks/registry.d.ts.map +1 -1
- package/dist/src/hooks/registry.js.map +1 -1
- package/dist/src/hooks/types.d.ts +5 -5
- package/dist/src/hooks/types.d.ts.map +1 -1
- package/dist/src/index.d.ts +4 -2
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +4 -3
- package/dist/src/index.js.map +1 -1
- package/dist/src/models/__tests__/anthropic.test.js +31 -42
- package/dist/src/models/__tests__/anthropic.test.js.map +1 -1
- package/dist/src/models/__tests__/bedrock.test.js +70 -107
- package/dist/src/models/__tests__/bedrock.test.js.map +1 -1
- package/dist/src/models/__tests__/gemini.test.js +438 -141
- package/dist/src/models/__tests__/gemini.test.js.map +1 -1
- package/dist/src/models/__tests__/model.test.js +14 -13
- package/dist/src/models/__tests__/model.test.js.map +1 -1
- package/dist/src/models/__tests__/openai.test.js +73 -83
- package/dist/src/models/__tests__/openai.test.js.map +1 -1
- package/dist/src/models/gemini/adapters.d.ts +2 -1
- package/dist/src/models/gemini/adapters.d.ts.map +1 -1
- package/dist/src/models/gemini/adapters.js +97 -10
- package/dist/src/models/gemini/adapters.js.map +1 -1
- package/dist/src/models/gemini/model.d.ts.map +1 -1
- package/dist/src/models/gemini/model.js +37 -1
- package/dist/src/models/gemini/model.js.map +1 -1
- package/dist/src/models/gemini/types.d.ts +9 -1
- package/dist/src/models/gemini/types.d.ts.map +1 -1
- package/dist/src/models/model.d.ts.map +1 -1
- package/dist/src/models/model.js +4 -0
- package/dist/src/models/model.js.map +1 -1
- package/dist/src/models/streaming.d.ts +13 -0
- package/dist/src/models/streaming.d.ts.map +1 -1
- package/dist/src/models/streaming.js +17 -0
- package/dist/src/models/streaming.js.map +1 -1
- package/dist/src/multiagent/__tests__/nodes.test.d.ts +2 -0
- package/dist/src/multiagent/__tests__/nodes.test.d.ts.map +1 -0
- package/dist/src/multiagent/__tests__/nodes.test.js +98 -0
- package/dist/src/multiagent/__tests__/nodes.test.js.map +1 -0
- package/dist/src/multiagent/events.d.ts +24 -0
- package/dist/src/multiagent/events.d.ts.map +1 -0
- package/dist/src/multiagent/events.js +19 -0
- package/dist/src/multiagent/events.js.map +1 -0
- package/dist/src/multiagent/index.d.ts +11 -0
- package/dist/src/multiagent/index.d.ts.map +1 -0
- package/dist/src/multiagent/index.js +7 -0
- package/dist/src/multiagent/index.js.map +1 -0
- package/dist/src/multiagent/nodes.d.ts +75 -0
- package/dist/src/multiagent/nodes.d.ts.map +1 -0
- package/dist/src/multiagent/nodes.js +101 -0
- package/dist/src/multiagent/nodes.js.map +1 -0
- package/dist/src/multiagent/state.d.ts +55 -0
- package/dist/src/multiagent/state.d.ts.map +1 -0
- package/dist/src/multiagent/state.js +43 -0
- package/dist/src/multiagent/state.js.map +1 -0
- package/dist/src/multiagent/types.d.ts +5 -0
- package/dist/src/multiagent/types.d.ts.map +1 -0
- package/dist/src/multiagent/types.js +2 -0
- package/dist/src/multiagent/types.js.map +1 -0
- package/dist/src/registry/tool-registry.d.ts +2 -1
- package/dist/src/registry/tool-registry.d.ts.map +1 -1
- package/dist/src/registry/tool-registry.js +4 -2
- package/dist/src/registry/tool-registry.js.map +1 -1
- package/dist/src/session/__tests__/file-storage.test.node.d.ts +2 -0
- package/dist/src/session/__tests__/file-storage.test.node.d.ts.map +1 -0
- package/dist/src/session/__tests__/file-storage.test.node.js +218 -0
- package/dist/src/session/__tests__/file-storage.test.node.js.map +1 -0
- package/dist/src/session/__tests__/s3-storage.test.node.d.ts +2 -0
- package/dist/src/session/__tests__/s3-storage.test.node.d.ts.map +1 -0
- package/dist/src/session/__tests__/s3-storage.test.node.js +375 -0
- package/dist/src/session/__tests__/s3-storage.test.node.js.map +1 -0
- package/dist/src/session/__tests__/validation.test.d.ts +2 -0
- package/dist/src/session/__tests__/validation.test.d.ts.map +1 -0
- package/dist/src/session/__tests__/validation.test.js +20 -0
- package/dist/src/session/__tests__/validation.test.js.map +1 -0
- package/dist/src/session/file-storage.d.ts +79 -0
- package/dist/src/session/file-storage.d.ts.map +1 -0
- package/dist/src/session/file-storage.js +144 -0
- package/dist/src/session/file-storage.js.map +1 -0
- package/dist/src/session/index.d.ts +19 -0
- package/dist/src/session/index.d.ts.map +1 -0
- package/dist/src/session/index.js +18 -0
- package/dist/src/session/index.js.map +1 -0
- package/dist/src/session/s3-storage.d.ts +93 -0
- package/dist/src/session/s3-storage.d.ts.map +1 -0
- package/dist/src/session/s3-storage.js +150 -0
- package/dist/src/session/s3-storage.js.map +1 -0
- package/dist/src/session/storage.d.ts +91 -0
- package/dist/src/session/storage.d.ts.map +1 -0
- package/dist/src/session/storage.js +2 -0
- package/dist/src/session/storage.js.map +1 -0
- package/dist/src/session/types.d.ts +49 -0
- package/dist/src/session/types.d.ts.map +1 -0
- package/dist/src/session/types.js +2 -0
- package/dist/src/session/types.js.map +1 -0
- package/dist/src/session/validation.d.ts +10 -0
- package/dist/src/session/validation.d.ts.map +1 -0
- package/dist/src/session/validation.js +16 -0
- package/dist/src/session/validation.js.map +1 -0
- package/dist/src/structured-output/__tests__/context.test.d.ts +2 -0
- package/dist/src/structured-output/__tests__/context.test.d.ts.map +1 -0
- package/dist/src/structured-output/__tests__/context.test.js +201 -0
- package/dist/src/structured-output/__tests__/context.test.js.map +1 -0
- package/dist/src/structured-output/__tests__/exceptions.test.d.ts +2 -0
- package/dist/src/structured-output/__tests__/exceptions.test.d.ts.map +1 -0
- package/dist/src/structured-output/__tests__/exceptions.test.js +103 -0
- package/dist/src/structured-output/__tests__/exceptions.test.js.map +1 -0
- package/dist/src/structured-output/__tests__/tool.test.d.ts +2 -0
- package/dist/src/structured-output/__tests__/tool.test.d.ts.map +1 -0
- package/dist/src/structured-output/__tests__/tool.test.js +256 -0
- package/dist/src/structured-output/__tests__/tool.test.js.map +1 -0
- package/dist/src/structured-output/__tests__/utils.test.d.ts +2 -0
- package/dist/src/structured-output/__tests__/utils.test.d.ts.map +1 -0
- package/dist/src/structured-output/__tests__/utils.test.js +183 -0
- package/dist/src/structured-output/__tests__/utils.test.js.map +1 -0
- package/dist/src/structured-output/context.d.ts +91 -0
- package/dist/src/structured-output/context.d.ts.map +1 -0
- package/dist/src/structured-output/context.js +112 -0
- package/dist/src/structured-output/context.js.map +1 -0
- package/dist/src/structured-output/exceptions.d.ts +18 -0
- package/dist/src/structured-output/exceptions.d.ts.map +1 -0
- package/dist/src/structured-output/exceptions.js +28 -0
- package/dist/src/structured-output/exceptions.js.map +1 -0
- package/dist/src/structured-output/tool.d.ts +33 -0
- package/dist/src/structured-output/tool.d.ts.map +1 -0
- package/dist/src/structured-output/tool.js +73 -0
- package/dist/src/structured-output/tool.js.map +1 -0
- package/dist/src/structured-output/utils.d.ts +23 -0
- package/dist/src/structured-output/utils.d.ts.map +1 -0
- package/dist/src/structured-output/utils.js +104 -0
- package/dist/src/structured-output/utils.js.map +1 -0
- package/dist/src/tools/zod-tool.d.ts.map +1 -1
- package/dist/src/tools/zod-tool.js +2 -5
- package/dist/src/tools/zod-tool.js.map +1 -1
- package/dist/src/tsconfig.tsbuildinfo +1 -1
- package/dist/src/types/__tests__/media.test.js +216 -1
- package/dist/src/types/__tests__/media.test.js.map +1 -1
- package/dist/src/types/__tests__/messages.test.js +193 -4
- package/dist/src/types/__tests__/messages.test.js.map +1 -1
- package/dist/src/types/agent.d.ts +13 -7
- package/dist/src/types/agent.d.ts.map +1 -1
- package/dist/src/types/agent.js +8 -1
- package/dist/src/types/agent.js.map +1 -1
- package/dist/src/types/json.d.ts +61 -0
- package/dist/src/types/json.d.ts.map +1 -1
- package/dist/src/types/json.js +24 -0
- package/dist/src/types/json.js.map +1 -1
- package/dist/src/types/media.d.ts +84 -4
- package/dist/src/types/media.d.ts.map +1 -1
- package/dist/src/types/media.js +194 -0
- package/dist/src/types/media.js.map +1 -1
- package/dist/src/types/messages.d.ts +158 -9
- package/dist/src/types/messages.d.ts.map +1 -1
- package/dist/src/types/messages.js +242 -7
- package/dist/src/types/messages.js.map +1 -1
- package/dist/src/types/serializable.d.ts +31 -0
- package/dist/src/types/serializable.d.ts.map +1 -0
- package/dist/src/types/serializable.js +19 -0
- package/dist/src/types/serializable.js.map +1 -0
- package/dist/src/utils/zod.d.ts +11 -0
- package/dist/src/utils/zod.d.ts.map +1 -0
- package/dist/src/utils/zod.js +14 -0
- package/dist/src/utils/zod.js.map +1 -0
- package/dist/src/vended-tools/bash/__tests__/bash.test.node.d.ts +2 -0
- package/dist/src/vended-tools/bash/__tests__/bash.test.node.d.ts.map +1 -0
- package/dist/src/vended-tools/bash/__tests__/{bash.test.js → bash.test.node.js} +3 -4
- package/dist/src/vended-tools/bash/__tests__/bash.test.node.js.map +1 -0
- package/dist/src/vended-tools/file_editor/__tests__/file-editor.test.node.d.ts +2 -0
- package/dist/src/vended-tools/file_editor/__tests__/file-editor.test.node.d.ts.map +1 -0
- package/dist/src/vended-tools/file_editor/__tests__/{file-editor.test.js → file-editor.test.node.js} +1 -1
- package/dist/src/vended-tools/file_editor/__tests__/file-editor.test.node.js.map +1 -0
- package/package.json +7 -2
- package/dist/src/vended-tools/bash/__tests__/bash.test.d.ts +0 -2
- package/dist/src/vended-tools/bash/__tests__/bash.test.d.ts.map +0 -1
- package/dist/src/vended-tools/bash/__tests__/bash.test.js.map +0 -1
- package/dist/src/vended-tools/file_editor/__tests__/file-editor.test.d.ts +0 -2
- package/dist/src/vended-tools/file_editor/__tests__/file-editor.test.d.ts.map +0 -1
- package/dist/src/vended-tools/file_editor/__tests__/file-editor.test.js.map +0 -1
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { convertSchemaToToolSpec, getSchemaDescription } from '../utils.js';
|
|
4
|
+
import { StructuredOutputException } from '../exceptions.js';
|
|
5
|
+
describe('convertSchemaToToolSpec', () => {
|
|
6
|
+
it('converts basic schema to tool spec', () => {
|
|
7
|
+
const schema = z.object({
|
|
8
|
+
name: z.string(),
|
|
9
|
+
age: z.number(),
|
|
10
|
+
});
|
|
11
|
+
const toolSpec = convertSchemaToToolSpec(schema, 'TestTool');
|
|
12
|
+
expect(toolSpec.name).toBe('TestTool');
|
|
13
|
+
expect(toolSpec.description).toContain('StructuredOutputTool');
|
|
14
|
+
expect(toolSpec.inputSchema).toStrictEqual({
|
|
15
|
+
type: 'object',
|
|
16
|
+
properties: {
|
|
17
|
+
name: { type: 'string' },
|
|
18
|
+
age: { type: 'number' },
|
|
19
|
+
},
|
|
20
|
+
required: ['name', 'age'],
|
|
21
|
+
additionalProperties: false,
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
it('includes schema description in tool spec', () => {
|
|
25
|
+
const schema = z
|
|
26
|
+
.object({
|
|
27
|
+
name: z.string(),
|
|
28
|
+
})
|
|
29
|
+
.describe('A person object');
|
|
30
|
+
const toolSpec = convertSchemaToToolSpec(schema, 'TestTool');
|
|
31
|
+
expect(toolSpec.description).toContain('A person object');
|
|
32
|
+
});
|
|
33
|
+
it('throws error for schema with refinements', () => {
|
|
34
|
+
const schema = z.object({
|
|
35
|
+
name: z.string().refine((val) => val.length > 0, 'Name cannot be empty'),
|
|
36
|
+
});
|
|
37
|
+
expect(() => convertSchemaToToolSpec(schema, 'TestTool')).toThrow(StructuredOutputException);
|
|
38
|
+
expect(() => convertSchemaToToolSpec(schema, 'TestTool')).toThrow('Zod refinements and transforms are not supported');
|
|
39
|
+
});
|
|
40
|
+
it('throws error for schema with transforms', () => {
|
|
41
|
+
const schema = z.string().transform((val) => val.toUpperCase());
|
|
42
|
+
expect(() => convertSchemaToToolSpec(schema, 'TestTool')).toThrow(StructuredOutputException);
|
|
43
|
+
expect(() => convertSchemaToToolSpec(schema, 'TestTool')).toThrow('Zod refinements and transforms are not supported');
|
|
44
|
+
});
|
|
45
|
+
it('throws error for schema with superRefine', () => {
|
|
46
|
+
const schema = z.object({ name: z.string() }).superRefine((val, ctx) => {
|
|
47
|
+
if (val.name.length === 0) {
|
|
48
|
+
ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Name required' });
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
expect(() => convertSchemaToToolSpec(schema, 'TestTool')).toThrow(StructuredOutputException);
|
|
52
|
+
});
|
|
53
|
+
it('accepts schema with basic validations', () => {
|
|
54
|
+
const schema = z.object({
|
|
55
|
+
name: z.string().min(1).max(100),
|
|
56
|
+
age: z.number().int().positive(),
|
|
57
|
+
email: z.string().email(),
|
|
58
|
+
});
|
|
59
|
+
const toolSpec = convertSchemaToToolSpec(schema, 'TestTool');
|
|
60
|
+
expect(toolSpec.inputSchema).toMatchObject({
|
|
61
|
+
type: 'object',
|
|
62
|
+
properties: {
|
|
63
|
+
name: {
|
|
64
|
+
type: 'string',
|
|
65
|
+
minLength: 1,
|
|
66
|
+
maxLength: 100,
|
|
67
|
+
},
|
|
68
|
+
age: {
|
|
69
|
+
type: 'integer',
|
|
70
|
+
},
|
|
71
|
+
email: {
|
|
72
|
+
type: 'string',
|
|
73
|
+
format: 'email',
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
required: ['name', 'age', 'email'],
|
|
77
|
+
additionalProperties: false,
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
it('throws error for nested schema with refinements', () => {
|
|
81
|
+
const schema = z.object({
|
|
82
|
+
user: z.object({
|
|
83
|
+
name: z.string().refine((val) => val.length > 0),
|
|
84
|
+
}),
|
|
85
|
+
});
|
|
86
|
+
expect(() => convertSchemaToToolSpec(schema, 'TestTool')).toThrow(StructuredOutputException);
|
|
87
|
+
});
|
|
88
|
+
it('accepts nested schema without refinements', () => {
|
|
89
|
+
const schema = z.object({
|
|
90
|
+
user: z.object({
|
|
91
|
+
name: z.string(),
|
|
92
|
+
age: z.number(),
|
|
93
|
+
}),
|
|
94
|
+
items: z.array(z.string()),
|
|
95
|
+
});
|
|
96
|
+
const toolSpec = convertSchemaToToolSpec(schema, 'TestTool');
|
|
97
|
+
expect(toolSpec.inputSchema).toStrictEqual({
|
|
98
|
+
type: 'object',
|
|
99
|
+
properties: {
|
|
100
|
+
user: {
|
|
101
|
+
type: 'object',
|
|
102
|
+
properties: {
|
|
103
|
+
name: { type: 'string' },
|
|
104
|
+
age: { type: 'number' },
|
|
105
|
+
},
|
|
106
|
+
required: ['name', 'age'],
|
|
107
|
+
additionalProperties: false,
|
|
108
|
+
},
|
|
109
|
+
items: {
|
|
110
|
+
type: 'array',
|
|
111
|
+
items: { type: 'string' },
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
required: ['user', 'items'],
|
|
115
|
+
additionalProperties: false,
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
it('throws error for array with refinements', () => {
|
|
119
|
+
const schema = z.array(z.string().refine((val) => val.length > 0));
|
|
120
|
+
expect(() => convertSchemaToToolSpec(schema, 'TestTool')).toThrow(StructuredOutputException);
|
|
121
|
+
});
|
|
122
|
+
it('accepts union types', () => {
|
|
123
|
+
const schema = z.union([z.string(), z.number()]);
|
|
124
|
+
expect(() => convertSchemaToToolSpec(schema, 'TestTool')).not.toThrow();
|
|
125
|
+
});
|
|
126
|
+
it('accepts optional fields', () => {
|
|
127
|
+
const schema = z.object({
|
|
128
|
+
name: z.string(),
|
|
129
|
+
age: z.number().optional(),
|
|
130
|
+
});
|
|
131
|
+
const toolSpec = convertSchemaToToolSpec(schema, 'TestTool');
|
|
132
|
+
expect(toolSpec.inputSchema).toStrictEqual({
|
|
133
|
+
type: 'object',
|
|
134
|
+
properties: {
|
|
135
|
+
name: { type: 'string' },
|
|
136
|
+
age: { type: 'number' },
|
|
137
|
+
},
|
|
138
|
+
required: ['name'],
|
|
139
|
+
additionalProperties: false,
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
it('throws error for deeply nested refinements', () => {
|
|
143
|
+
const schema = z.object({
|
|
144
|
+
level1: z.object({
|
|
145
|
+
level2: z.object({
|
|
146
|
+
level3: z.string().refine((val) => val.length > 0),
|
|
147
|
+
}),
|
|
148
|
+
}),
|
|
149
|
+
});
|
|
150
|
+
expect(() => convertSchemaToToolSpec(schema, 'TestTool')).toThrow(StructuredOutputException);
|
|
151
|
+
});
|
|
152
|
+
it('throws error for refinements in union types', () => {
|
|
153
|
+
const schema = z.union([z.string().refine((val) => val.length > 0), z.number()]);
|
|
154
|
+
expect(() => convertSchemaToToolSpec(schema, 'TestTool')).toThrow(StructuredOutputException);
|
|
155
|
+
});
|
|
156
|
+
it('throws error for refinements in array items', () => {
|
|
157
|
+
const schema = z.object({
|
|
158
|
+
items: z.array(z.object({
|
|
159
|
+
name: z.string().refine((val) => val.length > 0),
|
|
160
|
+
})),
|
|
161
|
+
});
|
|
162
|
+
expect(() => convertSchemaToToolSpec(schema, 'TestTool')).toThrow(StructuredOutputException);
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
describe('getSchemaDescription', () => {
|
|
166
|
+
it('returns description from schema metadata', () => {
|
|
167
|
+
const schema = z.object({ name: z.string() }).describe('Test description');
|
|
168
|
+
const description = getSchemaDescription(schema);
|
|
169
|
+
expect(description).toBe('Test description');
|
|
170
|
+
});
|
|
171
|
+
it('returns empty string when no description', () => {
|
|
172
|
+
const schema = z.object({ name: z.string() });
|
|
173
|
+
const description = getSchemaDescription(schema);
|
|
174
|
+
expect(description).toBe('');
|
|
175
|
+
});
|
|
176
|
+
it('returns description from _def', () => {
|
|
177
|
+
const schema = z.object({ name: z.string() });
|
|
178
|
+
schema._def.description = 'Description in _def';
|
|
179
|
+
const description = getSchemaDescription(schema);
|
|
180
|
+
expect(description).toBe('Description in _def');
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
//# sourceMappingURL=utils.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.test.js","sourceRoot":"","sources":["../../../../src/structured-output/__tests__/utils.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAA;AAC7C,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,EAAE,uBAAuB,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAA;AAC3E,OAAO,EAAE,yBAAyB,EAAE,MAAM,kBAAkB,CAAA;AAE5D,QAAQ,CAAC,yBAAyB,EAAE,GAAG,EAAE;IACvC,EAAE,CAAC,oCAAoC,EAAE,GAAG,EAAE;QAC5C,MAAM,MAAM,GAAG,CAAC,CAAC,MAAM,CA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 'zod';
|
|
2
|
+
import type { ToolRegistry } from '../registry/tool-registry.js';
|
|
3
|
+
/**
|
|
4
|
+
* Interface for structured output context operations.
|
|
5
|
+
* Allows for null object pattern implementation.
|
|
6
|
+
*/
|
|
7
|
+
export interface IStructuredOutputContext {
|
|
8
|
+
registerTool(registry: ToolRegistry): void;
|
|
9
|
+
storeResult(toolUseId: string, result: unknown): void;
|
|
10
|
+
hasResult(): boolean;
|
|
11
|
+
getResult(): unknown | undefined;
|
|
12
|
+
getToolName(): string;
|
|
13
|
+
cleanup(registry: ToolRegistry): void;
|
|
14
|
+
readonly isEnabled: boolean;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Null object implementation that does nothing.
|
|
18
|
+
* Used when no structured output schema is provided.
|
|
19
|
+
*/
|
|
20
|
+
export declare class NullStructuredOutputContext implements IStructuredOutputContext {
|
|
21
|
+
readonly isEnabled = false;
|
|
22
|
+
registerTool(_registry: ToolRegistry): void;
|
|
23
|
+
storeResult(_toolUseId: string, _result: unknown): void;
|
|
24
|
+
hasResult(): boolean;
|
|
25
|
+
getResult(): unknown | undefined;
|
|
26
|
+
getToolName(): string;
|
|
27
|
+
cleanup(_registry: ToolRegistry): void;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Context for managing structured output tool lifecycle per-invocation.
|
|
31
|
+
* Handles tool registration, result storage, and cleanup.
|
|
32
|
+
*/
|
|
33
|
+
export declare class StructuredOutputContext implements IStructuredOutputContext {
|
|
34
|
+
readonly isEnabled = true;
|
|
35
|
+
private _schema;
|
|
36
|
+
private _tool?;
|
|
37
|
+
private _result;
|
|
38
|
+
/**
|
|
39
|
+
* Creates a new StructuredOutputContext.
|
|
40
|
+
*
|
|
41
|
+
* @param schema - Zod schema for structured output
|
|
42
|
+
*/
|
|
43
|
+
constructor(schema: z.ZodSchema);
|
|
44
|
+
/**
|
|
45
|
+
* Registers the structured output tool with the tool registry.
|
|
46
|
+
*
|
|
47
|
+
* @param registry - The tool registry to register with
|
|
48
|
+
*/
|
|
49
|
+
registerTool(registry: ToolRegistry): void;
|
|
50
|
+
/**
|
|
51
|
+
* Stores the validated result from the structured output tool.
|
|
52
|
+
* If called multiple times, only the latest result is kept.
|
|
53
|
+
*
|
|
54
|
+
* @param toolUseId - The tool use ID (unused, kept for interface compatibility)
|
|
55
|
+
* @param result - The validated result
|
|
56
|
+
*/
|
|
57
|
+
storeResult(toolUseId: string, result: unknown): void;
|
|
58
|
+
/**
|
|
59
|
+
* Checks if a result has been stored.
|
|
60
|
+
*
|
|
61
|
+
* @returns true if a result has been stored
|
|
62
|
+
*/
|
|
63
|
+
hasResult(): boolean;
|
|
64
|
+
/**
|
|
65
|
+
* Retrieves the stored result, if available.
|
|
66
|
+
*
|
|
67
|
+
* @returns The validated result or undefined if not yet stored
|
|
68
|
+
*/
|
|
69
|
+
getResult(): unknown | undefined;
|
|
70
|
+
/**
|
|
71
|
+
* Gets the tool name for forcing.
|
|
72
|
+
*
|
|
73
|
+
* @returns The tool name or 'strands_structured_output' as fallback
|
|
74
|
+
*/
|
|
75
|
+
getToolName(): string;
|
|
76
|
+
/**
|
|
77
|
+
* Cleans up the structured output tool by removing it from the registry.
|
|
78
|
+
* Should be called in a finally block to ensure cleanup happens regardless of success/failure.
|
|
79
|
+
*
|
|
80
|
+
* @param registry - The tool registry to clean up from
|
|
81
|
+
*/
|
|
82
|
+
cleanup(registry: ToolRegistry): void;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Factory function to create the appropriate context based on schema presence.
|
|
86
|
+
*
|
|
87
|
+
* @param schema - Optional Zod schema for structured output
|
|
88
|
+
* @returns StructuredOutputContext if schema provided, NullStructuredOutputContext otherwise
|
|
89
|
+
*/
|
|
90
|
+
export declare function createStructuredOutputContext(schema?: z.ZodSchema): IStructuredOutputContext;
|
|
91
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"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E,MAAM,EAAE,OAAO,GAAG,IAAI;IAIrD;;;;OAIG;IACH,SAAS,IAAI,OAAO;IAIpB;;;;OAIG;IACH,SAAS,IAAI,OAAO,GAAG,SAAS;IAIhC;;;;OAIG;IACH,WAAW,IAAI,MAAM;IAIrB;;;;;OAKG;IACH,OAAO,CAAC,QAAQ,EAAE,YAAY,GAAG,IAAI;CAMtC;AAED;;;;;GAKG;AACH,wBAAgB,6BAA6B,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,SAAS,GAAG,wBAAwB,CAE5F"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { StructuredOutputTool } from './tool.js';
|
|
3
|
+
/**
|
|
4
|
+
* Null object implementation that does nothing.
|
|
5
|
+
* Used when no structured output schema is provided.
|
|
6
|
+
*/
|
|
7
|
+
export class NullStructuredOutputContext {
|
|
8
|
+
isEnabled = false;
|
|
9
|
+
registerTool(_registry) {
|
|
10
|
+
// No-op
|
|
11
|
+
}
|
|
12
|
+
storeResult(_toolUseId, _result) {
|
|
13
|
+
// No-op
|
|
14
|
+
}
|
|
15
|
+
hasResult() {
|
|
16
|
+
return true; // Always "has result" to skip forcing logic
|
|
17
|
+
}
|
|
18
|
+
getResult() {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
getToolName() {
|
|
22
|
+
return 'strands_structured_output';
|
|
23
|
+
}
|
|
24
|
+
cleanup(_registry) {
|
|
25
|
+
// No-op
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Context for managing structured output tool lifecycle per-invocation.
|
|
30
|
+
* Handles tool registration, result storage, and cleanup.
|
|
31
|
+
*/
|
|
32
|
+
export class StructuredOutputContext {
|
|
33
|
+
isEnabled = true;
|
|
34
|
+
_schema;
|
|
35
|
+
// The `| undefined` is needed for `exactOptionalPropertyTypes` since we assign undefined in cleanup()
|
|
36
|
+
_tool;
|
|
37
|
+
_result = undefined;
|
|
38
|
+
/**
|
|
39
|
+
* Creates a new StructuredOutputContext.
|
|
40
|
+
*
|
|
41
|
+
* @param schema - Zod schema for structured output
|
|
42
|
+
*/
|
|
43
|
+
constructor(schema) {
|
|
44
|
+
this._schema = schema;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Registers the structured output tool with the tool registry.
|
|
48
|
+
*
|
|
49
|
+
* @param registry - The tool registry to register with
|
|
50
|
+
*/
|
|
51
|
+
registerTool(registry) {
|
|
52
|
+
this._tool = new StructuredOutputTool(this._schema, 'strands_structured_output', this);
|
|
53
|
+
// Register tool (will be removed in cleanup)
|
|
54
|
+
registry.add(this._tool);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Stores the validated result from the structured output tool.
|
|
58
|
+
* If called multiple times, only the latest result is kept.
|
|
59
|
+
*
|
|
60
|
+
* @param toolUseId - The tool use ID (unused, kept for interface compatibility)
|
|
61
|
+
* @param result - The validated result
|
|
62
|
+
*/
|
|
63
|
+
storeResult(toolUseId, result) {
|
|
64
|
+
this._result = result;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Checks if a result has been stored.
|
|
68
|
+
*
|
|
69
|
+
* @returns true if a result has been stored
|
|
70
|
+
*/
|
|
71
|
+
hasResult() {
|
|
72
|
+
return this._result !== undefined;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Retrieves the stored result, if available.
|
|
76
|
+
*
|
|
77
|
+
* @returns The validated result or undefined if not yet stored
|
|
78
|
+
*/
|
|
79
|
+
getResult() {
|
|
80
|
+
return this._result;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Gets the tool name for forcing.
|
|
84
|
+
*
|
|
85
|
+
* @returns The tool name or 'strands_structured_output' as fallback
|
|
86
|
+
*/
|
|
87
|
+
getToolName() {
|
|
88
|
+
return this._tool?.name ?? 'strands_structured_output';
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Cleans up the structured output tool by removing it from the registry.
|
|
92
|
+
* Should be called in a finally block to ensure cleanup happens regardless of success/failure.
|
|
93
|
+
*
|
|
94
|
+
* @param registry - The tool registry to clean up from
|
|
95
|
+
*/
|
|
96
|
+
cleanup(registry) {
|
|
97
|
+
if (this._tool) {
|
|
98
|
+
registry.removeByName(this._tool.name);
|
|
99
|
+
this._tool = undefined;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Factory function to create the appropriate context based on schema presence.
|
|
105
|
+
*
|
|
106
|
+
* @param schema - Optional Zod schema for structured output
|
|
107
|
+
* @returns StructuredOutputContext if schema provided, NullStructuredOutputContext otherwise
|
|
108
|
+
*/
|
|
109
|
+
export function createStructuredOutputContext(schema) {
|
|
110
|
+
return schema ? new StructuredOutputContext(schema) : new NullStructuredOutputContext();
|
|
111
|
+
}
|
|
112
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../../src/structured-output/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAEvB,OAAO,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAA;AAgBhD;;;GAGG;AACH,MAAM,OAAO,2BAA2B;IAC7B,SAAS,GAAG,KAAK,CAAA;IAE1B,YAAY,CAAC,SAAuB;QAClC,QAAQ;IACV,CAAC;IAED,WAAW,CAAC,UAAkB,EAAE,OAAgB;QAC9C,QAAQ;IACV,CAAC;IAED,SAAS;QACP,OAAO,IAAI,CAAA,CAAC,4CAA4C;IAC1D,CAAC;IAED,SAAS;QACP,OAAO,SAAS,CAAA;IAClB,CAAC;IAED,WAAW;QACT,OAAO,2BAA2B,CAAA;IACpC,CAAC;IAED,OAAO,CAAC,SAAuB;QAC7B,QAAQ;IACV,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,OAAO,uBAAuB;IACzB,SAAS,GAAG,IAAI,CA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 'zod';
|
|
2
|
+
/**
|
|
3
|
+
* Exception raised when the model fails to produce structured output.
|
|
4
|
+
* This is raised only when the LLM refuses to use the structured output tool
|
|
5
|
+
* even after being forced via toolChoice.
|
|
6
|
+
*/
|
|
7
|
+
export declare class StructuredOutputException extends Error {
|
|
8
|
+
constructor(message: string);
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Formats Zod validation errors into a human-readable bullet list.
|
|
12
|
+
* Used to provide LLM-friendly error feedback for retry attempts.
|
|
13
|
+
*
|
|
14
|
+
* @param issues - Array of Zod validation issues
|
|
15
|
+
* @returns Formatted error message with bullet points
|
|
16
|
+
*/
|
|
17
|
+
export declare function formatValidationErrors(issues: z.ZodIssue[]): string;
|
|
18
|
+
//# sourceMappingURL=exceptions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exceptions.d.ts","sourceRoot":"","sources":["../../../src/structured-output/exceptions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAE5B;;;;GAIG;AACH,qBAAa,yBAA0B,SAAQ,KAAK;gBACtC,OAAO,EAAE,MAAM;CAI5B;AAED;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,CAAC,CAAC,QAAQ,EAAE,GAAG,MAAM,CASnE"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Exception raised when the model fails to produce structured output.
|
|
3
|
+
* This is raised only when the LLM refuses to use the structured output tool
|
|
4
|
+
* even after being forced via toolChoice.
|
|
5
|
+
*/
|
|
6
|
+
export class StructuredOutputException extends Error {
|
|
7
|
+
constructor(message) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = 'StructuredOutputException';
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Formats Zod validation errors into a human-readable bullet list.
|
|
14
|
+
* Used to provide LLM-friendly error feedback for retry attempts.
|
|
15
|
+
*
|
|
16
|
+
* @param issues - Array of Zod validation issues
|
|
17
|
+
* @returns Formatted error message with bullet points
|
|
18
|
+
*/
|
|
19
|
+
export function formatValidationErrors(issues) {
|
|
20
|
+
const formatted = issues
|
|
21
|
+
.map((issue) => {
|
|
22
|
+
const path = issue.path.length > 0 ? issue.path.join('.') : 'root';
|
|
23
|
+
return `- Field '${path}': ${issue.message}`;
|
|
24
|
+
})
|
|
25
|
+
.join('\n');
|
|
26
|
+
return formatted;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=exceptions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exceptions.js","sourceRoot":"","sources":["../../../src/structured-output/exceptions.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,OAAO,yBAA0B,SAAQ,KAAK;IAClD,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,IAAI,CAAC,IAAI,GAAG,2BAA2B,CAAA;IACzC,CAAC;CACF;AAED;;;;;;GAMG;AACH,MAAM,UAAU,sBAAsB,CAAC,MAAoB;IACzD,MAAM,SAAS,GAAG,MAAM;SACrB,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE;QACb,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAA;QAClE,OAAO,YAAY,IAAI,MAAM,KAAK,CAAC,OAAO,EAAE,CAAA;IAC9C,CAAC,CAAC;SACD,IAAI,CAAC,IAAI,CAAC,CAAA;IAEb,OAAO,SAAS,CAAA;AAClB,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { Tool, type ToolContext, type ToolStreamGenerator } from '../tools/tool.js';
|
|
3
|
+
import type { ToolSpec } from '../tools/types.js';
|
|
4
|
+
import type { StructuredOutputContext } from './context.js';
|
|
5
|
+
/**
|
|
6
|
+
* Tool implementation that validates LLM output against a Zod schema.
|
|
7
|
+
* Provides validation feedback to the LLM for retry on failures.
|
|
8
|
+
*/
|
|
9
|
+
export declare class StructuredOutputTool extends Tool {
|
|
10
|
+
readonly name: string;
|
|
11
|
+
readonly description: string;
|
|
12
|
+
readonly toolSpec: ToolSpec;
|
|
13
|
+
private _schema;
|
|
14
|
+
private _context;
|
|
15
|
+
/**
|
|
16
|
+
* Creates a new StructuredOutputTool.
|
|
17
|
+
*
|
|
18
|
+
* @param schema - The Zod schema to validate against
|
|
19
|
+
* @param toolName - The name of the tool
|
|
20
|
+
* @param context - The structured output context for result storage
|
|
21
|
+
*/
|
|
22
|
+
constructor(schema: z.ZodSchema, toolName: string, context: StructuredOutputContext);
|
|
23
|
+
/**
|
|
24
|
+
* Executes the tool by validating input against the schema.
|
|
25
|
+
* On success, stores the validated result in context.
|
|
26
|
+
* On failure, returns formatted validation errors for LLM retry.
|
|
27
|
+
*
|
|
28
|
+
* @param toolContext - The tool execution context
|
|
29
|
+
* @returns Generator that returns a ToolResultBlock
|
|
30
|
+
*/
|
|
31
|
+
stream(toolContext: ToolContext): ToolStreamGenerator;
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=tool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool.d.ts","sourceRoot":"","sources":["../../../src/structured-output/tool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,EAAE,IAAI,EAAE,KAAK,WAAW,EAAE,KAAK,mBAAmB,EAAE,MAAM,kBAAkB,CAAA;AACnF,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAIjD,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAA;AAE3D;;;GAGG;AACH,qBAAa,oBAAqB,SAAQ,IAAI;IAC5C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAA;IAC5B,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAA;IAE3B,OAAO,CAAC,OAAO,CAAa;IAC5B,OAAO,CAAC,QAAQ,CAAyB;IAEzC;;;;;;OAMG;gBACS,MAAM,EAAE,CAAC,CAAC,SAAS,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,uBAAuB;IASnF;;;;;;;OAOG;IAGI,MAAM,CAAC,WAAW,EAAE,WAAW,GAAG,mBAAmB;CAmC7D"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { Tool } from '../tools/tool.js';
|
|
3
|
+
import { TextBlock, ToolResultBlock } from '../types/messages.js';
|
|
4
|
+
import { convertSchemaToToolSpec } from './utils.js';
|
|
5
|
+
import { formatValidationErrors } from './exceptions.js';
|
|
6
|
+
/**
|
|
7
|
+
* Tool implementation that validates LLM output against a Zod schema.
|
|
8
|
+
* Provides validation feedback to the LLM for retry on failures.
|
|
9
|
+
*/
|
|
10
|
+
export class StructuredOutputTool extends Tool {
|
|
11
|
+
name;
|
|
12
|
+
description;
|
|
13
|
+
toolSpec;
|
|
14
|
+
_schema;
|
|
15
|
+
_context;
|
|
16
|
+
/**
|
|
17
|
+
* Creates a new StructuredOutputTool.
|
|
18
|
+
*
|
|
19
|
+
* @param schema - The Zod schema to validate against
|
|
20
|
+
* @param toolName - The name of the tool
|
|
21
|
+
* @param context - The structured output context for result storage
|
|
22
|
+
*/
|
|
23
|
+
constructor(schema, toolName, context) {
|
|
24
|
+
super();
|
|
25
|
+
this._schema = schema;
|
|
26
|
+
this._context = context;
|
|
27
|
+
this.toolSpec = convertSchemaToToolSpec(schema, toolName);
|
|
28
|
+
this.name = this.toolSpec.name;
|
|
29
|
+
this.description = this.toolSpec.description;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Executes the tool by validating input against the schema.
|
|
33
|
+
* On success, stores the validated result in context.
|
|
34
|
+
* On failure, returns formatted validation errors for LLM retry.
|
|
35
|
+
*
|
|
36
|
+
* @param toolContext - The tool execution context
|
|
37
|
+
* @returns Generator that returns a ToolResultBlock
|
|
38
|
+
*/
|
|
39
|
+
// Validation is synchronous, so no streaming events are yielded - only the final result is returned
|
|
40
|
+
// eslint-disable-next-line require-yield
|
|
41
|
+
async *stream(toolContext) {
|
|
42
|
+
const { toolUse } = toolContext;
|
|
43
|
+
try {
|
|
44
|
+
// Validate input against schema
|
|
45
|
+
const validated = this._schema.parse(toolUse.input);
|
|
46
|
+
// Store validated result in context
|
|
47
|
+
this._context.storeResult(toolUse.toolUseId, validated);
|
|
48
|
+
// Return success result
|
|
49
|
+
return new ToolResultBlock({
|
|
50
|
+
toolUseId: toolUse.toolUseId,
|
|
51
|
+
status: 'success',
|
|
52
|
+
content: [new TextBlock(JSON.stringify(validated))],
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
// Handle validation errors
|
|
57
|
+
if (error instanceof z.ZodError) {
|
|
58
|
+
const formattedErrors = formatValidationErrors(error.issues);
|
|
59
|
+
const errorMessage = `Validation failed for ${this.name}. Please fix the following errors:\n${formattedErrors}`;
|
|
60
|
+
// Return error result with formatted validation feedback
|
|
61
|
+
return new ToolResultBlock({
|
|
62
|
+
toolUseId: toolUse.toolUseId,
|
|
63
|
+
status: 'error',
|
|
64
|
+
content: [new TextBlock(errorMessage)],
|
|
65
|
+
error: error,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
// Re-throw unexpected errors
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=tool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool.js","sourceRoot":"","sources":["../../../src/structured-output/tool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE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CAAC,SAAS;gBAC5B,MAAM,EAAE,SAAS;gBACjB,OAAO,EAAE,CAAC,IAAI,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,CAAC;aACpD,CAAC,CAAA;QACJ,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,2BAA2B;YAC3B,IAAI,KAAK,YAAY,CAAC,CAAC,QAAQ,EAAE,CAAC;gBAChC,MAAM,eAAe,GAAG,sBAAsB,CAAC,KAAK,CAAC,MAAM,CAAC,CAAA;gBAC5D,MAAM,YAAY,GAAG,yBAAyB,IAAI,CAAC,IAAI,uCAAuC,eAAe,EAAE,CAAA;gBAE/G,yDAAyD;gBACzD,OAAO,IAAI,eAAe,CAAC;oBACzB,SAAS,EAAE,OAAO,CAAC,SAAS;oBAC5B,MAAM,EAAE,OAAO;oBACf,OAAO,EAAE,CAAC,IAAI,SAAS,CAAC,YAAY,CAAC,CAAC;oBACtC,KAAK,EAAE,KAAK;iBACb,CAAC,CAAA;YACJ,CAAC;YAED,6BAA6B;YAC7B,MAAM,KAAK,CAAA;QACb,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import type { ToolSpec } from '../tools/types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Converts a Zod schema to a complete tool specification.
|
|
5
|
+
*
|
|
6
|
+
* Validates that the schema doesn't contain refinements or transforms, which cannot be
|
|
7
|
+
* properly represented in JSON Schema. Refinements are silently dropped by z.toJSONSchema(),
|
|
8
|
+
* creating a mismatch between what the LLM sees and what validation enforces.
|
|
9
|
+
*
|
|
10
|
+
* @param schema - The Zod schema to convert
|
|
11
|
+
* @param toolName - The name to use for the tool
|
|
12
|
+
* @returns Complete tool specification
|
|
13
|
+
* @throws StructuredOutputException if the schema contains unsupported features
|
|
14
|
+
*/
|
|
15
|
+
export declare function convertSchemaToToolSpec(schema: z.ZodSchema, toolName: string): ToolSpec;
|
|
16
|
+
/**
|
|
17
|
+
* Extracts a description from the Zod schema if available.
|
|
18
|
+
*
|
|
19
|
+
* @param schema - The Zod schema to extract description from
|
|
20
|
+
* @returns The schema description or empty string if not available
|
|
21
|
+
*/
|
|
22
|
+
export declare function getSchemaDescription(schema: z.ZodSchema): string;
|
|
23
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/structured-output/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAIjD;;;;;;;;;;;GAWG;AACH,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,CAAC,CAAC,SAAS,EAAE,QAAQ,EAAE,MAAM,GAAG,QAAQ,CAevF;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,CAAC,CAAC,SAAS,GAAG,MAAM,CAahE"}
|