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