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