@mastra/client-js 0.0.0-revert-schema-20250416221206 → 0.0.0-scorer-agentnames-conditional-20250926065249
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 +2195 -2
- package/LICENSE.md +11 -42
- package/README.md +8 -9
- package/dist/client.d.ts +289 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/example.d.ts +2 -0
- package/dist/example.d.ts.map +1 -0
- package/dist/index.cjs +2769 -183
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +5 -585
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2765 -185
- package/dist/index.js.map +1 -0
- package/dist/resources/a2a.d.ts +41 -0
- package/dist/resources/a2a.d.ts.map +1 -0
- package/dist/resources/agent-builder.d.ts +160 -0
- package/dist/resources/agent-builder.d.ts.map +1 -0
- package/dist/resources/agent.d.ts +191 -0
- package/dist/resources/agent.d.ts.map +1 -0
- package/dist/resources/base.d.ts +13 -0
- package/dist/resources/base.d.ts.map +1 -0
- package/dist/resources/index.d.ts +11 -0
- package/dist/resources/index.d.ts.map +1 -0
- package/dist/resources/mcp-tool.d.ts +28 -0
- package/dist/resources/mcp-tool.d.ts.map +1 -0
- package/dist/resources/memory-thread.d.ts +53 -0
- package/dist/resources/memory-thread.d.ts.map +1 -0
- package/dist/resources/network-memory-thread.d.ts +47 -0
- package/dist/resources/network-memory-thread.d.ts.map +1 -0
- package/dist/resources/observability.d.ts +29 -0
- package/dist/resources/observability.d.ts.map +1 -0
- package/dist/resources/tool.d.ts +24 -0
- package/dist/resources/tool.d.ts.map +1 -0
- package/dist/resources/vNextNetwork.d.ts +43 -0
- package/dist/resources/vNextNetwork.d.ts.map +1 -0
- package/dist/resources/vector.d.ts +51 -0
- package/dist/resources/vector.d.ts.map +1 -0
- package/dist/resources/workflow.d.ts +254 -0
- package/dist/resources/workflow.d.ts.map +1 -0
- package/dist/tools.d.ts +22 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/types.d.ts +470 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/utils/index.d.ts +5 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/process-client-tools.d.ts +3 -0
- package/dist/utils/process-client-tools.d.ts.map +1 -0
- package/dist/utils/process-mastra-stream.d.ts +11 -0
- package/dist/utils/process-mastra-stream.d.ts.map +1 -0
- package/dist/utils/zod-to-json-schema.d.ts +3 -0
- package/dist/utils/zod-to-json-schema.d.ts.map +1 -0
- package/package.json +40 -18
- package/dist/index.d.cts +0 -585
- package/eslint.config.js +0 -6
- package/src/client.ts +0 -223
- package/src/example.ts +0 -65
- package/src/index.test.ts +0 -710
- package/src/index.ts +0 -2
- package/src/resources/agent.ts +0 -205
- package/src/resources/base.ts +0 -70
- package/src/resources/index.ts +0 -7
- package/src/resources/memory-thread.ts +0 -60
- package/src/resources/network.ts +0 -92
- package/src/resources/tool.ts +0 -32
- package/src/resources/vector.ts +0 -83
- package/src/resources/workflow.ts +0 -215
- package/src/types.ts +0 -224
- package/tsconfig.json +0 -5
- package/vitest.config.js +0 -8
package/dist/index.cjs
CHANGED
|
@@ -1,10 +1,136 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var zod = require('zod');
|
|
4
|
-
var zodToJsonSchema = require('zod-to-json-schema');
|
|
5
3
|
var uiUtils = require('@ai-sdk/ui-utils');
|
|
4
|
+
var uuid = require('@lukeed/uuid');
|
|
5
|
+
var runtimeContext = require('@mastra/core/runtime-context');
|
|
6
|
+
var isVercelTool = require('@mastra/core/tools/is-vercel-tool');
|
|
7
|
+
var zod = require('zod');
|
|
8
|
+
var originalZodToJsonSchema = require('zod-to-json-schema');
|
|
9
|
+
|
|
10
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
11
|
+
|
|
12
|
+
var originalZodToJsonSchema__default = /*#__PURE__*/_interopDefault(originalZodToJsonSchema);
|
|
6
13
|
|
|
7
14
|
// src/resources/agent.ts
|
|
15
|
+
function parseClientRuntimeContext(runtimeContext$1) {
|
|
16
|
+
if (runtimeContext$1) {
|
|
17
|
+
if (runtimeContext$1 instanceof runtimeContext.RuntimeContext) {
|
|
18
|
+
return Object.fromEntries(runtimeContext$1.entries());
|
|
19
|
+
}
|
|
20
|
+
return runtimeContext$1;
|
|
21
|
+
}
|
|
22
|
+
return void 0;
|
|
23
|
+
}
|
|
24
|
+
function base64RuntimeContext(runtimeContext) {
|
|
25
|
+
if (runtimeContext) {
|
|
26
|
+
return btoa(JSON.stringify(runtimeContext));
|
|
27
|
+
}
|
|
28
|
+
return void 0;
|
|
29
|
+
}
|
|
30
|
+
function runtimeContextQueryString(runtimeContext) {
|
|
31
|
+
const runtimeContextParam = base64RuntimeContext(parseClientRuntimeContext(runtimeContext));
|
|
32
|
+
if (!runtimeContextParam) return "";
|
|
33
|
+
const searchParams = new URLSearchParams();
|
|
34
|
+
searchParams.set("runtimeContext", runtimeContextParam);
|
|
35
|
+
const queryString = searchParams.toString();
|
|
36
|
+
return queryString ? `?${queryString}` : "";
|
|
37
|
+
}
|
|
38
|
+
function isZodType(value) {
|
|
39
|
+
return typeof value === "object" && value !== null && "_def" in value && "parse" in value && typeof value.parse === "function" && "safeParse" in value && typeof value.safeParse === "function";
|
|
40
|
+
}
|
|
41
|
+
function zodToJsonSchema(zodSchema) {
|
|
42
|
+
if (!isZodType(zodSchema)) {
|
|
43
|
+
return zodSchema;
|
|
44
|
+
}
|
|
45
|
+
if ("toJSONSchema" in zod.z) {
|
|
46
|
+
const fn = "toJSONSchema";
|
|
47
|
+
return zod.z[fn].call(zod.z, zodSchema);
|
|
48
|
+
}
|
|
49
|
+
return originalZodToJsonSchema__default.default(zodSchema, { $refStrategy: "relative" });
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// src/utils/process-client-tools.ts
|
|
53
|
+
function processClientTools(clientTools) {
|
|
54
|
+
if (!clientTools) {
|
|
55
|
+
return void 0;
|
|
56
|
+
}
|
|
57
|
+
return Object.fromEntries(
|
|
58
|
+
Object.entries(clientTools).map(([key, value]) => {
|
|
59
|
+
if (isVercelTool.isVercelTool(value)) {
|
|
60
|
+
return [
|
|
61
|
+
key,
|
|
62
|
+
{
|
|
63
|
+
...value,
|
|
64
|
+
parameters: value.parameters ? zodToJsonSchema(value.parameters) : void 0
|
|
65
|
+
}
|
|
66
|
+
];
|
|
67
|
+
} else {
|
|
68
|
+
return [
|
|
69
|
+
key,
|
|
70
|
+
{
|
|
71
|
+
...value,
|
|
72
|
+
inputSchema: value.inputSchema ? zodToJsonSchema(value.inputSchema) : void 0,
|
|
73
|
+
outputSchema: value.outputSchema ? zodToJsonSchema(value.outputSchema) : void 0
|
|
74
|
+
}
|
|
75
|
+
];
|
|
76
|
+
}
|
|
77
|
+
})
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// src/utils/process-mastra-stream.ts
|
|
82
|
+
async function sharedProcessMastraStream({
|
|
83
|
+
stream,
|
|
84
|
+
onChunk
|
|
85
|
+
}) {
|
|
86
|
+
const reader = stream.getReader();
|
|
87
|
+
const decoder = new TextDecoder();
|
|
88
|
+
let buffer = "";
|
|
89
|
+
try {
|
|
90
|
+
while (true) {
|
|
91
|
+
const { done, value } = await reader.read();
|
|
92
|
+
if (done) break;
|
|
93
|
+
buffer += decoder.decode(value, { stream: true });
|
|
94
|
+
const lines = buffer.split("\n\n");
|
|
95
|
+
buffer = lines.pop() || "";
|
|
96
|
+
for (const line of lines) {
|
|
97
|
+
if (line.startsWith("data: ")) {
|
|
98
|
+
const data = line.slice(6);
|
|
99
|
+
if (data === "[DONE]") {
|
|
100
|
+
console.info("\u{1F3C1} Stream finished");
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
try {
|
|
104
|
+
const json = JSON.parse(data);
|
|
105
|
+
await onChunk(json);
|
|
106
|
+
} catch (error) {
|
|
107
|
+
console.error("\u274C JSON parse error:", error, "Data:", data);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
} finally {
|
|
113
|
+
reader.releaseLock();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
async function processMastraNetworkStream({
|
|
117
|
+
stream,
|
|
118
|
+
onChunk
|
|
119
|
+
}) {
|
|
120
|
+
return sharedProcessMastraStream({
|
|
121
|
+
stream,
|
|
122
|
+
onChunk
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
async function processMastraStream({
|
|
126
|
+
stream,
|
|
127
|
+
onChunk
|
|
128
|
+
}) {
|
|
129
|
+
return sharedProcessMastraStream({
|
|
130
|
+
stream,
|
|
131
|
+
onChunk
|
|
132
|
+
});
|
|
133
|
+
}
|
|
8
134
|
|
|
9
135
|
// src/resources/base.ts
|
|
10
136
|
var BaseResource = class {
|
|
@@ -20,18 +146,21 @@ var BaseResource = class {
|
|
|
20
146
|
*/
|
|
21
147
|
async request(path, options = {}) {
|
|
22
148
|
let lastError = null;
|
|
23
|
-
const { baseUrl, retries = 3, backoffMs = 100, maxBackoffMs = 1e3, headers = {} } = this.options;
|
|
149
|
+
const { baseUrl, retries = 3, backoffMs = 100, maxBackoffMs = 1e3, headers = {}, credentials } = this.options;
|
|
24
150
|
let delay = backoffMs;
|
|
25
151
|
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
26
152
|
try {
|
|
27
|
-
const response = await fetch(`${baseUrl}${path}`, {
|
|
153
|
+
const response = await fetch(`${baseUrl.replace(/\/$/, "")}${path}`, {
|
|
28
154
|
...options,
|
|
29
155
|
headers: {
|
|
156
|
+
...options.body && !(options.body instanceof FormData) && (options.method === "POST" || options.method === "PUT") ? { "content-type": "application/json" } : {},
|
|
30
157
|
...headers,
|
|
31
158
|
...options.headers
|
|
32
159
|
// TODO: Bring this back once we figure out what we/users need to do to make this work with cross-origin requests
|
|
33
160
|
// 'x-mastra-client-type': 'js',
|
|
34
161
|
},
|
|
162
|
+
signal: this.options.abortSignal,
|
|
163
|
+
credentials: options.credentials ?? credentials,
|
|
35
164
|
body: options.body instanceof FormData ? options.body : options.body ? JSON.stringify(options.body) : void 0
|
|
36
165
|
});
|
|
37
166
|
if (!response.ok) {
|
|
@@ -66,6 +195,61 @@ var BaseResource = class {
|
|
|
66
195
|
};
|
|
67
196
|
|
|
68
197
|
// src/resources/agent.ts
|
|
198
|
+
async function executeToolCallAndRespond({
|
|
199
|
+
response,
|
|
200
|
+
params,
|
|
201
|
+
runId,
|
|
202
|
+
resourceId,
|
|
203
|
+
threadId,
|
|
204
|
+
runtimeContext,
|
|
205
|
+
respondFn
|
|
206
|
+
}) {
|
|
207
|
+
if (response.finishReason === "tool-calls") {
|
|
208
|
+
const toolCalls = response.toolCalls;
|
|
209
|
+
if (!toolCalls || !Array.isArray(toolCalls)) {
|
|
210
|
+
return response;
|
|
211
|
+
}
|
|
212
|
+
for (const toolCall of toolCalls) {
|
|
213
|
+
const clientTool = params.clientTools?.[toolCall.toolName];
|
|
214
|
+
if (clientTool && clientTool.execute) {
|
|
215
|
+
const result = await clientTool.execute(
|
|
216
|
+
{
|
|
217
|
+
context: toolCall?.args,
|
|
218
|
+
runId,
|
|
219
|
+
resourceId,
|
|
220
|
+
threadId,
|
|
221
|
+
runtimeContext,
|
|
222
|
+
tracingContext: { currentSpan: void 0 },
|
|
223
|
+
suspend: async () => {
|
|
224
|
+
}
|
|
225
|
+
},
|
|
226
|
+
{
|
|
227
|
+
messages: response.messages,
|
|
228
|
+
toolCallId: toolCall?.toolCallId
|
|
229
|
+
}
|
|
230
|
+
);
|
|
231
|
+
const updatedMessages = [
|
|
232
|
+
...response.response.messages || [],
|
|
233
|
+
{
|
|
234
|
+
role: "tool",
|
|
235
|
+
content: [
|
|
236
|
+
{
|
|
237
|
+
type: "tool-result",
|
|
238
|
+
toolCallId: toolCall.toolCallId,
|
|
239
|
+
toolName: toolCall.toolName,
|
|
240
|
+
result
|
|
241
|
+
}
|
|
242
|
+
]
|
|
243
|
+
}
|
|
244
|
+
];
|
|
245
|
+
return respondFn({
|
|
246
|
+
...params,
|
|
247
|
+
messages: updatedMessages
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
69
253
|
var AgentVoice = class extends BaseResource {
|
|
70
254
|
constructor(options, agentId) {
|
|
71
255
|
super(options);
|
|
@@ -107,10 +291,21 @@ var AgentVoice = class extends BaseResource {
|
|
|
107
291
|
}
|
|
108
292
|
/**
|
|
109
293
|
* Get available speakers for the agent's voice provider
|
|
294
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
295
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
110
296
|
* @returns Promise containing list of available speakers
|
|
111
297
|
*/
|
|
112
|
-
getSpeakers() {
|
|
113
|
-
return this.request(`/api/agents/${this.agentId}/voice/speakers`);
|
|
298
|
+
getSpeakers(runtimeContext) {
|
|
299
|
+
return this.request(`/api/agents/${this.agentId}/voice/speakers${runtimeContextQueryString(runtimeContext)}`);
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Get the listener configuration for the agent's voice provider
|
|
303
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
304
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
305
|
+
* @returns Promise containing a check if the agent has listening capabilities
|
|
306
|
+
*/
|
|
307
|
+
getListener(runtimeContext) {
|
|
308
|
+
return this.request(`/api/agents/${this.agentId}/voice/listener${runtimeContextQueryString(runtimeContext)}`);
|
|
114
309
|
}
|
|
115
310
|
};
|
|
116
311
|
var Agent = class extends BaseResource {
|
|
@@ -122,26 +317,383 @@ var Agent = class extends BaseResource {
|
|
|
122
317
|
voice;
|
|
123
318
|
/**
|
|
124
319
|
* Retrieves details about the agent
|
|
320
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
125
321
|
* @returns Promise containing agent details including model and instructions
|
|
126
322
|
*/
|
|
127
|
-
details() {
|
|
128
|
-
return this.request(`/api/agents/${this.agentId}`);
|
|
323
|
+
details(runtimeContext) {
|
|
324
|
+
return this.request(`/api/agents/${this.agentId}${runtimeContextQueryString(runtimeContext)}`);
|
|
129
325
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
326
|
+
async generate(params) {
|
|
327
|
+
console.warn(
|
|
328
|
+
"Deprecation NOTICE:Generate method will switch to use generateVNext implementation September 30th, 2025. Please use generateLegacy if you don't want to upgrade just yet."
|
|
329
|
+
);
|
|
330
|
+
return this.generateLegacy(params);
|
|
331
|
+
}
|
|
332
|
+
async generateLegacy(params) {
|
|
136
333
|
const processedParams = {
|
|
137
334
|
...params,
|
|
138
|
-
output: params.output
|
|
139
|
-
experimental_output: params.experimental_output
|
|
335
|
+
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
336
|
+
experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
|
|
337
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
338
|
+
clientTools: processClientTools(params.clientTools)
|
|
140
339
|
};
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
340
|
+
const { runId, resourceId, threadId, runtimeContext } = processedParams;
|
|
341
|
+
const response = await this.request(
|
|
342
|
+
`/api/agents/${this.agentId}/generate-legacy`,
|
|
343
|
+
{
|
|
344
|
+
method: "POST",
|
|
345
|
+
body: processedParams
|
|
346
|
+
}
|
|
347
|
+
);
|
|
348
|
+
if (response.finishReason === "tool-calls") {
|
|
349
|
+
const toolCalls = response.toolCalls;
|
|
350
|
+
if (!toolCalls || !Array.isArray(toolCalls)) {
|
|
351
|
+
return response;
|
|
352
|
+
}
|
|
353
|
+
for (const toolCall of toolCalls) {
|
|
354
|
+
const clientTool = params.clientTools?.[toolCall.toolName];
|
|
355
|
+
if (clientTool && clientTool.execute) {
|
|
356
|
+
const result = await clientTool.execute(
|
|
357
|
+
{
|
|
358
|
+
context: toolCall?.args,
|
|
359
|
+
runId,
|
|
360
|
+
resourceId,
|
|
361
|
+
threadId,
|
|
362
|
+
runtimeContext,
|
|
363
|
+
tracingContext: { currentSpan: void 0 },
|
|
364
|
+
suspend: async () => {
|
|
365
|
+
}
|
|
366
|
+
},
|
|
367
|
+
{
|
|
368
|
+
messages: response.messages,
|
|
369
|
+
toolCallId: toolCall?.toolCallId
|
|
370
|
+
}
|
|
371
|
+
);
|
|
372
|
+
const updatedMessages = [
|
|
373
|
+
...response.response.messages,
|
|
374
|
+
{
|
|
375
|
+
role: "tool",
|
|
376
|
+
content: [
|
|
377
|
+
{
|
|
378
|
+
type: "tool-result",
|
|
379
|
+
toolCallId: toolCall.toolCallId,
|
|
380
|
+
toolName: toolCall.toolName,
|
|
381
|
+
result
|
|
382
|
+
}
|
|
383
|
+
]
|
|
384
|
+
}
|
|
385
|
+
];
|
|
386
|
+
return this.generate({
|
|
387
|
+
...params,
|
|
388
|
+
messages: updatedMessages
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
return response;
|
|
394
|
+
}
|
|
395
|
+
async generateVNext(messagesOrParams, options) {
|
|
396
|
+
let params;
|
|
397
|
+
if (typeof messagesOrParams === "object" && "messages" in messagesOrParams) {
|
|
398
|
+
params = messagesOrParams;
|
|
399
|
+
} else {
|
|
400
|
+
params = {
|
|
401
|
+
messages: messagesOrParams,
|
|
402
|
+
...options
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
const processedParams = {
|
|
406
|
+
...params,
|
|
407
|
+
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
408
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
409
|
+
clientTools: processClientTools(params.clientTools),
|
|
410
|
+
structuredOutput: params.structuredOutput ? {
|
|
411
|
+
...params.structuredOutput,
|
|
412
|
+
schema: zodToJsonSchema(params.structuredOutput.schema)
|
|
413
|
+
} : void 0
|
|
414
|
+
};
|
|
415
|
+
const { runId, resourceId, threadId, runtimeContext } = processedParams;
|
|
416
|
+
const response = await this.request(
|
|
417
|
+
`/api/agents/${this.agentId}/generate/vnext`,
|
|
418
|
+
{
|
|
419
|
+
method: "POST",
|
|
420
|
+
body: processedParams
|
|
421
|
+
}
|
|
422
|
+
);
|
|
423
|
+
if (response.finishReason === "tool-calls") {
|
|
424
|
+
return executeToolCallAndRespond({
|
|
425
|
+
response,
|
|
426
|
+
params,
|
|
427
|
+
runId,
|
|
428
|
+
resourceId,
|
|
429
|
+
threadId,
|
|
430
|
+
runtimeContext,
|
|
431
|
+
respondFn: this.generateVNext.bind(this)
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
return response;
|
|
435
|
+
}
|
|
436
|
+
async processChatResponse({
|
|
437
|
+
stream,
|
|
438
|
+
update,
|
|
439
|
+
onToolCall,
|
|
440
|
+
onFinish,
|
|
441
|
+
getCurrentDate = () => /* @__PURE__ */ new Date(),
|
|
442
|
+
lastMessage
|
|
443
|
+
}) {
|
|
444
|
+
const replaceLastMessage = lastMessage?.role === "assistant";
|
|
445
|
+
let step = replaceLastMessage ? 1 + // find max step in existing tool invocations:
|
|
446
|
+
(lastMessage.toolInvocations?.reduce((max, toolInvocation) => {
|
|
447
|
+
return Math.max(max, toolInvocation.step ?? 0);
|
|
448
|
+
}, 0) ?? 0) : 0;
|
|
449
|
+
const message = replaceLastMessage ? structuredClone(lastMessage) : {
|
|
450
|
+
id: uuid.v4(),
|
|
451
|
+
createdAt: getCurrentDate(),
|
|
452
|
+
role: "assistant",
|
|
453
|
+
content: "",
|
|
454
|
+
parts: []
|
|
455
|
+
};
|
|
456
|
+
let currentTextPart = void 0;
|
|
457
|
+
let currentReasoningPart = void 0;
|
|
458
|
+
let currentReasoningTextDetail = void 0;
|
|
459
|
+
function updateToolInvocationPart(toolCallId, invocation) {
|
|
460
|
+
const part = message.parts.find(
|
|
461
|
+
(part2) => part2.type === "tool-invocation" && part2.toolInvocation.toolCallId === toolCallId
|
|
462
|
+
);
|
|
463
|
+
if (part != null) {
|
|
464
|
+
part.toolInvocation = invocation;
|
|
465
|
+
} else {
|
|
466
|
+
message.parts.push({
|
|
467
|
+
type: "tool-invocation",
|
|
468
|
+
toolInvocation: invocation
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
const data = [];
|
|
473
|
+
let messageAnnotations = replaceLastMessage ? lastMessage?.annotations : void 0;
|
|
474
|
+
const partialToolCalls = {};
|
|
475
|
+
let usage = {
|
|
476
|
+
completionTokens: NaN,
|
|
477
|
+
promptTokens: NaN,
|
|
478
|
+
totalTokens: NaN
|
|
479
|
+
};
|
|
480
|
+
let finishReason = "unknown";
|
|
481
|
+
function execUpdate() {
|
|
482
|
+
const copiedData = [...data];
|
|
483
|
+
if (messageAnnotations?.length) {
|
|
484
|
+
message.annotations = messageAnnotations;
|
|
485
|
+
}
|
|
486
|
+
const copiedMessage = {
|
|
487
|
+
// deep copy the message to ensure that deep changes (msg attachments) are updated
|
|
488
|
+
// with SolidJS. SolidJS uses referential integration of sub-objects to detect changes.
|
|
489
|
+
...structuredClone(message),
|
|
490
|
+
// add a revision id to ensure that the message is updated with SWR. SWR uses a
|
|
491
|
+
// hashing approach by default to detect changes, but it only works for shallow
|
|
492
|
+
// changes. This is why we need to add a revision id to ensure that the message
|
|
493
|
+
// is updated with SWR (without it, the changes get stuck in SWR and are not
|
|
494
|
+
// forwarded to rendering):
|
|
495
|
+
revisionId: uuid.v4()
|
|
496
|
+
};
|
|
497
|
+
update({
|
|
498
|
+
message: copiedMessage,
|
|
499
|
+
data: copiedData,
|
|
500
|
+
replaceLastMessage
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
await uiUtils.processDataStream({
|
|
504
|
+
stream,
|
|
505
|
+
onTextPart(value) {
|
|
506
|
+
if (currentTextPart == null) {
|
|
507
|
+
currentTextPart = {
|
|
508
|
+
type: "text",
|
|
509
|
+
text: value
|
|
510
|
+
};
|
|
511
|
+
message.parts.push(currentTextPart);
|
|
512
|
+
} else {
|
|
513
|
+
currentTextPart.text += value;
|
|
514
|
+
}
|
|
515
|
+
message.content += value;
|
|
516
|
+
execUpdate();
|
|
517
|
+
},
|
|
518
|
+
onReasoningPart(value) {
|
|
519
|
+
if (currentReasoningTextDetail == null) {
|
|
520
|
+
currentReasoningTextDetail = { type: "text", text: value };
|
|
521
|
+
if (currentReasoningPart != null) {
|
|
522
|
+
currentReasoningPart.details.push(currentReasoningTextDetail);
|
|
523
|
+
}
|
|
524
|
+
} else {
|
|
525
|
+
currentReasoningTextDetail.text += value;
|
|
526
|
+
}
|
|
527
|
+
if (currentReasoningPart == null) {
|
|
528
|
+
currentReasoningPart = {
|
|
529
|
+
type: "reasoning",
|
|
530
|
+
reasoning: value,
|
|
531
|
+
details: [currentReasoningTextDetail]
|
|
532
|
+
};
|
|
533
|
+
message.parts.push(currentReasoningPart);
|
|
534
|
+
} else {
|
|
535
|
+
currentReasoningPart.reasoning += value;
|
|
536
|
+
}
|
|
537
|
+
message.reasoning = (message.reasoning ?? "") + value;
|
|
538
|
+
execUpdate();
|
|
539
|
+
},
|
|
540
|
+
onReasoningSignaturePart(value) {
|
|
541
|
+
if (currentReasoningTextDetail != null) {
|
|
542
|
+
currentReasoningTextDetail.signature = value.signature;
|
|
543
|
+
}
|
|
544
|
+
},
|
|
545
|
+
onRedactedReasoningPart(value) {
|
|
546
|
+
if (currentReasoningPart == null) {
|
|
547
|
+
currentReasoningPart = {
|
|
548
|
+
type: "reasoning",
|
|
549
|
+
reasoning: "",
|
|
550
|
+
details: []
|
|
551
|
+
};
|
|
552
|
+
message.parts.push(currentReasoningPart);
|
|
553
|
+
}
|
|
554
|
+
currentReasoningPart.details.push({
|
|
555
|
+
type: "redacted",
|
|
556
|
+
data: value.data
|
|
557
|
+
});
|
|
558
|
+
currentReasoningTextDetail = void 0;
|
|
559
|
+
execUpdate();
|
|
560
|
+
},
|
|
561
|
+
onFilePart(value) {
|
|
562
|
+
message.parts.push({
|
|
563
|
+
type: "file",
|
|
564
|
+
mimeType: value.mimeType,
|
|
565
|
+
data: value.data
|
|
566
|
+
});
|
|
567
|
+
execUpdate();
|
|
568
|
+
},
|
|
569
|
+
onSourcePart(value) {
|
|
570
|
+
message.parts.push({
|
|
571
|
+
type: "source",
|
|
572
|
+
source: value
|
|
573
|
+
});
|
|
574
|
+
execUpdate();
|
|
575
|
+
},
|
|
576
|
+
onToolCallStreamingStartPart(value) {
|
|
577
|
+
if (message.toolInvocations == null) {
|
|
578
|
+
message.toolInvocations = [];
|
|
579
|
+
}
|
|
580
|
+
partialToolCalls[value.toolCallId] = {
|
|
581
|
+
text: "",
|
|
582
|
+
step,
|
|
583
|
+
toolName: value.toolName,
|
|
584
|
+
index: message.toolInvocations.length
|
|
585
|
+
};
|
|
586
|
+
const invocation = {
|
|
587
|
+
state: "partial-call",
|
|
588
|
+
step,
|
|
589
|
+
toolCallId: value.toolCallId,
|
|
590
|
+
toolName: value.toolName,
|
|
591
|
+
args: void 0
|
|
592
|
+
};
|
|
593
|
+
message.toolInvocations.push(invocation);
|
|
594
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
595
|
+
execUpdate();
|
|
596
|
+
},
|
|
597
|
+
onToolCallDeltaPart(value) {
|
|
598
|
+
const partialToolCall = partialToolCalls[value.toolCallId];
|
|
599
|
+
partialToolCall.text += value.argsTextDelta;
|
|
600
|
+
const { value: partialArgs } = uiUtils.parsePartialJson(partialToolCall.text);
|
|
601
|
+
const invocation = {
|
|
602
|
+
state: "partial-call",
|
|
603
|
+
step: partialToolCall.step,
|
|
604
|
+
toolCallId: value.toolCallId,
|
|
605
|
+
toolName: partialToolCall.toolName,
|
|
606
|
+
args: partialArgs
|
|
607
|
+
};
|
|
608
|
+
message.toolInvocations[partialToolCall.index] = invocation;
|
|
609
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
610
|
+
execUpdate();
|
|
611
|
+
},
|
|
612
|
+
async onToolCallPart(value) {
|
|
613
|
+
const invocation = {
|
|
614
|
+
state: "call",
|
|
615
|
+
step,
|
|
616
|
+
...value
|
|
617
|
+
};
|
|
618
|
+
if (partialToolCalls[value.toolCallId] != null) {
|
|
619
|
+
message.toolInvocations[partialToolCalls[value.toolCallId].index] = invocation;
|
|
620
|
+
} else {
|
|
621
|
+
if (message.toolInvocations == null) {
|
|
622
|
+
message.toolInvocations = [];
|
|
623
|
+
}
|
|
624
|
+
message.toolInvocations.push(invocation);
|
|
625
|
+
}
|
|
626
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
627
|
+
execUpdate();
|
|
628
|
+
if (onToolCall) {
|
|
629
|
+
const result = await onToolCall({ toolCall: value });
|
|
630
|
+
if (result != null) {
|
|
631
|
+
const invocation2 = {
|
|
632
|
+
state: "result",
|
|
633
|
+
step,
|
|
634
|
+
...value,
|
|
635
|
+
result
|
|
636
|
+
};
|
|
637
|
+
message.toolInvocations[message.toolInvocations.length - 1] = invocation2;
|
|
638
|
+
updateToolInvocationPart(value.toolCallId, invocation2);
|
|
639
|
+
execUpdate();
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
},
|
|
643
|
+
onToolResultPart(value) {
|
|
644
|
+
const toolInvocations = message.toolInvocations;
|
|
645
|
+
if (toolInvocations == null) {
|
|
646
|
+
throw new Error("tool_result must be preceded by a tool_call");
|
|
647
|
+
}
|
|
648
|
+
const toolInvocationIndex = toolInvocations.findIndex((invocation2) => invocation2.toolCallId === value.toolCallId);
|
|
649
|
+
if (toolInvocationIndex === -1) {
|
|
650
|
+
throw new Error("tool_result must be preceded by a tool_call with the same toolCallId");
|
|
651
|
+
}
|
|
652
|
+
const invocation = {
|
|
653
|
+
...toolInvocations[toolInvocationIndex],
|
|
654
|
+
state: "result",
|
|
655
|
+
...value
|
|
656
|
+
};
|
|
657
|
+
toolInvocations[toolInvocationIndex] = invocation;
|
|
658
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
659
|
+
execUpdate();
|
|
660
|
+
},
|
|
661
|
+
onDataPart(value) {
|
|
662
|
+
data.push(...value);
|
|
663
|
+
execUpdate();
|
|
664
|
+
},
|
|
665
|
+
onMessageAnnotationsPart(value) {
|
|
666
|
+
if (messageAnnotations == null) {
|
|
667
|
+
messageAnnotations = [...value];
|
|
668
|
+
} else {
|
|
669
|
+
messageAnnotations.push(...value);
|
|
670
|
+
}
|
|
671
|
+
execUpdate();
|
|
672
|
+
},
|
|
673
|
+
onFinishStepPart(value) {
|
|
674
|
+
step += 1;
|
|
675
|
+
currentTextPart = value.isContinued ? currentTextPart : void 0;
|
|
676
|
+
currentReasoningPart = void 0;
|
|
677
|
+
currentReasoningTextDetail = void 0;
|
|
678
|
+
},
|
|
679
|
+
onStartStepPart(value) {
|
|
680
|
+
if (!replaceLastMessage) {
|
|
681
|
+
message.id = value.messageId;
|
|
682
|
+
}
|
|
683
|
+
message.parts.push({ type: "step-start" });
|
|
684
|
+
execUpdate();
|
|
685
|
+
},
|
|
686
|
+
onFinishMessagePart(value) {
|
|
687
|
+
finishReason = value.finishReason;
|
|
688
|
+
if (value.usage != null) {
|
|
689
|
+
usage = value.usage;
|
|
690
|
+
}
|
|
691
|
+
},
|
|
692
|
+
onErrorPart(error) {
|
|
693
|
+
throw new Error(error);
|
|
694
|
+
}
|
|
144
695
|
});
|
|
696
|
+
onFinish?.({ message, finishReason, usage });
|
|
145
697
|
}
|
|
146
698
|
/**
|
|
147
699
|
* Streams a response from the agent
|
|
@@ -149,12 +701,294 @@ var Agent = class extends BaseResource {
|
|
|
149
701
|
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
150
702
|
*/
|
|
151
703
|
async stream(params) {
|
|
704
|
+
console.warn(
|
|
705
|
+
"Deprecation NOTICE:\nStream method will switch to use streamVNext implementation September 30th, 2025. Please use streamLegacy if you don't want to upgrade just yet."
|
|
706
|
+
);
|
|
707
|
+
return this.streamLegacy(params);
|
|
708
|
+
}
|
|
709
|
+
/**
|
|
710
|
+
* Streams a response from the agent
|
|
711
|
+
* @param params - Stream parameters including prompt
|
|
712
|
+
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
713
|
+
*/
|
|
714
|
+
async streamLegacy(params) {
|
|
152
715
|
const processedParams = {
|
|
153
716
|
...params,
|
|
154
|
-
output: params.output
|
|
155
|
-
experimental_output: params.experimental_output
|
|
717
|
+
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
718
|
+
experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
|
|
719
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
720
|
+
clientTools: processClientTools(params.clientTools)
|
|
721
|
+
};
|
|
722
|
+
const { readable, writable } = new TransformStream();
|
|
723
|
+
const response = await this.processStreamResponse(processedParams, writable);
|
|
724
|
+
const streamResponse = new Response(readable, {
|
|
725
|
+
status: response.status,
|
|
726
|
+
statusText: response.statusText,
|
|
727
|
+
headers: response.headers
|
|
728
|
+
});
|
|
729
|
+
streamResponse.processDataStream = async (options = {}) => {
|
|
730
|
+
await uiUtils.processDataStream({
|
|
731
|
+
stream: streamResponse.body,
|
|
732
|
+
...options
|
|
733
|
+
});
|
|
734
|
+
};
|
|
735
|
+
return streamResponse;
|
|
736
|
+
}
|
|
737
|
+
async processChatResponse_vNext({
|
|
738
|
+
stream,
|
|
739
|
+
update,
|
|
740
|
+
onToolCall,
|
|
741
|
+
onFinish,
|
|
742
|
+
getCurrentDate = () => /* @__PURE__ */ new Date(),
|
|
743
|
+
lastMessage
|
|
744
|
+
}) {
|
|
745
|
+
const replaceLastMessage = lastMessage?.role === "assistant";
|
|
746
|
+
let step = replaceLastMessage ? 1 + // find max step in existing tool invocations:
|
|
747
|
+
(lastMessage.toolInvocations?.reduce((max, toolInvocation) => {
|
|
748
|
+
return Math.max(max, toolInvocation.step ?? 0);
|
|
749
|
+
}, 0) ?? 0) : 0;
|
|
750
|
+
const message = replaceLastMessage ? structuredClone(lastMessage) : {
|
|
751
|
+
id: uuid.v4(),
|
|
752
|
+
createdAt: getCurrentDate(),
|
|
753
|
+
role: "assistant",
|
|
754
|
+
content: "",
|
|
755
|
+
parts: []
|
|
756
|
+
};
|
|
757
|
+
let currentTextPart = void 0;
|
|
758
|
+
let currentReasoningPart = void 0;
|
|
759
|
+
let currentReasoningTextDetail = void 0;
|
|
760
|
+
function updateToolInvocationPart(toolCallId, invocation) {
|
|
761
|
+
const part = message.parts.find(
|
|
762
|
+
(part2) => part2.type === "tool-invocation" && part2.toolInvocation.toolCallId === toolCallId
|
|
763
|
+
);
|
|
764
|
+
if (part != null) {
|
|
765
|
+
part.toolInvocation = invocation;
|
|
766
|
+
} else {
|
|
767
|
+
message.parts.push({
|
|
768
|
+
type: "tool-invocation",
|
|
769
|
+
toolInvocation: invocation
|
|
770
|
+
});
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
const data = [];
|
|
774
|
+
let messageAnnotations = replaceLastMessage ? lastMessage?.annotations : void 0;
|
|
775
|
+
const partialToolCalls = {};
|
|
776
|
+
let usage = {
|
|
777
|
+
completionTokens: NaN,
|
|
778
|
+
promptTokens: NaN,
|
|
779
|
+
totalTokens: NaN
|
|
156
780
|
};
|
|
157
|
-
|
|
781
|
+
let finishReason = "unknown";
|
|
782
|
+
function execUpdate() {
|
|
783
|
+
const copiedData = [...data];
|
|
784
|
+
if (messageAnnotations?.length) {
|
|
785
|
+
message.annotations = messageAnnotations;
|
|
786
|
+
}
|
|
787
|
+
const copiedMessage = {
|
|
788
|
+
// deep copy the message to ensure that deep changes (msg attachments) are updated
|
|
789
|
+
// with SolidJS. SolidJS uses referential integration of sub-objects to detect changes.
|
|
790
|
+
...structuredClone(message),
|
|
791
|
+
// add a revision id to ensure that the message is updated with SWR. SWR uses a
|
|
792
|
+
// hashing approach by default to detect changes, but it only works for shallow
|
|
793
|
+
// changes. This is why we need to add a revision id to ensure that the message
|
|
794
|
+
// is updated with SWR (without it, the changes get stuck in SWR and are not
|
|
795
|
+
// forwarded to rendering):
|
|
796
|
+
revisionId: uuid.v4()
|
|
797
|
+
};
|
|
798
|
+
update({
|
|
799
|
+
message: copiedMessage,
|
|
800
|
+
data: copiedData,
|
|
801
|
+
replaceLastMessage
|
|
802
|
+
});
|
|
803
|
+
}
|
|
804
|
+
await processMastraStream({
|
|
805
|
+
stream,
|
|
806
|
+
// TODO: casting as any here because the stream types were all typed as any before in core.
|
|
807
|
+
// but this is completely wrong and this fn is probably broken. Remove ":any" and you'll see a bunch of type errors
|
|
808
|
+
onChunk: async (chunk) => {
|
|
809
|
+
switch (chunk.type) {
|
|
810
|
+
case "step-start": {
|
|
811
|
+
if (!replaceLastMessage) {
|
|
812
|
+
message.id = chunk.payload.messageId;
|
|
813
|
+
}
|
|
814
|
+
message.parts.push({ type: "step-start" });
|
|
815
|
+
execUpdate();
|
|
816
|
+
break;
|
|
817
|
+
}
|
|
818
|
+
case "text-delta": {
|
|
819
|
+
if (currentTextPart == null) {
|
|
820
|
+
currentTextPart = {
|
|
821
|
+
type: "text",
|
|
822
|
+
text: chunk.payload.text
|
|
823
|
+
};
|
|
824
|
+
message.parts.push(currentTextPart);
|
|
825
|
+
} else {
|
|
826
|
+
currentTextPart.text += chunk.payload.text;
|
|
827
|
+
}
|
|
828
|
+
message.content += chunk.payload.text;
|
|
829
|
+
execUpdate();
|
|
830
|
+
break;
|
|
831
|
+
}
|
|
832
|
+
case "reasoning-delta": {
|
|
833
|
+
if (currentReasoningTextDetail == null) {
|
|
834
|
+
currentReasoningTextDetail = { type: "text", text: chunk.payload.text };
|
|
835
|
+
if (currentReasoningPart != null) {
|
|
836
|
+
currentReasoningPart.details.push(currentReasoningTextDetail);
|
|
837
|
+
}
|
|
838
|
+
} else {
|
|
839
|
+
currentReasoningTextDetail.text += chunk.payload.text;
|
|
840
|
+
}
|
|
841
|
+
if (currentReasoningPart == null) {
|
|
842
|
+
currentReasoningPart = {
|
|
843
|
+
type: "reasoning",
|
|
844
|
+
reasoning: chunk.payload.text,
|
|
845
|
+
details: [currentReasoningTextDetail]
|
|
846
|
+
};
|
|
847
|
+
message.parts.push(currentReasoningPart);
|
|
848
|
+
} else {
|
|
849
|
+
currentReasoningPart.reasoning += chunk.payload.text;
|
|
850
|
+
}
|
|
851
|
+
message.reasoning = (message.reasoning ?? "") + chunk.payload.text;
|
|
852
|
+
execUpdate();
|
|
853
|
+
break;
|
|
854
|
+
}
|
|
855
|
+
case "file": {
|
|
856
|
+
message.parts.push({
|
|
857
|
+
type: "file",
|
|
858
|
+
mimeType: chunk.payload.mimeType,
|
|
859
|
+
data: chunk.payload.data
|
|
860
|
+
});
|
|
861
|
+
execUpdate();
|
|
862
|
+
break;
|
|
863
|
+
}
|
|
864
|
+
case "source": {
|
|
865
|
+
message.parts.push({
|
|
866
|
+
type: "source",
|
|
867
|
+
source: chunk.payload.source
|
|
868
|
+
});
|
|
869
|
+
execUpdate();
|
|
870
|
+
break;
|
|
871
|
+
}
|
|
872
|
+
case "tool-call": {
|
|
873
|
+
const invocation = {
|
|
874
|
+
state: "call",
|
|
875
|
+
step,
|
|
876
|
+
...chunk.payload
|
|
877
|
+
};
|
|
878
|
+
if (partialToolCalls[chunk.payload.toolCallId] != null) {
|
|
879
|
+
message.toolInvocations[partialToolCalls[chunk.payload.toolCallId].index] = invocation;
|
|
880
|
+
} else {
|
|
881
|
+
if (message.toolInvocations == null) {
|
|
882
|
+
message.toolInvocations = [];
|
|
883
|
+
}
|
|
884
|
+
message.toolInvocations.push(invocation);
|
|
885
|
+
}
|
|
886
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation);
|
|
887
|
+
execUpdate();
|
|
888
|
+
if (onToolCall) {
|
|
889
|
+
const result = await onToolCall({ toolCall: chunk.payload });
|
|
890
|
+
if (result != null) {
|
|
891
|
+
const invocation2 = {
|
|
892
|
+
state: "result",
|
|
893
|
+
step,
|
|
894
|
+
...chunk.payload,
|
|
895
|
+
result
|
|
896
|
+
};
|
|
897
|
+
message.toolInvocations[message.toolInvocations.length - 1] = invocation2;
|
|
898
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation2);
|
|
899
|
+
execUpdate();
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
case "tool-call-input-streaming-start": {
|
|
904
|
+
if (message.toolInvocations == null) {
|
|
905
|
+
message.toolInvocations = [];
|
|
906
|
+
}
|
|
907
|
+
partialToolCalls[chunk.payload.toolCallId] = {
|
|
908
|
+
text: "",
|
|
909
|
+
step,
|
|
910
|
+
toolName: chunk.payload.toolName,
|
|
911
|
+
index: message.toolInvocations.length
|
|
912
|
+
};
|
|
913
|
+
const invocation = {
|
|
914
|
+
state: "partial-call",
|
|
915
|
+
step,
|
|
916
|
+
toolCallId: chunk.payload.toolCallId,
|
|
917
|
+
toolName: chunk.payload.toolName,
|
|
918
|
+
args: chunk.payload.args
|
|
919
|
+
};
|
|
920
|
+
message.toolInvocations.push(invocation);
|
|
921
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation);
|
|
922
|
+
execUpdate();
|
|
923
|
+
break;
|
|
924
|
+
}
|
|
925
|
+
case "tool-call-delta": {
|
|
926
|
+
const partialToolCall = partialToolCalls[chunk.payload.toolCallId];
|
|
927
|
+
partialToolCall.text += chunk.payload.argsTextDelta;
|
|
928
|
+
const { value: partialArgs } = uiUtils.parsePartialJson(partialToolCall.text);
|
|
929
|
+
const invocation = {
|
|
930
|
+
state: "partial-call",
|
|
931
|
+
step: partialToolCall.step,
|
|
932
|
+
toolCallId: chunk.payload.toolCallId,
|
|
933
|
+
toolName: partialToolCall.toolName,
|
|
934
|
+
args: partialArgs
|
|
935
|
+
};
|
|
936
|
+
message.toolInvocations[partialToolCall.index] = invocation;
|
|
937
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation);
|
|
938
|
+
execUpdate();
|
|
939
|
+
break;
|
|
940
|
+
}
|
|
941
|
+
case "tool-result": {
|
|
942
|
+
const toolInvocations = message.toolInvocations;
|
|
943
|
+
if (toolInvocations == null) {
|
|
944
|
+
throw new Error("tool_result must be preceded by a tool_call");
|
|
945
|
+
}
|
|
946
|
+
const toolInvocationIndex = toolInvocations.findIndex(
|
|
947
|
+
(invocation2) => invocation2.toolCallId === chunk.payload.toolCallId
|
|
948
|
+
);
|
|
949
|
+
if (toolInvocationIndex === -1) {
|
|
950
|
+
throw new Error("tool_result must be preceded by a tool_call with the same toolCallId");
|
|
951
|
+
}
|
|
952
|
+
const invocation = {
|
|
953
|
+
...toolInvocations[toolInvocationIndex],
|
|
954
|
+
state: "result",
|
|
955
|
+
...chunk.payload
|
|
956
|
+
};
|
|
957
|
+
toolInvocations[toolInvocationIndex] = invocation;
|
|
958
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation);
|
|
959
|
+
execUpdate();
|
|
960
|
+
break;
|
|
961
|
+
}
|
|
962
|
+
case "error": {
|
|
963
|
+
throw new Error(chunk.payload.error);
|
|
964
|
+
}
|
|
965
|
+
case "data": {
|
|
966
|
+
data.push(...chunk.payload.data);
|
|
967
|
+
execUpdate();
|
|
968
|
+
break;
|
|
969
|
+
}
|
|
970
|
+
case "step-finish": {
|
|
971
|
+
step += 1;
|
|
972
|
+
currentTextPart = chunk.payload.stepResult.isContinued ? currentTextPart : void 0;
|
|
973
|
+
currentReasoningPart = void 0;
|
|
974
|
+
currentReasoningTextDetail = void 0;
|
|
975
|
+
execUpdate();
|
|
976
|
+
break;
|
|
977
|
+
}
|
|
978
|
+
case "finish": {
|
|
979
|
+
finishReason = chunk.payload.stepResult.reason;
|
|
980
|
+
if (chunk.payload.usage != null) {
|
|
981
|
+
usage = chunk.payload.usage;
|
|
982
|
+
}
|
|
983
|
+
break;
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
});
|
|
988
|
+
onFinish?.({ message, finishReason, usage });
|
|
989
|
+
}
|
|
990
|
+
async processStreamResponse_vNext(processedParams, writable) {
|
|
991
|
+
const response = await this.request(`/api/agents/${this.agentId}/stream/vnext`, {
|
|
158
992
|
method: "POST",
|
|
159
993
|
body: processedParams,
|
|
160
994
|
stream: true
|
|
@@ -162,91 +996,364 @@ var Agent = class extends BaseResource {
|
|
|
162
996
|
if (!response.body) {
|
|
163
997
|
throw new Error("No response body");
|
|
164
998
|
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
999
|
+
try {
|
|
1000
|
+
let toolCalls = [];
|
|
1001
|
+
let messages = [];
|
|
1002
|
+
const [streamForWritable, streamForProcessing] = response.body.tee();
|
|
1003
|
+
streamForWritable.pipeTo(
|
|
1004
|
+
new WritableStream({
|
|
1005
|
+
async write(chunk) {
|
|
1006
|
+
try {
|
|
1007
|
+
const text = new TextDecoder().decode(chunk);
|
|
1008
|
+
if (text.includes("[DONE]")) {
|
|
1009
|
+
return;
|
|
1010
|
+
}
|
|
1011
|
+
} catch {
|
|
1012
|
+
}
|
|
1013
|
+
const writer = writable.getWriter();
|
|
1014
|
+
try {
|
|
1015
|
+
await writer.write(chunk);
|
|
1016
|
+
} finally {
|
|
1017
|
+
writer.releaseLock();
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
}),
|
|
1021
|
+
{
|
|
1022
|
+
preventClose: true
|
|
1023
|
+
}
|
|
1024
|
+
).catch((error) => {
|
|
1025
|
+
console.error("Error piping to writable stream:", error);
|
|
1026
|
+
});
|
|
1027
|
+
this.processChatResponse_vNext({
|
|
1028
|
+
stream: streamForProcessing,
|
|
1029
|
+
update: ({ message }) => {
|
|
1030
|
+
const existingIndex = messages.findIndex((m) => m.id === message.id);
|
|
1031
|
+
if (existingIndex !== -1) {
|
|
1032
|
+
messages[existingIndex] = message;
|
|
1033
|
+
} else {
|
|
1034
|
+
messages.push(message);
|
|
1035
|
+
}
|
|
1036
|
+
},
|
|
1037
|
+
onFinish: async ({ finishReason, message }) => {
|
|
1038
|
+
if (finishReason === "tool-calls") {
|
|
1039
|
+
const toolCall = [...message?.parts ?? []].reverse().find((part) => part.type === "tool-invocation")?.toolInvocation;
|
|
1040
|
+
if (toolCall) {
|
|
1041
|
+
toolCalls.push(toolCall);
|
|
1042
|
+
}
|
|
1043
|
+
for (const toolCall2 of toolCalls) {
|
|
1044
|
+
const clientTool = processedParams.clientTools?.[toolCall2.toolName];
|
|
1045
|
+
if (clientTool && clientTool.execute) {
|
|
1046
|
+
const result = await clientTool.execute(
|
|
1047
|
+
{
|
|
1048
|
+
context: toolCall2?.args,
|
|
1049
|
+
runId: processedParams.runId,
|
|
1050
|
+
resourceId: processedParams.resourceId,
|
|
1051
|
+
threadId: processedParams.threadId,
|
|
1052
|
+
runtimeContext: processedParams.runtimeContext,
|
|
1053
|
+
// TODO: Pass proper tracing context when client-js supports tracing
|
|
1054
|
+
tracingContext: { currentSpan: void 0 },
|
|
1055
|
+
suspend: async () => {
|
|
1056
|
+
}
|
|
1057
|
+
},
|
|
1058
|
+
{
|
|
1059
|
+
messages: response.messages,
|
|
1060
|
+
toolCallId: toolCall2?.toolCallId
|
|
1061
|
+
}
|
|
1062
|
+
);
|
|
1063
|
+
const lastMessageRaw = messages[messages.length - 1];
|
|
1064
|
+
const lastMessage = lastMessageRaw != null ? JSON.parse(JSON.stringify(lastMessageRaw)) : void 0;
|
|
1065
|
+
const toolInvocationPart = lastMessage?.parts?.find(
|
|
1066
|
+
(part) => part.type === "tool-invocation" && part.toolInvocation?.toolCallId === toolCall2.toolCallId
|
|
1067
|
+
);
|
|
1068
|
+
if (toolInvocationPart) {
|
|
1069
|
+
toolInvocationPart.toolInvocation = {
|
|
1070
|
+
...toolInvocationPart.toolInvocation,
|
|
1071
|
+
state: "result",
|
|
1072
|
+
result
|
|
1073
|
+
};
|
|
1074
|
+
}
|
|
1075
|
+
const toolInvocation = lastMessage?.toolInvocations?.find(
|
|
1076
|
+
(toolInvocation2) => toolInvocation2.toolCallId === toolCall2.toolCallId
|
|
1077
|
+
);
|
|
1078
|
+
if (toolInvocation) {
|
|
1079
|
+
toolInvocation.state = "result";
|
|
1080
|
+
toolInvocation.result = result;
|
|
1081
|
+
}
|
|
1082
|
+
const updatedMessages = lastMessage != null ? [...messages.filter((m) => m.id !== lastMessage.id), lastMessage] : [...messages];
|
|
1083
|
+
this.processStreamResponse_vNext(
|
|
1084
|
+
{
|
|
1085
|
+
...processedParams,
|
|
1086
|
+
messages: updatedMessages
|
|
1087
|
+
},
|
|
1088
|
+
writable
|
|
1089
|
+
).catch((error) => {
|
|
1090
|
+
console.error("Error processing stream response:", error);
|
|
1091
|
+
});
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
} else {
|
|
1095
|
+
setTimeout(() => {
|
|
1096
|
+
writable.close();
|
|
1097
|
+
}, 0);
|
|
1098
|
+
}
|
|
1099
|
+
},
|
|
1100
|
+
lastMessage: void 0
|
|
1101
|
+
}).catch((error) => {
|
|
1102
|
+
console.error("Error processing stream response:", error);
|
|
1103
|
+
});
|
|
1104
|
+
} catch (error) {
|
|
1105
|
+
console.error("Error processing stream response:", error);
|
|
1106
|
+
}
|
|
1107
|
+
return response;
|
|
1108
|
+
}
|
|
1109
|
+
async network(params) {
|
|
1110
|
+
const response = await this.request(`/api/agents/${this.agentId}/network`, {
|
|
1111
|
+
method: "POST",
|
|
1112
|
+
body: params,
|
|
1113
|
+
stream: true
|
|
1114
|
+
});
|
|
1115
|
+
if (!response.body) {
|
|
1116
|
+
throw new Error("No response body");
|
|
1117
|
+
}
|
|
1118
|
+
const streamResponse = new Response(response.body, {
|
|
1119
|
+
status: response.status,
|
|
1120
|
+
statusText: response.statusText,
|
|
1121
|
+
headers: response.headers
|
|
1122
|
+
});
|
|
1123
|
+
streamResponse.processDataStream = async ({
|
|
1124
|
+
onChunk
|
|
1125
|
+
}) => {
|
|
1126
|
+
await processMastraNetworkStream({
|
|
1127
|
+
stream: streamResponse.body,
|
|
1128
|
+
onChunk
|
|
1129
|
+
});
|
|
1130
|
+
};
|
|
1131
|
+
return streamResponse;
|
|
1132
|
+
}
|
|
1133
|
+
async streamVNext(messagesOrParams, options) {
|
|
1134
|
+
let params;
|
|
1135
|
+
if (typeof messagesOrParams === "object" && "messages" in messagesOrParams) {
|
|
1136
|
+
params = messagesOrParams;
|
|
1137
|
+
} else {
|
|
1138
|
+
params = {
|
|
1139
|
+
messages: messagesOrParams,
|
|
168
1140
|
...options
|
|
1141
|
+
};
|
|
1142
|
+
}
|
|
1143
|
+
const processedParams = {
|
|
1144
|
+
...params,
|
|
1145
|
+
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
1146
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
1147
|
+
clientTools: processClientTools(params.clientTools),
|
|
1148
|
+
structuredOutput: params.structuredOutput ? {
|
|
1149
|
+
...params.structuredOutput,
|
|
1150
|
+
schema: zodToJsonSchema(params.structuredOutput.schema)
|
|
1151
|
+
} : void 0
|
|
1152
|
+
};
|
|
1153
|
+
const { readable, writable } = new TransformStream();
|
|
1154
|
+
const response = await this.processStreamResponse_vNext(processedParams, writable);
|
|
1155
|
+
const streamResponse = new Response(readable, {
|
|
1156
|
+
status: response.status,
|
|
1157
|
+
statusText: response.statusText,
|
|
1158
|
+
headers: response.headers
|
|
1159
|
+
});
|
|
1160
|
+
streamResponse.processDataStream = async ({
|
|
1161
|
+
onChunk
|
|
1162
|
+
}) => {
|
|
1163
|
+
await processMastraStream({
|
|
1164
|
+
stream: streamResponse.body,
|
|
1165
|
+
onChunk
|
|
169
1166
|
});
|
|
170
1167
|
};
|
|
1168
|
+
return streamResponse;
|
|
1169
|
+
}
|
|
1170
|
+
/**
|
|
1171
|
+
* Processes the stream response and handles tool calls
|
|
1172
|
+
*/
|
|
1173
|
+
async processStreamResponse(processedParams, writable) {
|
|
1174
|
+
const response = await this.request(`/api/agents/${this.agentId}/stream-legacy`, {
|
|
1175
|
+
method: "POST",
|
|
1176
|
+
body: processedParams,
|
|
1177
|
+
stream: true
|
|
1178
|
+
});
|
|
1179
|
+
if (!response.body) {
|
|
1180
|
+
throw new Error("No response body");
|
|
1181
|
+
}
|
|
1182
|
+
try {
|
|
1183
|
+
let toolCalls = [];
|
|
1184
|
+
let messages = [];
|
|
1185
|
+
const [streamForWritable, streamForProcessing] = response.body.tee();
|
|
1186
|
+
streamForWritable.pipeTo(writable, {
|
|
1187
|
+
preventClose: true
|
|
1188
|
+
}).catch((error) => {
|
|
1189
|
+
console.error("Error piping to writable stream:", error);
|
|
1190
|
+
});
|
|
1191
|
+
this.processChatResponse({
|
|
1192
|
+
stream: streamForProcessing,
|
|
1193
|
+
update: ({ message }) => {
|
|
1194
|
+
const existingIndex = messages.findIndex((m) => m.id === message.id);
|
|
1195
|
+
if (existingIndex !== -1) {
|
|
1196
|
+
messages[existingIndex] = message;
|
|
1197
|
+
} else {
|
|
1198
|
+
messages.push(message);
|
|
1199
|
+
}
|
|
1200
|
+
},
|
|
1201
|
+
onFinish: async ({ finishReason, message }) => {
|
|
1202
|
+
if (finishReason === "tool-calls") {
|
|
1203
|
+
const toolCall = [...message?.parts ?? []].reverse().find((part) => part.type === "tool-invocation")?.toolInvocation;
|
|
1204
|
+
if (toolCall) {
|
|
1205
|
+
toolCalls.push(toolCall);
|
|
1206
|
+
}
|
|
1207
|
+
for (const toolCall2 of toolCalls) {
|
|
1208
|
+
const clientTool = processedParams.clientTools?.[toolCall2.toolName];
|
|
1209
|
+
if (clientTool && clientTool.execute) {
|
|
1210
|
+
const result = await clientTool.execute(
|
|
1211
|
+
{
|
|
1212
|
+
context: toolCall2?.args,
|
|
1213
|
+
runId: processedParams.runId,
|
|
1214
|
+
resourceId: processedParams.resourceId,
|
|
1215
|
+
threadId: processedParams.threadId,
|
|
1216
|
+
runtimeContext: processedParams.runtimeContext,
|
|
1217
|
+
// TODO: Pass proper tracing context when client-js supports tracing
|
|
1218
|
+
tracingContext: { currentSpan: void 0 },
|
|
1219
|
+
suspend: async () => {
|
|
1220
|
+
}
|
|
1221
|
+
},
|
|
1222
|
+
{
|
|
1223
|
+
messages: response.messages,
|
|
1224
|
+
toolCallId: toolCall2?.toolCallId
|
|
1225
|
+
}
|
|
1226
|
+
);
|
|
1227
|
+
const lastMessage = JSON.parse(JSON.stringify(messages[messages.length - 1]));
|
|
1228
|
+
const toolInvocationPart = lastMessage?.parts?.find(
|
|
1229
|
+
(part) => part.type === "tool-invocation" && part.toolInvocation?.toolCallId === toolCall2.toolCallId
|
|
1230
|
+
);
|
|
1231
|
+
if (toolInvocationPart) {
|
|
1232
|
+
toolInvocationPart.toolInvocation = {
|
|
1233
|
+
...toolInvocationPart.toolInvocation,
|
|
1234
|
+
state: "result",
|
|
1235
|
+
result
|
|
1236
|
+
};
|
|
1237
|
+
}
|
|
1238
|
+
const toolInvocation = lastMessage?.toolInvocations?.find(
|
|
1239
|
+
(toolInvocation2) => toolInvocation2.toolCallId === toolCall2.toolCallId
|
|
1240
|
+
);
|
|
1241
|
+
if (toolInvocation) {
|
|
1242
|
+
toolInvocation.state = "result";
|
|
1243
|
+
toolInvocation.result = result;
|
|
1244
|
+
}
|
|
1245
|
+
const writer = writable.getWriter();
|
|
1246
|
+
try {
|
|
1247
|
+
await writer.write(
|
|
1248
|
+
new TextEncoder().encode(
|
|
1249
|
+
"a:" + JSON.stringify({
|
|
1250
|
+
toolCallId: toolCall2.toolCallId,
|
|
1251
|
+
result
|
|
1252
|
+
}) + "\n"
|
|
1253
|
+
)
|
|
1254
|
+
);
|
|
1255
|
+
} finally {
|
|
1256
|
+
writer.releaseLock();
|
|
1257
|
+
}
|
|
1258
|
+
this.processStreamResponse(
|
|
1259
|
+
{
|
|
1260
|
+
...processedParams,
|
|
1261
|
+
messages: [...messages.filter((m) => m.id !== lastMessage.id), lastMessage]
|
|
1262
|
+
},
|
|
1263
|
+
writable
|
|
1264
|
+
).catch((error) => {
|
|
1265
|
+
console.error("Error processing stream response:", error);
|
|
1266
|
+
});
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
} else {
|
|
1270
|
+
setTimeout(() => {
|
|
1271
|
+
writable.close();
|
|
1272
|
+
}, 0);
|
|
1273
|
+
}
|
|
1274
|
+
},
|
|
1275
|
+
lastMessage: void 0
|
|
1276
|
+
}).catch((error) => {
|
|
1277
|
+
console.error("Error processing stream response:", error);
|
|
1278
|
+
});
|
|
1279
|
+
} catch (error) {
|
|
1280
|
+
console.error("Error processing stream response:", error);
|
|
1281
|
+
}
|
|
171
1282
|
return response;
|
|
172
1283
|
}
|
|
173
1284
|
/**
|
|
174
1285
|
* Gets details about a specific tool available to the agent
|
|
175
1286
|
* @param toolId - ID of the tool to retrieve
|
|
1287
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
176
1288
|
* @returns Promise containing tool details
|
|
177
1289
|
*/
|
|
178
|
-
getTool(toolId) {
|
|
179
|
-
return this.request(`/api/agents/${this.agentId}/tools/${toolId}`);
|
|
1290
|
+
getTool(toolId, runtimeContext) {
|
|
1291
|
+
return this.request(`/api/agents/${this.agentId}/tools/${toolId}${runtimeContextQueryString(runtimeContext)}`);
|
|
1292
|
+
}
|
|
1293
|
+
/**
|
|
1294
|
+
* Executes a tool for the agent
|
|
1295
|
+
* @param toolId - ID of the tool to execute
|
|
1296
|
+
* @param params - Parameters required for tool execution
|
|
1297
|
+
* @returns Promise containing the tool execution results
|
|
1298
|
+
*/
|
|
1299
|
+
executeTool(toolId, params) {
|
|
1300
|
+
const body = {
|
|
1301
|
+
data: params.data,
|
|
1302
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1303
|
+
};
|
|
1304
|
+
return this.request(`/api/agents/${this.agentId}/tools/${toolId}/execute`, {
|
|
1305
|
+
method: "POST",
|
|
1306
|
+
body
|
|
1307
|
+
});
|
|
180
1308
|
}
|
|
181
1309
|
/**
|
|
182
1310
|
* Retrieves evaluation results for the agent
|
|
1311
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
183
1312
|
* @returns Promise containing agent evaluations
|
|
184
1313
|
*/
|
|
185
|
-
evals() {
|
|
186
|
-
return this.request(`/api/agents/${this.agentId}/evals/ci`);
|
|
1314
|
+
evals(runtimeContext) {
|
|
1315
|
+
return this.request(`/api/agents/${this.agentId}/evals/ci${runtimeContextQueryString(runtimeContext)}`);
|
|
187
1316
|
}
|
|
188
1317
|
/**
|
|
189
1318
|
* Retrieves live evaluation results for the agent
|
|
1319
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
190
1320
|
* @returns Promise containing live agent evaluations
|
|
191
1321
|
*/
|
|
192
|
-
liveEvals() {
|
|
193
|
-
return this.request(`/api/agents/${this.agentId}/evals/live`);
|
|
194
|
-
}
|
|
195
|
-
};
|
|
196
|
-
var Network = class extends BaseResource {
|
|
197
|
-
constructor(options, networkId) {
|
|
198
|
-
super(options);
|
|
199
|
-
this.networkId = networkId;
|
|
1322
|
+
liveEvals(runtimeContext) {
|
|
1323
|
+
return this.request(`/api/agents/${this.agentId}/evals/live${runtimeContextQueryString(runtimeContext)}`);
|
|
200
1324
|
}
|
|
201
1325
|
/**
|
|
202
|
-
*
|
|
203
|
-
* @
|
|
1326
|
+
* Updates the model for the agent
|
|
1327
|
+
* @param params - Parameters for updating the model
|
|
1328
|
+
* @returns Promise containing the updated model
|
|
204
1329
|
*/
|
|
205
|
-
|
|
206
|
-
return this.request(`/api/
|
|
1330
|
+
updateModel(params) {
|
|
1331
|
+
return this.request(`/api/agents/${this.agentId}/model`, {
|
|
1332
|
+
method: "POST",
|
|
1333
|
+
body: params
|
|
1334
|
+
});
|
|
207
1335
|
}
|
|
208
1336
|
/**
|
|
209
|
-
*
|
|
210
|
-
* @param params -
|
|
211
|
-
* @returns Promise containing the
|
|
1337
|
+
* Updates the model for the agent in the model list
|
|
1338
|
+
* @param params - Parameters for updating the model
|
|
1339
|
+
* @returns Promise containing the updated model
|
|
212
1340
|
*/
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
...params,
|
|
216
|
-
output: params.output instanceof zod.ZodSchema ? zodToJsonSchema.zodToJsonSchema(params.output) : params.output,
|
|
217
|
-
experimental_output: params.experimental_output instanceof zod.ZodSchema ? zodToJsonSchema.zodToJsonSchema(params.experimental_output) : params.experimental_output
|
|
218
|
-
};
|
|
219
|
-
return this.request(`/api/networks/${this.networkId}/generate`, {
|
|
1341
|
+
updateModelInModelList({ modelConfigId, ...params }) {
|
|
1342
|
+
return this.request(`/api/agents/${this.agentId}/models/${modelConfigId}`, {
|
|
220
1343
|
method: "POST",
|
|
221
|
-
body:
|
|
1344
|
+
body: params
|
|
222
1345
|
});
|
|
223
1346
|
}
|
|
224
1347
|
/**
|
|
225
|
-
*
|
|
226
|
-
* @param params -
|
|
227
|
-
* @returns Promise containing the
|
|
1348
|
+
* Reorders the models for the agent
|
|
1349
|
+
* @param params - Parameters for reordering the model list
|
|
1350
|
+
* @returns Promise containing the updated model list
|
|
228
1351
|
*/
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
...params,
|
|
232
|
-
output: params.output instanceof zod.ZodSchema ? zodToJsonSchema.zodToJsonSchema(params.output) : params.output,
|
|
233
|
-
experimental_output: params.experimental_output instanceof zod.ZodSchema ? zodToJsonSchema.zodToJsonSchema(params.experimental_output) : params.experimental_output
|
|
234
|
-
};
|
|
235
|
-
const response = await this.request(`/api/networks/${this.networkId}/stream`, {
|
|
1352
|
+
reorderModelList(params) {
|
|
1353
|
+
return this.request(`/api/agents/${this.agentId}/models/reorder`, {
|
|
236
1354
|
method: "POST",
|
|
237
|
-
body:
|
|
238
|
-
stream: true
|
|
1355
|
+
body: params
|
|
239
1356
|
});
|
|
240
|
-
if (!response.body) {
|
|
241
|
-
throw new Error("No response body");
|
|
242
|
-
}
|
|
243
|
-
response.processDataStream = async (options = {}) => {
|
|
244
|
-
await uiUtils.processDataStream({
|
|
245
|
-
stream: response.body,
|
|
246
|
-
...options
|
|
247
|
-
});
|
|
248
|
-
};
|
|
249
|
-
return response;
|
|
250
1357
|
}
|
|
251
1358
|
};
|
|
252
1359
|
|
|
@@ -286,10 +1393,45 @@ var MemoryThread = class extends BaseResource {
|
|
|
286
1393
|
}
|
|
287
1394
|
/**
|
|
288
1395
|
* Retrieves messages associated with the thread
|
|
1396
|
+
* @param params - Optional parameters including limit for number of messages to retrieve
|
|
289
1397
|
* @returns Promise containing thread messages and UI messages
|
|
290
1398
|
*/
|
|
291
|
-
getMessages() {
|
|
292
|
-
|
|
1399
|
+
getMessages(params) {
|
|
1400
|
+
const query = new URLSearchParams({
|
|
1401
|
+
agentId: this.agentId,
|
|
1402
|
+
...params?.limit ? { limit: params.limit.toString() } : {}
|
|
1403
|
+
});
|
|
1404
|
+
return this.request(`/api/memory/threads/${this.threadId}/messages?${query.toString()}`);
|
|
1405
|
+
}
|
|
1406
|
+
/**
|
|
1407
|
+
* Retrieves paginated messages associated with the thread with advanced filtering and selection options
|
|
1408
|
+
* @param params - Pagination parameters including selectBy criteria, page, perPage, date ranges, and message inclusion options
|
|
1409
|
+
* @returns Promise containing paginated thread messages with pagination metadata (total, page, perPage, hasMore)
|
|
1410
|
+
*/
|
|
1411
|
+
getMessagesPaginated({
|
|
1412
|
+
selectBy,
|
|
1413
|
+
...rest
|
|
1414
|
+
}) {
|
|
1415
|
+
const query = new URLSearchParams({
|
|
1416
|
+
...rest,
|
|
1417
|
+
...selectBy ? { selectBy: JSON.stringify(selectBy) } : {}
|
|
1418
|
+
});
|
|
1419
|
+
return this.request(`/api/memory/threads/${this.threadId}/messages/paginated?${query.toString()}`);
|
|
1420
|
+
}
|
|
1421
|
+
/**
|
|
1422
|
+
* Deletes one or more messages from the thread
|
|
1423
|
+
* @param messageIds - Can be a single message ID (string), array of message IDs,
|
|
1424
|
+
* message object with id property, or array of message objects
|
|
1425
|
+
* @returns Promise containing deletion result
|
|
1426
|
+
*/
|
|
1427
|
+
deleteMessages(messageIds) {
|
|
1428
|
+
const query = new URLSearchParams({
|
|
1429
|
+
agentId: this.agentId
|
|
1430
|
+
});
|
|
1431
|
+
return this.request(`/api/memory/messages/delete?${query.toString()}`, {
|
|
1432
|
+
method: "POST",
|
|
1433
|
+
body: { messageIds }
|
|
1434
|
+
});
|
|
293
1435
|
}
|
|
294
1436
|
};
|
|
295
1437
|
|
|
@@ -302,10 +1444,13 @@ var Vector = class extends BaseResource {
|
|
|
302
1444
|
/**
|
|
303
1445
|
* Retrieves details about a specific vector index
|
|
304
1446
|
* @param indexName - Name of the index to get details for
|
|
1447
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
305
1448
|
* @returns Promise containing vector index details
|
|
306
1449
|
*/
|
|
307
|
-
details(indexName) {
|
|
308
|
-
return this.request(
|
|
1450
|
+
details(indexName, runtimeContext) {
|
|
1451
|
+
return this.request(
|
|
1452
|
+
`/api/vector/${this.vectorName}/indexes/${indexName}${runtimeContextQueryString(runtimeContext)}`
|
|
1453
|
+
);
|
|
309
1454
|
}
|
|
310
1455
|
/**
|
|
311
1456
|
* Deletes a vector index
|
|
@@ -319,10 +1464,11 @@ var Vector = class extends BaseResource {
|
|
|
319
1464
|
}
|
|
320
1465
|
/**
|
|
321
1466
|
* Retrieves a list of all available indexes
|
|
1467
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
322
1468
|
* @returns Promise containing array of index names
|
|
323
1469
|
*/
|
|
324
|
-
getIndexes() {
|
|
325
|
-
return this.request(`/api/vector/${this.vectorName}/indexes`);
|
|
1470
|
+
getIndexes(runtimeContext) {
|
|
1471
|
+
return this.request(`/api/vector/${this.vectorName}/indexes${runtimeContextQueryString(runtimeContext)}`);
|
|
326
1472
|
}
|
|
327
1473
|
/**
|
|
328
1474
|
* Creates a new vector index
|
|
@@ -359,6 +1505,41 @@ var Vector = class extends BaseResource {
|
|
|
359
1505
|
}
|
|
360
1506
|
};
|
|
361
1507
|
|
|
1508
|
+
// src/resources/tool.ts
|
|
1509
|
+
var Tool = class extends BaseResource {
|
|
1510
|
+
constructor(options, toolId) {
|
|
1511
|
+
super(options);
|
|
1512
|
+
this.toolId = toolId;
|
|
1513
|
+
}
|
|
1514
|
+
/**
|
|
1515
|
+
* Retrieves details about the tool
|
|
1516
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
1517
|
+
* @returns Promise containing tool details including description and schemas
|
|
1518
|
+
*/
|
|
1519
|
+
details(runtimeContext) {
|
|
1520
|
+
return this.request(`/api/tools/${this.toolId}${runtimeContextQueryString(runtimeContext)}`);
|
|
1521
|
+
}
|
|
1522
|
+
/**
|
|
1523
|
+
* Executes the tool with the provided parameters
|
|
1524
|
+
* @param params - Parameters required for tool execution
|
|
1525
|
+
* @returns Promise containing the tool execution results
|
|
1526
|
+
*/
|
|
1527
|
+
execute(params) {
|
|
1528
|
+
const url = new URLSearchParams();
|
|
1529
|
+
if (params.runId) {
|
|
1530
|
+
url.set("runId", params.runId);
|
|
1531
|
+
}
|
|
1532
|
+
const body = {
|
|
1533
|
+
data: params.data,
|
|
1534
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1535
|
+
};
|
|
1536
|
+
return this.request(`/api/tools/${this.toolId}/execute?${url.toString()}`, {
|
|
1537
|
+
method: "POST",
|
|
1538
|
+
body
|
|
1539
|
+
});
|
|
1540
|
+
}
|
|
1541
|
+
};
|
|
1542
|
+
|
|
362
1543
|
// src/resources/workflow.ts
|
|
363
1544
|
var RECORD_SEPARATOR = "";
|
|
364
1545
|
var Workflow = class extends BaseResource {
|
|
@@ -366,105 +1547,1109 @@ var Workflow = class extends BaseResource {
|
|
|
366
1547
|
super(options);
|
|
367
1548
|
this.workflowId = workflowId;
|
|
368
1549
|
}
|
|
1550
|
+
/**
|
|
1551
|
+
* Creates an async generator that processes a readable stream and yields workflow records
|
|
1552
|
+
* separated by the Record Separator character (\x1E)
|
|
1553
|
+
*
|
|
1554
|
+
* @param stream - The readable stream to process
|
|
1555
|
+
* @returns An async generator that yields parsed records
|
|
1556
|
+
*/
|
|
1557
|
+
async *streamProcessor(stream) {
|
|
1558
|
+
const reader = stream.getReader();
|
|
1559
|
+
let doneReading = false;
|
|
1560
|
+
let buffer = "";
|
|
1561
|
+
try {
|
|
1562
|
+
while (!doneReading) {
|
|
1563
|
+
const { done, value } = await reader.read();
|
|
1564
|
+
doneReading = done;
|
|
1565
|
+
if (done && !value) continue;
|
|
1566
|
+
try {
|
|
1567
|
+
const decoded = value ? new TextDecoder().decode(value) : "";
|
|
1568
|
+
const chunks = (buffer + decoded).split(RECORD_SEPARATOR);
|
|
1569
|
+
buffer = chunks.pop() || "";
|
|
1570
|
+
for (const chunk of chunks) {
|
|
1571
|
+
if (chunk) {
|
|
1572
|
+
if (typeof chunk === "string") {
|
|
1573
|
+
try {
|
|
1574
|
+
const parsedChunk = JSON.parse(chunk);
|
|
1575
|
+
yield parsedChunk;
|
|
1576
|
+
} catch {
|
|
1577
|
+
}
|
|
1578
|
+
}
|
|
1579
|
+
}
|
|
1580
|
+
}
|
|
1581
|
+
} catch {
|
|
1582
|
+
}
|
|
1583
|
+
}
|
|
1584
|
+
if (buffer) {
|
|
1585
|
+
try {
|
|
1586
|
+
yield JSON.parse(buffer);
|
|
1587
|
+
} catch {
|
|
1588
|
+
}
|
|
1589
|
+
}
|
|
1590
|
+
} finally {
|
|
1591
|
+
reader.cancel().catch(() => {
|
|
1592
|
+
});
|
|
1593
|
+
}
|
|
1594
|
+
}
|
|
369
1595
|
/**
|
|
370
1596
|
* Retrieves details about the workflow
|
|
1597
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
371
1598
|
* @returns Promise containing workflow details including steps and graphs
|
|
372
1599
|
*/
|
|
373
|
-
details() {
|
|
374
|
-
return this.request(`/api/workflows/${this.workflowId}`);
|
|
1600
|
+
details(runtimeContext) {
|
|
1601
|
+
return this.request(`/api/workflows/${this.workflowId}${runtimeContextQueryString(runtimeContext)}`);
|
|
375
1602
|
}
|
|
376
1603
|
/**
|
|
377
|
-
*
|
|
378
|
-
*
|
|
379
|
-
* @param
|
|
380
|
-
* @returns Promise containing
|
|
1604
|
+
* Retrieves all runs for a workflow
|
|
1605
|
+
* @param params - Parameters for filtering runs
|
|
1606
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
1607
|
+
* @returns Promise containing workflow runs array
|
|
381
1608
|
*/
|
|
382
|
-
|
|
383
|
-
|
|
1609
|
+
runs(params, runtimeContext) {
|
|
1610
|
+
const runtimeContextParam = base64RuntimeContext(parseClientRuntimeContext(runtimeContext));
|
|
1611
|
+
const searchParams = new URLSearchParams();
|
|
1612
|
+
if (params?.fromDate) {
|
|
1613
|
+
searchParams.set("fromDate", params.fromDate.toISOString());
|
|
1614
|
+
}
|
|
1615
|
+
if (params?.toDate) {
|
|
1616
|
+
searchParams.set("toDate", params.toDate.toISOString());
|
|
1617
|
+
}
|
|
1618
|
+
if (params?.limit !== null && params?.limit !== void 0 && !isNaN(Number(params?.limit))) {
|
|
1619
|
+
searchParams.set("limit", String(params.limit));
|
|
1620
|
+
}
|
|
1621
|
+
if (params?.offset !== null && params?.offset !== void 0 && !isNaN(Number(params?.offset))) {
|
|
1622
|
+
searchParams.set("offset", String(params.offset));
|
|
1623
|
+
}
|
|
1624
|
+
if (params?.resourceId) {
|
|
1625
|
+
searchParams.set("resourceId", params.resourceId);
|
|
1626
|
+
}
|
|
1627
|
+
if (runtimeContextParam) {
|
|
1628
|
+
searchParams.set("runtimeContext", runtimeContextParam);
|
|
1629
|
+
}
|
|
1630
|
+
if (searchParams.size) {
|
|
1631
|
+
return this.request(`/api/workflows/${this.workflowId}/runs?${searchParams}`);
|
|
1632
|
+
} else {
|
|
1633
|
+
return this.request(`/api/workflows/${this.workflowId}/runs`);
|
|
1634
|
+
}
|
|
1635
|
+
}
|
|
1636
|
+
/**
|
|
1637
|
+
* Retrieves a specific workflow run by its ID
|
|
1638
|
+
* @param runId - The ID of the workflow run to retrieve
|
|
1639
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
1640
|
+
* @returns Promise containing the workflow run details
|
|
1641
|
+
*/
|
|
1642
|
+
runById(runId, runtimeContext) {
|
|
1643
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}${runtimeContextQueryString(runtimeContext)}`);
|
|
1644
|
+
}
|
|
1645
|
+
/**
|
|
1646
|
+
* Retrieves the execution result for a specific workflow run by its ID
|
|
1647
|
+
* @param runId - The ID of the workflow run to retrieve the execution result for
|
|
1648
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
1649
|
+
* @returns Promise containing the workflow run execution result
|
|
1650
|
+
*/
|
|
1651
|
+
runExecutionResult(runId, runtimeContext) {
|
|
1652
|
+
return this.request(
|
|
1653
|
+
`/api/workflows/${this.workflowId}/runs/${runId}/execution-result${runtimeContextQueryString(runtimeContext)}`
|
|
1654
|
+
);
|
|
1655
|
+
}
|
|
1656
|
+
/**
|
|
1657
|
+
* Cancels a specific workflow run by its ID
|
|
1658
|
+
* @param runId - The ID of the workflow run to cancel
|
|
1659
|
+
* @returns Promise containing a success message
|
|
1660
|
+
*/
|
|
1661
|
+
cancelRun(runId) {
|
|
1662
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}/cancel`, {
|
|
1663
|
+
method: "POST"
|
|
1664
|
+
});
|
|
1665
|
+
}
|
|
1666
|
+
/**
|
|
1667
|
+
* Sends an event to a specific workflow run by its ID
|
|
1668
|
+
* @param params - Object containing the runId, event and data
|
|
1669
|
+
* @returns Promise containing a success message
|
|
1670
|
+
*/
|
|
1671
|
+
sendRunEvent(params) {
|
|
1672
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${params.runId}/send-event`, {
|
|
384
1673
|
method: "POST",
|
|
385
|
-
body: params
|
|
1674
|
+
body: { event: params.event, data: params.data }
|
|
386
1675
|
});
|
|
387
1676
|
}
|
|
1677
|
+
/**
|
|
1678
|
+
* @deprecated Use createRunAsync() instead.
|
|
1679
|
+
* @throws {Error} Always throws an error directing users to use createRunAsync()
|
|
1680
|
+
*/
|
|
1681
|
+
async createRun(_params) {
|
|
1682
|
+
throw new Error(
|
|
1683
|
+
"createRun() has been deprecated. Please use createRunAsync() instead.\n\nMigration guide:\n Before: const run = workflow.createRun();\n After: const run = await workflow.createRunAsync();\n\nNote: createRunAsync() is an async method, so make sure your calling function is async."
|
|
1684
|
+
);
|
|
1685
|
+
}
|
|
388
1686
|
/**
|
|
389
1687
|
* Creates a new workflow run
|
|
390
|
-
* @
|
|
1688
|
+
* @param params - Optional object containing the optional runId
|
|
1689
|
+
* @returns Promise containing the runId of the created run with methods to control execution
|
|
391
1690
|
*/
|
|
392
|
-
|
|
1691
|
+
async createRunAsync(params) {
|
|
393
1692
|
const searchParams = new URLSearchParams();
|
|
394
1693
|
if (!!params?.runId) {
|
|
395
1694
|
searchParams.set("runId", params.runId);
|
|
396
1695
|
}
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
1696
|
+
const res = await this.request(
|
|
1697
|
+
`/api/workflows/${this.workflowId}/create-run?${searchParams.toString()}`,
|
|
1698
|
+
{
|
|
1699
|
+
method: "POST"
|
|
1700
|
+
}
|
|
1701
|
+
);
|
|
1702
|
+
const runId = res.runId;
|
|
1703
|
+
return {
|
|
1704
|
+
runId,
|
|
1705
|
+
start: async (p) => {
|
|
1706
|
+
return this.start({ runId, inputData: p.inputData, runtimeContext: p.runtimeContext });
|
|
1707
|
+
},
|
|
1708
|
+
startAsync: async (p) => {
|
|
1709
|
+
return this.startAsync({ runId, inputData: p.inputData, runtimeContext: p.runtimeContext });
|
|
1710
|
+
},
|
|
1711
|
+
watch: async (onRecord) => {
|
|
1712
|
+
return this.watch({ runId }, onRecord);
|
|
1713
|
+
},
|
|
1714
|
+
stream: async (p) => {
|
|
1715
|
+
return this.stream({ runId, inputData: p.inputData, runtimeContext: p.runtimeContext });
|
|
1716
|
+
},
|
|
1717
|
+
resume: async (p) => {
|
|
1718
|
+
return this.resume({ runId, step: p.step, resumeData: p.resumeData, runtimeContext: p.runtimeContext });
|
|
1719
|
+
},
|
|
1720
|
+
resumeAsync: async (p) => {
|
|
1721
|
+
return this.resumeAsync({ runId, step: p.step, resumeData: p.resumeData, runtimeContext: p.runtimeContext });
|
|
1722
|
+
},
|
|
1723
|
+
resumeStreamVNext: async (p) => {
|
|
1724
|
+
return this.resumeStreamVNext({
|
|
1725
|
+
runId,
|
|
1726
|
+
step: p.step,
|
|
1727
|
+
resumeData: p.resumeData,
|
|
1728
|
+
runtimeContext: p.runtimeContext
|
|
1729
|
+
});
|
|
1730
|
+
}
|
|
1731
|
+
};
|
|
400
1732
|
}
|
|
401
1733
|
/**
|
|
402
1734
|
* Starts a workflow run synchronously without waiting for the workflow to complete
|
|
403
|
-
* @param params - Object containing the runId and
|
|
1735
|
+
* @param params - Object containing the runId, inputData and runtimeContext
|
|
404
1736
|
* @returns Promise containing success message
|
|
405
1737
|
*/
|
|
406
1738
|
start(params) {
|
|
1739
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
407
1740
|
return this.request(`/api/workflows/${this.workflowId}/start?runId=${params.runId}`, {
|
|
408
1741
|
method: "POST",
|
|
409
|
-
body: params?.
|
|
1742
|
+
body: { inputData: params?.inputData, runtimeContext }
|
|
1743
|
+
});
|
|
1744
|
+
}
|
|
1745
|
+
/**
|
|
1746
|
+
* Resumes a suspended workflow step synchronously without waiting for the workflow to complete
|
|
1747
|
+
* @param params - Object containing the runId, step, resumeData and runtimeContext
|
|
1748
|
+
* @returns Promise containing success message
|
|
1749
|
+
*/
|
|
1750
|
+
resume({
|
|
1751
|
+
step,
|
|
1752
|
+
runId,
|
|
1753
|
+
resumeData,
|
|
1754
|
+
...rest
|
|
1755
|
+
}) {
|
|
1756
|
+
const runtimeContext = parseClientRuntimeContext(rest.runtimeContext);
|
|
1757
|
+
return this.request(`/api/workflows/${this.workflowId}/resume?runId=${runId}`, {
|
|
1758
|
+
method: "POST",
|
|
1759
|
+
body: {
|
|
1760
|
+
step,
|
|
1761
|
+
resumeData,
|
|
1762
|
+
runtimeContext
|
|
1763
|
+
}
|
|
1764
|
+
});
|
|
1765
|
+
}
|
|
1766
|
+
/**
|
|
1767
|
+
* Starts a workflow run asynchronously and returns a promise that resolves when the workflow is complete
|
|
1768
|
+
* @param params - Object containing the optional runId, inputData and runtimeContext
|
|
1769
|
+
* @returns Promise containing the workflow execution results
|
|
1770
|
+
*/
|
|
1771
|
+
startAsync(params) {
|
|
1772
|
+
const searchParams = new URLSearchParams();
|
|
1773
|
+
if (!!params?.runId) {
|
|
1774
|
+
searchParams.set("runId", params.runId);
|
|
1775
|
+
}
|
|
1776
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1777
|
+
return this.request(`/api/workflows/${this.workflowId}/start-async?${searchParams.toString()}`, {
|
|
1778
|
+
method: "POST",
|
|
1779
|
+
body: { inputData: params.inputData, runtimeContext }
|
|
1780
|
+
});
|
|
1781
|
+
}
|
|
1782
|
+
/**
|
|
1783
|
+
* Starts a workflow run and returns a stream
|
|
1784
|
+
* @param params - Object containing the optional runId, inputData and runtimeContext
|
|
1785
|
+
* @returns Promise containing the workflow execution results
|
|
1786
|
+
*/
|
|
1787
|
+
async stream(params) {
|
|
1788
|
+
const searchParams = new URLSearchParams();
|
|
1789
|
+
if (!!params?.runId) {
|
|
1790
|
+
searchParams.set("runId", params.runId);
|
|
1791
|
+
}
|
|
1792
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1793
|
+
const response = await this.request(
|
|
1794
|
+
`/api/workflows/${this.workflowId}/stream?${searchParams.toString()}`,
|
|
1795
|
+
{
|
|
1796
|
+
method: "POST",
|
|
1797
|
+
body: { inputData: params.inputData, runtimeContext },
|
|
1798
|
+
stream: true
|
|
1799
|
+
}
|
|
1800
|
+
);
|
|
1801
|
+
if (!response.ok) {
|
|
1802
|
+
throw new Error(`Failed to stream workflow: ${response.statusText}`);
|
|
1803
|
+
}
|
|
1804
|
+
if (!response.body) {
|
|
1805
|
+
throw new Error("Response body is null");
|
|
1806
|
+
}
|
|
1807
|
+
let failedChunk = void 0;
|
|
1808
|
+
const transformStream = new TransformStream({
|
|
1809
|
+
start() {
|
|
1810
|
+
},
|
|
1811
|
+
async transform(chunk, controller) {
|
|
1812
|
+
try {
|
|
1813
|
+
const decoded = new TextDecoder().decode(chunk);
|
|
1814
|
+
const chunks = decoded.split(RECORD_SEPARATOR);
|
|
1815
|
+
for (const chunk2 of chunks) {
|
|
1816
|
+
if (chunk2) {
|
|
1817
|
+
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
1818
|
+
try {
|
|
1819
|
+
const parsedChunk = JSON.parse(newChunk);
|
|
1820
|
+
controller.enqueue(parsedChunk);
|
|
1821
|
+
failedChunk = void 0;
|
|
1822
|
+
} catch {
|
|
1823
|
+
failedChunk = newChunk;
|
|
1824
|
+
}
|
|
1825
|
+
}
|
|
1826
|
+
}
|
|
1827
|
+
} catch {
|
|
1828
|
+
}
|
|
1829
|
+
}
|
|
1830
|
+
});
|
|
1831
|
+
return response.body.pipeThrough(transformStream);
|
|
1832
|
+
}
|
|
1833
|
+
/**
|
|
1834
|
+
* Observes workflow stream for a workflow run
|
|
1835
|
+
* @param params - Object containing the runId
|
|
1836
|
+
* @returns Promise containing the workflow execution results
|
|
1837
|
+
*/
|
|
1838
|
+
async observeStream(params) {
|
|
1839
|
+
const searchParams = new URLSearchParams();
|
|
1840
|
+
searchParams.set("runId", params.runId);
|
|
1841
|
+
const response = await this.request(
|
|
1842
|
+
`/api/workflows/${this.workflowId}/observe-stream?${searchParams.toString()}`,
|
|
1843
|
+
{
|
|
1844
|
+
method: "POST",
|
|
1845
|
+
stream: true
|
|
1846
|
+
}
|
|
1847
|
+
);
|
|
1848
|
+
if (!response.ok) {
|
|
1849
|
+
throw new Error(`Failed to observe workflow stream: ${response.statusText}`);
|
|
1850
|
+
}
|
|
1851
|
+
if (!response.body) {
|
|
1852
|
+
throw new Error("Response body is null");
|
|
1853
|
+
}
|
|
1854
|
+
let failedChunk = void 0;
|
|
1855
|
+
const transformStream = new TransformStream({
|
|
1856
|
+
start() {
|
|
1857
|
+
},
|
|
1858
|
+
async transform(chunk, controller) {
|
|
1859
|
+
try {
|
|
1860
|
+
const decoded = new TextDecoder().decode(chunk);
|
|
1861
|
+
const chunks = decoded.split(RECORD_SEPARATOR);
|
|
1862
|
+
for (const chunk2 of chunks) {
|
|
1863
|
+
if (chunk2) {
|
|
1864
|
+
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
1865
|
+
try {
|
|
1866
|
+
const parsedChunk = JSON.parse(newChunk);
|
|
1867
|
+
controller.enqueue(parsedChunk);
|
|
1868
|
+
failedChunk = void 0;
|
|
1869
|
+
} catch {
|
|
1870
|
+
failedChunk = newChunk;
|
|
1871
|
+
}
|
|
1872
|
+
}
|
|
1873
|
+
}
|
|
1874
|
+
} catch {
|
|
1875
|
+
}
|
|
1876
|
+
}
|
|
1877
|
+
});
|
|
1878
|
+
return response.body.pipeThrough(transformStream);
|
|
1879
|
+
}
|
|
1880
|
+
/**
|
|
1881
|
+
* Starts a workflow run and returns a stream
|
|
1882
|
+
* @param params - Object containing the optional runId, inputData and runtimeContext
|
|
1883
|
+
* @returns Promise containing the workflow execution results
|
|
1884
|
+
*/
|
|
1885
|
+
async streamVNext(params) {
|
|
1886
|
+
const searchParams = new URLSearchParams();
|
|
1887
|
+
if (!!params?.runId) {
|
|
1888
|
+
searchParams.set("runId", params.runId);
|
|
1889
|
+
}
|
|
1890
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1891
|
+
const response = await this.request(
|
|
1892
|
+
`/api/workflows/${this.workflowId}/streamVNext?${searchParams.toString()}`,
|
|
1893
|
+
{
|
|
1894
|
+
method: "POST",
|
|
1895
|
+
body: { inputData: params.inputData, runtimeContext, closeOnSuspend: params.closeOnSuspend },
|
|
1896
|
+
stream: true
|
|
1897
|
+
}
|
|
1898
|
+
);
|
|
1899
|
+
if (!response.ok) {
|
|
1900
|
+
throw new Error(`Failed to stream vNext workflow: ${response.statusText}`);
|
|
1901
|
+
}
|
|
1902
|
+
if (!response.body) {
|
|
1903
|
+
throw new Error("Response body is null");
|
|
1904
|
+
}
|
|
1905
|
+
let failedChunk = void 0;
|
|
1906
|
+
const transformStream = new TransformStream({
|
|
1907
|
+
start() {
|
|
1908
|
+
},
|
|
1909
|
+
async transform(chunk, controller) {
|
|
1910
|
+
try {
|
|
1911
|
+
const decoded = new TextDecoder().decode(chunk);
|
|
1912
|
+
const chunks = decoded.split(RECORD_SEPARATOR);
|
|
1913
|
+
for (const chunk2 of chunks) {
|
|
1914
|
+
if (chunk2) {
|
|
1915
|
+
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
1916
|
+
try {
|
|
1917
|
+
const parsedChunk = JSON.parse(newChunk);
|
|
1918
|
+
controller.enqueue(parsedChunk);
|
|
1919
|
+
failedChunk = void 0;
|
|
1920
|
+
} catch {
|
|
1921
|
+
failedChunk = newChunk;
|
|
1922
|
+
}
|
|
1923
|
+
}
|
|
1924
|
+
}
|
|
1925
|
+
} catch {
|
|
1926
|
+
}
|
|
1927
|
+
}
|
|
1928
|
+
});
|
|
1929
|
+
return response.body.pipeThrough(transformStream);
|
|
1930
|
+
}
|
|
1931
|
+
/**
|
|
1932
|
+
* Resumes a suspended workflow step asynchronously and returns a promise that resolves when the workflow is complete
|
|
1933
|
+
* @param params - Object containing the runId, step, resumeData and runtimeContext
|
|
1934
|
+
* @returns Promise containing the workflow resume results
|
|
1935
|
+
*/
|
|
1936
|
+
resumeAsync(params) {
|
|
1937
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1938
|
+
return this.request(`/api/workflows/${this.workflowId}/resume-async?runId=${params.runId}`, {
|
|
1939
|
+
method: "POST",
|
|
1940
|
+
body: {
|
|
1941
|
+
step: params.step,
|
|
1942
|
+
resumeData: params.resumeData,
|
|
1943
|
+
runtimeContext
|
|
1944
|
+
}
|
|
1945
|
+
});
|
|
1946
|
+
}
|
|
1947
|
+
/**
|
|
1948
|
+
* Resumes a suspended workflow step that uses streamVNext asynchronously and returns a promise that resolves when the workflow is complete
|
|
1949
|
+
* @param params - Object containing the runId, step, resumeData and runtimeContext
|
|
1950
|
+
* @returns Promise containing the workflow resume results
|
|
1951
|
+
*/
|
|
1952
|
+
resumeStreamVNext(params) {
|
|
1953
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1954
|
+
return this.request(`/api/workflows/${this.workflowId}/resume-stream?runId=${params.runId}`, {
|
|
1955
|
+
method: "POST",
|
|
1956
|
+
body: {
|
|
1957
|
+
step: params.step,
|
|
1958
|
+
resumeData: params.resumeData,
|
|
1959
|
+
runtimeContext
|
|
1960
|
+
}
|
|
1961
|
+
});
|
|
1962
|
+
}
|
|
1963
|
+
/**
|
|
1964
|
+
* Watches workflow transitions in real-time
|
|
1965
|
+
* @param runId - Optional run ID to filter the watch stream
|
|
1966
|
+
* @returns AsyncGenerator that yields parsed records from the workflow watch stream
|
|
1967
|
+
*/
|
|
1968
|
+
async watch({ runId }, onRecord) {
|
|
1969
|
+
const response = await this.request(`/api/workflows/${this.workflowId}/watch?runId=${runId}`, {
|
|
1970
|
+
stream: true
|
|
1971
|
+
});
|
|
1972
|
+
if (!response.ok) {
|
|
1973
|
+
throw new Error(`Failed to watch workflow: ${response.statusText}`);
|
|
1974
|
+
}
|
|
1975
|
+
if (!response.body) {
|
|
1976
|
+
throw new Error("Response body is null");
|
|
1977
|
+
}
|
|
1978
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
1979
|
+
if (typeof record === "string") {
|
|
1980
|
+
onRecord(JSON.parse(record));
|
|
1981
|
+
} else {
|
|
1982
|
+
onRecord(record);
|
|
1983
|
+
}
|
|
1984
|
+
}
|
|
1985
|
+
}
|
|
1986
|
+
/**
|
|
1987
|
+
* Creates a new ReadableStream from an iterable or async iterable of objects,
|
|
1988
|
+
* serializing each as JSON and separating them with the record separator (\x1E).
|
|
1989
|
+
*
|
|
1990
|
+
* @param records - An iterable or async iterable of objects to stream
|
|
1991
|
+
* @returns A ReadableStream emitting the records as JSON strings separated by the record separator
|
|
1992
|
+
*/
|
|
1993
|
+
static createRecordStream(records) {
|
|
1994
|
+
const encoder = new TextEncoder();
|
|
1995
|
+
return new ReadableStream({
|
|
1996
|
+
async start(controller) {
|
|
1997
|
+
try {
|
|
1998
|
+
for await (const record of records) {
|
|
1999
|
+
const json = JSON.stringify(record) + RECORD_SEPARATOR;
|
|
2000
|
+
controller.enqueue(encoder.encode(json));
|
|
2001
|
+
}
|
|
2002
|
+
controller.close();
|
|
2003
|
+
} catch (err) {
|
|
2004
|
+
controller.error(err);
|
|
2005
|
+
}
|
|
2006
|
+
}
|
|
2007
|
+
});
|
|
2008
|
+
}
|
|
2009
|
+
};
|
|
2010
|
+
|
|
2011
|
+
// src/resources/a2a.ts
|
|
2012
|
+
var A2A = class extends BaseResource {
|
|
2013
|
+
constructor(options, agentId) {
|
|
2014
|
+
super(options);
|
|
2015
|
+
this.agentId = agentId;
|
|
2016
|
+
}
|
|
2017
|
+
/**
|
|
2018
|
+
* Get the agent card with metadata about the agent
|
|
2019
|
+
* @returns Promise containing the agent card information
|
|
2020
|
+
*/
|
|
2021
|
+
async getCard() {
|
|
2022
|
+
return this.request(`/.well-known/${this.agentId}/agent-card.json`);
|
|
2023
|
+
}
|
|
2024
|
+
/**
|
|
2025
|
+
* Send a message to the agent and gets a message or task response
|
|
2026
|
+
* @param params - Parameters for the task
|
|
2027
|
+
* @returns Promise containing the response
|
|
2028
|
+
*/
|
|
2029
|
+
async sendMessage(params) {
|
|
2030
|
+
const response = await this.request(`/a2a/${this.agentId}`, {
|
|
2031
|
+
method: "POST",
|
|
2032
|
+
body: {
|
|
2033
|
+
method: "message/send",
|
|
2034
|
+
params
|
|
2035
|
+
}
|
|
2036
|
+
});
|
|
2037
|
+
return response;
|
|
2038
|
+
}
|
|
2039
|
+
/**
|
|
2040
|
+
* Sends a message to an agent to initiate/continue a task and subscribes
|
|
2041
|
+
* the client to real-time updates for that task via Server-Sent Events (SSE).
|
|
2042
|
+
* @param params - Parameters for the task
|
|
2043
|
+
* @returns A stream of Server-Sent Events. Each SSE `data` field contains a `SendStreamingMessageResponse`
|
|
2044
|
+
*/
|
|
2045
|
+
async sendStreamingMessage(params) {
|
|
2046
|
+
const response = await this.request(`/a2a/${this.agentId}`, {
|
|
2047
|
+
method: "POST",
|
|
2048
|
+
body: {
|
|
2049
|
+
method: "message/stream",
|
|
2050
|
+
params
|
|
2051
|
+
}
|
|
2052
|
+
});
|
|
2053
|
+
return response;
|
|
2054
|
+
}
|
|
2055
|
+
/**
|
|
2056
|
+
* Get the status and result of a task
|
|
2057
|
+
* @param params - Parameters for querying the task
|
|
2058
|
+
* @returns Promise containing the task response
|
|
2059
|
+
*/
|
|
2060
|
+
async getTask(params) {
|
|
2061
|
+
const response = await this.request(`/a2a/${this.agentId}`, {
|
|
2062
|
+
method: "POST",
|
|
2063
|
+
body: {
|
|
2064
|
+
method: "tasks/get",
|
|
2065
|
+
params
|
|
2066
|
+
}
|
|
2067
|
+
});
|
|
2068
|
+
return response;
|
|
2069
|
+
}
|
|
2070
|
+
/**
|
|
2071
|
+
* Cancel a running task
|
|
2072
|
+
* @param params - Parameters identifying the task to cancel
|
|
2073
|
+
* @returns Promise containing the task response
|
|
2074
|
+
*/
|
|
2075
|
+
async cancelTask(params) {
|
|
2076
|
+
return this.request(`/a2a/${this.agentId}`, {
|
|
2077
|
+
method: "POST",
|
|
2078
|
+
body: {
|
|
2079
|
+
method: "tasks/cancel",
|
|
2080
|
+
params
|
|
2081
|
+
}
|
|
2082
|
+
});
|
|
2083
|
+
}
|
|
2084
|
+
};
|
|
2085
|
+
|
|
2086
|
+
// src/resources/mcp-tool.ts
|
|
2087
|
+
var MCPTool = class extends BaseResource {
|
|
2088
|
+
serverId;
|
|
2089
|
+
toolId;
|
|
2090
|
+
constructor(options, serverId, toolId) {
|
|
2091
|
+
super(options);
|
|
2092
|
+
this.serverId = serverId;
|
|
2093
|
+
this.toolId = toolId;
|
|
2094
|
+
}
|
|
2095
|
+
/**
|
|
2096
|
+
* Retrieves details about this specific tool from the MCP server.
|
|
2097
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
2098
|
+
* @returns Promise containing the tool's information (name, description, schema).
|
|
2099
|
+
*/
|
|
2100
|
+
details(runtimeContext) {
|
|
2101
|
+
return this.request(`/api/mcp/${this.serverId}/tools/${this.toolId}${runtimeContextQueryString(runtimeContext)}`);
|
|
2102
|
+
}
|
|
2103
|
+
/**
|
|
2104
|
+
* Executes this specific tool on the MCP server.
|
|
2105
|
+
* @param params - Parameters for tool execution, including data/args and optional runtimeContext.
|
|
2106
|
+
* @returns Promise containing the result of the tool execution.
|
|
2107
|
+
*/
|
|
2108
|
+
execute(params) {
|
|
2109
|
+
const body = {};
|
|
2110
|
+
if (params.data !== void 0) body.data = params.data;
|
|
2111
|
+
if (params.runtimeContext !== void 0) {
|
|
2112
|
+
body.runtimeContext = params.runtimeContext;
|
|
2113
|
+
}
|
|
2114
|
+
return this.request(`/api/mcp/${this.serverId}/tools/${this.toolId}/execute`, {
|
|
2115
|
+
method: "POST",
|
|
2116
|
+
body: Object.keys(body).length > 0 ? body : void 0
|
|
2117
|
+
});
|
|
2118
|
+
}
|
|
2119
|
+
};
|
|
2120
|
+
|
|
2121
|
+
// src/resources/agent-builder.ts
|
|
2122
|
+
var RECORD_SEPARATOR2 = "";
|
|
2123
|
+
var AgentBuilder = class extends BaseResource {
|
|
2124
|
+
constructor(options, actionId) {
|
|
2125
|
+
super(options);
|
|
2126
|
+
this.actionId = actionId;
|
|
2127
|
+
}
|
|
2128
|
+
// Helper function to transform workflow result to action result
|
|
2129
|
+
transformWorkflowResult(result) {
|
|
2130
|
+
if (result.status === "success") {
|
|
2131
|
+
return {
|
|
2132
|
+
success: result.result.success || false,
|
|
2133
|
+
applied: result.result.applied || false,
|
|
2134
|
+
branchName: result.result.branchName,
|
|
2135
|
+
message: result.result.message || "Agent builder action completed",
|
|
2136
|
+
validationResults: result.result.validationResults,
|
|
2137
|
+
error: result.result.error,
|
|
2138
|
+
errors: result.result.errors,
|
|
2139
|
+
stepResults: result.result.stepResults
|
|
2140
|
+
};
|
|
2141
|
+
} else if (result.status === "failed") {
|
|
2142
|
+
return {
|
|
2143
|
+
success: false,
|
|
2144
|
+
applied: false,
|
|
2145
|
+
message: `Agent builder action failed: ${result.error.message}`,
|
|
2146
|
+
error: result.error.message
|
|
2147
|
+
};
|
|
2148
|
+
} else {
|
|
2149
|
+
return {
|
|
2150
|
+
success: false,
|
|
2151
|
+
applied: false,
|
|
2152
|
+
message: "Agent builder action was suspended",
|
|
2153
|
+
error: "Workflow suspended - manual intervention required"
|
|
2154
|
+
};
|
|
2155
|
+
}
|
|
2156
|
+
}
|
|
2157
|
+
/**
|
|
2158
|
+
* @deprecated Use createRunAsync() instead.
|
|
2159
|
+
* @throws {Error} Always throws an error directing users to use createRunAsync()
|
|
2160
|
+
*/
|
|
2161
|
+
async createRun(_params) {
|
|
2162
|
+
throw new Error(
|
|
2163
|
+
"createRun() has been deprecated. Please use createRunAsync() instead.\n\nMigration guide:\n Before: const run = agentBuilder.createRun();\n After: const run = await agentBuilder.createRunAsync();\n\nNote: createRunAsync() is an async method, so make sure your calling function is async."
|
|
2164
|
+
);
|
|
2165
|
+
}
|
|
2166
|
+
/**
|
|
2167
|
+
* Creates a new agent builder action run and returns the runId.
|
|
2168
|
+
* This calls `/api/agent-builder/:actionId/create-run`.
|
|
2169
|
+
*/
|
|
2170
|
+
async createRunAsync(params) {
|
|
2171
|
+
const searchParams = new URLSearchParams();
|
|
2172
|
+
if (!!params?.runId) {
|
|
2173
|
+
searchParams.set("runId", params.runId);
|
|
2174
|
+
}
|
|
2175
|
+
const url = `/api/agent-builder/${this.actionId}/create-run${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2176
|
+
return this.request(url, {
|
|
2177
|
+
method: "POST"
|
|
2178
|
+
});
|
|
2179
|
+
}
|
|
2180
|
+
/**
|
|
2181
|
+
* Starts agent builder action asynchronously and waits for completion.
|
|
2182
|
+
* This calls `/api/agent-builder/:actionId/start-async`.
|
|
2183
|
+
*/
|
|
2184
|
+
async startAsync(params, runId) {
|
|
2185
|
+
const searchParams = new URLSearchParams();
|
|
2186
|
+
if (runId) {
|
|
2187
|
+
searchParams.set("runId", runId);
|
|
2188
|
+
}
|
|
2189
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2190
|
+
const { runtimeContext: _, ...actionParams } = params;
|
|
2191
|
+
const url = `/api/agent-builder/${this.actionId}/start-async${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2192
|
+
const result = await this.request(url, {
|
|
2193
|
+
method: "POST",
|
|
2194
|
+
body: { ...actionParams, runtimeContext }
|
|
2195
|
+
});
|
|
2196
|
+
return this.transformWorkflowResult(result);
|
|
2197
|
+
}
|
|
2198
|
+
/**
|
|
2199
|
+
* Starts an existing agent builder action run.
|
|
2200
|
+
* This calls `/api/agent-builder/:actionId/start`.
|
|
2201
|
+
*/
|
|
2202
|
+
async startActionRun(params, runId) {
|
|
2203
|
+
const searchParams = new URLSearchParams();
|
|
2204
|
+
searchParams.set("runId", runId);
|
|
2205
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2206
|
+
const { runtimeContext: _, ...actionParams } = params;
|
|
2207
|
+
const url = `/api/agent-builder/${this.actionId}/start?${searchParams.toString()}`;
|
|
2208
|
+
return this.request(url, {
|
|
2209
|
+
method: "POST",
|
|
2210
|
+
body: { ...actionParams, runtimeContext }
|
|
2211
|
+
});
|
|
2212
|
+
}
|
|
2213
|
+
/**
|
|
2214
|
+
* Resumes a suspended agent builder action step.
|
|
2215
|
+
* This calls `/api/agent-builder/:actionId/resume`.
|
|
2216
|
+
*/
|
|
2217
|
+
async resume(params, runId) {
|
|
2218
|
+
const searchParams = new URLSearchParams();
|
|
2219
|
+
searchParams.set("runId", runId);
|
|
2220
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2221
|
+
const { runtimeContext: _, ...resumeParams } = params;
|
|
2222
|
+
const url = `/api/agent-builder/${this.actionId}/resume?${searchParams.toString()}`;
|
|
2223
|
+
return this.request(url, {
|
|
2224
|
+
method: "POST",
|
|
2225
|
+
body: { ...resumeParams, runtimeContext }
|
|
2226
|
+
});
|
|
2227
|
+
}
|
|
2228
|
+
/**
|
|
2229
|
+
* Resumes a suspended agent builder action step asynchronously.
|
|
2230
|
+
* This calls `/api/agent-builder/:actionId/resume-async`.
|
|
2231
|
+
*/
|
|
2232
|
+
async resumeAsync(params, runId) {
|
|
2233
|
+
const searchParams = new URLSearchParams();
|
|
2234
|
+
searchParams.set("runId", runId);
|
|
2235
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2236
|
+
const { runtimeContext: _, ...resumeParams } = params;
|
|
2237
|
+
const url = `/api/agent-builder/${this.actionId}/resume-async?${searchParams.toString()}`;
|
|
2238
|
+
const result = await this.request(url, {
|
|
2239
|
+
method: "POST",
|
|
2240
|
+
body: { ...resumeParams, runtimeContext }
|
|
2241
|
+
});
|
|
2242
|
+
return this.transformWorkflowResult(result);
|
|
2243
|
+
}
|
|
2244
|
+
/**
|
|
2245
|
+
* Creates an async generator that processes a readable stream and yields action records
|
|
2246
|
+
* separated by the Record Separator character (\x1E)
|
|
2247
|
+
*
|
|
2248
|
+
* @param stream - The readable stream to process
|
|
2249
|
+
* @returns An async generator that yields parsed records
|
|
2250
|
+
*/
|
|
2251
|
+
async *streamProcessor(stream) {
|
|
2252
|
+
const reader = stream.getReader();
|
|
2253
|
+
let doneReading = false;
|
|
2254
|
+
let buffer = "";
|
|
2255
|
+
try {
|
|
2256
|
+
while (!doneReading) {
|
|
2257
|
+
const { done, value } = await reader.read();
|
|
2258
|
+
doneReading = done;
|
|
2259
|
+
if (done && !value) continue;
|
|
2260
|
+
try {
|
|
2261
|
+
const decoded = value ? new TextDecoder().decode(value) : "";
|
|
2262
|
+
const chunks = (buffer + decoded).split(RECORD_SEPARATOR2);
|
|
2263
|
+
buffer = chunks.pop() || "";
|
|
2264
|
+
for (const chunk of chunks) {
|
|
2265
|
+
if (chunk) {
|
|
2266
|
+
if (typeof chunk === "string") {
|
|
2267
|
+
try {
|
|
2268
|
+
const parsedChunk = JSON.parse(chunk);
|
|
2269
|
+
yield parsedChunk;
|
|
2270
|
+
} catch {
|
|
2271
|
+
}
|
|
2272
|
+
}
|
|
2273
|
+
}
|
|
2274
|
+
}
|
|
2275
|
+
} catch {
|
|
2276
|
+
}
|
|
2277
|
+
}
|
|
2278
|
+
if (buffer) {
|
|
2279
|
+
try {
|
|
2280
|
+
yield JSON.parse(buffer);
|
|
2281
|
+
} catch {
|
|
2282
|
+
}
|
|
2283
|
+
}
|
|
2284
|
+
} finally {
|
|
2285
|
+
reader.cancel().catch(() => {
|
|
2286
|
+
});
|
|
2287
|
+
}
|
|
2288
|
+
}
|
|
2289
|
+
/**
|
|
2290
|
+
* Streams agent builder action progress in real-time.
|
|
2291
|
+
* This calls `/api/agent-builder/:actionId/stream`.
|
|
2292
|
+
*/
|
|
2293
|
+
async stream(params, runId) {
|
|
2294
|
+
const searchParams = new URLSearchParams();
|
|
2295
|
+
if (runId) {
|
|
2296
|
+
searchParams.set("runId", runId);
|
|
2297
|
+
}
|
|
2298
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2299
|
+
const { runtimeContext: _, ...actionParams } = params;
|
|
2300
|
+
const url = `/api/agent-builder/${this.actionId}/stream${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2301
|
+
const response = await this.request(url, {
|
|
2302
|
+
method: "POST",
|
|
2303
|
+
body: { ...actionParams, runtimeContext },
|
|
2304
|
+
stream: true
|
|
2305
|
+
});
|
|
2306
|
+
if (!response.ok) {
|
|
2307
|
+
throw new Error(`Failed to stream agent builder action: ${response.statusText}`);
|
|
2308
|
+
}
|
|
2309
|
+
if (!response.body) {
|
|
2310
|
+
throw new Error("Response body is null");
|
|
2311
|
+
}
|
|
2312
|
+
let failedChunk = void 0;
|
|
2313
|
+
const transformStream = new TransformStream({
|
|
2314
|
+
start() {
|
|
2315
|
+
},
|
|
2316
|
+
async transform(chunk, controller) {
|
|
2317
|
+
try {
|
|
2318
|
+
const decoded = new TextDecoder().decode(chunk);
|
|
2319
|
+
const chunks = decoded.split(RECORD_SEPARATOR2);
|
|
2320
|
+
for (const chunk2 of chunks) {
|
|
2321
|
+
if (chunk2) {
|
|
2322
|
+
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
2323
|
+
try {
|
|
2324
|
+
const parsedChunk = JSON.parse(newChunk);
|
|
2325
|
+
controller.enqueue(parsedChunk);
|
|
2326
|
+
failedChunk = void 0;
|
|
2327
|
+
} catch {
|
|
2328
|
+
failedChunk = newChunk;
|
|
2329
|
+
}
|
|
2330
|
+
}
|
|
2331
|
+
}
|
|
2332
|
+
} catch {
|
|
2333
|
+
}
|
|
2334
|
+
}
|
|
2335
|
+
});
|
|
2336
|
+
return response.body.pipeThrough(transformStream);
|
|
2337
|
+
}
|
|
2338
|
+
/**
|
|
2339
|
+
* Streams agent builder action progress in real-time using VNext streaming.
|
|
2340
|
+
* This calls `/api/agent-builder/:actionId/streamVNext`.
|
|
2341
|
+
*/
|
|
2342
|
+
async streamVNext(params, runId) {
|
|
2343
|
+
const searchParams = new URLSearchParams();
|
|
2344
|
+
if (runId) {
|
|
2345
|
+
searchParams.set("runId", runId);
|
|
2346
|
+
}
|
|
2347
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
2348
|
+
const { runtimeContext: _, ...actionParams } = params;
|
|
2349
|
+
const url = `/api/agent-builder/${this.actionId}/streamVNext${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2350
|
+
const response = await this.request(url, {
|
|
2351
|
+
method: "POST",
|
|
2352
|
+
body: { ...actionParams, runtimeContext },
|
|
2353
|
+
stream: true
|
|
2354
|
+
});
|
|
2355
|
+
if (!response.ok) {
|
|
2356
|
+
throw new Error(`Failed to stream agent builder action VNext: ${response.statusText}`);
|
|
2357
|
+
}
|
|
2358
|
+
if (!response.body) {
|
|
2359
|
+
throw new Error("Response body is null");
|
|
2360
|
+
}
|
|
2361
|
+
let failedChunk = void 0;
|
|
2362
|
+
const transformStream = new TransformStream({
|
|
2363
|
+
start() {
|
|
2364
|
+
},
|
|
2365
|
+
async transform(chunk, controller) {
|
|
2366
|
+
try {
|
|
2367
|
+
const decoded = new TextDecoder().decode(chunk);
|
|
2368
|
+
const chunks = decoded.split(RECORD_SEPARATOR2);
|
|
2369
|
+
for (const chunk2 of chunks) {
|
|
2370
|
+
if (chunk2) {
|
|
2371
|
+
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
2372
|
+
try {
|
|
2373
|
+
const parsedChunk = JSON.parse(newChunk);
|
|
2374
|
+
controller.enqueue(parsedChunk);
|
|
2375
|
+
failedChunk = void 0;
|
|
2376
|
+
} catch {
|
|
2377
|
+
failedChunk = newChunk;
|
|
2378
|
+
}
|
|
2379
|
+
}
|
|
2380
|
+
}
|
|
2381
|
+
} catch {
|
|
2382
|
+
}
|
|
2383
|
+
}
|
|
2384
|
+
});
|
|
2385
|
+
return response.body.pipeThrough(transformStream);
|
|
2386
|
+
}
|
|
2387
|
+
/**
|
|
2388
|
+
* Watches an existing agent builder action run by runId.
|
|
2389
|
+
* This is used for hot reload recovery - it loads the existing run state
|
|
2390
|
+
* and streams any remaining progress.
|
|
2391
|
+
* This calls `/api/agent-builder/:actionId/watch`.
|
|
2392
|
+
*/
|
|
2393
|
+
async watch({ runId, eventType }, onRecord) {
|
|
2394
|
+
const url = `/api/agent-builder/${this.actionId}/watch?runId=${runId}${eventType ? `&eventType=${eventType}` : ""}`;
|
|
2395
|
+
const response = await this.request(url, {
|
|
2396
|
+
method: "GET",
|
|
2397
|
+
stream: true
|
|
2398
|
+
});
|
|
2399
|
+
if (!response.ok) {
|
|
2400
|
+
throw new Error(`Failed to watch agent builder action: ${response.statusText}`);
|
|
2401
|
+
}
|
|
2402
|
+
if (!response.body) {
|
|
2403
|
+
throw new Error("Response body is null");
|
|
2404
|
+
}
|
|
2405
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
2406
|
+
if (typeof record === "string") {
|
|
2407
|
+
onRecord(JSON.parse(record));
|
|
2408
|
+
} else {
|
|
2409
|
+
onRecord(record);
|
|
2410
|
+
}
|
|
2411
|
+
}
|
|
2412
|
+
}
|
|
2413
|
+
/**
|
|
2414
|
+
* Gets a specific action run by its ID.
|
|
2415
|
+
* This calls `/api/agent-builder/:actionId/runs/:runId`.
|
|
2416
|
+
*/
|
|
2417
|
+
async runById(runId) {
|
|
2418
|
+
const url = `/api/agent-builder/${this.actionId}/runs/${runId}`;
|
|
2419
|
+
return this.request(url, {
|
|
2420
|
+
method: "GET"
|
|
2421
|
+
});
|
|
2422
|
+
}
|
|
2423
|
+
/**
|
|
2424
|
+
* Gets details about this agent builder action.
|
|
2425
|
+
* This calls `/api/agent-builder/:actionId`.
|
|
2426
|
+
*/
|
|
2427
|
+
async details() {
|
|
2428
|
+
const result = await this.request(`/api/agent-builder/${this.actionId}`);
|
|
2429
|
+
return result;
|
|
2430
|
+
}
|
|
2431
|
+
/**
|
|
2432
|
+
* Gets all runs for this agent builder action.
|
|
2433
|
+
* This calls `/api/agent-builder/:actionId/runs`.
|
|
2434
|
+
*/
|
|
2435
|
+
async runs(params) {
|
|
2436
|
+
const searchParams = new URLSearchParams();
|
|
2437
|
+
if (params?.fromDate) {
|
|
2438
|
+
searchParams.set("fromDate", params.fromDate.toISOString());
|
|
2439
|
+
}
|
|
2440
|
+
if (params?.toDate) {
|
|
2441
|
+
searchParams.set("toDate", params.toDate.toISOString());
|
|
2442
|
+
}
|
|
2443
|
+
if (params?.limit !== void 0) {
|
|
2444
|
+
searchParams.set("limit", String(params.limit));
|
|
2445
|
+
}
|
|
2446
|
+
if (params?.offset !== void 0) {
|
|
2447
|
+
searchParams.set("offset", String(params.offset));
|
|
2448
|
+
}
|
|
2449
|
+
if (params?.resourceId) {
|
|
2450
|
+
searchParams.set("resourceId", params.resourceId);
|
|
2451
|
+
}
|
|
2452
|
+
const url = `/api/agent-builder/${this.actionId}/runs${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
2453
|
+
return this.request(url, {
|
|
2454
|
+
method: "GET"
|
|
2455
|
+
});
|
|
2456
|
+
}
|
|
2457
|
+
/**
|
|
2458
|
+
* Gets the execution result of an agent builder action run.
|
|
2459
|
+
* This calls `/api/agent-builder/:actionId/runs/:runId/execution-result`.
|
|
2460
|
+
*/
|
|
2461
|
+
async runExecutionResult(runId) {
|
|
2462
|
+
const url = `/api/agent-builder/${this.actionId}/runs/${runId}/execution-result`;
|
|
2463
|
+
return this.request(url, {
|
|
2464
|
+
method: "GET"
|
|
2465
|
+
});
|
|
2466
|
+
}
|
|
2467
|
+
/**
|
|
2468
|
+
* Cancels an agent builder action run.
|
|
2469
|
+
* This calls `/api/agent-builder/:actionId/runs/:runId/cancel`.
|
|
2470
|
+
*/
|
|
2471
|
+
async cancelRun(runId) {
|
|
2472
|
+
const url = `/api/agent-builder/${this.actionId}/runs/${runId}/cancel`;
|
|
2473
|
+
return this.request(url, {
|
|
2474
|
+
method: "POST"
|
|
2475
|
+
});
|
|
2476
|
+
}
|
|
2477
|
+
/**
|
|
2478
|
+
* Sends an event to an agent builder action run.
|
|
2479
|
+
* This calls `/api/agent-builder/:actionId/runs/:runId/send-event`.
|
|
2480
|
+
*/
|
|
2481
|
+
async sendRunEvent(params) {
|
|
2482
|
+
const url = `/api/agent-builder/${this.actionId}/runs/${params.runId}/send-event`;
|
|
2483
|
+
return this.request(url, {
|
|
2484
|
+
method: "POST",
|
|
2485
|
+
body: { event: params.event, data: params.data }
|
|
2486
|
+
});
|
|
2487
|
+
}
|
|
2488
|
+
};
|
|
2489
|
+
|
|
2490
|
+
// src/resources/observability.ts
|
|
2491
|
+
var Observability = class extends BaseResource {
|
|
2492
|
+
constructor(options) {
|
|
2493
|
+
super(options);
|
|
2494
|
+
}
|
|
2495
|
+
/**
|
|
2496
|
+
* Retrieves a specific AI trace by ID
|
|
2497
|
+
* @param traceId - ID of the trace to retrieve
|
|
2498
|
+
* @returns Promise containing the AI trace with all its spans
|
|
2499
|
+
*/
|
|
2500
|
+
getTrace(traceId) {
|
|
2501
|
+
return this.request(`/api/observability/traces/${traceId}`);
|
|
2502
|
+
}
|
|
2503
|
+
/**
|
|
2504
|
+
* Retrieves paginated list of AI traces with optional filtering
|
|
2505
|
+
* @param params - Parameters for pagination and filtering
|
|
2506
|
+
* @returns Promise containing paginated traces and pagination info
|
|
2507
|
+
*/
|
|
2508
|
+
getTraces(params) {
|
|
2509
|
+
const { pagination, filters } = params;
|
|
2510
|
+
const { page, perPage, dateRange } = pagination || {};
|
|
2511
|
+
const { name, spanType, entityId, entityType } = filters || {};
|
|
2512
|
+
const searchParams = new URLSearchParams();
|
|
2513
|
+
if (page !== void 0) {
|
|
2514
|
+
searchParams.set("page", String(page));
|
|
2515
|
+
}
|
|
2516
|
+
if (perPage !== void 0) {
|
|
2517
|
+
searchParams.set("perPage", String(perPage));
|
|
2518
|
+
}
|
|
2519
|
+
if (name) {
|
|
2520
|
+
searchParams.set("name", name);
|
|
2521
|
+
}
|
|
2522
|
+
if (spanType !== void 0) {
|
|
2523
|
+
searchParams.set("spanType", String(spanType));
|
|
2524
|
+
}
|
|
2525
|
+
if (entityId && entityType) {
|
|
2526
|
+
searchParams.set("entityId", entityId);
|
|
2527
|
+
searchParams.set("entityType", entityType);
|
|
2528
|
+
}
|
|
2529
|
+
if (dateRange) {
|
|
2530
|
+
const dateRangeStr = JSON.stringify({
|
|
2531
|
+
start: dateRange.start instanceof Date ? dateRange.start.toISOString() : dateRange.start,
|
|
2532
|
+
end: dateRange.end instanceof Date ? dateRange.end.toISOString() : dateRange.end
|
|
2533
|
+
});
|
|
2534
|
+
searchParams.set("dateRange", dateRangeStr);
|
|
2535
|
+
}
|
|
2536
|
+
const queryString = searchParams.toString();
|
|
2537
|
+
return this.request(`/api/observability/traces${queryString ? `?${queryString}` : ""}`);
|
|
2538
|
+
}
|
|
2539
|
+
score(params) {
|
|
2540
|
+
return this.request(`/api/observability/traces/score`, {
|
|
2541
|
+
method: "POST",
|
|
2542
|
+
body: { ...params }
|
|
2543
|
+
});
|
|
2544
|
+
}
|
|
2545
|
+
};
|
|
2546
|
+
|
|
2547
|
+
// src/resources/network-memory-thread.ts
|
|
2548
|
+
var NetworkMemoryThread = class extends BaseResource {
|
|
2549
|
+
constructor(options, threadId, networkId) {
|
|
2550
|
+
super(options);
|
|
2551
|
+
this.threadId = threadId;
|
|
2552
|
+
this.networkId = networkId;
|
|
2553
|
+
}
|
|
2554
|
+
/**
|
|
2555
|
+
* Retrieves the memory thread details
|
|
2556
|
+
* @returns Promise containing thread details including title and metadata
|
|
2557
|
+
*/
|
|
2558
|
+
get() {
|
|
2559
|
+
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`);
|
|
2560
|
+
}
|
|
2561
|
+
/**
|
|
2562
|
+
* Updates the memory thread properties
|
|
2563
|
+
* @param params - Update parameters including title and metadata
|
|
2564
|
+
* @returns Promise containing updated thread details
|
|
2565
|
+
*/
|
|
2566
|
+
update(params) {
|
|
2567
|
+
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
|
|
2568
|
+
method: "PATCH",
|
|
2569
|
+
body: params
|
|
2570
|
+
});
|
|
2571
|
+
}
|
|
2572
|
+
/**
|
|
2573
|
+
* Deletes the memory thread
|
|
2574
|
+
* @returns Promise containing deletion result
|
|
2575
|
+
*/
|
|
2576
|
+
delete() {
|
|
2577
|
+
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
|
|
2578
|
+
method: "DELETE"
|
|
2579
|
+
});
|
|
2580
|
+
}
|
|
2581
|
+
/**
|
|
2582
|
+
* Retrieves messages associated with the thread
|
|
2583
|
+
* @param params - Optional parameters including limit for number of messages to retrieve
|
|
2584
|
+
* @returns Promise containing thread messages and UI messages
|
|
2585
|
+
*/
|
|
2586
|
+
getMessages(params) {
|
|
2587
|
+
const query = new URLSearchParams({
|
|
2588
|
+
networkId: this.networkId,
|
|
2589
|
+
...params?.limit ? { limit: params.limit.toString() } : {}
|
|
410
2590
|
});
|
|
2591
|
+
return this.request(`/api/memory/network/threads/${this.threadId}/messages?${query.toString()}`);
|
|
411
2592
|
}
|
|
412
2593
|
/**
|
|
413
|
-
*
|
|
414
|
-
* @param
|
|
415
|
-
*
|
|
416
|
-
* @
|
|
417
|
-
* @returns Promise containing the workflow resume results
|
|
2594
|
+
* Deletes one or more messages from the thread
|
|
2595
|
+
* @param messageIds - Can be a single message ID (string), array of message IDs,
|
|
2596
|
+
* message object with id property, or array of message objects
|
|
2597
|
+
* @returns Promise containing deletion result
|
|
418
2598
|
*/
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
return this.request(`/api/workflows/${this.workflowId}/resume?runId=${runId}`, {
|
|
2599
|
+
deleteMessages(messageIds) {
|
|
2600
|
+
const query = new URLSearchParams({
|
|
2601
|
+
networkId: this.networkId
|
|
2602
|
+
});
|
|
2603
|
+
return this.request(`/api/memory/network/messages/delete?${query.toString()}`, {
|
|
425
2604
|
method: "POST",
|
|
426
|
-
body: {
|
|
427
|
-
stepId,
|
|
428
|
-
context
|
|
429
|
-
}
|
|
2605
|
+
body: { messageIds }
|
|
430
2606
|
});
|
|
431
2607
|
}
|
|
2608
|
+
};
|
|
2609
|
+
|
|
2610
|
+
// src/resources/vNextNetwork.ts
|
|
2611
|
+
var RECORD_SEPARATOR3 = "";
|
|
2612
|
+
var VNextNetwork = class extends BaseResource {
|
|
2613
|
+
constructor(options, networkId) {
|
|
2614
|
+
super(options);
|
|
2615
|
+
this.networkId = networkId;
|
|
2616
|
+
}
|
|
432
2617
|
/**
|
|
433
|
-
*
|
|
434
|
-
* @param
|
|
435
|
-
* @returns Promise containing
|
|
2618
|
+
* Retrieves details about the network
|
|
2619
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
2620
|
+
* @returns Promise containing vNext network details
|
|
436
2621
|
*/
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
if (!!params?.runId) {
|
|
440
|
-
searchParams.set("runId", params.runId);
|
|
441
|
-
}
|
|
442
|
-
return this.request(`/api/workflows/${this.workflowId}/start-async?${searchParams.toString()}`, {
|
|
443
|
-
method: "POST",
|
|
444
|
-
body: params?.triggerData
|
|
445
|
-
});
|
|
2622
|
+
details(runtimeContext) {
|
|
2623
|
+
return this.request(`/api/networks/v-next/${this.networkId}${runtimeContextQueryString(runtimeContext)}`);
|
|
446
2624
|
}
|
|
447
2625
|
/**
|
|
448
|
-
*
|
|
449
|
-
* @param params -
|
|
450
|
-
* @returns Promise containing the
|
|
2626
|
+
* Generates a response from the v-next network
|
|
2627
|
+
* @param params - Generation parameters including message
|
|
2628
|
+
* @returns Promise containing the generated response
|
|
451
2629
|
*/
|
|
452
|
-
|
|
453
|
-
return this.request(`/api/
|
|
2630
|
+
generate(params) {
|
|
2631
|
+
return this.request(`/api/networks/v-next/${this.networkId}/generate`, {
|
|
454
2632
|
method: "POST",
|
|
455
2633
|
body: {
|
|
456
|
-
|
|
457
|
-
|
|
2634
|
+
...params,
|
|
2635
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
458
2636
|
}
|
|
459
2637
|
});
|
|
460
2638
|
}
|
|
461
2639
|
/**
|
|
462
|
-
*
|
|
463
|
-
*
|
|
464
|
-
*
|
|
465
|
-
* @param stream - The readable stream to process
|
|
466
|
-
* @returns An async generator that yields parsed records
|
|
2640
|
+
* Generates a response from the v-next network using multiple primitives
|
|
2641
|
+
* @param params - Generation parameters including message
|
|
2642
|
+
* @returns Promise containing the generated response
|
|
467
2643
|
*/
|
|
2644
|
+
loop(params) {
|
|
2645
|
+
return this.request(`/api/networks/v-next/${this.networkId}/loop`, {
|
|
2646
|
+
method: "POST",
|
|
2647
|
+
body: {
|
|
2648
|
+
...params,
|
|
2649
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
2650
|
+
}
|
|
2651
|
+
});
|
|
2652
|
+
}
|
|
468
2653
|
async *streamProcessor(stream) {
|
|
469
2654
|
const reader = stream.getReader();
|
|
470
2655
|
let doneReading = false;
|
|
@@ -476,7 +2661,7 @@ var Workflow = class extends BaseResource {
|
|
|
476
2661
|
if (done && !value) continue;
|
|
477
2662
|
try {
|
|
478
2663
|
const decoded = value ? new TextDecoder().decode(value) : "";
|
|
479
|
-
const chunks = (buffer + decoded).split(
|
|
2664
|
+
const chunks = (buffer + decoded).split(RECORD_SEPARATOR3);
|
|
480
2665
|
buffer = chunks.pop() || "";
|
|
481
2666
|
for (const chunk of chunks) {
|
|
482
2667
|
if (chunk) {
|
|
@@ -489,7 +2674,7 @@ var Workflow = class extends BaseResource {
|
|
|
489
2674
|
}
|
|
490
2675
|
}
|
|
491
2676
|
}
|
|
492
|
-
} catch
|
|
2677
|
+
} catch {
|
|
493
2678
|
}
|
|
494
2679
|
}
|
|
495
2680
|
if (buffer) {
|
|
@@ -504,63 +2689,83 @@ var Workflow = class extends BaseResource {
|
|
|
504
2689
|
}
|
|
505
2690
|
}
|
|
506
2691
|
/**
|
|
507
|
-
*
|
|
508
|
-
* @param
|
|
509
|
-
* @returns
|
|
2692
|
+
* Streams a response from the v-next network
|
|
2693
|
+
* @param params - Stream parameters including message
|
|
2694
|
+
* @returns Promise containing the results
|
|
510
2695
|
*/
|
|
511
|
-
async
|
|
512
|
-
const response = await this.request(`/api/
|
|
2696
|
+
async stream(params, onRecord) {
|
|
2697
|
+
const response = await this.request(`/api/networks/v-next/${this.networkId}/stream`, {
|
|
2698
|
+
method: "POST",
|
|
2699
|
+
body: {
|
|
2700
|
+
...params,
|
|
2701
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
2702
|
+
},
|
|
513
2703
|
stream: true
|
|
514
2704
|
});
|
|
515
2705
|
if (!response.ok) {
|
|
516
|
-
throw new Error(`Failed to
|
|
2706
|
+
throw new Error(`Failed to stream vNext network: ${response.statusText}`);
|
|
517
2707
|
}
|
|
518
2708
|
if (!response.body) {
|
|
519
2709
|
throw new Error("Response body is null");
|
|
520
2710
|
}
|
|
521
2711
|
for await (const record of this.streamProcessor(response.body)) {
|
|
522
|
-
|
|
2712
|
+
if (typeof record === "string") {
|
|
2713
|
+
onRecord(JSON.parse(record));
|
|
2714
|
+
} else {
|
|
2715
|
+
onRecord(record);
|
|
2716
|
+
}
|
|
523
2717
|
}
|
|
524
2718
|
}
|
|
525
|
-
};
|
|
526
|
-
|
|
527
|
-
// src/resources/tool.ts
|
|
528
|
-
var Tool = class extends BaseResource {
|
|
529
|
-
constructor(options, toolId) {
|
|
530
|
-
super(options);
|
|
531
|
-
this.toolId = toolId;
|
|
532
|
-
}
|
|
533
|
-
/**
|
|
534
|
-
* Retrieves details about the tool
|
|
535
|
-
* @returns Promise containing tool details including description and schemas
|
|
536
|
-
*/
|
|
537
|
-
details() {
|
|
538
|
-
return this.request(`/api/tools/${this.toolId}`);
|
|
539
|
-
}
|
|
540
2719
|
/**
|
|
541
|
-
*
|
|
542
|
-
* @param params -
|
|
543
|
-
* @returns Promise containing the
|
|
2720
|
+
* Streams a response from the v-next network loop
|
|
2721
|
+
* @param params - Stream parameters including message
|
|
2722
|
+
* @returns Promise containing the results
|
|
544
2723
|
*/
|
|
545
|
-
|
|
546
|
-
|
|
2724
|
+
async loopStream(params, onRecord) {
|
|
2725
|
+
const response = await this.request(`/api/networks/v-next/${this.networkId}/loop-stream`, {
|
|
547
2726
|
method: "POST",
|
|
548
|
-
body:
|
|
2727
|
+
body: {
|
|
2728
|
+
...params,
|
|
2729
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
2730
|
+
},
|
|
2731
|
+
stream: true
|
|
549
2732
|
});
|
|
2733
|
+
if (!response.ok) {
|
|
2734
|
+
throw new Error(`Failed to stream vNext network loop: ${response.statusText}`);
|
|
2735
|
+
}
|
|
2736
|
+
if (!response.body) {
|
|
2737
|
+
throw new Error("Response body is null");
|
|
2738
|
+
}
|
|
2739
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
2740
|
+
if (typeof record === "string") {
|
|
2741
|
+
onRecord(JSON.parse(record));
|
|
2742
|
+
} else {
|
|
2743
|
+
onRecord(record);
|
|
2744
|
+
}
|
|
2745
|
+
}
|
|
550
2746
|
}
|
|
551
2747
|
};
|
|
552
2748
|
|
|
553
2749
|
// src/client.ts
|
|
554
2750
|
var MastraClient = class extends BaseResource {
|
|
2751
|
+
observability;
|
|
555
2752
|
constructor(options) {
|
|
556
2753
|
super(options);
|
|
2754
|
+
this.observability = new Observability(options);
|
|
557
2755
|
}
|
|
558
2756
|
/**
|
|
559
2757
|
* Retrieves all available agents
|
|
2758
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
560
2759
|
* @returns Promise containing map of agent IDs to agent details
|
|
561
2760
|
*/
|
|
562
|
-
getAgents() {
|
|
563
|
-
|
|
2761
|
+
getAgents(runtimeContext) {
|
|
2762
|
+
const runtimeContextParam = base64RuntimeContext(parseClientRuntimeContext(runtimeContext));
|
|
2763
|
+
const searchParams = new URLSearchParams();
|
|
2764
|
+
if (runtimeContextParam) {
|
|
2765
|
+
searchParams.set("runtimeContext", runtimeContextParam);
|
|
2766
|
+
}
|
|
2767
|
+
const queryString = searchParams.toString();
|
|
2768
|
+
return this.request(`/api/agents${queryString ? `?${queryString}` : ""}`);
|
|
564
2769
|
}
|
|
565
2770
|
/**
|
|
566
2771
|
* Gets an agent instance by ID
|
|
@@ -578,6 +2783,14 @@ var MastraClient = class extends BaseResource {
|
|
|
578
2783
|
getMemoryThreads(params) {
|
|
579
2784
|
return this.request(`/api/memory/threads?resourceid=${params.resourceId}&agentId=${params.agentId}`);
|
|
580
2785
|
}
|
|
2786
|
+
/**
|
|
2787
|
+
* Retrieves memory config for a resource
|
|
2788
|
+
* @param params - Parameters containing the resource ID
|
|
2789
|
+
* @returns Promise containing array of memory threads
|
|
2790
|
+
*/
|
|
2791
|
+
getMemoryConfig(params) {
|
|
2792
|
+
return this.request(`/api/memory/config?agentId=${params.agentId}`);
|
|
2793
|
+
}
|
|
581
2794
|
/**
|
|
582
2795
|
* Creates a new memory thread
|
|
583
2796
|
* @param params - Parameters for creating the memory thread
|
|
@@ -594,6 +2807,24 @@ var MastraClient = class extends BaseResource {
|
|
|
594
2807
|
getMemoryThread(threadId, agentId) {
|
|
595
2808
|
return new MemoryThread(this.options, threadId, agentId);
|
|
596
2809
|
}
|
|
2810
|
+
getThreadMessages(threadId, opts = {}) {
|
|
2811
|
+
let url = "";
|
|
2812
|
+
if (opts.agentId) {
|
|
2813
|
+
url = `/api/memory/threads/${threadId}/messages?agentId=${opts.agentId}`;
|
|
2814
|
+
} else if (opts.networkId) {
|
|
2815
|
+
url = `/api/memory/network/threads/${threadId}/messages?networkId=${opts.networkId}`;
|
|
2816
|
+
}
|
|
2817
|
+
return this.request(url);
|
|
2818
|
+
}
|
|
2819
|
+
deleteThread(threadId, opts = {}) {
|
|
2820
|
+
let url = "";
|
|
2821
|
+
if (opts.agentId) {
|
|
2822
|
+
url = `/api/memory/threads/${threadId}?agentId=${opts.agentId}`;
|
|
2823
|
+
} else if (opts.networkId) {
|
|
2824
|
+
url = `/api/memory/network/threads/${threadId}?networkId=${opts.networkId}`;
|
|
2825
|
+
}
|
|
2826
|
+
return this.request(url, { method: "DELETE" });
|
|
2827
|
+
}
|
|
597
2828
|
/**
|
|
598
2829
|
* Saves messages to memory
|
|
599
2830
|
* @param params - Parameters containing messages to save
|
|
@@ -612,12 +2843,61 @@ var MastraClient = class extends BaseResource {
|
|
|
612
2843
|
getMemoryStatus(agentId) {
|
|
613
2844
|
return this.request(`/api/memory/status?agentId=${agentId}`);
|
|
614
2845
|
}
|
|
2846
|
+
/**
|
|
2847
|
+
* Retrieves memory threads for a resource
|
|
2848
|
+
* @param params - Parameters containing the resource ID
|
|
2849
|
+
* @returns Promise containing array of memory threads
|
|
2850
|
+
*/
|
|
2851
|
+
getNetworkMemoryThreads(params) {
|
|
2852
|
+
return this.request(`/api/memory/network/threads?resourceid=${params.resourceId}&networkId=${params.networkId}`);
|
|
2853
|
+
}
|
|
2854
|
+
/**
|
|
2855
|
+
* Creates a new memory thread
|
|
2856
|
+
* @param params - Parameters for creating the memory thread
|
|
2857
|
+
* @returns Promise containing the created memory thread
|
|
2858
|
+
*/
|
|
2859
|
+
createNetworkMemoryThread(params) {
|
|
2860
|
+
return this.request(`/api/memory/network/threads?networkId=${params.networkId}`, { method: "POST", body: params });
|
|
2861
|
+
}
|
|
2862
|
+
/**
|
|
2863
|
+
* Gets a memory thread instance by ID
|
|
2864
|
+
* @param threadId - ID of the memory thread to retrieve
|
|
2865
|
+
* @returns MemoryThread instance
|
|
2866
|
+
*/
|
|
2867
|
+
getNetworkMemoryThread(threadId, networkId) {
|
|
2868
|
+
return new NetworkMemoryThread(this.options, threadId, networkId);
|
|
2869
|
+
}
|
|
2870
|
+
/**
|
|
2871
|
+
* Saves messages to memory
|
|
2872
|
+
* @param params - Parameters containing messages to save
|
|
2873
|
+
* @returns Promise containing the saved messages
|
|
2874
|
+
*/
|
|
2875
|
+
saveNetworkMessageToMemory(params) {
|
|
2876
|
+
return this.request(`/api/memory/network/save-messages?networkId=${params.networkId}`, {
|
|
2877
|
+
method: "POST",
|
|
2878
|
+
body: params
|
|
2879
|
+
});
|
|
2880
|
+
}
|
|
2881
|
+
/**
|
|
2882
|
+
* Gets the status of the memory system
|
|
2883
|
+
* @returns Promise containing memory system status
|
|
2884
|
+
*/
|
|
2885
|
+
getNetworkMemoryStatus(networkId) {
|
|
2886
|
+
return this.request(`/api/memory/network/status?networkId=${networkId}`);
|
|
2887
|
+
}
|
|
615
2888
|
/**
|
|
616
2889
|
* Retrieves all available tools
|
|
2890
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
617
2891
|
* @returns Promise containing map of tool IDs to tool details
|
|
618
2892
|
*/
|
|
619
|
-
getTools() {
|
|
620
|
-
|
|
2893
|
+
getTools(runtimeContext) {
|
|
2894
|
+
const runtimeContextParam = base64RuntimeContext(parseClientRuntimeContext(runtimeContext));
|
|
2895
|
+
const searchParams = new URLSearchParams();
|
|
2896
|
+
if (runtimeContextParam) {
|
|
2897
|
+
searchParams.set("runtimeContext", runtimeContextParam);
|
|
2898
|
+
}
|
|
2899
|
+
const queryString = searchParams.toString();
|
|
2900
|
+
return this.request(`/api/tools${queryString ? `?${queryString}` : ""}`);
|
|
621
2901
|
}
|
|
622
2902
|
/**
|
|
623
2903
|
* Gets a tool instance by ID
|
|
@@ -629,10 +2909,17 @@ var MastraClient = class extends BaseResource {
|
|
|
629
2909
|
}
|
|
630
2910
|
/**
|
|
631
2911
|
* Retrieves all available workflows
|
|
2912
|
+
* @param runtimeContext - Optional runtime context to pass as query parameter
|
|
632
2913
|
* @returns Promise containing map of workflow IDs to workflow details
|
|
633
2914
|
*/
|
|
634
|
-
getWorkflows() {
|
|
635
|
-
|
|
2915
|
+
getWorkflows(runtimeContext) {
|
|
2916
|
+
const runtimeContextParam = base64RuntimeContext(parseClientRuntimeContext(runtimeContext));
|
|
2917
|
+
const searchParams = new URLSearchParams();
|
|
2918
|
+
if (runtimeContextParam) {
|
|
2919
|
+
searchParams.set("runtimeContext", runtimeContextParam);
|
|
2920
|
+
}
|
|
2921
|
+
const queryString = searchParams.toString();
|
|
2922
|
+
return this.request(`/api/workflows${queryString ? `?${queryString}` : ""}`);
|
|
636
2923
|
}
|
|
637
2924
|
/**
|
|
638
2925
|
* Gets a workflow instance by ID
|
|
@@ -642,6 +2929,20 @@ var MastraClient = class extends BaseResource {
|
|
|
642
2929
|
getWorkflow(workflowId) {
|
|
643
2930
|
return new Workflow(this.options, workflowId);
|
|
644
2931
|
}
|
|
2932
|
+
/**
|
|
2933
|
+
* Gets all available agent builder actions
|
|
2934
|
+
* @returns Promise containing map of action IDs to action details
|
|
2935
|
+
*/
|
|
2936
|
+
getAgentBuilderActions() {
|
|
2937
|
+
return this.request("/api/agent-builder/");
|
|
2938
|
+
}
|
|
2939
|
+
/**
|
|
2940
|
+
* Gets an agent builder instance for executing agent-builder workflows
|
|
2941
|
+
* @returns AgentBuilder instance
|
|
2942
|
+
*/
|
|
2943
|
+
getAgentBuilderAction(actionId) {
|
|
2944
|
+
return new AgentBuilder(this.options, actionId);
|
|
2945
|
+
}
|
|
645
2946
|
/**
|
|
646
2947
|
* Gets a vector instance by name
|
|
647
2948
|
* @param vectorName - Name of the vector to retrieve
|
|
@@ -656,7 +2957,41 @@ var MastraClient = class extends BaseResource {
|
|
|
656
2957
|
* @returns Promise containing array of log messages
|
|
657
2958
|
*/
|
|
658
2959
|
getLogs(params) {
|
|
659
|
-
|
|
2960
|
+
const { transportId, fromDate, toDate, logLevel, filters, page, perPage } = params;
|
|
2961
|
+
const _filters = filters ? Object.entries(filters).map(([key, value]) => `${key}:${value}`) : [];
|
|
2962
|
+
const searchParams = new URLSearchParams();
|
|
2963
|
+
if (transportId) {
|
|
2964
|
+
searchParams.set("transportId", transportId);
|
|
2965
|
+
}
|
|
2966
|
+
if (fromDate) {
|
|
2967
|
+
searchParams.set("fromDate", fromDate.toISOString());
|
|
2968
|
+
}
|
|
2969
|
+
if (toDate) {
|
|
2970
|
+
searchParams.set("toDate", toDate.toISOString());
|
|
2971
|
+
}
|
|
2972
|
+
if (logLevel) {
|
|
2973
|
+
searchParams.set("logLevel", logLevel);
|
|
2974
|
+
}
|
|
2975
|
+
if (page) {
|
|
2976
|
+
searchParams.set("page", String(page));
|
|
2977
|
+
}
|
|
2978
|
+
if (perPage) {
|
|
2979
|
+
searchParams.set("perPage", String(perPage));
|
|
2980
|
+
}
|
|
2981
|
+
if (_filters) {
|
|
2982
|
+
if (Array.isArray(_filters)) {
|
|
2983
|
+
for (const filter of _filters) {
|
|
2984
|
+
searchParams.append("filters", filter);
|
|
2985
|
+
}
|
|
2986
|
+
} else {
|
|
2987
|
+
searchParams.set("filters", _filters);
|
|
2988
|
+
}
|
|
2989
|
+
}
|
|
2990
|
+
if (searchParams.size) {
|
|
2991
|
+
return this.request(`/api/logs?${searchParams}`);
|
|
2992
|
+
} else {
|
|
2993
|
+
return this.request(`/api/logs`);
|
|
2994
|
+
}
|
|
660
2995
|
}
|
|
661
2996
|
/**
|
|
662
2997
|
* Gets logs for a specific run
|
|
@@ -664,7 +2999,44 @@ var MastraClient = class extends BaseResource {
|
|
|
664
2999
|
* @returns Promise containing array of log messages
|
|
665
3000
|
*/
|
|
666
3001
|
getLogForRun(params) {
|
|
667
|
-
|
|
3002
|
+
const { runId, transportId, fromDate, toDate, logLevel, filters, page, perPage } = params;
|
|
3003
|
+
const _filters = filters ? Object.entries(filters).map(([key, value]) => `${key}:${value}`) : [];
|
|
3004
|
+
const searchParams = new URLSearchParams();
|
|
3005
|
+
if (runId) {
|
|
3006
|
+
searchParams.set("runId", runId);
|
|
3007
|
+
}
|
|
3008
|
+
if (transportId) {
|
|
3009
|
+
searchParams.set("transportId", transportId);
|
|
3010
|
+
}
|
|
3011
|
+
if (fromDate) {
|
|
3012
|
+
searchParams.set("fromDate", fromDate.toISOString());
|
|
3013
|
+
}
|
|
3014
|
+
if (toDate) {
|
|
3015
|
+
searchParams.set("toDate", toDate.toISOString());
|
|
3016
|
+
}
|
|
3017
|
+
if (logLevel) {
|
|
3018
|
+
searchParams.set("logLevel", logLevel);
|
|
3019
|
+
}
|
|
3020
|
+
if (page) {
|
|
3021
|
+
searchParams.set("page", String(page));
|
|
3022
|
+
}
|
|
3023
|
+
if (perPage) {
|
|
3024
|
+
searchParams.set("perPage", String(perPage));
|
|
3025
|
+
}
|
|
3026
|
+
if (_filters) {
|
|
3027
|
+
if (Array.isArray(_filters)) {
|
|
3028
|
+
for (const filter of _filters) {
|
|
3029
|
+
searchParams.append("filters", filter);
|
|
3030
|
+
}
|
|
3031
|
+
} else {
|
|
3032
|
+
searchParams.set("filters", _filters);
|
|
3033
|
+
}
|
|
3034
|
+
}
|
|
3035
|
+
if (searchParams.size) {
|
|
3036
|
+
return this.request(`/api/logs/${runId}?${searchParams}`);
|
|
3037
|
+
} else {
|
|
3038
|
+
return this.request(`/api/logs/${runId}`);
|
|
3039
|
+
}
|
|
668
3040
|
}
|
|
669
3041
|
/**
|
|
670
3042
|
* List of all log transports
|
|
@@ -679,7 +3051,7 @@ var MastraClient = class extends BaseResource {
|
|
|
679
3051
|
* @returns Promise containing telemetry data
|
|
680
3052
|
*/
|
|
681
3053
|
getTelemetry(params) {
|
|
682
|
-
const { name, scope, page, perPage, attribute } = params || {};
|
|
3054
|
+
const { name, scope, page, perPage, attribute, fromDate, toDate } = params || {};
|
|
683
3055
|
const _attribute = attribute ? Object.entries(attribute).map(([key, value]) => `${key}:${value}`) : [];
|
|
684
3056
|
const searchParams = new URLSearchParams();
|
|
685
3057
|
if (name) {
|
|
@@ -703,6 +3075,12 @@ var MastraClient = class extends BaseResource {
|
|
|
703
3075
|
searchParams.set("attribute", _attribute);
|
|
704
3076
|
}
|
|
705
3077
|
}
|
|
3078
|
+
if (fromDate) {
|
|
3079
|
+
searchParams.set("fromDate", fromDate.toISOString());
|
|
3080
|
+
}
|
|
3081
|
+
if (toDate) {
|
|
3082
|
+
searchParams.set("toDate", toDate.toISOString());
|
|
3083
|
+
}
|
|
706
3084
|
if (searchParams.size) {
|
|
707
3085
|
return this.request(`/api/telemetry?${searchParams}`);
|
|
708
3086
|
} else {
|
|
@@ -710,20 +3088,228 @@ var MastraClient = class extends BaseResource {
|
|
|
710
3088
|
}
|
|
711
3089
|
}
|
|
712
3090
|
/**
|
|
713
|
-
* Retrieves all available networks
|
|
714
|
-
* @returns Promise containing map of network IDs to network details
|
|
3091
|
+
* Retrieves all available vNext networks
|
|
3092
|
+
* @returns Promise containing map of vNext network IDs to vNext network details
|
|
3093
|
+
*/
|
|
3094
|
+
getVNextNetworks() {
|
|
3095
|
+
return this.request("/api/networks/v-next");
|
|
3096
|
+
}
|
|
3097
|
+
/**
|
|
3098
|
+
* Gets a vNext network instance by ID
|
|
3099
|
+
* @param networkId - ID of the vNext network to retrieve
|
|
3100
|
+
* @returns vNext Network instance
|
|
3101
|
+
*/
|
|
3102
|
+
getVNextNetwork(networkId) {
|
|
3103
|
+
return new VNextNetwork(this.options, networkId);
|
|
3104
|
+
}
|
|
3105
|
+
/**
|
|
3106
|
+
* Retrieves a list of available MCP servers.
|
|
3107
|
+
* @param params - Optional parameters for pagination (limit, offset).
|
|
3108
|
+
* @returns Promise containing the list of MCP servers and pagination info.
|
|
3109
|
+
*/
|
|
3110
|
+
getMcpServers(params) {
|
|
3111
|
+
const searchParams = new URLSearchParams();
|
|
3112
|
+
if (params?.limit !== void 0) {
|
|
3113
|
+
searchParams.set("limit", String(params.limit));
|
|
3114
|
+
}
|
|
3115
|
+
if (params?.offset !== void 0) {
|
|
3116
|
+
searchParams.set("offset", String(params.offset));
|
|
3117
|
+
}
|
|
3118
|
+
const queryString = searchParams.toString();
|
|
3119
|
+
return this.request(`/api/mcp/v0/servers${queryString ? `?${queryString}` : ""}`);
|
|
3120
|
+
}
|
|
3121
|
+
/**
|
|
3122
|
+
* Retrieves detailed information for a specific MCP server.
|
|
3123
|
+
* @param serverId - The ID of the MCP server to retrieve.
|
|
3124
|
+
* @param params - Optional parameters, e.g., specific version.
|
|
3125
|
+
* @returns Promise containing the detailed MCP server information.
|
|
3126
|
+
*/
|
|
3127
|
+
getMcpServerDetails(serverId, params) {
|
|
3128
|
+
const searchParams = new URLSearchParams();
|
|
3129
|
+
if (params?.version) {
|
|
3130
|
+
searchParams.set("version", params.version);
|
|
3131
|
+
}
|
|
3132
|
+
const queryString = searchParams.toString();
|
|
3133
|
+
return this.request(`/api/mcp/v0/servers/${serverId}${queryString ? `?${queryString}` : ""}`);
|
|
3134
|
+
}
|
|
3135
|
+
/**
|
|
3136
|
+
* Retrieves a list of tools for a specific MCP server.
|
|
3137
|
+
* @param serverId - The ID of the MCP server.
|
|
3138
|
+
* @returns Promise containing the list of tools.
|
|
3139
|
+
*/
|
|
3140
|
+
getMcpServerTools(serverId) {
|
|
3141
|
+
return this.request(`/api/mcp/${serverId}/tools`);
|
|
3142
|
+
}
|
|
3143
|
+
/**
|
|
3144
|
+
* Gets an MCPTool resource instance for a specific tool on an MCP server.
|
|
3145
|
+
* This instance can then be used to fetch details or execute the tool.
|
|
3146
|
+
* @param serverId - The ID of the MCP server.
|
|
3147
|
+
* @param toolId - The ID of the tool.
|
|
3148
|
+
* @returns MCPTool instance.
|
|
3149
|
+
*/
|
|
3150
|
+
getMcpServerTool(serverId, toolId) {
|
|
3151
|
+
return new MCPTool(this.options, serverId, toolId);
|
|
3152
|
+
}
|
|
3153
|
+
/**
|
|
3154
|
+
* Gets an A2A client for interacting with an agent via the A2A protocol
|
|
3155
|
+
* @param agentId - ID of the agent to interact with
|
|
3156
|
+
* @returns A2A client instance
|
|
3157
|
+
*/
|
|
3158
|
+
getA2A(agentId) {
|
|
3159
|
+
return new A2A(this.options, agentId);
|
|
3160
|
+
}
|
|
3161
|
+
/**
|
|
3162
|
+
* Retrieves the working memory for a specific thread (optionally resource-scoped).
|
|
3163
|
+
* @param agentId - ID of the agent.
|
|
3164
|
+
* @param threadId - ID of the thread.
|
|
3165
|
+
* @param resourceId - Optional ID of the resource.
|
|
3166
|
+
* @returns Working memory for the specified thread or resource.
|
|
3167
|
+
*/
|
|
3168
|
+
getWorkingMemory({
|
|
3169
|
+
agentId,
|
|
3170
|
+
threadId,
|
|
3171
|
+
resourceId
|
|
3172
|
+
}) {
|
|
3173
|
+
return this.request(`/api/memory/threads/${threadId}/working-memory?agentId=${agentId}&resourceId=${resourceId}`);
|
|
3174
|
+
}
|
|
3175
|
+
/**
|
|
3176
|
+
* Updates the working memory for a specific thread (optionally resource-scoped).
|
|
3177
|
+
* @param agentId - ID of the agent.
|
|
3178
|
+
* @param threadId - ID of the thread.
|
|
3179
|
+
* @param workingMemory - The new working memory content.
|
|
3180
|
+
* @param resourceId - Optional ID of the resource.
|
|
3181
|
+
*/
|
|
3182
|
+
updateWorkingMemory({
|
|
3183
|
+
agentId,
|
|
3184
|
+
threadId,
|
|
3185
|
+
workingMemory,
|
|
3186
|
+
resourceId
|
|
3187
|
+
}) {
|
|
3188
|
+
return this.request(`/api/memory/threads/${threadId}/working-memory?agentId=${agentId}`, {
|
|
3189
|
+
method: "POST",
|
|
3190
|
+
body: {
|
|
3191
|
+
workingMemory,
|
|
3192
|
+
resourceId
|
|
3193
|
+
}
|
|
3194
|
+
});
|
|
3195
|
+
}
|
|
3196
|
+
/**
|
|
3197
|
+
* Retrieves all available scorers
|
|
3198
|
+
* @returns Promise containing list of available scorers
|
|
3199
|
+
*/
|
|
3200
|
+
getScorers() {
|
|
3201
|
+
return this.request("/api/scores/scorers");
|
|
3202
|
+
}
|
|
3203
|
+
/**
|
|
3204
|
+
* Retrieves a scorer by ID
|
|
3205
|
+
* @param scorerId - ID of the scorer to retrieve
|
|
3206
|
+
* @returns Promise containing the scorer
|
|
3207
|
+
*/
|
|
3208
|
+
getScorer(scorerId) {
|
|
3209
|
+
return this.request(`/api/scores/scorers/${scorerId}`);
|
|
3210
|
+
}
|
|
3211
|
+
getScoresByScorerId(params) {
|
|
3212
|
+
const { page, perPage, scorerId, entityId, entityType } = params;
|
|
3213
|
+
const searchParams = new URLSearchParams();
|
|
3214
|
+
if (entityId) {
|
|
3215
|
+
searchParams.set("entityId", entityId);
|
|
3216
|
+
}
|
|
3217
|
+
if (entityType) {
|
|
3218
|
+
searchParams.set("entityType", entityType);
|
|
3219
|
+
}
|
|
3220
|
+
if (page !== void 0) {
|
|
3221
|
+
searchParams.set("page", String(page));
|
|
3222
|
+
}
|
|
3223
|
+
if (perPage !== void 0) {
|
|
3224
|
+
searchParams.set("perPage", String(perPage));
|
|
3225
|
+
}
|
|
3226
|
+
const queryString = searchParams.toString();
|
|
3227
|
+
return this.request(`/api/scores/scorer/${scorerId}${queryString ? `?${queryString}` : ""}`);
|
|
3228
|
+
}
|
|
3229
|
+
/**
|
|
3230
|
+
* Retrieves scores by run ID
|
|
3231
|
+
* @param params - Parameters containing run ID and pagination options
|
|
3232
|
+
* @returns Promise containing scores and pagination info
|
|
3233
|
+
*/
|
|
3234
|
+
getScoresByRunId(params) {
|
|
3235
|
+
const { runId, page, perPage } = params;
|
|
3236
|
+
const searchParams = new URLSearchParams();
|
|
3237
|
+
if (page !== void 0) {
|
|
3238
|
+
searchParams.set("page", String(page));
|
|
3239
|
+
}
|
|
3240
|
+
if (perPage !== void 0) {
|
|
3241
|
+
searchParams.set("perPage", String(perPage));
|
|
3242
|
+
}
|
|
3243
|
+
const queryString = searchParams.toString();
|
|
3244
|
+
return this.request(`/api/scores/run/${runId}${queryString ? `?${queryString}` : ""}`);
|
|
3245
|
+
}
|
|
3246
|
+
/**
|
|
3247
|
+
* Retrieves scores by entity ID and type
|
|
3248
|
+
* @param params - Parameters containing entity ID, type, and pagination options
|
|
3249
|
+
* @returns Promise containing scores and pagination info
|
|
3250
|
+
*/
|
|
3251
|
+
getScoresByEntityId(params) {
|
|
3252
|
+
const { entityId, entityType, page, perPage } = params;
|
|
3253
|
+
const searchParams = new URLSearchParams();
|
|
3254
|
+
if (page !== void 0) {
|
|
3255
|
+
searchParams.set("page", String(page));
|
|
3256
|
+
}
|
|
3257
|
+
if (perPage !== void 0) {
|
|
3258
|
+
searchParams.set("perPage", String(perPage));
|
|
3259
|
+
}
|
|
3260
|
+
const queryString = searchParams.toString();
|
|
3261
|
+
return this.request(`/api/scores/entity/${entityType}/${entityId}${queryString ? `?${queryString}` : ""}`);
|
|
3262
|
+
}
|
|
3263
|
+
/**
|
|
3264
|
+
* Saves a score
|
|
3265
|
+
* @param params - Parameters containing the score data to save
|
|
3266
|
+
* @returns Promise containing the saved score
|
|
715
3267
|
*/
|
|
716
|
-
|
|
717
|
-
return this.request("/api/
|
|
3268
|
+
saveScore(params) {
|
|
3269
|
+
return this.request("/api/scores", {
|
|
3270
|
+
method: "POST",
|
|
3271
|
+
body: params
|
|
3272
|
+
});
|
|
718
3273
|
}
|
|
719
3274
|
/**
|
|
720
|
-
*
|
|
721
|
-
* @
|
|
722
|
-
* @returns Network instance
|
|
3275
|
+
* Retrieves model providers with available keys
|
|
3276
|
+
* @returns Promise containing model providers with available keys
|
|
723
3277
|
*/
|
|
724
|
-
|
|
725
|
-
return
|
|
3278
|
+
getModelProviders() {
|
|
3279
|
+
return this.request(`/api/model-providers`);
|
|
3280
|
+
}
|
|
3281
|
+
getAITrace(traceId) {
|
|
3282
|
+
return this.observability.getTrace(traceId);
|
|
3283
|
+
}
|
|
3284
|
+
getAITraces(params) {
|
|
3285
|
+
return this.observability.getTraces(params);
|
|
3286
|
+
}
|
|
3287
|
+
score(params) {
|
|
3288
|
+
return this.observability.score(params);
|
|
3289
|
+
}
|
|
3290
|
+
};
|
|
3291
|
+
|
|
3292
|
+
// src/tools.ts
|
|
3293
|
+
var ClientTool = class {
|
|
3294
|
+
id;
|
|
3295
|
+
description;
|
|
3296
|
+
inputSchema;
|
|
3297
|
+
outputSchema;
|
|
3298
|
+
execute;
|
|
3299
|
+
constructor(opts) {
|
|
3300
|
+
this.id = opts.id;
|
|
3301
|
+
this.description = opts.description;
|
|
3302
|
+
this.inputSchema = opts.inputSchema;
|
|
3303
|
+
this.outputSchema = opts.outputSchema;
|
|
3304
|
+
this.execute = opts.execute;
|
|
726
3305
|
}
|
|
727
3306
|
};
|
|
3307
|
+
function createTool(opts) {
|
|
3308
|
+
return new ClientTool(opts);
|
|
3309
|
+
}
|
|
728
3310
|
|
|
3311
|
+
exports.ClientTool = ClientTool;
|
|
729
3312
|
exports.MastraClient = MastraClient;
|
|
3313
|
+
exports.createTool = createTool;
|
|
3314
|
+
//# sourceMappingURL=index.cjs.map
|
|
3315
|
+
//# sourceMappingURL=index.cjs.map
|