@mastra/client-js 0.10.6-alpha.1 → 0.10.6-alpha.3
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 +24 -0
- package/dist/index.cjs +606 -13
- package/dist/index.d.cts +166 -4
- package/dist/index.d.ts +166 -4
- package/dist/index.js +607 -14
- package/package.json +4 -4
- package/src/client.ts +71 -1
- package/src/example.ts +4 -1
- package/src/resources/agent.ts +533 -12
- 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 +147 -0
- package/src/resources/workflow.ts +2 -2
- package/src/types.ts +61 -0
- package/src/utils/process-client-tools.ts +3 -2
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AbstractAgent, EventType } from '@ag-ui/client';
|
|
2
2
|
import { Observable } from 'rxjs';
|
|
3
|
-
import { processDataStream } from '@ai-sdk/ui-utils';
|
|
3
|
+
import { 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';
|
|
@@ -246,6 +246,7 @@ var BaseResource = class {
|
|
|
246
246
|
const response = await fetch(`${baseUrl.replace(/\/$/, "")}${path}`, {
|
|
247
247
|
...options,
|
|
248
248
|
headers: {
|
|
249
|
+
...options.method === "POST" || options.method === "PUT" ? { "content-type": "application/json" } : {},
|
|
249
250
|
...headers,
|
|
250
251
|
...options.headers
|
|
251
252
|
// TODO: Bring this back once we figure out what we/users need to do to make this work with cross-origin requests
|
|
@@ -292,8 +293,6 @@ function parseClientRuntimeContext(runtimeContext) {
|
|
|
292
293
|
}
|
|
293
294
|
return void 0;
|
|
294
295
|
}
|
|
295
|
-
|
|
296
|
-
// src/resources/agent.ts
|
|
297
296
|
var AgentVoice = class extends BaseResource {
|
|
298
297
|
constructor(options, agentId) {
|
|
299
298
|
super(options);
|
|
@@ -362,7 +361,7 @@ var Agent = class extends BaseResource {
|
|
|
362
361
|
details() {
|
|
363
362
|
return this.request(`/api/agents/${this.agentId}`);
|
|
364
363
|
}
|
|
365
|
-
generate(params) {
|
|
364
|
+
async generate(params) {
|
|
366
365
|
const processedParams = {
|
|
367
366
|
...params,
|
|
368
367
|
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
@@ -370,10 +369,310 @@ var Agent = class extends BaseResource {
|
|
|
370
369
|
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
371
370
|
clientTools: processClientTools(params.clientTools)
|
|
372
371
|
};
|
|
373
|
-
|
|
372
|
+
const { runId, resourceId, threadId, runtimeContext } = processedParams;
|
|
373
|
+
const response = await this.request(`/api/agents/${this.agentId}/generate`, {
|
|
374
374
|
method: "POST",
|
|
375
375
|
body: processedParams
|
|
376
376
|
});
|
|
377
|
+
if (response.finishReason === "tool-calls") {
|
|
378
|
+
for (const toolCall of response.toolCalls) {
|
|
379
|
+
const clientTool = params.clientTools?.[toolCall.toolName];
|
|
380
|
+
if (clientTool && clientTool.execute) {
|
|
381
|
+
const result = await clientTool.execute(
|
|
382
|
+
{ context: toolCall?.args, runId, resourceId, threadId, runtimeContext },
|
|
383
|
+
{
|
|
384
|
+
messages: response.messages,
|
|
385
|
+
toolCallId: toolCall?.toolCallId
|
|
386
|
+
}
|
|
387
|
+
);
|
|
388
|
+
const updatedMessages = [
|
|
389
|
+
{
|
|
390
|
+
role: "user",
|
|
391
|
+
content: params.messages
|
|
392
|
+
},
|
|
393
|
+
...response.response.messages,
|
|
394
|
+
{
|
|
395
|
+
role: "tool",
|
|
396
|
+
content: [
|
|
397
|
+
{
|
|
398
|
+
type: "tool-result",
|
|
399
|
+
toolCallId: toolCall.toolCallId,
|
|
400
|
+
toolName: toolCall.toolName,
|
|
401
|
+
result
|
|
402
|
+
}
|
|
403
|
+
]
|
|
404
|
+
}
|
|
405
|
+
];
|
|
406
|
+
return this.generate({
|
|
407
|
+
...params,
|
|
408
|
+
messages: updatedMessages
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
return response;
|
|
414
|
+
}
|
|
415
|
+
async processChatResponse({
|
|
416
|
+
stream,
|
|
417
|
+
update,
|
|
418
|
+
onToolCall,
|
|
419
|
+
onFinish,
|
|
420
|
+
getCurrentDate = () => /* @__PURE__ */ new Date(),
|
|
421
|
+
lastMessage
|
|
422
|
+
}) {
|
|
423
|
+
const replaceLastMessage = lastMessage?.role === "assistant";
|
|
424
|
+
let step = replaceLastMessage ? 1 + // find max step in existing tool invocations:
|
|
425
|
+
(lastMessage.toolInvocations?.reduce((max, toolInvocation) => {
|
|
426
|
+
return Math.max(max, toolInvocation.step ?? 0);
|
|
427
|
+
}, 0) ?? 0) : 0;
|
|
428
|
+
const message = replaceLastMessage ? structuredClone(lastMessage) : {
|
|
429
|
+
id: crypto.randomUUID(),
|
|
430
|
+
createdAt: getCurrentDate(),
|
|
431
|
+
role: "assistant",
|
|
432
|
+
content: "",
|
|
433
|
+
parts: []
|
|
434
|
+
};
|
|
435
|
+
let currentTextPart = void 0;
|
|
436
|
+
let currentReasoningPart = void 0;
|
|
437
|
+
let currentReasoningTextDetail = void 0;
|
|
438
|
+
function updateToolInvocationPart(toolCallId, invocation) {
|
|
439
|
+
const part = message.parts.find(
|
|
440
|
+
(part2) => part2.type === "tool-invocation" && part2.toolInvocation.toolCallId === toolCallId
|
|
441
|
+
);
|
|
442
|
+
if (part != null) {
|
|
443
|
+
part.toolInvocation = invocation;
|
|
444
|
+
} else {
|
|
445
|
+
message.parts.push({
|
|
446
|
+
type: "tool-invocation",
|
|
447
|
+
toolInvocation: invocation
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
const data = [];
|
|
452
|
+
let messageAnnotations = replaceLastMessage ? lastMessage?.annotations : void 0;
|
|
453
|
+
const partialToolCalls = {};
|
|
454
|
+
let usage = {
|
|
455
|
+
completionTokens: NaN,
|
|
456
|
+
promptTokens: NaN,
|
|
457
|
+
totalTokens: NaN
|
|
458
|
+
};
|
|
459
|
+
let finishReason = "unknown";
|
|
460
|
+
function execUpdate() {
|
|
461
|
+
const copiedData = [...data];
|
|
462
|
+
if (messageAnnotations?.length) {
|
|
463
|
+
message.annotations = messageAnnotations;
|
|
464
|
+
}
|
|
465
|
+
const copiedMessage = {
|
|
466
|
+
// deep copy the message to ensure that deep changes (msg attachments) are updated
|
|
467
|
+
// with SolidJS. SolidJS uses referential integration of sub-objects to detect changes.
|
|
468
|
+
...structuredClone(message),
|
|
469
|
+
// add a revision id to ensure that the message is updated with SWR. SWR uses a
|
|
470
|
+
// hashing approach by default to detect changes, but it only works for shallow
|
|
471
|
+
// changes. This is why we need to add a revision id to ensure that the message
|
|
472
|
+
// is updated with SWR (without it, the changes get stuck in SWR and are not
|
|
473
|
+
// forwarded to rendering):
|
|
474
|
+
revisionId: crypto.randomUUID()
|
|
475
|
+
};
|
|
476
|
+
update({
|
|
477
|
+
message: copiedMessage,
|
|
478
|
+
data: copiedData,
|
|
479
|
+
replaceLastMessage
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
await processDataStream({
|
|
483
|
+
stream,
|
|
484
|
+
onTextPart(value) {
|
|
485
|
+
if (currentTextPart == null) {
|
|
486
|
+
currentTextPart = {
|
|
487
|
+
type: "text",
|
|
488
|
+
text: value
|
|
489
|
+
};
|
|
490
|
+
message.parts.push(currentTextPart);
|
|
491
|
+
} else {
|
|
492
|
+
currentTextPart.text += value;
|
|
493
|
+
}
|
|
494
|
+
message.content += value;
|
|
495
|
+
execUpdate();
|
|
496
|
+
},
|
|
497
|
+
onReasoningPart(value) {
|
|
498
|
+
if (currentReasoningTextDetail == null) {
|
|
499
|
+
currentReasoningTextDetail = { type: "text", text: value };
|
|
500
|
+
if (currentReasoningPart != null) {
|
|
501
|
+
currentReasoningPart.details.push(currentReasoningTextDetail);
|
|
502
|
+
}
|
|
503
|
+
} else {
|
|
504
|
+
currentReasoningTextDetail.text += value;
|
|
505
|
+
}
|
|
506
|
+
if (currentReasoningPart == null) {
|
|
507
|
+
currentReasoningPart = {
|
|
508
|
+
type: "reasoning",
|
|
509
|
+
reasoning: value,
|
|
510
|
+
details: [currentReasoningTextDetail]
|
|
511
|
+
};
|
|
512
|
+
message.parts.push(currentReasoningPart);
|
|
513
|
+
} else {
|
|
514
|
+
currentReasoningPart.reasoning += value;
|
|
515
|
+
}
|
|
516
|
+
message.reasoning = (message.reasoning ?? "") + value;
|
|
517
|
+
execUpdate();
|
|
518
|
+
},
|
|
519
|
+
onReasoningSignaturePart(value) {
|
|
520
|
+
if (currentReasoningTextDetail != null) {
|
|
521
|
+
currentReasoningTextDetail.signature = value.signature;
|
|
522
|
+
}
|
|
523
|
+
},
|
|
524
|
+
onRedactedReasoningPart(value) {
|
|
525
|
+
if (currentReasoningPart == null) {
|
|
526
|
+
currentReasoningPart = {
|
|
527
|
+
type: "reasoning",
|
|
528
|
+
reasoning: "",
|
|
529
|
+
details: []
|
|
530
|
+
};
|
|
531
|
+
message.parts.push(currentReasoningPart);
|
|
532
|
+
}
|
|
533
|
+
currentReasoningPart.details.push({
|
|
534
|
+
type: "redacted",
|
|
535
|
+
data: value.data
|
|
536
|
+
});
|
|
537
|
+
currentReasoningTextDetail = void 0;
|
|
538
|
+
execUpdate();
|
|
539
|
+
},
|
|
540
|
+
onFilePart(value) {
|
|
541
|
+
message.parts.push({
|
|
542
|
+
type: "file",
|
|
543
|
+
mimeType: value.mimeType,
|
|
544
|
+
data: value.data
|
|
545
|
+
});
|
|
546
|
+
execUpdate();
|
|
547
|
+
},
|
|
548
|
+
onSourcePart(value) {
|
|
549
|
+
message.parts.push({
|
|
550
|
+
type: "source",
|
|
551
|
+
source: value
|
|
552
|
+
});
|
|
553
|
+
execUpdate();
|
|
554
|
+
},
|
|
555
|
+
onToolCallStreamingStartPart(value) {
|
|
556
|
+
if (message.toolInvocations == null) {
|
|
557
|
+
message.toolInvocations = [];
|
|
558
|
+
}
|
|
559
|
+
partialToolCalls[value.toolCallId] = {
|
|
560
|
+
text: "",
|
|
561
|
+
step,
|
|
562
|
+
toolName: value.toolName,
|
|
563
|
+
index: message.toolInvocations.length
|
|
564
|
+
};
|
|
565
|
+
const invocation = {
|
|
566
|
+
state: "partial-call",
|
|
567
|
+
step,
|
|
568
|
+
toolCallId: value.toolCallId,
|
|
569
|
+
toolName: value.toolName,
|
|
570
|
+
args: void 0
|
|
571
|
+
};
|
|
572
|
+
message.toolInvocations.push(invocation);
|
|
573
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
574
|
+
execUpdate();
|
|
575
|
+
},
|
|
576
|
+
onToolCallDeltaPart(value) {
|
|
577
|
+
const partialToolCall = partialToolCalls[value.toolCallId];
|
|
578
|
+
partialToolCall.text += value.argsTextDelta;
|
|
579
|
+
const { value: partialArgs } = parsePartialJson(partialToolCall.text);
|
|
580
|
+
const invocation = {
|
|
581
|
+
state: "partial-call",
|
|
582
|
+
step: partialToolCall.step,
|
|
583
|
+
toolCallId: value.toolCallId,
|
|
584
|
+
toolName: partialToolCall.toolName,
|
|
585
|
+
args: partialArgs
|
|
586
|
+
};
|
|
587
|
+
message.toolInvocations[partialToolCall.index] = invocation;
|
|
588
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
589
|
+
execUpdate();
|
|
590
|
+
},
|
|
591
|
+
async onToolCallPart(value) {
|
|
592
|
+
const invocation = {
|
|
593
|
+
state: "call",
|
|
594
|
+
step,
|
|
595
|
+
...value
|
|
596
|
+
};
|
|
597
|
+
if (partialToolCalls[value.toolCallId] != null) {
|
|
598
|
+
message.toolInvocations[partialToolCalls[value.toolCallId].index] = invocation;
|
|
599
|
+
} else {
|
|
600
|
+
if (message.toolInvocations == null) {
|
|
601
|
+
message.toolInvocations = [];
|
|
602
|
+
}
|
|
603
|
+
message.toolInvocations.push(invocation);
|
|
604
|
+
}
|
|
605
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
606
|
+
execUpdate();
|
|
607
|
+
if (onToolCall) {
|
|
608
|
+
const result = await onToolCall({ toolCall: value });
|
|
609
|
+
if (result != null) {
|
|
610
|
+
const invocation2 = {
|
|
611
|
+
state: "result",
|
|
612
|
+
step,
|
|
613
|
+
...value,
|
|
614
|
+
result
|
|
615
|
+
};
|
|
616
|
+
message.toolInvocations[message.toolInvocations.length - 1] = invocation2;
|
|
617
|
+
updateToolInvocationPart(value.toolCallId, invocation2);
|
|
618
|
+
execUpdate();
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
},
|
|
622
|
+
onToolResultPart(value) {
|
|
623
|
+
const toolInvocations = message.toolInvocations;
|
|
624
|
+
if (toolInvocations == null) {
|
|
625
|
+
throw new Error("tool_result must be preceded by a tool_call");
|
|
626
|
+
}
|
|
627
|
+
const toolInvocationIndex = toolInvocations.findIndex((invocation2) => invocation2.toolCallId === value.toolCallId);
|
|
628
|
+
if (toolInvocationIndex === -1) {
|
|
629
|
+
throw new Error("tool_result must be preceded by a tool_call with the same toolCallId");
|
|
630
|
+
}
|
|
631
|
+
const invocation = {
|
|
632
|
+
...toolInvocations[toolInvocationIndex],
|
|
633
|
+
state: "result",
|
|
634
|
+
...value
|
|
635
|
+
};
|
|
636
|
+
toolInvocations[toolInvocationIndex] = invocation;
|
|
637
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
638
|
+
execUpdate();
|
|
639
|
+
},
|
|
640
|
+
onDataPart(value) {
|
|
641
|
+
data.push(...value);
|
|
642
|
+
execUpdate();
|
|
643
|
+
},
|
|
644
|
+
onMessageAnnotationsPart(value) {
|
|
645
|
+
if (messageAnnotations == null) {
|
|
646
|
+
messageAnnotations = [...value];
|
|
647
|
+
} else {
|
|
648
|
+
messageAnnotations.push(...value);
|
|
649
|
+
}
|
|
650
|
+
execUpdate();
|
|
651
|
+
},
|
|
652
|
+
onFinishStepPart(value) {
|
|
653
|
+
step += 1;
|
|
654
|
+
currentTextPart = value.isContinued ? currentTextPart : void 0;
|
|
655
|
+
currentReasoningPart = void 0;
|
|
656
|
+
currentReasoningTextDetail = void 0;
|
|
657
|
+
},
|
|
658
|
+
onStartStepPart(value) {
|
|
659
|
+
if (!replaceLastMessage) {
|
|
660
|
+
message.id = value.messageId;
|
|
661
|
+
}
|
|
662
|
+
message.parts.push({ type: "step-start" });
|
|
663
|
+
execUpdate();
|
|
664
|
+
},
|
|
665
|
+
onFinishMessagePart(value) {
|
|
666
|
+
finishReason = value.finishReason;
|
|
667
|
+
if (value.usage != null) {
|
|
668
|
+
usage = value.usage;
|
|
669
|
+
}
|
|
670
|
+
},
|
|
671
|
+
onErrorPart(error) {
|
|
672
|
+
throw new Error(error);
|
|
673
|
+
}
|
|
674
|
+
});
|
|
675
|
+
onFinish?.({ message, finishReason, usage });
|
|
377
676
|
}
|
|
378
677
|
/**
|
|
379
678
|
* Streams a response from the agent
|
|
@@ -388,6 +687,25 @@ var Agent = class extends BaseResource {
|
|
|
388
687
|
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
389
688
|
clientTools: processClientTools(params.clientTools)
|
|
390
689
|
};
|
|
690
|
+
const { readable, writable } = new TransformStream();
|
|
691
|
+
const response = await this.processStreamResponse(processedParams, writable);
|
|
692
|
+
const streamResponse = new Response(readable, {
|
|
693
|
+
status: response.status,
|
|
694
|
+
statusText: response.statusText,
|
|
695
|
+
headers: response.headers
|
|
696
|
+
});
|
|
697
|
+
streamResponse.processDataStream = async (options = {}) => {
|
|
698
|
+
await processDataStream({
|
|
699
|
+
stream: readable,
|
|
700
|
+
...options
|
|
701
|
+
});
|
|
702
|
+
};
|
|
703
|
+
return streamResponse;
|
|
704
|
+
}
|
|
705
|
+
/**
|
|
706
|
+
* Processes the stream response and handles tool calls
|
|
707
|
+
*/
|
|
708
|
+
async processStreamResponse(processedParams, writable) {
|
|
391
709
|
const response = await this.request(`/api/agents/${this.agentId}/stream`, {
|
|
392
710
|
method: "POST",
|
|
393
711
|
body: processedParams,
|
|
@@ -396,12 +714,81 @@ var Agent = class extends BaseResource {
|
|
|
396
714
|
if (!response.body) {
|
|
397
715
|
throw new Error("No response body");
|
|
398
716
|
}
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
717
|
+
try {
|
|
718
|
+
let toolCalls = [];
|
|
719
|
+
let finishReasonToolCalls = false;
|
|
720
|
+
let messages = [];
|
|
721
|
+
let hasProcessedToolCalls = false;
|
|
722
|
+
response.clone().body.pipeTo(writable, {
|
|
723
|
+
preventClose: true
|
|
403
724
|
});
|
|
404
|
-
|
|
725
|
+
await this.processChatResponse({
|
|
726
|
+
stream: response.clone().body,
|
|
727
|
+
update: ({ message }) => {
|
|
728
|
+
messages.push(message);
|
|
729
|
+
},
|
|
730
|
+
onFinish: ({ finishReason, message }) => {
|
|
731
|
+
if (finishReason === "tool-calls") {
|
|
732
|
+
finishReasonToolCalls = true;
|
|
733
|
+
const toolCall = [...message?.parts ?? []].reverse().find((part) => part.type === "tool-invocation")?.toolInvocation;
|
|
734
|
+
if (toolCall) {
|
|
735
|
+
toolCalls.push(toolCall);
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
},
|
|
739
|
+
lastMessage: void 0
|
|
740
|
+
});
|
|
741
|
+
if (finishReasonToolCalls && !hasProcessedToolCalls) {
|
|
742
|
+
hasProcessedToolCalls = true;
|
|
743
|
+
for (const toolCall of toolCalls) {
|
|
744
|
+
const clientTool = processedParams.clientTools?.[toolCall.toolName];
|
|
745
|
+
if (clientTool && clientTool.execute) {
|
|
746
|
+
const result = await clientTool.execute(
|
|
747
|
+
{
|
|
748
|
+
context: toolCall?.args,
|
|
749
|
+
runId: processedParams.runId,
|
|
750
|
+
resourceId: processedParams.resourceId,
|
|
751
|
+
threadId: processedParams.threadId,
|
|
752
|
+
runtimeContext: processedParams.runtimeContext
|
|
753
|
+
},
|
|
754
|
+
{
|
|
755
|
+
messages: response.messages,
|
|
756
|
+
toolCallId: toolCall?.toolCallId
|
|
757
|
+
}
|
|
758
|
+
);
|
|
759
|
+
const lastMessage = JSON.parse(JSON.stringify(messages[messages.length - 1]));
|
|
760
|
+
const toolInvocationPart = lastMessage?.parts?.find(
|
|
761
|
+
(part) => part.type === "tool-invocation" && part.toolInvocation?.toolCallId === toolCall.toolCallId
|
|
762
|
+
);
|
|
763
|
+
if (toolInvocationPart) {
|
|
764
|
+
toolInvocationPart.toolInvocation = {
|
|
765
|
+
...toolInvocationPart.toolInvocation,
|
|
766
|
+
state: "result",
|
|
767
|
+
result
|
|
768
|
+
};
|
|
769
|
+
}
|
|
770
|
+
const toolInvocation = lastMessage?.toolInvocations?.find(
|
|
771
|
+
(toolInvocation2) => toolInvocation2.toolCallId === toolCall.toolCallId
|
|
772
|
+
);
|
|
773
|
+
if (toolInvocation) {
|
|
774
|
+
toolInvocation.state = "result";
|
|
775
|
+
toolInvocation.result = result;
|
|
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
|
+
}
|
|
789
|
+
} catch (error) {
|
|
790
|
+
console.error("Error processing stream response:", error);
|
|
791
|
+
}
|
|
405
792
|
return response;
|
|
406
793
|
}
|
|
407
794
|
/**
|
|
@@ -796,7 +1183,7 @@ var LegacyWorkflow = class extends BaseResource {
|
|
|
796
1183
|
};
|
|
797
1184
|
|
|
798
1185
|
// src/resources/tool.ts
|
|
799
|
-
var
|
|
1186
|
+
var Tool2 = class extends BaseResource {
|
|
800
1187
|
constructor(options, toolId) {
|
|
801
1188
|
super(options);
|
|
802
1189
|
this.toolId = toolId;
|
|
@@ -997,9 +1384,9 @@ var Workflow = class extends BaseResource {
|
|
|
997
1384
|
});
|
|
998
1385
|
}
|
|
999
1386
|
/**
|
|
1000
|
-
* Starts a
|
|
1387
|
+
* Starts a workflow run and returns a stream
|
|
1001
1388
|
* @param params - Object containing the optional runId, inputData and runtimeContext
|
|
1002
|
-
* @returns Promise containing the
|
|
1389
|
+
* @returns Promise containing the workflow execution results
|
|
1003
1390
|
*/
|
|
1004
1391
|
async stream(params) {
|
|
1005
1392
|
const searchParams = new URLSearchParams();
|
|
@@ -1215,6 +1602,155 @@ var MCPTool = class extends BaseResource {
|
|
|
1215
1602
|
}
|
|
1216
1603
|
};
|
|
1217
1604
|
|
|
1605
|
+
// src/resources/vNextNetwork.ts
|
|
1606
|
+
var RECORD_SEPARATOR3 = "";
|
|
1607
|
+
var VNextNetwork = class extends BaseResource {
|
|
1608
|
+
constructor(options, networkId) {
|
|
1609
|
+
super(options);
|
|
1610
|
+
this.networkId = networkId;
|
|
1611
|
+
}
|
|
1612
|
+
/**
|
|
1613
|
+
* Retrieves details about the network
|
|
1614
|
+
* @returns Promise containing vNext network details
|
|
1615
|
+
*/
|
|
1616
|
+
details() {
|
|
1617
|
+
return this.request(`/api/networks/v-next/${this.networkId}`);
|
|
1618
|
+
}
|
|
1619
|
+
/**
|
|
1620
|
+
* Generates a response from the v-next network
|
|
1621
|
+
* @param params - Generation parameters including message
|
|
1622
|
+
* @returns Promise containing the generated response
|
|
1623
|
+
*/
|
|
1624
|
+
generate(params) {
|
|
1625
|
+
return this.request(`/api/networks/v-next/${this.networkId}/generate`, {
|
|
1626
|
+
method: "POST",
|
|
1627
|
+
body: params
|
|
1628
|
+
});
|
|
1629
|
+
}
|
|
1630
|
+
/**
|
|
1631
|
+
* Generates a response from the v-next network using multiple primitives
|
|
1632
|
+
* @param params - Generation parameters including message
|
|
1633
|
+
* @returns Promise containing the generated response
|
|
1634
|
+
*/
|
|
1635
|
+
loop(params) {
|
|
1636
|
+
return this.request(`/api/networks/v-next/${this.networkId}/loop`, {
|
|
1637
|
+
method: "POST",
|
|
1638
|
+
body: params
|
|
1639
|
+
});
|
|
1640
|
+
}
|
|
1641
|
+
async *streamProcessor(stream) {
|
|
1642
|
+
const reader = stream.getReader();
|
|
1643
|
+
let doneReading = false;
|
|
1644
|
+
let buffer = "";
|
|
1645
|
+
try {
|
|
1646
|
+
while (!doneReading) {
|
|
1647
|
+
const { done, value } = await reader.read();
|
|
1648
|
+
doneReading = done;
|
|
1649
|
+
if (done && !value) continue;
|
|
1650
|
+
try {
|
|
1651
|
+
const decoded = value ? new TextDecoder().decode(value) : "";
|
|
1652
|
+
const chunks = (buffer + decoded).split(RECORD_SEPARATOR3);
|
|
1653
|
+
buffer = chunks.pop() || "";
|
|
1654
|
+
for (const chunk of chunks) {
|
|
1655
|
+
if (chunk) {
|
|
1656
|
+
if (typeof chunk === "string") {
|
|
1657
|
+
try {
|
|
1658
|
+
const parsedChunk = JSON.parse(chunk);
|
|
1659
|
+
yield parsedChunk;
|
|
1660
|
+
} catch {
|
|
1661
|
+
}
|
|
1662
|
+
}
|
|
1663
|
+
}
|
|
1664
|
+
}
|
|
1665
|
+
} catch {
|
|
1666
|
+
}
|
|
1667
|
+
}
|
|
1668
|
+
if (buffer) {
|
|
1669
|
+
try {
|
|
1670
|
+
yield JSON.parse(buffer);
|
|
1671
|
+
} catch {
|
|
1672
|
+
}
|
|
1673
|
+
}
|
|
1674
|
+
} finally {
|
|
1675
|
+
reader.cancel().catch(() => {
|
|
1676
|
+
});
|
|
1677
|
+
}
|
|
1678
|
+
}
|
|
1679
|
+
/**
|
|
1680
|
+
* Streams a response from the v-next network
|
|
1681
|
+
* @param params - Stream parameters including message
|
|
1682
|
+
* @returns Promise containing the results
|
|
1683
|
+
*/
|
|
1684
|
+
async stream(params, onRecord) {
|
|
1685
|
+
const response = await this.request(`/api/networks/v-next/${this.networkId}/stream`, {
|
|
1686
|
+
method: "POST",
|
|
1687
|
+
body: params,
|
|
1688
|
+
stream: true
|
|
1689
|
+
});
|
|
1690
|
+
if (!response.ok) {
|
|
1691
|
+
throw new Error(`Failed to stream vNext network: ${response.statusText}`);
|
|
1692
|
+
}
|
|
1693
|
+
if (!response.body) {
|
|
1694
|
+
throw new Error("Response body is null");
|
|
1695
|
+
}
|
|
1696
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
1697
|
+
if (typeof record === "string") {
|
|
1698
|
+
onRecord(JSON.parse(record));
|
|
1699
|
+
} else {
|
|
1700
|
+
onRecord(record);
|
|
1701
|
+
}
|
|
1702
|
+
}
|
|
1703
|
+
}
|
|
1704
|
+
};
|
|
1705
|
+
|
|
1706
|
+
// src/resources/network-memory-thread.ts
|
|
1707
|
+
var NetworkMemoryThread = class extends BaseResource {
|
|
1708
|
+
constructor(options, threadId, networkId) {
|
|
1709
|
+
super(options);
|
|
1710
|
+
this.threadId = threadId;
|
|
1711
|
+
this.networkId = networkId;
|
|
1712
|
+
}
|
|
1713
|
+
/**
|
|
1714
|
+
* Retrieves the memory thread details
|
|
1715
|
+
* @returns Promise containing thread details including title and metadata
|
|
1716
|
+
*/
|
|
1717
|
+
get() {
|
|
1718
|
+
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`);
|
|
1719
|
+
}
|
|
1720
|
+
/**
|
|
1721
|
+
* Updates the memory thread properties
|
|
1722
|
+
* @param params - Update parameters including title and metadata
|
|
1723
|
+
* @returns Promise containing updated thread details
|
|
1724
|
+
*/
|
|
1725
|
+
update(params) {
|
|
1726
|
+
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
|
|
1727
|
+
method: "PATCH",
|
|
1728
|
+
body: params
|
|
1729
|
+
});
|
|
1730
|
+
}
|
|
1731
|
+
/**
|
|
1732
|
+
* Deletes the memory thread
|
|
1733
|
+
* @returns Promise containing deletion result
|
|
1734
|
+
*/
|
|
1735
|
+
delete() {
|
|
1736
|
+
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
|
|
1737
|
+
method: "DELETE"
|
|
1738
|
+
});
|
|
1739
|
+
}
|
|
1740
|
+
/**
|
|
1741
|
+
* Retrieves messages associated with the thread
|
|
1742
|
+
* @param params - Optional parameters including limit for number of messages to retrieve
|
|
1743
|
+
* @returns Promise containing thread messages and UI messages
|
|
1744
|
+
*/
|
|
1745
|
+
getMessages(params) {
|
|
1746
|
+
const query = new URLSearchParams({
|
|
1747
|
+
networkId: this.networkId,
|
|
1748
|
+
...params?.limit ? { limit: params.limit.toString() } : {}
|
|
1749
|
+
});
|
|
1750
|
+
return this.request(`/api/memory/network/threads/${this.threadId}/messages?${query.toString()}`);
|
|
1751
|
+
}
|
|
1752
|
+
};
|
|
1753
|
+
|
|
1218
1754
|
// src/client.ts
|
|
1219
1755
|
var MastraClient = class extends BaseResource {
|
|
1220
1756
|
constructor(options) {
|
|
@@ -1292,6 +1828,48 @@ var MastraClient = class extends BaseResource {
|
|
|
1292
1828
|
getMemoryStatus(agentId) {
|
|
1293
1829
|
return this.request(`/api/memory/status?agentId=${agentId}`);
|
|
1294
1830
|
}
|
|
1831
|
+
/**
|
|
1832
|
+
* Retrieves memory threads for a resource
|
|
1833
|
+
* @param params - Parameters containing the resource ID
|
|
1834
|
+
* @returns Promise containing array of memory threads
|
|
1835
|
+
*/
|
|
1836
|
+
getNetworkMemoryThreads(params) {
|
|
1837
|
+
return this.request(`/api/memory/network/threads?resourceid=${params.resourceId}&networkId=${params.networkId}`);
|
|
1838
|
+
}
|
|
1839
|
+
/**
|
|
1840
|
+
* Creates a new memory thread
|
|
1841
|
+
* @param params - Parameters for creating the memory thread
|
|
1842
|
+
* @returns Promise containing the created memory thread
|
|
1843
|
+
*/
|
|
1844
|
+
createNetworkMemoryThread(params) {
|
|
1845
|
+
return this.request(`/api/memory/network/threads?networkId=${params.networkId}`, { method: "POST", body: params });
|
|
1846
|
+
}
|
|
1847
|
+
/**
|
|
1848
|
+
* Gets a memory thread instance by ID
|
|
1849
|
+
* @param threadId - ID of the memory thread to retrieve
|
|
1850
|
+
* @returns MemoryThread instance
|
|
1851
|
+
*/
|
|
1852
|
+
getNetworkMemoryThread(threadId, networkId) {
|
|
1853
|
+
return new NetworkMemoryThread(this.options, threadId, networkId);
|
|
1854
|
+
}
|
|
1855
|
+
/**
|
|
1856
|
+
* Saves messages to memory
|
|
1857
|
+
* @param params - Parameters containing messages to save
|
|
1858
|
+
* @returns Promise containing the saved messages
|
|
1859
|
+
*/
|
|
1860
|
+
saveNetworkMessageToMemory(params) {
|
|
1861
|
+
return this.request(`/api/memory/network/save-messages?networkId=${params.networkId}`, {
|
|
1862
|
+
method: "POST",
|
|
1863
|
+
body: params
|
|
1864
|
+
});
|
|
1865
|
+
}
|
|
1866
|
+
/**
|
|
1867
|
+
* Gets the status of the memory system
|
|
1868
|
+
* @returns Promise containing memory system status
|
|
1869
|
+
*/
|
|
1870
|
+
getNetworkMemoryStatus(networkId) {
|
|
1871
|
+
return this.request(`/api/memory/network/status?networkId=${networkId}`);
|
|
1872
|
+
}
|
|
1295
1873
|
/**
|
|
1296
1874
|
* Retrieves all available tools
|
|
1297
1875
|
* @returns Promise containing map of tool IDs to tool details
|
|
@@ -1305,7 +1883,7 @@ var MastraClient = class extends BaseResource {
|
|
|
1305
1883
|
* @returns Tool instance
|
|
1306
1884
|
*/
|
|
1307
1885
|
getTool(toolId) {
|
|
1308
|
-
return new
|
|
1886
|
+
return new Tool2(this.options, toolId);
|
|
1309
1887
|
}
|
|
1310
1888
|
/**
|
|
1311
1889
|
* Retrieves all available legacy workflows
|
|
@@ -1488,6 +2066,13 @@ var MastraClient = class extends BaseResource {
|
|
|
1488
2066
|
getNetworks() {
|
|
1489
2067
|
return this.request("/api/networks");
|
|
1490
2068
|
}
|
|
2069
|
+
/**
|
|
2070
|
+
* Retrieves all available vNext networks
|
|
2071
|
+
* @returns Promise containing map of vNext network IDs to vNext network details
|
|
2072
|
+
*/
|
|
2073
|
+
getVNextNetworks() {
|
|
2074
|
+
return this.request("/api/networks/v-next");
|
|
2075
|
+
}
|
|
1491
2076
|
/**
|
|
1492
2077
|
* Gets a network instance by ID
|
|
1493
2078
|
* @param networkId - ID of the network to retrieve
|
|
@@ -1496,6 +2081,14 @@ var MastraClient = class extends BaseResource {
|
|
|
1496
2081
|
getNetwork(networkId) {
|
|
1497
2082
|
return new Network(this.options, networkId);
|
|
1498
2083
|
}
|
|
2084
|
+
/**
|
|
2085
|
+
* Gets a vNext network instance by ID
|
|
2086
|
+
* @param networkId - ID of the vNext network to retrieve
|
|
2087
|
+
* @returns vNext Network instance
|
|
2088
|
+
*/
|
|
2089
|
+
getVNextNetwork(networkId) {
|
|
2090
|
+
return new VNextNetwork(this.options, networkId);
|
|
2091
|
+
}
|
|
1499
2092
|
/**
|
|
1500
2093
|
* Retrieves a list of available MCP servers.
|
|
1501
2094
|
* @param params - Optional parameters for pagination (limit, offset).
|