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