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