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