@ai-sdk/provider-utils 5.0.19 → 5.0.21
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/CHANGELOG.md +12 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +61 -61
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/streaming-tool-call-tracker.ts +31 -13
- package/src/to-json-schema/zod3-to-json-schema/parsers/array.ts +2 -5
- package/src/to-json-schema/zod3-to-json-schema/parsers/record.ts +5 -10
- package/src/to-json-schema/zod3-to-json-schema/select-parser.ts +40 -38
package/package.json
CHANGED
|
@@ -77,7 +77,10 @@ type StreamingToolCallTrackerController = Pick<
|
|
|
77
77
|
export class StreamingToolCallTracker<
|
|
78
78
|
DELTA extends StreamingToolCallDelta = StreamingToolCallDelta,
|
|
79
79
|
> {
|
|
80
|
-
private toolCalls
|
|
80
|
+
private toolCalls = new Set<TrackedToolCall>();
|
|
81
|
+
private toolCallsById = new Map<string, TrackedToolCall>();
|
|
82
|
+
private toolCallsByIndex = new Map<number, TrackedToolCall>();
|
|
83
|
+
private latestToolCall: TrackedToolCall | undefined;
|
|
81
84
|
private readonly controller: StreamingToolCallTrackerController;
|
|
82
85
|
private readonly _generateId: () => string;
|
|
83
86
|
private readonly typeValidation: 'none' | 'if-present' | 'required';
|
|
@@ -105,13 +108,24 @@ export class StreamingToolCallTracker<
|
|
|
105
108
|
* events as appropriate.
|
|
106
109
|
*/
|
|
107
110
|
processDelta(toolCallDelta: DELTA): void {
|
|
108
|
-
const index = toolCallDelta
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
111
|
+
const { id, index } = toolCallDelta;
|
|
112
|
+
let toolCall =
|
|
113
|
+
id != null && id.length > 0
|
|
114
|
+
? this.toolCallsById.get(id)
|
|
115
|
+
: index != null
|
|
116
|
+
? this.toolCallsByIndex.get(index)
|
|
117
|
+
: this.latestToolCall;
|
|
118
|
+
|
|
119
|
+
if (toolCall == null) {
|
|
120
|
+
toolCall = this.processNewToolCall(toolCallDelta);
|
|
112
121
|
} else {
|
|
113
|
-
this.processExistingToolCall(
|
|
122
|
+
this.processExistingToolCall(toolCall, toolCallDelta);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (index != null) {
|
|
126
|
+
this.toolCallsByIndex.set(index, toolCall);
|
|
114
127
|
}
|
|
128
|
+
this.latestToolCall = toolCall;
|
|
115
129
|
}
|
|
116
130
|
|
|
117
131
|
/**
|
|
@@ -126,7 +140,7 @@ export class StreamingToolCallTracker<
|
|
|
126
140
|
}
|
|
127
141
|
}
|
|
128
142
|
|
|
129
|
-
private processNewToolCall(
|
|
143
|
+
private processNewToolCall(toolCallDelta: DELTA): TrackedToolCall {
|
|
130
144
|
if (this.typeValidation === 'required') {
|
|
131
145
|
if (toolCallDelta.type !== 'function') {
|
|
132
146
|
throw new InvalidResponseDataError({
|
|
@@ -165,7 +179,7 @@ export class StreamingToolCallTracker<
|
|
|
165
179
|
|
|
166
180
|
const metadata = this.extractMetadata?.(toolCallDelta);
|
|
167
181
|
|
|
168
|
-
|
|
182
|
+
const toolCall: TrackedToolCall = {
|
|
169
183
|
id: toolCallDelta.id,
|
|
170
184
|
type: 'function',
|
|
171
185
|
function: {
|
|
@@ -175,8 +189,10 @@ export class StreamingToolCallTracker<
|
|
|
175
189
|
hasFinished: false,
|
|
176
190
|
metadata,
|
|
177
191
|
};
|
|
178
|
-
|
|
179
|
-
|
|
192
|
+
this.toolCalls.add(toolCall);
|
|
193
|
+
if (toolCall.id.length > 0) {
|
|
194
|
+
this.toolCallsById.set(toolCall.id, toolCall);
|
|
195
|
+
}
|
|
180
196
|
|
|
181
197
|
// Emit initial delta if arguments already present
|
|
182
198
|
if (toolCall.function.arguments.length > 0) {
|
|
@@ -191,11 +207,13 @@ export class StreamingToolCallTracker<
|
|
|
191
207
|
// argument buffer can still be the prefix of a longer argument string,
|
|
192
208
|
// so acting on it early would use truncated inputs (see #13137).
|
|
193
209
|
// Finalization happens in flush().
|
|
210
|
+
return toolCall;
|
|
194
211
|
}
|
|
195
212
|
|
|
196
|
-
private processExistingToolCall(
|
|
197
|
-
|
|
198
|
-
|
|
213
|
+
private processExistingToolCall(
|
|
214
|
+
toolCall: TrackedToolCall,
|
|
215
|
+
toolCallDelta: DELTA,
|
|
216
|
+
): void {
|
|
199
217
|
if (toolCall.hasFinished) {
|
|
200
218
|
return;
|
|
201
219
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { ZodArrayDef } from 'zod/v3';
|
|
2
2
|
import { parseDef } from '../parse-def';
|
|
3
3
|
import type { JsonSchema7Type } from '../parse-types';
|
|
4
4
|
import type { Refs } from '../refs';
|
|
@@ -14,10 +14,7 @@ export function parseArrayDef(def: ZodArrayDef, refs: Refs) {
|
|
|
14
14
|
const res: JsonSchema7ArrayType = {
|
|
15
15
|
type: 'array',
|
|
16
16
|
};
|
|
17
|
-
if (
|
|
18
|
-
def.type?._def &&
|
|
19
|
-
def.type?._def?.typeName !== ZodFirstPartyTypeKind.ZodAny
|
|
20
|
-
) {
|
|
17
|
+
if (def.type?._def && def.type?._def?.typeName !== 'ZodAny') {
|
|
21
18
|
res.items = parseDef(def.type._def, {
|
|
22
19
|
...refs,
|
|
23
20
|
currentPath: [...refs.currentPath, 'items'],
|
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ZodFirstPartyTypeKind,
|
|
3
|
-
type ZodMapDef,
|
|
4
|
-
type ZodRecordDef,
|
|
5
|
-
type ZodTypeAny,
|
|
6
|
-
} from 'zod/v3';
|
|
1
|
+
import type { ZodMapDef, ZodRecordDef, ZodTypeAny } from 'zod/v3';
|
|
7
2
|
import { parseDef } from '../parse-def';
|
|
8
3
|
import type { JsonSchema7Type } from '../parse-types';
|
|
9
4
|
import type { Refs } from '../refs';
|
|
@@ -34,7 +29,7 @@ export function parseRecordDef(
|
|
|
34
29
|
};
|
|
35
30
|
|
|
36
31
|
if (
|
|
37
|
-
def.keyType?._def.typeName ===
|
|
32
|
+
def.keyType?._def.typeName === 'ZodString' &&
|
|
38
33
|
def.keyType._def.checks?.length
|
|
39
34
|
) {
|
|
40
35
|
const { type: _type, ...keyType } = parseStringDef(def.keyType._def, refs);
|
|
@@ -43,7 +38,7 @@ export function parseRecordDef(
|
|
|
43
38
|
...schema,
|
|
44
39
|
propertyNames: keyType,
|
|
45
40
|
};
|
|
46
|
-
} else if (def.keyType?._def.typeName ===
|
|
41
|
+
} else if (def.keyType?._def.typeName === 'ZodEnum') {
|
|
47
42
|
return {
|
|
48
43
|
...schema,
|
|
49
44
|
propertyNames: {
|
|
@@ -51,8 +46,8 @@ export function parseRecordDef(
|
|
|
51
46
|
},
|
|
52
47
|
};
|
|
53
48
|
} else if (
|
|
54
|
-
def.keyType?._def.typeName ===
|
|
55
|
-
def.keyType._def.type._def.typeName ===
|
|
49
|
+
def.keyType?._def.typeName === 'ZodBranded' &&
|
|
50
|
+
def.keyType._def.type._def.typeName === 'ZodString' &&
|
|
56
51
|
def.keyType._def.type._def.checks?.length
|
|
57
52
|
) {
|
|
58
53
|
const { type: _type, ...keyType } = parseBrandedDef(
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ZodFirstPartyTypeKind } from 'zod/v3';
|
|
1
|
+
import type { ZodFirstPartyTypeKind } from 'zod/v3';
|
|
2
2
|
import { parseAnyDef } from './parsers/any';
|
|
3
3
|
import { parseArrayDef } from './parsers/array';
|
|
4
4
|
import { parseBigintDef } from './parsers/bigint';
|
|
@@ -34,79 +34,81 @@ import type { JsonSchema7Type } from './parse-types';
|
|
|
34
34
|
|
|
35
35
|
export type InnerDefGetter = () => any;
|
|
36
36
|
|
|
37
|
+
type Zod3TypeName = `${ZodFirstPartyTypeKind}`;
|
|
38
|
+
|
|
37
39
|
export const selectParser = (
|
|
38
40
|
def: any,
|
|
39
|
-
typeName:
|
|
41
|
+
typeName: Zod3TypeName,
|
|
40
42
|
refs: Refs,
|
|
41
43
|
): JsonSchema7Type | undefined | InnerDefGetter => {
|
|
42
44
|
switch (typeName) {
|
|
43
|
-
case
|
|
45
|
+
case 'ZodString':
|
|
44
46
|
return parseStringDef(def, refs);
|
|
45
|
-
case
|
|
47
|
+
case 'ZodNumber':
|
|
46
48
|
return parseNumberDef(def);
|
|
47
|
-
case
|
|
49
|
+
case 'ZodObject':
|
|
48
50
|
return parseObjectDef(def, refs);
|
|
49
|
-
case
|
|
51
|
+
case 'ZodBigInt':
|
|
50
52
|
return parseBigintDef(def);
|
|
51
|
-
case
|
|
53
|
+
case 'ZodBoolean':
|
|
52
54
|
return parseBooleanDef();
|
|
53
|
-
case
|
|
55
|
+
case 'ZodDate':
|
|
54
56
|
return parseDateDef(def, refs);
|
|
55
|
-
case
|
|
57
|
+
case 'ZodUndefined':
|
|
56
58
|
return parseUndefinedDef();
|
|
57
|
-
case
|
|
59
|
+
case 'ZodNull':
|
|
58
60
|
return parseNullDef();
|
|
59
|
-
case
|
|
61
|
+
case 'ZodArray':
|
|
60
62
|
return parseArrayDef(def, refs);
|
|
61
|
-
case
|
|
62
|
-
case
|
|
63
|
+
case 'ZodUnion':
|
|
64
|
+
case 'ZodDiscriminatedUnion':
|
|
63
65
|
return parseUnionDef(def, refs);
|
|
64
|
-
case
|
|
66
|
+
case 'ZodIntersection':
|
|
65
67
|
return parseIntersectionDef(def, refs);
|
|
66
|
-
case
|
|
68
|
+
case 'ZodTuple':
|
|
67
69
|
return parseTupleDef(def, refs);
|
|
68
|
-
case
|
|
70
|
+
case 'ZodRecord':
|
|
69
71
|
return parseRecordDef(def, refs);
|
|
70
|
-
case
|
|
72
|
+
case 'ZodLiteral':
|
|
71
73
|
return parseLiteralDef(def);
|
|
72
|
-
case
|
|
74
|
+
case 'ZodEnum':
|
|
73
75
|
return parseEnumDef(def);
|
|
74
|
-
case
|
|
76
|
+
case 'ZodNativeEnum':
|
|
75
77
|
return parseNativeEnumDef(def);
|
|
76
|
-
case
|
|
78
|
+
case 'ZodNullable':
|
|
77
79
|
return parseNullableDef(def, refs);
|
|
78
|
-
case
|
|
80
|
+
case 'ZodOptional':
|
|
79
81
|
return parseOptionalDef(def, refs);
|
|
80
|
-
case
|
|
82
|
+
case 'ZodMap':
|
|
81
83
|
return parseMapDef(def, refs);
|
|
82
|
-
case
|
|
84
|
+
case 'ZodSet':
|
|
83
85
|
return parseSetDef(def, refs);
|
|
84
|
-
case
|
|
86
|
+
case 'ZodLazy':
|
|
85
87
|
return () => (def as any).getter()._def;
|
|
86
|
-
case
|
|
88
|
+
case 'ZodPromise':
|
|
87
89
|
return parsePromiseDef(def, refs);
|
|
88
|
-
case
|
|
89
|
-
case
|
|
90
|
+
case 'ZodNaN':
|
|
91
|
+
case 'ZodNever':
|
|
90
92
|
return parseNeverDef();
|
|
91
|
-
case
|
|
93
|
+
case 'ZodEffects':
|
|
92
94
|
return parseEffectsDef(def, refs);
|
|
93
|
-
case
|
|
95
|
+
case 'ZodAny':
|
|
94
96
|
return parseAnyDef();
|
|
95
|
-
case
|
|
97
|
+
case 'ZodUnknown':
|
|
96
98
|
return parseUnknownDef();
|
|
97
|
-
case
|
|
99
|
+
case 'ZodDefault':
|
|
98
100
|
return parseDefaultDef(def, refs);
|
|
99
|
-
case
|
|
101
|
+
case 'ZodBranded':
|
|
100
102
|
return parseBrandedDef(def, refs);
|
|
101
|
-
case
|
|
103
|
+
case 'ZodReadonly':
|
|
102
104
|
return parseReadonlyDef(def, refs);
|
|
103
|
-
case
|
|
105
|
+
case 'ZodCatch':
|
|
104
106
|
return parseCatchDef(def, refs);
|
|
105
|
-
case
|
|
107
|
+
case 'ZodPipeline':
|
|
106
108
|
return parsePipelineDef(def, refs);
|
|
107
|
-
case
|
|
108
|
-
case
|
|
109
|
-
case
|
|
109
|
+
case 'ZodFunction':
|
|
110
|
+
case 'ZodVoid':
|
|
111
|
+
case 'ZodSymbol':
|
|
110
112
|
return undefined;
|
|
111
113
|
default:
|
|
112
114
|
/* c8 ignore next */
|