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