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