@mastra/client-js 1.0.0-beta.8 → 1.0.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 +1647 -0
- package/README.md +1 -3
- package/dist/_types/@ai-sdk_ui-utils/dist/index.d.ts +820 -0
- package/dist/_types/@internal_ai-sdk-v5/dist/index.d.ts +8511 -0
- package/dist/client.d.ts +74 -11
- package/dist/client.d.ts.map +1 -1
- package/dist/docs/README.md +33 -0
- package/dist/docs/SKILL.md +34 -0
- package/dist/docs/SOURCE_MAP.json +6 -0
- package/dist/docs/ai-sdk/01-reference.md +358 -0
- package/dist/docs/client-js/01-reference.md +1180 -0
- package/dist/docs/server/01-mastra-client.md +256 -0
- package/dist/docs/server/02-jwt.md +99 -0
- package/dist/docs/server/03-clerk.md +143 -0
- package/dist/docs/server/04-supabase.md +128 -0
- package/dist/docs/server/05-firebase.md +286 -0
- package/dist/docs/server/06-workos.md +201 -0
- package/dist/docs/server/07-auth0.md +233 -0
- package/dist/index.cjs +1839 -664
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1837 -662
- package/dist/index.js.map +1 -1
- package/dist/resources/agent-builder.d.ts +10 -26
- package/dist/resources/agent-builder.d.ts.map +1 -1
- package/dist/resources/agent.d.ts +43 -8
- package/dist/resources/agent.d.ts.map +1 -1
- package/dist/resources/index.d.ts +2 -0
- package/dist/resources/index.d.ts.map +1 -1
- package/dist/resources/memory-thread.d.ts +18 -3
- package/dist/resources/memory-thread.d.ts.map +1 -1
- package/dist/resources/observability.d.ts +58 -15
- package/dist/resources/observability.d.ts.map +1 -1
- package/dist/resources/processor.d.ts +20 -0
- package/dist/resources/processor.d.ts.map +1 -0
- package/dist/resources/run.d.ts +210 -0
- package/dist/resources/run.d.ts.map +1 -0
- package/dist/resources/stored-agent.d.ts +26 -0
- package/dist/resources/stored-agent.d.ts.map +1 -0
- package/dist/resources/workflow.d.ts +19 -224
- package/dist/resources/workflow.d.ts.map +1 -1
- package/dist/tools.d.ts +2 -2
- package/dist/tools.d.ts.map +1 -1
- package/dist/types.d.ts +240 -37
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/index.d.ts +26 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/package.json +13 -12
package/dist/index.cjs
CHANGED
|
@@ -1,13 +1,1103 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var uiUtils = require('@ai-sdk/ui-utils');
|
|
4
3
|
var uuid = require('@lukeed/uuid');
|
|
5
4
|
var error = require('@mastra/core/error');
|
|
6
5
|
var requestContext = require('@mastra/core/request-context');
|
|
7
6
|
var isVercelTool = require('@mastra/core/tools/is-vercel-tool');
|
|
8
7
|
var zodToJson = require('@mastra/schema-compat/zod-to-json');
|
|
9
8
|
|
|
10
|
-
|
|
9
|
+
var __create = Object.create;
|
|
10
|
+
var __defProp = Object.defineProperty;
|
|
11
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
14
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
15
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
16
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
17
|
+
};
|
|
18
|
+
var __copyProps = (to, from, except, desc) => {
|
|
19
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
20
|
+
for (let key of __getOwnPropNames(from))
|
|
21
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
22
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
23
|
+
}
|
|
24
|
+
return to;
|
|
25
|
+
};
|
|
26
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
27
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
28
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
29
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
30
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
31
|
+
__defProp(target, "default", { value: mod, enumerable: true }) ,
|
|
32
|
+
mod
|
|
33
|
+
));
|
|
34
|
+
|
|
35
|
+
// ../../node_modules/.pnpm/secure-json-parse@2.7.0/node_modules/secure-json-parse/index.js
|
|
36
|
+
var require_secure_json_parse = __commonJS({
|
|
37
|
+
"../../node_modules/.pnpm/secure-json-parse@2.7.0/node_modules/secure-json-parse/index.js"(exports, module) {
|
|
38
|
+
var hasBuffer = typeof Buffer !== "undefined";
|
|
39
|
+
var suspectProtoRx = /"(?:_|\\u005[Ff])(?:_|\\u005[Ff])(?:p|\\u0070)(?:r|\\u0072)(?:o|\\u006[Ff])(?:t|\\u0074)(?:o|\\u006[Ff])(?:_|\\u005[Ff])(?:_|\\u005[Ff])"\s*:/;
|
|
40
|
+
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*:/;
|
|
41
|
+
function _parse(text, reviver, options) {
|
|
42
|
+
if (options == null) {
|
|
43
|
+
if (reviver !== null && typeof reviver === "object") {
|
|
44
|
+
options = reviver;
|
|
45
|
+
reviver = void 0;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (hasBuffer && Buffer.isBuffer(text)) {
|
|
49
|
+
text = text.toString();
|
|
50
|
+
}
|
|
51
|
+
if (text && text.charCodeAt(0) === 65279) {
|
|
52
|
+
text = text.slice(1);
|
|
53
|
+
}
|
|
54
|
+
const obj = JSON.parse(text, reviver);
|
|
55
|
+
if (obj === null || typeof obj !== "object") {
|
|
56
|
+
return obj;
|
|
57
|
+
}
|
|
58
|
+
const protoAction = options && options.protoAction || "error";
|
|
59
|
+
const constructorAction = options && options.constructorAction || "error";
|
|
60
|
+
if (protoAction === "ignore" && constructorAction === "ignore") {
|
|
61
|
+
return obj;
|
|
62
|
+
}
|
|
63
|
+
if (protoAction !== "ignore" && constructorAction !== "ignore") {
|
|
64
|
+
if (suspectProtoRx.test(text) === false && suspectConstructorRx.test(text) === false) {
|
|
65
|
+
return obj;
|
|
66
|
+
}
|
|
67
|
+
} else if (protoAction !== "ignore" && constructorAction === "ignore") {
|
|
68
|
+
if (suspectProtoRx.test(text) === false) {
|
|
69
|
+
return obj;
|
|
70
|
+
}
|
|
71
|
+
} else {
|
|
72
|
+
if (suspectConstructorRx.test(text) === false) {
|
|
73
|
+
return obj;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return filter(obj, { protoAction, constructorAction, safe: options && options.safe });
|
|
77
|
+
}
|
|
78
|
+
function filter(obj, { protoAction = "error", constructorAction = "error", safe } = {}) {
|
|
79
|
+
let next = [obj];
|
|
80
|
+
while (next.length) {
|
|
81
|
+
const nodes = next;
|
|
82
|
+
next = [];
|
|
83
|
+
for (const node of nodes) {
|
|
84
|
+
if (protoAction !== "ignore" && Object.prototype.hasOwnProperty.call(node, "__proto__")) {
|
|
85
|
+
if (safe === true) {
|
|
86
|
+
return null;
|
|
87
|
+
} else if (protoAction === "error") {
|
|
88
|
+
throw new SyntaxError("Object contains forbidden prototype property");
|
|
89
|
+
}
|
|
90
|
+
delete node.__proto__;
|
|
91
|
+
}
|
|
92
|
+
if (constructorAction !== "ignore" && Object.prototype.hasOwnProperty.call(node, "constructor") && Object.prototype.hasOwnProperty.call(node.constructor, "prototype")) {
|
|
93
|
+
if (safe === true) {
|
|
94
|
+
return null;
|
|
95
|
+
} else if (constructorAction === "error") {
|
|
96
|
+
throw new SyntaxError("Object contains forbidden prototype property");
|
|
97
|
+
}
|
|
98
|
+
delete node.constructor;
|
|
99
|
+
}
|
|
100
|
+
for (const key in node) {
|
|
101
|
+
const value = node[key];
|
|
102
|
+
if (value && typeof value === "object") {
|
|
103
|
+
next.push(value);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return obj;
|
|
109
|
+
}
|
|
110
|
+
function parse(text, reviver, options) {
|
|
111
|
+
const stackTraceLimit = Error.stackTraceLimit;
|
|
112
|
+
Error.stackTraceLimit = 0;
|
|
113
|
+
try {
|
|
114
|
+
return _parse(text, reviver, options);
|
|
115
|
+
} finally {
|
|
116
|
+
Error.stackTraceLimit = stackTraceLimit;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function safeParse(text, reviver) {
|
|
120
|
+
const stackTraceLimit = Error.stackTraceLimit;
|
|
121
|
+
Error.stackTraceLimit = 0;
|
|
122
|
+
try {
|
|
123
|
+
return _parse(text, reviver, { safe: true });
|
|
124
|
+
} catch (_e) {
|
|
125
|
+
return null;
|
|
126
|
+
} finally {
|
|
127
|
+
Error.stackTraceLimit = stackTraceLimit;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
module.exports = parse;
|
|
131
|
+
module.exports.default = parse;
|
|
132
|
+
module.exports.parse = parse;
|
|
133
|
+
module.exports.safeParse = safeParse;
|
|
134
|
+
module.exports.scan = filter;
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
// ../../node_modules/.pnpm/@ai-sdk+provider@1.1.3/node_modules/@ai-sdk/provider/dist/index.mjs
|
|
139
|
+
var marker = "vercel.ai.error";
|
|
140
|
+
var symbol = Symbol.for(marker);
|
|
141
|
+
var _a;
|
|
142
|
+
var _AISDKError = class _AISDKError2 extends Error {
|
|
143
|
+
/**
|
|
144
|
+
* Creates an AI SDK Error.
|
|
145
|
+
*
|
|
146
|
+
* @param {Object} params - The parameters for creating the error.
|
|
147
|
+
* @param {string} params.name - The name of the error.
|
|
148
|
+
* @param {string} params.message - The error message.
|
|
149
|
+
* @param {unknown} [params.cause] - The underlying cause of the error.
|
|
150
|
+
*/
|
|
151
|
+
constructor({
|
|
152
|
+
name: name14,
|
|
153
|
+
message,
|
|
154
|
+
cause
|
|
155
|
+
}) {
|
|
156
|
+
super(message);
|
|
157
|
+
this[_a] = true;
|
|
158
|
+
this.name = name14;
|
|
159
|
+
this.cause = cause;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Checks if the given error is an AI SDK Error.
|
|
163
|
+
* @param {unknown} error - The error to check.
|
|
164
|
+
* @returns {boolean} True if the error is an AI SDK Error, false otherwise.
|
|
165
|
+
*/
|
|
166
|
+
static isInstance(error) {
|
|
167
|
+
return _AISDKError2.hasMarker(error, marker);
|
|
168
|
+
}
|
|
169
|
+
static hasMarker(error, marker15) {
|
|
170
|
+
const markerSymbol = Symbol.for(marker15);
|
|
171
|
+
return error != null && typeof error === "object" && markerSymbol in error && typeof error[markerSymbol] === "boolean" && error[markerSymbol] === true;
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
_a = symbol;
|
|
175
|
+
var AISDKError = _AISDKError;
|
|
176
|
+
function getErrorMessage(error) {
|
|
177
|
+
if (error == null) {
|
|
178
|
+
return "unknown error";
|
|
179
|
+
}
|
|
180
|
+
if (typeof error === "string") {
|
|
181
|
+
return error;
|
|
182
|
+
}
|
|
183
|
+
if (error instanceof Error) {
|
|
184
|
+
return error.message;
|
|
185
|
+
}
|
|
186
|
+
return JSON.stringify(error);
|
|
187
|
+
}
|
|
188
|
+
var name3 = "AI_InvalidArgumentError";
|
|
189
|
+
var marker4 = `vercel.ai.error.${name3}`;
|
|
190
|
+
var symbol4 = Symbol.for(marker4);
|
|
191
|
+
var _a4;
|
|
192
|
+
var InvalidArgumentError = class extends AISDKError {
|
|
193
|
+
constructor({
|
|
194
|
+
message,
|
|
195
|
+
cause,
|
|
196
|
+
argument
|
|
197
|
+
}) {
|
|
198
|
+
super({ name: name3, message, cause });
|
|
199
|
+
this[_a4] = true;
|
|
200
|
+
this.argument = argument;
|
|
201
|
+
}
|
|
202
|
+
static isInstance(error) {
|
|
203
|
+
return AISDKError.hasMarker(error, marker4);
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
_a4 = symbol4;
|
|
207
|
+
var name6 = "AI_JSONParseError";
|
|
208
|
+
var marker7 = `vercel.ai.error.${name6}`;
|
|
209
|
+
var symbol7 = Symbol.for(marker7);
|
|
210
|
+
var _a7;
|
|
211
|
+
var JSONParseError = class extends AISDKError {
|
|
212
|
+
constructor({ text, cause }) {
|
|
213
|
+
super({
|
|
214
|
+
name: name6,
|
|
215
|
+
message: `JSON parsing failed: Text: ${text}.
|
|
216
|
+
Error message: ${getErrorMessage(cause)}`,
|
|
217
|
+
cause
|
|
218
|
+
});
|
|
219
|
+
this[_a7] = true;
|
|
220
|
+
this.text = text;
|
|
221
|
+
}
|
|
222
|
+
static isInstance(error) {
|
|
223
|
+
return AISDKError.hasMarker(error, marker7);
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
_a7 = symbol7;
|
|
227
|
+
var name12 = "AI_TypeValidationError";
|
|
228
|
+
var marker13 = `vercel.ai.error.${name12}`;
|
|
229
|
+
var symbol13 = Symbol.for(marker13);
|
|
230
|
+
var _a13;
|
|
231
|
+
var _TypeValidationError = class _TypeValidationError2 extends AISDKError {
|
|
232
|
+
constructor({ value, cause }) {
|
|
233
|
+
super({
|
|
234
|
+
name: name12,
|
|
235
|
+
message: `Type validation failed: Value: ${JSON.stringify(value)}.
|
|
236
|
+
Error message: ${getErrorMessage(cause)}`,
|
|
237
|
+
cause
|
|
238
|
+
});
|
|
239
|
+
this[_a13] = true;
|
|
240
|
+
this.value = value;
|
|
241
|
+
}
|
|
242
|
+
static isInstance(error) {
|
|
243
|
+
return AISDKError.hasMarker(error, marker13);
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Wraps an error into a TypeValidationError.
|
|
247
|
+
* If the cause is already a TypeValidationError with the same value, it returns the cause.
|
|
248
|
+
* Otherwise, it creates a new TypeValidationError.
|
|
249
|
+
*
|
|
250
|
+
* @param {Object} params - The parameters for wrapping the error.
|
|
251
|
+
* @param {unknown} params.value - The value that failed validation.
|
|
252
|
+
* @param {unknown} params.cause - The original error or cause of the validation failure.
|
|
253
|
+
* @returns {TypeValidationError} A TypeValidationError instance.
|
|
254
|
+
*/
|
|
255
|
+
static wrap({
|
|
256
|
+
value,
|
|
257
|
+
cause
|
|
258
|
+
}) {
|
|
259
|
+
return _TypeValidationError2.isInstance(cause) && cause.value === value ? cause : new _TypeValidationError2({ value, cause });
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
_a13 = symbol13;
|
|
263
|
+
var TypeValidationError = _TypeValidationError;
|
|
264
|
+
|
|
265
|
+
// ../../node_modules/.pnpm/nanoid@3.3.11/node_modules/nanoid/non-secure/index.js
|
|
266
|
+
var customAlphabet = (alphabet, defaultSize = 21) => {
|
|
267
|
+
return (size = defaultSize) => {
|
|
268
|
+
let id = "";
|
|
269
|
+
let i = size | 0;
|
|
270
|
+
while (i--) {
|
|
271
|
+
id += alphabet[Math.random() * alphabet.length | 0];
|
|
272
|
+
}
|
|
273
|
+
return id;
|
|
274
|
+
};
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
// ../../node_modules/.pnpm/@ai-sdk+provider-utils@2.2.8_zod@3.25.76/node_modules/@ai-sdk/provider-utils/dist/index.mjs
|
|
278
|
+
var import_secure_json_parse = __toESM(require_secure_json_parse());
|
|
279
|
+
var createIdGenerator = ({
|
|
280
|
+
prefix,
|
|
281
|
+
size: defaultSize = 16,
|
|
282
|
+
alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
|
283
|
+
separator = "-"
|
|
284
|
+
} = {}) => {
|
|
285
|
+
const generator = customAlphabet(alphabet, defaultSize);
|
|
286
|
+
if (prefix == null) {
|
|
287
|
+
return generator;
|
|
288
|
+
}
|
|
289
|
+
if (alphabet.includes(separator)) {
|
|
290
|
+
throw new InvalidArgumentError({
|
|
291
|
+
argument: "separator",
|
|
292
|
+
message: `The separator "${separator}" must not be part of the alphabet "${alphabet}".`
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
return (size) => `${prefix}${separator}${generator(size)}`;
|
|
296
|
+
};
|
|
297
|
+
createIdGenerator();
|
|
298
|
+
var validatorSymbol = /* @__PURE__ */ Symbol.for("vercel.ai.validator");
|
|
299
|
+
function validator(validate) {
|
|
300
|
+
return { [validatorSymbol]: true, validate };
|
|
301
|
+
}
|
|
302
|
+
function isValidator(value) {
|
|
303
|
+
return typeof value === "object" && value !== null && validatorSymbol in value && value[validatorSymbol] === true && "validate" in value;
|
|
304
|
+
}
|
|
305
|
+
function asValidator(value) {
|
|
306
|
+
return isValidator(value) ? value : zodValidator(value);
|
|
307
|
+
}
|
|
308
|
+
function zodValidator(zodSchema) {
|
|
309
|
+
return validator((value) => {
|
|
310
|
+
const result = zodSchema.safeParse(value);
|
|
311
|
+
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
function safeValidateTypes({
|
|
315
|
+
value,
|
|
316
|
+
schema
|
|
317
|
+
}) {
|
|
318
|
+
const validator2 = asValidator(schema);
|
|
319
|
+
try {
|
|
320
|
+
if (validator2.validate == null) {
|
|
321
|
+
return { success: true, value };
|
|
322
|
+
}
|
|
323
|
+
const result = validator2.validate(value);
|
|
324
|
+
if (result.success) {
|
|
325
|
+
return result;
|
|
326
|
+
}
|
|
327
|
+
return {
|
|
328
|
+
success: false,
|
|
329
|
+
error: TypeValidationError.wrap({ value, cause: result.error })
|
|
330
|
+
};
|
|
331
|
+
} catch (error) {
|
|
332
|
+
return {
|
|
333
|
+
success: false,
|
|
334
|
+
error: TypeValidationError.wrap({ value, cause: error })
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
function safeParseJSON({
|
|
339
|
+
text,
|
|
340
|
+
schema
|
|
341
|
+
}) {
|
|
342
|
+
try {
|
|
343
|
+
const value = import_secure_json_parse.default.parse(text);
|
|
344
|
+
if (schema == null) {
|
|
345
|
+
return { success: true, value, rawValue: value };
|
|
346
|
+
}
|
|
347
|
+
const validationResult = safeValidateTypes({ value, schema });
|
|
348
|
+
return validationResult.success ? { ...validationResult, rawValue: value } : validationResult;
|
|
349
|
+
} catch (error) {
|
|
350
|
+
return {
|
|
351
|
+
success: false,
|
|
352
|
+
error: JSONParseError.isInstance(error) ? error : new JSONParseError({ text, cause: error })
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// ../../node_modules/.pnpm/zod-to-json-schema@3.24.6_zod@3.25.76/node_modules/zod-to-json-schema/dist/esm/parsers/string.js
|
|
358
|
+
new Set("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvxyz0123456789");
|
|
359
|
+
function fixJson(input) {
|
|
360
|
+
const stack = ["ROOT"];
|
|
361
|
+
let lastValidIndex = -1;
|
|
362
|
+
let literalStart = null;
|
|
363
|
+
function processValueStart(char, i, swapState) {
|
|
364
|
+
{
|
|
365
|
+
switch (char) {
|
|
366
|
+
case '"': {
|
|
367
|
+
lastValidIndex = i;
|
|
368
|
+
stack.pop();
|
|
369
|
+
stack.push(swapState);
|
|
370
|
+
stack.push("INSIDE_STRING");
|
|
371
|
+
break;
|
|
372
|
+
}
|
|
373
|
+
case "f":
|
|
374
|
+
case "t":
|
|
375
|
+
case "n": {
|
|
376
|
+
lastValidIndex = i;
|
|
377
|
+
literalStart = i;
|
|
378
|
+
stack.pop();
|
|
379
|
+
stack.push(swapState);
|
|
380
|
+
stack.push("INSIDE_LITERAL");
|
|
381
|
+
break;
|
|
382
|
+
}
|
|
383
|
+
case "-": {
|
|
384
|
+
stack.pop();
|
|
385
|
+
stack.push(swapState);
|
|
386
|
+
stack.push("INSIDE_NUMBER");
|
|
387
|
+
break;
|
|
388
|
+
}
|
|
389
|
+
case "0":
|
|
390
|
+
case "1":
|
|
391
|
+
case "2":
|
|
392
|
+
case "3":
|
|
393
|
+
case "4":
|
|
394
|
+
case "5":
|
|
395
|
+
case "6":
|
|
396
|
+
case "7":
|
|
397
|
+
case "8":
|
|
398
|
+
case "9": {
|
|
399
|
+
lastValidIndex = i;
|
|
400
|
+
stack.pop();
|
|
401
|
+
stack.push(swapState);
|
|
402
|
+
stack.push("INSIDE_NUMBER");
|
|
403
|
+
break;
|
|
404
|
+
}
|
|
405
|
+
case "{": {
|
|
406
|
+
lastValidIndex = i;
|
|
407
|
+
stack.pop();
|
|
408
|
+
stack.push(swapState);
|
|
409
|
+
stack.push("INSIDE_OBJECT_START");
|
|
410
|
+
break;
|
|
411
|
+
}
|
|
412
|
+
case "[": {
|
|
413
|
+
lastValidIndex = i;
|
|
414
|
+
stack.pop();
|
|
415
|
+
stack.push(swapState);
|
|
416
|
+
stack.push("INSIDE_ARRAY_START");
|
|
417
|
+
break;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
function processAfterObjectValue(char, i) {
|
|
423
|
+
switch (char) {
|
|
424
|
+
case ",": {
|
|
425
|
+
stack.pop();
|
|
426
|
+
stack.push("INSIDE_OBJECT_AFTER_COMMA");
|
|
427
|
+
break;
|
|
428
|
+
}
|
|
429
|
+
case "}": {
|
|
430
|
+
lastValidIndex = i;
|
|
431
|
+
stack.pop();
|
|
432
|
+
break;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
function processAfterArrayValue(char, i) {
|
|
437
|
+
switch (char) {
|
|
438
|
+
case ",": {
|
|
439
|
+
stack.pop();
|
|
440
|
+
stack.push("INSIDE_ARRAY_AFTER_COMMA");
|
|
441
|
+
break;
|
|
442
|
+
}
|
|
443
|
+
case "]": {
|
|
444
|
+
lastValidIndex = i;
|
|
445
|
+
stack.pop();
|
|
446
|
+
break;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
for (let i = 0; i < input.length; i++) {
|
|
451
|
+
const char = input[i];
|
|
452
|
+
const currentState = stack[stack.length - 1];
|
|
453
|
+
switch (currentState) {
|
|
454
|
+
case "ROOT":
|
|
455
|
+
processValueStart(char, i, "FINISH");
|
|
456
|
+
break;
|
|
457
|
+
case "INSIDE_OBJECT_START": {
|
|
458
|
+
switch (char) {
|
|
459
|
+
case '"': {
|
|
460
|
+
stack.pop();
|
|
461
|
+
stack.push("INSIDE_OBJECT_KEY");
|
|
462
|
+
break;
|
|
463
|
+
}
|
|
464
|
+
case "}": {
|
|
465
|
+
lastValidIndex = i;
|
|
466
|
+
stack.pop();
|
|
467
|
+
break;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
break;
|
|
471
|
+
}
|
|
472
|
+
case "INSIDE_OBJECT_AFTER_COMMA": {
|
|
473
|
+
switch (char) {
|
|
474
|
+
case '"': {
|
|
475
|
+
stack.pop();
|
|
476
|
+
stack.push("INSIDE_OBJECT_KEY");
|
|
477
|
+
break;
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
break;
|
|
481
|
+
}
|
|
482
|
+
case "INSIDE_OBJECT_KEY": {
|
|
483
|
+
switch (char) {
|
|
484
|
+
case '"': {
|
|
485
|
+
stack.pop();
|
|
486
|
+
stack.push("INSIDE_OBJECT_AFTER_KEY");
|
|
487
|
+
break;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
break;
|
|
491
|
+
}
|
|
492
|
+
case "INSIDE_OBJECT_AFTER_KEY": {
|
|
493
|
+
switch (char) {
|
|
494
|
+
case ":": {
|
|
495
|
+
stack.pop();
|
|
496
|
+
stack.push("INSIDE_OBJECT_BEFORE_VALUE");
|
|
497
|
+
break;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
break;
|
|
501
|
+
}
|
|
502
|
+
case "INSIDE_OBJECT_BEFORE_VALUE": {
|
|
503
|
+
processValueStart(char, i, "INSIDE_OBJECT_AFTER_VALUE");
|
|
504
|
+
break;
|
|
505
|
+
}
|
|
506
|
+
case "INSIDE_OBJECT_AFTER_VALUE": {
|
|
507
|
+
processAfterObjectValue(char, i);
|
|
508
|
+
break;
|
|
509
|
+
}
|
|
510
|
+
case "INSIDE_STRING": {
|
|
511
|
+
switch (char) {
|
|
512
|
+
case '"': {
|
|
513
|
+
stack.pop();
|
|
514
|
+
lastValidIndex = i;
|
|
515
|
+
break;
|
|
516
|
+
}
|
|
517
|
+
case "\\": {
|
|
518
|
+
stack.push("INSIDE_STRING_ESCAPE");
|
|
519
|
+
break;
|
|
520
|
+
}
|
|
521
|
+
default: {
|
|
522
|
+
lastValidIndex = i;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
break;
|
|
526
|
+
}
|
|
527
|
+
case "INSIDE_ARRAY_START": {
|
|
528
|
+
switch (char) {
|
|
529
|
+
case "]": {
|
|
530
|
+
lastValidIndex = i;
|
|
531
|
+
stack.pop();
|
|
532
|
+
break;
|
|
533
|
+
}
|
|
534
|
+
default: {
|
|
535
|
+
lastValidIndex = i;
|
|
536
|
+
processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
|
|
537
|
+
break;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
break;
|
|
541
|
+
}
|
|
542
|
+
case "INSIDE_ARRAY_AFTER_VALUE": {
|
|
543
|
+
switch (char) {
|
|
544
|
+
case ",": {
|
|
545
|
+
stack.pop();
|
|
546
|
+
stack.push("INSIDE_ARRAY_AFTER_COMMA");
|
|
547
|
+
break;
|
|
548
|
+
}
|
|
549
|
+
case "]": {
|
|
550
|
+
lastValidIndex = i;
|
|
551
|
+
stack.pop();
|
|
552
|
+
break;
|
|
553
|
+
}
|
|
554
|
+
default: {
|
|
555
|
+
lastValidIndex = i;
|
|
556
|
+
break;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
break;
|
|
560
|
+
}
|
|
561
|
+
case "INSIDE_ARRAY_AFTER_COMMA": {
|
|
562
|
+
processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
|
|
563
|
+
break;
|
|
564
|
+
}
|
|
565
|
+
case "INSIDE_STRING_ESCAPE": {
|
|
566
|
+
stack.pop();
|
|
567
|
+
lastValidIndex = i;
|
|
568
|
+
break;
|
|
569
|
+
}
|
|
570
|
+
case "INSIDE_NUMBER": {
|
|
571
|
+
switch (char) {
|
|
572
|
+
case "0":
|
|
573
|
+
case "1":
|
|
574
|
+
case "2":
|
|
575
|
+
case "3":
|
|
576
|
+
case "4":
|
|
577
|
+
case "5":
|
|
578
|
+
case "6":
|
|
579
|
+
case "7":
|
|
580
|
+
case "8":
|
|
581
|
+
case "9": {
|
|
582
|
+
lastValidIndex = i;
|
|
583
|
+
break;
|
|
584
|
+
}
|
|
585
|
+
case "e":
|
|
586
|
+
case "E":
|
|
587
|
+
case "-":
|
|
588
|
+
case ".": {
|
|
589
|
+
break;
|
|
590
|
+
}
|
|
591
|
+
case ",": {
|
|
592
|
+
stack.pop();
|
|
593
|
+
if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
|
|
594
|
+
processAfterArrayValue(char, i);
|
|
595
|
+
}
|
|
596
|
+
if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
|
|
597
|
+
processAfterObjectValue(char, i);
|
|
598
|
+
}
|
|
599
|
+
break;
|
|
600
|
+
}
|
|
601
|
+
case "}": {
|
|
602
|
+
stack.pop();
|
|
603
|
+
if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
|
|
604
|
+
processAfterObjectValue(char, i);
|
|
605
|
+
}
|
|
606
|
+
break;
|
|
607
|
+
}
|
|
608
|
+
case "]": {
|
|
609
|
+
stack.pop();
|
|
610
|
+
if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
|
|
611
|
+
processAfterArrayValue(char, i);
|
|
612
|
+
}
|
|
613
|
+
break;
|
|
614
|
+
}
|
|
615
|
+
default: {
|
|
616
|
+
stack.pop();
|
|
617
|
+
break;
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
break;
|
|
621
|
+
}
|
|
622
|
+
case "INSIDE_LITERAL": {
|
|
623
|
+
const partialLiteral = input.substring(literalStart, i + 1);
|
|
624
|
+
if (!"false".startsWith(partialLiteral) && !"true".startsWith(partialLiteral) && !"null".startsWith(partialLiteral)) {
|
|
625
|
+
stack.pop();
|
|
626
|
+
if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
|
|
627
|
+
processAfterObjectValue(char, i);
|
|
628
|
+
} else if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
|
|
629
|
+
processAfterArrayValue(char, i);
|
|
630
|
+
}
|
|
631
|
+
} else {
|
|
632
|
+
lastValidIndex = i;
|
|
633
|
+
}
|
|
634
|
+
break;
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
let result = input.slice(0, lastValidIndex + 1);
|
|
639
|
+
for (let i = stack.length - 1; i >= 0; i--) {
|
|
640
|
+
const state = stack[i];
|
|
641
|
+
switch (state) {
|
|
642
|
+
case "INSIDE_STRING": {
|
|
643
|
+
result += '"';
|
|
644
|
+
break;
|
|
645
|
+
}
|
|
646
|
+
case "INSIDE_OBJECT_KEY":
|
|
647
|
+
case "INSIDE_OBJECT_AFTER_KEY":
|
|
648
|
+
case "INSIDE_OBJECT_AFTER_COMMA":
|
|
649
|
+
case "INSIDE_OBJECT_START":
|
|
650
|
+
case "INSIDE_OBJECT_BEFORE_VALUE":
|
|
651
|
+
case "INSIDE_OBJECT_AFTER_VALUE": {
|
|
652
|
+
result += "}";
|
|
653
|
+
break;
|
|
654
|
+
}
|
|
655
|
+
case "INSIDE_ARRAY_START":
|
|
656
|
+
case "INSIDE_ARRAY_AFTER_COMMA":
|
|
657
|
+
case "INSIDE_ARRAY_AFTER_VALUE": {
|
|
658
|
+
result += "]";
|
|
659
|
+
break;
|
|
660
|
+
}
|
|
661
|
+
case "INSIDE_LITERAL": {
|
|
662
|
+
const partialLiteral = input.substring(literalStart, input.length);
|
|
663
|
+
if ("true".startsWith(partialLiteral)) {
|
|
664
|
+
result += "true".slice(partialLiteral.length);
|
|
665
|
+
} else if ("false".startsWith(partialLiteral)) {
|
|
666
|
+
result += "false".slice(partialLiteral.length);
|
|
667
|
+
} else if ("null".startsWith(partialLiteral)) {
|
|
668
|
+
result += "null".slice(partialLiteral.length);
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
return result;
|
|
674
|
+
}
|
|
675
|
+
function parsePartialJson(jsonText) {
|
|
676
|
+
if (jsonText === void 0) {
|
|
677
|
+
return { value: void 0, state: "undefined-input" };
|
|
678
|
+
}
|
|
679
|
+
let result = safeParseJSON({ text: jsonText });
|
|
680
|
+
if (result.success) {
|
|
681
|
+
return { value: result.value, state: "successful-parse" };
|
|
682
|
+
}
|
|
683
|
+
result = safeParseJSON({ text: fixJson(jsonText) });
|
|
684
|
+
if (result.success) {
|
|
685
|
+
return { value: result.value, state: "repaired-parse" };
|
|
686
|
+
}
|
|
687
|
+
return { value: void 0, state: "failed-parse" };
|
|
688
|
+
}
|
|
689
|
+
var textStreamPart2 = {
|
|
690
|
+
code: "0",
|
|
691
|
+
name: "text",
|
|
692
|
+
parse: (value) => {
|
|
693
|
+
if (typeof value !== "string") {
|
|
694
|
+
throw new Error('"text" parts expect a string value.');
|
|
695
|
+
}
|
|
696
|
+
return { type: "text", value };
|
|
697
|
+
}
|
|
698
|
+
};
|
|
699
|
+
var dataStreamPart = {
|
|
700
|
+
code: "2",
|
|
701
|
+
name: "data",
|
|
702
|
+
parse: (value) => {
|
|
703
|
+
if (!Array.isArray(value)) {
|
|
704
|
+
throw new Error('"data" parts expect an array value.');
|
|
705
|
+
}
|
|
706
|
+
return { type: "data", value };
|
|
707
|
+
}
|
|
708
|
+
};
|
|
709
|
+
var errorStreamPart2 = {
|
|
710
|
+
code: "3",
|
|
711
|
+
name: "error",
|
|
712
|
+
parse: (value) => {
|
|
713
|
+
if (typeof value !== "string") {
|
|
714
|
+
throw new Error('"error" parts expect a string value.');
|
|
715
|
+
}
|
|
716
|
+
return { type: "error", value };
|
|
717
|
+
}
|
|
718
|
+
};
|
|
719
|
+
var messageAnnotationsStreamPart = {
|
|
720
|
+
code: "8",
|
|
721
|
+
name: "message_annotations",
|
|
722
|
+
parse: (value) => {
|
|
723
|
+
if (!Array.isArray(value)) {
|
|
724
|
+
throw new Error('"message_annotations" parts expect an array value.');
|
|
725
|
+
}
|
|
726
|
+
return { type: "message_annotations", value };
|
|
727
|
+
}
|
|
728
|
+
};
|
|
729
|
+
var toolCallStreamPart = {
|
|
730
|
+
code: "9",
|
|
731
|
+
name: "tool_call",
|
|
732
|
+
parse: (value) => {
|
|
733
|
+
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") {
|
|
734
|
+
throw new Error(
|
|
735
|
+
'"tool_call" parts expect an object with a "toolCallId", "toolName", and "args" property.'
|
|
736
|
+
);
|
|
737
|
+
}
|
|
738
|
+
return {
|
|
739
|
+
type: "tool_call",
|
|
740
|
+
value
|
|
741
|
+
};
|
|
742
|
+
}
|
|
743
|
+
};
|
|
744
|
+
var toolResultStreamPart = {
|
|
745
|
+
code: "a",
|
|
746
|
+
name: "tool_result",
|
|
747
|
+
parse: (value) => {
|
|
748
|
+
if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("result" in value)) {
|
|
749
|
+
throw new Error(
|
|
750
|
+
'"tool_result" parts expect an object with a "toolCallId" and a "result" property.'
|
|
751
|
+
);
|
|
752
|
+
}
|
|
753
|
+
return {
|
|
754
|
+
type: "tool_result",
|
|
755
|
+
value
|
|
756
|
+
};
|
|
757
|
+
}
|
|
758
|
+
};
|
|
759
|
+
var toolCallStreamingStartStreamPart = {
|
|
760
|
+
code: "b",
|
|
761
|
+
name: "tool_call_streaming_start",
|
|
762
|
+
parse: (value) => {
|
|
763
|
+
if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("toolName" in value) || typeof value.toolName !== "string") {
|
|
764
|
+
throw new Error(
|
|
765
|
+
'"tool_call_streaming_start" parts expect an object with a "toolCallId" and "toolName" property.'
|
|
766
|
+
);
|
|
767
|
+
}
|
|
768
|
+
return {
|
|
769
|
+
type: "tool_call_streaming_start",
|
|
770
|
+
value
|
|
771
|
+
};
|
|
772
|
+
}
|
|
773
|
+
};
|
|
774
|
+
var toolCallDeltaStreamPart = {
|
|
775
|
+
code: "c",
|
|
776
|
+
name: "tool_call_delta",
|
|
777
|
+
parse: (value) => {
|
|
778
|
+
if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("argsTextDelta" in value) || typeof value.argsTextDelta !== "string") {
|
|
779
|
+
throw new Error(
|
|
780
|
+
'"tool_call_delta" parts expect an object with a "toolCallId" and "argsTextDelta" property.'
|
|
781
|
+
);
|
|
782
|
+
}
|
|
783
|
+
return {
|
|
784
|
+
type: "tool_call_delta",
|
|
785
|
+
value
|
|
786
|
+
};
|
|
787
|
+
}
|
|
788
|
+
};
|
|
789
|
+
var finishMessageStreamPart = {
|
|
790
|
+
code: "d",
|
|
791
|
+
name: "finish_message",
|
|
792
|
+
parse: (value) => {
|
|
793
|
+
if (value == null || typeof value !== "object" || !("finishReason" in value) || typeof value.finishReason !== "string") {
|
|
794
|
+
throw new Error(
|
|
795
|
+
'"finish_message" parts expect an object with a "finishReason" property.'
|
|
796
|
+
);
|
|
797
|
+
}
|
|
798
|
+
const result = {
|
|
799
|
+
finishReason: value.finishReason
|
|
800
|
+
};
|
|
801
|
+
if ("usage" in value && value.usage != null && typeof value.usage === "object" && "promptTokens" in value.usage && "completionTokens" in value.usage) {
|
|
802
|
+
result.usage = {
|
|
803
|
+
promptTokens: typeof value.usage.promptTokens === "number" ? value.usage.promptTokens : Number.NaN,
|
|
804
|
+
completionTokens: typeof value.usage.completionTokens === "number" ? value.usage.completionTokens : Number.NaN
|
|
805
|
+
};
|
|
806
|
+
}
|
|
807
|
+
return {
|
|
808
|
+
type: "finish_message",
|
|
809
|
+
value: result
|
|
810
|
+
};
|
|
811
|
+
}
|
|
812
|
+
};
|
|
813
|
+
var finishStepStreamPart = {
|
|
814
|
+
code: "e",
|
|
815
|
+
name: "finish_step",
|
|
816
|
+
parse: (value) => {
|
|
817
|
+
if (value == null || typeof value !== "object" || !("finishReason" in value) || typeof value.finishReason !== "string") {
|
|
818
|
+
throw new Error(
|
|
819
|
+
'"finish_step" parts expect an object with a "finishReason" property.'
|
|
820
|
+
);
|
|
821
|
+
}
|
|
822
|
+
const result = {
|
|
823
|
+
finishReason: value.finishReason,
|
|
824
|
+
isContinued: false
|
|
825
|
+
};
|
|
826
|
+
if ("usage" in value && value.usage != null && typeof value.usage === "object" && "promptTokens" in value.usage && "completionTokens" in value.usage) {
|
|
827
|
+
result.usage = {
|
|
828
|
+
promptTokens: typeof value.usage.promptTokens === "number" ? value.usage.promptTokens : Number.NaN,
|
|
829
|
+
completionTokens: typeof value.usage.completionTokens === "number" ? value.usage.completionTokens : Number.NaN
|
|
830
|
+
};
|
|
831
|
+
}
|
|
832
|
+
if ("isContinued" in value && typeof value.isContinued === "boolean") {
|
|
833
|
+
result.isContinued = value.isContinued;
|
|
834
|
+
}
|
|
835
|
+
return {
|
|
836
|
+
type: "finish_step",
|
|
837
|
+
value: result
|
|
838
|
+
};
|
|
839
|
+
}
|
|
840
|
+
};
|
|
841
|
+
var startStepStreamPart = {
|
|
842
|
+
code: "f",
|
|
843
|
+
name: "start_step",
|
|
844
|
+
parse: (value) => {
|
|
845
|
+
if (value == null || typeof value !== "object" || !("messageId" in value) || typeof value.messageId !== "string") {
|
|
846
|
+
throw new Error(
|
|
847
|
+
'"start_step" parts expect an object with an "id" property.'
|
|
848
|
+
);
|
|
849
|
+
}
|
|
850
|
+
return {
|
|
851
|
+
type: "start_step",
|
|
852
|
+
value: {
|
|
853
|
+
messageId: value.messageId
|
|
854
|
+
}
|
|
855
|
+
};
|
|
856
|
+
}
|
|
857
|
+
};
|
|
858
|
+
var reasoningStreamPart = {
|
|
859
|
+
code: "g",
|
|
860
|
+
name: "reasoning",
|
|
861
|
+
parse: (value) => {
|
|
862
|
+
if (typeof value !== "string") {
|
|
863
|
+
throw new Error('"reasoning" parts expect a string value.');
|
|
864
|
+
}
|
|
865
|
+
return { type: "reasoning", value };
|
|
866
|
+
}
|
|
867
|
+
};
|
|
868
|
+
var sourcePart = {
|
|
869
|
+
code: "h",
|
|
870
|
+
name: "source",
|
|
871
|
+
parse: (value) => {
|
|
872
|
+
if (value == null || typeof value !== "object") {
|
|
873
|
+
throw new Error('"source" parts expect a Source object.');
|
|
874
|
+
}
|
|
875
|
+
return {
|
|
876
|
+
type: "source",
|
|
877
|
+
value
|
|
878
|
+
};
|
|
879
|
+
}
|
|
880
|
+
};
|
|
881
|
+
var redactedReasoningStreamPart = {
|
|
882
|
+
code: "i",
|
|
883
|
+
name: "redacted_reasoning",
|
|
884
|
+
parse: (value) => {
|
|
885
|
+
if (value == null || typeof value !== "object" || !("data" in value) || typeof value.data !== "string") {
|
|
886
|
+
throw new Error(
|
|
887
|
+
'"redacted_reasoning" parts expect an object with a "data" property.'
|
|
888
|
+
);
|
|
889
|
+
}
|
|
890
|
+
return { type: "redacted_reasoning", value: { data: value.data } };
|
|
891
|
+
}
|
|
892
|
+
};
|
|
893
|
+
var reasoningSignatureStreamPart = {
|
|
894
|
+
code: "j",
|
|
895
|
+
name: "reasoning_signature",
|
|
896
|
+
parse: (value) => {
|
|
897
|
+
if (value == null || typeof value !== "object" || !("signature" in value) || typeof value.signature !== "string") {
|
|
898
|
+
throw new Error(
|
|
899
|
+
'"reasoning_signature" parts expect an object with a "signature" property.'
|
|
900
|
+
);
|
|
901
|
+
}
|
|
902
|
+
return {
|
|
903
|
+
type: "reasoning_signature",
|
|
904
|
+
value: { signature: value.signature }
|
|
905
|
+
};
|
|
906
|
+
}
|
|
907
|
+
};
|
|
908
|
+
var fileStreamPart = {
|
|
909
|
+
code: "k",
|
|
910
|
+
name: "file",
|
|
911
|
+
parse: (value) => {
|
|
912
|
+
if (value == null || typeof value !== "object" || !("data" in value) || typeof value.data !== "string" || !("mimeType" in value) || typeof value.mimeType !== "string") {
|
|
913
|
+
throw new Error(
|
|
914
|
+
'"file" parts expect an object with a "data" and "mimeType" property.'
|
|
915
|
+
);
|
|
916
|
+
}
|
|
917
|
+
return { type: "file", value };
|
|
918
|
+
}
|
|
919
|
+
};
|
|
920
|
+
var dataStreamParts = [
|
|
921
|
+
textStreamPart2,
|
|
922
|
+
dataStreamPart,
|
|
923
|
+
errorStreamPart2,
|
|
924
|
+
messageAnnotationsStreamPart,
|
|
925
|
+
toolCallStreamPart,
|
|
926
|
+
toolResultStreamPart,
|
|
927
|
+
toolCallStreamingStartStreamPart,
|
|
928
|
+
toolCallDeltaStreamPart,
|
|
929
|
+
finishMessageStreamPart,
|
|
930
|
+
finishStepStreamPart,
|
|
931
|
+
startStepStreamPart,
|
|
932
|
+
reasoningStreamPart,
|
|
933
|
+
sourcePart,
|
|
934
|
+
redactedReasoningStreamPart,
|
|
935
|
+
reasoningSignatureStreamPart,
|
|
936
|
+
fileStreamPart
|
|
937
|
+
];
|
|
938
|
+
var dataStreamPartsByCode = Object.fromEntries(
|
|
939
|
+
dataStreamParts.map((part) => [part.code, part])
|
|
940
|
+
);
|
|
941
|
+
Object.fromEntries(
|
|
942
|
+
dataStreamParts.map((part) => [part.name, part.code])
|
|
943
|
+
);
|
|
944
|
+
var validCodes2 = dataStreamParts.map((part) => part.code);
|
|
945
|
+
var parseDataStreamPart = (line) => {
|
|
946
|
+
const firstSeparatorIndex = line.indexOf(":");
|
|
947
|
+
if (firstSeparatorIndex === -1) {
|
|
948
|
+
throw new Error("Failed to parse stream string. No separator found.");
|
|
949
|
+
}
|
|
950
|
+
const prefix = line.slice(0, firstSeparatorIndex);
|
|
951
|
+
if (!validCodes2.includes(prefix)) {
|
|
952
|
+
throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);
|
|
953
|
+
}
|
|
954
|
+
const code = prefix;
|
|
955
|
+
const textValue = line.slice(firstSeparatorIndex + 1);
|
|
956
|
+
const jsonValue = JSON.parse(textValue);
|
|
957
|
+
return dataStreamPartsByCode[code].parse(jsonValue);
|
|
958
|
+
};
|
|
959
|
+
var NEWLINE = "\n".charCodeAt(0);
|
|
960
|
+
function concatChunks(chunks, totalLength) {
|
|
961
|
+
const concatenatedChunks = new Uint8Array(totalLength);
|
|
962
|
+
let offset = 0;
|
|
963
|
+
for (const chunk of chunks) {
|
|
964
|
+
concatenatedChunks.set(chunk, offset);
|
|
965
|
+
offset += chunk.length;
|
|
966
|
+
}
|
|
967
|
+
chunks.length = 0;
|
|
968
|
+
return concatenatedChunks;
|
|
969
|
+
}
|
|
970
|
+
async function processDataStream({
|
|
971
|
+
stream,
|
|
972
|
+
onTextPart,
|
|
973
|
+
onReasoningPart,
|
|
974
|
+
onReasoningSignaturePart,
|
|
975
|
+
onRedactedReasoningPart,
|
|
976
|
+
onSourcePart,
|
|
977
|
+
onFilePart,
|
|
978
|
+
onDataPart,
|
|
979
|
+
onErrorPart,
|
|
980
|
+
onToolCallStreamingStartPart,
|
|
981
|
+
onToolCallDeltaPart,
|
|
982
|
+
onToolCallPart,
|
|
983
|
+
onToolResultPart,
|
|
984
|
+
onMessageAnnotationsPart,
|
|
985
|
+
onFinishMessagePart,
|
|
986
|
+
onFinishStepPart,
|
|
987
|
+
onStartStepPart
|
|
988
|
+
}) {
|
|
989
|
+
const reader = stream.getReader();
|
|
990
|
+
const decoder = new TextDecoder();
|
|
991
|
+
const chunks = [];
|
|
992
|
+
let totalLength = 0;
|
|
993
|
+
while (true) {
|
|
994
|
+
const { value } = await reader.read();
|
|
995
|
+
if (value) {
|
|
996
|
+
chunks.push(value);
|
|
997
|
+
totalLength += value.length;
|
|
998
|
+
if (value[value.length - 1] !== NEWLINE) {
|
|
999
|
+
continue;
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
if (chunks.length === 0) {
|
|
1003
|
+
break;
|
|
1004
|
+
}
|
|
1005
|
+
const concatenatedChunks = concatChunks(chunks, totalLength);
|
|
1006
|
+
totalLength = 0;
|
|
1007
|
+
const streamParts = decoder.decode(concatenatedChunks, { stream: true }).split("\n").filter((line) => line !== "").map(parseDataStreamPart);
|
|
1008
|
+
for (const { type, value: value2 } of streamParts) {
|
|
1009
|
+
switch (type) {
|
|
1010
|
+
case "text":
|
|
1011
|
+
await (onTextPart == null ? void 0 : onTextPart(value2));
|
|
1012
|
+
break;
|
|
1013
|
+
case "reasoning":
|
|
1014
|
+
await (onReasoningPart == null ? void 0 : onReasoningPart(value2));
|
|
1015
|
+
break;
|
|
1016
|
+
case "reasoning_signature":
|
|
1017
|
+
await (onReasoningSignaturePart == null ? void 0 : onReasoningSignaturePart(value2));
|
|
1018
|
+
break;
|
|
1019
|
+
case "redacted_reasoning":
|
|
1020
|
+
await (onRedactedReasoningPart == null ? void 0 : onRedactedReasoningPart(value2));
|
|
1021
|
+
break;
|
|
1022
|
+
case "file":
|
|
1023
|
+
await (onFilePart == null ? void 0 : onFilePart(value2));
|
|
1024
|
+
break;
|
|
1025
|
+
case "source":
|
|
1026
|
+
await (onSourcePart == null ? void 0 : onSourcePart(value2));
|
|
1027
|
+
break;
|
|
1028
|
+
case "data":
|
|
1029
|
+
await (onDataPart == null ? void 0 : onDataPart(value2));
|
|
1030
|
+
break;
|
|
1031
|
+
case "error":
|
|
1032
|
+
await (onErrorPart == null ? void 0 : onErrorPart(value2));
|
|
1033
|
+
break;
|
|
1034
|
+
case "message_annotations":
|
|
1035
|
+
await (onMessageAnnotationsPart == null ? void 0 : onMessageAnnotationsPart(value2));
|
|
1036
|
+
break;
|
|
1037
|
+
case "tool_call_streaming_start":
|
|
1038
|
+
await (onToolCallStreamingStartPart == null ? void 0 : onToolCallStreamingStartPart(value2));
|
|
1039
|
+
break;
|
|
1040
|
+
case "tool_call_delta":
|
|
1041
|
+
await (onToolCallDeltaPart == null ? void 0 : onToolCallDeltaPart(value2));
|
|
1042
|
+
break;
|
|
1043
|
+
case "tool_call":
|
|
1044
|
+
await (onToolCallPart == null ? void 0 : onToolCallPart(value2));
|
|
1045
|
+
break;
|
|
1046
|
+
case "tool_result":
|
|
1047
|
+
await (onToolResultPart == null ? void 0 : onToolResultPart(value2));
|
|
1048
|
+
break;
|
|
1049
|
+
case "finish_message":
|
|
1050
|
+
await (onFinishMessagePart == null ? void 0 : onFinishMessagePart(value2));
|
|
1051
|
+
break;
|
|
1052
|
+
case "finish_step":
|
|
1053
|
+
await (onFinishStepPart == null ? void 0 : onFinishStepPart(value2));
|
|
1054
|
+
break;
|
|
1055
|
+
case "start_step":
|
|
1056
|
+
await (onStartStepPart == null ? void 0 : onStartStepPart(value2));
|
|
1057
|
+
break;
|
|
1058
|
+
default: {
|
|
1059
|
+
const exhaustiveCheck = type;
|
|
1060
|
+
throw new Error(`Unknown stream part type: ${exhaustiveCheck}`);
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
function isComplexValue(value) {
|
|
1067
|
+
if (value === null || value === void 0) return false;
|
|
1068
|
+
if (value instanceof Date) return false;
|
|
1069
|
+
return typeof value === "object";
|
|
1070
|
+
}
|
|
1071
|
+
function serializeQueryValue(value) {
|
|
1072
|
+
if (value instanceof Date) {
|
|
1073
|
+
return value.toISOString();
|
|
1074
|
+
}
|
|
1075
|
+
if (isComplexValue(value)) {
|
|
1076
|
+
return JSON.stringify(value, (_key, val) => {
|
|
1077
|
+
if (val instanceof Date) {
|
|
1078
|
+
return val.toISOString();
|
|
1079
|
+
}
|
|
1080
|
+
return val;
|
|
1081
|
+
});
|
|
1082
|
+
}
|
|
1083
|
+
return String(value);
|
|
1084
|
+
}
|
|
1085
|
+
function toQueryParams(params, flattenKeys = []) {
|
|
1086
|
+
const searchParams = new URLSearchParams();
|
|
1087
|
+
const keysToFlatten = flattenKeys;
|
|
1088
|
+
function addParams(obj) {
|
|
1089
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
1090
|
+
if (value === void 0 || value === null) continue;
|
|
1091
|
+
if (keysToFlatten.includes(key) && typeof value === "object" && !Array.isArray(value) && !(value instanceof Date)) {
|
|
1092
|
+
addParams(value);
|
|
1093
|
+
} else {
|
|
1094
|
+
searchParams.set(key, serializeQueryValue(value));
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
addParams(params);
|
|
1099
|
+
return searchParams.toString();
|
|
1100
|
+
}
|
|
11
1101
|
function parseClientRequestContext(requestContext$1) {
|
|
12
1102
|
if (requestContext$1) {
|
|
13
1103
|
if (requestContext$1 instanceof requestContext.RequestContext) {
|
|
@@ -34,7 +1124,7 @@ function requestContextQueryString(requestContext, delimiter = "?") {
|
|
|
34
1124
|
function isZodType(value) {
|
|
35
1125
|
return typeof value === "object" && value !== null && "_def" in value && "parse" in value && typeof value.parse === "function" && "safeParse" in value && typeof value.safeParse === "function";
|
|
36
1126
|
}
|
|
37
|
-
function
|
|
1127
|
+
function zodToJsonSchema2(zodSchema) {
|
|
38
1128
|
if (!isZodType(zodSchema)) {
|
|
39
1129
|
return zodSchema;
|
|
40
1130
|
}
|
|
@@ -53,7 +1143,7 @@ function processClientTools(clientTools) {
|
|
|
53
1143
|
key,
|
|
54
1144
|
{
|
|
55
1145
|
...value,
|
|
56
|
-
parameters: value.parameters ?
|
|
1146
|
+
parameters: value.parameters ? zodToJsonSchema2(value.parameters) : void 0
|
|
57
1147
|
}
|
|
58
1148
|
];
|
|
59
1149
|
} else {
|
|
@@ -61,8 +1151,8 @@ function processClientTools(clientTools) {
|
|
|
61
1151
|
key,
|
|
62
1152
|
{
|
|
63
1153
|
...value,
|
|
64
|
-
inputSchema: value.inputSchema ?
|
|
65
|
-
outputSchema: value.outputSchema ?
|
|
1154
|
+
inputSchema: value.inputSchema ? zodToJsonSchema2(value.inputSchema) : void 0,
|
|
1155
|
+
outputSchema: value.outputSchema ? zodToJsonSchema2(value.outputSchema) : void 0
|
|
66
1156
|
}
|
|
67
1157
|
];
|
|
68
1158
|
}
|
|
@@ -241,10 +1331,7 @@ async function executeToolCallAndRespond({
|
|
|
241
1331
|
]
|
|
242
1332
|
}
|
|
243
1333
|
];
|
|
244
|
-
return respondFn(
|
|
245
|
-
...params,
|
|
246
|
-
messages: updatedMessages
|
|
247
|
-
});
|
|
1334
|
+
return respondFn(updatedMessages, params);
|
|
248
1335
|
}
|
|
249
1336
|
}
|
|
250
1337
|
}
|
|
@@ -332,8 +1419,8 @@ var Agent = class extends BaseResource {
|
|
|
332
1419
|
async generateLegacy(params) {
|
|
333
1420
|
const processedParams = {
|
|
334
1421
|
...params,
|
|
335
|
-
output: params.output ?
|
|
336
|
-
experimental_output: params.experimental_output ?
|
|
1422
|
+
output: params.output ? zodToJsonSchema2(params.output) : void 0,
|
|
1423
|
+
experimental_output: params.experimental_output ? zodToJsonSchema2(params.experimental_output) : void 0,
|
|
337
1424
|
requestContext: parseClientRequestContext(params.requestContext),
|
|
338
1425
|
clientTools: processClientTools(params.clientTools)
|
|
339
1426
|
};
|
|
@@ -388,26 +1475,24 @@ var Agent = class extends BaseResource {
|
|
|
388
1475
|
}
|
|
389
1476
|
return response;
|
|
390
1477
|
}
|
|
391
|
-
async generate(
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
}
|
|
396
|
-
params = {
|
|
397
|
-
messages: messagesOrParams,
|
|
398
|
-
...options
|
|
399
|
-
};
|
|
400
|
-
}
|
|
1478
|
+
async generate(messages, options) {
|
|
1479
|
+
const params = {
|
|
1480
|
+
...options,
|
|
1481
|
+
messages
|
|
1482
|
+
};
|
|
401
1483
|
const processedParams = {
|
|
402
1484
|
...params,
|
|
403
1485
|
requestContext: parseClientRequestContext(params.requestContext),
|
|
404
1486
|
clientTools: processClientTools(params.clientTools),
|
|
405
1487
|
structuredOutput: params.structuredOutput ? {
|
|
406
1488
|
...params.structuredOutput,
|
|
407
|
-
schema:
|
|
1489
|
+
schema: zodToJsonSchema2(params.structuredOutput.schema)
|
|
408
1490
|
} : void 0
|
|
409
1491
|
};
|
|
410
|
-
const {
|
|
1492
|
+
const { memory, requestContext } = processedParams;
|
|
1493
|
+
const { resource, thread } = memory ?? {};
|
|
1494
|
+
const resourceId = resource;
|
|
1495
|
+
const threadId = typeof thread === "string" ? thread : thread?.id;
|
|
411
1496
|
const response = await this.request(
|
|
412
1497
|
`/api/agents/${this.agentId}/generate`,
|
|
413
1498
|
{
|
|
@@ -494,7 +1579,7 @@ var Agent = class extends BaseResource {
|
|
|
494
1579
|
replaceLastMessage
|
|
495
1580
|
});
|
|
496
1581
|
}
|
|
497
|
-
await
|
|
1582
|
+
await processDataStream({
|
|
498
1583
|
stream,
|
|
499
1584
|
onTextPart(value) {
|
|
500
1585
|
if (currentTextPart == null) {
|
|
@@ -591,7 +1676,7 @@ var Agent = class extends BaseResource {
|
|
|
591
1676
|
onToolCallDeltaPart(value) {
|
|
592
1677
|
const partialToolCall = partialToolCalls[value.toolCallId];
|
|
593
1678
|
partialToolCall.text += value.argsTextDelta;
|
|
594
|
-
const { value: partialArgs } =
|
|
1679
|
+
const { value: partialArgs } = parsePartialJson(partialToolCall.text);
|
|
595
1680
|
const invocation = {
|
|
596
1681
|
state: "partial-call",
|
|
597
1682
|
step: partialToolCall.step,
|
|
@@ -697,8 +1782,8 @@ var Agent = class extends BaseResource {
|
|
|
697
1782
|
async streamLegacy(params) {
|
|
698
1783
|
const processedParams = {
|
|
699
1784
|
...params,
|
|
700
|
-
output: params.output ?
|
|
701
|
-
experimental_output: params.experimental_output ?
|
|
1785
|
+
output: params.output ? zodToJsonSchema2(params.output) : void 0,
|
|
1786
|
+
experimental_output: params.experimental_output ? zodToJsonSchema2(params.experimental_output) : void 0,
|
|
702
1787
|
requestContext: parseClientRequestContext(params.requestContext),
|
|
703
1788
|
clientTools: processClientTools(params.clientTools)
|
|
704
1789
|
};
|
|
@@ -710,7 +1795,7 @@ var Agent = class extends BaseResource {
|
|
|
710
1795
|
headers: response.headers
|
|
711
1796
|
});
|
|
712
1797
|
streamResponse.processDataStream = async (options = {}) => {
|
|
713
|
-
await
|
|
1798
|
+
await processDataStream({
|
|
714
1799
|
stream: streamResponse.body,
|
|
715
1800
|
...options
|
|
716
1801
|
});
|
|
@@ -793,7 +1878,7 @@ var Agent = class extends BaseResource {
|
|
|
793
1878
|
case "tripwire": {
|
|
794
1879
|
message.parts.push({
|
|
795
1880
|
type: "text",
|
|
796
|
-
text: chunk.payload.
|
|
1881
|
+
text: chunk.payload.reason
|
|
797
1882
|
});
|
|
798
1883
|
execUpdate();
|
|
799
1884
|
break;
|
|
@@ -916,7 +2001,7 @@ var Agent = class extends BaseResource {
|
|
|
916
2001
|
case "tool-call-delta": {
|
|
917
2002
|
const partialToolCall = partialToolCalls[chunk.payload.toolCallId];
|
|
918
2003
|
partialToolCall.text += chunk.payload.argsTextDelta;
|
|
919
|
-
const { value: partialArgs } =
|
|
2004
|
+
const { value: partialArgs } = parsePartialJson(partialToolCall.text);
|
|
920
2005
|
const invocation = {
|
|
921
2006
|
state: "partial-call",
|
|
922
2007
|
step: partialToolCall.step,
|
|
@@ -1108,8 +2193,35 @@ var Agent = class extends BaseResource {
|
|
|
1108
2193
|
}
|
|
1109
2194
|
return response;
|
|
1110
2195
|
}
|
|
1111
|
-
async network(params) {
|
|
2196
|
+
async network(messages, params) {
|
|
1112
2197
|
const response = await this.request(`/api/agents/${this.agentId}/network`, {
|
|
2198
|
+
method: "POST",
|
|
2199
|
+
body: {
|
|
2200
|
+
messages,
|
|
2201
|
+
...params
|
|
2202
|
+
},
|
|
2203
|
+
stream: true
|
|
2204
|
+
});
|
|
2205
|
+
if (!response.body) {
|
|
2206
|
+
throw new Error("No response body");
|
|
2207
|
+
}
|
|
2208
|
+
const streamResponse = new Response(response.body, {
|
|
2209
|
+
status: response.status,
|
|
2210
|
+
statusText: response.statusText,
|
|
2211
|
+
headers: response.headers
|
|
2212
|
+
});
|
|
2213
|
+
streamResponse.processDataStream = async ({
|
|
2214
|
+
onChunk
|
|
2215
|
+
}) => {
|
|
2216
|
+
await processMastraNetworkStream({
|
|
2217
|
+
stream: streamResponse.body,
|
|
2218
|
+
onChunk
|
|
2219
|
+
});
|
|
2220
|
+
};
|
|
2221
|
+
return streamResponse;
|
|
2222
|
+
}
|
|
2223
|
+
async approveNetworkToolCall(params) {
|
|
2224
|
+
const response = await this.request(`/api/agents/${this.agentId}/approve-network-tool-call`, {
|
|
1113
2225
|
method: "POST",
|
|
1114
2226
|
body: params,
|
|
1115
2227
|
stream: true
|
|
@@ -1132,23 +2244,42 @@ var Agent = class extends BaseResource {
|
|
|
1132
2244
|
};
|
|
1133
2245
|
return streamResponse;
|
|
1134
2246
|
}
|
|
1135
|
-
async
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
params
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
};
|
|
2247
|
+
async declineNetworkToolCall(params) {
|
|
2248
|
+
const response = await this.request(`/api/agents/${this.agentId}/decline-network-tool-call`, {
|
|
2249
|
+
method: "POST",
|
|
2250
|
+
body: params,
|
|
2251
|
+
stream: true
|
|
2252
|
+
});
|
|
2253
|
+
if (!response.body) {
|
|
2254
|
+
throw new Error("No response body");
|
|
1144
2255
|
}
|
|
2256
|
+
const streamResponse = new Response(response.body, {
|
|
2257
|
+
status: response.status,
|
|
2258
|
+
statusText: response.statusText,
|
|
2259
|
+
headers: response.headers
|
|
2260
|
+
});
|
|
2261
|
+
streamResponse.processDataStream = async ({
|
|
2262
|
+
onChunk
|
|
2263
|
+
}) => {
|
|
2264
|
+
await processMastraNetworkStream({
|
|
2265
|
+
stream: streamResponse.body,
|
|
2266
|
+
onChunk
|
|
2267
|
+
});
|
|
2268
|
+
};
|
|
2269
|
+
return streamResponse;
|
|
2270
|
+
}
|
|
2271
|
+
async stream(messagesOrParams, options) {
|
|
2272
|
+
let params = {
|
|
2273
|
+
messages: messagesOrParams,
|
|
2274
|
+
...options
|
|
2275
|
+
};
|
|
1145
2276
|
const processedParams = {
|
|
1146
2277
|
...params,
|
|
1147
2278
|
requestContext: parseClientRequestContext(params.requestContext),
|
|
1148
2279
|
clientTools: processClientTools(params.clientTools),
|
|
1149
2280
|
structuredOutput: params.structuredOutput ? {
|
|
1150
2281
|
...params.structuredOutput,
|
|
1151
|
-
schema:
|
|
2282
|
+
schema: zodToJsonSchema2(params.structuredOutput.schema)
|
|
1152
2283
|
} : void 0
|
|
1153
2284
|
};
|
|
1154
2285
|
let readableController;
|
|
@@ -1219,6 +2350,26 @@ var Agent = class extends BaseResource {
|
|
|
1219
2350
|
};
|
|
1220
2351
|
return streamResponse;
|
|
1221
2352
|
}
|
|
2353
|
+
/**
|
|
2354
|
+
* Approves a pending tool call and returns the complete response (non-streaming).
|
|
2355
|
+
* Used when `requireToolApproval` is enabled with generate() to allow the agent to proceed.
|
|
2356
|
+
*/
|
|
2357
|
+
async approveToolCallGenerate(params) {
|
|
2358
|
+
return this.request(`/api/agents/${this.agentId}/approve-tool-call-generate`, {
|
|
2359
|
+
method: "POST",
|
|
2360
|
+
body: params
|
|
2361
|
+
});
|
|
2362
|
+
}
|
|
2363
|
+
/**
|
|
2364
|
+
* Declines a pending tool call and returns the complete response (non-streaming).
|
|
2365
|
+
* Used when `requireToolApproval` is enabled with generate() to prevent tool execution.
|
|
2366
|
+
*/
|
|
2367
|
+
async declineToolCallGenerate(params) {
|
|
2368
|
+
return this.request(`/api/agents/${this.agentId}/decline-tool-call-generate`, {
|
|
2369
|
+
method: "POST",
|
|
2370
|
+
body: params
|
|
2371
|
+
});
|
|
2372
|
+
}
|
|
1222
2373
|
/**
|
|
1223
2374
|
* Processes the stream response and handles tool calls
|
|
1224
2375
|
*/
|
|
@@ -1371,7 +2522,8 @@ var Agent = class extends BaseResource {
|
|
|
1371
2522
|
*/
|
|
1372
2523
|
resetModel() {
|
|
1373
2524
|
return this.request(`/api/agents/${this.agentId}/model/reset`, {
|
|
1374
|
-
method: "POST"
|
|
2525
|
+
method: "POST",
|
|
2526
|
+
body: {}
|
|
1375
2527
|
});
|
|
1376
2528
|
}
|
|
1377
2529
|
/**
|
|
@@ -1405,15 +2557,21 @@ var MemoryThread = class extends BaseResource {
|
|
|
1405
2557
|
this.threadId = threadId;
|
|
1406
2558
|
this.agentId = agentId;
|
|
1407
2559
|
}
|
|
2560
|
+
/**
|
|
2561
|
+
* Builds the query string for agentId (if provided)
|
|
2562
|
+
*/
|
|
2563
|
+
getAgentIdQueryParam(prefix = "?") {
|
|
2564
|
+
return this.agentId ? `${prefix}agentId=${this.agentId}` : "";
|
|
2565
|
+
}
|
|
1408
2566
|
/**
|
|
1409
2567
|
* Retrieves the memory thread details
|
|
1410
2568
|
* @param requestContext - Optional request context to pass as query parameter
|
|
1411
2569
|
* @returns Promise containing thread details including title and metadata
|
|
1412
2570
|
*/
|
|
1413
2571
|
get(requestContext) {
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
);
|
|
2572
|
+
const agentIdParam = this.getAgentIdQueryParam("?");
|
|
2573
|
+
const contextParam = requestContextQueryString(requestContext, agentIdParam ? "&" : "?");
|
|
2574
|
+
return this.request(`/api/memory/threads/${this.threadId}${agentIdParam}${contextParam}`);
|
|
1417
2575
|
}
|
|
1418
2576
|
/**
|
|
1419
2577
|
* Updates the memory thread properties
|
|
@@ -1421,13 +2579,12 @@ var MemoryThread = class extends BaseResource {
|
|
|
1421
2579
|
* @returns Promise containing updated thread details
|
|
1422
2580
|
*/
|
|
1423
2581
|
update(params) {
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
);
|
|
2582
|
+
const agentIdParam = this.getAgentIdQueryParam("?");
|
|
2583
|
+
const contextParam = requestContextQueryString(params.requestContext, agentIdParam ? "&" : "?");
|
|
2584
|
+
return this.request(`/api/memory/threads/${this.threadId}${agentIdParam}${contextParam}`, {
|
|
2585
|
+
method: "PATCH",
|
|
2586
|
+
body: params
|
|
2587
|
+
});
|
|
1431
2588
|
}
|
|
1432
2589
|
/**
|
|
1433
2590
|
* Deletes the memory thread
|
|
@@ -1435,12 +2592,11 @@ var MemoryThread = class extends BaseResource {
|
|
|
1435
2592
|
* @returns Promise containing deletion result
|
|
1436
2593
|
*/
|
|
1437
2594
|
delete(requestContext) {
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
);
|
|
2595
|
+
const agentIdParam = this.getAgentIdQueryParam("?");
|
|
2596
|
+
const contextParam = requestContextQueryString(requestContext, agentIdParam ? "&" : "?");
|
|
2597
|
+
return this.request(`/api/memory/threads/${this.threadId}${agentIdParam}${contextParam}`, {
|
|
2598
|
+
method: "DELETE"
|
|
2599
|
+
});
|
|
1444
2600
|
}
|
|
1445
2601
|
/**
|
|
1446
2602
|
* Retrieves paginated messages associated with the thread with filtering and ordering options
|
|
@@ -1450,6 +2606,7 @@ var MemoryThread = class extends BaseResource {
|
|
|
1450
2606
|
listMessages(params = {}) {
|
|
1451
2607
|
const { page, perPage, orderBy, filter, include, resourceId, requestContext } = params;
|
|
1452
2608
|
const queryParams = {};
|
|
2609
|
+
if (this.agentId) queryParams.agentId = this.agentId;
|
|
1453
2610
|
if (resourceId) queryParams.resourceId = resourceId;
|
|
1454
2611
|
if (page !== void 0) queryParams.page = String(page);
|
|
1455
2612
|
if (perPage !== void 0) queryParams.perPage = String(perPage);
|
|
@@ -1458,7 +2615,7 @@ var MemoryThread = class extends BaseResource {
|
|
|
1458
2615
|
if (include) queryParams.include = JSON.stringify(include);
|
|
1459
2616
|
const query = new URLSearchParams(queryParams);
|
|
1460
2617
|
const queryString = query.toString();
|
|
1461
|
-
const url = `/api/memory/threads/${this.threadId}/messages
|
|
2618
|
+
const url = `/api/memory/threads/${this.threadId}/messages${queryString ? `?${queryString}` : ""}${requestContextQueryString(requestContext, queryString ? "&" : "?")}`;
|
|
1462
2619
|
return this.request(url);
|
|
1463
2620
|
}
|
|
1464
2621
|
/**
|
|
@@ -1469,17 +2626,32 @@ var MemoryThread = class extends BaseResource {
|
|
|
1469
2626
|
* @returns Promise containing deletion result
|
|
1470
2627
|
*/
|
|
1471
2628
|
deleteMessages(messageIds, requestContext) {
|
|
1472
|
-
const
|
|
1473
|
-
|
|
1474
|
-
|
|
2629
|
+
const queryParams = {};
|
|
2630
|
+
if (this.agentId) queryParams.agentId = this.agentId;
|
|
2631
|
+
const query = new URLSearchParams(queryParams);
|
|
2632
|
+
const queryString = query.toString();
|
|
1475
2633
|
return this.request(
|
|
1476
|
-
`/api/memory/messages/delete
|
|
2634
|
+
`/api/memory/messages/delete${queryString ? `?${queryString}` : ""}${requestContextQueryString(requestContext, queryString ? "&" : "?")}`,
|
|
1477
2635
|
{
|
|
1478
2636
|
method: "POST",
|
|
1479
2637
|
body: { messageIds }
|
|
1480
2638
|
}
|
|
1481
2639
|
);
|
|
1482
2640
|
}
|
|
2641
|
+
/**
|
|
2642
|
+
* Clones the thread with all its messages to a new thread
|
|
2643
|
+
* @param params - Clone parameters including optional new thread ID, title, metadata, and message filters
|
|
2644
|
+
* @returns Promise containing the cloned thread and copied messages
|
|
2645
|
+
*/
|
|
2646
|
+
clone(params = {}) {
|
|
2647
|
+
const { requestContext, ...body } = params;
|
|
2648
|
+
const agentIdParam = this.getAgentIdQueryParam("?");
|
|
2649
|
+
const contextParam = requestContextQueryString(requestContext, agentIdParam ? "&" : "?");
|
|
2650
|
+
return this.request(`/api/memory/threads/${this.threadId}/clone${agentIdParam}${contextParam}`, {
|
|
2651
|
+
method: "POST",
|
|
2652
|
+
body
|
|
2653
|
+
});
|
|
2654
|
+
}
|
|
1483
2655
|
};
|
|
1484
2656
|
|
|
1485
2657
|
// src/resources/vector.ts
|
|
@@ -1587,460 +2759,303 @@ var Tool = class extends BaseResource {
|
|
|
1587
2759
|
}
|
|
1588
2760
|
};
|
|
1589
2761
|
|
|
1590
|
-
// src/resources/
|
|
1591
|
-
var
|
|
1592
|
-
|
|
1593
|
-
constructor(options, workflowId) {
|
|
2762
|
+
// src/resources/processor.ts
|
|
2763
|
+
var Processor = class extends BaseResource {
|
|
2764
|
+
constructor(options, processorId) {
|
|
1594
2765
|
super(options);
|
|
1595
|
-
this.
|
|
2766
|
+
this.processorId = processorId;
|
|
1596
2767
|
}
|
|
1597
2768
|
/**
|
|
1598
|
-
* Retrieves details about the
|
|
2769
|
+
* Retrieves details about the processor
|
|
1599
2770
|
* @param requestContext - Optional request context to pass as query parameter
|
|
1600
|
-
* @returns Promise containing
|
|
2771
|
+
* @returns Promise containing processor details including phases and configurations
|
|
1601
2772
|
*/
|
|
1602
2773
|
details(requestContext) {
|
|
1603
|
-
return this.request(`/api/
|
|
2774
|
+
return this.request(`/api/processors/${this.processorId}${requestContextQueryString(requestContext)}`);
|
|
1604
2775
|
}
|
|
1605
2776
|
/**
|
|
1606
|
-
*
|
|
1607
|
-
* @param params - Parameters for
|
|
1608
|
-
* @
|
|
1609
|
-
* @returns Promise containing workflow runs array
|
|
2777
|
+
* Executes the processor with the provided parameters
|
|
2778
|
+
* @param params - Parameters required for processor execution including phase and messages
|
|
2779
|
+
* @returns Promise containing the processor execution results
|
|
1610
2780
|
*/
|
|
1611
|
-
|
|
1612
|
-
const
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
}
|
|
1623
|
-
if (params?.perPage !== void 0) {
|
|
1624
|
-
searchParams.set("perPage", String(params.perPage));
|
|
1625
|
-
}
|
|
1626
|
-
if (params?.limit !== null && params?.limit !== void 0) {
|
|
1627
|
-
if (params.limit === false) {
|
|
1628
|
-
searchParams.set("limit", "false");
|
|
1629
|
-
} else if (typeof params.limit === "number" && params.limit > 0 && Number.isInteger(params.limit)) {
|
|
1630
|
-
searchParams.set("limit", String(params.limit));
|
|
1631
|
-
}
|
|
1632
|
-
}
|
|
1633
|
-
if (params?.offset !== null && params?.offset !== void 0 && !isNaN(Number(params?.offset))) {
|
|
1634
|
-
searchParams.set("offset", String(params.offset));
|
|
1635
|
-
}
|
|
1636
|
-
if (params?.resourceId) {
|
|
1637
|
-
searchParams.set("resourceId", params.resourceId);
|
|
1638
|
-
}
|
|
1639
|
-
if (requestContextParam) {
|
|
1640
|
-
searchParams.set("requestContext", requestContextParam);
|
|
1641
|
-
}
|
|
1642
|
-
if (searchParams.size) {
|
|
1643
|
-
return this.request(`/api/workflows/${this.workflowId}/runs?${searchParams}`);
|
|
1644
|
-
} else {
|
|
1645
|
-
return this.request(`/api/workflows/${this.workflowId}/runs`);
|
|
1646
|
-
}
|
|
2781
|
+
execute(params) {
|
|
2782
|
+
const body = {
|
|
2783
|
+
phase: params.phase,
|
|
2784
|
+
messages: params.messages,
|
|
2785
|
+
agentId: params.agentId,
|
|
2786
|
+
requestContext: parseClientRequestContext(params.requestContext)
|
|
2787
|
+
};
|
|
2788
|
+
return this.request(`/api/processors/${this.processorId}/execute`, {
|
|
2789
|
+
method: "POST",
|
|
2790
|
+
body
|
|
2791
|
+
});
|
|
1647
2792
|
}
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
2793
|
+
};
|
|
2794
|
+
function deserializeWorkflowError(result) {
|
|
2795
|
+
if (result.status === "failed" && result.error) {
|
|
2796
|
+
result.error = error.getErrorFromUnknown(result.error, {
|
|
2797
|
+
fallbackMessage: "Unknown workflow error",
|
|
2798
|
+
supportSerialization: false
|
|
2799
|
+
});
|
|
2800
|
+
}
|
|
2801
|
+
return result;
|
|
2802
|
+
}
|
|
2803
|
+
var RECORD_SEPARATOR = "";
|
|
2804
|
+
var Run = class extends BaseResource {
|
|
2805
|
+
constructor(options, workflowId, runId) {
|
|
2806
|
+
super(options);
|
|
2807
|
+
this.workflowId = workflowId;
|
|
2808
|
+
this.runId = runId;
|
|
1656
2809
|
}
|
|
1657
2810
|
/**
|
|
1658
|
-
*
|
|
1659
|
-
* @param runId - The ID of the workflow run to retrieve the execution result for
|
|
1660
|
-
* @param requestContext - Optional request context to pass as query parameter
|
|
1661
|
-
* @returns Promise containing the workflow run execution result
|
|
2811
|
+
* Creates a transform stream that parses RECORD_SEPARATOR-delimited JSON chunks
|
|
1662
2812
|
*/
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
2813
|
+
createChunkTransformStream() {
|
|
2814
|
+
let failedChunk = void 0;
|
|
2815
|
+
return new TransformStream({
|
|
2816
|
+
start() {
|
|
2817
|
+
},
|
|
2818
|
+
async transform(chunk, controller) {
|
|
2819
|
+
try {
|
|
2820
|
+
const decoded = new TextDecoder().decode(chunk);
|
|
2821
|
+
const chunks = decoded.split(RECORD_SEPARATOR);
|
|
2822
|
+
for (const chunk2 of chunks) {
|
|
2823
|
+
if (chunk2) {
|
|
2824
|
+
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
2825
|
+
try {
|
|
2826
|
+
const parsedChunk = JSON.parse(newChunk);
|
|
2827
|
+
controller.enqueue(parsedChunk);
|
|
2828
|
+
failedChunk = void 0;
|
|
2829
|
+
} catch {
|
|
2830
|
+
failedChunk = newChunk;
|
|
2831
|
+
}
|
|
2832
|
+
}
|
|
2833
|
+
}
|
|
2834
|
+
} catch {
|
|
2835
|
+
}
|
|
2836
|
+
}
|
|
2837
|
+
});
|
|
1667
2838
|
}
|
|
1668
2839
|
/**
|
|
1669
2840
|
* Cancels a specific workflow run by its ID
|
|
1670
|
-
* @param runId - The ID of the workflow run to cancel
|
|
1671
2841
|
* @returns Promise containing a success message
|
|
2842
|
+
* @deprecated Use `cancel()` instead
|
|
1672
2843
|
*/
|
|
1673
|
-
cancelRun(
|
|
1674
|
-
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}/cancel`, {
|
|
2844
|
+
cancelRun() {
|
|
2845
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${this.runId}/cancel`, {
|
|
1675
2846
|
method: "POST"
|
|
1676
2847
|
});
|
|
1677
2848
|
}
|
|
1678
2849
|
/**
|
|
1679
|
-
*
|
|
1680
|
-
*
|
|
1681
|
-
*
|
|
2850
|
+
* Cancels a workflow run.
|
|
2851
|
+
*
|
|
2852
|
+
* This method aborts any running steps and updates the workflow status to 'canceled' .
|
|
2853
|
+
* It works for both actively running workflows and suspended/waiting workflows.
|
|
2854
|
+
*
|
|
2855
|
+
* ## How cancellation works
|
|
2856
|
+
*
|
|
2857
|
+
* When called, the workflow will:
|
|
2858
|
+
* 1. **Trigger the abort signal** - Uses the standard Web API AbortSignal to notify running steps
|
|
2859
|
+
* 2. **Prevent subsequent steps** - No further steps will be executed
|
|
2860
|
+
*
|
|
2861
|
+
* ## Abort signal behavior
|
|
2862
|
+
*
|
|
2863
|
+
* Steps that check the `abortSignal` parameter can respond to cancellation:
|
|
2864
|
+
* - Steps can listen to the 'abort' event: `abortSignal.addEventListener('abort', callback)`
|
|
2865
|
+
* - Steps can check if already aborted: `if (abortSignal.aborted) { ... }`
|
|
2866
|
+
* - Useful for canceling timeouts, network requests, or long-running operations
|
|
2867
|
+
*
|
|
2868
|
+
* **Note:** Steps must actively check the abort signal to be canceled mid-execution.
|
|
2869
|
+
* Steps that don't check the signal will run to completion, but subsequent steps won't execute.
|
|
2870
|
+
*
|
|
2871
|
+
* @returns Promise that resolves with `{ message: 'Workflow run canceled' }` when cancellation succeeds
|
|
2872
|
+
* @throws {HTTPException} 400 - If workflow ID or run ID is missing
|
|
2873
|
+
* @throws {HTTPException} 404 - If workflow or workflow run is not found
|
|
2874
|
+
*
|
|
2875
|
+
* @example
|
|
2876
|
+
* ```typescript
|
|
2877
|
+
* const run = await workflow.createRun({ runId: 'run-123' });
|
|
2878
|
+
* await run.cancel();
|
|
2879
|
+
* // Returns: { message: 'Workflow run canceled' }
|
|
2880
|
+
* ```
|
|
2881
|
+
*
|
|
2882
|
+
* @example
|
|
2883
|
+
* ```typescript
|
|
2884
|
+
* // Example of a step that responds to cancellation
|
|
2885
|
+
* const step = createStep({
|
|
2886
|
+
* id: 'long-running-step',
|
|
2887
|
+
* execute: async ({ inputData, abortSignal, abort }) => {
|
|
2888
|
+
* const timeout = new Promise((resolve) => {
|
|
2889
|
+
* const timer = setTimeout(() => resolve('done'), 10000);
|
|
2890
|
+
*
|
|
2891
|
+
* // Clean up if canceled
|
|
2892
|
+
* abortSignal.addEventListener('abort', () => {
|
|
2893
|
+
* clearTimeout(timer);
|
|
2894
|
+
* resolve('canceled');
|
|
2895
|
+
* });
|
|
2896
|
+
* });
|
|
2897
|
+
*
|
|
2898
|
+
* const result = await timeout;
|
|
2899
|
+
*
|
|
2900
|
+
* // Check if aborted after async operation
|
|
2901
|
+
* if (abortSignal.aborted) {
|
|
2902
|
+
* return abort(); // Stop execution
|
|
2903
|
+
* }
|
|
2904
|
+
*
|
|
2905
|
+
* return { result };
|
|
2906
|
+
* }
|
|
2907
|
+
* });
|
|
2908
|
+
* ```
|
|
1682
2909
|
*/
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
}
|
|
1688
|
-
const res = await this.request(
|
|
1689
|
-
`/api/workflows/${this.workflowId}/create-run?${searchParams.toString()}`,
|
|
1690
|
-
{
|
|
1691
|
-
method: "POST"
|
|
1692
|
-
}
|
|
1693
|
-
);
|
|
1694
|
-
const runId = res.runId;
|
|
1695
|
-
return {
|
|
1696
|
-
runId,
|
|
1697
|
-
start: async (p) => {
|
|
1698
|
-
return this.start({
|
|
1699
|
-
runId,
|
|
1700
|
-
inputData: p.inputData,
|
|
1701
|
-
initialState: p.initialState,
|
|
1702
|
-
requestContext: p.requestContext,
|
|
1703
|
-
tracingOptions: p.tracingOptions
|
|
1704
|
-
});
|
|
1705
|
-
},
|
|
1706
|
-
startAsync: async (p) => {
|
|
1707
|
-
return this.startAsync({
|
|
1708
|
-
runId,
|
|
1709
|
-
inputData: p.inputData,
|
|
1710
|
-
initialState: p.initialState,
|
|
1711
|
-
requestContext: p.requestContext,
|
|
1712
|
-
tracingOptions: p.tracingOptions
|
|
1713
|
-
});
|
|
1714
|
-
},
|
|
1715
|
-
stream: async (p) => {
|
|
1716
|
-
return this.stream({
|
|
1717
|
-
runId,
|
|
1718
|
-
inputData: p.inputData,
|
|
1719
|
-
initialState: p.initialState,
|
|
1720
|
-
requestContext: p.requestContext
|
|
1721
|
-
});
|
|
1722
|
-
},
|
|
1723
|
-
resume: async (p) => {
|
|
1724
|
-
return this.resume({
|
|
1725
|
-
runId,
|
|
1726
|
-
step: p.step,
|
|
1727
|
-
resumeData: p.resumeData,
|
|
1728
|
-
requestContext: p.requestContext,
|
|
1729
|
-
tracingOptions: p.tracingOptions
|
|
1730
|
-
});
|
|
1731
|
-
},
|
|
1732
|
-
resumeAsync: async (p) => {
|
|
1733
|
-
return this.resumeAsync({
|
|
1734
|
-
runId,
|
|
1735
|
-
step: p.step,
|
|
1736
|
-
resumeData: p.resumeData,
|
|
1737
|
-
requestContext: p.requestContext,
|
|
1738
|
-
tracingOptions: p.tracingOptions
|
|
1739
|
-
});
|
|
1740
|
-
},
|
|
1741
|
-
resumeStreamVNext: async (p) => {
|
|
1742
|
-
return this.resumeStreamVNext({
|
|
1743
|
-
runId,
|
|
1744
|
-
step: p.step,
|
|
1745
|
-
resumeData: p.resumeData,
|
|
1746
|
-
requestContext: p.requestContext
|
|
1747
|
-
});
|
|
1748
|
-
}
|
|
1749
|
-
};
|
|
2910
|
+
cancel() {
|
|
2911
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${this.runId}/cancel`, {
|
|
2912
|
+
method: "POST"
|
|
2913
|
+
});
|
|
1750
2914
|
}
|
|
1751
2915
|
/**
|
|
1752
2916
|
* Starts a workflow run synchronously without waiting for the workflow to complete
|
|
1753
|
-
* @param params - Object containing the
|
|
2917
|
+
* @param params - Object containing the inputData, initialState and requestContext
|
|
1754
2918
|
* @returns Promise containing success message
|
|
1755
2919
|
*/
|
|
1756
2920
|
start(params) {
|
|
1757
2921
|
const requestContext = parseClientRequestContext(params.requestContext);
|
|
1758
|
-
return this.request(`/api/workflows/${this.workflowId}/start?runId=${
|
|
2922
|
+
return this.request(`/api/workflows/${this.workflowId}/start?runId=${this.runId}`, {
|
|
1759
2923
|
method: "POST",
|
|
1760
2924
|
body: {
|
|
1761
2925
|
inputData: params?.inputData,
|
|
1762
2926
|
initialState: params?.initialState,
|
|
1763
2927
|
requestContext,
|
|
1764
|
-
tracingOptions: params.tracingOptions
|
|
2928
|
+
tracingOptions: params.tracingOptions,
|
|
2929
|
+
perStep: params.perStep
|
|
1765
2930
|
}
|
|
1766
2931
|
});
|
|
1767
2932
|
}
|
|
1768
2933
|
/**
|
|
1769
2934
|
* Resumes a suspended workflow step synchronously without waiting for the workflow to complete
|
|
1770
|
-
* @param params - Object containing the
|
|
2935
|
+
* @param params - Object containing the step, resumeData and requestContext
|
|
1771
2936
|
* @returns Promise containing success message
|
|
1772
2937
|
*/
|
|
1773
2938
|
resume({
|
|
1774
2939
|
step,
|
|
1775
|
-
runId,
|
|
1776
2940
|
resumeData,
|
|
1777
2941
|
tracingOptions,
|
|
2942
|
+
perStep,
|
|
1778
2943
|
...rest
|
|
1779
2944
|
}) {
|
|
1780
2945
|
const requestContext = parseClientRequestContext(rest.requestContext);
|
|
1781
|
-
return this.request(`/api/workflows/${this.workflowId}/resume?runId=${runId}`, {
|
|
1782
|
-
method: "POST",
|
|
1783
|
-
body: {
|
|
1784
|
-
step,
|
|
1785
|
-
resumeData,
|
|
1786
|
-
requestContext,
|
|
1787
|
-
tracingOptions
|
|
1788
|
-
}
|
|
1789
|
-
});
|
|
1790
|
-
}
|
|
1791
|
-
/**
|
|
1792
|
-
* Starts a workflow run asynchronously and returns a promise that resolves when the workflow is complete
|
|
1793
|
-
* @param params - Object containing the optional runId, inputData, initialState and requestContext
|
|
1794
|
-
* @returns Promise containing the workflow execution results
|
|
1795
|
-
*/
|
|
1796
|
-
startAsync(params) {
|
|
1797
|
-
const searchParams = new URLSearchParams();
|
|
1798
|
-
if (!!params?.runId) {
|
|
1799
|
-
searchParams.set("runId", params.runId);
|
|
1800
|
-
}
|
|
1801
|
-
const requestContext = parseClientRequestContext(params.requestContext);
|
|
1802
|
-
return this.request(`/api/workflows/${this.workflowId}/start-async?${searchParams.toString()}`, {
|
|
2946
|
+
return this.request(`/api/workflows/${this.workflowId}/resume?runId=${this.runId}`, {
|
|
1803
2947
|
method: "POST",
|
|
1804
2948
|
body: {
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
requestContext,
|
|
1808
|
-
tracingOptions
|
|
1809
|
-
|
|
1810
|
-
});
|
|
1811
|
-
}
|
|
1812
|
-
/**
|
|
1813
|
-
* Starts a workflow run and returns a stream
|
|
1814
|
-
* @param params - Object containing the optional runId, inputData, initialState and requestContext
|
|
1815
|
-
* @returns Promise containing the workflow execution results
|
|
1816
|
-
*/
|
|
1817
|
-
async stream(params) {
|
|
1818
|
-
const searchParams = new URLSearchParams();
|
|
1819
|
-
if (!!params?.runId) {
|
|
1820
|
-
searchParams.set("runId", params.runId);
|
|
1821
|
-
}
|
|
1822
|
-
const requestContext = parseClientRequestContext(params.requestContext);
|
|
1823
|
-
const response = await this.request(
|
|
1824
|
-
`/api/workflows/${this.workflowId}/stream?${searchParams.toString()}`,
|
|
1825
|
-
{
|
|
1826
|
-
method: "POST",
|
|
1827
|
-
body: {
|
|
1828
|
-
inputData: params.inputData,
|
|
1829
|
-
initialState: params.initialState,
|
|
1830
|
-
requestContext,
|
|
1831
|
-
tracingOptions: params.tracingOptions
|
|
1832
|
-
},
|
|
1833
|
-
stream: true
|
|
1834
|
-
}
|
|
1835
|
-
);
|
|
1836
|
-
if (!response.ok) {
|
|
1837
|
-
throw new Error(`Failed to stream workflow: ${response.statusText}`);
|
|
1838
|
-
}
|
|
1839
|
-
if (!response.body) {
|
|
1840
|
-
throw new Error("Response body is null");
|
|
1841
|
-
}
|
|
1842
|
-
let failedChunk = void 0;
|
|
1843
|
-
const transformStream = new TransformStream({
|
|
1844
|
-
start() {
|
|
1845
|
-
},
|
|
1846
|
-
async transform(chunk, controller) {
|
|
1847
|
-
try {
|
|
1848
|
-
const decoded = new TextDecoder().decode(chunk);
|
|
1849
|
-
const chunks = decoded.split(RECORD_SEPARATOR);
|
|
1850
|
-
for (const chunk2 of chunks) {
|
|
1851
|
-
if (chunk2) {
|
|
1852
|
-
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
1853
|
-
try {
|
|
1854
|
-
const parsedChunk = JSON.parse(newChunk);
|
|
1855
|
-
controller.enqueue(parsedChunk);
|
|
1856
|
-
failedChunk = void 0;
|
|
1857
|
-
} catch {
|
|
1858
|
-
failedChunk = newChunk;
|
|
1859
|
-
}
|
|
1860
|
-
}
|
|
1861
|
-
}
|
|
1862
|
-
} catch {
|
|
1863
|
-
}
|
|
1864
|
-
}
|
|
1865
|
-
});
|
|
1866
|
-
return response.body.pipeThrough(transformStream);
|
|
1867
|
-
}
|
|
1868
|
-
/**
|
|
1869
|
-
* Observes workflow stream for a workflow run
|
|
1870
|
-
* @param params - Object containing the runId
|
|
1871
|
-
* @returns Promise containing the workflow execution results
|
|
1872
|
-
*/
|
|
1873
|
-
async observeStream(params) {
|
|
1874
|
-
const searchParams = new URLSearchParams();
|
|
1875
|
-
searchParams.set("runId", params.runId);
|
|
1876
|
-
const response = await this.request(
|
|
1877
|
-
`/api/workflows/${this.workflowId}/observe-stream?${searchParams.toString()}`,
|
|
1878
|
-
{
|
|
1879
|
-
method: "POST",
|
|
1880
|
-
stream: true
|
|
1881
|
-
}
|
|
1882
|
-
);
|
|
1883
|
-
if (!response.ok) {
|
|
1884
|
-
throw new Error(`Failed to observe workflow stream: ${response.statusText}`);
|
|
1885
|
-
}
|
|
1886
|
-
if (!response.body) {
|
|
1887
|
-
throw new Error("Response body is null");
|
|
1888
|
-
}
|
|
1889
|
-
let failedChunk = void 0;
|
|
1890
|
-
const transformStream = new TransformStream({
|
|
1891
|
-
start() {
|
|
1892
|
-
},
|
|
1893
|
-
async transform(chunk, controller) {
|
|
1894
|
-
try {
|
|
1895
|
-
const decoded = new TextDecoder().decode(chunk);
|
|
1896
|
-
const chunks = decoded.split(RECORD_SEPARATOR);
|
|
1897
|
-
for (const chunk2 of chunks) {
|
|
1898
|
-
if (chunk2) {
|
|
1899
|
-
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
1900
|
-
try {
|
|
1901
|
-
const parsedChunk = JSON.parse(newChunk);
|
|
1902
|
-
controller.enqueue(parsedChunk);
|
|
1903
|
-
failedChunk = void 0;
|
|
1904
|
-
} catch {
|
|
1905
|
-
failedChunk = newChunk;
|
|
1906
|
-
}
|
|
1907
|
-
}
|
|
1908
|
-
}
|
|
1909
|
-
} catch {
|
|
1910
|
-
}
|
|
2949
|
+
step,
|
|
2950
|
+
resumeData,
|
|
2951
|
+
requestContext,
|
|
2952
|
+
tracingOptions,
|
|
2953
|
+
perStep
|
|
1911
2954
|
}
|
|
1912
2955
|
});
|
|
1913
|
-
|
|
2956
|
+
}
|
|
2957
|
+
/**
|
|
2958
|
+
* Starts a workflow run asynchronously and returns a promise that resolves when the workflow is complete
|
|
2959
|
+
* @param params - Object containing the inputData, initialState and requestContext
|
|
2960
|
+
* @returns Promise containing the workflow execution results
|
|
2961
|
+
*/
|
|
2962
|
+
startAsync(params) {
|
|
2963
|
+
const searchParams = new URLSearchParams();
|
|
2964
|
+
searchParams.set("runId", this.runId);
|
|
2965
|
+
const requestContext = parseClientRequestContext(params.requestContext);
|
|
2966
|
+
return this.request(`/api/workflows/${this.workflowId}/start-async?${searchParams.toString()}`, {
|
|
2967
|
+
method: "POST",
|
|
2968
|
+
body: {
|
|
2969
|
+
inputData: params.inputData,
|
|
2970
|
+
initialState: params.initialState,
|
|
2971
|
+
requestContext,
|
|
2972
|
+
tracingOptions: params.tracingOptions,
|
|
2973
|
+
resourceId: params.resourceId,
|
|
2974
|
+
perStep: params.perStep
|
|
2975
|
+
}
|
|
2976
|
+
}).then(deserializeWorkflowError);
|
|
1914
2977
|
}
|
|
1915
2978
|
/**
|
|
1916
2979
|
* Starts a workflow run and returns a stream
|
|
1917
|
-
* @param params - Object containing the
|
|
2980
|
+
* @param params - Object containing the inputData, initialState and requestContext
|
|
1918
2981
|
* @returns Promise containing the workflow execution results
|
|
1919
2982
|
*/
|
|
1920
|
-
async
|
|
2983
|
+
async stream(params) {
|
|
1921
2984
|
const searchParams = new URLSearchParams();
|
|
1922
|
-
|
|
1923
|
-
searchParams.set("runId", params.runId);
|
|
1924
|
-
}
|
|
2985
|
+
searchParams.set("runId", this.runId);
|
|
1925
2986
|
const requestContext = parseClientRequestContext(params.requestContext);
|
|
1926
2987
|
const response = await this.request(
|
|
1927
|
-
`/api/workflows/${this.workflowId}/
|
|
2988
|
+
`/api/workflows/${this.workflowId}/stream?${searchParams.toString()}`,
|
|
1928
2989
|
{
|
|
1929
2990
|
method: "POST",
|
|
1930
2991
|
body: {
|
|
1931
2992
|
inputData: params.inputData,
|
|
1932
2993
|
initialState: params.initialState,
|
|
1933
2994
|
requestContext,
|
|
1934
|
-
|
|
1935
|
-
|
|
2995
|
+
tracingOptions: params.tracingOptions,
|
|
2996
|
+
resourceId: params.resourceId,
|
|
2997
|
+
perStep: params.perStep,
|
|
2998
|
+
closeOnSuspend: params.closeOnSuspend
|
|
1936
2999
|
},
|
|
1937
3000
|
stream: true
|
|
1938
3001
|
}
|
|
1939
3002
|
);
|
|
1940
3003
|
if (!response.ok) {
|
|
1941
|
-
throw new Error(`Failed to stream
|
|
3004
|
+
throw new Error(`Failed to stream workflow: ${response.statusText}`);
|
|
1942
3005
|
}
|
|
1943
3006
|
if (!response.body) {
|
|
1944
3007
|
throw new Error("Response body is null");
|
|
1945
3008
|
}
|
|
1946
|
-
|
|
1947
|
-
const transformStream = new TransformStream({
|
|
1948
|
-
start() {
|
|
1949
|
-
},
|
|
1950
|
-
async transform(chunk, controller) {
|
|
1951
|
-
try {
|
|
1952
|
-
const decoded = new TextDecoder().decode(chunk);
|
|
1953
|
-
const chunks = decoded.split(RECORD_SEPARATOR);
|
|
1954
|
-
for (const chunk2 of chunks) {
|
|
1955
|
-
if (chunk2) {
|
|
1956
|
-
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
1957
|
-
try {
|
|
1958
|
-
const parsedChunk = JSON.parse(newChunk);
|
|
1959
|
-
controller.enqueue(parsedChunk);
|
|
1960
|
-
failedChunk = void 0;
|
|
1961
|
-
} catch {
|
|
1962
|
-
failedChunk = newChunk;
|
|
1963
|
-
}
|
|
1964
|
-
}
|
|
1965
|
-
}
|
|
1966
|
-
} catch {
|
|
1967
|
-
}
|
|
1968
|
-
}
|
|
1969
|
-
});
|
|
1970
|
-
return response.body.pipeThrough(transformStream);
|
|
3009
|
+
return response.body.pipeThrough(this.createChunkTransformStream());
|
|
1971
3010
|
}
|
|
1972
3011
|
/**
|
|
1973
|
-
* Observes workflow
|
|
1974
|
-
* @param params - Object containing the runId
|
|
3012
|
+
* Observes workflow stream for a workflow run
|
|
1975
3013
|
* @returns Promise containing the workflow execution results
|
|
1976
3014
|
*/
|
|
1977
|
-
async
|
|
3015
|
+
async observeStream() {
|
|
1978
3016
|
const searchParams = new URLSearchParams();
|
|
1979
|
-
searchParams.set("runId",
|
|
3017
|
+
searchParams.set("runId", this.runId);
|
|
1980
3018
|
const response = await this.request(
|
|
1981
|
-
`/api/workflows/${this.workflowId}/observe-
|
|
3019
|
+
`/api/workflows/${this.workflowId}/observe-stream?${searchParams.toString()}`,
|
|
1982
3020
|
{
|
|
1983
3021
|
method: "POST",
|
|
1984
3022
|
stream: true
|
|
1985
3023
|
}
|
|
1986
3024
|
);
|
|
1987
3025
|
if (!response.ok) {
|
|
1988
|
-
throw new Error(`Failed to observe stream
|
|
3026
|
+
throw new Error(`Failed to observe workflow stream: ${response.statusText}`);
|
|
1989
3027
|
}
|
|
1990
3028
|
if (!response.body) {
|
|
1991
3029
|
throw new Error("Response body is null");
|
|
1992
3030
|
}
|
|
1993
|
-
|
|
1994
|
-
const transformStream = new TransformStream({
|
|
1995
|
-
start() {
|
|
1996
|
-
},
|
|
1997
|
-
async transform(chunk, controller) {
|
|
1998
|
-
try {
|
|
1999
|
-
const decoded = new TextDecoder().decode(chunk);
|
|
2000
|
-
const chunks = decoded.split(RECORD_SEPARATOR);
|
|
2001
|
-
for (const chunk2 of chunks) {
|
|
2002
|
-
if (chunk2) {
|
|
2003
|
-
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
2004
|
-
try {
|
|
2005
|
-
const parsedChunk = JSON.parse(newChunk);
|
|
2006
|
-
controller.enqueue(parsedChunk);
|
|
2007
|
-
failedChunk = void 0;
|
|
2008
|
-
} catch {
|
|
2009
|
-
failedChunk = newChunk;
|
|
2010
|
-
}
|
|
2011
|
-
}
|
|
2012
|
-
}
|
|
2013
|
-
} catch {
|
|
2014
|
-
}
|
|
2015
|
-
}
|
|
2016
|
-
});
|
|
2017
|
-
return response.body.pipeThrough(transformStream);
|
|
3031
|
+
return response.body.pipeThrough(this.createChunkTransformStream());
|
|
2018
3032
|
}
|
|
2019
3033
|
/**
|
|
2020
3034
|
* Resumes a suspended workflow step asynchronously and returns a promise that resolves when the workflow is complete
|
|
2021
|
-
* @param params - Object containing the
|
|
3035
|
+
* @param params - Object containing the step, resumeData and requestContext
|
|
2022
3036
|
* @returns Promise containing the workflow resume results
|
|
2023
3037
|
*/
|
|
2024
3038
|
resumeAsync(params) {
|
|
2025
3039
|
const requestContext = parseClientRequestContext(params.requestContext);
|
|
2026
|
-
return this.request(`/api/workflows/${this.workflowId}/resume-async?runId=${
|
|
3040
|
+
return this.request(`/api/workflows/${this.workflowId}/resume-async?runId=${this.runId}`, {
|
|
2027
3041
|
method: "POST",
|
|
2028
3042
|
body: {
|
|
2029
3043
|
step: params.step,
|
|
2030
3044
|
resumeData: params.resumeData,
|
|
2031
3045
|
requestContext,
|
|
2032
|
-
tracingOptions: params.tracingOptions
|
|
3046
|
+
tracingOptions: params.tracingOptions,
|
|
3047
|
+
perStep: params.perStep
|
|
2033
3048
|
}
|
|
2034
|
-
});
|
|
3049
|
+
}).then(deserializeWorkflowError);
|
|
2035
3050
|
}
|
|
2036
3051
|
/**
|
|
2037
|
-
* Resumes a suspended workflow step that uses
|
|
2038
|
-
* @param params - Object containing the
|
|
3052
|
+
* Resumes a suspended workflow step that uses stream asynchronously and returns a promise that resolves when the workflow is complete
|
|
3053
|
+
* @param params - Object containing the step, resumeData and requestContext
|
|
2039
3054
|
* @returns Promise containing the workflow resume results
|
|
2040
3055
|
*/
|
|
2041
|
-
async
|
|
3056
|
+
async resumeStream(params) {
|
|
2042
3057
|
const searchParams = new URLSearchParams();
|
|
2043
|
-
searchParams.set("runId",
|
|
3058
|
+
searchParams.set("runId", this.runId);
|
|
2044
3059
|
const requestContext = parseClientRequestContext(params.requestContext);
|
|
2045
3060
|
const response = await this.request(
|
|
2046
3061
|
`/api/workflows/${this.workflowId}/resume-stream?${searchParams.toString()}`,
|
|
@@ -2050,7 +3065,8 @@ var Workflow = class extends BaseResource {
|
|
|
2050
3065
|
step: params.step,
|
|
2051
3066
|
resumeData: params.resumeData,
|
|
2052
3067
|
requestContext,
|
|
2053
|
-
tracingOptions: params.tracingOptions
|
|
3068
|
+
tracingOptions: params.tracingOptions,
|
|
3069
|
+
perStep: params.perStep
|
|
2054
3070
|
},
|
|
2055
3071
|
stream: true
|
|
2056
3072
|
}
|
|
@@ -2061,31 +3077,213 @@ var Workflow = class extends BaseResource {
|
|
|
2061
3077
|
if (!response.body) {
|
|
2062
3078
|
throw new Error("Response body is null");
|
|
2063
3079
|
}
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
3080
|
+
return response.body.pipeThrough(this.createChunkTransformStream());
|
|
3081
|
+
}
|
|
3082
|
+
/**
|
|
3083
|
+
* Restarts an active workflow run synchronously without waiting for the workflow to complete
|
|
3084
|
+
* @param params - Object containing the requestContext
|
|
3085
|
+
* @returns Promise containing success message
|
|
3086
|
+
*/
|
|
3087
|
+
restart(params) {
|
|
3088
|
+
const requestContext = parseClientRequestContext(params.requestContext);
|
|
3089
|
+
return this.request(`/api/workflows/${this.workflowId}/restart?runId=${this.runId}`, {
|
|
3090
|
+
method: "POST",
|
|
3091
|
+
body: {
|
|
3092
|
+
requestContext,
|
|
3093
|
+
tracingOptions: params.tracingOptions
|
|
3094
|
+
}
|
|
3095
|
+
});
|
|
3096
|
+
}
|
|
3097
|
+
/**
|
|
3098
|
+
* Restarts an active workflow run asynchronously
|
|
3099
|
+
* @param params - optional object containing the requestContext
|
|
3100
|
+
* @returns Promise containing the workflow restart results
|
|
3101
|
+
*/
|
|
3102
|
+
restartAsync(params) {
|
|
3103
|
+
const requestContext = parseClientRequestContext(params?.requestContext);
|
|
3104
|
+
return this.request(`/api/workflows/${this.workflowId}/restart-async?runId=${this.runId}`, {
|
|
3105
|
+
method: "POST",
|
|
3106
|
+
body: {
|
|
3107
|
+
requestContext,
|
|
3108
|
+
tracingOptions: params?.tracingOptions
|
|
3109
|
+
}
|
|
3110
|
+
}).then(deserializeWorkflowError);
|
|
3111
|
+
}
|
|
3112
|
+
/**
|
|
3113
|
+
* Time travels a workflow run synchronously without waiting for the workflow to complete
|
|
3114
|
+
* @param params - Object containing the step, inputData, resumeData, initialState, context, nestedStepsContext, requestContext and tracingOptions
|
|
3115
|
+
* @returns Promise containing success message
|
|
3116
|
+
*/
|
|
3117
|
+
timeTravel({ requestContext: paramsRequestContext, ...params }) {
|
|
3118
|
+
const requestContext = parseClientRequestContext(paramsRequestContext);
|
|
3119
|
+
return this.request(`/api/workflows/${this.workflowId}/time-travel?runId=${this.runId}`, {
|
|
3120
|
+
method: "POST",
|
|
3121
|
+
body: {
|
|
3122
|
+
...params,
|
|
3123
|
+
requestContext
|
|
3124
|
+
}
|
|
3125
|
+
});
|
|
3126
|
+
}
|
|
3127
|
+
/**
|
|
3128
|
+
* Time travels a workflow run asynchronously
|
|
3129
|
+
* @param params - Object containing the step, inputData, resumeData, initialState, context, nestedStepsContext, requestContext and tracingOptions
|
|
3130
|
+
* @returns Promise containing the workflow time travel results
|
|
3131
|
+
*/
|
|
3132
|
+
timeTravelAsync({ requestContext: paramsRequestContext, ...params }) {
|
|
3133
|
+
const requestContext = parseClientRequestContext(paramsRequestContext);
|
|
3134
|
+
return this.request(`/api/workflows/${this.workflowId}/time-travel-async?runId=${this.runId}`, {
|
|
3135
|
+
method: "POST",
|
|
3136
|
+
body: {
|
|
3137
|
+
...params,
|
|
3138
|
+
requestContext
|
|
3139
|
+
}
|
|
3140
|
+
}).then(deserializeWorkflowError);
|
|
3141
|
+
}
|
|
3142
|
+
/**
|
|
3143
|
+
* Time travels a workflow run and returns a stream
|
|
3144
|
+
* @param params - Object containing the step, inputData, resumeData, initialState, context, nestedStepsContext, requestContext and tracingOptions
|
|
3145
|
+
* @returns Promise containing the workflow execution results
|
|
3146
|
+
*/
|
|
3147
|
+
async timeTravelStream({ requestContext: paramsRequestContext, ...params }) {
|
|
3148
|
+
const requestContext = parseClientRequestContext(paramsRequestContext);
|
|
3149
|
+
const response = await this.request(
|
|
3150
|
+
`/api/workflows/${this.workflowId}/time-travel-stream?runId=${this.runId}`,
|
|
3151
|
+
{
|
|
3152
|
+
method: "POST",
|
|
3153
|
+
body: {
|
|
3154
|
+
...params,
|
|
3155
|
+
requestContext
|
|
3156
|
+
},
|
|
3157
|
+
stream: true
|
|
3158
|
+
}
|
|
3159
|
+
);
|
|
3160
|
+
if (!response.ok) {
|
|
3161
|
+
throw new Error(`Failed to time travel workflow: ${response.statusText}`);
|
|
3162
|
+
}
|
|
3163
|
+
if (!response.body) {
|
|
3164
|
+
throw new Error("Response body is null");
|
|
3165
|
+
}
|
|
3166
|
+
return response.body.pipeThrough(this.createChunkTransformStream());
|
|
3167
|
+
}
|
|
3168
|
+
};
|
|
3169
|
+
|
|
3170
|
+
// src/resources/workflow.ts
|
|
3171
|
+
var RECORD_SEPARATOR2 = "";
|
|
3172
|
+
var Workflow = class extends BaseResource {
|
|
3173
|
+
constructor(options, workflowId) {
|
|
3174
|
+
super(options);
|
|
3175
|
+
this.workflowId = workflowId;
|
|
3176
|
+
}
|
|
3177
|
+
/**
|
|
3178
|
+
* Retrieves details about the workflow
|
|
3179
|
+
* @param requestContext - Optional request context to pass as query parameter
|
|
3180
|
+
* @returns Promise containing workflow details including steps and graphs
|
|
3181
|
+
*/
|
|
3182
|
+
details(requestContext) {
|
|
3183
|
+
return this.request(`/api/workflows/${this.workflowId}${requestContextQueryString(requestContext)}`);
|
|
3184
|
+
}
|
|
3185
|
+
/**
|
|
3186
|
+
* Retrieves all runs for a workflow
|
|
3187
|
+
* @param params - Parameters for filtering runs
|
|
3188
|
+
* @param requestContext - Optional request context to pass as query parameter
|
|
3189
|
+
* @returns Promise containing workflow runs array
|
|
3190
|
+
*/
|
|
3191
|
+
runs(params, requestContext) {
|
|
3192
|
+
const requestContextParam = base64RequestContext(parseClientRequestContext(requestContext));
|
|
3193
|
+
const searchParams = new URLSearchParams();
|
|
3194
|
+
if (params?.fromDate) {
|
|
3195
|
+
searchParams.set("fromDate", params.fromDate.toISOString());
|
|
3196
|
+
}
|
|
3197
|
+
if (params?.toDate) {
|
|
3198
|
+
searchParams.set("toDate", params.toDate.toISOString());
|
|
3199
|
+
}
|
|
3200
|
+
if (params?.page !== void 0) {
|
|
3201
|
+
searchParams.set("page", String(params.page));
|
|
3202
|
+
}
|
|
3203
|
+
if (params?.perPage !== void 0) {
|
|
3204
|
+
searchParams.set("perPage", String(params.perPage));
|
|
3205
|
+
}
|
|
3206
|
+
if (params?.limit !== null && params?.limit !== void 0) {
|
|
3207
|
+
if (params.limit === false) {
|
|
3208
|
+
searchParams.set("limit", "false");
|
|
3209
|
+
} else if (typeof params.limit === "number" && params.limit > 0 && Number.isInteger(params.limit)) {
|
|
3210
|
+
searchParams.set("limit", String(params.limit));
|
|
3211
|
+
}
|
|
3212
|
+
}
|
|
3213
|
+
if (params?.offset !== null && params?.offset !== void 0 && !isNaN(Number(params?.offset))) {
|
|
3214
|
+
searchParams.set("offset", String(params.offset));
|
|
3215
|
+
}
|
|
3216
|
+
if (params?.resourceId) {
|
|
3217
|
+
searchParams.set("resourceId", params.resourceId);
|
|
3218
|
+
}
|
|
3219
|
+
if (params?.status) {
|
|
3220
|
+
searchParams.set("status", params.status);
|
|
3221
|
+
}
|
|
3222
|
+
if (requestContextParam) {
|
|
3223
|
+
searchParams.set("requestContext", requestContextParam);
|
|
3224
|
+
}
|
|
3225
|
+
if (searchParams.size) {
|
|
3226
|
+
return this.request(`/api/workflows/${this.workflowId}/runs?${searchParams}`);
|
|
3227
|
+
} else {
|
|
3228
|
+
return this.request(`/api/workflows/${this.workflowId}/runs`);
|
|
3229
|
+
}
|
|
3230
|
+
}
|
|
3231
|
+
/**
|
|
3232
|
+
* Retrieves a specific workflow run by its ID
|
|
3233
|
+
* @param runId - The ID of the workflow run to retrieve
|
|
3234
|
+
* @param options - Optional configuration
|
|
3235
|
+
* @param options.requestContext - Optional request context to pass as query parameter
|
|
3236
|
+
* @param options.fields - Optional array of fields to return (e.g., ['result', 'steps']). Available fields: result, error, payload, steps, activeStepsPath, serializedStepGraph. Metadata fields (runId, workflowName, resourceId, createdAt, updatedAt) and status are always included.
|
|
3237
|
+
* @param options.withNestedWorkflows - Whether to include nested workflow data in steps. Defaults to true. Set to false for better performance when you don't need nested workflow details.
|
|
3238
|
+
* @returns Promise containing the workflow run details with metadata and processed execution state
|
|
3239
|
+
*/
|
|
3240
|
+
runById(runId, options) {
|
|
3241
|
+
const searchParams = new URLSearchParams();
|
|
3242
|
+
if (options?.fields && options.fields.length > 0) {
|
|
3243
|
+
searchParams.set("fields", options.fields.join(","));
|
|
3244
|
+
}
|
|
3245
|
+
if (options?.withNestedWorkflows !== void 0) {
|
|
3246
|
+
searchParams.set("withNestedWorkflows", String(options.withNestedWorkflows));
|
|
3247
|
+
}
|
|
3248
|
+
const requestContextParam = base64RequestContext(parseClientRequestContext(options?.requestContext));
|
|
3249
|
+
if (requestContextParam) {
|
|
3250
|
+
searchParams.set("requestContext", requestContextParam);
|
|
3251
|
+
}
|
|
3252
|
+
const queryString = searchParams.size > 0 ? `?${searchParams.toString()}` : "";
|
|
3253
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}${queryString}`);
|
|
3254
|
+
}
|
|
3255
|
+
/**
|
|
3256
|
+
* Deletes a specific workflow run by its ID
|
|
3257
|
+
* @param runId - The ID of the workflow run to delete
|
|
3258
|
+
* @returns Promise containing a success message
|
|
3259
|
+
*/
|
|
3260
|
+
deleteRunById(runId) {
|
|
3261
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}`, {
|
|
3262
|
+
method: "DELETE"
|
|
3263
|
+
});
|
|
3264
|
+
}
|
|
3265
|
+
/**
|
|
3266
|
+
* Creates a new workflow run
|
|
3267
|
+
* @param params - Optional object containing the optional runId
|
|
3268
|
+
* @returns Promise containing the Run instance
|
|
3269
|
+
*/
|
|
3270
|
+
async createRun(params) {
|
|
3271
|
+
const searchParams = new URLSearchParams();
|
|
3272
|
+
if (!!params?.runId) {
|
|
3273
|
+
searchParams.set("runId", params.runId);
|
|
3274
|
+
}
|
|
3275
|
+
const res = await this.request(
|
|
3276
|
+
`/api/workflows/${this.workflowId}/create-run?${searchParams.toString()}`,
|
|
3277
|
+
{
|
|
3278
|
+
method: "POST",
|
|
3279
|
+
body: {
|
|
3280
|
+
resourceId: params?.resourceId,
|
|
3281
|
+
disableScorers: params?.disableScorers
|
|
2085
3282
|
}
|
|
2086
3283
|
}
|
|
2087
|
-
|
|
2088
|
-
|
|
3284
|
+
);
|
|
3285
|
+
const run = new Run(this.options, this.workflowId, res.runId);
|
|
3286
|
+
return run;
|
|
2089
3287
|
}
|
|
2090
3288
|
/**
|
|
2091
3289
|
* Creates a new ReadableStream from an iterable or async iterable of objects,
|
|
@@ -2100,7 +3298,7 @@ var Workflow = class extends BaseResource {
|
|
|
2100
3298
|
async start(controller) {
|
|
2101
3299
|
try {
|
|
2102
3300
|
for await (const record of records) {
|
|
2103
|
-
const json = JSON.stringify(record) +
|
|
3301
|
+
const json = JSON.stringify(record) + RECORD_SEPARATOR2;
|
|
2104
3302
|
controller.enqueue(encoder.encode(json));
|
|
2105
3303
|
}
|
|
2106
3304
|
controller.close();
|
|
@@ -2110,142 +3308,6 @@ var Workflow = class extends BaseResource {
|
|
|
2110
3308
|
}
|
|
2111
3309
|
});
|
|
2112
3310
|
}
|
|
2113
|
-
/**
|
|
2114
|
-
* Restarts an active workflow run synchronously without waiting for the workflow to complete
|
|
2115
|
-
* @param params - Object containing the runId and requestContext
|
|
2116
|
-
* @returns Promise containing success message
|
|
2117
|
-
*/
|
|
2118
|
-
restart(params) {
|
|
2119
|
-
const requestContext = parseClientRequestContext(params.requestContext);
|
|
2120
|
-
return this.request(`/api/workflows/${this.workflowId}/restart?runId=${params.runId}`, {
|
|
2121
|
-
method: "POST",
|
|
2122
|
-
body: {
|
|
2123
|
-
requestContext,
|
|
2124
|
-
tracingOptions: params.tracingOptions
|
|
2125
|
-
}
|
|
2126
|
-
});
|
|
2127
|
-
}
|
|
2128
|
-
/**
|
|
2129
|
-
* Restarts an active workflow run asynchronously
|
|
2130
|
-
* @param params - Object containing the runId and requestContext
|
|
2131
|
-
* @returns Promise containing the workflow restart results
|
|
2132
|
-
*/
|
|
2133
|
-
restartAsync(params) {
|
|
2134
|
-
const requestContext = parseClientRequestContext(params.requestContext);
|
|
2135
|
-
return this.request(`/api/workflows/${this.workflowId}/restart-async?runId=${params.runId}`, {
|
|
2136
|
-
method: "POST",
|
|
2137
|
-
body: {
|
|
2138
|
-
requestContext,
|
|
2139
|
-
tracingOptions: params.tracingOptions
|
|
2140
|
-
}
|
|
2141
|
-
});
|
|
2142
|
-
}
|
|
2143
|
-
/**
|
|
2144
|
-
* Restart all active workflow runs synchronously without waiting for the workflow to complete
|
|
2145
|
-
* @returns Promise containing success message
|
|
2146
|
-
*/
|
|
2147
|
-
restartAllActiveWorkflowRuns() {
|
|
2148
|
-
return this.request(`/api/workflows/${this.workflowId}/restart-all-active-workflow-runs`, {
|
|
2149
|
-
method: "POST"
|
|
2150
|
-
});
|
|
2151
|
-
}
|
|
2152
|
-
/**
|
|
2153
|
-
* Restart all active workflow runs asynchronously
|
|
2154
|
-
* @returns Promise containing success message
|
|
2155
|
-
*/
|
|
2156
|
-
restartAllActiveWorkflowRunsAsync() {
|
|
2157
|
-
return this.request(`/api/workflows/${this.workflowId}/restart-all-active-workflow-runs-async`, {
|
|
2158
|
-
method: "POST"
|
|
2159
|
-
});
|
|
2160
|
-
}
|
|
2161
|
-
/**
|
|
2162
|
-
* Time travels a workflow run synchronously without waiting for the workflow to complete
|
|
2163
|
-
* @param params - Object containing the runId, step, inputData, resumeData, initialState, context, nestedStepsContext, requestContext and tracingOptions
|
|
2164
|
-
* @returns Promise containing success message
|
|
2165
|
-
*/
|
|
2166
|
-
timeTravel({
|
|
2167
|
-
runId,
|
|
2168
|
-
requestContext: paramsRequestContext,
|
|
2169
|
-
...params
|
|
2170
|
-
}) {
|
|
2171
|
-
const requestContext = parseClientRequestContext(paramsRequestContext);
|
|
2172
|
-
return this.request(`/api/workflows/${this.workflowId}/time-travel?runId=${runId}`, {
|
|
2173
|
-
method: "POST",
|
|
2174
|
-
body: {
|
|
2175
|
-
...params,
|
|
2176
|
-
requestContext
|
|
2177
|
-
}
|
|
2178
|
-
});
|
|
2179
|
-
}
|
|
2180
|
-
/**
|
|
2181
|
-
* Time travels a workflow run asynchronously
|
|
2182
|
-
* @param params - Object containing the runId, step, inputData, resumeData, initialState, context, nestedStepsContext, requestContext and tracingOptions
|
|
2183
|
-
* @returns Promise containing the workflow time travel results
|
|
2184
|
-
*/
|
|
2185
|
-
timeTravelAsync({
|
|
2186
|
-
runId,
|
|
2187
|
-
requestContext: paramsRequestContext,
|
|
2188
|
-
...params
|
|
2189
|
-
}) {
|
|
2190
|
-
const requestContext = parseClientRequestContext(paramsRequestContext);
|
|
2191
|
-
return this.request(`/api/workflows/${this.workflowId}/time-travel-async?runId=${runId}`, {
|
|
2192
|
-
method: "POST",
|
|
2193
|
-
body: {
|
|
2194
|
-
...params,
|
|
2195
|
-
requestContext
|
|
2196
|
-
}
|
|
2197
|
-
});
|
|
2198
|
-
}
|
|
2199
|
-
/**
|
|
2200
|
-
* Time travels a workflow run and returns a stream
|
|
2201
|
-
* @param params - Object containing the runId, step, inputData, resumeData, initialState, context, nestedStepsContext, requestContext and tracingOptions
|
|
2202
|
-
* @returns Promise containing the workflow execution results
|
|
2203
|
-
*/
|
|
2204
|
-
async timeTravelStream({ runId, requestContext: paramsRequestContext, ...params }) {
|
|
2205
|
-
const requestContext = parseClientRequestContext(paramsRequestContext);
|
|
2206
|
-
const response = await this.request(
|
|
2207
|
-
`/api/workflows/${this.workflowId}/time-travel-stream?runId=${runId}`,
|
|
2208
|
-
{
|
|
2209
|
-
method: "POST",
|
|
2210
|
-
body: {
|
|
2211
|
-
...params,
|
|
2212
|
-
requestContext
|
|
2213
|
-
},
|
|
2214
|
-
stream: true
|
|
2215
|
-
}
|
|
2216
|
-
);
|
|
2217
|
-
if (!response.ok) {
|
|
2218
|
-
throw new Error(`Failed to time travel workflow: ${response.statusText}`);
|
|
2219
|
-
}
|
|
2220
|
-
if (!response.body) {
|
|
2221
|
-
throw new Error("Response body is null");
|
|
2222
|
-
}
|
|
2223
|
-
let failedChunk = void 0;
|
|
2224
|
-
const transformStream = new TransformStream({
|
|
2225
|
-
start() {
|
|
2226
|
-
},
|
|
2227
|
-
async transform(chunk, controller) {
|
|
2228
|
-
try {
|
|
2229
|
-
const decoded = new TextDecoder().decode(chunk);
|
|
2230
|
-
const chunks = decoded.split(RECORD_SEPARATOR);
|
|
2231
|
-
for (const chunk2 of chunks) {
|
|
2232
|
-
if (chunk2) {
|
|
2233
|
-
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
2234
|
-
try {
|
|
2235
|
-
const parsedChunk = JSON.parse(newChunk);
|
|
2236
|
-
controller.enqueue(parsedChunk);
|
|
2237
|
-
failedChunk = void 0;
|
|
2238
|
-
} catch {
|
|
2239
|
-
failedChunk = newChunk;
|
|
2240
|
-
}
|
|
2241
|
-
}
|
|
2242
|
-
}
|
|
2243
|
-
} catch {
|
|
2244
|
-
}
|
|
2245
|
-
}
|
|
2246
|
-
});
|
|
2247
|
-
return response.body.pipeThrough(transformStream);
|
|
2248
|
-
}
|
|
2249
3311
|
};
|
|
2250
3312
|
|
|
2251
3313
|
// src/resources/a2a.ts
|
|
@@ -2360,7 +3422,7 @@ var MCPTool = class extends BaseResource {
|
|
|
2360
3422
|
};
|
|
2361
3423
|
|
|
2362
3424
|
// src/resources/agent-builder.ts
|
|
2363
|
-
var
|
|
3425
|
+
var RECORD_SEPARATOR3 = "";
|
|
2364
3426
|
var AgentBuilder = class extends BaseResource {
|
|
2365
3427
|
constructor(options, actionId) {
|
|
2366
3428
|
super(options);
|
|
@@ -2406,7 +3468,7 @@ var AgentBuilder = class extends BaseResource {
|
|
|
2406
3468
|
async transform(chunk, controller) {
|
|
2407
3469
|
try {
|
|
2408
3470
|
const decoded = new TextDecoder().decode(chunk);
|
|
2409
|
-
const chunks = decoded.split(
|
|
3471
|
+
const chunks = decoded.split(RECORD_SEPARATOR3);
|
|
2410
3472
|
for (const chunk2 of chunks) {
|
|
2411
3473
|
if (chunk2) {
|
|
2412
3474
|
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
@@ -2520,7 +3582,7 @@ var AgentBuilder = class extends BaseResource {
|
|
|
2520
3582
|
if (done && !value) continue;
|
|
2521
3583
|
try {
|
|
2522
3584
|
const decoded = value ? new TextDecoder().decode(value) : "";
|
|
2523
|
-
const chunks = (buffer + decoded).split(
|
|
3585
|
+
const chunks = (buffer + decoded).split(RECORD_SEPARATOR3);
|
|
2524
3586
|
buffer = chunks.pop() || "";
|
|
2525
3587
|
for (const chunk of chunks) {
|
|
2526
3588
|
if (chunk) {
|
|
@@ -2572,36 +3634,11 @@ var AgentBuilder = class extends BaseResource {
|
|
|
2572
3634
|
}
|
|
2573
3635
|
return response.body.pipeThrough(this.createRecordParserTransform());
|
|
2574
3636
|
}
|
|
2575
|
-
/**
|
|
2576
|
-
* Streams agent builder action progress in real-time using VNext streaming.
|
|
2577
|
-
* This calls `/api/agent-builder/:actionId/streamVNext`.
|
|
2578
|
-
*/
|
|
2579
|
-
async streamVNext(params, runId) {
|
|
2580
|
-
const searchParams = new URLSearchParams();
|
|
2581
|
-
if (runId) {
|
|
2582
|
-
searchParams.set("runId", runId);
|
|
2583
|
-
}
|
|
2584
|
-
const requestContext = parseClientRequestContext(params.requestContext);
|
|
2585
|
-
const { requestContext: _, ...actionParams } = params;
|
|
2586
|
-
const url = `/api/agent-builder/${this.actionId}/streamVNext${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2587
|
-
const response = await this.request(url, {
|
|
2588
|
-
method: "POST",
|
|
2589
|
-
body: { ...actionParams, requestContext },
|
|
2590
|
-
stream: true
|
|
2591
|
-
});
|
|
2592
|
-
if (!response.ok) {
|
|
2593
|
-
throw new Error(`Failed to stream agent builder action VNext: ${response.statusText}`);
|
|
2594
|
-
}
|
|
2595
|
-
if (!response.body) {
|
|
2596
|
-
throw new Error("Response body is null");
|
|
2597
|
-
}
|
|
2598
|
-
return response.body.pipeThrough(this.createRecordParserTransform());
|
|
2599
|
-
}
|
|
2600
3637
|
/**
|
|
2601
3638
|
* Observes an existing agent builder action run stream.
|
|
2602
3639
|
* Replays cached execution from the beginning, then continues with live stream.
|
|
2603
3640
|
* This is the recommended method for recovery after page refresh/hot reload.
|
|
2604
|
-
* This calls `/api/agent-builder/:actionId/observe`
|
|
3641
|
+
* This calls `/api/agent-builder/:actionId/observe`
|
|
2605
3642
|
*/
|
|
2606
3643
|
async observeStream(params) {
|
|
2607
3644
|
const searchParams = new URLSearchParams();
|
|
@@ -2619,27 +3656,6 @@ var AgentBuilder = class extends BaseResource {
|
|
|
2619
3656
|
}
|
|
2620
3657
|
return response.body.pipeThrough(this.createRecordParserTransform());
|
|
2621
3658
|
}
|
|
2622
|
-
/**
|
|
2623
|
-
* Observes an existing agent builder action run stream using VNext streaming API.
|
|
2624
|
-
* Replays cached execution from the beginning, then continues with live stream.
|
|
2625
|
-
* This calls `/api/agent-builder/:actionId/observe-streamVNext`.
|
|
2626
|
-
*/
|
|
2627
|
-
async observeStreamVNext(params) {
|
|
2628
|
-
const searchParams = new URLSearchParams();
|
|
2629
|
-
searchParams.set("runId", params.runId);
|
|
2630
|
-
const url = `/api/agent-builder/${this.actionId}/observe-streamVNext?${searchParams.toString()}`;
|
|
2631
|
-
const response = await this.request(url, {
|
|
2632
|
-
method: "POST",
|
|
2633
|
-
stream: true
|
|
2634
|
-
});
|
|
2635
|
-
if (!response.ok) {
|
|
2636
|
-
throw new Error(`Failed to observe agent builder action stream VNext: ${response.statusText}`);
|
|
2637
|
-
}
|
|
2638
|
-
if (!response.body) {
|
|
2639
|
-
throw new Error("Response body is null");
|
|
2640
|
-
}
|
|
2641
|
-
return response.body.pipeThrough(this.createRecordParserTransform());
|
|
2642
|
-
}
|
|
2643
3659
|
/**
|
|
2644
3660
|
* Observes an existing agent builder action run stream using legacy streaming API.
|
|
2645
3661
|
* Replays cached execution from the beginning, then continues with live stream.
|
|
@@ -2687,9 +3703,22 @@ var AgentBuilder = class extends BaseResource {
|
|
|
2687
3703
|
/**
|
|
2688
3704
|
* Gets a specific action run by its ID.
|
|
2689
3705
|
* This calls `/api/agent-builder/:actionId/runs/:runId`.
|
|
3706
|
+
* @param runId - The ID of the action run to retrieve
|
|
3707
|
+
* @param options - Optional configuration
|
|
3708
|
+
* @param options.fields - Optional array of fields to return (e.g., ['result', 'steps']). Available fields: result, error, payload, steps, activeStepsPath, serializedStepGraph. Metadata fields (runId, workflowName, resourceId, createdAt, updatedAt) and status are always included.
|
|
3709
|
+
* @param options.withNestedWorkflows - Whether to include nested workflow data in steps. Defaults to true. Set to false for better performance when you don't need nested workflow details.
|
|
3710
|
+
* @returns Promise containing the action run details with metadata and processed execution state
|
|
2690
3711
|
*/
|
|
2691
|
-
async runById(runId) {
|
|
2692
|
-
const
|
|
3712
|
+
async runById(runId, options) {
|
|
3713
|
+
const searchParams = new URLSearchParams();
|
|
3714
|
+
if (options?.fields && options.fields.length > 0) {
|
|
3715
|
+
searchParams.set("fields", options.fields.join(","));
|
|
3716
|
+
}
|
|
3717
|
+
if (options?.withNestedWorkflows !== void 0) {
|
|
3718
|
+
searchParams.set("withNestedWorkflows", String(options.withNestedWorkflows));
|
|
3719
|
+
}
|
|
3720
|
+
const queryString = searchParams.size > 0 ? `?${searchParams.toString()}` : "";
|
|
3721
|
+
const url = `/api/agent-builder/${this.actionId}/runs/${runId}${queryString}`;
|
|
2693
3722
|
return this.request(url, {
|
|
2694
3723
|
method: "GET"
|
|
2695
3724
|
});
|
|
@@ -2738,16 +3767,6 @@ var AgentBuilder = class extends BaseResource {
|
|
|
2738
3767
|
method: "GET"
|
|
2739
3768
|
});
|
|
2740
3769
|
}
|
|
2741
|
-
/**
|
|
2742
|
-
* Gets the execution result of an agent builder action run.
|
|
2743
|
-
* This calls `/api/agent-builder/:actionId/runs/:runId/execution-result`.
|
|
2744
|
-
*/
|
|
2745
|
-
async runExecutionResult(runId) {
|
|
2746
|
-
const url = `/api/agent-builder/${this.actionId}/runs/${runId}/execution-result`;
|
|
2747
|
-
return this.request(url, {
|
|
2748
|
-
method: "GET"
|
|
2749
|
-
});
|
|
2750
|
-
}
|
|
2751
3770
|
/**
|
|
2752
3771
|
* Cancels an agent builder action run.
|
|
2753
3772
|
* This calls `/api/agent-builder/:actionId/runs/:runId/cancel`.
|
|
@@ -2774,14 +3793,17 @@ var Observability = class extends BaseResource {
|
|
|
2774
3793
|
return this.request(`/api/observability/traces/${traceId}`);
|
|
2775
3794
|
}
|
|
2776
3795
|
/**
|
|
2777
|
-
* Retrieves paginated list of traces with optional filtering
|
|
2778
|
-
*
|
|
3796
|
+
* Retrieves paginated list of traces with optional filtering.
|
|
3797
|
+
* This is the legacy API preserved for backward compatibility.
|
|
3798
|
+
*
|
|
3799
|
+
* @param params - Parameters for pagination and filtering (legacy format)
|
|
2779
3800
|
* @returns Promise containing paginated traces and pagination info
|
|
3801
|
+
* @deprecated Use {@link listTraces} instead for new features like ordering and more filters.
|
|
2780
3802
|
*/
|
|
2781
3803
|
getTraces(params) {
|
|
2782
3804
|
const { pagination, filters } = params;
|
|
2783
3805
|
const { page, perPage, dateRange } = pagination || {};
|
|
2784
|
-
const { name, spanType, entityId, entityType } = filters || {};
|
|
3806
|
+
const { name: name14, spanType, entityId, entityType } = filters || {};
|
|
2785
3807
|
const searchParams = new URLSearchParams();
|
|
2786
3808
|
if (page !== void 0) {
|
|
2787
3809
|
searchParams.set("page", String(page));
|
|
@@ -2789,8 +3811,8 @@ var Observability = class extends BaseResource {
|
|
|
2789
3811
|
if (perPage !== void 0) {
|
|
2790
3812
|
searchParams.set("perPage", String(perPage));
|
|
2791
3813
|
}
|
|
2792
|
-
if (
|
|
2793
|
-
searchParams.set("name",
|
|
3814
|
+
if (name14) {
|
|
3815
|
+
searchParams.set("name", name14);
|
|
2794
3816
|
}
|
|
2795
3817
|
if (spanType !== void 0) {
|
|
2796
3818
|
searchParams.set("spanType", String(spanType));
|
|
@@ -2809,25 +3831,34 @@ var Observability = class extends BaseResource {
|
|
|
2809
3831
|
const queryString = searchParams.toString();
|
|
2810
3832
|
return this.request(`/api/observability/traces${queryString ? `?${queryString}` : ""}`);
|
|
2811
3833
|
}
|
|
3834
|
+
/**
|
|
3835
|
+
* Retrieves paginated list of traces with optional filtering and sorting.
|
|
3836
|
+
* This is the new API with improved filtering options.
|
|
3837
|
+
*
|
|
3838
|
+
* @param params - Parameters for pagination, filtering, and ordering
|
|
3839
|
+
* @returns Promise containing paginated traces and pagination info
|
|
3840
|
+
*/
|
|
3841
|
+
listTraces(params = {}) {
|
|
3842
|
+
const queryString = toQueryParams(params, ["filters", "pagination", "orderBy"]);
|
|
3843
|
+
return this.request(`/api/observability/traces${queryString ? `?${queryString}` : ""}`);
|
|
3844
|
+
}
|
|
2812
3845
|
/**
|
|
2813
3846
|
* Retrieves scores by trace ID and span ID
|
|
2814
3847
|
* @param params - Parameters containing trace ID, span ID, and pagination options
|
|
2815
3848
|
* @returns Promise containing scores and pagination info
|
|
2816
3849
|
*/
|
|
2817
3850
|
listScoresBySpan(params) {
|
|
2818
|
-
const { traceId, spanId,
|
|
2819
|
-
const
|
|
2820
|
-
if (page !== void 0) {
|
|
2821
|
-
searchParams.set("page", String(page));
|
|
2822
|
-
}
|
|
2823
|
-
if (perPage !== void 0) {
|
|
2824
|
-
searchParams.set("perPage", String(perPage));
|
|
2825
|
-
}
|
|
2826
|
-
const queryString = searchParams.toString();
|
|
3851
|
+
const { traceId, spanId, ...pagination } = params;
|
|
3852
|
+
const queryString = toQueryParams(pagination);
|
|
2827
3853
|
return this.request(
|
|
2828
3854
|
`/api/observability/traces/${encodeURIComponent(traceId)}/${encodeURIComponent(spanId)}/scores${queryString ? `?${queryString}` : ""}`
|
|
2829
3855
|
);
|
|
2830
3856
|
}
|
|
3857
|
+
/**
|
|
3858
|
+
* Scores one or more traces using a specified scorer.
|
|
3859
|
+
* @param params - Scorer name and targets to score
|
|
3860
|
+
* @returns Promise containing the scoring status
|
|
3861
|
+
*/
|
|
2831
3862
|
score(params) {
|
|
2832
3863
|
return this.request(`/api/observability/traces/score`, {
|
|
2833
3864
|
method: "POST",
|
|
@@ -2836,6 +3867,41 @@ var Observability = class extends BaseResource {
|
|
|
2836
3867
|
}
|
|
2837
3868
|
};
|
|
2838
3869
|
|
|
3870
|
+
// src/resources/stored-agent.ts
|
|
3871
|
+
var StoredAgent = class extends BaseResource {
|
|
3872
|
+
constructor(options, storedAgentId) {
|
|
3873
|
+
super(options);
|
|
3874
|
+
this.storedAgentId = storedAgentId;
|
|
3875
|
+
}
|
|
3876
|
+
/**
|
|
3877
|
+
* Retrieves details about the stored agent
|
|
3878
|
+
* @returns Promise containing stored agent details
|
|
3879
|
+
*/
|
|
3880
|
+
details() {
|
|
3881
|
+
return this.request(`/api/stored/agents/${encodeURIComponent(this.storedAgentId)}`);
|
|
3882
|
+
}
|
|
3883
|
+
/**
|
|
3884
|
+
* Updates the stored agent with the provided fields
|
|
3885
|
+
* @param params - Fields to update
|
|
3886
|
+
* @returns Promise containing the updated stored agent
|
|
3887
|
+
*/
|
|
3888
|
+
update(params) {
|
|
3889
|
+
return this.request(`/api/stored/agents/${encodeURIComponent(this.storedAgentId)}`, {
|
|
3890
|
+
method: "PATCH",
|
|
3891
|
+
body: params
|
|
3892
|
+
});
|
|
3893
|
+
}
|
|
3894
|
+
/**
|
|
3895
|
+
* Deletes the stored agent
|
|
3896
|
+
* @returns Promise containing deletion confirmation
|
|
3897
|
+
*/
|
|
3898
|
+
delete() {
|
|
3899
|
+
return this.request(`/api/stored/agents/${encodeURIComponent(this.storedAgentId)}`, {
|
|
3900
|
+
method: "DELETE"
|
|
3901
|
+
});
|
|
3902
|
+
}
|
|
3903
|
+
};
|
|
3904
|
+
|
|
2839
3905
|
// src/client.ts
|
|
2840
3906
|
var MastraClient = class extends BaseResource {
|
|
2841
3907
|
observability;
|
|
@@ -2872,22 +3938,26 @@ var MastraClient = class extends BaseResource {
|
|
|
2872
3938
|
return new Agent(this.options, agentId);
|
|
2873
3939
|
}
|
|
2874
3940
|
/**
|
|
2875
|
-
* Lists memory threads
|
|
2876
|
-
* @param params - Parameters containing
|
|
3941
|
+
* Lists memory threads with optional filtering by resourceId and/or metadata
|
|
3942
|
+
* @param params - Parameters containing optional filters, pagination options, and request context
|
|
2877
3943
|
* @returns Promise containing paginated array of memory threads with metadata
|
|
2878
3944
|
*/
|
|
2879
|
-
async listMemoryThreads(params) {
|
|
2880
|
-
const queryParams = new URLSearchParams(
|
|
2881
|
-
|
|
2882
|
-
|
|
2883
|
-
|
|
2884
|
-
|
|
2885
|
-
|
|
2886
|
-
|
|
2887
|
-
|
|
2888
|
-
|
|
3945
|
+
async listMemoryThreads(params = {}) {
|
|
3946
|
+
const queryParams = new URLSearchParams();
|
|
3947
|
+
if (params.resourceId) {
|
|
3948
|
+
queryParams.set("resourceId", params.resourceId);
|
|
3949
|
+
}
|
|
3950
|
+
if (params.metadata) {
|
|
3951
|
+
queryParams.set("metadata", JSON.stringify(params.metadata));
|
|
3952
|
+
}
|
|
3953
|
+
if (params.agentId) queryParams.set("agentId", params.agentId);
|
|
3954
|
+
if (params.page !== void 0) queryParams.set("page", params.page.toString());
|
|
3955
|
+
if (params.perPage !== void 0) queryParams.set("perPage", params.perPage.toString());
|
|
3956
|
+
if (params.orderBy) queryParams.set("orderBy", params.orderBy);
|
|
3957
|
+
if (params.sortDirection) queryParams.set("sortDirection", params.sortDirection);
|
|
3958
|
+
const queryString = queryParams.toString();
|
|
2889
3959
|
const response = await this.request(
|
|
2890
|
-
`/api/memory/threads
|
|
3960
|
+
`/api/memory/threads${queryString ? `?${queryString}` : ""}${requestContextQueryString(params.requestContext, queryString ? "&" : "?")}`
|
|
2891
3961
|
);
|
|
2892
3962
|
const actualResponse = "threads" in response ? response : {
|
|
2893
3963
|
threads: response,
|
|
@@ -2922,20 +3992,29 @@ var MastraClient = class extends BaseResource {
|
|
|
2922
3992
|
/**
|
|
2923
3993
|
* Gets a memory thread instance by ID
|
|
2924
3994
|
* @param threadId - ID of the memory thread to retrieve
|
|
3995
|
+
* @param agentId - Optional agent ID. When not provided, uses storage directly
|
|
2925
3996
|
* @returns MemoryThread instance
|
|
2926
3997
|
*/
|
|
2927
3998
|
getMemoryThread({ threadId, agentId }) {
|
|
2928
3999
|
return new MemoryThread(this.options, threadId, agentId);
|
|
2929
4000
|
}
|
|
4001
|
+
/**
|
|
4002
|
+
* Lists messages for a thread.
|
|
4003
|
+
* @param threadId - ID of the thread
|
|
4004
|
+
* @param opts - Optional parameters including agentId, networkId, and requestContext
|
|
4005
|
+
* - When agentId is provided, uses the agent's memory
|
|
4006
|
+
* - When networkId is provided, uses the network endpoint
|
|
4007
|
+
* - When neither is provided, uses storage directly
|
|
4008
|
+
* @returns Promise containing the thread messages
|
|
4009
|
+
*/
|
|
2930
4010
|
listThreadMessages(threadId, opts = {}) {
|
|
2931
|
-
if (!opts.agentId && !opts.networkId) {
|
|
2932
|
-
throw new Error("Either agentId or networkId must be provided");
|
|
2933
|
-
}
|
|
2934
4011
|
let url = "";
|
|
2935
|
-
if (opts.
|
|
2936
|
-
url = `/api/memory/threads/${threadId}/messages?agentId=${opts.agentId}${requestContextQueryString(opts.requestContext, "&")}`;
|
|
2937
|
-
} else if (opts.networkId) {
|
|
4012
|
+
if (opts.networkId) {
|
|
2938
4013
|
url = `/api/memory/network/threads/${threadId}/messages?networkId=${opts.networkId}${requestContextQueryString(opts.requestContext, "&")}`;
|
|
4014
|
+
} else if (opts.agentId) {
|
|
4015
|
+
url = `/api/memory/threads/${threadId}/messages?agentId=${opts.agentId}${requestContextQueryString(opts.requestContext, "&")}`;
|
|
4016
|
+
} else {
|
|
4017
|
+
url = `/api/memory/threads/${threadId}/messages${requestContextQueryString(opts.requestContext, "?")}`;
|
|
2939
4018
|
}
|
|
2940
4019
|
return this.request(url);
|
|
2941
4020
|
}
|
|
@@ -2993,6 +4072,28 @@ var MastraClient = class extends BaseResource {
|
|
|
2993
4072
|
getTool(toolId) {
|
|
2994
4073
|
return new Tool(this.options, toolId);
|
|
2995
4074
|
}
|
|
4075
|
+
/**
|
|
4076
|
+
* Retrieves all available processors
|
|
4077
|
+
* @param requestContext - Optional request context to pass as query parameter
|
|
4078
|
+
* @returns Promise containing map of processor IDs to processor details
|
|
4079
|
+
*/
|
|
4080
|
+
listProcessors(requestContext) {
|
|
4081
|
+
const requestContextParam = base64RequestContext(parseClientRequestContext(requestContext));
|
|
4082
|
+
const searchParams = new URLSearchParams();
|
|
4083
|
+
if (requestContextParam) {
|
|
4084
|
+
searchParams.set("requestContext", requestContextParam);
|
|
4085
|
+
}
|
|
4086
|
+
const queryString = searchParams.toString();
|
|
4087
|
+
return this.request(`/api/processors${queryString ? `?${queryString}` : ""}`);
|
|
4088
|
+
}
|
|
4089
|
+
/**
|
|
4090
|
+
* Gets a processor instance by ID
|
|
4091
|
+
* @param processorId - ID of the processor to retrieve
|
|
4092
|
+
* @returns Processor instance
|
|
4093
|
+
*/
|
|
4094
|
+
getProcessor(processorId) {
|
|
4095
|
+
return new Processor(this.options, processorId);
|
|
4096
|
+
}
|
|
2996
4097
|
/**
|
|
2997
4098
|
* Retrieves all available workflows
|
|
2998
4099
|
* @param requestContext - Optional request context to pass as query parameter
|
|
@@ -3342,15 +4443,89 @@ var MastraClient = class extends BaseResource {
|
|
|
3342
4443
|
getTrace(traceId) {
|
|
3343
4444
|
return this.observability.getTrace(traceId);
|
|
3344
4445
|
}
|
|
4446
|
+
/**
|
|
4447
|
+
* Retrieves paginated list of traces with optional filtering.
|
|
4448
|
+
* This is the legacy API preserved for backward compatibility.
|
|
4449
|
+
*
|
|
4450
|
+
* @param params - Parameters for pagination and filtering (legacy format)
|
|
4451
|
+
* @returns Promise containing paginated traces and pagination info
|
|
4452
|
+
* @deprecated Use {@link listTraces} instead for new features like ordering and more filters.
|
|
4453
|
+
*/
|
|
3345
4454
|
getTraces(params) {
|
|
3346
4455
|
return this.observability.getTraces(params);
|
|
3347
4456
|
}
|
|
4457
|
+
/**
|
|
4458
|
+
* Retrieves paginated list of traces with optional filtering and sorting.
|
|
4459
|
+
* This is the new API with improved filtering options.
|
|
4460
|
+
*
|
|
4461
|
+
* @param params - Parameters for pagination, filtering, and ordering
|
|
4462
|
+
* @returns Promise containing paginated traces and pagination info
|
|
4463
|
+
*/
|
|
4464
|
+
listTraces(params = {}) {
|
|
4465
|
+
return this.observability.listTraces(params);
|
|
4466
|
+
}
|
|
3348
4467
|
listScoresBySpan(params) {
|
|
3349
4468
|
return this.observability.listScoresBySpan(params);
|
|
3350
4469
|
}
|
|
3351
4470
|
score(params) {
|
|
3352
4471
|
return this.observability.score(params);
|
|
3353
4472
|
}
|
|
4473
|
+
// ============================================================================
|
|
4474
|
+
// Stored Agents
|
|
4475
|
+
// ============================================================================
|
|
4476
|
+
/**
|
|
4477
|
+
* Lists all stored agents with optional pagination
|
|
4478
|
+
* @param params - Optional pagination and ordering parameters
|
|
4479
|
+
* @returns Promise containing paginated list of stored agents
|
|
4480
|
+
*/
|
|
4481
|
+
listStoredAgents(params) {
|
|
4482
|
+
const searchParams = new URLSearchParams();
|
|
4483
|
+
if (params?.page !== void 0) {
|
|
4484
|
+
searchParams.set("page", String(params.page));
|
|
4485
|
+
}
|
|
4486
|
+
if (params?.perPage !== void 0) {
|
|
4487
|
+
searchParams.set("perPage", String(params.perPage));
|
|
4488
|
+
}
|
|
4489
|
+
if (params?.orderBy) {
|
|
4490
|
+
if (params.orderBy.field) {
|
|
4491
|
+
searchParams.set("orderBy[field]", params.orderBy.field);
|
|
4492
|
+
}
|
|
4493
|
+
if (params.orderBy.direction) {
|
|
4494
|
+
searchParams.set("orderBy[direction]", params.orderBy.direction);
|
|
4495
|
+
}
|
|
4496
|
+
}
|
|
4497
|
+
const queryString = searchParams.toString();
|
|
4498
|
+
return this.request(`/api/stored/agents${queryString ? `?${queryString}` : ""}`);
|
|
4499
|
+
}
|
|
4500
|
+
/**
|
|
4501
|
+
* Creates a new stored agent
|
|
4502
|
+
* @param params - Agent configuration including id, name, instructions, model, etc.
|
|
4503
|
+
* @returns Promise containing the created stored agent
|
|
4504
|
+
*/
|
|
4505
|
+
createStoredAgent(params) {
|
|
4506
|
+
return this.request("/api/stored/agents", {
|
|
4507
|
+
method: "POST",
|
|
4508
|
+
body: params
|
|
4509
|
+
});
|
|
4510
|
+
}
|
|
4511
|
+
/**
|
|
4512
|
+
* Gets a stored agent instance by ID for further operations (details, update, delete)
|
|
4513
|
+
* @param storedAgentId - ID of the stored agent to retrieve
|
|
4514
|
+
* @returns StoredAgent instance
|
|
4515
|
+
*/
|
|
4516
|
+
getStoredAgent(storedAgentId) {
|
|
4517
|
+
return new StoredAgent(this.options, storedAgentId);
|
|
4518
|
+
}
|
|
4519
|
+
// ============================================================================
|
|
4520
|
+
// System
|
|
4521
|
+
// ============================================================================
|
|
4522
|
+
/**
|
|
4523
|
+
* Retrieves installed Mastra packages and their versions
|
|
4524
|
+
* @returns Promise containing the list of installed Mastra packages
|
|
4525
|
+
*/
|
|
4526
|
+
getSystemPackages() {
|
|
4527
|
+
return this.request("/api/system/packages");
|
|
4528
|
+
}
|
|
3354
4529
|
};
|
|
3355
4530
|
|
|
3356
4531
|
// src/tools.ts
|