@mastra/client-js 0.0.0-cli-debug-2-20250611100354 → 0.0.0-cloudflare-deployer-dont-install-deps-20250714111754
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 +397 -2
- package/LICENSE.md +11 -42
- package/README.md +1 -1
- package/dist/index.cjs +769 -22
- package/dist/index.d.cts +268 -12
- package/dist/index.d.ts +268 -12
- package/dist/index.js +770 -23
- package/package.json +19 -13
- package/src/client.ts +117 -1
- package/src/example.ts +46 -15
- package/src/resources/agent.ts +604 -21
- package/src/resources/base.ts +2 -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 +49 -6
- package/src/types.ts +97 -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 }; }
|
|
@@ -252,11 +253,13 @@ var BaseResource = class {
|
|
|
252
253
|
const response = await fetch(`${baseUrl.replace(/\/$/, "")}${path}`, {
|
|
253
254
|
...options,
|
|
254
255
|
headers: {
|
|
256
|
+
...options.method === "POST" || options.method === "PUT" ? { "content-type": "application/json" } : {},
|
|
255
257
|
...headers,
|
|
256
258
|
...options.headers
|
|
257
259
|
// TODO: Bring this back once we figure out what we/users need to do to make this work with cross-origin requests
|
|
258
260
|
// 'x-mastra-client-type': 'js',
|
|
259
261
|
},
|
|
262
|
+
signal: this.options.abortSignal,
|
|
260
263
|
body: options.body instanceof FormData ? options.body : options.body ? JSON.stringify(options.body) : void 0
|
|
261
264
|
});
|
|
262
265
|
if (!response.ok) {
|
|
@@ -298,8 +301,6 @@ function parseClientRuntimeContext(runtimeContext$1) {
|
|
|
298
301
|
}
|
|
299
302
|
return void 0;
|
|
300
303
|
}
|
|
301
|
-
|
|
302
|
-
// src/resources/agent.ts
|
|
303
304
|
var AgentVoice = class extends BaseResource {
|
|
304
305
|
constructor(options, agentId) {
|
|
305
306
|
super(options);
|
|
@@ -368,7 +369,7 @@ var Agent = class extends BaseResource {
|
|
|
368
369
|
details() {
|
|
369
370
|
return this.request(`/api/agents/${this.agentId}`);
|
|
370
371
|
}
|
|
371
|
-
generate(params) {
|
|
372
|
+
async generate(params) {
|
|
372
373
|
const processedParams = {
|
|
373
374
|
...params,
|
|
374
375
|
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
@@ -376,15 +377,440 @@ var Agent = class extends BaseResource {
|
|
|
376
377
|
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
377
378
|
clientTools: processClientTools(params.clientTools)
|
|
378
379
|
};
|
|
379
|
-
|
|
380
|
+
const { runId, resourceId, threadId, runtimeContext } = processedParams;
|
|
381
|
+
const response = await this.request(`/api/agents/${this.agentId}/generate`, {
|
|
380
382
|
method: "POST",
|
|
381
383
|
body: processedParams
|
|
382
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;
|
|
383
809
|
}
|
|
384
810
|
/**
|
|
385
811
|
* Streams a response from the agent
|
|
386
812
|
* @param params - Stream parameters including prompt
|
|
387
|
-
* @returns Promise containing the enhanced Response object with processDataStream
|
|
813
|
+
* @returns Promise containing the enhanced Response object with processDataStream and processTextStream methods
|
|
388
814
|
*/
|
|
389
815
|
async stream(params) {
|
|
390
816
|
const processedParams = {
|
|
@@ -394,21 +820,27 @@ var Agent = class extends BaseResource {
|
|
|
394
820
|
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
395
821
|
clientTools: processClientTools(params.clientTools)
|
|
396
822
|
};
|
|
397
|
-
const
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
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
|
|
401
829
|
});
|
|
402
|
-
|
|
403
|
-
throw new Error("No response body");
|
|
404
|
-
}
|
|
405
|
-
response.processDataStream = async (options = {}) => {
|
|
830
|
+
streamResponse.processDataStream = async (options = {}) => {
|
|
406
831
|
await uiUtils.processDataStream({
|
|
407
|
-
stream:
|
|
832
|
+
stream: streamResponse.body,
|
|
408
833
|
...options
|
|
409
834
|
});
|
|
410
835
|
};
|
|
411
|
-
|
|
836
|
+
streamResponse.processTextStream = async (options) => {
|
|
837
|
+
await uiUtils.processTextStream({
|
|
838
|
+
stream: streamResponse.body,
|
|
839
|
+
onTextPart: options?.onTextPart ?? (() => {
|
|
840
|
+
})
|
|
841
|
+
});
|
|
842
|
+
};
|
|
843
|
+
return streamResponse;
|
|
412
844
|
}
|
|
413
845
|
/**
|
|
414
846
|
* Gets details about a specific tool available to the agent
|
|
@@ -802,7 +1234,7 @@ var LegacyWorkflow = class extends BaseResource {
|
|
|
802
1234
|
};
|
|
803
1235
|
|
|
804
1236
|
// src/resources/tool.ts
|
|
805
|
-
var
|
|
1237
|
+
var Tool2 = class extends BaseResource {
|
|
806
1238
|
constructor(options, toolId) {
|
|
807
1239
|
super(options);
|
|
808
1240
|
this.toolId = toolId;
|
|
@@ -907,10 +1339,10 @@ var Workflow = class extends BaseResource {
|
|
|
907
1339
|
if (params?.toDate) {
|
|
908
1340
|
searchParams.set("toDate", params.toDate.toISOString());
|
|
909
1341
|
}
|
|
910
|
-
if (params?.limit) {
|
|
1342
|
+
if (params?.limit !== null && params?.limit !== void 0 && !isNaN(Number(params?.limit))) {
|
|
911
1343
|
searchParams.set("limit", String(params.limit));
|
|
912
1344
|
}
|
|
913
|
-
if (params?.offset) {
|
|
1345
|
+
if (params?.offset !== null && params?.offset !== void 0 && !isNaN(Number(params?.offset))) {
|
|
914
1346
|
searchParams.set("offset", String(params.offset));
|
|
915
1347
|
}
|
|
916
1348
|
if (params?.resourceId) {
|
|
@@ -922,6 +1354,43 @@ var Workflow = class extends BaseResource {
|
|
|
922
1354
|
return this.request(`/api/workflows/${this.workflowId}/runs`);
|
|
923
1355
|
}
|
|
924
1356
|
}
|
|
1357
|
+
/**
|
|
1358
|
+
* Retrieves a specific workflow run by its ID
|
|
1359
|
+
* @param runId - The ID of the workflow run to retrieve
|
|
1360
|
+
* @returns Promise containing the workflow run details
|
|
1361
|
+
*/
|
|
1362
|
+
runById(runId) {
|
|
1363
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}`);
|
|
1364
|
+
}
|
|
1365
|
+
/**
|
|
1366
|
+
* Retrieves the execution result for a specific workflow run by its ID
|
|
1367
|
+
* @param runId - The ID of the workflow run to retrieve the execution result for
|
|
1368
|
+
* @returns Promise containing the workflow run execution result
|
|
1369
|
+
*/
|
|
1370
|
+
runExecutionResult(runId) {
|
|
1371
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}/execution-result`);
|
|
1372
|
+
}
|
|
1373
|
+
/**
|
|
1374
|
+
* Cancels a specific workflow run by its ID
|
|
1375
|
+
* @param runId - The ID of the workflow run to cancel
|
|
1376
|
+
* @returns Promise containing a success message
|
|
1377
|
+
*/
|
|
1378
|
+
cancelRun(runId) {
|
|
1379
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}/cancel`, {
|
|
1380
|
+
method: "POST"
|
|
1381
|
+
});
|
|
1382
|
+
}
|
|
1383
|
+
/**
|
|
1384
|
+
* Sends an event to a specific workflow run by its ID
|
|
1385
|
+
* @param params - Object containing the runId, event and data
|
|
1386
|
+
* @returns Promise containing a success message
|
|
1387
|
+
*/
|
|
1388
|
+
sendRunEvent(params) {
|
|
1389
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${params.runId}/send-event`, {
|
|
1390
|
+
method: "POST",
|
|
1391
|
+
body: { event: params.event, data: params.data }
|
|
1392
|
+
});
|
|
1393
|
+
}
|
|
925
1394
|
/**
|
|
926
1395
|
* Creates a new workflow run
|
|
927
1396
|
* @param params - Optional object containing the optional runId
|
|
@@ -987,16 +1456,16 @@ var Workflow = class extends BaseResource {
|
|
|
987
1456
|
});
|
|
988
1457
|
}
|
|
989
1458
|
/**
|
|
990
|
-
* Starts a
|
|
1459
|
+
* Starts a workflow run and returns a stream
|
|
991
1460
|
* @param params - Object containing the optional runId, inputData and runtimeContext
|
|
992
|
-
* @returns Promise containing the
|
|
1461
|
+
* @returns Promise containing the workflow execution results
|
|
993
1462
|
*/
|
|
994
1463
|
async stream(params) {
|
|
995
1464
|
const searchParams = new URLSearchParams();
|
|
996
1465
|
if (!!params?.runId) {
|
|
997
1466
|
searchParams.set("runId", params.runId);
|
|
998
1467
|
}
|
|
999
|
-
const runtimeContext =
|
|
1468
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
1000
1469
|
const response = await this.request(
|
|
1001
1470
|
`/api/workflows/${this.workflowId}/stream?${searchParams.toString()}`,
|
|
1002
1471
|
{
|
|
@@ -1205,6 +1674,192 @@ var MCPTool = class extends BaseResource {
|
|
|
1205
1674
|
}
|
|
1206
1675
|
};
|
|
1207
1676
|
|
|
1677
|
+
// src/resources/network-memory-thread.ts
|
|
1678
|
+
var NetworkMemoryThread = class extends BaseResource {
|
|
1679
|
+
constructor(options, threadId, networkId) {
|
|
1680
|
+
super(options);
|
|
1681
|
+
this.threadId = threadId;
|
|
1682
|
+
this.networkId = networkId;
|
|
1683
|
+
}
|
|
1684
|
+
/**
|
|
1685
|
+
* Retrieves the memory thread details
|
|
1686
|
+
* @returns Promise containing thread details including title and metadata
|
|
1687
|
+
*/
|
|
1688
|
+
get() {
|
|
1689
|
+
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`);
|
|
1690
|
+
}
|
|
1691
|
+
/**
|
|
1692
|
+
* Updates the memory thread properties
|
|
1693
|
+
* @param params - Update parameters including title and metadata
|
|
1694
|
+
* @returns Promise containing updated thread details
|
|
1695
|
+
*/
|
|
1696
|
+
update(params) {
|
|
1697
|
+
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
|
|
1698
|
+
method: "PATCH",
|
|
1699
|
+
body: params
|
|
1700
|
+
});
|
|
1701
|
+
}
|
|
1702
|
+
/**
|
|
1703
|
+
* Deletes the memory thread
|
|
1704
|
+
* @returns Promise containing deletion result
|
|
1705
|
+
*/
|
|
1706
|
+
delete() {
|
|
1707
|
+
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
|
|
1708
|
+
method: "DELETE"
|
|
1709
|
+
});
|
|
1710
|
+
}
|
|
1711
|
+
/**
|
|
1712
|
+
* Retrieves messages associated with the thread
|
|
1713
|
+
* @param params - Optional parameters including limit for number of messages to retrieve
|
|
1714
|
+
* @returns Promise containing thread messages and UI messages
|
|
1715
|
+
*/
|
|
1716
|
+
getMessages(params) {
|
|
1717
|
+
const query = new URLSearchParams({
|
|
1718
|
+
networkId: this.networkId,
|
|
1719
|
+
...params?.limit ? { limit: params.limit.toString() } : {}
|
|
1720
|
+
});
|
|
1721
|
+
return this.request(`/api/memory/network/threads/${this.threadId}/messages?${query.toString()}`);
|
|
1722
|
+
}
|
|
1723
|
+
};
|
|
1724
|
+
|
|
1725
|
+
// src/resources/vNextNetwork.ts
|
|
1726
|
+
var RECORD_SEPARATOR3 = "";
|
|
1727
|
+
var VNextNetwork = class extends BaseResource {
|
|
1728
|
+
constructor(options, networkId) {
|
|
1729
|
+
super(options);
|
|
1730
|
+
this.networkId = networkId;
|
|
1731
|
+
}
|
|
1732
|
+
/**
|
|
1733
|
+
* Retrieves details about the network
|
|
1734
|
+
* @returns Promise containing vNext network details
|
|
1735
|
+
*/
|
|
1736
|
+
details() {
|
|
1737
|
+
return this.request(`/api/networks/v-next/${this.networkId}`);
|
|
1738
|
+
}
|
|
1739
|
+
/**
|
|
1740
|
+
* Generates a response from the v-next network
|
|
1741
|
+
* @param params - Generation parameters including message
|
|
1742
|
+
* @returns Promise containing the generated response
|
|
1743
|
+
*/
|
|
1744
|
+
generate(params) {
|
|
1745
|
+
return this.request(`/api/networks/v-next/${this.networkId}/generate`, {
|
|
1746
|
+
method: "POST",
|
|
1747
|
+
body: {
|
|
1748
|
+
...params,
|
|
1749
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1750
|
+
}
|
|
1751
|
+
});
|
|
1752
|
+
}
|
|
1753
|
+
/**
|
|
1754
|
+
* Generates a response from the v-next network using multiple primitives
|
|
1755
|
+
* @param params - Generation parameters including message
|
|
1756
|
+
* @returns Promise containing the generated response
|
|
1757
|
+
*/
|
|
1758
|
+
loop(params) {
|
|
1759
|
+
return this.request(`/api/networks/v-next/${this.networkId}/loop`, {
|
|
1760
|
+
method: "POST",
|
|
1761
|
+
body: {
|
|
1762
|
+
...params,
|
|
1763
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1764
|
+
}
|
|
1765
|
+
});
|
|
1766
|
+
}
|
|
1767
|
+
async *streamProcessor(stream) {
|
|
1768
|
+
const reader = stream.getReader();
|
|
1769
|
+
let doneReading = false;
|
|
1770
|
+
let buffer = "";
|
|
1771
|
+
try {
|
|
1772
|
+
while (!doneReading) {
|
|
1773
|
+
const { done, value } = await reader.read();
|
|
1774
|
+
doneReading = done;
|
|
1775
|
+
if (done && !value) continue;
|
|
1776
|
+
try {
|
|
1777
|
+
const decoded = value ? new TextDecoder().decode(value) : "";
|
|
1778
|
+
const chunks = (buffer + decoded).split(RECORD_SEPARATOR3);
|
|
1779
|
+
buffer = chunks.pop() || "";
|
|
1780
|
+
for (const chunk of chunks) {
|
|
1781
|
+
if (chunk) {
|
|
1782
|
+
if (typeof chunk === "string") {
|
|
1783
|
+
try {
|
|
1784
|
+
const parsedChunk = JSON.parse(chunk);
|
|
1785
|
+
yield parsedChunk;
|
|
1786
|
+
} catch {
|
|
1787
|
+
}
|
|
1788
|
+
}
|
|
1789
|
+
}
|
|
1790
|
+
}
|
|
1791
|
+
} catch {
|
|
1792
|
+
}
|
|
1793
|
+
}
|
|
1794
|
+
if (buffer) {
|
|
1795
|
+
try {
|
|
1796
|
+
yield JSON.parse(buffer);
|
|
1797
|
+
} catch {
|
|
1798
|
+
}
|
|
1799
|
+
}
|
|
1800
|
+
} finally {
|
|
1801
|
+
reader.cancel().catch(() => {
|
|
1802
|
+
});
|
|
1803
|
+
}
|
|
1804
|
+
}
|
|
1805
|
+
/**
|
|
1806
|
+
* Streams a response from the v-next network
|
|
1807
|
+
* @param params - Stream parameters including message
|
|
1808
|
+
* @returns Promise containing the results
|
|
1809
|
+
*/
|
|
1810
|
+
async stream(params, onRecord) {
|
|
1811
|
+
const response = await this.request(`/api/networks/v-next/${this.networkId}/stream`, {
|
|
1812
|
+
method: "POST",
|
|
1813
|
+
body: {
|
|
1814
|
+
...params,
|
|
1815
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1816
|
+
},
|
|
1817
|
+
stream: true
|
|
1818
|
+
});
|
|
1819
|
+
if (!response.ok) {
|
|
1820
|
+
throw new Error(`Failed to stream vNext network: ${response.statusText}`);
|
|
1821
|
+
}
|
|
1822
|
+
if (!response.body) {
|
|
1823
|
+
throw new Error("Response body is null");
|
|
1824
|
+
}
|
|
1825
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
1826
|
+
if (typeof record === "string") {
|
|
1827
|
+
onRecord(JSON.parse(record));
|
|
1828
|
+
} else {
|
|
1829
|
+
onRecord(record);
|
|
1830
|
+
}
|
|
1831
|
+
}
|
|
1832
|
+
}
|
|
1833
|
+
/**
|
|
1834
|
+
* Streams a response from the v-next network loop
|
|
1835
|
+
* @param params - Stream parameters including message
|
|
1836
|
+
* @returns Promise containing the results
|
|
1837
|
+
*/
|
|
1838
|
+
async loopStream(params, onRecord) {
|
|
1839
|
+
const response = await this.request(`/api/networks/v-next/${this.networkId}/loop-stream`, {
|
|
1840
|
+
method: "POST",
|
|
1841
|
+
body: {
|
|
1842
|
+
...params,
|
|
1843
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1844
|
+
},
|
|
1845
|
+
stream: true
|
|
1846
|
+
});
|
|
1847
|
+
if (!response.ok) {
|
|
1848
|
+
throw new Error(`Failed to stream vNext network loop: ${response.statusText}`);
|
|
1849
|
+
}
|
|
1850
|
+
if (!response.body) {
|
|
1851
|
+
throw new Error("Response body is null");
|
|
1852
|
+
}
|
|
1853
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
1854
|
+
if (typeof record === "string") {
|
|
1855
|
+
onRecord(JSON.parse(record));
|
|
1856
|
+
} else {
|
|
1857
|
+
onRecord(record);
|
|
1858
|
+
}
|
|
1859
|
+
}
|
|
1860
|
+
}
|
|
1861
|
+
};
|
|
1862
|
+
|
|
1208
1863
|
// src/client.ts
|
|
1209
1864
|
var MastraClient = class extends BaseResource {
|
|
1210
1865
|
constructor(options) {
|
|
@@ -1282,6 +1937,48 @@ var MastraClient = class extends BaseResource {
|
|
|
1282
1937
|
getMemoryStatus(agentId) {
|
|
1283
1938
|
return this.request(`/api/memory/status?agentId=${agentId}`);
|
|
1284
1939
|
}
|
|
1940
|
+
/**
|
|
1941
|
+
* Retrieves memory threads for a resource
|
|
1942
|
+
* @param params - Parameters containing the resource ID
|
|
1943
|
+
* @returns Promise containing array of memory threads
|
|
1944
|
+
*/
|
|
1945
|
+
getNetworkMemoryThreads(params) {
|
|
1946
|
+
return this.request(`/api/memory/network/threads?resourceid=${params.resourceId}&networkId=${params.networkId}`);
|
|
1947
|
+
}
|
|
1948
|
+
/**
|
|
1949
|
+
* Creates a new memory thread
|
|
1950
|
+
* @param params - Parameters for creating the memory thread
|
|
1951
|
+
* @returns Promise containing the created memory thread
|
|
1952
|
+
*/
|
|
1953
|
+
createNetworkMemoryThread(params) {
|
|
1954
|
+
return this.request(`/api/memory/network/threads?networkId=${params.networkId}`, { method: "POST", body: params });
|
|
1955
|
+
}
|
|
1956
|
+
/**
|
|
1957
|
+
* Gets a memory thread instance by ID
|
|
1958
|
+
* @param threadId - ID of the memory thread to retrieve
|
|
1959
|
+
* @returns MemoryThread instance
|
|
1960
|
+
*/
|
|
1961
|
+
getNetworkMemoryThread(threadId, networkId) {
|
|
1962
|
+
return new NetworkMemoryThread(this.options, threadId, networkId);
|
|
1963
|
+
}
|
|
1964
|
+
/**
|
|
1965
|
+
* Saves messages to memory
|
|
1966
|
+
* @param params - Parameters containing messages to save
|
|
1967
|
+
* @returns Promise containing the saved messages
|
|
1968
|
+
*/
|
|
1969
|
+
saveNetworkMessageToMemory(params) {
|
|
1970
|
+
return this.request(`/api/memory/network/save-messages?networkId=${params.networkId}`, {
|
|
1971
|
+
method: "POST",
|
|
1972
|
+
body: params
|
|
1973
|
+
});
|
|
1974
|
+
}
|
|
1975
|
+
/**
|
|
1976
|
+
* Gets the status of the memory system
|
|
1977
|
+
* @returns Promise containing memory system status
|
|
1978
|
+
*/
|
|
1979
|
+
getNetworkMemoryStatus(networkId) {
|
|
1980
|
+
return this.request(`/api/memory/network/status?networkId=${networkId}`);
|
|
1981
|
+
}
|
|
1285
1982
|
/**
|
|
1286
1983
|
* Retrieves all available tools
|
|
1287
1984
|
* @returns Promise containing map of tool IDs to tool details
|
|
@@ -1295,7 +1992,7 @@ var MastraClient = class extends BaseResource {
|
|
|
1295
1992
|
* @returns Tool instance
|
|
1296
1993
|
*/
|
|
1297
1994
|
getTool(toolId) {
|
|
1298
|
-
return new
|
|
1995
|
+
return new Tool2(this.options, toolId);
|
|
1299
1996
|
}
|
|
1300
1997
|
/**
|
|
1301
1998
|
* Retrieves all available legacy workflows
|
|
@@ -1478,6 +2175,13 @@ var MastraClient = class extends BaseResource {
|
|
|
1478
2175
|
getNetworks() {
|
|
1479
2176
|
return this.request("/api/networks");
|
|
1480
2177
|
}
|
|
2178
|
+
/**
|
|
2179
|
+
* Retrieves all available vNext networks
|
|
2180
|
+
* @returns Promise containing map of vNext network IDs to vNext network details
|
|
2181
|
+
*/
|
|
2182
|
+
getVNextNetworks() {
|
|
2183
|
+
return this.request("/api/networks/v-next");
|
|
2184
|
+
}
|
|
1481
2185
|
/**
|
|
1482
2186
|
* Gets a network instance by ID
|
|
1483
2187
|
* @param networkId - ID of the network to retrieve
|
|
@@ -1486,6 +2190,14 @@ var MastraClient = class extends BaseResource {
|
|
|
1486
2190
|
getNetwork(networkId) {
|
|
1487
2191
|
return new Network(this.options, networkId);
|
|
1488
2192
|
}
|
|
2193
|
+
/**
|
|
2194
|
+
* Gets a vNext network instance by ID
|
|
2195
|
+
* @param networkId - ID of the vNext network to retrieve
|
|
2196
|
+
* @returns vNext Network instance
|
|
2197
|
+
*/
|
|
2198
|
+
getVNextNetwork(networkId) {
|
|
2199
|
+
return new VNextNetwork(this.options, networkId);
|
|
2200
|
+
}
|
|
1489
2201
|
/**
|
|
1490
2202
|
* Retrieves a list of available MCP servers.
|
|
1491
2203
|
* @param params - Optional parameters for pagination (limit, offset).
|
|
@@ -1542,6 +2254,41 @@ var MastraClient = class extends BaseResource {
|
|
|
1542
2254
|
getA2A(agentId) {
|
|
1543
2255
|
return new A2A(this.options, agentId);
|
|
1544
2256
|
}
|
|
2257
|
+
/**
|
|
2258
|
+
* Retrieves the working memory for a specific thread (optionally resource-scoped).
|
|
2259
|
+
* @param agentId - ID of the agent.
|
|
2260
|
+
* @param threadId - ID of the thread.
|
|
2261
|
+
* @param resourceId - Optional ID of the resource.
|
|
2262
|
+
* @returns Working memory for the specified thread or resource.
|
|
2263
|
+
*/
|
|
2264
|
+
getWorkingMemory({
|
|
2265
|
+
agentId,
|
|
2266
|
+
threadId,
|
|
2267
|
+
resourceId
|
|
2268
|
+
}) {
|
|
2269
|
+
return this.request(`/api/memory/threads/${threadId}/working-memory?agentId=${agentId}&resourceId=${resourceId}`);
|
|
2270
|
+
}
|
|
2271
|
+
/**
|
|
2272
|
+
* Updates the working memory for a specific thread (optionally resource-scoped).
|
|
2273
|
+
* @param agentId - ID of the agent.
|
|
2274
|
+
* @param threadId - ID of the thread.
|
|
2275
|
+
* @param workingMemory - The new working memory content.
|
|
2276
|
+
* @param resourceId - Optional ID of the resource.
|
|
2277
|
+
*/
|
|
2278
|
+
updateWorkingMemory({
|
|
2279
|
+
agentId,
|
|
2280
|
+
threadId,
|
|
2281
|
+
workingMemory,
|
|
2282
|
+
resourceId
|
|
2283
|
+
}) {
|
|
2284
|
+
return this.request(`/api/memory/threads/${threadId}/working-memory?agentId=${agentId}`, {
|
|
2285
|
+
method: "POST",
|
|
2286
|
+
body: {
|
|
2287
|
+
workingMemory,
|
|
2288
|
+
resourceId
|
|
2289
|
+
}
|
|
2290
|
+
});
|
|
2291
|
+
}
|
|
1545
2292
|
};
|
|
1546
2293
|
|
|
1547
2294
|
exports.MastraClient = MastraClient;
|