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