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