@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.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
|
|
@@ -246,11 +247,13 @@ var BaseResource = class {
|
|
|
246
247
|
const response = await fetch(`${baseUrl.replace(/\/$/, "")}${path}`, {
|
|
247
248
|
...options,
|
|
248
249
|
headers: {
|
|
250
|
+
...options.method === "POST" || options.method === "PUT" ? { "content-type": "application/json" } : {},
|
|
249
251
|
...headers,
|
|
250
252
|
...options.headers
|
|
251
253
|
// TODO: Bring this back once we figure out what we/users need to do to make this work with cross-origin requests
|
|
252
254
|
// 'x-mastra-client-type': 'js',
|
|
253
255
|
},
|
|
256
|
+
signal: this.options.abortSignal,
|
|
254
257
|
body: options.body instanceof FormData ? options.body : options.body ? JSON.stringify(options.body) : void 0
|
|
255
258
|
});
|
|
256
259
|
if (!response.ok) {
|
|
@@ -292,8 +295,6 @@ function parseClientRuntimeContext(runtimeContext) {
|
|
|
292
295
|
}
|
|
293
296
|
return void 0;
|
|
294
297
|
}
|
|
295
|
-
|
|
296
|
-
// src/resources/agent.ts
|
|
297
298
|
var AgentVoice = class extends BaseResource {
|
|
298
299
|
constructor(options, agentId) {
|
|
299
300
|
super(options);
|
|
@@ -362,7 +363,7 @@ var Agent = class extends BaseResource {
|
|
|
362
363
|
details() {
|
|
363
364
|
return this.request(`/api/agents/${this.agentId}`);
|
|
364
365
|
}
|
|
365
|
-
generate(params) {
|
|
366
|
+
async generate(params) {
|
|
366
367
|
const processedParams = {
|
|
367
368
|
...params,
|
|
368
369
|
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
@@ -370,15 +371,440 @@ var Agent = class extends BaseResource {
|
|
|
370
371
|
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
371
372
|
clientTools: processClientTools(params.clientTools)
|
|
372
373
|
};
|
|
373
|
-
|
|
374
|
+
const { runId, resourceId, threadId, runtimeContext } = processedParams;
|
|
375
|
+
const response = await this.request(`/api/agents/${this.agentId}/generate`, {
|
|
374
376
|
method: "POST",
|
|
375
377
|
body: processedParams
|
|
376
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;
|
|
377
803
|
}
|
|
378
804
|
/**
|
|
379
805
|
* Streams a response from the agent
|
|
380
806
|
* @param params - Stream parameters including prompt
|
|
381
|
-
* @returns Promise containing the enhanced Response object with processDataStream
|
|
807
|
+
* @returns Promise containing the enhanced Response object with processDataStream and processTextStream methods
|
|
382
808
|
*/
|
|
383
809
|
async stream(params) {
|
|
384
810
|
const processedParams = {
|
|
@@ -388,21 +814,27 @@ var Agent = class extends BaseResource {
|
|
|
388
814
|
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
389
815
|
clientTools: processClientTools(params.clientTools)
|
|
390
816
|
};
|
|
391
|
-
const
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
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
|
|
395
823
|
});
|
|
396
|
-
|
|
397
|
-
throw new Error("No response body");
|
|
398
|
-
}
|
|
399
|
-
response.processDataStream = async (options = {}) => {
|
|
824
|
+
streamResponse.processDataStream = async (options = {}) => {
|
|
400
825
|
await processDataStream({
|
|
401
|
-
stream:
|
|
826
|
+
stream: streamResponse.body,
|
|
402
827
|
...options
|
|
403
828
|
});
|
|
404
829
|
};
|
|
405
|
-
|
|
830
|
+
streamResponse.processTextStream = async (options) => {
|
|
831
|
+
await processTextStream({
|
|
832
|
+
stream: streamResponse.body,
|
|
833
|
+
onTextPart: options?.onTextPart ?? (() => {
|
|
834
|
+
})
|
|
835
|
+
});
|
|
836
|
+
};
|
|
837
|
+
return streamResponse;
|
|
406
838
|
}
|
|
407
839
|
/**
|
|
408
840
|
* Gets details about a specific tool available to the agent
|
|
@@ -796,7 +1228,7 @@ var LegacyWorkflow = class extends BaseResource {
|
|
|
796
1228
|
};
|
|
797
1229
|
|
|
798
1230
|
// src/resources/tool.ts
|
|
799
|
-
var
|
|
1231
|
+
var Tool2 = class extends BaseResource {
|
|
800
1232
|
constructor(options, toolId) {
|
|
801
1233
|
super(options);
|
|
802
1234
|
this.toolId = toolId;
|
|
@@ -901,10 +1333,10 @@ var Workflow = class extends BaseResource {
|
|
|
901
1333
|
if (params?.toDate) {
|
|
902
1334
|
searchParams.set("toDate", params.toDate.toISOString());
|
|
903
1335
|
}
|
|
904
|
-
if (params?.limit) {
|
|
1336
|
+
if (params?.limit !== null && params?.limit !== void 0 && !isNaN(Number(params?.limit))) {
|
|
905
1337
|
searchParams.set("limit", String(params.limit));
|
|
906
1338
|
}
|
|
907
|
-
if (params?.offset) {
|
|
1339
|
+
if (params?.offset !== null && params?.offset !== void 0 && !isNaN(Number(params?.offset))) {
|
|
908
1340
|
searchParams.set("offset", String(params.offset));
|
|
909
1341
|
}
|
|
910
1342
|
if (params?.resourceId) {
|
|
@@ -916,6 +1348,43 @@ var Workflow = class extends BaseResource {
|
|
|
916
1348
|
return this.request(`/api/workflows/${this.workflowId}/runs`);
|
|
917
1349
|
}
|
|
918
1350
|
}
|
|
1351
|
+
/**
|
|
1352
|
+
* Retrieves a specific workflow run by its ID
|
|
1353
|
+
* @param runId - The ID of the workflow run to retrieve
|
|
1354
|
+
* @returns Promise containing the workflow run details
|
|
1355
|
+
*/
|
|
1356
|
+
runById(runId) {
|
|
1357
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}`);
|
|
1358
|
+
}
|
|
1359
|
+
/**
|
|
1360
|
+
* Retrieves the execution result for a specific workflow run by its ID
|
|
1361
|
+
* @param runId - The ID of the workflow run to retrieve the execution result for
|
|
1362
|
+
* @returns Promise containing the workflow run execution result
|
|
1363
|
+
*/
|
|
1364
|
+
runExecutionResult(runId) {
|
|
1365
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}/execution-result`);
|
|
1366
|
+
}
|
|
1367
|
+
/**
|
|
1368
|
+
* Cancels a specific workflow run by its ID
|
|
1369
|
+
* @param runId - The ID of the workflow run to cancel
|
|
1370
|
+
* @returns Promise containing a success message
|
|
1371
|
+
*/
|
|
1372
|
+
cancelRun(runId) {
|
|
1373
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}/cancel`, {
|
|
1374
|
+
method: "POST"
|
|
1375
|
+
});
|
|
1376
|
+
}
|
|
1377
|
+
/**
|
|
1378
|
+
* Sends an event to a specific workflow run by its ID
|
|
1379
|
+
* @param params - Object containing the runId, event and data
|
|
1380
|
+
* @returns Promise containing a success message
|
|
1381
|
+
*/
|
|
1382
|
+
sendRunEvent(params) {
|
|
1383
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${params.runId}/send-event`, {
|
|
1384
|
+
method: "POST",
|
|
1385
|
+
body: { event: params.event, data: params.data }
|
|
1386
|
+
});
|
|
1387
|
+
}
|
|
919
1388
|
/**
|
|
920
1389
|
* Creates a new workflow run
|
|
921
1390
|
* @param params - Optional object containing the optional runId
|
|
@@ -981,16 +1450,16 @@ var Workflow = class extends BaseResource {
|
|
|
981
1450
|
});
|
|
982
1451
|
}
|
|
983
1452
|
/**
|
|
984
|
-
* Starts a
|
|
1453
|
+
* Starts a workflow run and returns a stream
|
|
985
1454
|
* @param params - Object containing the optional runId, inputData and runtimeContext
|
|
986
|
-
* @returns Promise containing the
|
|
1455
|
+
* @returns Promise containing the workflow execution results
|
|
987
1456
|
*/
|
|
988
1457
|
async stream(params) {
|
|
989
1458
|
const searchParams = new URLSearchParams();
|
|
990
1459
|
if (!!params?.runId) {
|
|
991
1460
|
searchParams.set("runId", params.runId);
|
|
992
1461
|
}
|
|
993
|
-
const runtimeContext =
|
|
1462
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
994
1463
|
const response = await this.request(
|
|
995
1464
|
`/api/workflows/${this.workflowId}/stream?${searchParams.toString()}`,
|
|
996
1465
|
{
|
|
@@ -1199,6 +1668,192 @@ var MCPTool = class extends BaseResource {
|
|
|
1199
1668
|
}
|
|
1200
1669
|
};
|
|
1201
1670
|
|
|
1671
|
+
// src/resources/network-memory-thread.ts
|
|
1672
|
+
var NetworkMemoryThread = class extends BaseResource {
|
|
1673
|
+
constructor(options, threadId, networkId) {
|
|
1674
|
+
super(options);
|
|
1675
|
+
this.threadId = threadId;
|
|
1676
|
+
this.networkId = networkId;
|
|
1677
|
+
}
|
|
1678
|
+
/**
|
|
1679
|
+
* Retrieves the memory thread details
|
|
1680
|
+
* @returns Promise containing thread details including title and metadata
|
|
1681
|
+
*/
|
|
1682
|
+
get() {
|
|
1683
|
+
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`);
|
|
1684
|
+
}
|
|
1685
|
+
/**
|
|
1686
|
+
* Updates the memory thread properties
|
|
1687
|
+
* @param params - Update parameters including title and metadata
|
|
1688
|
+
* @returns Promise containing updated thread details
|
|
1689
|
+
*/
|
|
1690
|
+
update(params) {
|
|
1691
|
+
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
|
|
1692
|
+
method: "PATCH",
|
|
1693
|
+
body: params
|
|
1694
|
+
});
|
|
1695
|
+
}
|
|
1696
|
+
/**
|
|
1697
|
+
* Deletes the memory thread
|
|
1698
|
+
* @returns Promise containing deletion result
|
|
1699
|
+
*/
|
|
1700
|
+
delete() {
|
|
1701
|
+
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
|
|
1702
|
+
method: "DELETE"
|
|
1703
|
+
});
|
|
1704
|
+
}
|
|
1705
|
+
/**
|
|
1706
|
+
* Retrieves messages associated with the thread
|
|
1707
|
+
* @param params - Optional parameters including limit for number of messages to retrieve
|
|
1708
|
+
* @returns Promise containing thread messages and UI messages
|
|
1709
|
+
*/
|
|
1710
|
+
getMessages(params) {
|
|
1711
|
+
const query = new URLSearchParams({
|
|
1712
|
+
networkId: this.networkId,
|
|
1713
|
+
...params?.limit ? { limit: params.limit.toString() } : {}
|
|
1714
|
+
});
|
|
1715
|
+
return this.request(`/api/memory/network/threads/${this.threadId}/messages?${query.toString()}`);
|
|
1716
|
+
}
|
|
1717
|
+
};
|
|
1718
|
+
|
|
1719
|
+
// src/resources/vNextNetwork.ts
|
|
1720
|
+
var RECORD_SEPARATOR3 = "";
|
|
1721
|
+
var VNextNetwork = class extends BaseResource {
|
|
1722
|
+
constructor(options, networkId) {
|
|
1723
|
+
super(options);
|
|
1724
|
+
this.networkId = networkId;
|
|
1725
|
+
}
|
|
1726
|
+
/**
|
|
1727
|
+
* Retrieves details about the network
|
|
1728
|
+
* @returns Promise containing vNext network details
|
|
1729
|
+
*/
|
|
1730
|
+
details() {
|
|
1731
|
+
return this.request(`/api/networks/v-next/${this.networkId}`);
|
|
1732
|
+
}
|
|
1733
|
+
/**
|
|
1734
|
+
* Generates a response from the v-next network
|
|
1735
|
+
* @param params - Generation parameters including message
|
|
1736
|
+
* @returns Promise containing the generated response
|
|
1737
|
+
*/
|
|
1738
|
+
generate(params) {
|
|
1739
|
+
return this.request(`/api/networks/v-next/${this.networkId}/generate`, {
|
|
1740
|
+
method: "POST",
|
|
1741
|
+
body: {
|
|
1742
|
+
...params,
|
|
1743
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1744
|
+
}
|
|
1745
|
+
});
|
|
1746
|
+
}
|
|
1747
|
+
/**
|
|
1748
|
+
* Generates a response from the v-next network using multiple primitives
|
|
1749
|
+
* @param params - Generation parameters including message
|
|
1750
|
+
* @returns Promise containing the generated response
|
|
1751
|
+
*/
|
|
1752
|
+
loop(params) {
|
|
1753
|
+
return this.request(`/api/networks/v-next/${this.networkId}/loop`, {
|
|
1754
|
+
method: "POST",
|
|
1755
|
+
body: {
|
|
1756
|
+
...params,
|
|
1757
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1758
|
+
}
|
|
1759
|
+
});
|
|
1760
|
+
}
|
|
1761
|
+
async *streamProcessor(stream) {
|
|
1762
|
+
const reader = stream.getReader();
|
|
1763
|
+
let doneReading = false;
|
|
1764
|
+
let buffer = "";
|
|
1765
|
+
try {
|
|
1766
|
+
while (!doneReading) {
|
|
1767
|
+
const { done, value } = await reader.read();
|
|
1768
|
+
doneReading = done;
|
|
1769
|
+
if (done && !value) continue;
|
|
1770
|
+
try {
|
|
1771
|
+
const decoded = value ? new TextDecoder().decode(value) : "";
|
|
1772
|
+
const chunks = (buffer + decoded).split(RECORD_SEPARATOR3);
|
|
1773
|
+
buffer = chunks.pop() || "";
|
|
1774
|
+
for (const chunk of chunks) {
|
|
1775
|
+
if (chunk) {
|
|
1776
|
+
if (typeof chunk === "string") {
|
|
1777
|
+
try {
|
|
1778
|
+
const parsedChunk = JSON.parse(chunk);
|
|
1779
|
+
yield parsedChunk;
|
|
1780
|
+
} catch {
|
|
1781
|
+
}
|
|
1782
|
+
}
|
|
1783
|
+
}
|
|
1784
|
+
}
|
|
1785
|
+
} catch {
|
|
1786
|
+
}
|
|
1787
|
+
}
|
|
1788
|
+
if (buffer) {
|
|
1789
|
+
try {
|
|
1790
|
+
yield JSON.parse(buffer);
|
|
1791
|
+
} catch {
|
|
1792
|
+
}
|
|
1793
|
+
}
|
|
1794
|
+
} finally {
|
|
1795
|
+
reader.cancel().catch(() => {
|
|
1796
|
+
});
|
|
1797
|
+
}
|
|
1798
|
+
}
|
|
1799
|
+
/**
|
|
1800
|
+
* Streams a response from the v-next network
|
|
1801
|
+
* @param params - Stream parameters including message
|
|
1802
|
+
* @returns Promise containing the results
|
|
1803
|
+
*/
|
|
1804
|
+
async stream(params, onRecord) {
|
|
1805
|
+
const response = await this.request(`/api/networks/v-next/${this.networkId}/stream`, {
|
|
1806
|
+
method: "POST",
|
|
1807
|
+
body: {
|
|
1808
|
+
...params,
|
|
1809
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1810
|
+
},
|
|
1811
|
+
stream: true
|
|
1812
|
+
});
|
|
1813
|
+
if (!response.ok) {
|
|
1814
|
+
throw new Error(`Failed to stream vNext network: ${response.statusText}`);
|
|
1815
|
+
}
|
|
1816
|
+
if (!response.body) {
|
|
1817
|
+
throw new Error("Response body is null");
|
|
1818
|
+
}
|
|
1819
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
1820
|
+
if (typeof record === "string") {
|
|
1821
|
+
onRecord(JSON.parse(record));
|
|
1822
|
+
} else {
|
|
1823
|
+
onRecord(record);
|
|
1824
|
+
}
|
|
1825
|
+
}
|
|
1826
|
+
}
|
|
1827
|
+
/**
|
|
1828
|
+
* Streams a response from the v-next network loop
|
|
1829
|
+
* @param params - Stream parameters including message
|
|
1830
|
+
* @returns Promise containing the results
|
|
1831
|
+
*/
|
|
1832
|
+
async loopStream(params, onRecord) {
|
|
1833
|
+
const response = await this.request(`/api/networks/v-next/${this.networkId}/loop-stream`, {
|
|
1834
|
+
method: "POST",
|
|
1835
|
+
body: {
|
|
1836
|
+
...params,
|
|
1837
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1838
|
+
},
|
|
1839
|
+
stream: true
|
|
1840
|
+
});
|
|
1841
|
+
if (!response.ok) {
|
|
1842
|
+
throw new Error(`Failed to stream vNext network loop: ${response.statusText}`);
|
|
1843
|
+
}
|
|
1844
|
+
if (!response.body) {
|
|
1845
|
+
throw new Error("Response body is null");
|
|
1846
|
+
}
|
|
1847
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
1848
|
+
if (typeof record === "string") {
|
|
1849
|
+
onRecord(JSON.parse(record));
|
|
1850
|
+
} else {
|
|
1851
|
+
onRecord(record);
|
|
1852
|
+
}
|
|
1853
|
+
}
|
|
1854
|
+
}
|
|
1855
|
+
};
|
|
1856
|
+
|
|
1202
1857
|
// src/client.ts
|
|
1203
1858
|
var MastraClient = class extends BaseResource {
|
|
1204
1859
|
constructor(options) {
|
|
@@ -1276,6 +1931,48 @@ var MastraClient = class extends BaseResource {
|
|
|
1276
1931
|
getMemoryStatus(agentId) {
|
|
1277
1932
|
return this.request(`/api/memory/status?agentId=${agentId}`);
|
|
1278
1933
|
}
|
|
1934
|
+
/**
|
|
1935
|
+
* Retrieves memory threads for a resource
|
|
1936
|
+
* @param params - Parameters containing the resource ID
|
|
1937
|
+
* @returns Promise containing array of memory threads
|
|
1938
|
+
*/
|
|
1939
|
+
getNetworkMemoryThreads(params) {
|
|
1940
|
+
return this.request(`/api/memory/network/threads?resourceid=${params.resourceId}&networkId=${params.networkId}`);
|
|
1941
|
+
}
|
|
1942
|
+
/**
|
|
1943
|
+
* Creates a new memory thread
|
|
1944
|
+
* @param params - Parameters for creating the memory thread
|
|
1945
|
+
* @returns Promise containing the created memory thread
|
|
1946
|
+
*/
|
|
1947
|
+
createNetworkMemoryThread(params) {
|
|
1948
|
+
return this.request(`/api/memory/network/threads?networkId=${params.networkId}`, { method: "POST", body: params });
|
|
1949
|
+
}
|
|
1950
|
+
/**
|
|
1951
|
+
* Gets a memory thread instance by ID
|
|
1952
|
+
* @param threadId - ID of the memory thread to retrieve
|
|
1953
|
+
* @returns MemoryThread instance
|
|
1954
|
+
*/
|
|
1955
|
+
getNetworkMemoryThread(threadId, networkId) {
|
|
1956
|
+
return new NetworkMemoryThread(this.options, threadId, networkId);
|
|
1957
|
+
}
|
|
1958
|
+
/**
|
|
1959
|
+
* Saves messages to memory
|
|
1960
|
+
* @param params - Parameters containing messages to save
|
|
1961
|
+
* @returns Promise containing the saved messages
|
|
1962
|
+
*/
|
|
1963
|
+
saveNetworkMessageToMemory(params) {
|
|
1964
|
+
return this.request(`/api/memory/network/save-messages?networkId=${params.networkId}`, {
|
|
1965
|
+
method: "POST",
|
|
1966
|
+
body: params
|
|
1967
|
+
});
|
|
1968
|
+
}
|
|
1969
|
+
/**
|
|
1970
|
+
* Gets the status of the memory system
|
|
1971
|
+
* @returns Promise containing memory system status
|
|
1972
|
+
*/
|
|
1973
|
+
getNetworkMemoryStatus(networkId) {
|
|
1974
|
+
return this.request(`/api/memory/network/status?networkId=${networkId}`);
|
|
1975
|
+
}
|
|
1279
1976
|
/**
|
|
1280
1977
|
* Retrieves all available tools
|
|
1281
1978
|
* @returns Promise containing map of tool IDs to tool details
|
|
@@ -1289,7 +1986,7 @@ var MastraClient = class extends BaseResource {
|
|
|
1289
1986
|
* @returns Tool instance
|
|
1290
1987
|
*/
|
|
1291
1988
|
getTool(toolId) {
|
|
1292
|
-
return new
|
|
1989
|
+
return new Tool2(this.options, toolId);
|
|
1293
1990
|
}
|
|
1294
1991
|
/**
|
|
1295
1992
|
* Retrieves all available legacy workflows
|
|
@@ -1472,6 +2169,13 @@ var MastraClient = class extends BaseResource {
|
|
|
1472
2169
|
getNetworks() {
|
|
1473
2170
|
return this.request("/api/networks");
|
|
1474
2171
|
}
|
|
2172
|
+
/**
|
|
2173
|
+
* Retrieves all available vNext networks
|
|
2174
|
+
* @returns Promise containing map of vNext network IDs to vNext network details
|
|
2175
|
+
*/
|
|
2176
|
+
getVNextNetworks() {
|
|
2177
|
+
return this.request("/api/networks/v-next");
|
|
2178
|
+
}
|
|
1475
2179
|
/**
|
|
1476
2180
|
* Gets a network instance by ID
|
|
1477
2181
|
* @param networkId - ID of the network to retrieve
|
|
@@ -1480,6 +2184,14 @@ var MastraClient = class extends BaseResource {
|
|
|
1480
2184
|
getNetwork(networkId) {
|
|
1481
2185
|
return new Network(this.options, networkId);
|
|
1482
2186
|
}
|
|
2187
|
+
/**
|
|
2188
|
+
* Gets a vNext network instance by ID
|
|
2189
|
+
* @param networkId - ID of the vNext network to retrieve
|
|
2190
|
+
* @returns vNext Network instance
|
|
2191
|
+
*/
|
|
2192
|
+
getVNextNetwork(networkId) {
|
|
2193
|
+
return new VNextNetwork(this.options, networkId);
|
|
2194
|
+
}
|
|
1483
2195
|
/**
|
|
1484
2196
|
* Retrieves a list of available MCP servers.
|
|
1485
2197
|
* @param params - Optional parameters for pagination (limit, offset).
|
|
@@ -1536,6 +2248,41 @@ var MastraClient = class extends BaseResource {
|
|
|
1536
2248
|
getA2A(agentId) {
|
|
1537
2249
|
return new A2A(this.options, agentId);
|
|
1538
2250
|
}
|
|
2251
|
+
/**
|
|
2252
|
+
* Retrieves the working memory for a specific thread (optionally resource-scoped).
|
|
2253
|
+
* @param agentId - ID of the agent.
|
|
2254
|
+
* @param threadId - ID of the thread.
|
|
2255
|
+
* @param resourceId - Optional ID of the resource.
|
|
2256
|
+
* @returns Working memory for the specified thread or resource.
|
|
2257
|
+
*/
|
|
2258
|
+
getWorkingMemory({
|
|
2259
|
+
agentId,
|
|
2260
|
+
threadId,
|
|
2261
|
+
resourceId
|
|
2262
|
+
}) {
|
|
2263
|
+
return this.request(`/api/memory/threads/${threadId}/working-memory?agentId=${agentId}&resourceId=${resourceId}`);
|
|
2264
|
+
}
|
|
2265
|
+
/**
|
|
2266
|
+
* Updates the working memory for a specific thread (optionally resource-scoped).
|
|
2267
|
+
* @param agentId - ID of the agent.
|
|
2268
|
+
* @param threadId - ID of the thread.
|
|
2269
|
+
* @param workingMemory - The new working memory content.
|
|
2270
|
+
* @param resourceId - Optional ID of the resource.
|
|
2271
|
+
*/
|
|
2272
|
+
updateWorkingMemory({
|
|
2273
|
+
agentId,
|
|
2274
|
+
threadId,
|
|
2275
|
+
workingMemory,
|
|
2276
|
+
resourceId
|
|
2277
|
+
}) {
|
|
2278
|
+
return this.request(`/api/memory/threads/${threadId}/working-memory?agentId=${agentId}`, {
|
|
2279
|
+
method: "POST",
|
|
2280
|
+
body: {
|
|
2281
|
+
workingMemory,
|
|
2282
|
+
resourceId
|
|
2283
|
+
}
|
|
2284
|
+
});
|
|
2285
|
+
}
|
|
1539
2286
|
};
|
|
1540
2287
|
|
|
1541
2288
|
export { MastraClient };
|