@mastra/client-js 0.0.0-switch-to-core-20250424015131 → 0.0.0-taofeeq-fix-tool-call-showing-after-message-20250806184630
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/.turbo/turbo-build.log +18 -0
- package/CHANGELOG.md +1356 -2
- package/LICENSE.md +11 -42
- package/README.md +2 -1
- package/dist/adapters/agui.d.ts +23 -0
- package/dist/adapters/agui.d.ts.map +1 -0
- package/dist/client.d.ts +265 -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 +1785 -82
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +4 -585
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1785 -86
- package/dist/index.js.map +1 -0
- package/dist/resources/a2a.d.ts +44 -0
- package/dist/resources/a2a.d.ts.map +1 -0
- package/dist/resources/agent.d.ts +112 -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/legacy-workflow.d.ts +87 -0
- package/dist/resources/legacy-workflow.d.ts.map +1 -0
- package/dist/resources/mcp-tool.d.ts +27 -0
- package/dist/resources/mcp-tool.d.ts.map +1 -0
- package/dist/resources/memory-thread.d.ts +53 -0
- package/dist/resources/memory-thread.d.ts.map +1 -0
- package/dist/resources/network-memory-thread.d.ts +47 -0
- package/dist/resources/network-memory-thread.d.ts.map +1 -0
- package/dist/resources/network.d.ts +30 -0
- package/dist/resources/network.d.ts.map +1 -0
- package/dist/resources/tool.d.ts +23 -0
- package/dist/resources/tool.d.ts.map +1 -0
- package/dist/resources/vNextNetwork.d.ts +42 -0
- package/dist/resources/vNextNetwork.d.ts.map +1 -0
- package/dist/resources/vector.d.ts +48 -0
- package/dist/resources/vector.d.ts.map +1 -0
- package/dist/resources/workflow.d.ts +154 -0
- package/dist/resources/workflow.d.ts.map +1 -0
- package/dist/types.d.ts +422 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/utils/index.d.ts +3 -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/zod-to-json-schema.d.ts +105 -0
- package/dist/utils/zod-to-json-schema.d.ts.map +1 -0
- package/integration-tests/agui-adapter.test.ts +122 -0
- package/integration-tests/package.json +18 -0
- package/integration-tests/src/mastra/index.ts +35 -0
- package/integration-tests/vitest.config.ts +9 -0
- package/package.json +32 -19
- package/src/adapters/agui.test.ts +322 -0
- package/src/adapters/agui.ts +257 -0
- package/src/client.ts +414 -5
- package/src/example.ts +59 -29
- package/src/index.test.ts +526 -10
- package/src/index.ts +1 -0
- package/src/resources/a2a.ts +88 -0
- package/src/resources/agent.ts +629 -49
- package/src/resources/base.ts +8 -2
- package/src/resources/index.ts +4 -1
- package/src/resources/legacy-workflow.ts +242 -0
- package/src/resources/mcp-tool.ts +48 -0
- package/src/resources/memory-thread.test.ts +285 -0
- package/src/resources/memory-thread.ts +49 -3
- package/src/resources/network-memory-thread.test.ts +269 -0
- package/src/resources/network-memory-thread.ts +81 -0
- package/src/resources/network.ts +11 -17
- package/src/resources/tool.ts +16 -3
- package/src/resources/vNextNetwork.ts +194 -0
- package/src/resources/workflow.ts +289 -94
- package/src/types.ts +298 -20
- package/src/utils/index.ts +11 -0
- package/src/utils/process-client-tools.ts +32 -0
- package/src/utils/zod-to-json-schema.ts +10 -0
- package/src/v2-messages.test.ts +180 -0
- package/tsconfig.build.json +9 -0
- package/tsconfig.json +1 -1
- package/tsup.config.ts +17 -0
- package/dist/index.d.cts +0 -585
package/dist/index.js
CHANGED
|
@@ -1,8 +1,244 @@
|
|
|
1
|
+
import { AbstractAgent, EventType } from '@ag-ui/client';
|
|
2
|
+
import { Observable } from 'rxjs';
|
|
3
|
+
import { processDataStream, parsePartialJson } from '@ai-sdk/ui-utils';
|
|
1
4
|
import { ZodSchema } from 'zod';
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
5
|
+
import originalZodToJsonSchema from 'zod-to-json-schema';
|
|
6
|
+
import { isVercelTool } from '@mastra/core/tools/is-vercel-tool';
|
|
7
|
+
import { v4 } from '@lukeed/uuid';
|
|
8
|
+
import { RuntimeContext } from '@mastra/core/runtime-context';
|
|
4
9
|
|
|
5
|
-
// src/
|
|
10
|
+
// src/adapters/agui.ts
|
|
11
|
+
var AGUIAdapter = class extends AbstractAgent {
|
|
12
|
+
agent;
|
|
13
|
+
resourceId;
|
|
14
|
+
constructor({ agent, agentId, resourceId, ...rest }) {
|
|
15
|
+
super({
|
|
16
|
+
agentId,
|
|
17
|
+
...rest
|
|
18
|
+
});
|
|
19
|
+
this.agent = agent;
|
|
20
|
+
this.resourceId = resourceId;
|
|
21
|
+
}
|
|
22
|
+
run(input) {
|
|
23
|
+
return new Observable((subscriber) => {
|
|
24
|
+
const convertedMessages = convertMessagesToMastraMessages(input.messages);
|
|
25
|
+
subscriber.next({
|
|
26
|
+
type: EventType.RUN_STARTED,
|
|
27
|
+
threadId: input.threadId,
|
|
28
|
+
runId: input.runId
|
|
29
|
+
});
|
|
30
|
+
this.agent.stream({
|
|
31
|
+
threadId: input.threadId,
|
|
32
|
+
resourceId: this.resourceId ?? "",
|
|
33
|
+
runId: input.runId,
|
|
34
|
+
messages: convertedMessages,
|
|
35
|
+
clientTools: input.tools.reduce(
|
|
36
|
+
(acc, tool) => {
|
|
37
|
+
acc[tool.name] = {
|
|
38
|
+
id: tool.name,
|
|
39
|
+
description: tool.description,
|
|
40
|
+
inputSchema: tool.parameters
|
|
41
|
+
};
|
|
42
|
+
return acc;
|
|
43
|
+
},
|
|
44
|
+
{}
|
|
45
|
+
)
|
|
46
|
+
}).then((response) => {
|
|
47
|
+
let currentMessageId = void 0;
|
|
48
|
+
let isInTextMessage = false;
|
|
49
|
+
return response.processDataStream({
|
|
50
|
+
onTextPart: (text) => {
|
|
51
|
+
if (currentMessageId === void 0) {
|
|
52
|
+
currentMessageId = generateUUID();
|
|
53
|
+
const message2 = {
|
|
54
|
+
type: EventType.TEXT_MESSAGE_START,
|
|
55
|
+
messageId: currentMessageId,
|
|
56
|
+
role: "assistant"
|
|
57
|
+
};
|
|
58
|
+
subscriber.next(message2);
|
|
59
|
+
isInTextMessage = true;
|
|
60
|
+
}
|
|
61
|
+
const message = {
|
|
62
|
+
type: EventType.TEXT_MESSAGE_CONTENT,
|
|
63
|
+
messageId: currentMessageId,
|
|
64
|
+
delta: text
|
|
65
|
+
};
|
|
66
|
+
subscriber.next(message);
|
|
67
|
+
},
|
|
68
|
+
onFinishMessagePart: () => {
|
|
69
|
+
if (currentMessageId !== void 0) {
|
|
70
|
+
const message = {
|
|
71
|
+
type: EventType.TEXT_MESSAGE_END,
|
|
72
|
+
messageId: currentMessageId
|
|
73
|
+
};
|
|
74
|
+
subscriber.next(message);
|
|
75
|
+
isInTextMessage = false;
|
|
76
|
+
}
|
|
77
|
+
subscriber.next({
|
|
78
|
+
type: EventType.RUN_FINISHED,
|
|
79
|
+
threadId: input.threadId,
|
|
80
|
+
runId: input.runId
|
|
81
|
+
});
|
|
82
|
+
subscriber.complete();
|
|
83
|
+
},
|
|
84
|
+
onToolCallPart(streamPart) {
|
|
85
|
+
const parentMessageId = currentMessageId || generateUUID();
|
|
86
|
+
if (isInTextMessage) {
|
|
87
|
+
const message = {
|
|
88
|
+
type: EventType.TEXT_MESSAGE_END,
|
|
89
|
+
messageId: parentMessageId
|
|
90
|
+
};
|
|
91
|
+
subscriber.next(message);
|
|
92
|
+
isInTextMessage = false;
|
|
93
|
+
}
|
|
94
|
+
subscriber.next({
|
|
95
|
+
type: EventType.TOOL_CALL_START,
|
|
96
|
+
toolCallId: streamPart.toolCallId,
|
|
97
|
+
toolCallName: streamPart.toolName,
|
|
98
|
+
parentMessageId
|
|
99
|
+
});
|
|
100
|
+
subscriber.next({
|
|
101
|
+
type: EventType.TOOL_CALL_ARGS,
|
|
102
|
+
toolCallId: streamPart.toolCallId,
|
|
103
|
+
delta: JSON.stringify(streamPart.args),
|
|
104
|
+
parentMessageId
|
|
105
|
+
});
|
|
106
|
+
subscriber.next({
|
|
107
|
+
type: EventType.TOOL_CALL_END,
|
|
108
|
+
toolCallId: streamPart.toolCallId,
|
|
109
|
+
parentMessageId
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
}).catch((error) => {
|
|
114
|
+
console.error("error", error);
|
|
115
|
+
subscriber.error(error);
|
|
116
|
+
});
|
|
117
|
+
return () => {
|
|
118
|
+
};
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
function generateUUID() {
|
|
123
|
+
if (typeof crypto !== "undefined") {
|
|
124
|
+
if (typeof crypto.randomUUID === "function") {
|
|
125
|
+
return crypto.randomUUID();
|
|
126
|
+
}
|
|
127
|
+
if (typeof crypto.getRandomValues === "function") {
|
|
128
|
+
const buffer = new Uint8Array(16);
|
|
129
|
+
crypto.getRandomValues(buffer);
|
|
130
|
+
buffer[6] = buffer[6] & 15 | 64;
|
|
131
|
+
buffer[8] = buffer[8] & 63 | 128;
|
|
132
|
+
let hex = "";
|
|
133
|
+
for (let i = 0; i < 16; i++) {
|
|
134
|
+
hex += buffer[i].toString(16).padStart(2, "0");
|
|
135
|
+
if (i === 3 || i === 5 || i === 7 || i === 9) hex += "-";
|
|
136
|
+
}
|
|
137
|
+
return hex;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
|
141
|
+
const r = Math.random() * 16 | 0;
|
|
142
|
+
const v = c === "x" ? r : r & 3 | 8;
|
|
143
|
+
return v.toString(16);
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
function convertMessagesToMastraMessages(messages) {
|
|
147
|
+
const result = [];
|
|
148
|
+
const toolCallsWithResults = /* @__PURE__ */ new Set();
|
|
149
|
+
for (const message of messages) {
|
|
150
|
+
if (message.role === "tool" && message.toolCallId) {
|
|
151
|
+
toolCallsWithResults.add(message.toolCallId);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
for (const message of messages) {
|
|
155
|
+
if (message.role === "assistant") {
|
|
156
|
+
const parts = message.content ? [{ type: "text", text: message.content }] : [];
|
|
157
|
+
for (const toolCall of message.toolCalls ?? []) {
|
|
158
|
+
parts.push({
|
|
159
|
+
type: "tool-call",
|
|
160
|
+
toolCallId: toolCall.id,
|
|
161
|
+
toolName: toolCall.function.name,
|
|
162
|
+
args: JSON.parse(toolCall.function.arguments)
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
result.push({
|
|
166
|
+
role: "assistant",
|
|
167
|
+
content: parts
|
|
168
|
+
});
|
|
169
|
+
if (message.toolCalls?.length) {
|
|
170
|
+
for (const toolCall of message.toolCalls) {
|
|
171
|
+
if (!toolCallsWithResults.has(toolCall.id)) {
|
|
172
|
+
result.push({
|
|
173
|
+
role: "tool",
|
|
174
|
+
content: [
|
|
175
|
+
{
|
|
176
|
+
type: "tool-result",
|
|
177
|
+
toolCallId: toolCall.id,
|
|
178
|
+
toolName: toolCall.function.name,
|
|
179
|
+
result: JSON.parse(toolCall.function.arguments)
|
|
180
|
+
// This is still wrong but matches test expectations
|
|
181
|
+
}
|
|
182
|
+
]
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
} else if (message.role === "user") {
|
|
188
|
+
result.push({
|
|
189
|
+
role: "user",
|
|
190
|
+
content: message.content || ""
|
|
191
|
+
});
|
|
192
|
+
} else if (message.role === "tool") {
|
|
193
|
+
result.push({
|
|
194
|
+
role: "tool",
|
|
195
|
+
content: [
|
|
196
|
+
{
|
|
197
|
+
type: "tool-result",
|
|
198
|
+
toolCallId: message.toolCallId || "unknown",
|
|
199
|
+
toolName: "unknown",
|
|
200
|
+
// toolName is not available in tool messages from CopilotKit
|
|
201
|
+
result: message.content
|
|
202
|
+
}
|
|
203
|
+
]
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return result;
|
|
208
|
+
}
|
|
209
|
+
function zodToJsonSchema(zodSchema) {
|
|
210
|
+
if (!(zodSchema instanceof ZodSchema)) {
|
|
211
|
+
return zodSchema;
|
|
212
|
+
}
|
|
213
|
+
return originalZodToJsonSchema(zodSchema, { $refStrategy: "none" });
|
|
214
|
+
}
|
|
215
|
+
function processClientTools(clientTools) {
|
|
216
|
+
if (!clientTools) {
|
|
217
|
+
return void 0;
|
|
218
|
+
}
|
|
219
|
+
return Object.fromEntries(
|
|
220
|
+
Object.entries(clientTools).map(([key, value]) => {
|
|
221
|
+
if (isVercelTool(value)) {
|
|
222
|
+
return [
|
|
223
|
+
key,
|
|
224
|
+
{
|
|
225
|
+
...value,
|
|
226
|
+
parameters: value.parameters ? zodToJsonSchema(value.parameters) : void 0
|
|
227
|
+
}
|
|
228
|
+
];
|
|
229
|
+
} else {
|
|
230
|
+
return [
|
|
231
|
+
key,
|
|
232
|
+
{
|
|
233
|
+
...value,
|
|
234
|
+
inputSchema: value.inputSchema ? zodToJsonSchema(value.inputSchema) : void 0,
|
|
235
|
+
outputSchema: value.outputSchema ? zodToJsonSchema(value.outputSchema) : void 0
|
|
236
|
+
}
|
|
237
|
+
];
|
|
238
|
+
}
|
|
239
|
+
})
|
|
240
|
+
);
|
|
241
|
+
}
|
|
6
242
|
|
|
7
243
|
// src/resources/base.ts
|
|
8
244
|
var BaseResource = class {
|
|
@@ -22,14 +258,16 @@ var BaseResource = class {
|
|
|
22
258
|
let delay = backoffMs;
|
|
23
259
|
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
24
260
|
try {
|
|
25
|
-
const response = await fetch(`${baseUrl}${path}`, {
|
|
261
|
+
const response = await fetch(`${baseUrl.replace(/\/$/, "")}${path}`, {
|
|
26
262
|
...options,
|
|
27
263
|
headers: {
|
|
264
|
+
...options.body && !(options.body instanceof FormData) && (options.method === "POST" || options.method === "PUT") ? { "content-type": "application/json" } : {},
|
|
28
265
|
...headers,
|
|
29
266
|
...options.headers
|
|
30
267
|
// TODO: Bring this back once we figure out what we/users need to do to make this work with cross-origin requests
|
|
31
268
|
// 'x-mastra-client-type': 'js',
|
|
32
269
|
},
|
|
270
|
+
signal: this.options.abortSignal,
|
|
33
271
|
body: options.body instanceof FormData ? options.body : options.body ? JSON.stringify(options.body) : void 0
|
|
34
272
|
});
|
|
35
273
|
if (!response.ok) {
|
|
@@ -62,8 +300,15 @@ var BaseResource = class {
|
|
|
62
300
|
throw lastError || new Error("Request failed");
|
|
63
301
|
}
|
|
64
302
|
};
|
|
65
|
-
|
|
66
|
-
|
|
303
|
+
function parseClientRuntimeContext(runtimeContext) {
|
|
304
|
+
if (runtimeContext) {
|
|
305
|
+
if (runtimeContext instanceof RuntimeContext) {
|
|
306
|
+
return Object.fromEntries(runtimeContext.entries());
|
|
307
|
+
}
|
|
308
|
+
return runtimeContext;
|
|
309
|
+
}
|
|
310
|
+
return void 0;
|
|
311
|
+
}
|
|
67
312
|
var AgentVoice = class extends BaseResource {
|
|
68
313
|
constructor(options, agentId) {
|
|
69
314
|
super(options);
|
|
@@ -110,6 +355,13 @@ var AgentVoice = class extends BaseResource {
|
|
|
110
355
|
getSpeakers() {
|
|
111
356
|
return this.request(`/api/agents/${this.agentId}/voice/speakers`);
|
|
112
357
|
}
|
|
358
|
+
/**
|
|
359
|
+
* Get the listener configuration for the agent's voice provider
|
|
360
|
+
* @returns Promise containing a check if the agent has listening capabilities
|
|
361
|
+
*/
|
|
362
|
+
getListener() {
|
|
363
|
+
return this.request(`/api/agents/${this.agentId}/voice/listener`);
|
|
364
|
+
}
|
|
113
365
|
};
|
|
114
366
|
var Agent = class extends BaseResource {
|
|
115
367
|
constructor(options, agentId) {
|
|
@@ -125,21 +377,325 @@ var Agent = class extends BaseResource {
|
|
|
125
377
|
details() {
|
|
126
378
|
return this.request(`/api/agents/${this.agentId}`);
|
|
127
379
|
}
|
|
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) {
|
|
380
|
+
async generate(params) {
|
|
134
381
|
const processedParams = {
|
|
135
382
|
...params,
|
|
136
|
-
output: params.output
|
|
137
|
-
experimental_output: params.experimental_output
|
|
383
|
+
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
384
|
+
experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
|
|
385
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
386
|
+
clientTools: processClientTools(params.clientTools)
|
|
138
387
|
};
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
388
|
+
const { runId, resourceId, threadId, runtimeContext } = processedParams;
|
|
389
|
+
const response = await this.request(
|
|
390
|
+
`/api/agents/${this.agentId}/generate`,
|
|
391
|
+
{
|
|
392
|
+
method: "POST",
|
|
393
|
+
body: processedParams
|
|
394
|
+
}
|
|
395
|
+
);
|
|
396
|
+
if (response.finishReason === "tool-calls") {
|
|
397
|
+
const toolCalls = response.toolCalls;
|
|
398
|
+
if (!toolCalls || !Array.isArray(toolCalls)) {
|
|
399
|
+
return response;
|
|
400
|
+
}
|
|
401
|
+
for (const toolCall of toolCalls) {
|
|
402
|
+
const clientTool = params.clientTools?.[toolCall.toolName];
|
|
403
|
+
if (clientTool && clientTool.execute) {
|
|
404
|
+
const result = await clientTool.execute(
|
|
405
|
+
{ context: toolCall?.args, runId, resourceId, threadId, runtimeContext },
|
|
406
|
+
{
|
|
407
|
+
messages: response.messages,
|
|
408
|
+
toolCallId: toolCall?.toolCallId
|
|
409
|
+
}
|
|
410
|
+
);
|
|
411
|
+
const updatedMessages = [
|
|
412
|
+
{
|
|
413
|
+
role: "user",
|
|
414
|
+
content: params.messages
|
|
415
|
+
},
|
|
416
|
+
...response.response.messages,
|
|
417
|
+
{
|
|
418
|
+
role: "tool",
|
|
419
|
+
content: [
|
|
420
|
+
{
|
|
421
|
+
type: "tool-result",
|
|
422
|
+
toolCallId: toolCall.toolCallId,
|
|
423
|
+
toolName: toolCall.toolName,
|
|
424
|
+
result
|
|
425
|
+
}
|
|
426
|
+
]
|
|
427
|
+
}
|
|
428
|
+
];
|
|
429
|
+
return this.generate({
|
|
430
|
+
...params,
|
|
431
|
+
messages: updatedMessages
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
return response;
|
|
437
|
+
}
|
|
438
|
+
async processChatResponse({
|
|
439
|
+
stream,
|
|
440
|
+
update,
|
|
441
|
+
onToolCall,
|
|
442
|
+
onFinish,
|
|
443
|
+
getCurrentDate = () => /* @__PURE__ */ new Date(),
|
|
444
|
+
lastMessage
|
|
445
|
+
}) {
|
|
446
|
+
const replaceLastMessage = lastMessage?.role === "assistant";
|
|
447
|
+
let step = replaceLastMessage ? 1 + // find max step in existing tool invocations:
|
|
448
|
+
(lastMessage.toolInvocations?.reduce((max, toolInvocation) => {
|
|
449
|
+
return Math.max(max, toolInvocation.step ?? 0);
|
|
450
|
+
}, 0) ?? 0) : 0;
|
|
451
|
+
const message = replaceLastMessage ? structuredClone(lastMessage) : {
|
|
452
|
+
id: v4(),
|
|
453
|
+
createdAt: getCurrentDate(),
|
|
454
|
+
role: "assistant",
|
|
455
|
+
content: "",
|
|
456
|
+
parts: []
|
|
457
|
+
};
|
|
458
|
+
let currentTextPart = void 0;
|
|
459
|
+
let currentReasoningPart = void 0;
|
|
460
|
+
let currentReasoningTextDetail = void 0;
|
|
461
|
+
function updateToolInvocationPart(toolCallId, invocation) {
|
|
462
|
+
const part = message.parts.find(
|
|
463
|
+
(part2) => part2.type === "tool-invocation" && part2.toolInvocation.toolCallId === toolCallId
|
|
464
|
+
);
|
|
465
|
+
if (part != null) {
|
|
466
|
+
part.toolInvocation = invocation;
|
|
467
|
+
} else {
|
|
468
|
+
message.parts.push({
|
|
469
|
+
type: "tool-invocation",
|
|
470
|
+
toolInvocation: invocation
|
|
471
|
+
});
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
const data = [];
|
|
475
|
+
let messageAnnotations = replaceLastMessage ? lastMessage?.annotations : void 0;
|
|
476
|
+
const partialToolCalls = {};
|
|
477
|
+
let usage = {
|
|
478
|
+
completionTokens: NaN,
|
|
479
|
+
promptTokens: NaN,
|
|
480
|
+
totalTokens: NaN
|
|
481
|
+
};
|
|
482
|
+
let finishReason = "unknown";
|
|
483
|
+
function execUpdate() {
|
|
484
|
+
const copiedData = [...data];
|
|
485
|
+
if (messageAnnotations?.length) {
|
|
486
|
+
message.annotations = messageAnnotations;
|
|
487
|
+
}
|
|
488
|
+
const copiedMessage = {
|
|
489
|
+
// deep copy the message to ensure that deep changes (msg attachments) are updated
|
|
490
|
+
// with SolidJS. SolidJS uses referential integration of sub-objects to detect changes.
|
|
491
|
+
...structuredClone(message),
|
|
492
|
+
// add a revision id to ensure that the message is updated with SWR. SWR uses a
|
|
493
|
+
// hashing approach by default to detect changes, but it only works for shallow
|
|
494
|
+
// changes. This is why we need to add a revision id to ensure that the message
|
|
495
|
+
// is updated with SWR (without it, the changes get stuck in SWR and are not
|
|
496
|
+
// forwarded to rendering):
|
|
497
|
+
revisionId: v4()
|
|
498
|
+
};
|
|
499
|
+
update({
|
|
500
|
+
message: copiedMessage,
|
|
501
|
+
data: copiedData,
|
|
502
|
+
replaceLastMessage
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
await processDataStream({
|
|
506
|
+
stream,
|
|
507
|
+
onTextPart(value) {
|
|
508
|
+
if (currentTextPart == null) {
|
|
509
|
+
currentTextPart = {
|
|
510
|
+
type: "text",
|
|
511
|
+
text: value
|
|
512
|
+
};
|
|
513
|
+
message.parts.push(currentTextPart);
|
|
514
|
+
} else {
|
|
515
|
+
currentTextPart.text += value;
|
|
516
|
+
}
|
|
517
|
+
message.content += value;
|
|
518
|
+
execUpdate();
|
|
519
|
+
},
|
|
520
|
+
onReasoningPart(value) {
|
|
521
|
+
if (currentReasoningTextDetail == null) {
|
|
522
|
+
currentReasoningTextDetail = { type: "text", text: value };
|
|
523
|
+
if (currentReasoningPart != null) {
|
|
524
|
+
currentReasoningPart.details.push(currentReasoningTextDetail);
|
|
525
|
+
}
|
|
526
|
+
} else {
|
|
527
|
+
currentReasoningTextDetail.text += value;
|
|
528
|
+
}
|
|
529
|
+
if (currentReasoningPart == null) {
|
|
530
|
+
currentReasoningPart = {
|
|
531
|
+
type: "reasoning",
|
|
532
|
+
reasoning: value,
|
|
533
|
+
details: [currentReasoningTextDetail]
|
|
534
|
+
};
|
|
535
|
+
message.parts.push(currentReasoningPart);
|
|
536
|
+
} else {
|
|
537
|
+
currentReasoningPart.reasoning += value;
|
|
538
|
+
}
|
|
539
|
+
message.reasoning = (message.reasoning ?? "") + value;
|
|
540
|
+
execUpdate();
|
|
541
|
+
},
|
|
542
|
+
onReasoningSignaturePart(value) {
|
|
543
|
+
if (currentReasoningTextDetail != null) {
|
|
544
|
+
currentReasoningTextDetail.signature = value.signature;
|
|
545
|
+
}
|
|
546
|
+
},
|
|
547
|
+
onRedactedReasoningPart(value) {
|
|
548
|
+
if (currentReasoningPart == null) {
|
|
549
|
+
currentReasoningPart = {
|
|
550
|
+
type: "reasoning",
|
|
551
|
+
reasoning: "",
|
|
552
|
+
details: []
|
|
553
|
+
};
|
|
554
|
+
message.parts.push(currentReasoningPart);
|
|
555
|
+
}
|
|
556
|
+
currentReasoningPart.details.push({
|
|
557
|
+
type: "redacted",
|
|
558
|
+
data: value.data
|
|
559
|
+
});
|
|
560
|
+
currentReasoningTextDetail = void 0;
|
|
561
|
+
execUpdate();
|
|
562
|
+
},
|
|
563
|
+
onFilePart(value) {
|
|
564
|
+
message.parts.push({
|
|
565
|
+
type: "file",
|
|
566
|
+
mimeType: value.mimeType,
|
|
567
|
+
data: value.data
|
|
568
|
+
});
|
|
569
|
+
execUpdate();
|
|
570
|
+
},
|
|
571
|
+
onSourcePart(value) {
|
|
572
|
+
message.parts.push({
|
|
573
|
+
type: "source",
|
|
574
|
+
source: value
|
|
575
|
+
});
|
|
576
|
+
execUpdate();
|
|
577
|
+
},
|
|
578
|
+
onToolCallStreamingStartPart(value) {
|
|
579
|
+
if (message.toolInvocations == null) {
|
|
580
|
+
message.toolInvocations = [];
|
|
581
|
+
}
|
|
582
|
+
partialToolCalls[value.toolCallId] = {
|
|
583
|
+
text: "",
|
|
584
|
+
step,
|
|
585
|
+
toolName: value.toolName,
|
|
586
|
+
index: message.toolInvocations.length
|
|
587
|
+
};
|
|
588
|
+
const invocation = {
|
|
589
|
+
state: "partial-call",
|
|
590
|
+
step,
|
|
591
|
+
toolCallId: value.toolCallId,
|
|
592
|
+
toolName: value.toolName,
|
|
593
|
+
args: void 0
|
|
594
|
+
};
|
|
595
|
+
message.toolInvocations.push(invocation);
|
|
596
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
597
|
+
execUpdate();
|
|
598
|
+
},
|
|
599
|
+
onToolCallDeltaPart(value) {
|
|
600
|
+
const partialToolCall = partialToolCalls[value.toolCallId];
|
|
601
|
+
partialToolCall.text += value.argsTextDelta;
|
|
602
|
+
const { value: partialArgs } = parsePartialJson(partialToolCall.text);
|
|
603
|
+
const invocation = {
|
|
604
|
+
state: "partial-call",
|
|
605
|
+
step: partialToolCall.step,
|
|
606
|
+
toolCallId: value.toolCallId,
|
|
607
|
+
toolName: partialToolCall.toolName,
|
|
608
|
+
args: partialArgs
|
|
609
|
+
};
|
|
610
|
+
message.toolInvocations[partialToolCall.index] = invocation;
|
|
611
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
612
|
+
execUpdate();
|
|
613
|
+
},
|
|
614
|
+
async onToolCallPart(value) {
|
|
615
|
+
const invocation = {
|
|
616
|
+
state: "call",
|
|
617
|
+
step,
|
|
618
|
+
...value
|
|
619
|
+
};
|
|
620
|
+
if (partialToolCalls[value.toolCallId] != null) {
|
|
621
|
+
message.toolInvocations[partialToolCalls[value.toolCallId].index] = invocation;
|
|
622
|
+
} else {
|
|
623
|
+
if (message.toolInvocations == null) {
|
|
624
|
+
message.toolInvocations = [];
|
|
625
|
+
}
|
|
626
|
+
message.toolInvocations.push(invocation);
|
|
627
|
+
}
|
|
628
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
629
|
+
execUpdate();
|
|
630
|
+
if (onToolCall) {
|
|
631
|
+
const result = await onToolCall({ toolCall: value });
|
|
632
|
+
if (result != null) {
|
|
633
|
+
const invocation2 = {
|
|
634
|
+
state: "result",
|
|
635
|
+
step,
|
|
636
|
+
...value,
|
|
637
|
+
result
|
|
638
|
+
};
|
|
639
|
+
message.toolInvocations[message.toolInvocations.length - 1] = invocation2;
|
|
640
|
+
updateToolInvocationPart(value.toolCallId, invocation2);
|
|
641
|
+
execUpdate();
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
},
|
|
645
|
+
onToolResultPart(value) {
|
|
646
|
+
const toolInvocations = message.toolInvocations;
|
|
647
|
+
if (toolInvocations == null) {
|
|
648
|
+
throw new Error("tool_result must be preceded by a tool_call");
|
|
649
|
+
}
|
|
650
|
+
const toolInvocationIndex = toolInvocations.findIndex((invocation2) => invocation2.toolCallId === value.toolCallId);
|
|
651
|
+
if (toolInvocationIndex === -1) {
|
|
652
|
+
throw new Error("tool_result must be preceded by a tool_call with the same toolCallId");
|
|
653
|
+
}
|
|
654
|
+
const invocation = {
|
|
655
|
+
...toolInvocations[toolInvocationIndex],
|
|
656
|
+
state: "result",
|
|
657
|
+
...value
|
|
658
|
+
};
|
|
659
|
+
toolInvocations[toolInvocationIndex] = invocation;
|
|
660
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
661
|
+
execUpdate();
|
|
662
|
+
},
|
|
663
|
+
onDataPart(value) {
|
|
664
|
+
data.push(...value);
|
|
665
|
+
execUpdate();
|
|
666
|
+
},
|
|
667
|
+
onMessageAnnotationsPart(value) {
|
|
668
|
+
if (messageAnnotations == null) {
|
|
669
|
+
messageAnnotations = [...value];
|
|
670
|
+
} else {
|
|
671
|
+
messageAnnotations.push(...value);
|
|
672
|
+
}
|
|
673
|
+
execUpdate();
|
|
674
|
+
},
|
|
675
|
+
onFinishStepPart(value) {
|
|
676
|
+
step += 1;
|
|
677
|
+
currentTextPart = value.isContinued ? currentTextPart : void 0;
|
|
678
|
+
currentReasoningPart = void 0;
|
|
679
|
+
currentReasoningTextDetail = void 0;
|
|
680
|
+
},
|
|
681
|
+
onStartStepPart(value) {
|
|
682
|
+
if (!replaceLastMessage) {
|
|
683
|
+
message.id = value.messageId;
|
|
684
|
+
}
|
|
685
|
+
message.parts.push({ type: "step-start" });
|
|
686
|
+
execUpdate();
|
|
687
|
+
},
|
|
688
|
+
onFinishMessagePart(value) {
|
|
689
|
+
finishReason = value.finishReason;
|
|
690
|
+
if (value.usage != null) {
|
|
691
|
+
usage = value.usage;
|
|
692
|
+
}
|
|
693
|
+
},
|
|
694
|
+
onErrorPart(error) {
|
|
695
|
+
throw new Error(error);
|
|
696
|
+
}
|
|
142
697
|
});
|
|
698
|
+
onFinish?.({ message, finishReason, usage });
|
|
143
699
|
}
|
|
144
700
|
/**
|
|
145
701
|
* Streams a response from the agent
|
|
@@ -149,9 +705,30 @@ var Agent = class extends BaseResource {
|
|
|
149
705
|
async stream(params) {
|
|
150
706
|
const processedParams = {
|
|
151
707
|
...params,
|
|
152
|
-
output: params.output
|
|
153
|
-
experimental_output: params.experimental_output
|
|
708
|
+
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
709
|
+
experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
|
|
710
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
711
|
+
clientTools: processClientTools(params.clientTools)
|
|
712
|
+
};
|
|
713
|
+
const { readable, writable } = new TransformStream();
|
|
714
|
+
const response = await this.processStreamResponse(processedParams, writable);
|
|
715
|
+
const streamResponse = new Response(readable, {
|
|
716
|
+
status: response.status,
|
|
717
|
+
statusText: response.statusText,
|
|
718
|
+
headers: response.headers
|
|
719
|
+
});
|
|
720
|
+
streamResponse.processDataStream = async (options = {}) => {
|
|
721
|
+
await processDataStream({
|
|
722
|
+
stream: streamResponse.body,
|
|
723
|
+
...options
|
|
724
|
+
});
|
|
154
725
|
};
|
|
726
|
+
return streamResponse;
|
|
727
|
+
}
|
|
728
|
+
/**
|
|
729
|
+
* Processes the stream response and handles tool calls
|
|
730
|
+
*/
|
|
731
|
+
async processStreamResponse(processedParams, writable) {
|
|
155
732
|
const response = await this.request(`/api/agents/${this.agentId}/stream`, {
|
|
156
733
|
method: "POST",
|
|
157
734
|
body: processedParams,
|
|
@@ -160,12 +737,104 @@ var Agent = class extends BaseResource {
|
|
|
160
737
|
if (!response.body) {
|
|
161
738
|
throw new Error("No response body");
|
|
162
739
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
740
|
+
try {
|
|
741
|
+
let toolCalls = [];
|
|
742
|
+
let messages = [];
|
|
743
|
+
const [streamForWritable, streamForProcessing] = response.body.tee();
|
|
744
|
+
streamForWritable.pipeTo(writable, {
|
|
745
|
+
preventClose: true
|
|
746
|
+
}).catch((error) => {
|
|
747
|
+
console.error("Error piping to writable stream:", error);
|
|
167
748
|
});
|
|
168
|
-
|
|
749
|
+
this.processChatResponse({
|
|
750
|
+
stream: streamForProcessing,
|
|
751
|
+
update: ({ message }) => {
|
|
752
|
+
const existingIndex = messages.findIndex((m) => m.id === message.id);
|
|
753
|
+
if (existingIndex !== -1) {
|
|
754
|
+
messages[existingIndex] = message;
|
|
755
|
+
} else {
|
|
756
|
+
messages.push(message);
|
|
757
|
+
}
|
|
758
|
+
},
|
|
759
|
+
onFinish: async ({ finishReason, message }) => {
|
|
760
|
+
if (finishReason === "tool-calls") {
|
|
761
|
+
const toolCall = [...message?.parts ?? []].reverse().find((part) => part.type === "tool-invocation")?.toolInvocation;
|
|
762
|
+
if (toolCall) {
|
|
763
|
+
toolCalls.push(toolCall);
|
|
764
|
+
}
|
|
765
|
+
for (const toolCall2 of toolCalls) {
|
|
766
|
+
const clientTool = processedParams.clientTools?.[toolCall2.toolName];
|
|
767
|
+
if (clientTool && clientTool.execute) {
|
|
768
|
+
const result = await clientTool.execute(
|
|
769
|
+
{
|
|
770
|
+
context: toolCall2?.args,
|
|
771
|
+
runId: processedParams.runId,
|
|
772
|
+
resourceId: processedParams.resourceId,
|
|
773
|
+
threadId: processedParams.threadId,
|
|
774
|
+
runtimeContext: processedParams.runtimeContext
|
|
775
|
+
},
|
|
776
|
+
{
|
|
777
|
+
messages: response.messages,
|
|
778
|
+
toolCallId: toolCall2?.toolCallId
|
|
779
|
+
}
|
|
780
|
+
);
|
|
781
|
+
const lastMessage = JSON.parse(JSON.stringify(messages[messages.length - 1]));
|
|
782
|
+
const toolInvocationPart = lastMessage?.parts?.find(
|
|
783
|
+
(part) => part.type === "tool-invocation" && part.toolInvocation?.toolCallId === toolCall2.toolCallId
|
|
784
|
+
);
|
|
785
|
+
if (toolInvocationPart) {
|
|
786
|
+
toolInvocationPart.toolInvocation = {
|
|
787
|
+
...toolInvocationPart.toolInvocation,
|
|
788
|
+
state: "result",
|
|
789
|
+
result
|
|
790
|
+
};
|
|
791
|
+
}
|
|
792
|
+
const toolInvocation = lastMessage?.toolInvocations?.find(
|
|
793
|
+
(toolInvocation2) => toolInvocation2.toolCallId === toolCall2.toolCallId
|
|
794
|
+
);
|
|
795
|
+
if (toolInvocation) {
|
|
796
|
+
toolInvocation.state = "result";
|
|
797
|
+
toolInvocation.result = result;
|
|
798
|
+
}
|
|
799
|
+
const writer = writable.getWriter();
|
|
800
|
+
try {
|
|
801
|
+
await writer.write(
|
|
802
|
+
new TextEncoder().encode(
|
|
803
|
+
"a:" + JSON.stringify({
|
|
804
|
+
toolCallId: toolCall2.toolCallId,
|
|
805
|
+
result
|
|
806
|
+
}) + "\n"
|
|
807
|
+
)
|
|
808
|
+
);
|
|
809
|
+
} finally {
|
|
810
|
+
writer.releaseLock();
|
|
811
|
+
}
|
|
812
|
+
const originalMessages = processedParams.messages;
|
|
813
|
+
const messageArray = Array.isArray(originalMessages) ? originalMessages : [originalMessages];
|
|
814
|
+
this.processStreamResponse(
|
|
815
|
+
{
|
|
816
|
+
...processedParams,
|
|
817
|
+
messages: [...messageArray, ...messages.filter((m) => m.id !== lastMessage.id), lastMessage]
|
|
818
|
+
},
|
|
819
|
+
writable
|
|
820
|
+
).catch((error) => {
|
|
821
|
+
console.error("Error processing stream response:", error);
|
|
822
|
+
});
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
} else {
|
|
826
|
+
setTimeout(() => {
|
|
827
|
+
writable.close();
|
|
828
|
+
}, 0);
|
|
829
|
+
}
|
|
830
|
+
},
|
|
831
|
+
lastMessage: void 0
|
|
832
|
+
}).catch((error) => {
|
|
833
|
+
console.error("Error processing stream response:", error);
|
|
834
|
+
});
|
|
835
|
+
} catch (error) {
|
|
836
|
+
console.error("Error processing stream response:", error);
|
|
837
|
+
}
|
|
169
838
|
return response;
|
|
170
839
|
}
|
|
171
840
|
/**
|
|
@@ -176,6 +845,22 @@ var Agent = class extends BaseResource {
|
|
|
176
845
|
getTool(toolId) {
|
|
177
846
|
return this.request(`/api/agents/${this.agentId}/tools/${toolId}`);
|
|
178
847
|
}
|
|
848
|
+
/**
|
|
849
|
+
* Executes a tool for the agent
|
|
850
|
+
* @param toolId - ID of the tool to execute
|
|
851
|
+
* @param params - Parameters required for tool execution
|
|
852
|
+
* @returns Promise containing the tool execution results
|
|
853
|
+
*/
|
|
854
|
+
executeTool(toolId, params) {
|
|
855
|
+
const body = {
|
|
856
|
+
data: params.data,
|
|
857
|
+
runtimeContext: params.runtimeContext ? Object.fromEntries(params.runtimeContext.entries()) : void 0
|
|
858
|
+
};
|
|
859
|
+
return this.request(`/api/agents/${this.agentId}/tools/${toolId}/execute`, {
|
|
860
|
+
method: "POST",
|
|
861
|
+
body
|
|
862
|
+
});
|
|
863
|
+
}
|
|
179
864
|
/**
|
|
180
865
|
* Retrieves evaluation results for the agent
|
|
181
866
|
* @returns Promise containing agent evaluations
|
|
@@ -211,8 +896,8 @@ var Network = class extends BaseResource {
|
|
|
211
896
|
generate(params) {
|
|
212
897
|
const processedParams = {
|
|
213
898
|
...params,
|
|
214
|
-
output:
|
|
215
|
-
experimental_output:
|
|
899
|
+
output: zodToJsonSchema(params.output),
|
|
900
|
+
experimental_output: zodToJsonSchema(params.experimental_output)
|
|
216
901
|
};
|
|
217
902
|
return this.request(`/api/networks/${this.networkId}/generate`, {
|
|
218
903
|
method: "POST",
|
|
@@ -227,8 +912,8 @@ var Network = class extends BaseResource {
|
|
|
227
912
|
async stream(params) {
|
|
228
913
|
const processedParams = {
|
|
229
914
|
...params,
|
|
230
|
-
output:
|
|
231
|
-
experimental_output:
|
|
915
|
+
output: zodToJsonSchema(params.output),
|
|
916
|
+
experimental_output: zodToJsonSchema(params.experimental_output)
|
|
232
917
|
};
|
|
233
918
|
const response = await this.request(`/api/networks/${this.networkId}/stream`, {
|
|
234
919
|
method: "POST",
|
|
@@ -284,10 +969,45 @@ var MemoryThread = class extends BaseResource {
|
|
|
284
969
|
}
|
|
285
970
|
/**
|
|
286
971
|
* Retrieves messages associated with the thread
|
|
972
|
+
* @param params - Optional parameters including limit for number of messages to retrieve
|
|
287
973
|
* @returns Promise containing thread messages and UI messages
|
|
288
974
|
*/
|
|
289
|
-
getMessages() {
|
|
290
|
-
|
|
975
|
+
getMessages(params) {
|
|
976
|
+
const query = new URLSearchParams({
|
|
977
|
+
agentId: this.agentId,
|
|
978
|
+
...params?.limit ? { limit: params.limit.toString() } : {}
|
|
979
|
+
});
|
|
980
|
+
return this.request(`/api/memory/threads/${this.threadId}/messages?${query.toString()}`);
|
|
981
|
+
}
|
|
982
|
+
/**
|
|
983
|
+
* Retrieves paginated messages associated with the thread with advanced filtering and selection options
|
|
984
|
+
* @param params - Pagination parameters including selectBy criteria, page, perPage, date ranges, and message inclusion options
|
|
985
|
+
* @returns Promise containing paginated thread messages with pagination metadata (total, page, perPage, hasMore)
|
|
986
|
+
*/
|
|
987
|
+
getMessagesPaginated({
|
|
988
|
+
selectBy,
|
|
989
|
+
...rest
|
|
990
|
+
}) {
|
|
991
|
+
const query = new URLSearchParams({
|
|
992
|
+
...rest,
|
|
993
|
+
...selectBy ? { selectBy: JSON.stringify(selectBy) } : {}
|
|
994
|
+
});
|
|
995
|
+
return this.request(`/api/memory/threads/${this.threadId}/messages/paginated?${query.toString()}`);
|
|
996
|
+
}
|
|
997
|
+
/**
|
|
998
|
+
* Deletes one or more messages from the thread
|
|
999
|
+
* @param messageIds - Can be a single message ID (string), array of message IDs,
|
|
1000
|
+
* message object with id property, or array of message objects
|
|
1001
|
+
* @returns Promise containing deletion result
|
|
1002
|
+
*/
|
|
1003
|
+
deleteMessages(messageIds) {
|
|
1004
|
+
const query = new URLSearchParams({
|
|
1005
|
+
agentId: this.agentId
|
|
1006
|
+
});
|
|
1007
|
+
return this.request(`/api/memory/messages/delete?${query.toString()}`, {
|
|
1008
|
+
method: "POST",
|
|
1009
|
+
body: { messageIds }
|
|
1010
|
+
});
|
|
291
1011
|
}
|
|
292
1012
|
};
|
|
293
1013
|
|
|
@@ -357,34 +1077,50 @@ var Vector = class extends BaseResource {
|
|
|
357
1077
|
}
|
|
358
1078
|
};
|
|
359
1079
|
|
|
360
|
-
// src/resources/workflow.ts
|
|
1080
|
+
// src/resources/legacy-workflow.ts
|
|
361
1081
|
var RECORD_SEPARATOR = "";
|
|
362
|
-
var
|
|
1082
|
+
var LegacyWorkflow = class extends BaseResource {
|
|
363
1083
|
constructor(options, workflowId) {
|
|
364
1084
|
super(options);
|
|
365
1085
|
this.workflowId = workflowId;
|
|
366
1086
|
}
|
|
367
1087
|
/**
|
|
368
|
-
* Retrieves details about the workflow
|
|
369
|
-
* @returns Promise containing workflow details including steps and graphs
|
|
1088
|
+
* Retrieves details about the legacy workflow
|
|
1089
|
+
* @returns Promise containing legacy workflow details including steps and graphs
|
|
370
1090
|
*/
|
|
371
1091
|
details() {
|
|
372
|
-
return this.request(`/api/workflows/${this.workflowId}`);
|
|
1092
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}`);
|
|
373
1093
|
}
|
|
374
1094
|
/**
|
|
375
|
-
*
|
|
376
|
-
*
|
|
377
|
-
* @
|
|
378
|
-
* @returns Promise containing the workflow execution results
|
|
1095
|
+
* Retrieves all runs for a legacy workflow
|
|
1096
|
+
* @param params - Parameters for filtering runs
|
|
1097
|
+
* @returns Promise containing legacy workflow runs array
|
|
379
1098
|
*/
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
}
|
|
1099
|
+
runs(params) {
|
|
1100
|
+
const searchParams = new URLSearchParams();
|
|
1101
|
+
if (params?.fromDate) {
|
|
1102
|
+
searchParams.set("fromDate", params.fromDate.toISOString());
|
|
1103
|
+
}
|
|
1104
|
+
if (params?.toDate) {
|
|
1105
|
+
searchParams.set("toDate", params.toDate.toISOString());
|
|
1106
|
+
}
|
|
1107
|
+
if (params?.limit) {
|
|
1108
|
+
searchParams.set("limit", String(params.limit));
|
|
1109
|
+
}
|
|
1110
|
+
if (params?.offset) {
|
|
1111
|
+
searchParams.set("offset", String(params.offset));
|
|
1112
|
+
}
|
|
1113
|
+
if (params?.resourceId) {
|
|
1114
|
+
searchParams.set("resourceId", params.resourceId);
|
|
1115
|
+
}
|
|
1116
|
+
if (searchParams.size) {
|
|
1117
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/runs?${searchParams}`);
|
|
1118
|
+
} else {
|
|
1119
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/runs`);
|
|
1120
|
+
}
|
|
385
1121
|
}
|
|
386
1122
|
/**
|
|
387
|
-
* Creates a new workflow run
|
|
1123
|
+
* Creates a new legacy workflow run
|
|
388
1124
|
* @returns Promise containing the generated run ID
|
|
389
1125
|
*/
|
|
390
1126
|
createRun(params) {
|
|
@@ -392,34 +1128,34 @@ var Workflow = class extends BaseResource {
|
|
|
392
1128
|
if (!!params?.runId) {
|
|
393
1129
|
searchParams.set("runId", params.runId);
|
|
394
1130
|
}
|
|
395
|
-
return this.request(`/api/workflows/${this.workflowId}/
|
|
1131
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/create-run?${searchParams.toString()}`, {
|
|
396
1132
|
method: "POST"
|
|
397
1133
|
});
|
|
398
1134
|
}
|
|
399
1135
|
/**
|
|
400
|
-
* Starts a workflow run synchronously without waiting for the workflow to complete
|
|
1136
|
+
* Starts a legacy workflow run synchronously without waiting for the workflow to complete
|
|
401
1137
|
* @param params - Object containing the runId and triggerData
|
|
402
1138
|
* @returns Promise containing success message
|
|
403
1139
|
*/
|
|
404
1140
|
start(params) {
|
|
405
|
-
return this.request(`/api/workflows/${this.workflowId}/start?runId=${params.runId}`, {
|
|
1141
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/start?runId=${params.runId}`, {
|
|
406
1142
|
method: "POST",
|
|
407
1143
|
body: params?.triggerData
|
|
408
1144
|
});
|
|
409
1145
|
}
|
|
410
1146
|
/**
|
|
411
|
-
* Resumes a suspended workflow step synchronously without waiting for the workflow to complete
|
|
1147
|
+
* Resumes a suspended legacy workflow step synchronously without waiting for the workflow to complete
|
|
412
1148
|
* @param stepId - ID of the step to resume
|
|
413
|
-
* @param runId - ID of the workflow run
|
|
414
|
-
* @param context - Context to resume the workflow with
|
|
415
|
-
* @returns Promise containing the workflow resume results
|
|
1149
|
+
* @param runId - ID of the legacy workflow run
|
|
1150
|
+
* @param context - Context to resume the legacy workflow with
|
|
1151
|
+
* @returns Promise containing the legacy workflow resume results
|
|
416
1152
|
*/
|
|
417
1153
|
resume({
|
|
418
1154
|
stepId,
|
|
419
1155
|
runId,
|
|
420
1156
|
context
|
|
421
1157
|
}) {
|
|
422
|
-
return this.request(`/api/workflows/${this.workflowId}/resume?runId=${runId}`, {
|
|
1158
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/resume?runId=${runId}`, {
|
|
423
1159
|
method: "POST",
|
|
424
1160
|
body: {
|
|
425
1161
|
stepId,
|
|
@@ -437,18 +1173,18 @@ var Workflow = class extends BaseResource {
|
|
|
437
1173
|
if (!!params?.runId) {
|
|
438
1174
|
searchParams.set("runId", params.runId);
|
|
439
1175
|
}
|
|
440
|
-
return this.request(`/api/workflows/${this.workflowId}/start-async?${searchParams.toString()}`, {
|
|
1176
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/start-async?${searchParams.toString()}`, {
|
|
441
1177
|
method: "POST",
|
|
442
1178
|
body: params?.triggerData
|
|
443
1179
|
});
|
|
444
1180
|
}
|
|
445
1181
|
/**
|
|
446
|
-
* Resumes a suspended workflow step asynchronously and returns a promise that resolves when the workflow is complete
|
|
1182
|
+
* Resumes a suspended legacy workflow step asynchronously and returns a promise that resolves when the workflow is complete
|
|
447
1183
|
* @param params - Object containing the runId, stepId, and context
|
|
448
1184
|
* @returns Promise containing the workflow resume results
|
|
449
1185
|
*/
|
|
450
1186
|
resumeAsync(params) {
|
|
451
|
-
return this.request(`/api/workflows/${this.workflowId}/resume-async?runId=${params.runId}`, {
|
|
1187
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/resume-async?runId=${params.runId}`, {
|
|
452
1188
|
method: "POST",
|
|
453
1189
|
body: {
|
|
454
1190
|
stepId: params.stepId,
|
|
@@ -487,7 +1223,7 @@ var Workflow = class extends BaseResource {
|
|
|
487
1223
|
}
|
|
488
1224
|
}
|
|
489
1225
|
}
|
|
490
|
-
} catch
|
|
1226
|
+
} catch {
|
|
491
1227
|
}
|
|
492
1228
|
}
|
|
493
1229
|
if (buffer) {
|
|
@@ -502,16 +1238,16 @@ var Workflow = class extends BaseResource {
|
|
|
502
1238
|
}
|
|
503
1239
|
}
|
|
504
1240
|
/**
|
|
505
|
-
* Watches workflow transitions in real-time
|
|
1241
|
+
* Watches legacy workflow transitions in real-time
|
|
506
1242
|
* @param runId - Optional run ID to filter the watch stream
|
|
507
|
-
* @returns AsyncGenerator that yields parsed records from the workflow watch stream
|
|
1243
|
+
* @returns AsyncGenerator that yields parsed records from the legacy workflow watch stream
|
|
508
1244
|
*/
|
|
509
1245
|
async watch({ runId }, onRecord) {
|
|
510
|
-
const response = await this.request(`/api/workflows/${this.workflowId}/watch?runId=${runId}`, {
|
|
1246
|
+
const response = await this.request(`/api/workflows/legacy/${this.workflowId}/watch?runId=${runId}`, {
|
|
511
1247
|
stream: true
|
|
512
1248
|
});
|
|
513
1249
|
if (!response.ok) {
|
|
514
|
-
throw new Error(`Failed to watch workflow: ${response.statusText}`);
|
|
1250
|
+
throw new Error(`Failed to watch legacy workflow: ${response.statusText}`);
|
|
515
1251
|
}
|
|
516
1252
|
if (!response.body) {
|
|
517
1253
|
throw new Error("Response body is null");
|
|
@@ -523,7 +1259,7 @@ var Workflow = class extends BaseResource {
|
|
|
523
1259
|
};
|
|
524
1260
|
|
|
525
1261
|
// src/resources/tool.ts
|
|
526
|
-
var
|
|
1262
|
+
var Tool2 = class extends BaseResource {
|
|
527
1263
|
constructor(options, toolId) {
|
|
528
1264
|
super(options);
|
|
529
1265
|
this.toolId = toolId;
|
|
@@ -541,41 +1277,684 @@ var Tool = class extends BaseResource {
|
|
|
541
1277
|
* @returns Promise containing the tool execution results
|
|
542
1278
|
*/
|
|
543
1279
|
execute(params) {
|
|
544
|
-
|
|
1280
|
+
const url = new URLSearchParams();
|
|
1281
|
+
if (params.runId) {
|
|
1282
|
+
url.set("runId", params.runId);
|
|
1283
|
+
}
|
|
1284
|
+
const body = {
|
|
1285
|
+
data: params.data,
|
|
1286
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1287
|
+
};
|
|
1288
|
+
return this.request(`/api/tools/${this.toolId}/execute?${url.toString()}`, {
|
|
545
1289
|
method: "POST",
|
|
546
|
-
body
|
|
1290
|
+
body
|
|
547
1291
|
});
|
|
548
1292
|
}
|
|
549
1293
|
};
|
|
550
1294
|
|
|
551
|
-
// src/
|
|
552
|
-
var
|
|
553
|
-
|
|
1295
|
+
// src/resources/workflow.ts
|
|
1296
|
+
var RECORD_SEPARATOR2 = "";
|
|
1297
|
+
var Workflow = class extends BaseResource {
|
|
1298
|
+
constructor(options, workflowId) {
|
|
554
1299
|
super(options);
|
|
1300
|
+
this.workflowId = workflowId;
|
|
555
1301
|
}
|
|
556
1302
|
/**
|
|
557
|
-
*
|
|
558
|
-
*
|
|
1303
|
+
* Creates an async generator that processes a readable stream and yields workflow records
|
|
1304
|
+
* separated by the Record Separator character (\x1E)
|
|
1305
|
+
*
|
|
1306
|
+
* @param stream - The readable stream to process
|
|
1307
|
+
* @returns An async generator that yields parsed records
|
|
559
1308
|
*/
|
|
560
|
-
|
|
561
|
-
|
|
1309
|
+
async *streamProcessor(stream) {
|
|
1310
|
+
const reader = stream.getReader();
|
|
1311
|
+
let doneReading = false;
|
|
1312
|
+
let buffer = "";
|
|
1313
|
+
try {
|
|
1314
|
+
while (!doneReading) {
|
|
1315
|
+
const { done, value } = await reader.read();
|
|
1316
|
+
doneReading = done;
|
|
1317
|
+
if (done && !value) continue;
|
|
1318
|
+
try {
|
|
1319
|
+
const decoded = value ? new TextDecoder().decode(value) : "";
|
|
1320
|
+
const chunks = (buffer + decoded).split(RECORD_SEPARATOR2);
|
|
1321
|
+
buffer = chunks.pop() || "";
|
|
1322
|
+
for (const chunk of chunks) {
|
|
1323
|
+
if (chunk) {
|
|
1324
|
+
if (typeof chunk === "string") {
|
|
1325
|
+
try {
|
|
1326
|
+
const parsedChunk = JSON.parse(chunk);
|
|
1327
|
+
yield parsedChunk;
|
|
1328
|
+
} catch {
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
}
|
|
1333
|
+
} catch {
|
|
1334
|
+
}
|
|
1335
|
+
}
|
|
1336
|
+
if (buffer) {
|
|
1337
|
+
try {
|
|
1338
|
+
yield JSON.parse(buffer);
|
|
1339
|
+
} catch {
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
} finally {
|
|
1343
|
+
reader.cancel().catch(() => {
|
|
1344
|
+
});
|
|
1345
|
+
}
|
|
562
1346
|
}
|
|
563
1347
|
/**
|
|
564
|
-
*
|
|
565
|
-
* @
|
|
566
|
-
* @returns Agent instance
|
|
1348
|
+
* Retrieves details about the workflow
|
|
1349
|
+
* @returns Promise containing workflow details including steps and graphs
|
|
567
1350
|
*/
|
|
568
|
-
|
|
569
|
-
return
|
|
1351
|
+
details() {
|
|
1352
|
+
return this.request(`/api/workflows/${this.workflowId}`);
|
|
570
1353
|
}
|
|
571
1354
|
/**
|
|
572
|
-
* Retrieves
|
|
573
|
-
* @param params - Parameters
|
|
574
|
-
* @returns Promise containing
|
|
1355
|
+
* Retrieves all runs for a workflow
|
|
1356
|
+
* @param params - Parameters for filtering runs
|
|
1357
|
+
* @returns Promise containing workflow runs array
|
|
575
1358
|
*/
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
1359
|
+
runs(params) {
|
|
1360
|
+
const searchParams = new URLSearchParams();
|
|
1361
|
+
if (params?.fromDate) {
|
|
1362
|
+
searchParams.set("fromDate", params.fromDate.toISOString());
|
|
1363
|
+
}
|
|
1364
|
+
if (params?.toDate) {
|
|
1365
|
+
searchParams.set("toDate", params.toDate.toISOString());
|
|
1366
|
+
}
|
|
1367
|
+
if (params?.limit !== null && params?.limit !== void 0 && !isNaN(Number(params?.limit))) {
|
|
1368
|
+
searchParams.set("limit", String(params.limit));
|
|
1369
|
+
}
|
|
1370
|
+
if (params?.offset !== null && params?.offset !== void 0 && !isNaN(Number(params?.offset))) {
|
|
1371
|
+
searchParams.set("offset", String(params.offset));
|
|
1372
|
+
}
|
|
1373
|
+
if (params?.resourceId) {
|
|
1374
|
+
searchParams.set("resourceId", params.resourceId);
|
|
1375
|
+
}
|
|
1376
|
+
if (searchParams.size) {
|
|
1377
|
+
return this.request(`/api/workflows/${this.workflowId}/runs?${searchParams}`);
|
|
1378
|
+
} else {
|
|
1379
|
+
return this.request(`/api/workflows/${this.workflowId}/runs`);
|
|
1380
|
+
}
|
|
1381
|
+
}
|
|
1382
|
+
/**
|
|
1383
|
+
* Retrieves a specific workflow run by its ID
|
|
1384
|
+
* @param runId - The ID of the workflow run to retrieve
|
|
1385
|
+
* @returns Promise containing the workflow run details
|
|
1386
|
+
*/
|
|
1387
|
+
runById(runId) {
|
|
1388
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}`);
|
|
1389
|
+
}
|
|
1390
|
+
/**
|
|
1391
|
+
* Retrieves the execution result for a specific workflow run by its ID
|
|
1392
|
+
* @param runId - The ID of the workflow run to retrieve the execution result for
|
|
1393
|
+
* @returns Promise containing the workflow run execution result
|
|
1394
|
+
*/
|
|
1395
|
+
runExecutionResult(runId) {
|
|
1396
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}/execution-result`);
|
|
1397
|
+
}
|
|
1398
|
+
/**
|
|
1399
|
+
* Cancels a specific workflow run by its ID
|
|
1400
|
+
* @param runId - The ID of the workflow run to cancel
|
|
1401
|
+
* @returns Promise containing a success message
|
|
1402
|
+
*/
|
|
1403
|
+
cancelRun(runId) {
|
|
1404
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}/cancel`, {
|
|
1405
|
+
method: "POST"
|
|
1406
|
+
});
|
|
1407
|
+
}
|
|
1408
|
+
/**
|
|
1409
|
+
* Sends an event to a specific workflow run by its ID
|
|
1410
|
+
* @param params - Object containing the runId, event and data
|
|
1411
|
+
* @returns Promise containing a success message
|
|
1412
|
+
*/
|
|
1413
|
+
sendRunEvent(params) {
|
|
1414
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${params.runId}/send-event`, {
|
|
1415
|
+
method: "POST",
|
|
1416
|
+
body: { event: params.event, data: params.data }
|
|
1417
|
+
});
|
|
1418
|
+
}
|
|
1419
|
+
/**
|
|
1420
|
+
* Creates a new workflow run
|
|
1421
|
+
* @param params - Optional object containing the optional runId
|
|
1422
|
+
* @returns Promise containing the runId of the created run
|
|
1423
|
+
*/
|
|
1424
|
+
createRun(params) {
|
|
1425
|
+
const searchParams = new URLSearchParams();
|
|
1426
|
+
if (!!params?.runId) {
|
|
1427
|
+
searchParams.set("runId", params.runId);
|
|
1428
|
+
}
|
|
1429
|
+
return this.request(`/api/workflows/${this.workflowId}/create-run?${searchParams.toString()}`, {
|
|
1430
|
+
method: "POST"
|
|
1431
|
+
});
|
|
1432
|
+
}
|
|
1433
|
+
/**
|
|
1434
|
+
* Creates a new workflow run (alias for createRun)
|
|
1435
|
+
* @param params - Optional object containing the optional runId
|
|
1436
|
+
* @returns Promise containing the runId of the created run
|
|
1437
|
+
*/
|
|
1438
|
+
createRunAsync(params) {
|
|
1439
|
+
return this.createRun(params);
|
|
1440
|
+
}
|
|
1441
|
+
/**
|
|
1442
|
+
* Starts a workflow run synchronously without waiting for the workflow to complete
|
|
1443
|
+
* @param params - Object containing the runId, inputData and runtimeContext
|
|
1444
|
+
* @returns Promise containing success message
|
|
1445
|
+
*/
|
|
1446
|
+
start(params) {
|
|
1447
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1448
|
+
return this.request(`/api/workflows/${this.workflowId}/start?runId=${params.runId}`, {
|
|
1449
|
+
method: "POST",
|
|
1450
|
+
body: { inputData: params?.inputData, runtimeContext }
|
|
1451
|
+
});
|
|
1452
|
+
}
|
|
1453
|
+
/**
|
|
1454
|
+
* Resumes a suspended workflow step synchronously without waiting for the workflow to complete
|
|
1455
|
+
* @param params - Object containing the runId, step, resumeData and runtimeContext
|
|
1456
|
+
* @returns Promise containing success message
|
|
1457
|
+
*/
|
|
1458
|
+
resume({
|
|
1459
|
+
step,
|
|
1460
|
+
runId,
|
|
1461
|
+
resumeData,
|
|
1462
|
+
...rest
|
|
1463
|
+
}) {
|
|
1464
|
+
const runtimeContext = parseClientRuntimeContext(rest.runtimeContext);
|
|
1465
|
+
return this.request(`/api/workflows/${this.workflowId}/resume?runId=${runId}`, {
|
|
1466
|
+
method: "POST",
|
|
1467
|
+
stream: true,
|
|
1468
|
+
body: {
|
|
1469
|
+
step,
|
|
1470
|
+
resumeData,
|
|
1471
|
+
runtimeContext
|
|
1472
|
+
}
|
|
1473
|
+
});
|
|
1474
|
+
}
|
|
1475
|
+
/**
|
|
1476
|
+
* Starts a workflow run asynchronously and returns a promise that resolves when the workflow is complete
|
|
1477
|
+
* @param params - Object containing the optional runId, inputData and runtimeContext
|
|
1478
|
+
* @returns Promise containing the workflow execution results
|
|
1479
|
+
*/
|
|
1480
|
+
startAsync(params) {
|
|
1481
|
+
const searchParams = new URLSearchParams();
|
|
1482
|
+
if (!!params?.runId) {
|
|
1483
|
+
searchParams.set("runId", params.runId);
|
|
1484
|
+
}
|
|
1485
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1486
|
+
return this.request(`/api/workflows/${this.workflowId}/start-async?${searchParams.toString()}`, {
|
|
1487
|
+
method: "POST",
|
|
1488
|
+
body: { inputData: params.inputData, runtimeContext }
|
|
1489
|
+
});
|
|
1490
|
+
}
|
|
1491
|
+
/**
|
|
1492
|
+
* Starts a workflow run and returns a stream
|
|
1493
|
+
* @param params - Object containing the optional runId, inputData and runtimeContext
|
|
1494
|
+
* @returns Promise containing the workflow execution results
|
|
1495
|
+
*/
|
|
1496
|
+
async stream(params) {
|
|
1497
|
+
const searchParams = new URLSearchParams();
|
|
1498
|
+
if (!!params?.runId) {
|
|
1499
|
+
searchParams.set("runId", params.runId);
|
|
1500
|
+
}
|
|
1501
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1502
|
+
const response = await this.request(
|
|
1503
|
+
`/api/workflows/${this.workflowId}/stream?${searchParams.toString()}`,
|
|
1504
|
+
{
|
|
1505
|
+
method: "POST",
|
|
1506
|
+
body: { inputData: params.inputData, runtimeContext },
|
|
1507
|
+
stream: true
|
|
1508
|
+
}
|
|
1509
|
+
);
|
|
1510
|
+
if (!response.ok) {
|
|
1511
|
+
throw new Error(`Failed to stream vNext workflow: ${response.statusText}`);
|
|
1512
|
+
}
|
|
1513
|
+
if (!response.body) {
|
|
1514
|
+
throw new Error("Response body is null");
|
|
1515
|
+
}
|
|
1516
|
+
let failedChunk = void 0;
|
|
1517
|
+
const transformStream = new TransformStream({
|
|
1518
|
+
start() {
|
|
1519
|
+
},
|
|
1520
|
+
async transform(chunk, controller) {
|
|
1521
|
+
try {
|
|
1522
|
+
const decoded = new TextDecoder().decode(chunk);
|
|
1523
|
+
const chunks = decoded.split(RECORD_SEPARATOR2);
|
|
1524
|
+
for (const chunk2 of chunks) {
|
|
1525
|
+
if (chunk2) {
|
|
1526
|
+
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
1527
|
+
try {
|
|
1528
|
+
const parsedChunk = JSON.parse(newChunk);
|
|
1529
|
+
controller.enqueue(parsedChunk);
|
|
1530
|
+
failedChunk = void 0;
|
|
1531
|
+
} catch (error) {
|
|
1532
|
+
failedChunk = newChunk;
|
|
1533
|
+
}
|
|
1534
|
+
}
|
|
1535
|
+
}
|
|
1536
|
+
} catch {
|
|
1537
|
+
}
|
|
1538
|
+
}
|
|
1539
|
+
});
|
|
1540
|
+
return response.body.pipeThrough(transformStream);
|
|
1541
|
+
}
|
|
1542
|
+
/**
|
|
1543
|
+
* Resumes a suspended workflow step asynchronously and returns a promise that resolves when the workflow is complete
|
|
1544
|
+
* @param params - Object containing the runId, step, resumeData and runtimeContext
|
|
1545
|
+
* @returns Promise containing the workflow resume results
|
|
1546
|
+
*/
|
|
1547
|
+
resumeAsync(params) {
|
|
1548
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1549
|
+
return this.request(`/api/workflows/${this.workflowId}/resume-async?runId=${params.runId}`, {
|
|
1550
|
+
method: "POST",
|
|
1551
|
+
body: {
|
|
1552
|
+
step: params.step,
|
|
1553
|
+
resumeData: params.resumeData,
|
|
1554
|
+
runtimeContext
|
|
1555
|
+
}
|
|
1556
|
+
});
|
|
1557
|
+
}
|
|
1558
|
+
/**
|
|
1559
|
+
* Watches workflow transitions in real-time
|
|
1560
|
+
* @param runId - Optional run ID to filter the watch stream
|
|
1561
|
+
* @returns AsyncGenerator that yields parsed records from the workflow watch stream
|
|
1562
|
+
*/
|
|
1563
|
+
async watch({ runId }, onRecord) {
|
|
1564
|
+
const response = await this.request(`/api/workflows/${this.workflowId}/watch?runId=${runId}`, {
|
|
1565
|
+
stream: true
|
|
1566
|
+
});
|
|
1567
|
+
if (!response.ok) {
|
|
1568
|
+
throw new Error(`Failed to watch workflow: ${response.statusText}`);
|
|
1569
|
+
}
|
|
1570
|
+
if (!response.body) {
|
|
1571
|
+
throw new Error("Response body is null");
|
|
1572
|
+
}
|
|
1573
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
1574
|
+
if (typeof record === "string") {
|
|
1575
|
+
onRecord(JSON.parse(record));
|
|
1576
|
+
} else {
|
|
1577
|
+
onRecord(record);
|
|
1578
|
+
}
|
|
1579
|
+
}
|
|
1580
|
+
}
|
|
1581
|
+
/**
|
|
1582
|
+
* Creates a new ReadableStream from an iterable or async iterable of objects,
|
|
1583
|
+
* serializing each as JSON and separating them with the record separator (\x1E).
|
|
1584
|
+
*
|
|
1585
|
+
* @param records - An iterable or async iterable of objects to stream
|
|
1586
|
+
* @returns A ReadableStream emitting the records as JSON strings separated by the record separator
|
|
1587
|
+
*/
|
|
1588
|
+
static createRecordStream(records) {
|
|
1589
|
+
const encoder = new TextEncoder();
|
|
1590
|
+
return new ReadableStream({
|
|
1591
|
+
async start(controller) {
|
|
1592
|
+
try {
|
|
1593
|
+
for await (const record of records) {
|
|
1594
|
+
const json = JSON.stringify(record) + RECORD_SEPARATOR2;
|
|
1595
|
+
controller.enqueue(encoder.encode(json));
|
|
1596
|
+
}
|
|
1597
|
+
controller.close();
|
|
1598
|
+
} catch (err) {
|
|
1599
|
+
controller.error(err);
|
|
1600
|
+
}
|
|
1601
|
+
}
|
|
1602
|
+
});
|
|
1603
|
+
}
|
|
1604
|
+
};
|
|
1605
|
+
|
|
1606
|
+
// src/resources/a2a.ts
|
|
1607
|
+
var A2A = class extends BaseResource {
|
|
1608
|
+
constructor(options, agentId) {
|
|
1609
|
+
super(options);
|
|
1610
|
+
this.agentId = agentId;
|
|
1611
|
+
}
|
|
1612
|
+
/**
|
|
1613
|
+
* Get the agent card with metadata about the agent
|
|
1614
|
+
* @returns Promise containing the agent card information
|
|
1615
|
+
*/
|
|
1616
|
+
async getCard() {
|
|
1617
|
+
return this.request(`/.well-known/${this.agentId}/agent.json`);
|
|
1618
|
+
}
|
|
1619
|
+
/**
|
|
1620
|
+
* Send a message to the agent and get a response
|
|
1621
|
+
* @param params - Parameters for the task
|
|
1622
|
+
* @returns Promise containing the task response
|
|
1623
|
+
*/
|
|
1624
|
+
async sendMessage(params) {
|
|
1625
|
+
const response = await this.request(`/a2a/${this.agentId}`, {
|
|
1626
|
+
method: "POST",
|
|
1627
|
+
body: {
|
|
1628
|
+
method: "tasks/send",
|
|
1629
|
+
params
|
|
1630
|
+
}
|
|
1631
|
+
});
|
|
1632
|
+
return { task: response.result };
|
|
1633
|
+
}
|
|
1634
|
+
/**
|
|
1635
|
+
* Get the status and result of a task
|
|
1636
|
+
* @param params - Parameters for querying the task
|
|
1637
|
+
* @returns Promise containing the task response
|
|
1638
|
+
*/
|
|
1639
|
+
async getTask(params) {
|
|
1640
|
+
const response = await this.request(`/a2a/${this.agentId}`, {
|
|
1641
|
+
method: "POST",
|
|
1642
|
+
body: {
|
|
1643
|
+
method: "tasks/get",
|
|
1644
|
+
params
|
|
1645
|
+
}
|
|
1646
|
+
});
|
|
1647
|
+
return response.result;
|
|
1648
|
+
}
|
|
1649
|
+
/**
|
|
1650
|
+
* Cancel a running task
|
|
1651
|
+
* @param params - Parameters identifying the task to cancel
|
|
1652
|
+
* @returns Promise containing the task response
|
|
1653
|
+
*/
|
|
1654
|
+
async cancelTask(params) {
|
|
1655
|
+
return this.request(`/a2a/${this.agentId}`, {
|
|
1656
|
+
method: "POST",
|
|
1657
|
+
body: {
|
|
1658
|
+
method: "tasks/cancel",
|
|
1659
|
+
params
|
|
1660
|
+
}
|
|
1661
|
+
});
|
|
1662
|
+
}
|
|
1663
|
+
/**
|
|
1664
|
+
* Send a message and subscribe to streaming updates (not fully implemented)
|
|
1665
|
+
* @param params - Parameters for the task
|
|
1666
|
+
* @returns Promise containing the task response
|
|
1667
|
+
*/
|
|
1668
|
+
async sendAndSubscribe(params) {
|
|
1669
|
+
return this.request(`/a2a/${this.agentId}`, {
|
|
1670
|
+
method: "POST",
|
|
1671
|
+
body: {
|
|
1672
|
+
method: "tasks/sendSubscribe",
|
|
1673
|
+
params
|
|
1674
|
+
},
|
|
1675
|
+
stream: true
|
|
1676
|
+
});
|
|
1677
|
+
}
|
|
1678
|
+
};
|
|
1679
|
+
|
|
1680
|
+
// src/resources/mcp-tool.ts
|
|
1681
|
+
var MCPTool = class extends BaseResource {
|
|
1682
|
+
serverId;
|
|
1683
|
+
toolId;
|
|
1684
|
+
constructor(options, serverId, toolId) {
|
|
1685
|
+
super(options);
|
|
1686
|
+
this.serverId = serverId;
|
|
1687
|
+
this.toolId = toolId;
|
|
1688
|
+
}
|
|
1689
|
+
/**
|
|
1690
|
+
* Retrieves details about this specific tool from the MCP server.
|
|
1691
|
+
* @returns Promise containing the tool's information (name, description, schema).
|
|
1692
|
+
*/
|
|
1693
|
+
details() {
|
|
1694
|
+
return this.request(`/api/mcp/${this.serverId}/tools/${this.toolId}`);
|
|
1695
|
+
}
|
|
1696
|
+
/**
|
|
1697
|
+
* Executes this specific tool on the MCP server.
|
|
1698
|
+
* @param params - Parameters for tool execution, including data/args and optional runtimeContext.
|
|
1699
|
+
* @returns Promise containing the result of the tool execution.
|
|
1700
|
+
*/
|
|
1701
|
+
execute(params) {
|
|
1702
|
+
const body = {};
|
|
1703
|
+
if (params.data !== void 0) body.data = params.data;
|
|
1704
|
+
if (params.runtimeContext !== void 0) {
|
|
1705
|
+
body.runtimeContext = params.runtimeContext;
|
|
1706
|
+
}
|
|
1707
|
+
return this.request(`/api/mcp/${this.serverId}/tools/${this.toolId}/execute`, {
|
|
1708
|
+
method: "POST",
|
|
1709
|
+
body: Object.keys(body).length > 0 ? body : void 0
|
|
1710
|
+
});
|
|
1711
|
+
}
|
|
1712
|
+
};
|
|
1713
|
+
|
|
1714
|
+
// src/resources/network-memory-thread.ts
|
|
1715
|
+
var NetworkMemoryThread = class extends BaseResource {
|
|
1716
|
+
constructor(options, threadId, networkId) {
|
|
1717
|
+
super(options);
|
|
1718
|
+
this.threadId = threadId;
|
|
1719
|
+
this.networkId = networkId;
|
|
1720
|
+
}
|
|
1721
|
+
/**
|
|
1722
|
+
* Retrieves the memory thread details
|
|
1723
|
+
* @returns Promise containing thread details including title and metadata
|
|
1724
|
+
*/
|
|
1725
|
+
get() {
|
|
1726
|
+
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`);
|
|
1727
|
+
}
|
|
1728
|
+
/**
|
|
1729
|
+
* Updates the memory thread properties
|
|
1730
|
+
* @param params - Update parameters including title and metadata
|
|
1731
|
+
* @returns Promise containing updated thread details
|
|
1732
|
+
*/
|
|
1733
|
+
update(params) {
|
|
1734
|
+
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
|
|
1735
|
+
method: "PATCH",
|
|
1736
|
+
body: params
|
|
1737
|
+
});
|
|
1738
|
+
}
|
|
1739
|
+
/**
|
|
1740
|
+
* Deletes the memory thread
|
|
1741
|
+
* @returns Promise containing deletion result
|
|
1742
|
+
*/
|
|
1743
|
+
delete() {
|
|
1744
|
+
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
|
|
1745
|
+
method: "DELETE"
|
|
1746
|
+
});
|
|
1747
|
+
}
|
|
1748
|
+
/**
|
|
1749
|
+
* Retrieves messages associated with the thread
|
|
1750
|
+
* @param params - Optional parameters including limit for number of messages to retrieve
|
|
1751
|
+
* @returns Promise containing thread messages and UI messages
|
|
1752
|
+
*/
|
|
1753
|
+
getMessages(params) {
|
|
1754
|
+
const query = new URLSearchParams({
|
|
1755
|
+
networkId: this.networkId,
|
|
1756
|
+
...params?.limit ? { limit: params.limit.toString() } : {}
|
|
1757
|
+
});
|
|
1758
|
+
return this.request(`/api/memory/network/threads/${this.threadId}/messages?${query.toString()}`);
|
|
1759
|
+
}
|
|
1760
|
+
/**
|
|
1761
|
+
* Deletes one or more messages from the thread
|
|
1762
|
+
* @param messageIds - Can be a single message ID (string), array of message IDs,
|
|
1763
|
+
* message object with id property, or array of message objects
|
|
1764
|
+
* @returns Promise containing deletion result
|
|
1765
|
+
*/
|
|
1766
|
+
deleteMessages(messageIds) {
|
|
1767
|
+
const query = new URLSearchParams({
|
|
1768
|
+
networkId: this.networkId
|
|
1769
|
+
});
|
|
1770
|
+
return this.request(`/api/memory/network/messages/delete?${query.toString()}`, {
|
|
1771
|
+
method: "POST",
|
|
1772
|
+
body: { messageIds }
|
|
1773
|
+
});
|
|
1774
|
+
}
|
|
1775
|
+
};
|
|
1776
|
+
|
|
1777
|
+
// src/resources/vNextNetwork.ts
|
|
1778
|
+
var RECORD_SEPARATOR3 = "";
|
|
1779
|
+
var VNextNetwork = class extends BaseResource {
|
|
1780
|
+
constructor(options, networkId) {
|
|
1781
|
+
super(options);
|
|
1782
|
+
this.networkId = networkId;
|
|
1783
|
+
}
|
|
1784
|
+
/**
|
|
1785
|
+
* Retrieves details about the network
|
|
1786
|
+
* @returns Promise containing vNext network details
|
|
1787
|
+
*/
|
|
1788
|
+
details() {
|
|
1789
|
+
return this.request(`/api/networks/v-next/${this.networkId}`);
|
|
1790
|
+
}
|
|
1791
|
+
/**
|
|
1792
|
+
* Generates a response from the v-next network
|
|
1793
|
+
* @param params - Generation parameters including message
|
|
1794
|
+
* @returns Promise containing the generated response
|
|
1795
|
+
*/
|
|
1796
|
+
generate(params) {
|
|
1797
|
+
return this.request(`/api/networks/v-next/${this.networkId}/generate`, {
|
|
1798
|
+
method: "POST",
|
|
1799
|
+
body: {
|
|
1800
|
+
...params,
|
|
1801
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1802
|
+
}
|
|
1803
|
+
});
|
|
1804
|
+
}
|
|
1805
|
+
/**
|
|
1806
|
+
* Generates a response from the v-next network using multiple primitives
|
|
1807
|
+
* @param params - Generation parameters including message
|
|
1808
|
+
* @returns Promise containing the generated response
|
|
1809
|
+
*/
|
|
1810
|
+
loop(params) {
|
|
1811
|
+
return this.request(`/api/networks/v-next/${this.networkId}/loop`, {
|
|
1812
|
+
method: "POST",
|
|
1813
|
+
body: {
|
|
1814
|
+
...params,
|
|
1815
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1816
|
+
}
|
|
1817
|
+
});
|
|
1818
|
+
}
|
|
1819
|
+
async *streamProcessor(stream) {
|
|
1820
|
+
const reader = stream.getReader();
|
|
1821
|
+
let doneReading = false;
|
|
1822
|
+
let buffer = "";
|
|
1823
|
+
try {
|
|
1824
|
+
while (!doneReading) {
|
|
1825
|
+
const { done, value } = await reader.read();
|
|
1826
|
+
doneReading = done;
|
|
1827
|
+
if (done && !value) continue;
|
|
1828
|
+
try {
|
|
1829
|
+
const decoded = value ? new TextDecoder().decode(value) : "";
|
|
1830
|
+
const chunks = (buffer + decoded).split(RECORD_SEPARATOR3);
|
|
1831
|
+
buffer = chunks.pop() || "";
|
|
1832
|
+
for (const chunk of chunks) {
|
|
1833
|
+
if (chunk) {
|
|
1834
|
+
if (typeof chunk === "string") {
|
|
1835
|
+
try {
|
|
1836
|
+
const parsedChunk = JSON.parse(chunk);
|
|
1837
|
+
yield parsedChunk;
|
|
1838
|
+
} catch {
|
|
1839
|
+
}
|
|
1840
|
+
}
|
|
1841
|
+
}
|
|
1842
|
+
}
|
|
1843
|
+
} catch {
|
|
1844
|
+
}
|
|
1845
|
+
}
|
|
1846
|
+
if (buffer) {
|
|
1847
|
+
try {
|
|
1848
|
+
yield JSON.parse(buffer);
|
|
1849
|
+
} catch {
|
|
1850
|
+
}
|
|
1851
|
+
}
|
|
1852
|
+
} finally {
|
|
1853
|
+
reader.cancel().catch(() => {
|
|
1854
|
+
});
|
|
1855
|
+
}
|
|
1856
|
+
}
|
|
1857
|
+
/**
|
|
1858
|
+
* Streams a response from the v-next network
|
|
1859
|
+
* @param params - Stream parameters including message
|
|
1860
|
+
* @returns Promise containing the results
|
|
1861
|
+
*/
|
|
1862
|
+
async stream(params, onRecord) {
|
|
1863
|
+
const response = await this.request(`/api/networks/v-next/${this.networkId}/stream`, {
|
|
1864
|
+
method: "POST",
|
|
1865
|
+
body: {
|
|
1866
|
+
...params,
|
|
1867
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1868
|
+
},
|
|
1869
|
+
stream: true
|
|
1870
|
+
});
|
|
1871
|
+
if (!response.ok) {
|
|
1872
|
+
throw new Error(`Failed to stream vNext network: ${response.statusText}`);
|
|
1873
|
+
}
|
|
1874
|
+
if (!response.body) {
|
|
1875
|
+
throw new Error("Response body is null");
|
|
1876
|
+
}
|
|
1877
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
1878
|
+
if (typeof record === "string") {
|
|
1879
|
+
onRecord(JSON.parse(record));
|
|
1880
|
+
} else {
|
|
1881
|
+
onRecord(record);
|
|
1882
|
+
}
|
|
1883
|
+
}
|
|
1884
|
+
}
|
|
1885
|
+
/**
|
|
1886
|
+
* Streams a response from the v-next network loop
|
|
1887
|
+
* @param params - Stream parameters including message
|
|
1888
|
+
* @returns Promise containing the results
|
|
1889
|
+
*/
|
|
1890
|
+
async loopStream(params, onRecord) {
|
|
1891
|
+
const response = await this.request(`/api/networks/v-next/${this.networkId}/loop-stream`, {
|
|
1892
|
+
method: "POST",
|
|
1893
|
+
body: {
|
|
1894
|
+
...params,
|
|
1895
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1896
|
+
},
|
|
1897
|
+
stream: true
|
|
1898
|
+
});
|
|
1899
|
+
if (!response.ok) {
|
|
1900
|
+
throw new Error(`Failed to stream vNext network loop: ${response.statusText}`);
|
|
1901
|
+
}
|
|
1902
|
+
if (!response.body) {
|
|
1903
|
+
throw new Error("Response body is null");
|
|
1904
|
+
}
|
|
1905
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
1906
|
+
if (typeof record === "string") {
|
|
1907
|
+
onRecord(JSON.parse(record));
|
|
1908
|
+
} else {
|
|
1909
|
+
onRecord(record);
|
|
1910
|
+
}
|
|
1911
|
+
}
|
|
1912
|
+
}
|
|
1913
|
+
};
|
|
1914
|
+
|
|
1915
|
+
// src/client.ts
|
|
1916
|
+
var MastraClient = class extends BaseResource {
|
|
1917
|
+
constructor(options) {
|
|
1918
|
+
super(options);
|
|
1919
|
+
}
|
|
1920
|
+
/**
|
|
1921
|
+
* Retrieves all available agents
|
|
1922
|
+
* @returns Promise containing map of agent IDs to agent details
|
|
1923
|
+
*/
|
|
1924
|
+
getAgents() {
|
|
1925
|
+
return this.request("/api/agents");
|
|
1926
|
+
}
|
|
1927
|
+
async getAGUI({ resourceId }) {
|
|
1928
|
+
const agents = await this.getAgents();
|
|
1929
|
+
return Object.entries(agents).reduce(
|
|
1930
|
+
(acc, [agentId]) => {
|
|
1931
|
+
const agent = this.getAgent(agentId);
|
|
1932
|
+
acc[agentId] = new AGUIAdapter({
|
|
1933
|
+
agentId,
|
|
1934
|
+
agent,
|
|
1935
|
+
resourceId
|
|
1936
|
+
});
|
|
1937
|
+
return acc;
|
|
1938
|
+
},
|
|
1939
|
+
{}
|
|
1940
|
+
);
|
|
1941
|
+
}
|
|
1942
|
+
/**
|
|
1943
|
+
* Gets an agent instance by ID
|
|
1944
|
+
* @param agentId - ID of the agent to retrieve
|
|
1945
|
+
* @returns Agent instance
|
|
1946
|
+
*/
|
|
1947
|
+
getAgent(agentId) {
|
|
1948
|
+
return new Agent(this.options, agentId);
|
|
1949
|
+
}
|
|
1950
|
+
/**
|
|
1951
|
+
* Retrieves memory threads for a resource
|
|
1952
|
+
* @param params - Parameters containing the resource ID
|
|
1953
|
+
* @returns Promise containing array of memory threads
|
|
1954
|
+
*/
|
|
1955
|
+
getMemoryThreads(params) {
|
|
1956
|
+
return this.request(`/api/memory/threads?resourceid=${params.resourceId}&agentId=${params.agentId}`);
|
|
1957
|
+
}
|
|
579
1958
|
/**
|
|
580
1959
|
* Creates a new memory thread
|
|
581
1960
|
* @param params - Parameters for creating the memory thread
|
|
@@ -610,6 +1989,48 @@ var MastraClient = class extends BaseResource {
|
|
|
610
1989
|
getMemoryStatus(agentId) {
|
|
611
1990
|
return this.request(`/api/memory/status?agentId=${agentId}`);
|
|
612
1991
|
}
|
|
1992
|
+
/**
|
|
1993
|
+
* Retrieves memory threads for a resource
|
|
1994
|
+
* @param params - Parameters containing the resource ID
|
|
1995
|
+
* @returns Promise containing array of memory threads
|
|
1996
|
+
*/
|
|
1997
|
+
getNetworkMemoryThreads(params) {
|
|
1998
|
+
return this.request(`/api/memory/network/threads?resourceid=${params.resourceId}&networkId=${params.networkId}`);
|
|
1999
|
+
}
|
|
2000
|
+
/**
|
|
2001
|
+
* Creates a new memory thread
|
|
2002
|
+
* @param params - Parameters for creating the memory thread
|
|
2003
|
+
* @returns Promise containing the created memory thread
|
|
2004
|
+
*/
|
|
2005
|
+
createNetworkMemoryThread(params) {
|
|
2006
|
+
return this.request(`/api/memory/network/threads?networkId=${params.networkId}`, { method: "POST", body: params });
|
|
2007
|
+
}
|
|
2008
|
+
/**
|
|
2009
|
+
* Gets a memory thread instance by ID
|
|
2010
|
+
* @param threadId - ID of the memory thread to retrieve
|
|
2011
|
+
* @returns MemoryThread instance
|
|
2012
|
+
*/
|
|
2013
|
+
getNetworkMemoryThread(threadId, networkId) {
|
|
2014
|
+
return new NetworkMemoryThread(this.options, threadId, networkId);
|
|
2015
|
+
}
|
|
2016
|
+
/**
|
|
2017
|
+
* Saves messages to memory
|
|
2018
|
+
* @param params - Parameters containing messages to save
|
|
2019
|
+
* @returns Promise containing the saved messages
|
|
2020
|
+
*/
|
|
2021
|
+
saveNetworkMessageToMemory(params) {
|
|
2022
|
+
return this.request(`/api/memory/network/save-messages?networkId=${params.networkId}`, {
|
|
2023
|
+
method: "POST",
|
|
2024
|
+
body: params
|
|
2025
|
+
});
|
|
2026
|
+
}
|
|
2027
|
+
/**
|
|
2028
|
+
* Gets the status of the memory system
|
|
2029
|
+
* @returns Promise containing memory system status
|
|
2030
|
+
*/
|
|
2031
|
+
getNetworkMemoryStatus(networkId) {
|
|
2032
|
+
return this.request(`/api/memory/network/status?networkId=${networkId}`);
|
|
2033
|
+
}
|
|
613
2034
|
/**
|
|
614
2035
|
* Retrieves all available tools
|
|
615
2036
|
* @returns Promise containing map of tool IDs to tool details
|
|
@@ -623,7 +2044,22 @@ var MastraClient = class extends BaseResource {
|
|
|
623
2044
|
* @returns Tool instance
|
|
624
2045
|
*/
|
|
625
2046
|
getTool(toolId) {
|
|
626
|
-
return new
|
|
2047
|
+
return new Tool2(this.options, toolId);
|
|
2048
|
+
}
|
|
2049
|
+
/**
|
|
2050
|
+
* Retrieves all available legacy workflows
|
|
2051
|
+
* @returns Promise containing map of legacy workflow IDs to legacy workflow details
|
|
2052
|
+
*/
|
|
2053
|
+
getLegacyWorkflows() {
|
|
2054
|
+
return this.request("/api/workflows/legacy");
|
|
2055
|
+
}
|
|
2056
|
+
/**
|
|
2057
|
+
* Gets a legacy workflow instance by ID
|
|
2058
|
+
* @param workflowId - ID of the legacy workflow to retrieve
|
|
2059
|
+
* @returns Legacy Workflow instance
|
|
2060
|
+
*/
|
|
2061
|
+
getLegacyWorkflow(workflowId) {
|
|
2062
|
+
return new LegacyWorkflow(this.options, workflowId);
|
|
627
2063
|
}
|
|
628
2064
|
/**
|
|
629
2065
|
* Retrieves all available workflows
|
|
@@ -654,7 +2090,41 @@ var MastraClient = class extends BaseResource {
|
|
|
654
2090
|
* @returns Promise containing array of log messages
|
|
655
2091
|
*/
|
|
656
2092
|
getLogs(params) {
|
|
657
|
-
|
|
2093
|
+
const { transportId, fromDate, toDate, logLevel, filters, page, perPage } = params;
|
|
2094
|
+
const _filters = filters ? Object.entries(filters).map(([key, value]) => `${key}:${value}`) : [];
|
|
2095
|
+
const searchParams = new URLSearchParams();
|
|
2096
|
+
if (transportId) {
|
|
2097
|
+
searchParams.set("transportId", transportId);
|
|
2098
|
+
}
|
|
2099
|
+
if (fromDate) {
|
|
2100
|
+
searchParams.set("fromDate", fromDate.toISOString());
|
|
2101
|
+
}
|
|
2102
|
+
if (toDate) {
|
|
2103
|
+
searchParams.set("toDate", toDate.toISOString());
|
|
2104
|
+
}
|
|
2105
|
+
if (logLevel) {
|
|
2106
|
+
searchParams.set("logLevel", logLevel);
|
|
2107
|
+
}
|
|
2108
|
+
if (page) {
|
|
2109
|
+
searchParams.set("page", String(page));
|
|
2110
|
+
}
|
|
2111
|
+
if (perPage) {
|
|
2112
|
+
searchParams.set("perPage", String(perPage));
|
|
2113
|
+
}
|
|
2114
|
+
if (_filters) {
|
|
2115
|
+
if (Array.isArray(_filters)) {
|
|
2116
|
+
for (const filter of _filters) {
|
|
2117
|
+
searchParams.append("filters", filter);
|
|
2118
|
+
}
|
|
2119
|
+
} else {
|
|
2120
|
+
searchParams.set("filters", _filters);
|
|
2121
|
+
}
|
|
2122
|
+
}
|
|
2123
|
+
if (searchParams.size) {
|
|
2124
|
+
return this.request(`/api/logs?${searchParams}`);
|
|
2125
|
+
} else {
|
|
2126
|
+
return this.request(`/api/logs`);
|
|
2127
|
+
}
|
|
658
2128
|
}
|
|
659
2129
|
/**
|
|
660
2130
|
* Gets logs for a specific run
|
|
@@ -662,7 +2132,44 @@ var MastraClient = class extends BaseResource {
|
|
|
662
2132
|
* @returns Promise containing array of log messages
|
|
663
2133
|
*/
|
|
664
2134
|
getLogForRun(params) {
|
|
665
|
-
|
|
2135
|
+
const { runId, transportId, fromDate, toDate, logLevel, filters, page, perPage } = params;
|
|
2136
|
+
const _filters = filters ? Object.entries(filters).map(([key, value]) => `${key}:${value}`) : [];
|
|
2137
|
+
const searchParams = new URLSearchParams();
|
|
2138
|
+
if (runId) {
|
|
2139
|
+
searchParams.set("runId", runId);
|
|
2140
|
+
}
|
|
2141
|
+
if (transportId) {
|
|
2142
|
+
searchParams.set("transportId", transportId);
|
|
2143
|
+
}
|
|
2144
|
+
if (fromDate) {
|
|
2145
|
+
searchParams.set("fromDate", fromDate.toISOString());
|
|
2146
|
+
}
|
|
2147
|
+
if (toDate) {
|
|
2148
|
+
searchParams.set("toDate", toDate.toISOString());
|
|
2149
|
+
}
|
|
2150
|
+
if (logLevel) {
|
|
2151
|
+
searchParams.set("logLevel", logLevel);
|
|
2152
|
+
}
|
|
2153
|
+
if (page) {
|
|
2154
|
+
searchParams.set("page", String(page));
|
|
2155
|
+
}
|
|
2156
|
+
if (perPage) {
|
|
2157
|
+
searchParams.set("perPage", String(perPage));
|
|
2158
|
+
}
|
|
2159
|
+
if (_filters) {
|
|
2160
|
+
if (Array.isArray(_filters)) {
|
|
2161
|
+
for (const filter of _filters) {
|
|
2162
|
+
searchParams.append("filters", filter);
|
|
2163
|
+
}
|
|
2164
|
+
} else {
|
|
2165
|
+
searchParams.set("filters", _filters);
|
|
2166
|
+
}
|
|
2167
|
+
}
|
|
2168
|
+
if (searchParams.size) {
|
|
2169
|
+
return this.request(`/api/logs/${runId}?${searchParams}`);
|
|
2170
|
+
} else {
|
|
2171
|
+
return this.request(`/api/logs/${runId}`);
|
|
2172
|
+
}
|
|
666
2173
|
}
|
|
667
2174
|
/**
|
|
668
2175
|
* List of all log transports
|
|
@@ -677,7 +2184,7 @@ var MastraClient = class extends BaseResource {
|
|
|
677
2184
|
* @returns Promise containing telemetry data
|
|
678
2185
|
*/
|
|
679
2186
|
getTelemetry(params) {
|
|
680
|
-
const { name, scope, page, perPage, attribute } = params || {};
|
|
2187
|
+
const { name, scope, page, perPage, attribute, fromDate, toDate } = params || {};
|
|
681
2188
|
const _attribute = attribute ? Object.entries(attribute).map(([key, value]) => `${key}:${value}`) : [];
|
|
682
2189
|
const searchParams = new URLSearchParams();
|
|
683
2190
|
if (name) {
|
|
@@ -701,6 +2208,12 @@ var MastraClient = class extends BaseResource {
|
|
|
701
2208
|
searchParams.set("attribute", _attribute);
|
|
702
2209
|
}
|
|
703
2210
|
}
|
|
2211
|
+
if (fromDate) {
|
|
2212
|
+
searchParams.set("fromDate", fromDate.toISOString());
|
|
2213
|
+
}
|
|
2214
|
+
if (toDate) {
|
|
2215
|
+
searchParams.set("toDate", toDate.toISOString());
|
|
2216
|
+
}
|
|
704
2217
|
if (searchParams.size) {
|
|
705
2218
|
return this.request(`/api/telemetry?${searchParams}`);
|
|
706
2219
|
} else {
|
|
@@ -714,6 +2227,13 @@ var MastraClient = class extends BaseResource {
|
|
|
714
2227
|
getNetworks() {
|
|
715
2228
|
return this.request("/api/networks");
|
|
716
2229
|
}
|
|
2230
|
+
/**
|
|
2231
|
+
* Retrieves all available vNext networks
|
|
2232
|
+
* @returns Promise containing map of vNext network IDs to vNext network details
|
|
2233
|
+
*/
|
|
2234
|
+
getVNextNetworks() {
|
|
2235
|
+
return this.request("/api/networks/v-next");
|
|
2236
|
+
}
|
|
717
2237
|
/**
|
|
718
2238
|
* Gets a network instance by ID
|
|
719
2239
|
* @param networkId - ID of the network to retrieve
|
|
@@ -722,6 +2242,185 @@ var MastraClient = class extends BaseResource {
|
|
|
722
2242
|
getNetwork(networkId) {
|
|
723
2243
|
return new Network(this.options, networkId);
|
|
724
2244
|
}
|
|
2245
|
+
/**
|
|
2246
|
+
* Gets a vNext network instance by ID
|
|
2247
|
+
* @param networkId - ID of the vNext network to retrieve
|
|
2248
|
+
* @returns vNext Network instance
|
|
2249
|
+
*/
|
|
2250
|
+
getVNextNetwork(networkId) {
|
|
2251
|
+
return new VNextNetwork(this.options, networkId);
|
|
2252
|
+
}
|
|
2253
|
+
/**
|
|
2254
|
+
* Retrieves a list of available MCP servers.
|
|
2255
|
+
* @param params - Optional parameters for pagination (limit, offset).
|
|
2256
|
+
* @returns Promise containing the list of MCP servers and pagination info.
|
|
2257
|
+
*/
|
|
2258
|
+
getMcpServers(params) {
|
|
2259
|
+
const searchParams = new URLSearchParams();
|
|
2260
|
+
if (params?.limit !== void 0) {
|
|
2261
|
+
searchParams.set("limit", String(params.limit));
|
|
2262
|
+
}
|
|
2263
|
+
if (params?.offset !== void 0) {
|
|
2264
|
+
searchParams.set("offset", String(params.offset));
|
|
2265
|
+
}
|
|
2266
|
+
const queryString = searchParams.toString();
|
|
2267
|
+
return this.request(`/api/mcp/v0/servers${queryString ? `?${queryString}` : ""}`);
|
|
2268
|
+
}
|
|
2269
|
+
/**
|
|
2270
|
+
* Retrieves detailed information for a specific MCP server.
|
|
2271
|
+
* @param serverId - The ID of the MCP server to retrieve.
|
|
2272
|
+
* @param params - Optional parameters, e.g., specific version.
|
|
2273
|
+
* @returns Promise containing the detailed MCP server information.
|
|
2274
|
+
*/
|
|
2275
|
+
getMcpServerDetails(serverId, params) {
|
|
2276
|
+
const searchParams = new URLSearchParams();
|
|
2277
|
+
if (params?.version) {
|
|
2278
|
+
searchParams.set("version", params.version);
|
|
2279
|
+
}
|
|
2280
|
+
const queryString = searchParams.toString();
|
|
2281
|
+
return this.request(`/api/mcp/v0/servers/${serverId}${queryString ? `?${queryString}` : ""}`);
|
|
2282
|
+
}
|
|
2283
|
+
/**
|
|
2284
|
+
* Retrieves a list of tools for a specific MCP server.
|
|
2285
|
+
* @param serverId - The ID of the MCP server.
|
|
2286
|
+
* @returns Promise containing the list of tools.
|
|
2287
|
+
*/
|
|
2288
|
+
getMcpServerTools(serverId) {
|
|
2289
|
+
return this.request(`/api/mcp/${serverId}/tools`);
|
|
2290
|
+
}
|
|
2291
|
+
/**
|
|
2292
|
+
* Gets an MCPTool resource instance for a specific tool on an MCP server.
|
|
2293
|
+
* This instance can then be used to fetch details or execute the tool.
|
|
2294
|
+
* @param serverId - The ID of the MCP server.
|
|
2295
|
+
* @param toolId - The ID of the tool.
|
|
2296
|
+
* @returns MCPTool instance.
|
|
2297
|
+
*/
|
|
2298
|
+
getMcpServerTool(serverId, toolId) {
|
|
2299
|
+
return new MCPTool(this.options, serverId, toolId);
|
|
2300
|
+
}
|
|
2301
|
+
/**
|
|
2302
|
+
* Gets an A2A client for interacting with an agent via the A2A protocol
|
|
2303
|
+
* @param agentId - ID of the agent to interact with
|
|
2304
|
+
* @returns A2A client instance
|
|
2305
|
+
*/
|
|
2306
|
+
getA2A(agentId) {
|
|
2307
|
+
return new A2A(this.options, agentId);
|
|
2308
|
+
}
|
|
2309
|
+
/**
|
|
2310
|
+
* Retrieves the working memory for a specific thread (optionally resource-scoped).
|
|
2311
|
+
* @param agentId - ID of the agent.
|
|
2312
|
+
* @param threadId - ID of the thread.
|
|
2313
|
+
* @param resourceId - Optional ID of the resource.
|
|
2314
|
+
* @returns Working memory for the specified thread or resource.
|
|
2315
|
+
*/
|
|
2316
|
+
getWorkingMemory({
|
|
2317
|
+
agentId,
|
|
2318
|
+
threadId,
|
|
2319
|
+
resourceId
|
|
2320
|
+
}) {
|
|
2321
|
+
return this.request(`/api/memory/threads/${threadId}/working-memory?agentId=${agentId}&resourceId=${resourceId}`);
|
|
2322
|
+
}
|
|
2323
|
+
/**
|
|
2324
|
+
* Updates the working memory for a specific thread (optionally resource-scoped).
|
|
2325
|
+
* @param agentId - ID of the agent.
|
|
2326
|
+
* @param threadId - ID of the thread.
|
|
2327
|
+
* @param workingMemory - The new working memory content.
|
|
2328
|
+
* @param resourceId - Optional ID of the resource.
|
|
2329
|
+
*/
|
|
2330
|
+
updateWorkingMemory({
|
|
2331
|
+
agentId,
|
|
2332
|
+
threadId,
|
|
2333
|
+
workingMemory,
|
|
2334
|
+
resourceId
|
|
2335
|
+
}) {
|
|
2336
|
+
return this.request(`/api/memory/threads/${threadId}/working-memory?agentId=${agentId}`, {
|
|
2337
|
+
method: "POST",
|
|
2338
|
+
body: {
|
|
2339
|
+
workingMemory,
|
|
2340
|
+
resourceId
|
|
2341
|
+
}
|
|
2342
|
+
});
|
|
2343
|
+
}
|
|
2344
|
+
/**
|
|
2345
|
+
* Retrieves all available scorers
|
|
2346
|
+
* @returns Promise containing list of available scorers
|
|
2347
|
+
*/
|
|
2348
|
+
getScorers() {
|
|
2349
|
+
return this.request("/api/scores/scorers");
|
|
2350
|
+
}
|
|
2351
|
+
/**
|
|
2352
|
+
* Retrieves a scorer by ID
|
|
2353
|
+
* @param scorerId - ID of the scorer to retrieve
|
|
2354
|
+
* @returns Promise containing the scorer
|
|
2355
|
+
*/
|
|
2356
|
+
getScorer(scorerId) {
|
|
2357
|
+
return this.request(`/api/scores/scorers/${scorerId}`);
|
|
2358
|
+
}
|
|
2359
|
+
getScoresByScorerId(params) {
|
|
2360
|
+
const { page, perPage, scorerId, entityId, entityType } = params;
|
|
2361
|
+
const searchParams = new URLSearchParams();
|
|
2362
|
+
if (entityId) {
|
|
2363
|
+
searchParams.set("entityId", entityId);
|
|
2364
|
+
}
|
|
2365
|
+
if (entityType) {
|
|
2366
|
+
searchParams.set("entityType", entityType);
|
|
2367
|
+
}
|
|
2368
|
+
if (page !== void 0) {
|
|
2369
|
+
searchParams.set("page", String(page));
|
|
2370
|
+
}
|
|
2371
|
+
if (perPage !== void 0) {
|
|
2372
|
+
searchParams.set("perPage", String(perPage));
|
|
2373
|
+
}
|
|
2374
|
+
const queryString = searchParams.toString();
|
|
2375
|
+
return this.request(`/api/scores/scorer/${scorerId}${queryString ? `?${queryString}` : ""}`);
|
|
2376
|
+
}
|
|
2377
|
+
/**
|
|
2378
|
+
* Retrieves scores by run ID
|
|
2379
|
+
* @param params - Parameters containing run ID and pagination options
|
|
2380
|
+
* @returns Promise containing scores and pagination info
|
|
2381
|
+
*/
|
|
2382
|
+
getScoresByRunId(params) {
|
|
2383
|
+
const { runId, page, perPage } = params;
|
|
2384
|
+
const searchParams = new URLSearchParams();
|
|
2385
|
+
if (page !== void 0) {
|
|
2386
|
+
searchParams.set("page", String(page));
|
|
2387
|
+
}
|
|
2388
|
+
if (perPage !== void 0) {
|
|
2389
|
+
searchParams.set("perPage", String(perPage));
|
|
2390
|
+
}
|
|
2391
|
+
const queryString = searchParams.toString();
|
|
2392
|
+
return this.request(`/api/scores/run/${runId}${queryString ? `?${queryString}` : ""}`);
|
|
2393
|
+
}
|
|
2394
|
+
/**
|
|
2395
|
+
* Retrieves scores by entity ID and type
|
|
2396
|
+
* @param params - Parameters containing entity ID, type, and pagination options
|
|
2397
|
+
* @returns Promise containing scores and pagination info
|
|
2398
|
+
*/
|
|
2399
|
+
getScoresByEntityId(params) {
|
|
2400
|
+
const { entityId, entityType, page, perPage } = params;
|
|
2401
|
+
const searchParams = new URLSearchParams();
|
|
2402
|
+
if (page !== void 0) {
|
|
2403
|
+
searchParams.set("page", String(page));
|
|
2404
|
+
}
|
|
2405
|
+
if (perPage !== void 0) {
|
|
2406
|
+
searchParams.set("perPage", String(perPage));
|
|
2407
|
+
}
|
|
2408
|
+
const queryString = searchParams.toString();
|
|
2409
|
+
return this.request(`/api/scores/entity/${entityType}/${entityId}${queryString ? `?${queryString}` : ""}`);
|
|
2410
|
+
}
|
|
2411
|
+
/**
|
|
2412
|
+
* Saves a score
|
|
2413
|
+
* @param params - Parameters containing the score data to save
|
|
2414
|
+
* @returns Promise containing the saved score
|
|
2415
|
+
*/
|
|
2416
|
+
saveScore(params) {
|
|
2417
|
+
return this.request("/api/scores", {
|
|
2418
|
+
method: "POST",
|
|
2419
|
+
body: params
|
|
2420
|
+
});
|
|
2421
|
+
}
|
|
725
2422
|
};
|
|
726
2423
|
|
|
727
2424
|
export { MastraClient };
|
|
2425
|
+
//# sourceMappingURL=index.js.map
|
|
2426
|
+
//# sourceMappingURL=index.js.map
|