@mastra/client-js 0.0.0-ai-v5-20250625173645 → 0.0.0-ai-v5-20250710191716
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 +8 -8
- package/CHANGELOG.md +264 -3
- package/LICENSE.md +11 -42
- package/dist/index.cjs +703 -21
- package/dist/index.d.cts +224 -10
- package/dist/index.d.ts +224 -10
- package/dist/index.js +704 -22
- package/package.json +7 -7
- package/src/client.ts +71 -1
- package/src/example.ts +46 -15
- package/src/resources/agent.ts +603 -21
- package/src/resources/base.ts +1 -0
- package/src/resources/network-memory-thread.ts +63 -0
- package/src/resources/network.ts +2 -3
- package/src/resources/vNextNetwork.ts +177 -0
- package/src/resources/workflow.ts +28 -5
- package/src/types.ts +90 -3
- package/src/utils/process-client-tools.ts +3 -2
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
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 { processTextStream, processDataStream, parsePartialJson } from '@ai-sdk/ui-utils';
|
|
4
4
|
import { ZodSchema } from 'zod';
|
|
5
5
|
import originalZodToJsonSchema from 'zod-to-json-schema';
|
|
6
6
|
import { isVercelTool } from '@mastra/core/tools';
|
|
@@ -252,6 +252,7 @@ var BaseResource = class {
|
|
|
252
252
|
// TODO: Bring this back once we figure out what we/users need to do to make this work with cross-origin requests
|
|
253
253
|
// 'x-mastra-client-type': 'js',
|
|
254
254
|
},
|
|
255
|
+
signal: this.options.abortSignal,
|
|
255
256
|
body: options.body instanceof FormData ? options.body : options.body ? JSON.stringify(options.body) : void 0
|
|
256
257
|
});
|
|
257
258
|
if (!response.ok) {
|
|
@@ -293,8 +294,6 @@ function parseClientRuntimeContext(runtimeContext) {
|
|
|
293
294
|
}
|
|
294
295
|
return void 0;
|
|
295
296
|
}
|
|
296
|
-
|
|
297
|
-
// src/resources/agent.ts
|
|
298
297
|
var AgentVoice = class extends BaseResource {
|
|
299
298
|
constructor(options, agentId) {
|
|
300
299
|
super(options);
|
|
@@ -363,7 +362,7 @@ var Agent = class extends BaseResource {
|
|
|
363
362
|
details() {
|
|
364
363
|
return this.request(`/api/agents/${this.agentId}`);
|
|
365
364
|
}
|
|
366
|
-
generate(params) {
|
|
365
|
+
async generate(params) {
|
|
367
366
|
const processedParams = {
|
|
368
367
|
...params,
|
|
369
368
|
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
@@ -371,15 +370,440 @@ var Agent = class extends BaseResource {
|
|
|
371
370
|
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
372
371
|
clientTools: processClientTools(params.clientTools)
|
|
373
372
|
};
|
|
374
|
-
|
|
373
|
+
const { runId, resourceId, threadId, runtimeContext } = processedParams;
|
|
374
|
+
const response = await this.request(`/api/agents/${this.agentId}/generate`, {
|
|
375
375
|
method: "POST",
|
|
376
376
|
body: processedParams
|
|
377
377
|
});
|
|
378
|
+
if (response.finishReason === "tool-calls") {
|
|
379
|
+
const toolCalls = response.toolCalls;
|
|
380
|
+
if (!toolCalls || !Array.isArray(toolCalls)) {
|
|
381
|
+
return response;
|
|
382
|
+
}
|
|
383
|
+
for (const toolCall of toolCalls) {
|
|
384
|
+
const clientTool = params.clientTools?.[toolCall.toolName];
|
|
385
|
+
if (clientTool && clientTool.execute) {
|
|
386
|
+
const result = await clientTool.execute(
|
|
387
|
+
{ context: toolCall?.args, runId, resourceId, threadId, runtimeContext },
|
|
388
|
+
{
|
|
389
|
+
messages: response.messages,
|
|
390
|
+
toolCallId: toolCall?.toolCallId
|
|
391
|
+
}
|
|
392
|
+
);
|
|
393
|
+
const updatedMessages = [
|
|
394
|
+
{
|
|
395
|
+
role: "user",
|
|
396
|
+
content: params.messages
|
|
397
|
+
},
|
|
398
|
+
...response.response.messages,
|
|
399
|
+
{
|
|
400
|
+
role: "tool",
|
|
401
|
+
content: [
|
|
402
|
+
{
|
|
403
|
+
type: "tool-result",
|
|
404
|
+
toolCallId: toolCall.toolCallId,
|
|
405
|
+
toolName: toolCall.toolName,
|
|
406
|
+
result
|
|
407
|
+
}
|
|
408
|
+
]
|
|
409
|
+
}
|
|
410
|
+
];
|
|
411
|
+
return this.generate({
|
|
412
|
+
...params,
|
|
413
|
+
messages: updatedMessages
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
return response;
|
|
419
|
+
}
|
|
420
|
+
async processChatResponse({
|
|
421
|
+
stream,
|
|
422
|
+
update,
|
|
423
|
+
onToolCall,
|
|
424
|
+
onFinish,
|
|
425
|
+
getCurrentDate = () => /* @__PURE__ */ new Date(),
|
|
426
|
+
lastMessage,
|
|
427
|
+
streamProtocol
|
|
428
|
+
}) {
|
|
429
|
+
const replaceLastMessage = lastMessage?.role === "assistant";
|
|
430
|
+
let step = replaceLastMessage ? 1 + // find max step in existing tool invocations:
|
|
431
|
+
(lastMessage.toolInvocations?.reduce((max, toolInvocation) => {
|
|
432
|
+
return Math.max(max, toolInvocation.step ?? 0);
|
|
433
|
+
}, 0) ?? 0) : 0;
|
|
434
|
+
const message = replaceLastMessage ? structuredClone(lastMessage) : {
|
|
435
|
+
id: crypto.randomUUID(),
|
|
436
|
+
createdAt: getCurrentDate(),
|
|
437
|
+
role: "assistant",
|
|
438
|
+
content: "",
|
|
439
|
+
parts: []
|
|
440
|
+
};
|
|
441
|
+
let currentTextPart = void 0;
|
|
442
|
+
let currentReasoningPart = void 0;
|
|
443
|
+
let currentReasoningTextDetail = void 0;
|
|
444
|
+
function updateToolInvocationPart(toolCallId, invocation) {
|
|
445
|
+
const part = message.parts.find(
|
|
446
|
+
(part2) => part2.type === "tool-invocation" && part2.toolInvocation.toolCallId === toolCallId
|
|
447
|
+
);
|
|
448
|
+
if (part != null) {
|
|
449
|
+
part.toolInvocation = invocation;
|
|
450
|
+
} else {
|
|
451
|
+
message.parts.push({
|
|
452
|
+
type: "tool-invocation",
|
|
453
|
+
toolInvocation: invocation
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
const data = [];
|
|
458
|
+
let messageAnnotations = replaceLastMessage ? lastMessage?.annotations : void 0;
|
|
459
|
+
const partialToolCalls = {};
|
|
460
|
+
let usage = {
|
|
461
|
+
completionTokens: NaN,
|
|
462
|
+
promptTokens: NaN,
|
|
463
|
+
totalTokens: NaN
|
|
464
|
+
};
|
|
465
|
+
let finishReason = "unknown";
|
|
466
|
+
function execUpdate() {
|
|
467
|
+
const copiedData = [...data];
|
|
468
|
+
if (messageAnnotations?.length) {
|
|
469
|
+
message.annotations = messageAnnotations;
|
|
470
|
+
}
|
|
471
|
+
const copiedMessage = {
|
|
472
|
+
// deep copy the message to ensure that deep changes (msg attachments) are updated
|
|
473
|
+
// with SolidJS. SolidJS uses referential integration of sub-objects to detect changes.
|
|
474
|
+
...structuredClone(message),
|
|
475
|
+
// add a revision id to ensure that the message is updated with SWR. SWR uses a
|
|
476
|
+
// hashing approach by default to detect changes, but it only works for shallow
|
|
477
|
+
// changes. This is why we need to add a revision id to ensure that the message
|
|
478
|
+
// is updated with SWR (without it, the changes get stuck in SWR and are not
|
|
479
|
+
// forwarded to rendering):
|
|
480
|
+
revisionId: crypto.randomUUID()
|
|
481
|
+
};
|
|
482
|
+
update({
|
|
483
|
+
message: copiedMessage,
|
|
484
|
+
data: copiedData,
|
|
485
|
+
replaceLastMessage
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
if (streamProtocol === "text") {
|
|
489
|
+
await processTextStream({
|
|
490
|
+
stream,
|
|
491
|
+
onTextPart(value) {
|
|
492
|
+
message.content += value;
|
|
493
|
+
execUpdate();
|
|
494
|
+
}
|
|
495
|
+
});
|
|
496
|
+
onFinish?.({ message, finishReason, usage });
|
|
497
|
+
} else {
|
|
498
|
+
await processDataStream({
|
|
499
|
+
stream,
|
|
500
|
+
onTextPart(value) {
|
|
501
|
+
if (currentTextPart == null) {
|
|
502
|
+
currentTextPart = {
|
|
503
|
+
type: "text",
|
|
504
|
+
text: value
|
|
505
|
+
};
|
|
506
|
+
message.parts.push(currentTextPart);
|
|
507
|
+
} else {
|
|
508
|
+
currentTextPart.text += value;
|
|
509
|
+
}
|
|
510
|
+
message.content += value;
|
|
511
|
+
execUpdate();
|
|
512
|
+
},
|
|
513
|
+
onReasoningPart(value) {
|
|
514
|
+
if (currentReasoningTextDetail == null) {
|
|
515
|
+
currentReasoningTextDetail = { type: "text", text: value };
|
|
516
|
+
if (currentReasoningPart != null) {
|
|
517
|
+
currentReasoningPart.details.push(currentReasoningTextDetail);
|
|
518
|
+
}
|
|
519
|
+
} else {
|
|
520
|
+
currentReasoningTextDetail.text += value;
|
|
521
|
+
}
|
|
522
|
+
if (currentReasoningPart == null) {
|
|
523
|
+
currentReasoningPart = {
|
|
524
|
+
type: "reasoning",
|
|
525
|
+
reasoning: value,
|
|
526
|
+
details: [currentReasoningTextDetail]
|
|
527
|
+
};
|
|
528
|
+
message.parts.push(currentReasoningPart);
|
|
529
|
+
} else {
|
|
530
|
+
currentReasoningPart.reasoning += value;
|
|
531
|
+
}
|
|
532
|
+
message.reasoning = (message.reasoning ?? "") + value;
|
|
533
|
+
execUpdate();
|
|
534
|
+
},
|
|
535
|
+
onReasoningSignaturePart(value) {
|
|
536
|
+
if (currentReasoningTextDetail != null) {
|
|
537
|
+
currentReasoningTextDetail.signature = value.signature;
|
|
538
|
+
}
|
|
539
|
+
},
|
|
540
|
+
onRedactedReasoningPart(value) {
|
|
541
|
+
if (currentReasoningPart == null) {
|
|
542
|
+
currentReasoningPart = {
|
|
543
|
+
type: "reasoning",
|
|
544
|
+
reasoning: "",
|
|
545
|
+
details: []
|
|
546
|
+
};
|
|
547
|
+
message.parts.push(currentReasoningPart);
|
|
548
|
+
}
|
|
549
|
+
currentReasoningPart.details.push({
|
|
550
|
+
type: "redacted",
|
|
551
|
+
data: value.data
|
|
552
|
+
});
|
|
553
|
+
currentReasoningTextDetail = void 0;
|
|
554
|
+
execUpdate();
|
|
555
|
+
},
|
|
556
|
+
onFilePart(value) {
|
|
557
|
+
message.parts.push({
|
|
558
|
+
type: "file",
|
|
559
|
+
mimeType: value.mimeType,
|
|
560
|
+
data: value.data
|
|
561
|
+
});
|
|
562
|
+
execUpdate();
|
|
563
|
+
},
|
|
564
|
+
onSourcePart(value) {
|
|
565
|
+
message.parts.push({
|
|
566
|
+
type: "source",
|
|
567
|
+
source: value
|
|
568
|
+
});
|
|
569
|
+
execUpdate();
|
|
570
|
+
},
|
|
571
|
+
onToolCallStreamingStartPart(value) {
|
|
572
|
+
if (message.toolInvocations == null) {
|
|
573
|
+
message.toolInvocations = [];
|
|
574
|
+
}
|
|
575
|
+
partialToolCalls[value.toolCallId] = {
|
|
576
|
+
text: "",
|
|
577
|
+
step,
|
|
578
|
+
toolName: value.toolName,
|
|
579
|
+
index: message.toolInvocations.length
|
|
580
|
+
};
|
|
581
|
+
const invocation = {
|
|
582
|
+
state: "partial-call",
|
|
583
|
+
step,
|
|
584
|
+
toolCallId: value.toolCallId,
|
|
585
|
+
toolName: value.toolName,
|
|
586
|
+
args: void 0
|
|
587
|
+
};
|
|
588
|
+
message.toolInvocations.push(invocation);
|
|
589
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
590
|
+
execUpdate();
|
|
591
|
+
},
|
|
592
|
+
onToolCallDeltaPart(value) {
|
|
593
|
+
const partialToolCall = partialToolCalls[value.toolCallId];
|
|
594
|
+
partialToolCall.text += value.argsTextDelta;
|
|
595
|
+
const { value: partialArgs } = parsePartialJson(partialToolCall.text);
|
|
596
|
+
const invocation = {
|
|
597
|
+
state: "partial-call",
|
|
598
|
+
step: partialToolCall.step,
|
|
599
|
+
toolCallId: value.toolCallId,
|
|
600
|
+
toolName: partialToolCall.toolName,
|
|
601
|
+
args: partialArgs
|
|
602
|
+
};
|
|
603
|
+
message.toolInvocations[partialToolCall.index] = invocation;
|
|
604
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
605
|
+
execUpdate();
|
|
606
|
+
},
|
|
607
|
+
async onToolCallPart(value) {
|
|
608
|
+
const invocation = {
|
|
609
|
+
state: "call",
|
|
610
|
+
step,
|
|
611
|
+
...value
|
|
612
|
+
};
|
|
613
|
+
if (partialToolCalls[value.toolCallId] != null) {
|
|
614
|
+
message.toolInvocations[partialToolCalls[value.toolCallId].index] = invocation;
|
|
615
|
+
} else {
|
|
616
|
+
if (message.toolInvocations == null) {
|
|
617
|
+
message.toolInvocations = [];
|
|
618
|
+
}
|
|
619
|
+
message.toolInvocations.push(invocation);
|
|
620
|
+
}
|
|
621
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
622
|
+
execUpdate();
|
|
623
|
+
if (onToolCall) {
|
|
624
|
+
const result = await onToolCall({ toolCall: value });
|
|
625
|
+
if (result != null) {
|
|
626
|
+
const invocation2 = {
|
|
627
|
+
state: "result",
|
|
628
|
+
step,
|
|
629
|
+
...value,
|
|
630
|
+
result
|
|
631
|
+
};
|
|
632
|
+
message.toolInvocations[message.toolInvocations.length - 1] = invocation2;
|
|
633
|
+
updateToolInvocationPart(value.toolCallId, invocation2);
|
|
634
|
+
execUpdate();
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
},
|
|
638
|
+
onToolResultPart(value) {
|
|
639
|
+
const toolInvocations = message.toolInvocations;
|
|
640
|
+
if (toolInvocations == null) {
|
|
641
|
+
throw new Error("tool_result must be preceded by a tool_call");
|
|
642
|
+
}
|
|
643
|
+
const toolInvocationIndex = toolInvocations.findIndex(
|
|
644
|
+
(invocation2) => invocation2.toolCallId === value.toolCallId
|
|
645
|
+
);
|
|
646
|
+
if (toolInvocationIndex === -1) {
|
|
647
|
+
throw new Error("tool_result must be preceded by a tool_call with the same toolCallId");
|
|
648
|
+
}
|
|
649
|
+
const invocation = {
|
|
650
|
+
...toolInvocations[toolInvocationIndex],
|
|
651
|
+
state: "result",
|
|
652
|
+
...value
|
|
653
|
+
};
|
|
654
|
+
toolInvocations[toolInvocationIndex] = invocation;
|
|
655
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
656
|
+
execUpdate();
|
|
657
|
+
},
|
|
658
|
+
onDataPart(value) {
|
|
659
|
+
data.push(...value);
|
|
660
|
+
execUpdate();
|
|
661
|
+
},
|
|
662
|
+
onMessageAnnotationsPart(value) {
|
|
663
|
+
if (messageAnnotations == null) {
|
|
664
|
+
messageAnnotations = [...value];
|
|
665
|
+
} else {
|
|
666
|
+
messageAnnotations.push(...value);
|
|
667
|
+
}
|
|
668
|
+
execUpdate();
|
|
669
|
+
},
|
|
670
|
+
onFinishStepPart(value) {
|
|
671
|
+
step += 1;
|
|
672
|
+
currentTextPart = value.isContinued ? currentTextPart : void 0;
|
|
673
|
+
currentReasoningPart = void 0;
|
|
674
|
+
currentReasoningTextDetail = void 0;
|
|
675
|
+
},
|
|
676
|
+
onStartStepPart(value) {
|
|
677
|
+
if (!replaceLastMessage) {
|
|
678
|
+
message.id = value.messageId;
|
|
679
|
+
}
|
|
680
|
+
message.parts.push({ type: "step-start" });
|
|
681
|
+
execUpdate();
|
|
682
|
+
},
|
|
683
|
+
onFinishMessagePart(value) {
|
|
684
|
+
finishReason = value.finishReason;
|
|
685
|
+
if (value.usage != null) {
|
|
686
|
+
usage = value.usage;
|
|
687
|
+
}
|
|
688
|
+
},
|
|
689
|
+
onErrorPart(error) {
|
|
690
|
+
throw new Error(error);
|
|
691
|
+
}
|
|
692
|
+
});
|
|
693
|
+
onFinish?.({ message, finishReason, usage });
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
/**
|
|
697
|
+
* Processes the stream response and handles tool calls
|
|
698
|
+
*/
|
|
699
|
+
async processStreamResponse(processedParams, writable) {
|
|
700
|
+
const response = await this.request(`/api/agents/${this.agentId}/stream`, {
|
|
701
|
+
method: "POST",
|
|
702
|
+
body: processedParams,
|
|
703
|
+
stream: true
|
|
704
|
+
});
|
|
705
|
+
if (!response.body) {
|
|
706
|
+
throw new Error("No response body");
|
|
707
|
+
}
|
|
708
|
+
try {
|
|
709
|
+
const streamProtocol = processedParams.output ? "text" : "data";
|
|
710
|
+
let toolCalls = [];
|
|
711
|
+
let messages = [];
|
|
712
|
+
const [streamForWritable, streamForProcessing] = response.body.tee();
|
|
713
|
+
streamForWritable.pipeTo(writable, {
|
|
714
|
+
preventClose: true
|
|
715
|
+
}).catch((error) => {
|
|
716
|
+
console.error("Error piping to writable stream:", error);
|
|
717
|
+
});
|
|
718
|
+
this.processChatResponse({
|
|
719
|
+
stream: streamForProcessing,
|
|
720
|
+
update: ({ message }) => {
|
|
721
|
+
messages.push(message);
|
|
722
|
+
},
|
|
723
|
+
onFinish: async ({ finishReason, message }) => {
|
|
724
|
+
if (finishReason === "tool-calls") {
|
|
725
|
+
const toolCall = [...message?.parts ?? []].reverse().find((part) => part.type === "tool-invocation")?.toolInvocation;
|
|
726
|
+
if (toolCall) {
|
|
727
|
+
toolCalls.push(toolCall);
|
|
728
|
+
}
|
|
729
|
+
for (const toolCall2 of toolCalls) {
|
|
730
|
+
const clientTool = processedParams.clientTools?.[toolCall2.toolName];
|
|
731
|
+
if (clientTool && clientTool.execute) {
|
|
732
|
+
const result = await clientTool.execute(
|
|
733
|
+
{
|
|
734
|
+
context: toolCall2?.args,
|
|
735
|
+
runId: processedParams.runId,
|
|
736
|
+
resourceId: processedParams.resourceId,
|
|
737
|
+
threadId: processedParams.threadId,
|
|
738
|
+
runtimeContext: processedParams.runtimeContext
|
|
739
|
+
},
|
|
740
|
+
{
|
|
741
|
+
messages: response.messages,
|
|
742
|
+
toolCallId: toolCall2?.toolCallId
|
|
743
|
+
}
|
|
744
|
+
);
|
|
745
|
+
const lastMessage = JSON.parse(JSON.stringify(messages[messages.length - 1]));
|
|
746
|
+
const toolInvocationPart = lastMessage?.parts?.find(
|
|
747
|
+
(part) => part.type === "tool-invocation" && part.toolInvocation?.toolCallId === toolCall2.toolCallId
|
|
748
|
+
);
|
|
749
|
+
if (toolInvocationPart) {
|
|
750
|
+
toolInvocationPart.toolInvocation = {
|
|
751
|
+
...toolInvocationPart.toolInvocation,
|
|
752
|
+
state: "result",
|
|
753
|
+
result
|
|
754
|
+
};
|
|
755
|
+
}
|
|
756
|
+
const toolInvocation = lastMessage?.toolInvocations?.find(
|
|
757
|
+
(toolInvocation2) => toolInvocation2.toolCallId === toolCall2.toolCallId
|
|
758
|
+
);
|
|
759
|
+
if (toolInvocation) {
|
|
760
|
+
toolInvocation.state = "result";
|
|
761
|
+
toolInvocation.result = result;
|
|
762
|
+
}
|
|
763
|
+
const writer = writable.getWriter();
|
|
764
|
+
try {
|
|
765
|
+
await writer.write(
|
|
766
|
+
new TextEncoder().encode(
|
|
767
|
+
"a:" + JSON.stringify({
|
|
768
|
+
toolCallId: toolCall2.toolCallId,
|
|
769
|
+
result
|
|
770
|
+
}) + "\n"
|
|
771
|
+
)
|
|
772
|
+
);
|
|
773
|
+
} finally {
|
|
774
|
+
writer.releaseLock();
|
|
775
|
+
}
|
|
776
|
+
const originalMessages = processedParams.messages;
|
|
777
|
+
const messageArray = Array.isArray(originalMessages) ? originalMessages : [originalMessages];
|
|
778
|
+
this.processStreamResponse(
|
|
779
|
+
{
|
|
780
|
+
...processedParams,
|
|
781
|
+
messages: [...messageArray, ...messages, lastMessage]
|
|
782
|
+
},
|
|
783
|
+
writable
|
|
784
|
+
);
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
} else {
|
|
788
|
+
setTimeout(() => {
|
|
789
|
+
if (!writable.locked) {
|
|
790
|
+
writable.close();
|
|
791
|
+
}
|
|
792
|
+
}, 0);
|
|
793
|
+
}
|
|
794
|
+
},
|
|
795
|
+
lastMessage: void 0,
|
|
796
|
+
streamProtocol
|
|
797
|
+
});
|
|
798
|
+
} catch (error) {
|
|
799
|
+
console.error("Error processing stream response:", error);
|
|
800
|
+
}
|
|
801
|
+
return response;
|
|
378
802
|
}
|
|
379
803
|
/**
|
|
380
804
|
* Streams a response from the agent
|
|
381
805
|
* @param params - Stream parameters including prompt
|
|
382
|
-
* @returns Promise containing the enhanced Response object with processDataStream
|
|
806
|
+
* @returns Promise containing the enhanced Response object with processDataStream and processTextStream methods
|
|
383
807
|
*/
|
|
384
808
|
async stream(params) {
|
|
385
809
|
const processedParams = {
|
|
@@ -389,21 +813,27 @@ var Agent = class extends BaseResource {
|
|
|
389
813
|
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
390
814
|
clientTools: processClientTools(params.clientTools)
|
|
391
815
|
};
|
|
392
|
-
const
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
816
|
+
const { readable, writable } = new TransformStream();
|
|
817
|
+
const response = await this.processStreamResponse(processedParams, writable);
|
|
818
|
+
const streamResponse = new Response(readable, {
|
|
819
|
+
status: response.status,
|
|
820
|
+
statusText: response.statusText,
|
|
821
|
+
headers: response.headers
|
|
396
822
|
});
|
|
397
|
-
|
|
398
|
-
throw new Error("No response body");
|
|
399
|
-
}
|
|
400
|
-
response.processDataStream = async (options = {}) => {
|
|
823
|
+
streamResponse.processDataStream = async (options = {}) => {
|
|
401
824
|
await processDataStream({
|
|
402
|
-
stream:
|
|
825
|
+
stream: streamResponse.body,
|
|
403
826
|
...options
|
|
404
827
|
});
|
|
405
828
|
};
|
|
406
|
-
|
|
829
|
+
streamResponse.processTextStream = async (options) => {
|
|
830
|
+
await processTextStream({
|
|
831
|
+
stream: streamResponse.body,
|
|
832
|
+
onTextPart: options?.onTextPart ?? (() => {
|
|
833
|
+
})
|
|
834
|
+
});
|
|
835
|
+
};
|
|
836
|
+
return streamResponse;
|
|
407
837
|
}
|
|
408
838
|
/**
|
|
409
839
|
* Gets details about a specific tool available to the agent
|
|
@@ -798,7 +1228,7 @@ var LegacyWorkflow = class extends BaseResource {
|
|
|
798
1228
|
};
|
|
799
1229
|
|
|
800
1230
|
// src/resources/tool.ts
|
|
801
|
-
var
|
|
1231
|
+
var Tool2 = class extends BaseResource {
|
|
802
1232
|
constructor(options, toolId) {
|
|
803
1233
|
super(options);
|
|
804
1234
|
this.toolId = toolId;
|
|
@@ -903,10 +1333,10 @@ var Workflow = class extends BaseResource {
|
|
|
903
1333
|
if (params?.toDate) {
|
|
904
1334
|
searchParams.set("toDate", params.toDate.toISOString());
|
|
905
1335
|
}
|
|
906
|
-
if (params?.limit) {
|
|
1336
|
+
if (params?.limit !== null && params?.limit !== void 0 && !isNaN(Number(params?.limit))) {
|
|
907
1337
|
searchParams.set("limit", String(params.limit));
|
|
908
1338
|
}
|
|
909
|
-
if (params?.offset) {
|
|
1339
|
+
if (params?.offset !== null && params?.offset !== void 0 && !isNaN(Number(params?.offset))) {
|
|
910
1340
|
searchParams.set("offset", String(params.offset));
|
|
911
1341
|
}
|
|
912
1342
|
if (params?.resourceId) {
|
|
@@ -934,6 +1364,27 @@ var Workflow = class extends BaseResource {
|
|
|
934
1364
|
runExecutionResult(runId) {
|
|
935
1365
|
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}/execution-result`);
|
|
936
1366
|
}
|
|
1367
|
+
/**
|
|
1368
|
+
* Cancels a specific workflow run by its ID
|
|
1369
|
+
* @param runId - The ID of the workflow run to cancel
|
|
1370
|
+
* @returns Promise containing a success message
|
|
1371
|
+
*/
|
|
1372
|
+
cancelRun(runId) {
|
|
1373
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}/cancel`, {
|
|
1374
|
+
method: "POST"
|
|
1375
|
+
});
|
|
1376
|
+
}
|
|
1377
|
+
/**
|
|
1378
|
+
* Sends an event to a specific workflow run by its ID
|
|
1379
|
+
* @param params - Object containing the runId, event and data
|
|
1380
|
+
* @returns Promise containing a success message
|
|
1381
|
+
*/
|
|
1382
|
+
sendRunEvent(params) {
|
|
1383
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${params.runId}/send-event`, {
|
|
1384
|
+
method: "POST",
|
|
1385
|
+
body: { event: params.event, data: params.data }
|
|
1386
|
+
});
|
|
1387
|
+
}
|
|
937
1388
|
/**
|
|
938
1389
|
* Creates a new workflow run
|
|
939
1390
|
* @param params - Optional object containing the optional runId
|
|
@@ -999,9 +1450,9 @@ var Workflow = class extends BaseResource {
|
|
|
999
1450
|
});
|
|
1000
1451
|
}
|
|
1001
1452
|
/**
|
|
1002
|
-
* Starts a
|
|
1453
|
+
* Starts a workflow run and returns a stream
|
|
1003
1454
|
* @param params - Object containing the optional runId, inputData and runtimeContext
|
|
1004
|
-
* @returns Promise containing the
|
|
1455
|
+
* @returns Promise containing the workflow execution results
|
|
1005
1456
|
*/
|
|
1006
1457
|
async stream(params) {
|
|
1007
1458
|
const searchParams = new URLSearchParams();
|
|
@@ -1217,6 +1668,180 @@ var MCPTool = class extends BaseResource {
|
|
|
1217
1668
|
}
|
|
1218
1669
|
};
|
|
1219
1670
|
|
|
1671
|
+
// src/resources/vNextNetwork.ts
|
|
1672
|
+
var RECORD_SEPARATOR3 = "";
|
|
1673
|
+
var VNextNetwork = class extends BaseResource {
|
|
1674
|
+
constructor(options, networkId) {
|
|
1675
|
+
super(options);
|
|
1676
|
+
this.networkId = networkId;
|
|
1677
|
+
}
|
|
1678
|
+
/**
|
|
1679
|
+
* Retrieves details about the network
|
|
1680
|
+
* @returns Promise containing vNext network details
|
|
1681
|
+
*/
|
|
1682
|
+
details() {
|
|
1683
|
+
return this.request(`/api/networks/v-next/${this.networkId}`);
|
|
1684
|
+
}
|
|
1685
|
+
/**
|
|
1686
|
+
* Generates a response from the v-next network
|
|
1687
|
+
* @param params - Generation parameters including message
|
|
1688
|
+
* @returns Promise containing the generated response
|
|
1689
|
+
*/
|
|
1690
|
+
generate(params) {
|
|
1691
|
+
return this.request(`/api/networks/v-next/${this.networkId}/generate`, {
|
|
1692
|
+
method: "POST",
|
|
1693
|
+
body: params
|
|
1694
|
+
});
|
|
1695
|
+
}
|
|
1696
|
+
/**
|
|
1697
|
+
* Generates a response from the v-next network using multiple primitives
|
|
1698
|
+
* @param params - Generation parameters including message
|
|
1699
|
+
* @returns Promise containing the generated response
|
|
1700
|
+
*/
|
|
1701
|
+
loop(params) {
|
|
1702
|
+
return this.request(`/api/networks/v-next/${this.networkId}/loop`, {
|
|
1703
|
+
method: "POST",
|
|
1704
|
+
body: params
|
|
1705
|
+
});
|
|
1706
|
+
}
|
|
1707
|
+
async *streamProcessor(stream) {
|
|
1708
|
+
const reader = stream.getReader();
|
|
1709
|
+
let doneReading = false;
|
|
1710
|
+
let buffer = "";
|
|
1711
|
+
try {
|
|
1712
|
+
while (!doneReading) {
|
|
1713
|
+
const { done, value } = await reader.read();
|
|
1714
|
+
doneReading = done;
|
|
1715
|
+
if (done && !value) continue;
|
|
1716
|
+
try {
|
|
1717
|
+
const decoded = value ? new TextDecoder().decode(value) : "";
|
|
1718
|
+
const chunks = (buffer + decoded).split(RECORD_SEPARATOR3);
|
|
1719
|
+
buffer = chunks.pop() || "";
|
|
1720
|
+
for (const chunk of chunks) {
|
|
1721
|
+
if (chunk) {
|
|
1722
|
+
if (typeof chunk === "string") {
|
|
1723
|
+
try {
|
|
1724
|
+
const parsedChunk = JSON.parse(chunk);
|
|
1725
|
+
yield parsedChunk;
|
|
1726
|
+
} catch {
|
|
1727
|
+
}
|
|
1728
|
+
}
|
|
1729
|
+
}
|
|
1730
|
+
}
|
|
1731
|
+
} catch {
|
|
1732
|
+
}
|
|
1733
|
+
}
|
|
1734
|
+
if (buffer) {
|
|
1735
|
+
try {
|
|
1736
|
+
yield JSON.parse(buffer);
|
|
1737
|
+
} catch {
|
|
1738
|
+
}
|
|
1739
|
+
}
|
|
1740
|
+
} finally {
|
|
1741
|
+
reader.cancel().catch(() => {
|
|
1742
|
+
});
|
|
1743
|
+
}
|
|
1744
|
+
}
|
|
1745
|
+
/**
|
|
1746
|
+
* Streams a response from the v-next network
|
|
1747
|
+
* @param params - Stream parameters including message
|
|
1748
|
+
* @returns Promise containing the results
|
|
1749
|
+
*/
|
|
1750
|
+
async stream(params, onRecord) {
|
|
1751
|
+
const response = await this.request(`/api/networks/v-next/${this.networkId}/stream`, {
|
|
1752
|
+
method: "POST",
|
|
1753
|
+
body: params,
|
|
1754
|
+
stream: true
|
|
1755
|
+
});
|
|
1756
|
+
if (!response.ok) {
|
|
1757
|
+
throw new Error(`Failed to stream vNext network: ${response.statusText}`);
|
|
1758
|
+
}
|
|
1759
|
+
if (!response.body) {
|
|
1760
|
+
throw new Error("Response body is null");
|
|
1761
|
+
}
|
|
1762
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
1763
|
+
if (typeof record === "string") {
|
|
1764
|
+
onRecord(JSON.parse(record));
|
|
1765
|
+
} else {
|
|
1766
|
+
onRecord(record);
|
|
1767
|
+
}
|
|
1768
|
+
}
|
|
1769
|
+
}
|
|
1770
|
+
/**
|
|
1771
|
+
* Streams a response from the v-next network loop
|
|
1772
|
+
* @param params - Stream parameters including message
|
|
1773
|
+
* @returns Promise containing the results
|
|
1774
|
+
*/
|
|
1775
|
+
async loopStream(params, onRecord) {
|
|
1776
|
+
const response = await this.request(`/api/networks/v-next/${this.networkId}/loop-stream`, {
|
|
1777
|
+
method: "POST",
|
|
1778
|
+
body: params,
|
|
1779
|
+
stream: true
|
|
1780
|
+
});
|
|
1781
|
+
if (!response.ok) {
|
|
1782
|
+
throw new Error(`Failed to stream vNext network loop: ${response.statusText}`);
|
|
1783
|
+
}
|
|
1784
|
+
if (!response.body) {
|
|
1785
|
+
throw new Error("Response body is null");
|
|
1786
|
+
}
|
|
1787
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
1788
|
+
if (typeof record === "string") {
|
|
1789
|
+
onRecord(JSON.parse(record));
|
|
1790
|
+
} else {
|
|
1791
|
+
onRecord(record);
|
|
1792
|
+
}
|
|
1793
|
+
}
|
|
1794
|
+
}
|
|
1795
|
+
};
|
|
1796
|
+
|
|
1797
|
+
// src/resources/network-memory-thread.ts
|
|
1798
|
+
var NetworkMemoryThread = class extends BaseResource {
|
|
1799
|
+
constructor(options, threadId, networkId) {
|
|
1800
|
+
super(options);
|
|
1801
|
+
this.threadId = threadId;
|
|
1802
|
+
this.networkId = networkId;
|
|
1803
|
+
}
|
|
1804
|
+
/**
|
|
1805
|
+
* Retrieves the memory thread details
|
|
1806
|
+
* @returns Promise containing thread details including title and metadata
|
|
1807
|
+
*/
|
|
1808
|
+
get() {
|
|
1809
|
+
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`);
|
|
1810
|
+
}
|
|
1811
|
+
/**
|
|
1812
|
+
* Updates the memory thread properties
|
|
1813
|
+
* @param params - Update parameters including title and metadata
|
|
1814
|
+
* @returns Promise containing updated thread details
|
|
1815
|
+
*/
|
|
1816
|
+
update(params) {
|
|
1817
|
+
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
|
|
1818
|
+
method: "PATCH",
|
|
1819
|
+
body: params
|
|
1820
|
+
});
|
|
1821
|
+
}
|
|
1822
|
+
/**
|
|
1823
|
+
* Deletes the memory thread
|
|
1824
|
+
* @returns Promise containing deletion result
|
|
1825
|
+
*/
|
|
1826
|
+
delete() {
|
|
1827
|
+
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
|
|
1828
|
+
method: "DELETE"
|
|
1829
|
+
});
|
|
1830
|
+
}
|
|
1831
|
+
/**
|
|
1832
|
+
* Retrieves messages associated with the thread
|
|
1833
|
+
* @param params - Optional parameters including limit for number of messages to retrieve
|
|
1834
|
+
* @returns Promise containing thread messages and UI messages
|
|
1835
|
+
*/
|
|
1836
|
+
getMessages(params) {
|
|
1837
|
+
const query = new URLSearchParams({
|
|
1838
|
+
networkId: this.networkId,
|
|
1839
|
+
...params?.limit ? { limit: params.limit.toString() } : {}
|
|
1840
|
+
});
|
|
1841
|
+
return this.request(`/api/memory/network/threads/${this.threadId}/messages?${query.toString()}`);
|
|
1842
|
+
}
|
|
1843
|
+
};
|
|
1844
|
+
|
|
1220
1845
|
// src/client.ts
|
|
1221
1846
|
var MastraClient = class extends BaseResource {
|
|
1222
1847
|
constructor(options) {
|
|
@@ -1294,6 +1919,48 @@ var MastraClient = class extends BaseResource {
|
|
|
1294
1919
|
getMemoryStatus(agentId) {
|
|
1295
1920
|
return this.request(`/api/memory/status?agentId=${agentId}`);
|
|
1296
1921
|
}
|
|
1922
|
+
/**
|
|
1923
|
+
* Retrieves memory threads for a resource
|
|
1924
|
+
* @param params - Parameters containing the resource ID
|
|
1925
|
+
* @returns Promise containing array of memory threads
|
|
1926
|
+
*/
|
|
1927
|
+
getNetworkMemoryThreads(params) {
|
|
1928
|
+
return this.request(`/api/memory/network/threads?resourceid=${params.resourceId}&networkId=${params.networkId}`);
|
|
1929
|
+
}
|
|
1930
|
+
/**
|
|
1931
|
+
* Creates a new memory thread
|
|
1932
|
+
* @param params - Parameters for creating the memory thread
|
|
1933
|
+
* @returns Promise containing the created memory thread
|
|
1934
|
+
*/
|
|
1935
|
+
createNetworkMemoryThread(params) {
|
|
1936
|
+
return this.request(`/api/memory/network/threads?networkId=${params.networkId}`, { method: "POST", body: params });
|
|
1937
|
+
}
|
|
1938
|
+
/**
|
|
1939
|
+
* Gets a memory thread instance by ID
|
|
1940
|
+
* @param threadId - ID of the memory thread to retrieve
|
|
1941
|
+
* @returns MemoryThread instance
|
|
1942
|
+
*/
|
|
1943
|
+
getNetworkMemoryThread(threadId, networkId) {
|
|
1944
|
+
return new NetworkMemoryThread(this.options, threadId, networkId);
|
|
1945
|
+
}
|
|
1946
|
+
/**
|
|
1947
|
+
* Saves messages to memory
|
|
1948
|
+
* @param params - Parameters containing messages to save
|
|
1949
|
+
* @returns Promise containing the saved messages
|
|
1950
|
+
*/
|
|
1951
|
+
saveNetworkMessageToMemory(params) {
|
|
1952
|
+
return this.request(`/api/memory/network/save-messages?networkId=${params.networkId}`, {
|
|
1953
|
+
method: "POST",
|
|
1954
|
+
body: params
|
|
1955
|
+
});
|
|
1956
|
+
}
|
|
1957
|
+
/**
|
|
1958
|
+
* Gets the status of the memory system
|
|
1959
|
+
* @returns Promise containing memory system status
|
|
1960
|
+
*/
|
|
1961
|
+
getNetworkMemoryStatus(networkId) {
|
|
1962
|
+
return this.request(`/api/memory/network/status?networkId=${networkId}`);
|
|
1963
|
+
}
|
|
1297
1964
|
/**
|
|
1298
1965
|
* Retrieves all available tools
|
|
1299
1966
|
* @returns Promise containing map of tool IDs to tool details
|
|
@@ -1307,7 +1974,7 @@ var MastraClient = class extends BaseResource {
|
|
|
1307
1974
|
* @returns Tool instance
|
|
1308
1975
|
*/
|
|
1309
1976
|
getTool(toolId) {
|
|
1310
|
-
return new
|
|
1977
|
+
return new Tool2(this.options, toolId);
|
|
1311
1978
|
}
|
|
1312
1979
|
/**
|
|
1313
1980
|
* Retrieves all available legacy workflows
|
|
@@ -1490,6 +2157,13 @@ var MastraClient = class extends BaseResource {
|
|
|
1490
2157
|
getNetworks() {
|
|
1491
2158
|
return this.request("/api/networks");
|
|
1492
2159
|
}
|
|
2160
|
+
/**
|
|
2161
|
+
* Retrieves all available vNext networks
|
|
2162
|
+
* @returns Promise containing map of vNext network IDs to vNext network details
|
|
2163
|
+
*/
|
|
2164
|
+
getVNextNetworks() {
|
|
2165
|
+
return this.request("/api/networks/v-next");
|
|
2166
|
+
}
|
|
1493
2167
|
/**
|
|
1494
2168
|
* Gets a network instance by ID
|
|
1495
2169
|
* @param networkId - ID of the network to retrieve
|
|
@@ -1498,6 +2172,14 @@ var MastraClient = class extends BaseResource {
|
|
|
1498
2172
|
getNetwork(networkId) {
|
|
1499
2173
|
return new Network(this.options, networkId);
|
|
1500
2174
|
}
|
|
2175
|
+
/**
|
|
2176
|
+
* Gets a vNext network instance by ID
|
|
2177
|
+
* @param networkId - ID of the vNext network to retrieve
|
|
2178
|
+
* @returns vNext Network instance
|
|
2179
|
+
*/
|
|
2180
|
+
getVNextNetwork(networkId) {
|
|
2181
|
+
return new VNextNetwork(this.options, networkId);
|
|
2182
|
+
}
|
|
1501
2183
|
/**
|
|
1502
2184
|
* Retrieves a list of available MCP servers.
|
|
1503
2185
|
* @param params - Optional parameters for pagination (limit, offset).
|