@mastra/client-js 1.8.4-alpha.0 → 1.8.5-alpha.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/CHANGELOG.md +22 -0
- package/dist/_types/@internal_ai-sdk-v5/dist/index.d.ts +33 -46
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/index.cjs +28 -1089
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +26 -1087
- package/dist/index.js.map +1 -1
- package/dist/utils/process-client-tools.d.ts.map +1 -1
- package/package.json +8 -8
package/dist/index.js
CHANGED
|
@@ -1,1067 +1,11 @@
|
|
|
1
|
+
import { processDataStream, parsePartialJson } from '@ai-sdk/ui-utils';
|
|
1
2
|
import { v4 } from '@lukeed/uuid';
|
|
2
3
|
import { getErrorFromUnknown } from '@mastra/core/error';
|
|
3
4
|
import { standardSchemaToJSONSchema, toStandardSchema } from '@mastra/schema-compat/schema';
|
|
4
5
|
import { RequestContext } from '@mastra/core/request-context';
|
|
5
|
-
import {
|
|
6
|
-
import { zodToJsonSchema } from '@mastra/schema-compat/zod-to-json';
|
|
6
|
+
import { zodToJsonSchema as zodToJsonSchema$1 } from '@mastra/schema-compat/zod-to-json';
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
var __defProp = Object.defineProperty;
|
|
10
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
11
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
12
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
13
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
-
var __commonJS = (cb, mod) => function __require() {
|
|
15
|
-
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
16
|
-
};
|
|
17
|
-
var __copyProps = (to, from, except, desc) => {
|
|
18
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
19
|
-
for (let key of __getOwnPropNames(from))
|
|
20
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
21
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
22
|
-
}
|
|
23
|
-
return to;
|
|
24
|
-
};
|
|
25
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
26
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
27
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
28
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
29
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
30
|
-
__defProp(target, "default", { value: mod, enumerable: true }) ,
|
|
31
|
-
mod
|
|
32
|
-
));
|
|
33
|
-
|
|
34
|
-
// ../../node_modules/.pnpm/secure-json-parse@2.7.0/node_modules/secure-json-parse/index.js
|
|
35
|
-
var require_secure_json_parse = __commonJS({
|
|
36
|
-
"../../node_modules/.pnpm/secure-json-parse@2.7.0/node_modules/secure-json-parse/index.js"(exports$1, module) {
|
|
37
|
-
var hasBuffer = typeof Buffer !== "undefined";
|
|
38
|
-
var suspectProtoRx = /"(?:_|\\u005[Ff])(?:_|\\u005[Ff])(?:p|\\u0070)(?:r|\\u0072)(?:o|\\u006[Ff])(?:t|\\u0074)(?:o|\\u006[Ff])(?:_|\\u005[Ff])(?:_|\\u005[Ff])"\s*:/;
|
|
39
|
-
var suspectConstructorRx = /"(?:c|\\u0063)(?:o|\\u006[Ff])(?:n|\\u006[Ee])(?:s|\\u0073)(?:t|\\u0074)(?:r|\\u0072)(?:u|\\u0075)(?:c|\\u0063)(?:t|\\u0074)(?:o|\\u006[Ff])(?:r|\\u0072)"\s*:/;
|
|
40
|
-
function _parse(text, reviver, options) {
|
|
41
|
-
if (options == null) {
|
|
42
|
-
if (reviver !== null && typeof reviver === "object") {
|
|
43
|
-
options = reviver;
|
|
44
|
-
reviver = void 0;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
if (hasBuffer && Buffer.isBuffer(text)) {
|
|
48
|
-
text = text.toString();
|
|
49
|
-
}
|
|
50
|
-
if (text && text.charCodeAt(0) === 65279) {
|
|
51
|
-
text = text.slice(1);
|
|
52
|
-
}
|
|
53
|
-
const obj = JSON.parse(text, reviver);
|
|
54
|
-
if (obj === null || typeof obj !== "object") {
|
|
55
|
-
return obj;
|
|
56
|
-
}
|
|
57
|
-
const protoAction = options && options.protoAction || "error";
|
|
58
|
-
const constructorAction = options && options.constructorAction || "error";
|
|
59
|
-
if (protoAction === "ignore" && constructorAction === "ignore") {
|
|
60
|
-
return obj;
|
|
61
|
-
}
|
|
62
|
-
if (protoAction !== "ignore" && constructorAction !== "ignore") {
|
|
63
|
-
if (suspectProtoRx.test(text) === false && suspectConstructorRx.test(text) === false) {
|
|
64
|
-
return obj;
|
|
65
|
-
}
|
|
66
|
-
} else if (protoAction !== "ignore" && constructorAction === "ignore") {
|
|
67
|
-
if (suspectProtoRx.test(text) === false) {
|
|
68
|
-
return obj;
|
|
69
|
-
}
|
|
70
|
-
} else {
|
|
71
|
-
if (suspectConstructorRx.test(text) === false) {
|
|
72
|
-
return obj;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
return filter(obj, { protoAction, constructorAction, safe: options && options.safe });
|
|
76
|
-
}
|
|
77
|
-
function filter(obj, { protoAction = "error", constructorAction = "error", safe } = {}) {
|
|
78
|
-
let next = [obj];
|
|
79
|
-
while (next.length) {
|
|
80
|
-
const nodes = next;
|
|
81
|
-
next = [];
|
|
82
|
-
for (const node of nodes) {
|
|
83
|
-
if (protoAction !== "ignore" && Object.prototype.hasOwnProperty.call(node, "__proto__")) {
|
|
84
|
-
if (safe === true) {
|
|
85
|
-
return null;
|
|
86
|
-
} else if (protoAction === "error") {
|
|
87
|
-
throw new SyntaxError("Object contains forbidden prototype property");
|
|
88
|
-
}
|
|
89
|
-
delete node.__proto__;
|
|
90
|
-
}
|
|
91
|
-
if (constructorAction !== "ignore" && Object.prototype.hasOwnProperty.call(node, "constructor") && Object.prototype.hasOwnProperty.call(node.constructor, "prototype")) {
|
|
92
|
-
if (safe === true) {
|
|
93
|
-
return null;
|
|
94
|
-
} else if (constructorAction === "error") {
|
|
95
|
-
throw new SyntaxError("Object contains forbidden prototype property");
|
|
96
|
-
}
|
|
97
|
-
delete node.constructor;
|
|
98
|
-
}
|
|
99
|
-
for (const key in node) {
|
|
100
|
-
const value = node[key];
|
|
101
|
-
if (value && typeof value === "object") {
|
|
102
|
-
next.push(value);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
return obj;
|
|
108
|
-
}
|
|
109
|
-
function parse(text, reviver, options) {
|
|
110
|
-
const stackTraceLimit = Error.stackTraceLimit;
|
|
111
|
-
Error.stackTraceLimit = 0;
|
|
112
|
-
try {
|
|
113
|
-
return _parse(text, reviver, options);
|
|
114
|
-
} finally {
|
|
115
|
-
Error.stackTraceLimit = stackTraceLimit;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
function safeParse(text, reviver) {
|
|
119
|
-
const stackTraceLimit = Error.stackTraceLimit;
|
|
120
|
-
Error.stackTraceLimit = 0;
|
|
121
|
-
try {
|
|
122
|
-
return _parse(text, reviver, { safe: true });
|
|
123
|
-
} catch (_e) {
|
|
124
|
-
return null;
|
|
125
|
-
} finally {
|
|
126
|
-
Error.stackTraceLimit = stackTraceLimit;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
module.exports = parse;
|
|
130
|
-
module.exports.default = parse;
|
|
131
|
-
module.exports.parse = parse;
|
|
132
|
-
module.exports.safeParse = safeParse;
|
|
133
|
-
module.exports.scan = filter;
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
// ../../node_modules/.pnpm/@ai-sdk+provider@1.1.3/node_modules/@ai-sdk/provider/dist/index.mjs
|
|
138
|
-
var marker = "vercel.ai.error";
|
|
139
|
-
var symbol = Symbol.for(marker);
|
|
140
|
-
var _a;
|
|
141
|
-
var _AISDKError = class _AISDKError2 extends Error {
|
|
142
|
-
/**
|
|
143
|
-
* Creates an AI SDK Error.
|
|
144
|
-
*
|
|
145
|
-
* @param {Object} params - The parameters for creating the error.
|
|
146
|
-
* @param {string} params.name - The name of the error.
|
|
147
|
-
* @param {string} params.message - The error message.
|
|
148
|
-
* @param {unknown} [params.cause] - The underlying cause of the error.
|
|
149
|
-
*/
|
|
150
|
-
constructor({
|
|
151
|
-
name: name14,
|
|
152
|
-
message,
|
|
153
|
-
cause
|
|
154
|
-
}) {
|
|
155
|
-
super(message);
|
|
156
|
-
this[_a] = true;
|
|
157
|
-
this.name = name14;
|
|
158
|
-
this.cause = cause;
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* Checks if the given error is an AI SDK Error.
|
|
162
|
-
* @param {unknown} error - The error to check.
|
|
163
|
-
* @returns {boolean} True if the error is an AI SDK Error, false otherwise.
|
|
164
|
-
*/
|
|
165
|
-
static isInstance(error) {
|
|
166
|
-
return _AISDKError2.hasMarker(error, marker);
|
|
167
|
-
}
|
|
168
|
-
static hasMarker(error, marker15) {
|
|
169
|
-
const markerSymbol = Symbol.for(marker15);
|
|
170
|
-
return error != null && typeof error === "object" && markerSymbol in error && typeof error[markerSymbol] === "boolean" && error[markerSymbol] === true;
|
|
171
|
-
}
|
|
172
|
-
};
|
|
173
|
-
_a = symbol;
|
|
174
|
-
var AISDKError = _AISDKError;
|
|
175
|
-
function getErrorMessage(error) {
|
|
176
|
-
if (error == null) {
|
|
177
|
-
return "unknown error";
|
|
178
|
-
}
|
|
179
|
-
if (typeof error === "string") {
|
|
180
|
-
return error;
|
|
181
|
-
}
|
|
182
|
-
if (error instanceof Error) {
|
|
183
|
-
return error.message;
|
|
184
|
-
}
|
|
185
|
-
return JSON.stringify(error);
|
|
186
|
-
}
|
|
187
|
-
var name3 = "AI_InvalidArgumentError";
|
|
188
|
-
var marker4 = `vercel.ai.error.${name3}`;
|
|
189
|
-
var symbol4 = Symbol.for(marker4);
|
|
190
|
-
var _a4;
|
|
191
|
-
var InvalidArgumentError = class extends AISDKError {
|
|
192
|
-
constructor({
|
|
193
|
-
message,
|
|
194
|
-
cause,
|
|
195
|
-
argument
|
|
196
|
-
}) {
|
|
197
|
-
super({ name: name3, message, cause });
|
|
198
|
-
this[_a4] = true;
|
|
199
|
-
this.argument = argument;
|
|
200
|
-
}
|
|
201
|
-
static isInstance(error) {
|
|
202
|
-
return AISDKError.hasMarker(error, marker4);
|
|
203
|
-
}
|
|
204
|
-
};
|
|
205
|
-
_a4 = symbol4;
|
|
206
|
-
var name6 = "AI_JSONParseError";
|
|
207
|
-
var marker7 = `vercel.ai.error.${name6}`;
|
|
208
|
-
var symbol7 = Symbol.for(marker7);
|
|
209
|
-
var _a7;
|
|
210
|
-
var JSONParseError = class extends AISDKError {
|
|
211
|
-
constructor({ text, cause }) {
|
|
212
|
-
super({
|
|
213
|
-
name: name6,
|
|
214
|
-
message: `JSON parsing failed: Text: ${text}.
|
|
215
|
-
Error message: ${getErrorMessage(cause)}`,
|
|
216
|
-
cause
|
|
217
|
-
});
|
|
218
|
-
this[_a7] = true;
|
|
219
|
-
this.text = text;
|
|
220
|
-
}
|
|
221
|
-
static isInstance(error) {
|
|
222
|
-
return AISDKError.hasMarker(error, marker7);
|
|
223
|
-
}
|
|
224
|
-
};
|
|
225
|
-
_a7 = symbol7;
|
|
226
|
-
var name12 = "AI_TypeValidationError";
|
|
227
|
-
var marker13 = `vercel.ai.error.${name12}`;
|
|
228
|
-
var symbol13 = Symbol.for(marker13);
|
|
229
|
-
var _a13;
|
|
230
|
-
var _TypeValidationError = class _TypeValidationError2 extends AISDKError {
|
|
231
|
-
constructor({ value, cause }) {
|
|
232
|
-
super({
|
|
233
|
-
name: name12,
|
|
234
|
-
message: `Type validation failed: Value: ${JSON.stringify(value)}.
|
|
235
|
-
Error message: ${getErrorMessage(cause)}`,
|
|
236
|
-
cause
|
|
237
|
-
});
|
|
238
|
-
this[_a13] = true;
|
|
239
|
-
this.value = value;
|
|
240
|
-
}
|
|
241
|
-
static isInstance(error) {
|
|
242
|
-
return AISDKError.hasMarker(error, marker13);
|
|
243
|
-
}
|
|
244
|
-
/**
|
|
245
|
-
* Wraps an error into a TypeValidationError.
|
|
246
|
-
* If the cause is already a TypeValidationError with the same value, it returns the cause.
|
|
247
|
-
* Otherwise, it creates a new TypeValidationError.
|
|
248
|
-
*
|
|
249
|
-
* @param {Object} params - The parameters for wrapping the error.
|
|
250
|
-
* @param {unknown} params.value - The value that failed validation.
|
|
251
|
-
* @param {unknown} params.cause - The original error or cause of the validation failure.
|
|
252
|
-
* @returns {TypeValidationError} A TypeValidationError instance.
|
|
253
|
-
*/
|
|
254
|
-
static wrap({
|
|
255
|
-
value,
|
|
256
|
-
cause
|
|
257
|
-
}) {
|
|
258
|
-
return _TypeValidationError2.isInstance(cause) && cause.value === value ? cause : new _TypeValidationError2({ value, cause });
|
|
259
|
-
}
|
|
260
|
-
};
|
|
261
|
-
_a13 = symbol13;
|
|
262
|
-
var TypeValidationError = _TypeValidationError;
|
|
263
|
-
|
|
264
|
-
// ../../node_modules/.pnpm/nanoid@3.3.11/node_modules/nanoid/non-secure/index.js
|
|
265
|
-
var customAlphabet = (alphabet, defaultSize = 21) => {
|
|
266
|
-
return (size = defaultSize) => {
|
|
267
|
-
let id = "";
|
|
268
|
-
let i = size | 0;
|
|
269
|
-
while (i--) {
|
|
270
|
-
id += alphabet[Math.random() * alphabet.length | 0];
|
|
271
|
-
}
|
|
272
|
-
return id;
|
|
273
|
-
};
|
|
274
|
-
};
|
|
275
|
-
|
|
276
|
-
// ../../node_modules/.pnpm/@ai-sdk+provider-utils@2.2.8_zod@4.3.6/node_modules/@ai-sdk/provider-utils/dist/index.mjs
|
|
277
|
-
var import_secure_json_parse = __toESM(require_secure_json_parse());
|
|
278
|
-
var createIdGenerator = ({
|
|
279
|
-
prefix,
|
|
280
|
-
size: defaultSize = 16,
|
|
281
|
-
alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
|
282
|
-
separator = "-"
|
|
283
|
-
} = {}) => {
|
|
284
|
-
const generator = customAlphabet(alphabet, defaultSize);
|
|
285
|
-
if (prefix == null) {
|
|
286
|
-
return generator;
|
|
287
|
-
}
|
|
288
|
-
if (alphabet.includes(separator)) {
|
|
289
|
-
throw new InvalidArgumentError({
|
|
290
|
-
argument: "separator",
|
|
291
|
-
message: `The separator "${separator}" must not be part of the alphabet "${alphabet}".`
|
|
292
|
-
});
|
|
293
|
-
}
|
|
294
|
-
return (size) => `${prefix}${separator}${generator(size)}`;
|
|
295
|
-
};
|
|
296
|
-
createIdGenerator();
|
|
297
|
-
var validatorSymbol = /* @__PURE__ */ Symbol.for("vercel.ai.validator");
|
|
298
|
-
function validator(validate) {
|
|
299
|
-
return { [validatorSymbol]: true, validate };
|
|
300
|
-
}
|
|
301
|
-
function isValidator(value) {
|
|
302
|
-
return typeof value === "object" && value !== null && validatorSymbol in value && value[validatorSymbol] === true && "validate" in value;
|
|
303
|
-
}
|
|
304
|
-
function asValidator(value) {
|
|
305
|
-
return isValidator(value) ? value : zodValidator(value);
|
|
306
|
-
}
|
|
307
|
-
function zodValidator(zodSchema) {
|
|
308
|
-
return validator((value) => {
|
|
309
|
-
const result = zodSchema.safeParse(value);
|
|
310
|
-
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
|
311
|
-
});
|
|
312
|
-
}
|
|
313
|
-
function safeValidateTypes({
|
|
314
|
-
value,
|
|
315
|
-
schema
|
|
316
|
-
}) {
|
|
317
|
-
const validator2 = asValidator(schema);
|
|
318
|
-
try {
|
|
319
|
-
if (validator2.validate == null) {
|
|
320
|
-
return { success: true, value };
|
|
321
|
-
}
|
|
322
|
-
const result = validator2.validate(value);
|
|
323
|
-
if (result.success) {
|
|
324
|
-
return result;
|
|
325
|
-
}
|
|
326
|
-
return {
|
|
327
|
-
success: false,
|
|
328
|
-
error: TypeValidationError.wrap({ value, cause: result.error })
|
|
329
|
-
};
|
|
330
|
-
} catch (error) {
|
|
331
|
-
return {
|
|
332
|
-
success: false,
|
|
333
|
-
error: TypeValidationError.wrap({ value, cause: error })
|
|
334
|
-
};
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
function safeParseJSON({
|
|
338
|
-
text,
|
|
339
|
-
schema
|
|
340
|
-
}) {
|
|
341
|
-
try {
|
|
342
|
-
const value = import_secure_json_parse.default.parse(text);
|
|
343
|
-
if (schema == null) {
|
|
344
|
-
return { success: true, value, rawValue: value };
|
|
345
|
-
}
|
|
346
|
-
const validationResult = safeValidateTypes({ value, schema });
|
|
347
|
-
return validationResult.success ? { ...validationResult, rawValue: value } : validationResult;
|
|
348
|
-
} catch (error) {
|
|
349
|
-
return {
|
|
350
|
-
success: false,
|
|
351
|
-
error: JSONParseError.isInstance(error) ? error : new JSONParseError({ text, cause: error })
|
|
352
|
-
};
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
// ../../node_modules/.pnpm/zod-to-json-schema@3.25.1_zod@4.3.6/node_modules/zod-to-json-schema/dist/esm/parsers/string.js
|
|
357
|
-
new Set("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvxyz0123456789");
|
|
358
|
-
function fixJson(input) {
|
|
359
|
-
const stack = ["ROOT"];
|
|
360
|
-
let lastValidIndex = -1;
|
|
361
|
-
let literalStart = null;
|
|
362
|
-
function processValueStart(char, i, swapState) {
|
|
363
|
-
{
|
|
364
|
-
switch (char) {
|
|
365
|
-
case '"': {
|
|
366
|
-
lastValidIndex = i;
|
|
367
|
-
stack.pop();
|
|
368
|
-
stack.push(swapState);
|
|
369
|
-
stack.push("INSIDE_STRING");
|
|
370
|
-
break;
|
|
371
|
-
}
|
|
372
|
-
case "f":
|
|
373
|
-
case "t":
|
|
374
|
-
case "n": {
|
|
375
|
-
lastValidIndex = i;
|
|
376
|
-
literalStart = i;
|
|
377
|
-
stack.pop();
|
|
378
|
-
stack.push(swapState);
|
|
379
|
-
stack.push("INSIDE_LITERAL");
|
|
380
|
-
break;
|
|
381
|
-
}
|
|
382
|
-
case "-": {
|
|
383
|
-
stack.pop();
|
|
384
|
-
stack.push(swapState);
|
|
385
|
-
stack.push("INSIDE_NUMBER");
|
|
386
|
-
break;
|
|
387
|
-
}
|
|
388
|
-
case "0":
|
|
389
|
-
case "1":
|
|
390
|
-
case "2":
|
|
391
|
-
case "3":
|
|
392
|
-
case "4":
|
|
393
|
-
case "5":
|
|
394
|
-
case "6":
|
|
395
|
-
case "7":
|
|
396
|
-
case "8":
|
|
397
|
-
case "9": {
|
|
398
|
-
lastValidIndex = i;
|
|
399
|
-
stack.pop();
|
|
400
|
-
stack.push(swapState);
|
|
401
|
-
stack.push("INSIDE_NUMBER");
|
|
402
|
-
break;
|
|
403
|
-
}
|
|
404
|
-
case "{": {
|
|
405
|
-
lastValidIndex = i;
|
|
406
|
-
stack.pop();
|
|
407
|
-
stack.push(swapState);
|
|
408
|
-
stack.push("INSIDE_OBJECT_START");
|
|
409
|
-
break;
|
|
410
|
-
}
|
|
411
|
-
case "[": {
|
|
412
|
-
lastValidIndex = i;
|
|
413
|
-
stack.pop();
|
|
414
|
-
stack.push(swapState);
|
|
415
|
-
stack.push("INSIDE_ARRAY_START");
|
|
416
|
-
break;
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
function processAfterObjectValue(char, i) {
|
|
422
|
-
switch (char) {
|
|
423
|
-
case ",": {
|
|
424
|
-
stack.pop();
|
|
425
|
-
stack.push("INSIDE_OBJECT_AFTER_COMMA");
|
|
426
|
-
break;
|
|
427
|
-
}
|
|
428
|
-
case "}": {
|
|
429
|
-
lastValidIndex = i;
|
|
430
|
-
stack.pop();
|
|
431
|
-
break;
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
}
|
|
435
|
-
function processAfterArrayValue(char, i) {
|
|
436
|
-
switch (char) {
|
|
437
|
-
case ",": {
|
|
438
|
-
stack.pop();
|
|
439
|
-
stack.push("INSIDE_ARRAY_AFTER_COMMA");
|
|
440
|
-
break;
|
|
441
|
-
}
|
|
442
|
-
case "]": {
|
|
443
|
-
lastValidIndex = i;
|
|
444
|
-
stack.pop();
|
|
445
|
-
break;
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
for (let i = 0; i < input.length; i++) {
|
|
450
|
-
const char = input[i];
|
|
451
|
-
const currentState = stack[stack.length - 1];
|
|
452
|
-
switch (currentState) {
|
|
453
|
-
case "ROOT":
|
|
454
|
-
processValueStart(char, i, "FINISH");
|
|
455
|
-
break;
|
|
456
|
-
case "INSIDE_OBJECT_START": {
|
|
457
|
-
switch (char) {
|
|
458
|
-
case '"': {
|
|
459
|
-
stack.pop();
|
|
460
|
-
stack.push("INSIDE_OBJECT_KEY");
|
|
461
|
-
break;
|
|
462
|
-
}
|
|
463
|
-
case "}": {
|
|
464
|
-
lastValidIndex = i;
|
|
465
|
-
stack.pop();
|
|
466
|
-
break;
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
break;
|
|
470
|
-
}
|
|
471
|
-
case "INSIDE_OBJECT_AFTER_COMMA": {
|
|
472
|
-
switch (char) {
|
|
473
|
-
case '"': {
|
|
474
|
-
stack.pop();
|
|
475
|
-
stack.push("INSIDE_OBJECT_KEY");
|
|
476
|
-
break;
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
break;
|
|
480
|
-
}
|
|
481
|
-
case "INSIDE_OBJECT_KEY": {
|
|
482
|
-
switch (char) {
|
|
483
|
-
case '"': {
|
|
484
|
-
stack.pop();
|
|
485
|
-
stack.push("INSIDE_OBJECT_AFTER_KEY");
|
|
486
|
-
break;
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
break;
|
|
490
|
-
}
|
|
491
|
-
case "INSIDE_OBJECT_AFTER_KEY": {
|
|
492
|
-
switch (char) {
|
|
493
|
-
case ":": {
|
|
494
|
-
stack.pop();
|
|
495
|
-
stack.push("INSIDE_OBJECT_BEFORE_VALUE");
|
|
496
|
-
break;
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
break;
|
|
500
|
-
}
|
|
501
|
-
case "INSIDE_OBJECT_BEFORE_VALUE": {
|
|
502
|
-
processValueStart(char, i, "INSIDE_OBJECT_AFTER_VALUE");
|
|
503
|
-
break;
|
|
504
|
-
}
|
|
505
|
-
case "INSIDE_OBJECT_AFTER_VALUE": {
|
|
506
|
-
processAfterObjectValue(char, i);
|
|
507
|
-
break;
|
|
508
|
-
}
|
|
509
|
-
case "INSIDE_STRING": {
|
|
510
|
-
switch (char) {
|
|
511
|
-
case '"': {
|
|
512
|
-
stack.pop();
|
|
513
|
-
lastValidIndex = i;
|
|
514
|
-
break;
|
|
515
|
-
}
|
|
516
|
-
case "\\": {
|
|
517
|
-
stack.push("INSIDE_STRING_ESCAPE");
|
|
518
|
-
break;
|
|
519
|
-
}
|
|
520
|
-
default: {
|
|
521
|
-
lastValidIndex = i;
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
break;
|
|
525
|
-
}
|
|
526
|
-
case "INSIDE_ARRAY_START": {
|
|
527
|
-
switch (char) {
|
|
528
|
-
case "]": {
|
|
529
|
-
lastValidIndex = i;
|
|
530
|
-
stack.pop();
|
|
531
|
-
break;
|
|
532
|
-
}
|
|
533
|
-
default: {
|
|
534
|
-
lastValidIndex = i;
|
|
535
|
-
processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
|
|
536
|
-
break;
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
break;
|
|
540
|
-
}
|
|
541
|
-
case "INSIDE_ARRAY_AFTER_VALUE": {
|
|
542
|
-
switch (char) {
|
|
543
|
-
case ",": {
|
|
544
|
-
stack.pop();
|
|
545
|
-
stack.push("INSIDE_ARRAY_AFTER_COMMA");
|
|
546
|
-
break;
|
|
547
|
-
}
|
|
548
|
-
case "]": {
|
|
549
|
-
lastValidIndex = i;
|
|
550
|
-
stack.pop();
|
|
551
|
-
break;
|
|
552
|
-
}
|
|
553
|
-
default: {
|
|
554
|
-
lastValidIndex = i;
|
|
555
|
-
break;
|
|
556
|
-
}
|
|
557
|
-
}
|
|
558
|
-
break;
|
|
559
|
-
}
|
|
560
|
-
case "INSIDE_ARRAY_AFTER_COMMA": {
|
|
561
|
-
processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
|
|
562
|
-
break;
|
|
563
|
-
}
|
|
564
|
-
case "INSIDE_STRING_ESCAPE": {
|
|
565
|
-
stack.pop();
|
|
566
|
-
lastValidIndex = i;
|
|
567
|
-
break;
|
|
568
|
-
}
|
|
569
|
-
case "INSIDE_NUMBER": {
|
|
570
|
-
switch (char) {
|
|
571
|
-
case "0":
|
|
572
|
-
case "1":
|
|
573
|
-
case "2":
|
|
574
|
-
case "3":
|
|
575
|
-
case "4":
|
|
576
|
-
case "5":
|
|
577
|
-
case "6":
|
|
578
|
-
case "7":
|
|
579
|
-
case "8":
|
|
580
|
-
case "9": {
|
|
581
|
-
lastValidIndex = i;
|
|
582
|
-
break;
|
|
583
|
-
}
|
|
584
|
-
case "e":
|
|
585
|
-
case "E":
|
|
586
|
-
case "-":
|
|
587
|
-
case ".": {
|
|
588
|
-
break;
|
|
589
|
-
}
|
|
590
|
-
case ",": {
|
|
591
|
-
stack.pop();
|
|
592
|
-
if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
|
|
593
|
-
processAfterArrayValue(char, i);
|
|
594
|
-
}
|
|
595
|
-
if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
|
|
596
|
-
processAfterObjectValue(char, i);
|
|
597
|
-
}
|
|
598
|
-
break;
|
|
599
|
-
}
|
|
600
|
-
case "}": {
|
|
601
|
-
stack.pop();
|
|
602
|
-
if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
|
|
603
|
-
processAfterObjectValue(char, i);
|
|
604
|
-
}
|
|
605
|
-
break;
|
|
606
|
-
}
|
|
607
|
-
case "]": {
|
|
608
|
-
stack.pop();
|
|
609
|
-
if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
|
|
610
|
-
processAfterArrayValue(char, i);
|
|
611
|
-
}
|
|
612
|
-
break;
|
|
613
|
-
}
|
|
614
|
-
default: {
|
|
615
|
-
stack.pop();
|
|
616
|
-
break;
|
|
617
|
-
}
|
|
618
|
-
}
|
|
619
|
-
break;
|
|
620
|
-
}
|
|
621
|
-
case "INSIDE_LITERAL": {
|
|
622
|
-
const partialLiteral = input.substring(literalStart, i + 1);
|
|
623
|
-
if (!"false".startsWith(partialLiteral) && !"true".startsWith(partialLiteral) && !"null".startsWith(partialLiteral)) {
|
|
624
|
-
stack.pop();
|
|
625
|
-
if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
|
|
626
|
-
processAfterObjectValue(char, i);
|
|
627
|
-
} else if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
|
|
628
|
-
processAfterArrayValue(char, i);
|
|
629
|
-
}
|
|
630
|
-
} else {
|
|
631
|
-
lastValidIndex = i;
|
|
632
|
-
}
|
|
633
|
-
break;
|
|
634
|
-
}
|
|
635
|
-
}
|
|
636
|
-
}
|
|
637
|
-
let result = input.slice(0, lastValidIndex + 1);
|
|
638
|
-
for (let i = stack.length - 1; i >= 0; i--) {
|
|
639
|
-
const state = stack[i];
|
|
640
|
-
switch (state) {
|
|
641
|
-
case "INSIDE_STRING": {
|
|
642
|
-
result += '"';
|
|
643
|
-
break;
|
|
644
|
-
}
|
|
645
|
-
case "INSIDE_OBJECT_KEY":
|
|
646
|
-
case "INSIDE_OBJECT_AFTER_KEY":
|
|
647
|
-
case "INSIDE_OBJECT_AFTER_COMMA":
|
|
648
|
-
case "INSIDE_OBJECT_START":
|
|
649
|
-
case "INSIDE_OBJECT_BEFORE_VALUE":
|
|
650
|
-
case "INSIDE_OBJECT_AFTER_VALUE": {
|
|
651
|
-
result += "}";
|
|
652
|
-
break;
|
|
653
|
-
}
|
|
654
|
-
case "INSIDE_ARRAY_START":
|
|
655
|
-
case "INSIDE_ARRAY_AFTER_COMMA":
|
|
656
|
-
case "INSIDE_ARRAY_AFTER_VALUE": {
|
|
657
|
-
result += "]";
|
|
658
|
-
break;
|
|
659
|
-
}
|
|
660
|
-
case "INSIDE_LITERAL": {
|
|
661
|
-
const partialLiteral = input.substring(literalStart, input.length);
|
|
662
|
-
if ("true".startsWith(partialLiteral)) {
|
|
663
|
-
result += "true".slice(partialLiteral.length);
|
|
664
|
-
} else if ("false".startsWith(partialLiteral)) {
|
|
665
|
-
result += "false".slice(partialLiteral.length);
|
|
666
|
-
} else if ("null".startsWith(partialLiteral)) {
|
|
667
|
-
result += "null".slice(partialLiteral.length);
|
|
668
|
-
}
|
|
669
|
-
}
|
|
670
|
-
}
|
|
671
|
-
}
|
|
672
|
-
return result;
|
|
673
|
-
}
|
|
674
|
-
function parsePartialJson(jsonText) {
|
|
675
|
-
if (jsonText === void 0) {
|
|
676
|
-
return { value: void 0, state: "undefined-input" };
|
|
677
|
-
}
|
|
678
|
-
let result = safeParseJSON({ text: jsonText });
|
|
679
|
-
if (result.success) {
|
|
680
|
-
return { value: result.value, state: "successful-parse" };
|
|
681
|
-
}
|
|
682
|
-
result = safeParseJSON({ text: fixJson(jsonText) });
|
|
683
|
-
if (result.success) {
|
|
684
|
-
return { value: result.value, state: "repaired-parse" };
|
|
685
|
-
}
|
|
686
|
-
return { value: void 0, state: "failed-parse" };
|
|
687
|
-
}
|
|
688
|
-
var textStreamPart2 = {
|
|
689
|
-
code: "0",
|
|
690
|
-
name: "text",
|
|
691
|
-
parse: (value) => {
|
|
692
|
-
if (typeof value !== "string") {
|
|
693
|
-
throw new Error('"text" parts expect a string value.');
|
|
694
|
-
}
|
|
695
|
-
return { type: "text", value };
|
|
696
|
-
}
|
|
697
|
-
};
|
|
698
|
-
var dataStreamPart = {
|
|
699
|
-
code: "2",
|
|
700
|
-
name: "data",
|
|
701
|
-
parse: (value) => {
|
|
702
|
-
if (!Array.isArray(value)) {
|
|
703
|
-
throw new Error('"data" parts expect an array value.');
|
|
704
|
-
}
|
|
705
|
-
return { type: "data", value };
|
|
706
|
-
}
|
|
707
|
-
};
|
|
708
|
-
var errorStreamPart2 = {
|
|
709
|
-
code: "3",
|
|
710
|
-
name: "error",
|
|
711
|
-
parse: (value) => {
|
|
712
|
-
if (typeof value !== "string") {
|
|
713
|
-
throw new Error('"error" parts expect a string value.');
|
|
714
|
-
}
|
|
715
|
-
return { type: "error", value };
|
|
716
|
-
}
|
|
717
|
-
};
|
|
718
|
-
var messageAnnotationsStreamPart = {
|
|
719
|
-
code: "8",
|
|
720
|
-
name: "message_annotations",
|
|
721
|
-
parse: (value) => {
|
|
722
|
-
if (!Array.isArray(value)) {
|
|
723
|
-
throw new Error('"message_annotations" parts expect an array value.');
|
|
724
|
-
}
|
|
725
|
-
return { type: "message_annotations", value };
|
|
726
|
-
}
|
|
727
|
-
};
|
|
728
|
-
var toolCallStreamPart = {
|
|
729
|
-
code: "9",
|
|
730
|
-
name: "tool_call",
|
|
731
|
-
parse: (value) => {
|
|
732
|
-
if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("toolName" in value) || typeof value.toolName !== "string" || !("args" in value) || typeof value.args !== "object") {
|
|
733
|
-
throw new Error(
|
|
734
|
-
'"tool_call" parts expect an object with a "toolCallId", "toolName", and "args" property.'
|
|
735
|
-
);
|
|
736
|
-
}
|
|
737
|
-
return {
|
|
738
|
-
type: "tool_call",
|
|
739
|
-
value
|
|
740
|
-
};
|
|
741
|
-
}
|
|
742
|
-
};
|
|
743
|
-
var toolResultStreamPart = {
|
|
744
|
-
code: "a",
|
|
745
|
-
name: "tool_result",
|
|
746
|
-
parse: (value) => {
|
|
747
|
-
if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("result" in value)) {
|
|
748
|
-
throw new Error(
|
|
749
|
-
'"tool_result" parts expect an object with a "toolCallId" and a "result" property.'
|
|
750
|
-
);
|
|
751
|
-
}
|
|
752
|
-
return {
|
|
753
|
-
type: "tool_result",
|
|
754
|
-
value
|
|
755
|
-
};
|
|
756
|
-
}
|
|
757
|
-
};
|
|
758
|
-
var toolCallStreamingStartStreamPart = {
|
|
759
|
-
code: "b",
|
|
760
|
-
name: "tool_call_streaming_start",
|
|
761
|
-
parse: (value) => {
|
|
762
|
-
if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("toolName" in value) || typeof value.toolName !== "string") {
|
|
763
|
-
throw new Error(
|
|
764
|
-
'"tool_call_streaming_start" parts expect an object with a "toolCallId" and "toolName" property.'
|
|
765
|
-
);
|
|
766
|
-
}
|
|
767
|
-
return {
|
|
768
|
-
type: "tool_call_streaming_start",
|
|
769
|
-
value
|
|
770
|
-
};
|
|
771
|
-
}
|
|
772
|
-
};
|
|
773
|
-
var toolCallDeltaStreamPart = {
|
|
774
|
-
code: "c",
|
|
775
|
-
name: "tool_call_delta",
|
|
776
|
-
parse: (value) => {
|
|
777
|
-
if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("argsTextDelta" in value) || typeof value.argsTextDelta !== "string") {
|
|
778
|
-
throw new Error(
|
|
779
|
-
'"tool_call_delta" parts expect an object with a "toolCallId" and "argsTextDelta" property.'
|
|
780
|
-
);
|
|
781
|
-
}
|
|
782
|
-
return {
|
|
783
|
-
type: "tool_call_delta",
|
|
784
|
-
value
|
|
785
|
-
};
|
|
786
|
-
}
|
|
787
|
-
};
|
|
788
|
-
var finishMessageStreamPart = {
|
|
789
|
-
code: "d",
|
|
790
|
-
name: "finish_message",
|
|
791
|
-
parse: (value) => {
|
|
792
|
-
if (value == null || typeof value !== "object" || !("finishReason" in value) || typeof value.finishReason !== "string") {
|
|
793
|
-
throw new Error(
|
|
794
|
-
'"finish_message" parts expect an object with a "finishReason" property.'
|
|
795
|
-
);
|
|
796
|
-
}
|
|
797
|
-
const result = {
|
|
798
|
-
finishReason: value.finishReason
|
|
799
|
-
};
|
|
800
|
-
if ("usage" in value && value.usage != null && typeof value.usage === "object" && "promptTokens" in value.usage && "completionTokens" in value.usage) {
|
|
801
|
-
result.usage = {
|
|
802
|
-
promptTokens: typeof value.usage.promptTokens === "number" ? value.usage.promptTokens : Number.NaN,
|
|
803
|
-
completionTokens: typeof value.usage.completionTokens === "number" ? value.usage.completionTokens : Number.NaN
|
|
804
|
-
};
|
|
805
|
-
}
|
|
806
|
-
return {
|
|
807
|
-
type: "finish_message",
|
|
808
|
-
value: result
|
|
809
|
-
};
|
|
810
|
-
}
|
|
811
|
-
};
|
|
812
|
-
var finishStepStreamPart = {
|
|
813
|
-
code: "e",
|
|
814
|
-
name: "finish_step",
|
|
815
|
-
parse: (value) => {
|
|
816
|
-
if (value == null || typeof value !== "object" || !("finishReason" in value) || typeof value.finishReason !== "string") {
|
|
817
|
-
throw new Error(
|
|
818
|
-
'"finish_step" parts expect an object with a "finishReason" property.'
|
|
819
|
-
);
|
|
820
|
-
}
|
|
821
|
-
const result = {
|
|
822
|
-
finishReason: value.finishReason,
|
|
823
|
-
isContinued: false
|
|
824
|
-
};
|
|
825
|
-
if ("usage" in value && value.usage != null && typeof value.usage === "object" && "promptTokens" in value.usage && "completionTokens" in value.usage) {
|
|
826
|
-
result.usage = {
|
|
827
|
-
promptTokens: typeof value.usage.promptTokens === "number" ? value.usage.promptTokens : Number.NaN,
|
|
828
|
-
completionTokens: typeof value.usage.completionTokens === "number" ? value.usage.completionTokens : Number.NaN
|
|
829
|
-
};
|
|
830
|
-
}
|
|
831
|
-
if ("isContinued" in value && typeof value.isContinued === "boolean") {
|
|
832
|
-
result.isContinued = value.isContinued;
|
|
833
|
-
}
|
|
834
|
-
return {
|
|
835
|
-
type: "finish_step",
|
|
836
|
-
value: result
|
|
837
|
-
};
|
|
838
|
-
}
|
|
839
|
-
};
|
|
840
|
-
var startStepStreamPart = {
|
|
841
|
-
code: "f",
|
|
842
|
-
name: "start_step",
|
|
843
|
-
parse: (value) => {
|
|
844
|
-
if (value == null || typeof value !== "object" || !("messageId" in value) || typeof value.messageId !== "string") {
|
|
845
|
-
throw new Error(
|
|
846
|
-
'"start_step" parts expect an object with an "id" property.'
|
|
847
|
-
);
|
|
848
|
-
}
|
|
849
|
-
return {
|
|
850
|
-
type: "start_step",
|
|
851
|
-
value: {
|
|
852
|
-
messageId: value.messageId
|
|
853
|
-
}
|
|
854
|
-
};
|
|
855
|
-
}
|
|
856
|
-
};
|
|
857
|
-
var reasoningStreamPart = {
|
|
858
|
-
code: "g",
|
|
859
|
-
name: "reasoning",
|
|
860
|
-
parse: (value) => {
|
|
861
|
-
if (typeof value !== "string") {
|
|
862
|
-
throw new Error('"reasoning" parts expect a string value.');
|
|
863
|
-
}
|
|
864
|
-
return { type: "reasoning", value };
|
|
865
|
-
}
|
|
866
|
-
};
|
|
867
|
-
var sourcePart = {
|
|
868
|
-
code: "h",
|
|
869
|
-
name: "source",
|
|
870
|
-
parse: (value) => {
|
|
871
|
-
if (value == null || typeof value !== "object") {
|
|
872
|
-
throw new Error('"source" parts expect a Source object.');
|
|
873
|
-
}
|
|
874
|
-
return {
|
|
875
|
-
type: "source",
|
|
876
|
-
value
|
|
877
|
-
};
|
|
878
|
-
}
|
|
879
|
-
};
|
|
880
|
-
var redactedReasoningStreamPart = {
|
|
881
|
-
code: "i",
|
|
882
|
-
name: "redacted_reasoning",
|
|
883
|
-
parse: (value) => {
|
|
884
|
-
if (value == null || typeof value !== "object" || !("data" in value) || typeof value.data !== "string") {
|
|
885
|
-
throw new Error(
|
|
886
|
-
'"redacted_reasoning" parts expect an object with a "data" property.'
|
|
887
|
-
);
|
|
888
|
-
}
|
|
889
|
-
return { type: "redacted_reasoning", value: { data: value.data } };
|
|
890
|
-
}
|
|
891
|
-
};
|
|
892
|
-
var reasoningSignatureStreamPart = {
|
|
893
|
-
code: "j",
|
|
894
|
-
name: "reasoning_signature",
|
|
895
|
-
parse: (value) => {
|
|
896
|
-
if (value == null || typeof value !== "object" || !("signature" in value) || typeof value.signature !== "string") {
|
|
897
|
-
throw new Error(
|
|
898
|
-
'"reasoning_signature" parts expect an object with a "signature" property.'
|
|
899
|
-
);
|
|
900
|
-
}
|
|
901
|
-
return {
|
|
902
|
-
type: "reasoning_signature",
|
|
903
|
-
value: { signature: value.signature }
|
|
904
|
-
};
|
|
905
|
-
}
|
|
906
|
-
};
|
|
907
|
-
var fileStreamPart = {
|
|
908
|
-
code: "k",
|
|
909
|
-
name: "file",
|
|
910
|
-
parse: (value) => {
|
|
911
|
-
if (value == null || typeof value !== "object" || !("data" in value) || typeof value.data !== "string" || !("mimeType" in value) || typeof value.mimeType !== "string") {
|
|
912
|
-
throw new Error(
|
|
913
|
-
'"file" parts expect an object with a "data" and "mimeType" property.'
|
|
914
|
-
);
|
|
915
|
-
}
|
|
916
|
-
return { type: "file", value };
|
|
917
|
-
}
|
|
918
|
-
};
|
|
919
|
-
var dataStreamParts = [
|
|
920
|
-
textStreamPart2,
|
|
921
|
-
dataStreamPart,
|
|
922
|
-
errorStreamPart2,
|
|
923
|
-
messageAnnotationsStreamPart,
|
|
924
|
-
toolCallStreamPart,
|
|
925
|
-
toolResultStreamPart,
|
|
926
|
-
toolCallStreamingStartStreamPart,
|
|
927
|
-
toolCallDeltaStreamPart,
|
|
928
|
-
finishMessageStreamPart,
|
|
929
|
-
finishStepStreamPart,
|
|
930
|
-
startStepStreamPart,
|
|
931
|
-
reasoningStreamPart,
|
|
932
|
-
sourcePart,
|
|
933
|
-
redactedReasoningStreamPart,
|
|
934
|
-
reasoningSignatureStreamPart,
|
|
935
|
-
fileStreamPart
|
|
936
|
-
];
|
|
937
|
-
var dataStreamPartsByCode = Object.fromEntries(
|
|
938
|
-
dataStreamParts.map((part) => [part.code, part])
|
|
939
|
-
);
|
|
940
|
-
Object.fromEntries(
|
|
941
|
-
dataStreamParts.map((part) => [part.name, part.code])
|
|
942
|
-
);
|
|
943
|
-
var validCodes2 = dataStreamParts.map((part) => part.code);
|
|
944
|
-
var parseDataStreamPart = (line) => {
|
|
945
|
-
const firstSeparatorIndex = line.indexOf(":");
|
|
946
|
-
if (firstSeparatorIndex === -1) {
|
|
947
|
-
throw new Error("Failed to parse stream string. No separator found.");
|
|
948
|
-
}
|
|
949
|
-
const prefix = line.slice(0, firstSeparatorIndex);
|
|
950
|
-
if (!validCodes2.includes(prefix)) {
|
|
951
|
-
throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);
|
|
952
|
-
}
|
|
953
|
-
const code = prefix;
|
|
954
|
-
const textValue = line.slice(firstSeparatorIndex + 1);
|
|
955
|
-
const jsonValue = JSON.parse(textValue);
|
|
956
|
-
return dataStreamPartsByCode[code].parse(jsonValue);
|
|
957
|
-
};
|
|
958
|
-
var NEWLINE = "\n".charCodeAt(0);
|
|
959
|
-
function concatChunks(chunks, totalLength) {
|
|
960
|
-
const concatenatedChunks = new Uint8Array(totalLength);
|
|
961
|
-
let offset = 0;
|
|
962
|
-
for (const chunk of chunks) {
|
|
963
|
-
concatenatedChunks.set(chunk, offset);
|
|
964
|
-
offset += chunk.length;
|
|
965
|
-
}
|
|
966
|
-
chunks.length = 0;
|
|
967
|
-
return concatenatedChunks;
|
|
968
|
-
}
|
|
969
|
-
async function processDataStream({
|
|
970
|
-
stream,
|
|
971
|
-
onTextPart,
|
|
972
|
-
onReasoningPart,
|
|
973
|
-
onReasoningSignaturePart,
|
|
974
|
-
onRedactedReasoningPart,
|
|
975
|
-
onSourcePart,
|
|
976
|
-
onFilePart,
|
|
977
|
-
onDataPart,
|
|
978
|
-
onErrorPart,
|
|
979
|
-
onToolCallStreamingStartPart,
|
|
980
|
-
onToolCallDeltaPart,
|
|
981
|
-
onToolCallPart,
|
|
982
|
-
onToolResultPart,
|
|
983
|
-
onMessageAnnotationsPart,
|
|
984
|
-
onFinishMessagePart,
|
|
985
|
-
onFinishStepPart,
|
|
986
|
-
onStartStepPart
|
|
987
|
-
}) {
|
|
988
|
-
const reader = stream.getReader();
|
|
989
|
-
const decoder = new TextDecoder();
|
|
990
|
-
const chunks = [];
|
|
991
|
-
let totalLength = 0;
|
|
992
|
-
while (true) {
|
|
993
|
-
const { value } = await reader.read();
|
|
994
|
-
if (value) {
|
|
995
|
-
chunks.push(value);
|
|
996
|
-
totalLength += value.length;
|
|
997
|
-
if (value[value.length - 1] !== NEWLINE) {
|
|
998
|
-
continue;
|
|
999
|
-
}
|
|
1000
|
-
}
|
|
1001
|
-
if (chunks.length === 0) {
|
|
1002
|
-
break;
|
|
1003
|
-
}
|
|
1004
|
-
const concatenatedChunks = concatChunks(chunks, totalLength);
|
|
1005
|
-
totalLength = 0;
|
|
1006
|
-
const streamParts = decoder.decode(concatenatedChunks, { stream: true }).split("\n").filter((line) => line !== "").map(parseDataStreamPart);
|
|
1007
|
-
for (const { type, value: value2 } of streamParts) {
|
|
1008
|
-
switch (type) {
|
|
1009
|
-
case "text":
|
|
1010
|
-
await (onTextPart == null ? void 0 : onTextPart(value2));
|
|
1011
|
-
break;
|
|
1012
|
-
case "reasoning":
|
|
1013
|
-
await (onReasoningPart == null ? void 0 : onReasoningPart(value2));
|
|
1014
|
-
break;
|
|
1015
|
-
case "reasoning_signature":
|
|
1016
|
-
await (onReasoningSignaturePart == null ? void 0 : onReasoningSignaturePart(value2));
|
|
1017
|
-
break;
|
|
1018
|
-
case "redacted_reasoning":
|
|
1019
|
-
await (onRedactedReasoningPart == null ? void 0 : onRedactedReasoningPart(value2));
|
|
1020
|
-
break;
|
|
1021
|
-
case "file":
|
|
1022
|
-
await (onFilePart == null ? void 0 : onFilePart(value2));
|
|
1023
|
-
break;
|
|
1024
|
-
case "source":
|
|
1025
|
-
await (onSourcePart == null ? void 0 : onSourcePart(value2));
|
|
1026
|
-
break;
|
|
1027
|
-
case "data":
|
|
1028
|
-
await (onDataPart == null ? void 0 : onDataPart(value2));
|
|
1029
|
-
break;
|
|
1030
|
-
case "error":
|
|
1031
|
-
await (onErrorPart == null ? void 0 : onErrorPart(value2));
|
|
1032
|
-
break;
|
|
1033
|
-
case "message_annotations":
|
|
1034
|
-
await (onMessageAnnotationsPart == null ? void 0 : onMessageAnnotationsPart(value2));
|
|
1035
|
-
break;
|
|
1036
|
-
case "tool_call_streaming_start":
|
|
1037
|
-
await (onToolCallStreamingStartPart == null ? void 0 : onToolCallStreamingStartPart(value2));
|
|
1038
|
-
break;
|
|
1039
|
-
case "tool_call_delta":
|
|
1040
|
-
await (onToolCallDeltaPart == null ? void 0 : onToolCallDeltaPart(value2));
|
|
1041
|
-
break;
|
|
1042
|
-
case "tool_call":
|
|
1043
|
-
await (onToolCallPart == null ? void 0 : onToolCallPart(value2));
|
|
1044
|
-
break;
|
|
1045
|
-
case "tool_result":
|
|
1046
|
-
await (onToolResultPart == null ? void 0 : onToolResultPart(value2));
|
|
1047
|
-
break;
|
|
1048
|
-
case "finish_message":
|
|
1049
|
-
await (onFinishMessagePart == null ? void 0 : onFinishMessagePart(value2));
|
|
1050
|
-
break;
|
|
1051
|
-
case "finish_step":
|
|
1052
|
-
await (onFinishStepPart == null ? void 0 : onFinishStepPart(value2));
|
|
1053
|
-
break;
|
|
1054
|
-
case "start_step":
|
|
1055
|
-
await (onStartStepPart == null ? void 0 : onStartStepPart(value2));
|
|
1056
|
-
break;
|
|
1057
|
-
default: {
|
|
1058
|
-
const exhaustiveCheck = type;
|
|
1059
|
-
throw new Error(`Unknown stream part type: ${exhaustiveCheck}`);
|
|
1060
|
-
}
|
|
1061
|
-
}
|
|
1062
|
-
}
|
|
1063
|
-
}
|
|
1064
|
-
}
|
|
8
|
+
// src/resources/agent.ts
|
|
1065
9
|
function normalizeRoutePath(path) {
|
|
1066
10
|
let normalized = path.trim();
|
|
1067
11
|
if (normalized.includes("..") || normalized.includes("?") || normalized.includes("#")) {
|
|
@@ -1148,11 +92,11 @@ function requestContextQueryString(requestContext, delimiter = "?") {
|
|
|
1148
92
|
function isZodType(value) {
|
|
1149
93
|
return typeof value === "object" && value !== null && "_def" in value && "parse" in value && typeof value.parse === "function" && "safeParse" in value && typeof value.safeParse === "function";
|
|
1150
94
|
}
|
|
1151
|
-
function
|
|
95
|
+
function zodToJsonSchema(zodSchema) {
|
|
1152
96
|
if (!isZodType(zodSchema)) {
|
|
1153
97
|
return zodSchema;
|
|
1154
98
|
}
|
|
1155
|
-
return zodToJsonSchema(zodSchema);
|
|
99
|
+
return zodToJsonSchema$1(zodSchema);
|
|
1156
100
|
}
|
|
1157
101
|
|
|
1158
102
|
// src/utils/process-client-tools.ts
|
|
@@ -1162,24 +106,19 @@ function processClientTools(clientTools) {
|
|
|
1162
106
|
}
|
|
1163
107
|
return Object.fromEntries(
|
|
1164
108
|
Object.entries(clientTools).map(([key, value]) => {
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
}
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
inputSchema: value.inputSchema ? zodToJsonSchema2(value.inputSchema) : void 0,
|
|
1179
|
-
outputSchema: value.outputSchema ? zodToJsonSchema2(value.outputSchema) : void 0
|
|
1180
|
-
}
|
|
1181
|
-
];
|
|
1182
|
-
}
|
|
109
|
+
const tool = value;
|
|
110
|
+
return [
|
|
111
|
+
key,
|
|
112
|
+
{
|
|
113
|
+
...value,
|
|
114
|
+
// Serialize parameters (Vercel v4 tool format)
|
|
115
|
+
...tool.parameters !== void 0 ? { parameters: zodToJsonSchema(tool.parameters) } : {},
|
|
116
|
+
// Serialize inputSchema (Mastra tool, ClientTool, or Vercel v5 tool format)
|
|
117
|
+
...tool.inputSchema !== void 0 ? { inputSchema: zodToJsonSchema(tool.inputSchema) } : {},
|
|
118
|
+
// Serialize outputSchema
|
|
119
|
+
...tool.outputSchema !== void 0 ? { outputSchema: zodToJsonSchema(tool.outputSchema) } : {}
|
|
120
|
+
}
|
|
121
|
+
];
|
|
1183
122
|
})
|
|
1184
123
|
);
|
|
1185
124
|
}
|
|
@@ -1484,8 +423,8 @@ var Agent = class extends BaseResource {
|
|
|
1484
423
|
async generateLegacy(params) {
|
|
1485
424
|
const processedParams = {
|
|
1486
425
|
...params,
|
|
1487
|
-
output: params.output ?
|
|
1488
|
-
experimental_output: params.experimental_output ?
|
|
426
|
+
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
427
|
+
experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
|
|
1489
428
|
requestContext: parseClientRequestContext(params.requestContext),
|
|
1490
429
|
clientTools: processClientTools(params.clientTools)
|
|
1491
430
|
};
|
|
@@ -1844,8 +783,8 @@ var Agent = class extends BaseResource {
|
|
|
1844
783
|
async streamLegacy(params) {
|
|
1845
784
|
const processedParams = {
|
|
1846
785
|
...params,
|
|
1847
|
-
output: params.output ?
|
|
1848
|
-
experimental_output: params.experimental_output ?
|
|
786
|
+
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
787
|
+
experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
|
|
1849
788
|
requestContext: parseClientRequestContext(params.requestContext),
|
|
1850
789
|
clientTools: processClientTools(params.clientTools)
|
|
1851
790
|
};
|
|
@@ -2267,7 +1206,7 @@ var Agent = class extends BaseResource {
|
|
|
2267
1206
|
requestContext: parseClientRequestContext(params.requestContext),
|
|
2268
1207
|
structuredOutput: params.structuredOutput ? {
|
|
2269
1208
|
...params.structuredOutput,
|
|
2270
|
-
schema:
|
|
1209
|
+
schema: zodToJsonSchema(params.structuredOutput.schema)
|
|
2271
1210
|
} : void 0
|
|
2272
1211
|
};
|
|
2273
1212
|
const response = await this.request(`/agents/${this.agentId}/network`, {
|
|
@@ -3909,7 +2848,7 @@ var Observability = class extends BaseResource {
|
|
|
3909
2848
|
getTraces(params) {
|
|
3910
2849
|
const { pagination, filters } = params;
|
|
3911
2850
|
const { page, perPage, dateRange } = pagination || {};
|
|
3912
|
-
const { name
|
|
2851
|
+
const { name, spanType, entityId, entityType } = filters || {};
|
|
3913
2852
|
const searchParams = new URLSearchParams();
|
|
3914
2853
|
if (page !== void 0) {
|
|
3915
2854
|
searchParams.set("page", String(page));
|
|
@@ -3917,8 +2856,8 @@ var Observability = class extends BaseResource {
|
|
|
3917
2856
|
if (perPage !== void 0) {
|
|
3918
2857
|
searchParams.set("perPage", String(perPage));
|
|
3919
2858
|
}
|
|
3920
|
-
if (
|
|
3921
|
-
searchParams.set("name",
|
|
2859
|
+
if (name) {
|
|
2860
|
+
searchParams.set("name", name);
|
|
3922
2861
|
}
|
|
3923
2862
|
if (spanType !== void 0) {
|
|
3924
2863
|
searchParams.set("spanType", String(spanType));
|