@tanstack/ai 0.1.0 → 0.2.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/dist/esm/activities/chat/index.d.ts +11 -10
- package/dist/esm/activities/chat/index.js +9 -9
- package/dist/esm/activities/chat/index.js.map +1 -1
- package/dist/esm/activities/chat/tools/schema-converter.d.ts +116 -0
- package/dist/esm/activities/chat/tools/schema-converter.js +115 -0
- package/dist/esm/activities/chat/tools/schema-converter.js.map +1 -0
- package/dist/esm/activities/chat/tools/tool-calls.js +23 -34
- package/dist/esm/activities/chat/tools/tool-calls.js.map +1 -1
- package/dist/esm/activities/chat/tools/tool-definition.d.ts +18 -14
- package/dist/esm/activities/chat/tools/tool-definition.js.map +1 -1
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +2 -2
- package/dist/esm/types.d.ts +45 -26
- package/package.json +3 -6
- package/src/activities/chat/index.ts +36 -28
- package/src/activities/chat/tools/schema-converter.ts +332 -0
- package/src/activities/chat/tools/tool-calls.ts +47 -53
- package/src/activities/chat/tools/tool-definition.ts +33 -29
- package/src/index.ts +3 -1
- package/src/types.ts +48 -27
- package/dist/esm/activities/chat/tools/zod-converter.d.ts +0 -69
- package/dist/esm/activities/chat/tools/zod-converter.js +0 -99
- package/dist/esm/activities/chat/tools/zod-converter.js.map +0 -1
- package/src/activities/chat/tools/zod-converter.ts +0 -235
|
@@ -1,20 +1,11 @@
|
|
|
1
|
+
import { isStandardSchema, parseWithStandardSchema } from './schema-converter'
|
|
1
2
|
import type {
|
|
2
3
|
DoneStreamChunk,
|
|
3
4
|
ModelMessage,
|
|
4
|
-
SchemaInput,
|
|
5
5
|
Tool,
|
|
6
6
|
ToolCall,
|
|
7
7
|
ToolResultStreamChunk,
|
|
8
8
|
} from '../../../types'
|
|
9
|
-
import type { z } from 'zod'
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Check if a value is a Zod schema by looking for Zod-specific internals.
|
|
13
|
-
*/
|
|
14
|
-
function isZodSchema(schema: SchemaInput | undefined): schema is z.ZodType {
|
|
15
|
-
if (!schema) return false
|
|
16
|
-
return '_zod' in schema && typeof (schema as any)._zod === 'object'
|
|
17
|
-
}
|
|
18
9
|
|
|
19
10
|
/**
|
|
20
11
|
* Manages tool call accumulation and execution for the chat() method's automatic tool execution loop.
|
|
@@ -131,7 +122,7 @@ export class ToolCallManager {
|
|
|
131
122
|
if (tool?.execute) {
|
|
132
123
|
try {
|
|
133
124
|
// Parse arguments
|
|
134
|
-
let args:
|
|
125
|
+
let args: unknown
|
|
135
126
|
try {
|
|
136
127
|
args = JSON.parse(toolCall.function.arguments)
|
|
137
128
|
} catch (parseError) {
|
|
@@ -140,13 +131,17 @@ export class ToolCallManager {
|
|
|
140
131
|
)
|
|
141
132
|
}
|
|
142
133
|
|
|
143
|
-
// Validate input against inputSchema (
|
|
144
|
-
if (tool.inputSchema &&
|
|
134
|
+
// Validate input against inputSchema (for Standard Schema compliant schemas)
|
|
135
|
+
if (tool.inputSchema && isStandardSchema(tool.inputSchema)) {
|
|
145
136
|
try {
|
|
146
|
-
args = tool.inputSchema
|
|
147
|
-
} catch (validationError:
|
|
137
|
+
args = parseWithStandardSchema(tool.inputSchema, args)
|
|
138
|
+
} catch (validationError: unknown) {
|
|
139
|
+
const message =
|
|
140
|
+
validationError instanceof Error
|
|
141
|
+
? validationError.message
|
|
142
|
+
: 'Validation failed'
|
|
148
143
|
throw new Error(
|
|
149
|
-
`Input validation failed for tool ${tool.name}: ${
|
|
144
|
+
`Input validation failed for tool ${tool.name}: ${message}`,
|
|
150
145
|
)
|
|
151
146
|
}
|
|
152
147
|
}
|
|
@@ -154,27 +149,33 @@ export class ToolCallManager {
|
|
|
154
149
|
// Execute the tool
|
|
155
150
|
let result = await tool.execute(args)
|
|
156
151
|
|
|
157
|
-
// Validate output against outputSchema if provided (
|
|
152
|
+
// Validate output against outputSchema if provided (for Standard Schema compliant schemas)
|
|
158
153
|
if (
|
|
159
154
|
tool.outputSchema &&
|
|
160
|
-
|
|
155
|
+
isStandardSchema(tool.outputSchema) &&
|
|
161
156
|
result !== undefined &&
|
|
162
157
|
result !== null
|
|
163
158
|
) {
|
|
164
159
|
try {
|
|
165
|
-
result = tool.outputSchema
|
|
166
|
-
} catch (validationError:
|
|
160
|
+
result = parseWithStandardSchema(tool.outputSchema, result)
|
|
161
|
+
} catch (validationError: unknown) {
|
|
162
|
+
const message =
|
|
163
|
+
validationError instanceof Error
|
|
164
|
+
? validationError.message
|
|
165
|
+
: 'Validation failed'
|
|
167
166
|
throw new Error(
|
|
168
|
-
`Output validation failed for tool ${tool.name}: ${
|
|
167
|
+
`Output validation failed for tool ${tool.name}: ${message}`,
|
|
169
168
|
)
|
|
170
169
|
}
|
|
171
170
|
}
|
|
172
171
|
|
|
173
172
|
toolResultContent =
|
|
174
173
|
typeof result === 'string' ? result : JSON.stringify(result)
|
|
175
|
-
} catch (error:
|
|
174
|
+
} catch (error: unknown) {
|
|
176
175
|
// If tool execution fails, add error message
|
|
177
|
-
|
|
176
|
+
const message =
|
|
177
|
+
error instanceof Error ? error.message : 'Unknown error'
|
|
178
|
+
toolResultContent = `Error executing tool: ${message}`
|
|
178
179
|
}
|
|
179
180
|
} else {
|
|
180
181
|
// Tool doesn't have execute function, add placeholder
|
|
@@ -286,7 +287,7 @@ export async function executeToolCalls(
|
|
|
286
287
|
}
|
|
287
288
|
|
|
288
289
|
// Parse arguments, throwing error if invalid JSON
|
|
289
|
-
let input:
|
|
290
|
+
let input: unknown = {}
|
|
290
291
|
const argsStr = toolCall.function.arguments.trim() || '{}'
|
|
291
292
|
if (argsStr) {
|
|
292
293
|
try {
|
|
@@ -297,16 +298,20 @@ export async function executeToolCalls(
|
|
|
297
298
|
}
|
|
298
299
|
}
|
|
299
300
|
|
|
300
|
-
// Validate input against inputSchema (
|
|
301
|
-
if (tool.inputSchema &&
|
|
301
|
+
// Validate input against inputSchema (for Standard Schema compliant schemas)
|
|
302
|
+
if (tool.inputSchema && isStandardSchema(tool.inputSchema)) {
|
|
302
303
|
try {
|
|
303
|
-
input = tool.inputSchema
|
|
304
|
-
} catch (validationError:
|
|
304
|
+
input = parseWithStandardSchema(tool.inputSchema, input)
|
|
305
|
+
} catch (validationError: unknown) {
|
|
306
|
+
const message =
|
|
307
|
+
validationError instanceof Error
|
|
308
|
+
? validationError.message
|
|
309
|
+
: 'Validation failed'
|
|
305
310
|
results.push({
|
|
306
311
|
toolCallId: toolCall.id,
|
|
307
312
|
toolName,
|
|
308
313
|
result: {
|
|
309
|
-
error: `Input validation failed for tool ${tool.name}: ${
|
|
314
|
+
error: `Input validation failed for tool ${tool.name}: ${message}`,
|
|
310
315
|
},
|
|
311
316
|
state: 'output-error',
|
|
312
317
|
})
|
|
@@ -393,21 +398,14 @@ export async function executeToolCalls(
|
|
|
393
398
|
let result = await tool.execute(input)
|
|
394
399
|
const duration = Date.now() - startTime
|
|
395
400
|
|
|
396
|
-
// Validate output against outputSchema if provided (
|
|
401
|
+
// Validate output against outputSchema if provided (for Standard Schema compliant schemas)
|
|
397
402
|
if (
|
|
398
403
|
tool.outputSchema &&
|
|
399
|
-
|
|
404
|
+
isStandardSchema(tool.outputSchema) &&
|
|
400
405
|
result !== undefined &&
|
|
401
406
|
result !== null
|
|
402
407
|
) {
|
|
403
|
-
|
|
404
|
-
if (parsed.success) {
|
|
405
|
-
result = parsed.data
|
|
406
|
-
} else {
|
|
407
|
-
throw new Error(
|
|
408
|
-
`Output validation failed for tool ${tool.name}: ${parsed.error.message}`,
|
|
409
|
-
)
|
|
410
|
-
}
|
|
408
|
+
result = parseWithStandardSchema(tool.outputSchema, result)
|
|
411
409
|
}
|
|
412
410
|
|
|
413
411
|
results.push({
|
|
@@ -419,12 +417,14 @@ export async function executeToolCalls(
|
|
|
419
417
|
: result || null,
|
|
420
418
|
duration,
|
|
421
419
|
})
|
|
422
|
-
} catch (error:
|
|
420
|
+
} catch (error: unknown) {
|
|
423
421
|
const duration = Date.now() - startTime
|
|
422
|
+
const message =
|
|
423
|
+
error instanceof Error ? error.message : 'Unknown error'
|
|
424
424
|
results.push({
|
|
425
425
|
toolCallId: toolCall.id,
|
|
426
426
|
toolName,
|
|
427
|
-
result: { error:
|
|
427
|
+
result: { error: message },
|
|
428
428
|
state: 'output-error',
|
|
429
429
|
duration,
|
|
430
430
|
})
|
|
@@ -456,21 +456,14 @@ export async function executeToolCalls(
|
|
|
456
456
|
let result = await tool.execute(input)
|
|
457
457
|
const duration = Date.now() - startTime
|
|
458
458
|
|
|
459
|
-
// Validate output against outputSchema if provided (
|
|
459
|
+
// Validate output against outputSchema if provided (for Standard Schema compliant schemas)
|
|
460
460
|
if (
|
|
461
461
|
tool.outputSchema &&
|
|
462
|
-
|
|
462
|
+
isStandardSchema(tool.outputSchema) &&
|
|
463
463
|
result !== undefined &&
|
|
464
464
|
result !== null
|
|
465
465
|
) {
|
|
466
|
-
|
|
467
|
-
if (parsed.success) {
|
|
468
|
-
result = parsed.data
|
|
469
|
-
} else {
|
|
470
|
-
throw new Error(
|
|
471
|
-
`Output validation failed for tool ${tool.name}: ${parsed.error.message}`,
|
|
472
|
-
)
|
|
473
|
-
}
|
|
466
|
+
result = parseWithStandardSchema(tool.outputSchema, result)
|
|
474
467
|
}
|
|
475
468
|
|
|
476
469
|
results.push({
|
|
@@ -480,12 +473,13 @@ export async function executeToolCalls(
|
|
|
480
473
|
typeof result === 'string' ? JSON.parse(result) : result || null,
|
|
481
474
|
duration,
|
|
482
475
|
})
|
|
483
|
-
} catch (error:
|
|
476
|
+
} catch (error: unknown) {
|
|
484
477
|
const duration = Date.now() - startTime
|
|
478
|
+
const message = error instanceof Error ? error.message : 'Unknown error'
|
|
485
479
|
results.push({
|
|
486
480
|
toolCallId: toolCall.id,
|
|
487
481
|
toolName,
|
|
488
|
-
result: { error:
|
|
482
|
+
result: { error: message },
|
|
489
483
|
state: 'output-error',
|
|
490
484
|
duration,
|
|
491
485
|
})
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { StandardJSONSchemaV1 } from '@standard-schema/spec'
|
|
2
2
|
import type {
|
|
3
3
|
InferSchemaType,
|
|
4
4
|
JSONSchema,
|
|
@@ -10,8 +10,8 @@ import type {
|
|
|
10
10
|
* Marker type for server-side tools
|
|
11
11
|
*/
|
|
12
12
|
export interface ServerTool<
|
|
13
|
-
TInput extends SchemaInput =
|
|
14
|
-
TOutput extends SchemaInput =
|
|
13
|
+
TInput extends SchemaInput = SchemaInput,
|
|
14
|
+
TOutput extends SchemaInput = SchemaInput,
|
|
15
15
|
TName extends string = string,
|
|
16
16
|
> extends Tool<TInput, TOutput, TName> {
|
|
17
17
|
__toolSide: 'server'
|
|
@@ -21,8 +21,8 @@ export interface ServerTool<
|
|
|
21
21
|
* Marker type for client-side tools
|
|
22
22
|
*/
|
|
23
23
|
export interface ClientTool<
|
|
24
|
-
TInput extends SchemaInput =
|
|
25
|
-
TOutput extends SchemaInput =
|
|
24
|
+
TInput extends SchemaInput = SchemaInput,
|
|
25
|
+
TOutput extends SchemaInput = SchemaInput,
|
|
26
26
|
TName extends string = string,
|
|
27
27
|
> {
|
|
28
28
|
__toolSide: 'client'
|
|
@@ -31,7 +31,7 @@ export interface ClientTool<
|
|
|
31
31
|
inputSchema?: TInput
|
|
32
32
|
outputSchema?: TOutput
|
|
33
33
|
needsApproval?: boolean
|
|
34
|
-
metadata?: Record<string,
|
|
34
|
+
metadata?: Record<string, unknown>
|
|
35
35
|
execute?: (
|
|
36
36
|
args: InferSchemaType<TInput>,
|
|
37
37
|
) => Promise<InferSchemaType<TOutput>> | InferSchemaType<TOutput>
|
|
@@ -41,8 +41,8 @@ export interface ClientTool<
|
|
|
41
41
|
* Tool definition that can be used directly or instantiated for server/client
|
|
42
42
|
*/
|
|
43
43
|
export interface ToolDefinitionInstance<
|
|
44
|
-
TInput extends SchemaInput =
|
|
45
|
-
TOutput extends SchemaInput =
|
|
44
|
+
TInput extends SchemaInput = SchemaInput,
|
|
45
|
+
TOutput extends SchemaInput = SchemaInput,
|
|
46
46
|
TName extends string = string,
|
|
47
47
|
> extends Tool<TInput, TOutput, TName> {
|
|
48
48
|
__toolSide: 'definition'
|
|
@@ -52,8 +52,8 @@ export interface ToolDefinitionInstance<
|
|
|
52
52
|
* Union type for any kind of client-side tool (client tool or definition)
|
|
53
53
|
*/
|
|
54
54
|
export type AnyClientTool =
|
|
55
|
-
| ClientTool<
|
|
56
|
-
| ToolDefinitionInstance<
|
|
55
|
+
| ClientTool<SchemaInput, SchemaInput>
|
|
56
|
+
| ToolDefinitionInstance<SchemaInput, SchemaInput>
|
|
57
57
|
|
|
58
58
|
/**
|
|
59
59
|
* Extract the tool name as a literal type
|
|
@@ -61,33 +61,33 @@ export type AnyClientTool =
|
|
|
61
61
|
export type InferToolName<T> = T extends { name: infer N } ? N : never
|
|
62
62
|
|
|
63
63
|
/**
|
|
64
|
-
* Extract the input type from a tool (inferred from
|
|
64
|
+
* Extract the input type from a tool (inferred from Standard JSON Schema, or `unknown` for plain JSONSchema)
|
|
65
65
|
*/
|
|
66
66
|
export type InferToolInput<T> = T extends { inputSchema?: infer TInput }
|
|
67
|
-
? TInput extends
|
|
68
|
-
?
|
|
67
|
+
? TInput extends StandardJSONSchemaV1<infer TInferred, unknown>
|
|
68
|
+
? TInferred
|
|
69
69
|
: TInput extends JSONSchema
|
|
70
|
-
?
|
|
71
|
-
:
|
|
72
|
-
:
|
|
70
|
+
? unknown
|
|
71
|
+
: unknown
|
|
72
|
+
: unknown
|
|
73
73
|
|
|
74
74
|
/**
|
|
75
|
-
* Extract the output type from a tool (inferred from
|
|
75
|
+
* Extract the output type from a tool (inferred from Standard JSON Schema, or `unknown` for plain JSONSchema)
|
|
76
76
|
*/
|
|
77
77
|
export type InferToolOutput<T> = T extends { outputSchema?: infer TOutput }
|
|
78
|
-
? TOutput extends
|
|
79
|
-
?
|
|
78
|
+
? TOutput extends StandardJSONSchemaV1<infer TInferred, unknown>
|
|
79
|
+
? TInferred
|
|
80
80
|
: TOutput extends JSONSchema
|
|
81
|
-
?
|
|
82
|
-
:
|
|
83
|
-
:
|
|
81
|
+
? unknown
|
|
82
|
+
: unknown
|
|
83
|
+
: unknown
|
|
84
84
|
|
|
85
85
|
/**
|
|
86
86
|
* Tool definition configuration
|
|
87
87
|
*/
|
|
88
88
|
export interface ToolDefinitionConfig<
|
|
89
|
-
TInput extends SchemaInput =
|
|
90
|
-
TOutput extends SchemaInput =
|
|
89
|
+
TInput extends SchemaInput = SchemaInput,
|
|
90
|
+
TOutput extends SchemaInput = SchemaInput,
|
|
91
91
|
TName extends string = string,
|
|
92
92
|
> {
|
|
93
93
|
name: TName
|
|
@@ -95,15 +95,15 @@ export interface ToolDefinitionConfig<
|
|
|
95
95
|
inputSchema?: TInput
|
|
96
96
|
outputSchema?: TOutput
|
|
97
97
|
needsApproval?: boolean
|
|
98
|
-
metadata?: Record<string,
|
|
98
|
+
metadata?: Record<string, unknown>
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
/**
|
|
102
102
|
* Tool definition builder that allows creating server or client tools from a shared definition
|
|
103
103
|
*/
|
|
104
104
|
export interface ToolDefinition<
|
|
105
|
-
TInput extends SchemaInput =
|
|
106
|
-
TOutput extends SchemaInput =
|
|
105
|
+
TInput extends SchemaInput = SchemaInput,
|
|
106
|
+
TOutput extends SchemaInput = SchemaInput,
|
|
107
107
|
TName extends string = string,
|
|
108
108
|
> extends ToolDefinitionInstance<TInput, TOutput, TName> {
|
|
109
109
|
/**
|
|
@@ -133,11 +133,15 @@ export interface ToolDefinition<
|
|
|
133
133
|
* 2. Instantiated as a server tool with .server()
|
|
134
134
|
* 3. Instantiated as a client tool with .client()
|
|
135
135
|
*
|
|
136
|
+
* Supports any Standard JSON Schema compliant library (Zod v4+, ArkType, Valibot, etc.)
|
|
137
|
+
* or plain JSON Schema objects.
|
|
138
|
+
*
|
|
136
139
|
* @example
|
|
137
140
|
* ```typescript
|
|
138
141
|
* import { toolDefinition } from '@tanstack/ai';
|
|
139
142
|
* import { z } from 'zod';
|
|
140
143
|
*
|
|
144
|
+
* // Using Zod (natively supports Standard JSON Schema)
|
|
141
145
|
* const addToCartTool = toolDefinition({
|
|
142
146
|
* name: 'addToCart',
|
|
143
147
|
* description: 'Add a guitar to the shopping cart (requires approval)',
|
|
@@ -177,8 +181,8 @@ export interface ToolDefinition<
|
|
|
177
181
|
* ```
|
|
178
182
|
*/
|
|
179
183
|
export function toolDefinition<
|
|
180
|
-
TInput extends SchemaInput =
|
|
181
|
-
TOutput extends SchemaInput =
|
|
184
|
+
TInput extends SchemaInput = SchemaInput,
|
|
185
|
+
TOutput extends SchemaInput = SchemaInput,
|
|
182
186
|
TName extends string = string,
|
|
183
187
|
>(
|
|
184
188
|
config: ToolDefinitionConfig<TInput, TOutput, TName>,
|
package/src/index.ts
CHANGED
|
@@ -47,7 +47,9 @@ export {
|
|
|
47
47
|
type InferToolInput,
|
|
48
48
|
type InferToolOutput,
|
|
49
49
|
} from './activities/chat/tools/tool-definition'
|
|
50
|
-
|
|
50
|
+
|
|
51
|
+
// Schema conversion (Standard JSON Schema compliant)
|
|
52
|
+
export { convertSchemaToJsonSchema } from './activities/chat/tools/schema-converter'
|
|
51
53
|
|
|
52
54
|
// Stream utilities
|
|
53
55
|
export {
|
package/src/types.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { StandardJSONSchemaV1 } from '@standard-schema/spec'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Tool call states - track the lifecycle of a tool call
|
|
@@ -20,17 +20,17 @@ export type ToolResultState =
|
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* JSON Schema type for defining tool input/output schemas as raw JSON Schema objects.
|
|
23
|
-
* This allows tools to be defined without
|
|
23
|
+
* This allows tools to be defined without schema libraries when you have JSON Schema definitions available.
|
|
24
24
|
*/
|
|
25
25
|
export interface JSONSchema {
|
|
26
26
|
type?: string | Array<string>
|
|
27
27
|
properties?: Record<string, JSONSchema>
|
|
28
28
|
items?: JSONSchema | Array<JSONSchema>
|
|
29
29
|
required?: Array<string>
|
|
30
|
-
enum?: Array<
|
|
31
|
-
const?:
|
|
30
|
+
enum?: Array<unknown>
|
|
31
|
+
const?: unknown
|
|
32
32
|
description?: string
|
|
33
|
-
default?:
|
|
33
|
+
default?: unknown
|
|
34
34
|
$ref?: string
|
|
35
35
|
$defs?: Record<string, JSONSchema>
|
|
36
36
|
definitions?: Record<string, JSONSchema>
|
|
@@ -59,21 +59,30 @@ export interface JSONSchema {
|
|
|
59
59
|
minProperties?: number
|
|
60
60
|
maxProperties?: number
|
|
61
61
|
title?: string
|
|
62
|
-
examples?: Array<
|
|
63
|
-
[key: string]:
|
|
62
|
+
examples?: Array<unknown>
|
|
63
|
+
[key: string]: unknown // Allow additional properties for extensibility
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
/**
|
|
67
|
-
* Union type for schema input - can be
|
|
67
|
+
* Union type for schema input - can be any Standard JSON Schema compliant schema or a plain JSONSchema object.
|
|
68
|
+
*
|
|
69
|
+
* Standard JSON Schema compliant libraries include:
|
|
70
|
+
* - Zod v4.2+ (natively supports StandardJSONSchemaV1)
|
|
71
|
+
* - ArkType v2.1.28+ (natively supports StandardJSONSchemaV1)
|
|
72
|
+
* - Valibot v1.2+ (via `toStandardJsonSchema()` from `@valibot/to-json-schema`)
|
|
73
|
+
*
|
|
74
|
+
* @see https://standardschema.dev/json-schema
|
|
68
75
|
*/
|
|
69
|
-
|
|
76
|
+
|
|
77
|
+
export type SchemaInput = StandardJSONSchemaV1<any, any> | JSONSchema
|
|
70
78
|
|
|
71
79
|
/**
|
|
72
80
|
* Infer the TypeScript type from a schema.
|
|
73
|
-
* For
|
|
74
|
-
* For JSONSchema, returns `any` since we can't infer types from JSON Schema at compile time.
|
|
81
|
+
* For Standard JSON Schema compliant schemas, extracts the input type.
|
|
82
|
+
* For plain JSONSchema, returns `any` since we can't infer types from JSON Schema at compile time.
|
|
75
83
|
*/
|
|
76
|
-
export type InferSchemaType<T> =
|
|
84
|
+
export type InferSchemaType<T> =
|
|
85
|
+
T extends StandardJSONSchemaV1<infer TInput, unknown> ? TInput : unknown
|
|
77
86
|
|
|
78
87
|
export interface ToolCall {
|
|
79
88
|
id: string
|
|
@@ -309,14 +318,16 @@ export type ConstrainedModelMessage<
|
|
|
309
318
|
* Tools allow the model to interact with external systems, APIs, or perform computations.
|
|
310
319
|
* The model will decide when to call tools based on the user's request and the tool descriptions.
|
|
311
320
|
*
|
|
312
|
-
* Tools can use
|
|
321
|
+
* Tools can use any Standard JSON Schema compliant library (Zod, ArkType, Valibot, etc.)
|
|
322
|
+
* or plain JSON Schema objects for runtime validation and type safety.
|
|
313
323
|
*
|
|
314
324
|
* @see https://platform.openai.com/docs/guides/function-calling
|
|
315
325
|
* @see https://docs.anthropic.com/claude/docs/tool-use
|
|
326
|
+
* @see https://standardschema.dev/json-schema
|
|
316
327
|
*/
|
|
317
328
|
export interface Tool<
|
|
318
|
-
TInput extends SchemaInput =
|
|
319
|
-
TOutput extends SchemaInput =
|
|
329
|
+
TInput extends SchemaInput = SchemaInput,
|
|
330
|
+
TOutput extends SchemaInput = SchemaInput,
|
|
320
331
|
TName extends string = string,
|
|
321
332
|
> {
|
|
322
333
|
/**
|
|
@@ -342,16 +353,16 @@ export interface Tool<
|
|
|
342
353
|
/**
|
|
343
354
|
* Schema describing the tool's input parameters.
|
|
344
355
|
*
|
|
345
|
-
* Can be
|
|
356
|
+
* Can be any Standard JSON Schema compliant schema (Zod, ArkType, Valibot, etc.) or a plain JSON Schema object.
|
|
346
357
|
* Defines the structure and types of arguments the tool accepts.
|
|
347
358
|
* The model will generate arguments matching this schema.
|
|
348
|
-
*
|
|
359
|
+
* Standard JSON Schema compliant schemas are converted to JSON Schema for LLM providers.
|
|
349
360
|
*
|
|
350
|
-
* @see https://
|
|
361
|
+
* @see https://standardschema.dev/json-schema
|
|
351
362
|
* @see https://json-schema.org/
|
|
352
363
|
*
|
|
353
364
|
* @example
|
|
354
|
-
* // Using Zod schema
|
|
365
|
+
* // Using Zod v4+ schema (natively supports Standard JSON Schema)
|
|
355
366
|
* import { z } from 'zod';
|
|
356
367
|
* z.object({
|
|
357
368
|
* location: z.string().describe("City name or coordinates"),
|
|
@@ -359,7 +370,15 @@ export interface Tool<
|
|
|
359
370
|
* })
|
|
360
371
|
*
|
|
361
372
|
* @example
|
|
362
|
-
* // Using JSON Schema
|
|
373
|
+
* // Using ArkType (natively supports Standard JSON Schema)
|
|
374
|
+
* import { type } from 'arktype';
|
|
375
|
+
* type({
|
|
376
|
+
* location: 'string',
|
|
377
|
+
* unit: "'celsius' | 'fahrenheit'"
|
|
378
|
+
* })
|
|
379
|
+
*
|
|
380
|
+
* @example
|
|
381
|
+
* // Using plain JSON Schema
|
|
363
382
|
* {
|
|
364
383
|
* type: 'object',
|
|
365
384
|
* properties: {
|
|
@@ -374,15 +393,16 @@ export interface Tool<
|
|
|
374
393
|
/**
|
|
375
394
|
* Optional schema for validating tool output.
|
|
376
395
|
*
|
|
377
|
-
* Can be
|
|
378
|
-
* If provided with a
|
|
379
|
-
* being sent back to the model. This catches bugs in tool
|
|
380
|
-
* and ensures consistent output formatting.
|
|
396
|
+
* Can be any Standard JSON Schema compliant schema or a plain JSON Schema object.
|
|
397
|
+
* If provided with a Standard Schema compliant schema, tool results will be validated
|
|
398
|
+
* against this schema before being sent back to the model. This catches bugs in tool
|
|
399
|
+
* implementations and ensures consistent output formatting.
|
|
381
400
|
*
|
|
382
401
|
* Note: This is client-side validation only - not sent to LLM providers.
|
|
383
|
-
* Note: JSON Schema output validation is not performed at runtime.
|
|
402
|
+
* Note: Plain JSON Schema output validation is not performed at runtime.
|
|
384
403
|
*
|
|
385
404
|
* @example
|
|
405
|
+
* // Using Zod
|
|
386
406
|
* z.object({
|
|
387
407
|
* temperature: z.number(),
|
|
388
408
|
* conditions: z.string(),
|
|
@@ -601,12 +621,13 @@ export interface TextOptions<
|
|
|
601
621
|
request?: Request | RequestInit
|
|
602
622
|
|
|
603
623
|
/**
|
|
604
|
-
*
|
|
624
|
+
* Schema for structured output.
|
|
605
625
|
* When provided, the adapter should use the provider's native structured output API
|
|
606
626
|
* to ensure the response conforms to this schema.
|
|
607
627
|
* The schema will be converted to JSON Schema format before being sent to the provider.
|
|
628
|
+
* Supports any Standard JSON Schema compliant library (Zod, ArkType, Valibot, etc.).
|
|
608
629
|
*/
|
|
609
|
-
outputSchema?:
|
|
630
|
+
outputSchema?: SchemaInput
|
|
610
631
|
/**
|
|
611
632
|
* Conversation ID for correlating client and server-side devtools events.
|
|
612
633
|
* When provided, server-side events will be linked to the client conversation in devtools.
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import { SchemaInput } from '../../../types.js';
|
|
2
|
-
/**
|
|
3
|
-
* Options for schema conversion
|
|
4
|
-
*/
|
|
5
|
-
export interface ConvertSchemaOptions {
|
|
6
|
-
/**
|
|
7
|
-
* When true, transforms the schema to be compatible with OpenAI's structured output requirements:
|
|
8
|
-
* - All properties are added to the `required` array
|
|
9
|
-
* - Optional fields get null added to their type union
|
|
10
|
-
* - additionalProperties is set to false for all objects
|
|
11
|
-
*
|
|
12
|
-
* @default false
|
|
13
|
-
*/
|
|
14
|
-
forStructuredOutput?: boolean;
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Converts a schema (Zod or JSONSchema) to JSON Schema format compatible with LLM providers.
|
|
18
|
-
* If the input is already a JSONSchema object, it is returned as-is.
|
|
19
|
-
* If the input is a Zod schema, it is converted to JSON Schema.
|
|
20
|
-
*
|
|
21
|
-
* @param schema - Zod schema or JSONSchema object to convert
|
|
22
|
-
* @param options - Conversion options
|
|
23
|
-
* @returns JSON Schema object that can be sent to LLM providers
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* ```typescript
|
|
27
|
-
* import { z } from 'zod';
|
|
28
|
-
*
|
|
29
|
-
* // Using Zod schema
|
|
30
|
-
* const zodSchema = z.object({
|
|
31
|
-
* location: z.string().describe('City name'),
|
|
32
|
-
* unit: z.enum(['celsius', 'fahrenheit']).optional()
|
|
33
|
-
* });
|
|
34
|
-
*
|
|
35
|
-
* const jsonSchema = convertZodToJsonSchema(zodSchema);
|
|
36
|
-
* // Returns:
|
|
37
|
-
* // {
|
|
38
|
-
* // type: 'object',
|
|
39
|
-
* // properties: {
|
|
40
|
-
* // location: { type: 'string', description: 'City name' },
|
|
41
|
-
* // unit: { type: 'string', enum: ['celsius', 'fahrenheit'] }
|
|
42
|
-
* // },
|
|
43
|
-
* // required: ['location']
|
|
44
|
-
* // }
|
|
45
|
-
*
|
|
46
|
-
* // For OpenAI structured output (all fields required, optional fields nullable)
|
|
47
|
-
* const structuredSchema = convertZodToJsonSchema(zodSchema, { forStructuredOutput: true });
|
|
48
|
-
* // Returns:
|
|
49
|
-
* // {
|
|
50
|
-
* // type: 'object',
|
|
51
|
-
* // properties: {
|
|
52
|
-
* // location: { type: 'string', description: 'City name' },
|
|
53
|
-
* // unit: { type: ['string', 'null'], enum: ['celsius', 'fahrenheit'] }
|
|
54
|
-
* // },
|
|
55
|
-
* // required: ['location', 'unit'],
|
|
56
|
-
* // additionalProperties: false
|
|
57
|
-
* // }
|
|
58
|
-
*
|
|
59
|
-
* // Using JSONSchema directly (passes through unchanged)
|
|
60
|
-
* const rawSchema = {
|
|
61
|
-
* type: 'object',
|
|
62
|
-
* properties: { location: { type: 'string' } },
|
|
63
|
-
* required: ['location']
|
|
64
|
-
* };
|
|
65
|
-
* const result = convertZodToJsonSchema(rawSchema);
|
|
66
|
-
* // Returns the same object
|
|
67
|
-
* ```
|
|
68
|
-
*/
|
|
69
|
-
export declare function convertZodToJsonSchema(schema: SchemaInput | undefined, options?: ConvertSchemaOptions): Record<string, any> | undefined;
|