@mastra/client-js 0.10.12 → 0.10.13
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 +20 -0
- package/CHANGELOG.md +6 -0
- package/dist/index.cjs +2246 -0
- package/dist/index.d.cts +1166 -0
- package/dist/index.d.ts +1166 -0
- package/dist/index.js +2240 -0
- package/package.json +2 -2
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,2246 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var client = require('@ag-ui/client');
|
|
4
|
+
var rxjs = require('rxjs');
|
|
5
|
+
var uiUtils = require('@ai-sdk/ui-utils');
|
|
6
|
+
var zod = require('zod');
|
|
7
|
+
var originalZodToJsonSchema = require('zod-to-json-schema');
|
|
8
|
+
var tools = require('@mastra/core/tools');
|
|
9
|
+
var runtimeContext = require('@mastra/core/runtime-context');
|
|
10
|
+
|
|
11
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
12
|
+
|
|
13
|
+
var originalZodToJsonSchema__default = /*#__PURE__*/_interopDefault(originalZodToJsonSchema);
|
|
14
|
+
|
|
15
|
+
// src/adapters/agui.ts
|
|
16
|
+
var AGUIAdapter = class extends client.AbstractAgent {
|
|
17
|
+
agent;
|
|
18
|
+
resourceId;
|
|
19
|
+
constructor({ agent, agentId, resourceId, ...rest }) {
|
|
20
|
+
super({
|
|
21
|
+
agentId,
|
|
22
|
+
...rest
|
|
23
|
+
});
|
|
24
|
+
this.agent = agent;
|
|
25
|
+
this.resourceId = resourceId;
|
|
26
|
+
}
|
|
27
|
+
run(input) {
|
|
28
|
+
return new rxjs.Observable((subscriber) => {
|
|
29
|
+
const convertedMessages = convertMessagesToMastraMessages(input.messages);
|
|
30
|
+
subscriber.next({
|
|
31
|
+
type: client.EventType.RUN_STARTED,
|
|
32
|
+
threadId: input.threadId,
|
|
33
|
+
runId: input.runId
|
|
34
|
+
});
|
|
35
|
+
this.agent.stream({
|
|
36
|
+
threadId: input.threadId,
|
|
37
|
+
resourceId: this.resourceId ?? "",
|
|
38
|
+
runId: input.runId,
|
|
39
|
+
messages: convertedMessages,
|
|
40
|
+
clientTools: input.tools.reduce(
|
|
41
|
+
(acc, tool) => {
|
|
42
|
+
acc[tool.name] = {
|
|
43
|
+
id: tool.name,
|
|
44
|
+
description: tool.description,
|
|
45
|
+
inputSchema: tool.parameters
|
|
46
|
+
};
|
|
47
|
+
return acc;
|
|
48
|
+
},
|
|
49
|
+
{}
|
|
50
|
+
)
|
|
51
|
+
}).then((response) => {
|
|
52
|
+
let currentMessageId = void 0;
|
|
53
|
+
let isInTextMessage = false;
|
|
54
|
+
return response.processDataStream({
|
|
55
|
+
onTextPart: (text) => {
|
|
56
|
+
if (currentMessageId === void 0) {
|
|
57
|
+
currentMessageId = generateUUID();
|
|
58
|
+
const message2 = {
|
|
59
|
+
type: client.EventType.TEXT_MESSAGE_START,
|
|
60
|
+
messageId: currentMessageId,
|
|
61
|
+
role: "assistant"
|
|
62
|
+
};
|
|
63
|
+
subscriber.next(message2);
|
|
64
|
+
isInTextMessage = true;
|
|
65
|
+
}
|
|
66
|
+
const message = {
|
|
67
|
+
type: client.EventType.TEXT_MESSAGE_CONTENT,
|
|
68
|
+
messageId: currentMessageId,
|
|
69
|
+
delta: text
|
|
70
|
+
};
|
|
71
|
+
subscriber.next(message);
|
|
72
|
+
},
|
|
73
|
+
onFinishMessagePart: () => {
|
|
74
|
+
if (currentMessageId !== void 0) {
|
|
75
|
+
const message = {
|
|
76
|
+
type: client.EventType.TEXT_MESSAGE_END,
|
|
77
|
+
messageId: currentMessageId
|
|
78
|
+
};
|
|
79
|
+
subscriber.next(message);
|
|
80
|
+
isInTextMessage = false;
|
|
81
|
+
}
|
|
82
|
+
subscriber.next({
|
|
83
|
+
type: client.EventType.RUN_FINISHED,
|
|
84
|
+
threadId: input.threadId,
|
|
85
|
+
runId: input.runId
|
|
86
|
+
});
|
|
87
|
+
subscriber.complete();
|
|
88
|
+
},
|
|
89
|
+
onToolCallPart(streamPart) {
|
|
90
|
+
const parentMessageId = currentMessageId || generateUUID();
|
|
91
|
+
if (isInTextMessage) {
|
|
92
|
+
const message = {
|
|
93
|
+
type: client.EventType.TEXT_MESSAGE_END,
|
|
94
|
+
messageId: parentMessageId
|
|
95
|
+
};
|
|
96
|
+
subscriber.next(message);
|
|
97
|
+
isInTextMessage = false;
|
|
98
|
+
}
|
|
99
|
+
subscriber.next({
|
|
100
|
+
type: client.EventType.TOOL_CALL_START,
|
|
101
|
+
toolCallId: streamPart.toolCallId,
|
|
102
|
+
toolCallName: streamPart.toolName,
|
|
103
|
+
parentMessageId
|
|
104
|
+
});
|
|
105
|
+
subscriber.next({
|
|
106
|
+
type: client.EventType.TOOL_CALL_ARGS,
|
|
107
|
+
toolCallId: streamPart.toolCallId,
|
|
108
|
+
delta: JSON.stringify(streamPart.args),
|
|
109
|
+
parentMessageId
|
|
110
|
+
});
|
|
111
|
+
subscriber.next({
|
|
112
|
+
type: client.EventType.TOOL_CALL_END,
|
|
113
|
+
toolCallId: streamPart.toolCallId,
|
|
114
|
+
parentMessageId
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}).catch((error) => {
|
|
119
|
+
console.error("error", error);
|
|
120
|
+
subscriber.error(error);
|
|
121
|
+
});
|
|
122
|
+
return () => {
|
|
123
|
+
};
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
function generateUUID() {
|
|
128
|
+
if (typeof crypto !== "undefined") {
|
|
129
|
+
if (typeof crypto.randomUUID === "function") {
|
|
130
|
+
return crypto.randomUUID();
|
|
131
|
+
}
|
|
132
|
+
if (typeof crypto.getRandomValues === "function") {
|
|
133
|
+
const buffer = new Uint8Array(16);
|
|
134
|
+
crypto.getRandomValues(buffer);
|
|
135
|
+
buffer[6] = buffer[6] & 15 | 64;
|
|
136
|
+
buffer[8] = buffer[8] & 63 | 128;
|
|
137
|
+
let hex = "";
|
|
138
|
+
for (let i = 0; i < 16; i++) {
|
|
139
|
+
hex += buffer[i].toString(16).padStart(2, "0");
|
|
140
|
+
if (i === 3 || i === 5 || i === 7 || i === 9) hex += "-";
|
|
141
|
+
}
|
|
142
|
+
return hex;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
|
146
|
+
const r = Math.random() * 16 | 0;
|
|
147
|
+
const v = c === "x" ? r : r & 3 | 8;
|
|
148
|
+
return v.toString(16);
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
function convertMessagesToMastraMessages(messages) {
|
|
152
|
+
const result = [];
|
|
153
|
+
for (const message of messages) {
|
|
154
|
+
if (message.role === "assistant") {
|
|
155
|
+
const parts = message.content ? [{ type: "text", text: message.content }] : [];
|
|
156
|
+
for (const toolCall of message.toolCalls ?? []) {
|
|
157
|
+
parts.push({
|
|
158
|
+
type: "tool-call",
|
|
159
|
+
toolCallId: toolCall.id,
|
|
160
|
+
toolName: toolCall.function.name,
|
|
161
|
+
args: JSON.parse(toolCall.function.arguments)
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
result.push({
|
|
165
|
+
role: "assistant",
|
|
166
|
+
content: parts
|
|
167
|
+
});
|
|
168
|
+
if (message.toolCalls?.length) {
|
|
169
|
+
result.push({
|
|
170
|
+
role: "tool",
|
|
171
|
+
content: message.toolCalls.map((toolCall) => ({
|
|
172
|
+
type: "tool-result",
|
|
173
|
+
toolCallId: toolCall.id,
|
|
174
|
+
toolName: toolCall.function.name,
|
|
175
|
+
result: JSON.parse(toolCall.function.arguments)
|
|
176
|
+
}))
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
} else if (message.role === "user") {
|
|
180
|
+
result.push({
|
|
181
|
+
role: "user",
|
|
182
|
+
content: message.content || ""
|
|
183
|
+
});
|
|
184
|
+
} else if (message.role === "tool") {
|
|
185
|
+
result.push({
|
|
186
|
+
role: "tool",
|
|
187
|
+
content: [
|
|
188
|
+
{
|
|
189
|
+
type: "tool-result",
|
|
190
|
+
toolCallId: message.toolCallId,
|
|
191
|
+
toolName: "unknown",
|
|
192
|
+
result: message.content
|
|
193
|
+
}
|
|
194
|
+
]
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return result;
|
|
199
|
+
}
|
|
200
|
+
function zodToJsonSchema(zodSchema) {
|
|
201
|
+
if (!(zodSchema instanceof zod.ZodSchema)) {
|
|
202
|
+
return zodSchema;
|
|
203
|
+
}
|
|
204
|
+
return originalZodToJsonSchema__default.default(zodSchema, { $refStrategy: "none" });
|
|
205
|
+
}
|
|
206
|
+
function processClientTools(clientTools) {
|
|
207
|
+
if (!clientTools) {
|
|
208
|
+
return void 0;
|
|
209
|
+
}
|
|
210
|
+
return Object.fromEntries(
|
|
211
|
+
Object.entries(clientTools).map(([key, value]) => {
|
|
212
|
+
if (tools.isVercelTool(value)) {
|
|
213
|
+
return [
|
|
214
|
+
key,
|
|
215
|
+
{
|
|
216
|
+
...value,
|
|
217
|
+
parameters: value.parameters ? zodToJsonSchema(value.parameters) : void 0
|
|
218
|
+
}
|
|
219
|
+
];
|
|
220
|
+
} else {
|
|
221
|
+
return [
|
|
222
|
+
key,
|
|
223
|
+
{
|
|
224
|
+
...value,
|
|
225
|
+
inputSchema: value.inputSchema ? zodToJsonSchema(value.inputSchema) : void 0,
|
|
226
|
+
outputSchema: value.outputSchema ? zodToJsonSchema(value.outputSchema) : void 0
|
|
227
|
+
}
|
|
228
|
+
];
|
|
229
|
+
}
|
|
230
|
+
})
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// src/resources/base.ts
|
|
235
|
+
var BaseResource = class {
|
|
236
|
+
options;
|
|
237
|
+
constructor(options) {
|
|
238
|
+
this.options = options;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Makes an HTTP request to the API with retries and exponential backoff
|
|
242
|
+
* @param path - The API endpoint path
|
|
243
|
+
* @param options - Optional request configuration
|
|
244
|
+
* @returns Promise containing the response data
|
|
245
|
+
*/
|
|
246
|
+
async request(path, options = {}) {
|
|
247
|
+
let lastError = null;
|
|
248
|
+
const { baseUrl, retries = 3, backoffMs = 100, maxBackoffMs = 1e3, headers = {} } = this.options;
|
|
249
|
+
let delay = backoffMs;
|
|
250
|
+
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
251
|
+
try {
|
|
252
|
+
const response = await fetch(`${baseUrl.replace(/\/$/, "")}${path}`, {
|
|
253
|
+
...options,
|
|
254
|
+
headers: {
|
|
255
|
+
...options.method === "POST" || options.method === "PUT" ? { "content-type": "application/json" } : {},
|
|
256
|
+
...headers,
|
|
257
|
+
...options.headers
|
|
258
|
+
// TODO: Bring this back once we figure out what we/users need to do to make this work with cross-origin requests
|
|
259
|
+
// 'x-mastra-client-type': 'js',
|
|
260
|
+
},
|
|
261
|
+
signal: this.options.abortSignal,
|
|
262
|
+
body: options.body instanceof FormData ? options.body : options.body ? JSON.stringify(options.body) : void 0
|
|
263
|
+
});
|
|
264
|
+
if (!response.ok) {
|
|
265
|
+
const errorBody = await response.text();
|
|
266
|
+
let errorMessage = `HTTP error! status: ${response.status}`;
|
|
267
|
+
try {
|
|
268
|
+
const errorJson = JSON.parse(errorBody);
|
|
269
|
+
errorMessage += ` - ${JSON.stringify(errorJson)}`;
|
|
270
|
+
} catch {
|
|
271
|
+
if (errorBody) {
|
|
272
|
+
errorMessage += ` - ${errorBody}`;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
throw new Error(errorMessage);
|
|
276
|
+
}
|
|
277
|
+
if (options.stream) {
|
|
278
|
+
return response;
|
|
279
|
+
}
|
|
280
|
+
const data = await response.json();
|
|
281
|
+
return data;
|
|
282
|
+
} catch (error) {
|
|
283
|
+
lastError = error;
|
|
284
|
+
if (attempt === retries) {
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
288
|
+
delay = Math.min(delay * 2, maxBackoffMs);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
throw lastError || new Error("Request failed");
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
function parseClientRuntimeContext(runtimeContext$1) {
|
|
295
|
+
if (runtimeContext$1) {
|
|
296
|
+
if (runtimeContext$1 instanceof runtimeContext.RuntimeContext) {
|
|
297
|
+
return Object.fromEntries(runtimeContext$1.entries());
|
|
298
|
+
}
|
|
299
|
+
return runtimeContext$1;
|
|
300
|
+
}
|
|
301
|
+
return void 0;
|
|
302
|
+
}
|
|
303
|
+
var AgentVoice = class extends BaseResource {
|
|
304
|
+
constructor(options, agentId) {
|
|
305
|
+
super(options);
|
|
306
|
+
this.agentId = agentId;
|
|
307
|
+
this.agentId = agentId;
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Convert text to speech using the agent's voice provider
|
|
311
|
+
* @param text - Text to convert to speech
|
|
312
|
+
* @param options - Optional provider-specific options for speech generation
|
|
313
|
+
* @returns Promise containing the audio data
|
|
314
|
+
*/
|
|
315
|
+
async speak(text, options) {
|
|
316
|
+
return this.request(`/api/agents/${this.agentId}/voice/speak`, {
|
|
317
|
+
method: "POST",
|
|
318
|
+
headers: {
|
|
319
|
+
"Content-Type": "application/json"
|
|
320
|
+
},
|
|
321
|
+
body: { input: text, options },
|
|
322
|
+
stream: true
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Convert speech to text using the agent's voice provider
|
|
327
|
+
* @param audio - Audio data to transcribe
|
|
328
|
+
* @param options - Optional provider-specific options
|
|
329
|
+
* @returns Promise containing the transcribed text
|
|
330
|
+
*/
|
|
331
|
+
listen(audio, options) {
|
|
332
|
+
const formData = new FormData();
|
|
333
|
+
formData.append("audio", audio);
|
|
334
|
+
if (options) {
|
|
335
|
+
formData.append("options", JSON.stringify(options));
|
|
336
|
+
}
|
|
337
|
+
return this.request(`/api/agents/${this.agentId}/voice/listen`, {
|
|
338
|
+
method: "POST",
|
|
339
|
+
body: formData
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Get available speakers for the agent's voice provider
|
|
344
|
+
* @returns Promise containing list of available speakers
|
|
345
|
+
*/
|
|
346
|
+
getSpeakers() {
|
|
347
|
+
return this.request(`/api/agents/${this.agentId}/voice/speakers`);
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Get the listener configuration for the agent's voice provider
|
|
351
|
+
* @returns Promise containing a check if the agent has listening capabilities
|
|
352
|
+
*/
|
|
353
|
+
getListener() {
|
|
354
|
+
return this.request(`/api/agents/${this.agentId}/voice/listener`);
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
var Agent = class extends BaseResource {
|
|
358
|
+
constructor(options, agentId) {
|
|
359
|
+
super(options);
|
|
360
|
+
this.agentId = agentId;
|
|
361
|
+
this.voice = new AgentVoice(options, this.agentId);
|
|
362
|
+
}
|
|
363
|
+
voice;
|
|
364
|
+
/**
|
|
365
|
+
* Retrieves details about the agent
|
|
366
|
+
* @returns Promise containing agent details including model and instructions
|
|
367
|
+
*/
|
|
368
|
+
details() {
|
|
369
|
+
return this.request(`/api/agents/${this.agentId}`);
|
|
370
|
+
}
|
|
371
|
+
async generate(params) {
|
|
372
|
+
const processedParams = {
|
|
373
|
+
...params,
|
|
374
|
+
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
375
|
+
experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
|
|
376
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
377
|
+
clientTools: processClientTools(params.clientTools)
|
|
378
|
+
};
|
|
379
|
+
const { runId, resourceId, threadId, runtimeContext } = processedParams;
|
|
380
|
+
const response = await this.request(`/api/agents/${this.agentId}/generate`, {
|
|
381
|
+
method: "POST",
|
|
382
|
+
body: processedParams
|
|
383
|
+
});
|
|
384
|
+
if (response.finishReason === "tool-calls") {
|
|
385
|
+
const toolCalls = response.toolCalls;
|
|
386
|
+
if (!toolCalls || !Array.isArray(toolCalls)) {
|
|
387
|
+
return response;
|
|
388
|
+
}
|
|
389
|
+
for (const toolCall of toolCalls) {
|
|
390
|
+
const clientTool = params.clientTools?.[toolCall.toolName];
|
|
391
|
+
if (clientTool && clientTool.execute) {
|
|
392
|
+
const result = await clientTool.execute(
|
|
393
|
+
{ context: toolCall?.args, runId, resourceId, threadId, runtimeContext },
|
|
394
|
+
{
|
|
395
|
+
messages: response.messages,
|
|
396
|
+
toolCallId: toolCall?.toolCallId
|
|
397
|
+
}
|
|
398
|
+
);
|
|
399
|
+
const updatedMessages = [
|
|
400
|
+
{
|
|
401
|
+
role: "user",
|
|
402
|
+
content: params.messages
|
|
403
|
+
},
|
|
404
|
+
...response.response.messages,
|
|
405
|
+
{
|
|
406
|
+
role: "tool",
|
|
407
|
+
content: [
|
|
408
|
+
{
|
|
409
|
+
type: "tool-result",
|
|
410
|
+
toolCallId: toolCall.toolCallId,
|
|
411
|
+
toolName: toolCall.toolName,
|
|
412
|
+
result
|
|
413
|
+
}
|
|
414
|
+
]
|
|
415
|
+
}
|
|
416
|
+
];
|
|
417
|
+
return this.generate({
|
|
418
|
+
...params,
|
|
419
|
+
messages: updatedMessages
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
return response;
|
|
425
|
+
}
|
|
426
|
+
async processChatResponse({
|
|
427
|
+
stream,
|
|
428
|
+
update,
|
|
429
|
+
onToolCall,
|
|
430
|
+
onFinish,
|
|
431
|
+
getCurrentDate = () => /* @__PURE__ */ new Date(),
|
|
432
|
+
lastMessage,
|
|
433
|
+
streamProtocol
|
|
434
|
+
}) {
|
|
435
|
+
const replaceLastMessage = lastMessage?.role === "assistant";
|
|
436
|
+
let step = replaceLastMessage ? 1 + // find max step in existing tool invocations:
|
|
437
|
+
(lastMessage.toolInvocations?.reduce((max, toolInvocation) => {
|
|
438
|
+
return Math.max(max, toolInvocation.step ?? 0);
|
|
439
|
+
}, 0) ?? 0) : 0;
|
|
440
|
+
const message = replaceLastMessage ? structuredClone(lastMessage) : {
|
|
441
|
+
id: crypto.randomUUID(),
|
|
442
|
+
createdAt: getCurrentDate(),
|
|
443
|
+
role: "assistant",
|
|
444
|
+
content: "",
|
|
445
|
+
parts: []
|
|
446
|
+
};
|
|
447
|
+
let currentTextPart = void 0;
|
|
448
|
+
let currentReasoningPart = void 0;
|
|
449
|
+
let currentReasoningTextDetail = void 0;
|
|
450
|
+
function updateToolInvocationPart(toolCallId, invocation) {
|
|
451
|
+
const part = message.parts.find(
|
|
452
|
+
(part2) => part2.type === "tool-invocation" && part2.toolInvocation.toolCallId === toolCallId
|
|
453
|
+
);
|
|
454
|
+
if (part != null) {
|
|
455
|
+
part.toolInvocation = invocation;
|
|
456
|
+
} else {
|
|
457
|
+
message.parts.push({
|
|
458
|
+
type: "tool-invocation",
|
|
459
|
+
toolInvocation: invocation
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
const data = [];
|
|
464
|
+
let messageAnnotations = replaceLastMessage ? lastMessage?.annotations : void 0;
|
|
465
|
+
const partialToolCalls = {};
|
|
466
|
+
let usage = {
|
|
467
|
+
completionTokens: NaN,
|
|
468
|
+
promptTokens: NaN,
|
|
469
|
+
totalTokens: NaN
|
|
470
|
+
};
|
|
471
|
+
let finishReason = "unknown";
|
|
472
|
+
function execUpdate() {
|
|
473
|
+
const copiedData = [...data];
|
|
474
|
+
if (messageAnnotations?.length) {
|
|
475
|
+
message.annotations = messageAnnotations;
|
|
476
|
+
}
|
|
477
|
+
const copiedMessage = {
|
|
478
|
+
// deep copy the message to ensure that deep changes (msg attachments) are updated
|
|
479
|
+
// with SolidJS. SolidJS uses referential integration of sub-objects to detect changes.
|
|
480
|
+
...structuredClone(message),
|
|
481
|
+
// add a revision id to ensure that the message is updated with SWR. SWR uses a
|
|
482
|
+
// hashing approach by default to detect changes, but it only works for shallow
|
|
483
|
+
// changes. This is why we need to add a revision id to ensure that the message
|
|
484
|
+
// is updated with SWR (without it, the changes get stuck in SWR and are not
|
|
485
|
+
// forwarded to rendering):
|
|
486
|
+
revisionId: crypto.randomUUID()
|
|
487
|
+
};
|
|
488
|
+
update({
|
|
489
|
+
message: copiedMessage,
|
|
490
|
+
data: copiedData,
|
|
491
|
+
replaceLastMessage
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
if (streamProtocol === "text") {
|
|
495
|
+
await uiUtils.processTextStream({
|
|
496
|
+
stream,
|
|
497
|
+
onTextPart(value) {
|
|
498
|
+
message.content += value;
|
|
499
|
+
execUpdate();
|
|
500
|
+
}
|
|
501
|
+
});
|
|
502
|
+
onFinish?.({ message, finishReason, usage });
|
|
503
|
+
} else {
|
|
504
|
+
await uiUtils.processDataStream({
|
|
505
|
+
stream,
|
|
506
|
+
onTextPart(value) {
|
|
507
|
+
if (currentTextPart == null) {
|
|
508
|
+
currentTextPart = {
|
|
509
|
+
type: "text",
|
|
510
|
+
text: value
|
|
511
|
+
};
|
|
512
|
+
message.parts.push(currentTextPart);
|
|
513
|
+
} else {
|
|
514
|
+
currentTextPart.text += value;
|
|
515
|
+
}
|
|
516
|
+
message.content += value;
|
|
517
|
+
execUpdate();
|
|
518
|
+
},
|
|
519
|
+
onReasoningPart(value) {
|
|
520
|
+
if (currentReasoningTextDetail == null) {
|
|
521
|
+
currentReasoningTextDetail = { type: "text", text: value };
|
|
522
|
+
if (currentReasoningPart != null) {
|
|
523
|
+
currentReasoningPart.details.push(currentReasoningTextDetail);
|
|
524
|
+
}
|
|
525
|
+
} else {
|
|
526
|
+
currentReasoningTextDetail.text += value;
|
|
527
|
+
}
|
|
528
|
+
if (currentReasoningPart == null) {
|
|
529
|
+
currentReasoningPart = {
|
|
530
|
+
type: "reasoning",
|
|
531
|
+
reasoning: value,
|
|
532
|
+
details: [currentReasoningTextDetail]
|
|
533
|
+
};
|
|
534
|
+
message.parts.push(currentReasoningPart);
|
|
535
|
+
} else {
|
|
536
|
+
currentReasoningPart.reasoning += value;
|
|
537
|
+
}
|
|
538
|
+
message.reasoning = (message.reasoning ?? "") + value;
|
|
539
|
+
execUpdate();
|
|
540
|
+
},
|
|
541
|
+
onReasoningSignaturePart(value) {
|
|
542
|
+
if (currentReasoningTextDetail != null) {
|
|
543
|
+
currentReasoningTextDetail.signature = value.signature;
|
|
544
|
+
}
|
|
545
|
+
},
|
|
546
|
+
onRedactedReasoningPart(value) {
|
|
547
|
+
if (currentReasoningPart == null) {
|
|
548
|
+
currentReasoningPart = {
|
|
549
|
+
type: "reasoning",
|
|
550
|
+
reasoning: "",
|
|
551
|
+
details: []
|
|
552
|
+
};
|
|
553
|
+
message.parts.push(currentReasoningPart);
|
|
554
|
+
}
|
|
555
|
+
currentReasoningPart.details.push({
|
|
556
|
+
type: "redacted",
|
|
557
|
+
data: value.data
|
|
558
|
+
});
|
|
559
|
+
currentReasoningTextDetail = void 0;
|
|
560
|
+
execUpdate();
|
|
561
|
+
},
|
|
562
|
+
onFilePart(value) {
|
|
563
|
+
message.parts.push({
|
|
564
|
+
type: "file",
|
|
565
|
+
mimeType: value.mimeType,
|
|
566
|
+
data: value.data
|
|
567
|
+
});
|
|
568
|
+
execUpdate();
|
|
569
|
+
},
|
|
570
|
+
onSourcePart(value) {
|
|
571
|
+
message.parts.push({
|
|
572
|
+
type: "source",
|
|
573
|
+
source: value
|
|
574
|
+
});
|
|
575
|
+
execUpdate();
|
|
576
|
+
},
|
|
577
|
+
onToolCallStreamingStartPart(value) {
|
|
578
|
+
if (message.toolInvocations == null) {
|
|
579
|
+
message.toolInvocations = [];
|
|
580
|
+
}
|
|
581
|
+
partialToolCalls[value.toolCallId] = {
|
|
582
|
+
text: "",
|
|
583
|
+
step,
|
|
584
|
+
toolName: value.toolName,
|
|
585
|
+
index: message.toolInvocations.length
|
|
586
|
+
};
|
|
587
|
+
const invocation = {
|
|
588
|
+
state: "partial-call",
|
|
589
|
+
step,
|
|
590
|
+
toolCallId: value.toolCallId,
|
|
591
|
+
toolName: value.toolName,
|
|
592
|
+
args: void 0
|
|
593
|
+
};
|
|
594
|
+
message.toolInvocations.push(invocation);
|
|
595
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
596
|
+
execUpdate();
|
|
597
|
+
},
|
|
598
|
+
onToolCallDeltaPart(value) {
|
|
599
|
+
const partialToolCall = partialToolCalls[value.toolCallId];
|
|
600
|
+
partialToolCall.text += value.argsTextDelta;
|
|
601
|
+
const { value: partialArgs } = uiUtils.parsePartialJson(partialToolCall.text);
|
|
602
|
+
const invocation = {
|
|
603
|
+
state: "partial-call",
|
|
604
|
+
step: partialToolCall.step,
|
|
605
|
+
toolCallId: value.toolCallId,
|
|
606
|
+
toolName: partialToolCall.toolName,
|
|
607
|
+
args: partialArgs
|
|
608
|
+
};
|
|
609
|
+
message.toolInvocations[partialToolCall.index] = invocation;
|
|
610
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
611
|
+
execUpdate();
|
|
612
|
+
},
|
|
613
|
+
async onToolCallPart(value) {
|
|
614
|
+
const invocation = {
|
|
615
|
+
state: "call",
|
|
616
|
+
step,
|
|
617
|
+
...value
|
|
618
|
+
};
|
|
619
|
+
if (partialToolCalls[value.toolCallId] != null) {
|
|
620
|
+
message.toolInvocations[partialToolCalls[value.toolCallId].index] = invocation;
|
|
621
|
+
} else {
|
|
622
|
+
if (message.toolInvocations == null) {
|
|
623
|
+
message.toolInvocations = [];
|
|
624
|
+
}
|
|
625
|
+
message.toolInvocations.push(invocation);
|
|
626
|
+
}
|
|
627
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
628
|
+
execUpdate();
|
|
629
|
+
if (onToolCall) {
|
|
630
|
+
const result = await onToolCall({ toolCall: value });
|
|
631
|
+
if (result != null) {
|
|
632
|
+
const invocation2 = {
|
|
633
|
+
state: "result",
|
|
634
|
+
step,
|
|
635
|
+
...value,
|
|
636
|
+
result
|
|
637
|
+
};
|
|
638
|
+
message.toolInvocations[message.toolInvocations.length - 1] = invocation2;
|
|
639
|
+
updateToolInvocationPart(value.toolCallId, invocation2);
|
|
640
|
+
execUpdate();
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
},
|
|
644
|
+
onToolResultPart(value) {
|
|
645
|
+
const toolInvocations = message.toolInvocations;
|
|
646
|
+
if (toolInvocations == null) {
|
|
647
|
+
throw new Error("tool_result must be preceded by a tool_call");
|
|
648
|
+
}
|
|
649
|
+
const toolInvocationIndex = toolInvocations.findIndex(
|
|
650
|
+
(invocation2) => invocation2.toolCallId === value.toolCallId
|
|
651
|
+
);
|
|
652
|
+
if (toolInvocationIndex === -1) {
|
|
653
|
+
throw new Error("tool_result must be preceded by a tool_call with the same toolCallId");
|
|
654
|
+
}
|
|
655
|
+
const invocation = {
|
|
656
|
+
...toolInvocations[toolInvocationIndex],
|
|
657
|
+
state: "result",
|
|
658
|
+
...value
|
|
659
|
+
};
|
|
660
|
+
toolInvocations[toolInvocationIndex] = invocation;
|
|
661
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
662
|
+
execUpdate();
|
|
663
|
+
},
|
|
664
|
+
onDataPart(value) {
|
|
665
|
+
data.push(...value);
|
|
666
|
+
execUpdate();
|
|
667
|
+
},
|
|
668
|
+
onMessageAnnotationsPart(value) {
|
|
669
|
+
if (messageAnnotations == null) {
|
|
670
|
+
messageAnnotations = [...value];
|
|
671
|
+
} else {
|
|
672
|
+
messageAnnotations.push(...value);
|
|
673
|
+
}
|
|
674
|
+
execUpdate();
|
|
675
|
+
},
|
|
676
|
+
onFinishStepPart(value) {
|
|
677
|
+
step += 1;
|
|
678
|
+
currentTextPart = value.isContinued ? currentTextPart : void 0;
|
|
679
|
+
currentReasoningPart = void 0;
|
|
680
|
+
currentReasoningTextDetail = void 0;
|
|
681
|
+
},
|
|
682
|
+
onStartStepPart(value) {
|
|
683
|
+
if (!replaceLastMessage) {
|
|
684
|
+
message.id = value.messageId;
|
|
685
|
+
}
|
|
686
|
+
message.parts.push({ type: "step-start" });
|
|
687
|
+
execUpdate();
|
|
688
|
+
},
|
|
689
|
+
onFinishMessagePart(value) {
|
|
690
|
+
finishReason = value.finishReason;
|
|
691
|
+
if (value.usage != null) {
|
|
692
|
+
usage = value.usage;
|
|
693
|
+
}
|
|
694
|
+
},
|
|
695
|
+
onErrorPart(error) {
|
|
696
|
+
throw new Error(error);
|
|
697
|
+
}
|
|
698
|
+
});
|
|
699
|
+
onFinish?.({ message, finishReason, usage });
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
/**
|
|
703
|
+
* Processes the stream response and handles tool calls
|
|
704
|
+
*/
|
|
705
|
+
async processStreamResponse(processedParams, writable) {
|
|
706
|
+
const response = await this.request(`/api/agents/${this.agentId}/stream`, {
|
|
707
|
+
method: "POST",
|
|
708
|
+
body: processedParams,
|
|
709
|
+
stream: true
|
|
710
|
+
});
|
|
711
|
+
if (!response.body) {
|
|
712
|
+
throw new Error("No response body");
|
|
713
|
+
}
|
|
714
|
+
try {
|
|
715
|
+
const streamProtocol = processedParams.output ? "text" : "data";
|
|
716
|
+
let toolCalls = [];
|
|
717
|
+
let messages = [];
|
|
718
|
+
const [streamForWritable, streamForProcessing] = response.body.tee();
|
|
719
|
+
streamForWritable.pipeTo(writable, {
|
|
720
|
+
preventClose: true
|
|
721
|
+
}).catch((error) => {
|
|
722
|
+
console.error("Error piping to writable stream:", error);
|
|
723
|
+
});
|
|
724
|
+
this.processChatResponse({
|
|
725
|
+
stream: streamForProcessing,
|
|
726
|
+
update: ({ message }) => {
|
|
727
|
+
messages.push(message);
|
|
728
|
+
},
|
|
729
|
+
onFinish: async ({ finishReason, message }) => {
|
|
730
|
+
if (finishReason === "tool-calls") {
|
|
731
|
+
const toolCall = [...message?.parts ?? []].reverse().find((part) => part.type === "tool-invocation")?.toolInvocation;
|
|
732
|
+
if (toolCall) {
|
|
733
|
+
toolCalls.push(toolCall);
|
|
734
|
+
}
|
|
735
|
+
for (const toolCall2 of toolCalls) {
|
|
736
|
+
const clientTool = processedParams.clientTools?.[toolCall2.toolName];
|
|
737
|
+
if (clientTool && clientTool.execute) {
|
|
738
|
+
const result = await clientTool.execute(
|
|
739
|
+
{
|
|
740
|
+
context: toolCall2?.args,
|
|
741
|
+
runId: processedParams.runId,
|
|
742
|
+
resourceId: processedParams.resourceId,
|
|
743
|
+
threadId: processedParams.threadId,
|
|
744
|
+
runtimeContext: processedParams.runtimeContext
|
|
745
|
+
},
|
|
746
|
+
{
|
|
747
|
+
messages: response.messages,
|
|
748
|
+
toolCallId: toolCall2?.toolCallId
|
|
749
|
+
}
|
|
750
|
+
);
|
|
751
|
+
const lastMessage = JSON.parse(JSON.stringify(messages[messages.length - 1]));
|
|
752
|
+
const toolInvocationPart = lastMessage?.parts?.find(
|
|
753
|
+
(part) => part.type === "tool-invocation" && part.toolInvocation?.toolCallId === toolCall2.toolCallId
|
|
754
|
+
);
|
|
755
|
+
if (toolInvocationPart) {
|
|
756
|
+
toolInvocationPart.toolInvocation = {
|
|
757
|
+
...toolInvocationPart.toolInvocation,
|
|
758
|
+
state: "result",
|
|
759
|
+
result
|
|
760
|
+
};
|
|
761
|
+
}
|
|
762
|
+
const toolInvocation = lastMessage?.toolInvocations?.find(
|
|
763
|
+
(toolInvocation2) => toolInvocation2.toolCallId === toolCall2.toolCallId
|
|
764
|
+
);
|
|
765
|
+
if (toolInvocation) {
|
|
766
|
+
toolInvocation.state = "result";
|
|
767
|
+
toolInvocation.result = result;
|
|
768
|
+
}
|
|
769
|
+
const writer = writable.getWriter();
|
|
770
|
+
try {
|
|
771
|
+
await writer.write(
|
|
772
|
+
new TextEncoder().encode(
|
|
773
|
+
"a:" + JSON.stringify({
|
|
774
|
+
toolCallId: toolCall2.toolCallId,
|
|
775
|
+
result
|
|
776
|
+
}) + "\n"
|
|
777
|
+
)
|
|
778
|
+
);
|
|
779
|
+
} finally {
|
|
780
|
+
writer.releaseLock();
|
|
781
|
+
}
|
|
782
|
+
const originalMessages = processedParams.messages;
|
|
783
|
+
const messageArray = Array.isArray(originalMessages) ? originalMessages : [originalMessages];
|
|
784
|
+
this.processStreamResponse(
|
|
785
|
+
{
|
|
786
|
+
...processedParams,
|
|
787
|
+
messages: [...messageArray, ...messages, lastMessage]
|
|
788
|
+
},
|
|
789
|
+
writable
|
|
790
|
+
);
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
} else {
|
|
794
|
+
setTimeout(() => {
|
|
795
|
+
if (!writable.locked) {
|
|
796
|
+
writable.close();
|
|
797
|
+
}
|
|
798
|
+
}, 0);
|
|
799
|
+
}
|
|
800
|
+
},
|
|
801
|
+
lastMessage: void 0,
|
|
802
|
+
streamProtocol
|
|
803
|
+
});
|
|
804
|
+
} catch (error) {
|
|
805
|
+
console.error("Error processing stream response:", error);
|
|
806
|
+
}
|
|
807
|
+
return response;
|
|
808
|
+
}
|
|
809
|
+
/**
|
|
810
|
+
* Streams a response from the agent
|
|
811
|
+
* @param params - Stream parameters including prompt
|
|
812
|
+
* @returns Promise containing the enhanced Response object with processDataStream and processTextStream methods
|
|
813
|
+
*/
|
|
814
|
+
async stream(params) {
|
|
815
|
+
const processedParams = {
|
|
816
|
+
...params,
|
|
817
|
+
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
818
|
+
experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
|
|
819
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
820
|
+
clientTools: processClientTools(params.clientTools)
|
|
821
|
+
};
|
|
822
|
+
const { readable, writable } = new TransformStream();
|
|
823
|
+
const response = await this.processStreamResponse(processedParams, writable);
|
|
824
|
+
const streamResponse = new Response(readable, {
|
|
825
|
+
status: response.status,
|
|
826
|
+
statusText: response.statusText,
|
|
827
|
+
headers: response.headers
|
|
828
|
+
});
|
|
829
|
+
streamResponse.processDataStream = async (options = {}) => {
|
|
830
|
+
await uiUtils.processDataStream({
|
|
831
|
+
stream: streamResponse.body,
|
|
832
|
+
...options
|
|
833
|
+
});
|
|
834
|
+
};
|
|
835
|
+
streamResponse.processTextStream = async (options) => {
|
|
836
|
+
await uiUtils.processTextStream({
|
|
837
|
+
stream: streamResponse.body,
|
|
838
|
+
onTextPart: options?.onTextPart ?? (() => {
|
|
839
|
+
})
|
|
840
|
+
});
|
|
841
|
+
};
|
|
842
|
+
return streamResponse;
|
|
843
|
+
}
|
|
844
|
+
/**
|
|
845
|
+
* Gets details about a specific tool available to the agent
|
|
846
|
+
* @param toolId - ID of the tool to retrieve
|
|
847
|
+
* @returns Promise containing tool details
|
|
848
|
+
*/
|
|
849
|
+
getTool(toolId) {
|
|
850
|
+
return this.request(`/api/agents/${this.agentId}/tools/${toolId}`);
|
|
851
|
+
}
|
|
852
|
+
/**
|
|
853
|
+
* Executes a tool for the agent
|
|
854
|
+
* @param toolId - ID of the tool to execute
|
|
855
|
+
* @param params - Parameters required for tool execution
|
|
856
|
+
* @returns Promise containing the tool execution results
|
|
857
|
+
*/
|
|
858
|
+
executeTool(toolId, params) {
|
|
859
|
+
const body = {
|
|
860
|
+
data: params.data,
|
|
861
|
+
runtimeContext: params.runtimeContext ? Object.fromEntries(params.runtimeContext.entries()) : void 0
|
|
862
|
+
};
|
|
863
|
+
return this.request(`/api/agents/${this.agentId}/tools/${toolId}/execute`, {
|
|
864
|
+
method: "POST",
|
|
865
|
+
body
|
|
866
|
+
});
|
|
867
|
+
}
|
|
868
|
+
/**
|
|
869
|
+
* Retrieves evaluation results for the agent
|
|
870
|
+
* @returns Promise containing agent evaluations
|
|
871
|
+
*/
|
|
872
|
+
evals() {
|
|
873
|
+
return this.request(`/api/agents/${this.agentId}/evals/ci`);
|
|
874
|
+
}
|
|
875
|
+
/**
|
|
876
|
+
* Retrieves live evaluation results for the agent
|
|
877
|
+
* @returns Promise containing live agent evaluations
|
|
878
|
+
*/
|
|
879
|
+
liveEvals() {
|
|
880
|
+
return this.request(`/api/agents/${this.agentId}/evals/live`);
|
|
881
|
+
}
|
|
882
|
+
};
|
|
883
|
+
var Network = class extends BaseResource {
|
|
884
|
+
constructor(options, networkId) {
|
|
885
|
+
super(options);
|
|
886
|
+
this.networkId = networkId;
|
|
887
|
+
}
|
|
888
|
+
/**
|
|
889
|
+
* Retrieves details about the network
|
|
890
|
+
* @returns Promise containing network details
|
|
891
|
+
*/
|
|
892
|
+
details() {
|
|
893
|
+
return this.request(`/api/networks/${this.networkId}`);
|
|
894
|
+
}
|
|
895
|
+
/**
|
|
896
|
+
* Generates a response from the agent
|
|
897
|
+
* @param params - Generation parameters including prompt
|
|
898
|
+
* @returns Promise containing the generated response
|
|
899
|
+
*/
|
|
900
|
+
generate(params) {
|
|
901
|
+
const processedParams = {
|
|
902
|
+
...params,
|
|
903
|
+
output: zodToJsonSchema(params.output),
|
|
904
|
+
experimental_output: zodToJsonSchema(params.experimental_output)
|
|
905
|
+
};
|
|
906
|
+
return this.request(`/api/networks/${this.networkId}/generate`, {
|
|
907
|
+
method: "POST",
|
|
908
|
+
body: processedParams
|
|
909
|
+
});
|
|
910
|
+
}
|
|
911
|
+
/**
|
|
912
|
+
* Streams a response from the agent
|
|
913
|
+
* @param params - Stream parameters including prompt
|
|
914
|
+
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
915
|
+
*/
|
|
916
|
+
async stream(params) {
|
|
917
|
+
const processedParams = {
|
|
918
|
+
...params,
|
|
919
|
+
output: zodToJsonSchema(params.output),
|
|
920
|
+
experimental_output: zodToJsonSchema(params.experimental_output)
|
|
921
|
+
};
|
|
922
|
+
const response = await this.request(`/api/networks/${this.networkId}/stream`, {
|
|
923
|
+
method: "POST",
|
|
924
|
+
body: processedParams,
|
|
925
|
+
stream: true
|
|
926
|
+
});
|
|
927
|
+
if (!response.body) {
|
|
928
|
+
throw new Error("No response body");
|
|
929
|
+
}
|
|
930
|
+
response.processDataStream = async (options = {}) => {
|
|
931
|
+
await uiUtils.processDataStream({
|
|
932
|
+
stream: response.body,
|
|
933
|
+
...options
|
|
934
|
+
});
|
|
935
|
+
};
|
|
936
|
+
return response;
|
|
937
|
+
}
|
|
938
|
+
};
|
|
939
|
+
|
|
940
|
+
// src/resources/memory-thread.ts
|
|
941
|
+
var MemoryThread = class extends BaseResource {
|
|
942
|
+
constructor(options, threadId, agentId) {
|
|
943
|
+
super(options);
|
|
944
|
+
this.threadId = threadId;
|
|
945
|
+
this.agentId = agentId;
|
|
946
|
+
}
|
|
947
|
+
/**
|
|
948
|
+
* Retrieves the memory thread details
|
|
949
|
+
* @returns Promise containing thread details including title and metadata
|
|
950
|
+
*/
|
|
951
|
+
get() {
|
|
952
|
+
return this.request(`/api/memory/threads/${this.threadId}?agentId=${this.agentId}`);
|
|
953
|
+
}
|
|
954
|
+
/**
|
|
955
|
+
* Updates the memory thread properties
|
|
956
|
+
* @param params - Update parameters including title and metadata
|
|
957
|
+
* @returns Promise containing updated thread details
|
|
958
|
+
*/
|
|
959
|
+
update(params) {
|
|
960
|
+
return this.request(`/api/memory/threads/${this.threadId}?agentId=${this.agentId}`, {
|
|
961
|
+
method: "PATCH",
|
|
962
|
+
body: params
|
|
963
|
+
});
|
|
964
|
+
}
|
|
965
|
+
/**
|
|
966
|
+
* Deletes the memory thread
|
|
967
|
+
* @returns Promise containing deletion result
|
|
968
|
+
*/
|
|
969
|
+
delete() {
|
|
970
|
+
return this.request(`/api/memory/threads/${this.threadId}?agentId=${this.agentId}`, {
|
|
971
|
+
method: "DELETE"
|
|
972
|
+
});
|
|
973
|
+
}
|
|
974
|
+
/**
|
|
975
|
+
* Retrieves messages associated with the thread
|
|
976
|
+
* @param params - Optional parameters including limit for number of messages to retrieve
|
|
977
|
+
* @returns Promise containing thread messages and UI messages
|
|
978
|
+
*/
|
|
979
|
+
getMessages(params) {
|
|
980
|
+
const query = new URLSearchParams({
|
|
981
|
+
agentId: this.agentId,
|
|
982
|
+
...params?.limit ? { limit: params.limit.toString() } : {}
|
|
983
|
+
});
|
|
984
|
+
return this.request(`/api/memory/threads/${this.threadId}/messages?${query.toString()}`);
|
|
985
|
+
}
|
|
986
|
+
};
|
|
987
|
+
|
|
988
|
+
// src/resources/vector.ts
|
|
989
|
+
var Vector = class extends BaseResource {
|
|
990
|
+
constructor(options, vectorName) {
|
|
991
|
+
super(options);
|
|
992
|
+
this.vectorName = vectorName;
|
|
993
|
+
}
|
|
994
|
+
/**
|
|
995
|
+
* Retrieves details about a specific vector index
|
|
996
|
+
* @param indexName - Name of the index to get details for
|
|
997
|
+
* @returns Promise containing vector index details
|
|
998
|
+
*/
|
|
999
|
+
details(indexName) {
|
|
1000
|
+
return this.request(`/api/vector/${this.vectorName}/indexes/${indexName}`);
|
|
1001
|
+
}
|
|
1002
|
+
/**
|
|
1003
|
+
* Deletes a vector index
|
|
1004
|
+
* @param indexName - Name of the index to delete
|
|
1005
|
+
* @returns Promise indicating deletion success
|
|
1006
|
+
*/
|
|
1007
|
+
delete(indexName) {
|
|
1008
|
+
return this.request(`/api/vector/${this.vectorName}/indexes/${indexName}`, {
|
|
1009
|
+
method: "DELETE"
|
|
1010
|
+
});
|
|
1011
|
+
}
|
|
1012
|
+
/**
|
|
1013
|
+
* Retrieves a list of all available indexes
|
|
1014
|
+
* @returns Promise containing array of index names
|
|
1015
|
+
*/
|
|
1016
|
+
getIndexes() {
|
|
1017
|
+
return this.request(`/api/vector/${this.vectorName}/indexes`);
|
|
1018
|
+
}
|
|
1019
|
+
/**
|
|
1020
|
+
* Creates a new vector index
|
|
1021
|
+
* @param params - Parameters for index creation including dimension and metric
|
|
1022
|
+
* @returns Promise indicating creation success
|
|
1023
|
+
*/
|
|
1024
|
+
createIndex(params) {
|
|
1025
|
+
return this.request(`/api/vector/${this.vectorName}/create-index`, {
|
|
1026
|
+
method: "POST",
|
|
1027
|
+
body: params
|
|
1028
|
+
});
|
|
1029
|
+
}
|
|
1030
|
+
/**
|
|
1031
|
+
* Upserts vectors into an index
|
|
1032
|
+
* @param params - Parameters containing vectors, metadata, and optional IDs
|
|
1033
|
+
* @returns Promise containing array of vector IDs
|
|
1034
|
+
*/
|
|
1035
|
+
upsert(params) {
|
|
1036
|
+
return this.request(`/api/vector/${this.vectorName}/upsert`, {
|
|
1037
|
+
method: "POST",
|
|
1038
|
+
body: params
|
|
1039
|
+
});
|
|
1040
|
+
}
|
|
1041
|
+
/**
|
|
1042
|
+
* Queries vectors in an index
|
|
1043
|
+
* @param params - Query parameters including query vector and search options
|
|
1044
|
+
* @returns Promise containing query results
|
|
1045
|
+
*/
|
|
1046
|
+
query(params) {
|
|
1047
|
+
return this.request(`/api/vector/${this.vectorName}/query`, {
|
|
1048
|
+
method: "POST",
|
|
1049
|
+
body: params
|
|
1050
|
+
});
|
|
1051
|
+
}
|
|
1052
|
+
};
|
|
1053
|
+
|
|
1054
|
+
// src/resources/legacy-workflow.ts
|
|
1055
|
+
var RECORD_SEPARATOR = "";
|
|
1056
|
+
var LegacyWorkflow = class extends BaseResource {
|
|
1057
|
+
constructor(options, workflowId) {
|
|
1058
|
+
super(options);
|
|
1059
|
+
this.workflowId = workflowId;
|
|
1060
|
+
}
|
|
1061
|
+
/**
|
|
1062
|
+
* Retrieves details about the legacy workflow
|
|
1063
|
+
* @returns Promise containing legacy workflow details including steps and graphs
|
|
1064
|
+
*/
|
|
1065
|
+
details() {
|
|
1066
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}`);
|
|
1067
|
+
}
|
|
1068
|
+
/**
|
|
1069
|
+
* Retrieves all runs for a legacy workflow
|
|
1070
|
+
* @param params - Parameters for filtering runs
|
|
1071
|
+
* @returns Promise containing legacy workflow runs array
|
|
1072
|
+
*/
|
|
1073
|
+
runs(params) {
|
|
1074
|
+
const searchParams = new URLSearchParams();
|
|
1075
|
+
if (params?.fromDate) {
|
|
1076
|
+
searchParams.set("fromDate", params.fromDate.toISOString());
|
|
1077
|
+
}
|
|
1078
|
+
if (params?.toDate) {
|
|
1079
|
+
searchParams.set("toDate", params.toDate.toISOString());
|
|
1080
|
+
}
|
|
1081
|
+
if (params?.limit) {
|
|
1082
|
+
searchParams.set("limit", String(params.limit));
|
|
1083
|
+
}
|
|
1084
|
+
if (params?.offset) {
|
|
1085
|
+
searchParams.set("offset", String(params.offset));
|
|
1086
|
+
}
|
|
1087
|
+
if (params?.resourceId) {
|
|
1088
|
+
searchParams.set("resourceId", params.resourceId);
|
|
1089
|
+
}
|
|
1090
|
+
if (searchParams.size) {
|
|
1091
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/runs?${searchParams}`);
|
|
1092
|
+
} else {
|
|
1093
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/runs`);
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
/**
|
|
1097
|
+
* Creates a new legacy workflow run
|
|
1098
|
+
* @returns Promise containing the generated run ID
|
|
1099
|
+
*/
|
|
1100
|
+
createRun(params) {
|
|
1101
|
+
const searchParams = new URLSearchParams();
|
|
1102
|
+
if (!!params?.runId) {
|
|
1103
|
+
searchParams.set("runId", params.runId);
|
|
1104
|
+
}
|
|
1105
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/create-run?${searchParams.toString()}`, {
|
|
1106
|
+
method: "POST"
|
|
1107
|
+
});
|
|
1108
|
+
}
|
|
1109
|
+
/**
|
|
1110
|
+
* Starts a legacy workflow run synchronously without waiting for the workflow to complete
|
|
1111
|
+
* @param params - Object containing the runId and triggerData
|
|
1112
|
+
* @returns Promise containing success message
|
|
1113
|
+
*/
|
|
1114
|
+
start(params) {
|
|
1115
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/start?runId=${params.runId}`, {
|
|
1116
|
+
method: "POST",
|
|
1117
|
+
body: params?.triggerData
|
|
1118
|
+
});
|
|
1119
|
+
}
|
|
1120
|
+
/**
|
|
1121
|
+
* Resumes a suspended legacy workflow step synchronously without waiting for the workflow to complete
|
|
1122
|
+
* @param stepId - ID of the step to resume
|
|
1123
|
+
* @param runId - ID of the legacy workflow run
|
|
1124
|
+
* @param context - Context to resume the legacy workflow with
|
|
1125
|
+
* @returns Promise containing the legacy workflow resume results
|
|
1126
|
+
*/
|
|
1127
|
+
resume({
|
|
1128
|
+
stepId,
|
|
1129
|
+
runId,
|
|
1130
|
+
context
|
|
1131
|
+
}) {
|
|
1132
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/resume?runId=${runId}`, {
|
|
1133
|
+
method: "POST",
|
|
1134
|
+
body: {
|
|
1135
|
+
stepId,
|
|
1136
|
+
context
|
|
1137
|
+
}
|
|
1138
|
+
});
|
|
1139
|
+
}
|
|
1140
|
+
/**
|
|
1141
|
+
* Starts a workflow run asynchronously and returns a promise that resolves when the workflow is complete
|
|
1142
|
+
* @param params - Object containing the optional runId and triggerData
|
|
1143
|
+
* @returns Promise containing the workflow execution results
|
|
1144
|
+
*/
|
|
1145
|
+
startAsync(params) {
|
|
1146
|
+
const searchParams = new URLSearchParams();
|
|
1147
|
+
if (!!params?.runId) {
|
|
1148
|
+
searchParams.set("runId", params.runId);
|
|
1149
|
+
}
|
|
1150
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/start-async?${searchParams.toString()}`, {
|
|
1151
|
+
method: "POST",
|
|
1152
|
+
body: params?.triggerData
|
|
1153
|
+
});
|
|
1154
|
+
}
|
|
1155
|
+
/**
|
|
1156
|
+
* Resumes a suspended legacy workflow step asynchronously and returns a promise that resolves when the workflow is complete
|
|
1157
|
+
* @param params - Object containing the runId, stepId, and context
|
|
1158
|
+
* @returns Promise containing the workflow resume results
|
|
1159
|
+
*/
|
|
1160
|
+
resumeAsync(params) {
|
|
1161
|
+
return this.request(`/api/workflows/legacy/${this.workflowId}/resume-async?runId=${params.runId}`, {
|
|
1162
|
+
method: "POST",
|
|
1163
|
+
body: {
|
|
1164
|
+
stepId: params.stepId,
|
|
1165
|
+
context: params.context
|
|
1166
|
+
}
|
|
1167
|
+
});
|
|
1168
|
+
}
|
|
1169
|
+
/**
|
|
1170
|
+
* Creates an async generator that processes a readable stream and yields records
|
|
1171
|
+
* separated by the Record Separator character (\x1E)
|
|
1172
|
+
*
|
|
1173
|
+
* @param stream - The readable stream to process
|
|
1174
|
+
* @returns An async generator that yields parsed records
|
|
1175
|
+
*/
|
|
1176
|
+
async *streamProcessor(stream) {
|
|
1177
|
+
const reader = stream.getReader();
|
|
1178
|
+
let doneReading = false;
|
|
1179
|
+
let buffer = "";
|
|
1180
|
+
try {
|
|
1181
|
+
while (!doneReading) {
|
|
1182
|
+
const { done, value } = await reader.read();
|
|
1183
|
+
doneReading = done;
|
|
1184
|
+
if (done && !value) continue;
|
|
1185
|
+
try {
|
|
1186
|
+
const decoded = value ? new TextDecoder().decode(value) : "";
|
|
1187
|
+
const chunks = (buffer + decoded).split(RECORD_SEPARATOR);
|
|
1188
|
+
buffer = chunks.pop() || "";
|
|
1189
|
+
for (const chunk of chunks) {
|
|
1190
|
+
if (chunk) {
|
|
1191
|
+
if (typeof chunk === "string") {
|
|
1192
|
+
try {
|
|
1193
|
+
const parsedChunk = JSON.parse(chunk);
|
|
1194
|
+
yield parsedChunk;
|
|
1195
|
+
} catch {
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
} catch {
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
if (buffer) {
|
|
1204
|
+
try {
|
|
1205
|
+
yield JSON.parse(buffer);
|
|
1206
|
+
} catch {
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
} finally {
|
|
1210
|
+
reader.cancel().catch(() => {
|
|
1211
|
+
});
|
|
1212
|
+
}
|
|
1213
|
+
}
|
|
1214
|
+
/**
|
|
1215
|
+
* Watches legacy workflow transitions in real-time
|
|
1216
|
+
* @param runId - Optional run ID to filter the watch stream
|
|
1217
|
+
* @returns AsyncGenerator that yields parsed records from the legacy workflow watch stream
|
|
1218
|
+
*/
|
|
1219
|
+
async watch({ runId }, onRecord) {
|
|
1220
|
+
const response = await this.request(`/api/workflows/legacy/${this.workflowId}/watch?runId=${runId}`, {
|
|
1221
|
+
stream: true
|
|
1222
|
+
});
|
|
1223
|
+
if (!response.ok) {
|
|
1224
|
+
throw new Error(`Failed to watch legacy workflow: ${response.statusText}`);
|
|
1225
|
+
}
|
|
1226
|
+
if (!response.body) {
|
|
1227
|
+
throw new Error("Response body is null");
|
|
1228
|
+
}
|
|
1229
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
1230
|
+
onRecord(record);
|
|
1231
|
+
}
|
|
1232
|
+
}
|
|
1233
|
+
};
|
|
1234
|
+
|
|
1235
|
+
// src/resources/tool.ts
|
|
1236
|
+
var Tool2 = class extends BaseResource {
|
|
1237
|
+
constructor(options, toolId) {
|
|
1238
|
+
super(options);
|
|
1239
|
+
this.toolId = toolId;
|
|
1240
|
+
}
|
|
1241
|
+
/**
|
|
1242
|
+
* Retrieves details about the tool
|
|
1243
|
+
* @returns Promise containing tool details including description and schemas
|
|
1244
|
+
*/
|
|
1245
|
+
details() {
|
|
1246
|
+
return this.request(`/api/tools/${this.toolId}`);
|
|
1247
|
+
}
|
|
1248
|
+
/**
|
|
1249
|
+
* Executes the tool with the provided parameters
|
|
1250
|
+
* @param params - Parameters required for tool execution
|
|
1251
|
+
* @returns Promise containing the tool execution results
|
|
1252
|
+
*/
|
|
1253
|
+
execute(params) {
|
|
1254
|
+
const url = new URLSearchParams();
|
|
1255
|
+
if (params.runId) {
|
|
1256
|
+
url.set("runId", params.runId);
|
|
1257
|
+
}
|
|
1258
|
+
const body = {
|
|
1259
|
+
data: params.data,
|
|
1260
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1261
|
+
};
|
|
1262
|
+
return this.request(`/api/tools/${this.toolId}/execute?${url.toString()}`, {
|
|
1263
|
+
method: "POST",
|
|
1264
|
+
body
|
|
1265
|
+
});
|
|
1266
|
+
}
|
|
1267
|
+
};
|
|
1268
|
+
|
|
1269
|
+
// src/resources/workflow.ts
|
|
1270
|
+
var RECORD_SEPARATOR2 = "";
|
|
1271
|
+
var Workflow = class extends BaseResource {
|
|
1272
|
+
constructor(options, workflowId) {
|
|
1273
|
+
super(options);
|
|
1274
|
+
this.workflowId = workflowId;
|
|
1275
|
+
}
|
|
1276
|
+
/**
|
|
1277
|
+
* Creates an async generator that processes a readable stream and yields workflow records
|
|
1278
|
+
* separated by the Record Separator character (\x1E)
|
|
1279
|
+
*
|
|
1280
|
+
* @param stream - The readable stream to process
|
|
1281
|
+
* @returns An async generator that yields parsed records
|
|
1282
|
+
*/
|
|
1283
|
+
async *streamProcessor(stream) {
|
|
1284
|
+
const reader = stream.getReader();
|
|
1285
|
+
let doneReading = false;
|
|
1286
|
+
let buffer = "";
|
|
1287
|
+
try {
|
|
1288
|
+
while (!doneReading) {
|
|
1289
|
+
const { done, value } = await reader.read();
|
|
1290
|
+
doneReading = done;
|
|
1291
|
+
if (done && !value) continue;
|
|
1292
|
+
try {
|
|
1293
|
+
const decoded = value ? new TextDecoder().decode(value) : "";
|
|
1294
|
+
const chunks = (buffer + decoded).split(RECORD_SEPARATOR2);
|
|
1295
|
+
buffer = chunks.pop() || "";
|
|
1296
|
+
for (const chunk of chunks) {
|
|
1297
|
+
if (chunk) {
|
|
1298
|
+
if (typeof chunk === "string") {
|
|
1299
|
+
try {
|
|
1300
|
+
const parsedChunk = JSON.parse(chunk);
|
|
1301
|
+
yield parsedChunk;
|
|
1302
|
+
} catch {
|
|
1303
|
+
}
|
|
1304
|
+
}
|
|
1305
|
+
}
|
|
1306
|
+
}
|
|
1307
|
+
} catch {
|
|
1308
|
+
}
|
|
1309
|
+
}
|
|
1310
|
+
if (buffer) {
|
|
1311
|
+
try {
|
|
1312
|
+
yield JSON.parse(buffer);
|
|
1313
|
+
} catch {
|
|
1314
|
+
}
|
|
1315
|
+
}
|
|
1316
|
+
} finally {
|
|
1317
|
+
reader.cancel().catch(() => {
|
|
1318
|
+
});
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1321
|
+
/**
|
|
1322
|
+
* Retrieves details about the workflow
|
|
1323
|
+
* @returns Promise containing workflow details including steps and graphs
|
|
1324
|
+
*/
|
|
1325
|
+
details() {
|
|
1326
|
+
return this.request(`/api/workflows/${this.workflowId}`);
|
|
1327
|
+
}
|
|
1328
|
+
/**
|
|
1329
|
+
* Retrieves all runs for a workflow
|
|
1330
|
+
* @param params - Parameters for filtering runs
|
|
1331
|
+
* @returns Promise containing workflow runs array
|
|
1332
|
+
*/
|
|
1333
|
+
runs(params) {
|
|
1334
|
+
const searchParams = new URLSearchParams();
|
|
1335
|
+
if (params?.fromDate) {
|
|
1336
|
+
searchParams.set("fromDate", params.fromDate.toISOString());
|
|
1337
|
+
}
|
|
1338
|
+
if (params?.toDate) {
|
|
1339
|
+
searchParams.set("toDate", params.toDate.toISOString());
|
|
1340
|
+
}
|
|
1341
|
+
if (params?.limit !== null && params?.limit !== void 0 && !isNaN(Number(params?.limit))) {
|
|
1342
|
+
searchParams.set("limit", String(params.limit));
|
|
1343
|
+
}
|
|
1344
|
+
if (params?.offset !== null && params?.offset !== void 0 && !isNaN(Number(params?.offset))) {
|
|
1345
|
+
searchParams.set("offset", String(params.offset));
|
|
1346
|
+
}
|
|
1347
|
+
if (params?.resourceId) {
|
|
1348
|
+
searchParams.set("resourceId", params.resourceId);
|
|
1349
|
+
}
|
|
1350
|
+
if (searchParams.size) {
|
|
1351
|
+
return this.request(`/api/workflows/${this.workflowId}/runs?${searchParams}`);
|
|
1352
|
+
} else {
|
|
1353
|
+
return this.request(`/api/workflows/${this.workflowId}/runs`);
|
|
1354
|
+
}
|
|
1355
|
+
}
|
|
1356
|
+
/**
|
|
1357
|
+
* Retrieves a specific workflow run by its ID
|
|
1358
|
+
* @param runId - The ID of the workflow run to retrieve
|
|
1359
|
+
* @returns Promise containing the workflow run details
|
|
1360
|
+
*/
|
|
1361
|
+
runById(runId) {
|
|
1362
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}`);
|
|
1363
|
+
}
|
|
1364
|
+
/**
|
|
1365
|
+
* Retrieves the execution result for a specific workflow run by its ID
|
|
1366
|
+
* @param runId - The ID of the workflow run to retrieve the execution result for
|
|
1367
|
+
* @returns Promise containing the workflow run execution result
|
|
1368
|
+
*/
|
|
1369
|
+
runExecutionResult(runId) {
|
|
1370
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}/execution-result`);
|
|
1371
|
+
}
|
|
1372
|
+
/**
|
|
1373
|
+
* Cancels a specific workflow run by its ID
|
|
1374
|
+
* @param runId - The ID of the workflow run to cancel
|
|
1375
|
+
* @returns Promise containing a success message
|
|
1376
|
+
*/
|
|
1377
|
+
cancelRun(runId) {
|
|
1378
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}/cancel`, {
|
|
1379
|
+
method: "POST"
|
|
1380
|
+
});
|
|
1381
|
+
}
|
|
1382
|
+
/**
|
|
1383
|
+
* Sends an event to a specific workflow run by its ID
|
|
1384
|
+
* @param params - Object containing the runId, event and data
|
|
1385
|
+
* @returns Promise containing a success message
|
|
1386
|
+
*/
|
|
1387
|
+
sendRunEvent(params) {
|
|
1388
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${params.runId}/send-event`, {
|
|
1389
|
+
method: "POST",
|
|
1390
|
+
body: { event: params.event, data: params.data }
|
|
1391
|
+
});
|
|
1392
|
+
}
|
|
1393
|
+
/**
|
|
1394
|
+
* Creates a new workflow run
|
|
1395
|
+
* @param params - Optional object containing the optional runId
|
|
1396
|
+
* @returns Promise containing the runId of the created run
|
|
1397
|
+
*/
|
|
1398
|
+
createRun(params) {
|
|
1399
|
+
const searchParams = new URLSearchParams();
|
|
1400
|
+
if (!!params?.runId) {
|
|
1401
|
+
searchParams.set("runId", params.runId);
|
|
1402
|
+
}
|
|
1403
|
+
return this.request(`/api/workflows/${this.workflowId}/create-run?${searchParams.toString()}`, {
|
|
1404
|
+
method: "POST"
|
|
1405
|
+
});
|
|
1406
|
+
}
|
|
1407
|
+
/**
|
|
1408
|
+
* Starts a workflow run synchronously without waiting for the workflow to complete
|
|
1409
|
+
* @param params - Object containing the runId, inputData and runtimeContext
|
|
1410
|
+
* @returns Promise containing success message
|
|
1411
|
+
*/
|
|
1412
|
+
start(params) {
|
|
1413
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1414
|
+
return this.request(`/api/workflows/${this.workflowId}/start?runId=${params.runId}`, {
|
|
1415
|
+
method: "POST",
|
|
1416
|
+
body: { inputData: params?.inputData, runtimeContext }
|
|
1417
|
+
});
|
|
1418
|
+
}
|
|
1419
|
+
/**
|
|
1420
|
+
* Resumes a suspended workflow step synchronously without waiting for the workflow to complete
|
|
1421
|
+
* @param params - Object containing the runId, step, resumeData and runtimeContext
|
|
1422
|
+
* @returns Promise containing success message
|
|
1423
|
+
*/
|
|
1424
|
+
resume({
|
|
1425
|
+
step,
|
|
1426
|
+
runId,
|
|
1427
|
+
resumeData,
|
|
1428
|
+
...rest
|
|
1429
|
+
}) {
|
|
1430
|
+
const runtimeContext = parseClientRuntimeContext(rest.runtimeContext);
|
|
1431
|
+
return this.request(`/api/workflows/${this.workflowId}/resume?runId=${runId}`, {
|
|
1432
|
+
method: "POST",
|
|
1433
|
+
stream: true,
|
|
1434
|
+
body: {
|
|
1435
|
+
step,
|
|
1436
|
+
resumeData,
|
|
1437
|
+
runtimeContext
|
|
1438
|
+
}
|
|
1439
|
+
});
|
|
1440
|
+
}
|
|
1441
|
+
/**
|
|
1442
|
+
* Starts a workflow run asynchronously and returns a promise that resolves when the workflow is complete
|
|
1443
|
+
* @param params - Object containing the optional runId, inputData and runtimeContext
|
|
1444
|
+
* @returns Promise containing the workflow execution results
|
|
1445
|
+
*/
|
|
1446
|
+
startAsync(params) {
|
|
1447
|
+
const searchParams = new URLSearchParams();
|
|
1448
|
+
if (!!params?.runId) {
|
|
1449
|
+
searchParams.set("runId", params.runId);
|
|
1450
|
+
}
|
|
1451
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1452
|
+
return this.request(`/api/workflows/${this.workflowId}/start-async?${searchParams.toString()}`, {
|
|
1453
|
+
method: "POST",
|
|
1454
|
+
body: { inputData: params.inputData, runtimeContext }
|
|
1455
|
+
});
|
|
1456
|
+
}
|
|
1457
|
+
/**
|
|
1458
|
+
* Starts a workflow run and returns a stream
|
|
1459
|
+
* @param params - Object containing the optional runId, inputData and runtimeContext
|
|
1460
|
+
* @returns Promise containing the workflow execution results
|
|
1461
|
+
*/
|
|
1462
|
+
async stream(params) {
|
|
1463
|
+
const searchParams = new URLSearchParams();
|
|
1464
|
+
if (!!params?.runId) {
|
|
1465
|
+
searchParams.set("runId", params.runId);
|
|
1466
|
+
}
|
|
1467
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1468
|
+
const response = await this.request(
|
|
1469
|
+
`/api/workflows/${this.workflowId}/stream?${searchParams.toString()}`,
|
|
1470
|
+
{
|
|
1471
|
+
method: "POST",
|
|
1472
|
+
body: { inputData: params.inputData, runtimeContext },
|
|
1473
|
+
stream: true
|
|
1474
|
+
}
|
|
1475
|
+
);
|
|
1476
|
+
if (!response.ok) {
|
|
1477
|
+
throw new Error(`Failed to stream vNext workflow: ${response.statusText}`);
|
|
1478
|
+
}
|
|
1479
|
+
if (!response.body) {
|
|
1480
|
+
throw new Error("Response body is null");
|
|
1481
|
+
}
|
|
1482
|
+
const transformStream = new TransformStream({
|
|
1483
|
+
start() {
|
|
1484
|
+
},
|
|
1485
|
+
async transform(chunk, controller) {
|
|
1486
|
+
try {
|
|
1487
|
+
const decoded = new TextDecoder().decode(chunk);
|
|
1488
|
+
const chunks = decoded.split(RECORD_SEPARATOR2);
|
|
1489
|
+
for (const chunk2 of chunks) {
|
|
1490
|
+
if (chunk2) {
|
|
1491
|
+
try {
|
|
1492
|
+
const parsedChunk = JSON.parse(chunk2);
|
|
1493
|
+
controller.enqueue(parsedChunk);
|
|
1494
|
+
} catch {
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
}
|
|
1498
|
+
} catch {
|
|
1499
|
+
}
|
|
1500
|
+
}
|
|
1501
|
+
});
|
|
1502
|
+
return response.body.pipeThrough(transformStream);
|
|
1503
|
+
}
|
|
1504
|
+
/**
|
|
1505
|
+
* Resumes a suspended workflow step asynchronously and returns a promise that resolves when the workflow is complete
|
|
1506
|
+
* @param params - Object containing the runId, step, resumeData and runtimeContext
|
|
1507
|
+
* @returns Promise containing the workflow resume results
|
|
1508
|
+
*/
|
|
1509
|
+
resumeAsync(params) {
|
|
1510
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1511
|
+
return this.request(`/api/workflows/${this.workflowId}/resume-async?runId=${params.runId}`, {
|
|
1512
|
+
method: "POST",
|
|
1513
|
+
body: {
|
|
1514
|
+
step: params.step,
|
|
1515
|
+
resumeData: params.resumeData,
|
|
1516
|
+
runtimeContext
|
|
1517
|
+
}
|
|
1518
|
+
});
|
|
1519
|
+
}
|
|
1520
|
+
/**
|
|
1521
|
+
* Watches workflow transitions in real-time
|
|
1522
|
+
* @param runId - Optional run ID to filter the watch stream
|
|
1523
|
+
* @returns AsyncGenerator that yields parsed records from the workflow watch stream
|
|
1524
|
+
*/
|
|
1525
|
+
async watch({ runId }, onRecord) {
|
|
1526
|
+
const response = await this.request(`/api/workflows/${this.workflowId}/watch?runId=${runId}`, {
|
|
1527
|
+
stream: true
|
|
1528
|
+
});
|
|
1529
|
+
if (!response.ok) {
|
|
1530
|
+
throw new Error(`Failed to watch workflow: ${response.statusText}`);
|
|
1531
|
+
}
|
|
1532
|
+
if (!response.body) {
|
|
1533
|
+
throw new Error("Response body is null");
|
|
1534
|
+
}
|
|
1535
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
1536
|
+
if (typeof record === "string") {
|
|
1537
|
+
onRecord(JSON.parse(record));
|
|
1538
|
+
} else {
|
|
1539
|
+
onRecord(record);
|
|
1540
|
+
}
|
|
1541
|
+
}
|
|
1542
|
+
}
|
|
1543
|
+
/**
|
|
1544
|
+
* Creates a new ReadableStream from an iterable or async iterable of objects,
|
|
1545
|
+
* serializing each as JSON and separating them with the record separator (\x1E).
|
|
1546
|
+
*
|
|
1547
|
+
* @param records - An iterable or async iterable of objects to stream
|
|
1548
|
+
* @returns A ReadableStream emitting the records as JSON strings separated by the record separator
|
|
1549
|
+
*/
|
|
1550
|
+
static createRecordStream(records) {
|
|
1551
|
+
const encoder = new TextEncoder();
|
|
1552
|
+
return new ReadableStream({
|
|
1553
|
+
async start(controller) {
|
|
1554
|
+
try {
|
|
1555
|
+
for await (const record of records) {
|
|
1556
|
+
const json = JSON.stringify(record) + RECORD_SEPARATOR2;
|
|
1557
|
+
controller.enqueue(encoder.encode(json));
|
|
1558
|
+
}
|
|
1559
|
+
controller.close();
|
|
1560
|
+
} catch (err) {
|
|
1561
|
+
controller.error(err);
|
|
1562
|
+
}
|
|
1563
|
+
}
|
|
1564
|
+
});
|
|
1565
|
+
}
|
|
1566
|
+
};
|
|
1567
|
+
|
|
1568
|
+
// src/resources/a2a.ts
|
|
1569
|
+
var A2A = class extends BaseResource {
|
|
1570
|
+
constructor(options, agentId) {
|
|
1571
|
+
super(options);
|
|
1572
|
+
this.agentId = agentId;
|
|
1573
|
+
}
|
|
1574
|
+
/**
|
|
1575
|
+
* Get the agent card with metadata about the agent
|
|
1576
|
+
* @returns Promise containing the agent card information
|
|
1577
|
+
*/
|
|
1578
|
+
async getCard() {
|
|
1579
|
+
return this.request(`/.well-known/${this.agentId}/agent.json`);
|
|
1580
|
+
}
|
|
1581
|
+
/**
|
|
1582
|
+
* Send a message to the agent and get a response
|
|
1583
|
+
* @param params - Parameters for the task
|
|
1584
|
+
* @returns Promise containing the task response
|
|
1585
|
+
*/
|
|
1586
|
+
async sendMessage(params) {
|
|
1587
|
+
const response = await this.request(`/a2a/${this.agentId}`, {
|
|
1588
|
+
method: "POST",
|
|
1589
|
+
body: {
|
|
1590
|
+
method: "tasks/send",
|
|
1591
|
+
params
|
|
1592
|
+
}
|
|
1593
|
+
});
|
|
1594
|
+
return { task: response.result };
|
|
1595
|
+
}
|
|
1596
|
+
/**
|
|
1597
|
+
* Get the status and result of a task
|
|
1598
|
+
* @param params - Parameters for querying the task
|
|
1599
|
+
* @returns Promise containing the task response
|
|
1600
|
+
*/
|
|
1601
|
+
async getTask(params) {
|
|
1602
|
+
const response = await this.request(`/a2a/${this.agentId}`, {
|
|
1603
|
+
method: "POST",
|
|
1604
|
+
body: {
|
|
1605
|
+
method: "tasks/get",
|
|
1606
|
+
params
|
|
1607
|
+
}
|
|
1608
|
+
});
|
|
1609
|
+
return response.result;
|
|
1610
|
+
}
|
|
1611
|
+
/**
|
|
1612
|
+
* Cancel a running task
|
|
1613
|
+
* @param params - Parameters identifying the task to cancel
|
|
1614
|
+
* @returns Promise containing the task response
|
|
1615
|
+
*/
|
|
1616
|
+
async cancelTask(params) {
|
|
1617
|
+
return this.request(`/a2a/${this.agentId}`, {
|
|
1618
|
+
method: "POST",
|
|
1619
|
+
body: {
|
|
1620
|
+
method: "tasks/cancel",
|
|
1621
|
+
params
|
|
1622
|
+
}
|
|
1623
|
+
});
|
|
1624
|
+
}
|
|
1625
|
+
/**
|
|
1626
|
+
* Send a message and subscribe to streaming updates (not fully implemented)
|
|
1627
|
+
* @param params - Parameters for the task
|
|
1628
|
+
* @returns Promise containing the task response
|
|
1629
|
+
*/
|
|
1630
|
+
async sendAndSubscribe(params) {
|
|
1631
|
+
return this.request(`/a2a/${this.agentId}`, {
|
|
1632
|
+
method: "POST",
|
|
1633
|
+
body: {
|
|
1634
|
+
method: "tasks/sendSubscribe",
|
|
1635
|
+
params
|
|
1636
|
+
},
|
|
1637
|
+
stream: true
|
|
1638
|
+
});
|
|
1639
|
+
}
|
|
1640
|
+
};
|
|
1641
|
+
|
|
1642
|
+
// src/resources/mcp-tool.ts
|
|
1643
|
+
var MCPTool = class extends BaseResource {
|
|
1644
|
+
serverId;
|
|
1645
|
+
toolId;
|
|
1646
|
+
constructor(options, serverId, toolId) {
|
|
1647
|
+
super(options);
|
|
1648
|
+
this.serverId = serverId;
|
|
1649
|
+
this.toolId = toolId;
|
|
1650
|
+
}
|
|
1651
|
+
/**
|
|
1652
|
+
* Retrieves details about this specific tool from the MCP server.
|
|
1653
|
+
* @returns Promise containing the tool's information (name, description, schema).
|
|
1654
|
+
*/
|
|
1655
|
+
details() {
|
|
1656
|
+
return this.request(`/api/mcp/${this.serverId}/tools/${this.toolId}`);
|
|
1657
|
+
}
|
|
1658
|
+
/**
|
|
1659
|
+
* Executes this specific tool on the MCP server.
|
|
1660
|
+
* @param params - Parameters for tool execution, including data/args and optional runtimeContext.
|
|
1661
|
+
* @returns Promise containing the result of the tool execution.
|
|
1662
|
+
*/
|
|
1663
|
+
execute(params) {
|
|
1664
|
+
const body = {};
|
|
1665
|
+
if (params.data !== void 0) body.data = params.data;
|
|
1666
|
+
if (params.runtimeContext !== void 0) {
|
|
1667
|
+
body.runtimeContext = params.runtimeContext;
|
|
1668
|
+
}
|
|
1669
|
+
return this.request(`/api/mcp/${this.serverId}/tools/${this.toolId}/execute`, {
|
|
1670
|
+
method: "POST",
|
|
1671
|
+
body: Object.keys(body).length > 0 ? body : void 0
|
|
1672
|
+
});
|
|
1673
|
+
}
|
|
1674
|
+
};
|
|
1675
|
+
|
|
1676
|
+
// src/resources/vNextNetwork.ts
|
|
1677
|
+
var RECORD_SEPARATOR3 = "";
|
|
1678
|
+
var VNextNetwork = class extends BaseResource {
|
|
1679
|
+
constructor(options, networkId) {
|
|
1680
|
+
super(options);
|
|
1681
|
+
this.networkId = networkId;
|
|
1682
|
+
}
|
|
1683
|
+
/**
|
|
1684
|
+
* Retrieves details about the network
|
|
1685
|
+
* @returns Promise containing vNext network details
|
|
1686
|
+
*/
|
|
1687
|
+
details() {
|
|
1688
|
+
return this.request(`/api/networks/v-next/${this.networkId}`);
|
|
1689
|
+
}
|
|
1690
|
+
/**
|
|
1691
|
+
* Generates a response from the v-next network
|
|
1692
|
+
* @param params - Generation parameters including message
|
|
1693
|
+
* @returns Promise containing the generated response
|
|
1694
|
+
*/
|
|
1695
|
+
generate(params) {
|
|
1696
|
+
return this.request(`/api/networks/v-next/${this.networkId}/generate`, {
|
|
1697
|
+
method: "POST",
|
|
1698
|
+
body: params
|
|
1699
|
+
});
|
|
1700
|
+
}
|
|
1701
|
+
/**
|
|
1702
|
+
* Generates a response from the v-next network using multiple primitives
|
|
1703
|
+
* @param params - Generation parameters including message
|
|
1704
|
+
* @returns Promise containing the generated response
|
|
1705
|
+
*/
|
|
1706
|
+
loop(params) {
|
|
1707
|
+
return this.request(`/api/networks/v-next/${this.networkId}/loop`, {
|
|
1708
|
+
method: "POST",
|
|
1709
|
+
body: params
|
|
1710
|
+
});
|
|
1711
|
+
}
|
|
1712
|
+
async *streamProcessor(stream) {
|
|
1713
|
+
const reader = stream.getReader();
|
|
1714
|
+
let doneReading = false;
|
|
1715
|
+
let buffer = "";
|
|
1716
|
+
try {
|
|
1717
|
+
while (!doneReading) {
|
|
1718
|
+
const { done, value } = await reader.read();
|
|
1719
|
+
doneReading = done;
|
|
1720
|
+
if (done && !value) continue;
|
|
1721
|
+
try {
|
|
1722
|
+
const decoded = value ? new TextDecoder().decode(value) : "";
|
|
1723
|
+
const chunks = (buffer + decoded).split(RECORD_SEPARATOR3);
|
|
1724
|
+
buffer = chunks.pop() || "";
|
|
1725
|
+
for (const chunk of chunks) {
|
|
1726
|
+
if (chunk) {
|
|
1727
|
+
if (typeof chunk === "string") {
|
|
1728
|
+
try {
|
|
1729
|
+
const parsedChunk = JSON.parse(chunk);
|
|
1730
|
+
yield parsedChunk;
|
|
1731
|
+
} catch {
|
|
1732
|
+
}
|
|
1733
|
+
}
|
|
1734
|
+
}
|
|
1735
|
+
}
|
|
1736
|
+
} catch {
|
|
1737
|
+
}
|
|
1738
|
+
}
|
|
1739
|
+
if (buffer) {
|
|
1740
|
+
try {
|
|
1741
|
+
yield JSON.parse(buffer);
|
|
1742
|
+
} catch {
|
|
1743
|
+
}
|
|
1744
|
+
}
|
|
1745
|
+
} finally {
|
|
1746
|
+
reader.cancel().catch(() => {
|
|
1747
|
+
});
|
|
1748
|
+
}
|
|
1749
|
+
}
|
|
1750
|
+
/**
|
|
1751
|
+
* Streams a response from the v-next network
|
|
1752
|
+
* @param params - Stream parameters including message
|
|
1753
|
+
* @returns Promise containing the results
|
|
1754
|
+
*/
|
|
1755
|
+
async stream(params, onRecord) {
|
|
1756
|
+
const response = await this.request(`/api/networks/v-next/${this.networkId}/stream`, {
|
|
1757
|
+
method: "POST",
|
|
1758
|
+
body: params,
|
|
1759
|
+
stream: true
|
|
1760
|
+
});
|
|
1761
|
+
if (!response.ok) {
|
|
1762
|
+
throw new Error(`Failed to stream vNext network: ${response.statusText}`);
|
|
1763
|
+
}
|
|
1764
|
+
if (!response.body) {
|
|
1765
|
+
throw new Error("Response body is null");
|
|
1766
|
+
}
|
|
1767
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
1768
|
+
if (typeof record === "string") {
|
|
1769
|
+
onRecord(JSON.parse(record));
|
|
1770
|
+
} else {
|
|
1771
|
+
onRecord(record);
|
|
1772
|
+
}
|
|
1773
|
+
}
|
|
1774
|
+
}
|
|
1775
|
+
/**
|
|
1776
|
+
* Streams a response from the v-next network loop
|
|
1777
|
+
* @param params - Stream parameters including message
|
|
1778
|
+
* @returns Promise containing the results
|
|
1779
|
+
*/
|
|
1780
|
+
async loopStream(params, onRecord) {
|
|
1781
|
+
const response = await this.request(`/api/networks/v-next/${this.networkId}/loop-stream`, {
|
|
1782
|
+
method: "POST",
|
|
1783
|
+
body: params,
|
|
1784
|
+
stream: true
|
|
1785
|
+
});
|
|
1786
|
+
if (!response.ok) {
|
|
1787
|
+
throw new Error(`Failed to stream vNext network loop: ${response.statusText}`);
|
|
1788
|
+
}
|
|
1789
|
+
if (!response.body) {
|
|
1790
|
+
throw new Error("Response body is null");
|
|
1791
|
+
}
|
|
1792
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
1793
|
+
if (typeof record === "string") {
|
|
1794
|
+
onRecord(JSON.parse(record));
|
|
1795
|
+
} else {
|
|
1796
|
+
onRecord(record);
|
|
1797
|
+
}
|
|
1798
|
+
}
|
|
1799
|
+
}
|
|
1800
|
+
};
|
|
1801
|
+
|
|
1802
|
+
// src/resources/network-memory-thread.ts
|
|
1803
|
+
var NetworkMemoryThread = class extends BaseResource {
|
|
1804
|
+
constructor(options, threadId, networkId) {
|
|
1805
|
+
super(options);
|
|
1806
|
+
this.threadId = threadId;
|
|
1807
|
+
this.networkId = networkId;
|
|
1808
|
+
}
|
|
1809
|
+
/**
|
|
1810
|
+
* Retrieves the memory thread details
|
|
1811
|
+
* @returns Promise containing thread details including title and metadata
|
|
1812
|
+
*/
|
|
1813
|
+
get() {
|
|
1814
|
+
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`);
|
|
1815
|
+
}
|
|
1816
|
+
/**
|
|
1817
|
+
* Updates the memory thread properties
|
|
1818
|
+
* @param params - Update parameters including title and metadata
|
|
1819
|
+
* @returns Promise containing updated thread details
|
|
1820
|
+
*/
|
|
1821
|
+
update(params) {
|
|
1822
|
+
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
|
|
1823
|
+
method: "PATCH",
|
|
1824
|
+
body: params
|
|
1825
|
+
});
|
|
1826
|
+
}
|
|
1827
|
+
/**
|
|
1828
|
+
* Deletes the memory thread
|
|
1829
|
+
* @returns Promise containing deletion result
|
|
1830
|
+
*/
|
|
1831
|
+
delete() {
|
|
1832
|
+
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
|
|
1833
|
+
method: "DELETE"
|
|
1834
|
+
});
|
|
1835
|
+
}
|
|
1836
|
+
/**
|
|
1837
|
+
* Retrieves messages associated with the thread
|
|
1838
|
+
* @param params - Optional parameters including limit for number of messages to retrieve
|
|
1839
|
+
* @returns Promise containing thread messages and UI messages
|
|
1840
|
+
*/
|
|
1841
|
+
getMessages(params) {
|
|
1842
|
+
const query = new URLSearchParams({
|
|
1843
|
+
networkId: this.networkId,
|
|
1844
|
+
...params?.limit ? { limit: params.limit.toString() } : {}
|
|
1845
|
+
});
|
|
1846
|
+
return this.request(`/api/memory/network/threads/${this.threadId}/messages?${query.toString()}`);
|
|
1847
|
+
}
|
|
1848
|
+
};
|
|
1849
|
+
|
|
1850
|
+
// src/client.ts
|
|
1851
|
+
var MastraClient = class extends BaseResource {
|
|
1852
|
+
constructor(options) {
|
|
1853
|
+
super(options);
|
|
1854
|
+
}
|
|
1855
|
+
/**
|
|
1856
|
+
* Retrieves all available agents
|
|
1857
|
+
* @returns Promise containing map of agent IDs to agent details
|
|
1858
|
+
*/
|
|
1859
|
+
getAgents() {
|
|
1860
|
+
return this.request("/api/agents");
|
|
1861
|
+
}
|
|
1862
|
+
async getAGUI({ resourceId }) {
|
|
1863
|
+
const agents = await this.getAgents();
|
|
1864
|
+
return Object.entries(agents).reduce(
|
|
1865
|
+
(acc, [agentId]) => {
|
|
1866
|
+
const agent = this.getAgent(agentId);
|
|
1867
|
+
acc[agentId] = new AGUIAdapter({
|
|
1868
|
+
agentId,
|
|
1869
|
+
agent,
|
|
1870
|
+
resourceId
|
|
1871
|
+
});
|
|
1872
|
+
return acc;
|
|
1873
|
+
},
|
|
1874
|
+
{}
|
|
1875
|
+
);
|
|
1876
|
+
}
|
|
1877
|
+
/**
|
|
1878
|
+
* Gets an agent instance by ID
|
|
1879
|
+
* @param agentId - ID of the agent to retrieve
|
|
1880
|
+
* @returns Agent instance
|
|
1881
|
+
*/
|
|
1882
|
+
getAgent(agentId) {
|
|
1883
|
+
return new Agent(this.options, agentId);
|
|
1884
|
+
}
|
|
1885
|
+
/**
|
|
1886
|
+
* Retrieves memory threads for a resource
|
|
1887
|
+
* @param params - Parameters containing the resource ID
|
|
1888
|
+
* @returns Promise containing array of memory threads
|
|
1889
|
+
*/
|
|
1890
|
+
getMemoryThreads(params) {
|
|
1891
|
+
return this.request(`/api/memory/threads?resourceid=${params.resourceId}&agentId=${params.agentId}`);
|
|
1892
|
+
}
|
|
1893
|
+
/**
|
|
1894
|
+
* Creates a new memory thread
|
|
1895
|
+
* @param params - Parameters for creating the memory thread
|
|
1896
|
+
* @returns Promise containing the created memory thread
|
|
1897
|
+
*/
|
|
1898
|
+
createMemoryThread(params) {
|
|
1899
|
+
return this.request(`/api/memory/threads?agentId=${params.agentId}`, { method: "POST", body: params });
|
|
1900
|
+
}
|
|
1901
|
+
/**
|
|
1902
|
+
* Gets a memory thread instance by ID
|
|
1903
|
+
* @param threadId - ID of the memory thread to retrieve
|
|
1904
|
+
* @returns MemoryThread instance
|
|
1905
|
+
*/
|
|
1906
|
+
getMemoryThread(threadId, agentId) {
|
|
1907
|
+
return new MemoryThread(this.options, threadId, agentId);
|
|
1908
|
+
}
|
|
1909
|
+
/**
|
|
1910
|
+
* Saves messages to memory
|
|
1911
|
+
* @param params - Parameters containing messages to save
|
|
1912
|
+
* @returns Promise containing the saved messages
|
|
1913
|
+
*/
|
|
1914
|
+
saveMessageToMemory(params) {
|
|
1915
|
+
return this.request(`/api/memory/save-messages?agentId=${params.agentId}`, {
|
|
1916
|
+
method: "POST",
|
|
1917
|
+
body: params
|
|
1918
|
+
});
|
|
1919
|
+
}
|
|
1920
|
+
/**
|
|
1921
|
+
* Gets the status of the memory system
|
|
1922
|
+
* @returns Promise containing memory system status
|
|
1923
|
+
*/
|
|
1924
|
+
getMemoryStatus(agentId) {
|
|
1925
|
+
return this.request(`/api/memory/status?agentId=${agentId}`);
|
|
1926
|
+
}
|
|
1927
|
+
/**
|
|
1928
|
+
* Retrieves memory threads for a resource
|
|
1929
|
+
* @param params - Parameters containing the resource ID
|
|
1930
|
+
* @returns Promise containing array of memory threads
|
|
1931
|
+
*/
|
|
1932
|
+
getNetworkMemoryThreads(params) {
|
|
1933
|
+
return this.request(`/api/memory/network/threads?resourceid=${params.resourceId}&networkId=${params.networkId}`);
|
|
1934
|
+
}
|
|
1935
|
+
/**
|
|
1936
|
+
* Creates a new memory thread
|
|
1937
|
+
* @param params - Parameters for creating the memory thread
|
|
1938
|
+
* @returns Promise containing the created memory thread
|
|
1939
|
+
*/
|
|
1940
|
+
createNetworkMemoryThread(params) {
|
|
1941
|
+
return this.request(`/api/memory/network/threads?networkId=${params.networkId}`, { method: "POST", body: params });
|
|
1942
|
+
}
|
|
1943
|
+
/**
|
|
1944
|
+
* Gets a memory thread instance by ID
|
|
1945
|
+
* @param threadId - ID of the memory thread to retrieve
|
|
1946
|
+
* @returns MemoryThread instance
|
|
1947
|
+
*/
|
|
1948
|
+
getNetworkMemoryThread(threadId, networkId) {
|
|
1949
|
+
return new NetworkMemoryThread(this.options, threadId, networkId);
|
|
1950
|
+
}
|
|
1951
|
+
/**
|
|
1952
|
+
* Saves messages to memory
|
|
1953
|
+
* @param params - Parameters containing messages to save
|
|
1954
|
+
* @returns Promise containing the saved messages
|
|
1955
|
+
*/
|
|
1956
|
+
saveNetworkMessageToMemory(params) {
|
|
1957
|
+
return this.request(`/api/memory/network/save-messages?networkId=${params.networkId}`, {
|
|
1958
|
+
method: "POST",
|
|
1959
|
+
body: params
|
|
1960
|
+
});
|
|
1961
|
+
}
|
|
1962
|
+
/**
|
|
1963
|
+
* Gets the status of the memory system
|
|
1964
|
+
* @returns Promise containing memory system status
|
|
1965
|
+
*/
|
|
1966
|
+
getNetworkMemoryStatus(networkId) {
|
|
1967
|
+
return this.request(`/api/memory/network/status?networkId=${networkId}`);
|
|
1968
|
+
}
|
|
1969
|
+
/**
|
|
1970
|
+
* Retrieves all available tools
|
|
1971
|
+
* @returns Promise containing map of tool IDs to tool details
|
|
1972
|
+
*/
|
|
1973
|
+
getTools() {
|
|
1974
|
+
return this.request("/api/tools");
|
|
1975
|
+
}
|
|
1976
|
+
/**
|
|
1977
|
+
* Gets a tool instance by ID
|
|
1978
|
+
* @param toolId - ID of the tool to retrieve
|
|
1979
|
+
* @returns Tool instance
|
|
1980
|
+
*/
|
|
1981
|
+
getTool(toolId) {
|
|
1982
|
+
return new Tool2(this.options, toolId);
|
|
1983
|
+
}
|
|
1984
|
+
/**
|
|
1985
|
+
* Retrieves all available legacy workflows
|
|
1986
|
+
* @returns Promise containing map of legacy workflow IDs to legacy workflow details
|
|
1987
|
+
*/
|
|
1988
|
+
getLegacyWorkflows() {
|
|
1989
|
+
return this.request("/api/workflows/legacy");
|
|
1990
|
+
}
|
|
1991
|
+
/**
|
|
1992
|
+
* Gets a legacy workflow instance by ID
|
|
1993
|
+
* @param workflowId - ID of the legacy workflow to retrieve
|
|
1994
|
+
* @returns Legacy Workflow instance
|
|
1995
|
+
*/
|
|
1996
|
+
getLegacyWorkflow(workflowId) {
|
|
1997
|
+
return new LegacyWorkflow(this.options, workflowId);
|
|
1998
|
+
}
|
|
1999
|
+
/**
|
|
2000
|
+
* Retrieves all available workflows
|
|
2001
|
+
* @returns Promise containing map of workflow IDs to workflow details
|
|
2002
|
+
*/
|
|
2003
|
+
getWorkflows() {
|
|
2004
|
+
return this.request("/api/workflows");
|
|
2005
|
+
}
|
|
2006
|
+
/**
|
|
2007
|
+
* Gets a workflow instance by ID
|
|
2008
|
+
* @param workflowId - ID of the workflow to retrieve
|
|
2009
|
+
* @returns Workflow instance
|
|
2010
|
+
*/
|
|
2011
|
+
getWorkflow(workflowId) {
|
|
2012
|
+
return new Workflow(this.options, workflowId);
|
|
2013
|
+
}
|
|
2014
|
+
/**
|
|
2015
|
+
* Gets a vector instance by name
|
|
2016
|
+
* @param vectorName - Name of the vector to retrieve
|
|
2017
|
+
* @returns Vector instance
|
|
2018
|
+
*/
|
|
2019
|
+
getVector(vectorName) {
|
|
2020
|
+
return new Vector(this.options, vectorName);
|
|
2021
|
+
}
|
|
2022
|
+
/**
|
|
2023
|
+
* Retrieves logs
|
|
2024
|
+
* @param params - Parameters for filtering logs
|
|
2025
|
+
* @returns Promise containing array of log messages
|
|
2026
|
+
*/
|
|
2027
|
+
getLogs(params) {
|
|
2028
|
+
const { transportId, fromDate, toDate, logLevel, filters, page, perPage } = params;
|
|
2029
|
+
const _filters = filters ? Object.entries(filters).map(([key, value]) => `${key}:${value}`) : [];
|
|
2030
|
+
const searchParams = new URLSearchParams();
|
|
2031
|
+
if (transportId) {
|
|
2032
|
+
searchParams.set("transportId", transportId);
|
|
2033
|
+
}
|
|
2034
|
+
if (fromDate) {
|
|
2035
|
+
searchParams.set("fromDate", fromDate.toISOString());
|
|
2036
|
+
}
|
|
2037
|
+
if (toDate) {
|
|
2038
|
+
searchParams.set("toDate", toDate.toISOString());
|
|
2039
|
+
}
|
|
2040
|
+
if (logLevel) {
|
|
2041
|
+
searchParams.set("logLevel", logLevel);
|
|
2042
|
+
}
|
|
2043
|
+
if (page) {
|
|
2044
|
+
searchParams.set("page", String(page));
|
|
2045
|
+
}
|
|
2046
|
+
if (perPage) {
|
|
2047
|
+
searchParams.set("perPage", String(perPage));
|
|
2048
|
+
}
|
|
2049
|
+
if (_filters) {
|
|
2050
|
+
if (Array.isArray(_filters)) {
|
|
2051
|
+
for (const filter of _filters) {
|
|
2052
|
+
searchParams.append("filters", filter);
|
|
2053
|
+
}
|
|
2054
|
+
} else {
|
|
2055
|
+
searchParams.set("filters", _filters);
|
|
2056
|
+
}
|
|
2057
|
+
}
|
|
2058
|
+
if (searchParams.size) {
|
|
2059
|
+
return this.request(`/api/logs?${searchParams}`);
|
|
2060
|
+
} else {
|
|
2061
|
+
return this.request(`/api/logs`);
|
|
2062
|
+
}
|
|
2063
|
+
}
|
|
2064
|
+
/**
|
|
2065
|
+
* Gets logs for a specific run
|
|
2066
|
+
* @param params - Parameters containing run ID to retrieve
|
|
2067
|
+
* @returns Promise containing array of log messages
|
|
2068
|
+
*/
|
|
2069
|
+
getLogForRun(params) {
|
|
2070
|
+
const { runId, transportId, fromDate, toDate, logLevel, filters, page, perPage } = params;
|
|
2071
|
+
const _filters = filters ? Object.entries(filters).map(([key, value]) => `${key}:${value}`) : [];
|
|
2072
|
+
const searchParams = new URLSearchParams();
|
|
2073
|
+
if (runId) {
|
|
2074
|
+
searchParams.set("runId", runId);
|
|
2075
|
+
}
|
|
2076
|
+
if (transportId) {
|
|
2077
|
+
searchParams.set("transportId", transportId);
|
|
2078
|
+
}
|
|
2079
|
+
if (fromDate) {
|
|
2080
|
+
searchParams.set("fromDate", fromDate.toISOString());
|
|
2081
|
+
}
|
|
2082
|
+
if (toDate) {
|
|
2083
|
+
searchParams.set("toDate", toDate.toISOString());
|
|
2084
|
+
}
|
|
2085
|
+
if (logLevel) {
|
|
2086
|
+
searchParams.set("logLevel", logLevel);
|
|
2087
|
+
}
|
|
2088
|
+
if (page) {
|
|
2089
|
+
searchParams.set("page", String(page));
|
|
2090
|
+
}
|
|
2091
|
+
if (perPage) {
|
|
2092
|
+
searchParams.set("perPage", String(perPage));
|
|
2093
|
+
}
|
|
2094
|
+
if (_filters) {
|
|
2095
|
+
if (Array.isArray(_filters)) {
|
|
2096
|
+
for (const filter of _filters) {
|
|
2097
|
+
searchParams.append("filters", filter);
|
|
2098
|
+
}
|
|
2099
|
+
} else {
|
|
2100
|
+
searchParams.set("filters", _filters);
|
|
2101
|
+
}
|
|
2102
|
+
}
|
|
2103
|
+
if (searchParams.size) {
|
|
2104
|
+
return this.request(`/api/logs/${runId}?${searchParams}`);
|
|
2105
|
+
} else {
|
|
2106
|
+
return this.request(`/api/logs/${runId}`);
|
|
2107
|
+
}
|
|
2108
|
+
}
|
|
2109
|
+
/**
|
|
2110
|
+
* List of all log transports
|
|
2111
|
+
* @returns Promise containing list of log transports
|
|
2112
|
+
*/
|
|
2113
|
+
getLogTransports() {
|
|
2114
|
+
return this.request("/api/logs/transports");
|
|
2115
|
+
}
|
|
2116
|
+
/**
|
|
2117
|
+
* List of all traces (paged)
|
|
2118
|
+
* @param params - Parameters for filtering traces
|
|
2119
|
+
* @returns Promise containing telemetry data
|
|
2120
|
+
*/
|
|
2121
|
+
getTelemetry(params) {
|
|
2122
|
+
const { name, scope, page, perPage, attribute, fromDate, toDate } = params || {};
|
|
2123
|
+
const _attribute = attribute ? Object.entries(attribute).map(([key, value]) => `${key}:${value}`) : [];
|
|
2124
|
+
const searchParams = new URLSearchParams();
|
|
2125
|
+
if (name) {
|
|
2126
|
+
searchParams.set("name", name);
|
|
2127
|
+
}
|
|
2128
|
+
if (scope) {
|
|
2129
|
+
searchParams.set("scope", scope);
|
|
2130
|
+
}
|
|
2131
|
+
if (page) {
|
|
2132
|
+
searchParams.set("page", String(page));
|
|
2133
|
+
}
|
|
2134
|
+
if (perPage) {
|
|
2135
|
+
searchParams.set("perPage", String(perPage));
|
|
2136
|
+
}
|
|
2137
|
+
if (_attribute) {
|
|
2138
|
+
if (Array.isArray(_attribute)) {
|
|
2139
|
+
for (const attr of _attribute) {
|
|
2140
|
+
searchParams.append("attribute", attr);
|
|
2141
|
+
}
|
|
2142
|
+
} else {
|
|
2143
|
+
searchParams.set("attribute", _attribute);
|
|
2144
|
+
}
|
|
2145
|
+
}
|
|
2146
|
+
if (fromDate) {
|
|
2147
|
+
searchParams.set("fromDate", fromDate.toISOString());
|
|
2148
|
+
}
|
|
2149
|
+
if (toDate) {
|
|
2150
|
+
searchParams.set("toDate", toDate.toISOString());
|
|
2151
|
+
}
|
|
2152
|
+
if (searchParams.size) {
|
|
2153
|
+
return this.request(`/api/telemetry?${searchParams}`);
|
|
2154
|
+
} else {
|
|
2155
|
+
return this.request(`/api/telemetry`);
|
|
2156
|
+
}
|
|
2157
|
+
}
|
|
2158
|
+
/**
|
|
2159
|
+
* Retrieves all available networks
|
|
2160
|
+
* @returns Promise containing map of network IDs to network details
|
|
2161
|
+
*/
|
|
2162
|
+
getNetworks() {
|
|
2163
|
+
return this.request("/api/networks");
|
|
2164
|
+
}
|
|
2165
|
+
/**
|
|
2166
|
+
* Retrieves all available vNext networks
|
|
2167
|
+
* @returns Promise containing map of vNext network IDs to vNext network details
|
|
2168
|
+
*/
|
|
2169
|
+
getVNextNetworks() {
|
|
2170
|
+
return this.request("/api/networks/v-next");
|
|
2171
|
+
}
|
|
2172
|
+
/**
|
|
2173
|
+
* Gets a network instance by ID
|
|
2174
|
+
* @param networkId - ID of the network to retrieve
|
|
2175
|
+
* @returns Network instance
|
|
2176
|
+
*/
|
|
2177
|
+
getNetwork(networkId) {
|
|
2178
|
+
return new Network(this.options, networkId);
|
|
2179
|
+
}
|
|
2180
|
+
/**
|
|
2181
|
+
* Gets a vNext network instance by ID
|
|
2182
|
+
* @param networkId - ID of the vNext network to retrieve
|
|
2183
|
+
* @returns vNext Network instance
|
|
2184
|
+
*/
|
|
2185
|
+
getVNextNetwork(networkId) {
|
|
2186
|
+
return new VNextNetwork(this.options, networkId);
|
|
2187
|
+
}
|
|
2188
|
+
/**
|
|
2189
|
+
* Retrieves a list of available MCP servers.
|
|
2190
|
+
* @param params - Optional parameters for pagination (limit, offset).
|
|
2191
|
+
* @returns Promise containing the list of MCP servers and pagination info.
|
|
2192
|
+
*/
|
|
2193
|
+
getMcpServers(params) {
|
|
2194
|
+
const searchParams = new URLSearchParams();
|
|
2195
|
+
if (params?.limit !== void 0) {
|
|
2196
|
+
searchParams.set("limit", String(params.limit));
|
|
2197
|
+
}
|
|
2198
|
+
if (params?.offset !== void 0) {
|
|
2199
|
+
searchParams.set("offset", String(params.offset));
|
|
2200
|
+
}
|
|
2201
|
+
const queryString = searchParams.toString();
|
|
2202
|
+
return this.request(`/api/mcp/v0/servers${queryString ? `?${queryString}` : ""}`);
|
|
2203
|
+
}
|
|
2204
|
+
/**
|
|
2205
|
+
* Retrieves detailed information for a specific MCP server.
|
|
2206
|
+
* @param serverId - The ID of the MCP server to retrieve.
|
|
2207
|
+
* @param params - Optional parameters, e.g., specific version.
|
|
2208
|
+
* @returns Promise containing the detailed MCP server information.
|
|
2209
|
+
*/
|
|
2210
|
+
getMcpServerDetails(serverId, params) {
|
|
2211
|
+
const searchParams = new URLSearchParams();
|
|
2212
|
+
if (params?.version) {
|
|
2213
|
+
searchParams.set("version", params.version);
|
|
2214
|
+
}
|
|
2215
|
+
const queryString = searchParams.toString();
|
|
2216
|
+
return this.request(`/api/mcp/v0/servers/${serverId}${queryString ? `?${queryString}` : ""}`);
|
|
2217
|
+
}
|
|
2218
|
+
/**
|
|
2219
|
+
* Retrieves a list of tools for a specific MCP server.
|
|
2220
|
+
* @param serverId - The ID of the MCP server.
|
|
2221
|
+
* @returns Promise containing the list of tools.
|
|
2222
|
+
*/
|
|
2223
|
+
getMcpServerTools(serverId) {
|
|
2224
|
+
return this.request(`/api/mcp/${serverId}/tools`);
|
|
2225
|
+
}
|
|
2226
|
+
/**
|
|
2227
|
+
* Gets an MCPTool resource instance for a specific tool on an MCP server.
|
|
2228
|
+
* This instance can then be used to fetch details or execute the tool.
|
|
2229
|
+
* @param serverId - The ID of the MCP server.
|
|
2230
|
+
* @param toolId - The ID of the tool.
|
|
2231
|
+
* @returns MCPTool instance.
|
|
2232
|
+
*/
|
|
2233
|
+
getMcpServerTool(serverId, toolId) {
|
|
2234
|
+
return new MCPTool(this.options, serverId, toolId);
|
|
2235
|
+
}
|
|
2236
|
+
/**
|
|
2237
|
+
* Gets an A2A client for interacting with an agent via the A2A protocol
|
|
2238
|
+
* @param agentId - ID of the agent to interact with
|
|
2239
|
+
* @returns A2A client instance
|
|
2240
|
+
*/
|
|
2241
|
+
getA2A(agentId) {
|
|
2242
|
+
return new A2A(this.options, agentId);
|
|
2243
|
+
}
|
|
2244
|
+
};
|
|
2245
|
+
|
|
2246
|
+
exports.MastraClient = MastraClient;
|