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