@mastra/client-js 0.0.0-fix-fetch-workflow-runs-20250624231457 → 0.0.0-fix-traces-pagination-plus-share-for-cloud-20250717083008

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/dist/index.cjs CHANGED
@@ -6,6 +6,7 @@ var uiUtils = require('@ai-sdk/ui-utils');
6
6
  var zod = require('zod');
7
7
  var originalZodToJsonSchema = require('zod-to-json-schema');
8
8
  var tools = require('@mastra/core/tools');
9
+ var uuid = require('@lukeed/uuid');
9
10
  var runtimeContext = require('@mastra/core/runtime-context');
10
11
 
11
12
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
@@ -258,6 +259,7 @@ var BaseResource = class {
258
259
  // TODO: Bring this back once we figure out what we/users need to do to make this work with cross-origin requests
259
260
  // 'x-mastra-client-type': 'js',
260
261
  },
262
+ signal: this.options.abortSignal,
261
263
  body: options.body instanceof FormData ? options.body : options.body ? JSON.stringify(options.body) : void 0
262
264
  });
263
265
  if (!response.ok) {
@@ -381,7 +383,11 @@ var Agent = class extends BaseResource {
381
383
  body: processedParams
382
384
  });
383
385
  if (response.finishReason === "tool-calls") {
384
- for (const toolCall of response.toolCalls) {
386
+ const toolCalls = response.toolCalls;
387
+ if (!toolCalls || !Array.isArray(toolCalls)) {
388
+ return response;
389
+ }
390
+ for (const toolCall of toolCalls) {
385
391
  const clientTool = params.clientTools?.[toolCall.toolName];
386
392
  if (clientTool && clientTool.execute) {
387
393
  const result = await clientTool.execute(
@@ -424,7 +430,8 @@ var Agent = class extends BaseResource {
424
430
  onToolCall,
425
431
  onFinish,
426
432
  getCurrentDate = () => /* @__PURE__ */ new Date(),
427
- lastMessage
433
+ lastMessage,
434
+ streamProtocol
428
435
  }) {
429
436
  const replaceLastMessage = lastMessage?.role === "assistant";
430
437
  let step = replaceLastMessage ? 1 + // find max step in existing tool invocations:
@@ -432,7 +439,7 @@ var Agent = class extends BaseResource {
432
439
  return Math.max(max, toolInvocation.step ?? 0);
433
440
  }, 0) ?? 0) : 0;
434
441
  const message = replaceLastMessage ? structuredClone(lastMessage) : {
435
- id: crypto.randomUUID(),
442
+ id: uuid.v4(),
436
443
  createdAt: getCurrentDate(),
437
444
  role: "assistant",
438
445
  content: "",
@@ -477,7 +484,7 @@ var Agent = class extends BaseResource {
477
484
  // changes. This is why we need to add a revision id to ensure that the message
478
485
  // is updated with SWR (without it, the changes get stuck in SWR and are not
479
486
  // forwarded to rendering):
480
- revisionId: crypto.randomUUID()
487
+ revisionId: uuid.v4()
481
488
  };
482
489
  update({
483
490
  message: copiedMessage,
@@ -485,228 +492,213 @@ var Agent = class extends BaseResource {
485
492
  replaceLastMessage
486
493
  });
487
494
  }
488
- await uiUtils.processDataStream({
489
- stream,
490
- onTextPart(value) {
491
- if (currentTextPart == null) {
492
- currentTextPart = {
493
- type: "text",
494
- text: value
495
- };
496
- message.parts.push(currentTextPart);
497
- } else {
498
- currentTextPart.text += value;
495
+ if (streamProtocol === "text") {
496
+ await uiUtils.processTextStream({
497
+ stream,
498
+ onTextPart(value) {
499
+ message.content += value;
500
+ execUpdate();
499
501
  }
500
- message.content += value;
501
- execUpdate();
502
- },
503
- onReasoningPart(value) {
504
- if (currentReasoningTextDetail == null) {
505
- currentReasoningTextDetail = { type: "text", text: value };
506
- if (currentReasoningPart != null) {
507
- currentReasoningPart.details.push(currentReasoningTextDetail);
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;
508
516
  }
509
- } else {
510
- currentReasoningTextDetail.text += value;
511
- }
512
- if (currentReasoningPart == null) {
513
- currentReasoningPart = {
514
- type: "reasoning",
515
- reasoning: value,
516
- details: [currentReasoningTextDetail]
517
- };
518
- message.parts.push(currentReasoningPart);
519
- } else {
520
- currentReasoningPart.reasoning += value;
521
- }
522
- message.reasoning = (message.reasoning ?? "") + value;
523
- execUpdate();
524
- },
525
- onReasoningSignaturePart(value) {
526
- if (currentReasoningTextDetail != null) {
527
- currentReasoningTextDetail.signature = value.signature;
528
- }
529
- },
530
- onRedactedReasoningPart(value) {
531
- if (currentReasoningPart == null) {
532
- currentReasoningPart = {
533
- type: "reasoning",
534
- reasoning: "",
535
- details: []
536
- };
537
- message.parts.push(currentReasoningPart);
538
- }
539
- currentReasoningPart.details.push({
540
- type: "redacted",
541
- data: value.data
542
- });
543
- currentReasoningTextDetail = void 0;
544
- execUpdate();
545
- },
546
- onFilePart(value) {
547
- message.parts.push({
548
- type: "file",
549
- mimeType: value.mimeType,
550
- data: value.data
551
- });
552
- execUpdate();
553
- },
554
- onSourcePart(value) {
555
- message.parts.push({
556
- type: "source",
557
- source: value
558
- });
559
- execUpdate();
560
- },
561
- onToolCallStreamingStartPart(value) {
562
- if (message.toolInvocations == null) {
563
- message.toolInvocations = [];
564
- }
565
- partialToolCalls[value.toolCallId] = {
566
- text: "",
567
- step,
568
- toolName: value.toolName,
569
- index: message.toolInvocations.length
570
- };
571
- const invocation = {
572
- state: "partial-call",
573
- step,
574
- toolCallId: value.toolCallId,
575
- toolName: value.toolName,
576
- args: void 0
577
- };
578
- message.toolInvocations.push(invocation);
579
- updateToolInvocationPart(value.toolCallId, invocation);
580
- execUpdate();
581
- },
582
- onToolCallDeltaPart(value) {
583
- const partialToolCall = partialToolCalls[value.toolCallId];
584
- partialToolCall.text += value.argsTextDelta;
585
- const { value: partialArgs } = uiUtils.parsePartialJson(partialToolCall.text);
586
- const invocation = {
587
- state: "partial-call",
588
- step: partialToolCall.step,
589
- toolCallId: value.toolCallId,
590
- toolName: partialToolCall.toolName,
591
- args: partialArgs
592
- };
593
- message.toolInvocations[partialToolCall.index] = invocation;
594
- updateToolInvocationPart(value.toolCallId, invocation);
595
- execUpdate();
596
- },
597
- async onToolCallPart(value) {
598
- const invocation = {
599
- state: "call",
600
- step,
601
- ...value
602
- };
603
- if (partialToolCalls[value.toolCallId] != null) {
604
- message.toolInvocations[partialToolCalls[value.toolCallId].index] = invocation;
605
- } else {
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) {
606
579
  if (message.toolInvocations == null) {
607
580
  message.toolInvocations = [];
608
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
+ };
609
595
  message.toolInvocations.push(invocation);
610
- }
611
- updateToolInvocationPart(value.toolCallId, invocation);
612
- execUpdate();
613
- if (onToolCall) {
614
- const result = await onToolCall({ toolCall: value });
615
- if (result != null) {
616
- const invocation2 = {
617
- state: "result",
618
- step,
619
- ...value,
620
- result
621
- };
622
- message.toolInvocations[message.toolInvocations.length - 1] = invocation2;
623
- updateToolInvocationPart(value.toolCallId, invocation2);
624
- execUpdate();
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);
625
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);
626
698
  }
627
- },
628
- onToolResultPart(value) {
629
- const toolInvocations = message.toolInvocations;
630
- if (toolInvocations == null) {
631
- throw new Error("tool_result must be preceded by a tool_call");
632
- }
633
- const toolInvocationIndex = toolInvocations.findIndex((invocation2) => invocation2.toolCallId === value.toolCallId);
634
- if (toolInvocationIndex === -1) {
635
- throw new Error("tool_result must be preceded by a tool_call with the same toolCallId");
636
- }
637
- const invocation = {
638
- ...toolInvocations[toolInvocationIndex],
639
- state: "result",
640
- ...value
641
- };
642
- toolInvocations[toolInvocationIndex] = invocation;
643
- updateToolInvocationPart(value.toolCallId, invocation);
644
- execUpdate();
645
- },
646
- onDataPart(value) {
647
- data.push(...value);
648
- execUpdate();
649
- },
650
- onMessageAnnotationsPart(value) {
651
- if (messageAnnotations == null) {
652
- messageAnnotations = [...value];
653
- } else {
654
- messageAnnotations.push(...value);
655
- }
656
- execUpdate();
657
- },
658
- onFinishStepPart(value) {
659
- step += 1;
660
- currentTextPart = value.isContinued ? currentTextPart : void 0;
661
- currentReasoningPart = void 0;
662
- currentReasoningTextDetail = void 0;
663
- },
664
- onStartStepPart(value) {
665
- if (!replaceLastMessage) {
666
- message.id = value.messageId;
667
- }
668
- message.parts.push({ type: "step-start" });
669
- execUpdate();
670
- },
671
- onFinishMessagePart(value) {
672
- finishReason = value.finishReason;
673
- if (value.usage != null) {
674
- usage = value.usage;
675
- }
676
- },
677
- onErrorPart(error) {
678
- throw new Error(error);
679
- }
680
- });
681
- onFinish?.({ message, finishReason, usage });
682
- }
683
- /**
684
- * Streams a response from the agent
685
- * @param params - Stream parameters including prompt
686
- * @returns Promise containing the enhanced Response object with processDataStream method
687
- */
688
- async stream(params) {
689
- const processedParams = {
690
- ...params,
691
- output: params.output ? zodToJsonSchema(params.output) : void 0,
692
- experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
693
- runtimeContext: parseClientRuntimeContext(params.runtimeContext),
694
- clientTools: processClientTools(params.clientTools)
695
- };
696
- const { readable, writable } = new TransformStream();
697
- const response = await this.processStreamResponse(processedParams, writable);
698
- const streamResponse = new Response(readable, {
699
- status: response.status,
700
- statusText: response.statusText,
701
- headers: response.headers
702
- });
703
- streamResponse.processDataStream = async (options = {}) => {
704
- await uiUtils.processDataStream({
705
- stream: streamResponse.body,
706
- ...options
707
699
  });
708
- };
709
- return streamResponse;
700
+ onFinish?.({ message, finishReason, usage });
701
+ }
710
702
  }
711
703
  /**
712
704
  * Processes the stream response and handles tool calls
@@ -721,80 +713,135 @@ var Agent = class extends BaseResource {
721
713
  throw new Error("No response body");
722
714
  }
723
715
  try {
716
+ const streamProtocol = processedParams.output ? "text" : "data";
724
717
  let toolCalls = [];
725
- let finishReasonToolCalls = false;
726
718
  let messages = [];
727
- let hasProcessedToolCalls = false;
728
- response.clone().body.pipeTo(writable);
729
- await this.processChatResponse({
730
- stream: response.clone().body,
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,
731
727
  update: ({ message }) => {
732
728
  messages.push(message);
733
729
  },
734
- onFinish: ({ finishReason, message }) => {
730
+ onFinish: async ({ finishReason, message }) => {
735
731
  if (finishReason === "tool-calls") {
736
- finishReasonToolCalls = true;
737
732
  const toolCall = [...message?.parts ?? []].reverse().find((part) => part.type === "tool-invocation")?.toolInvocation;
738
733
  if (toolCall) {
739
734
  toolCalls.push(toolCall);
740
735
  }
741
- }
742
- },
743
- lastMessage: void 0
744
- });
745
- if (finishReasonToolCalls && !hasProcessedToolCalls) {
746
- hasProcessedToolCalls = true;
747
- for (const toolCall of toolCalls) {
748
- const clientTool = processedParams.clientTools?.[toolCall.toolName];
749
- if (clientTool && clientTool.execute) {
750
- const result = await clientTool.execute(
751
- {
752
- context: toolCall?.args,
753
- runId: processedParams.runId,
754
- resourceId: processedParams.resourceId,
755
- threadId: processedParams.threadId,
756
- runtimeContext: processedParams.runtimeContext
757
- },
758
- {
759
- messages: response.messages,
760
- toolCallId: toolCall?.toolCallId
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
+ );
761
792
  }
762
- );
763
- const lastMessage = JSON.parse(JSON.stringify(messages[messages.length - 1]));
764
- const toolInvocationPart = lastMessage?.parts?.find(
765
- (part) => part.type === "tool-invocation" && part.toolInvocation?.toolCallId === toolCall.toolCallId
766
- );
767
- if (toolInvocationPart) {
768
- toolInvocationPart.toolInvocation = {
769
- ...toolInvocationPart.toolInvocation,
770
- state: "result",
771
- result
772
- };
773
793
  }
774
- const toolInvocation = lastMessage?.toolInvocations?.find(
775
- (toolInvocation2) => toolInvocation2.toolCallId === toolCall.toolCallId
776
- );
777
- if (toolInvocation) {
778
- toolInvocation.state = "result";
779
- toolInvocation.result = result;
780
- }
781
- const originalMessages = processedParams.messages;
782
- const messageArray = Array.isArray(originalMessages) ? originalMessages : [originalMessages];
783
- this.processStreamResponse(
784
- {
785
- ...processedParams,
786
- messages: [...messageArray, ...messages, lastMessage]
787
- },
788
- writable
789
- );
794
+ } else {
795
+ setTimeout(() => {
796
+ if (!writable.locked) {
797
+ writable.close();
798
+ }
799
+ }, 0);
790
800
  }
791
- }
792
- }
801
+ },
802
+ lastMessage: void 0,
803
+ streamProtocol
804
+ });
793
805
  } catch (error) {
794
806
  console.error("Error processing stream response:", error);
795
807
  }
796
808
  return response;
797
809
  }
810
+ /**
811
+ * Streams a response from the agent
812
+ * @param params - Stream parameters including prompt
813
+ * @returns Promise containing the enhanced Response object with processDataStream and processTextStream methods
814
+ */
815
+ async stream(params) {
816
+ const processedParams = {
817
+ ...params,
818
+ output: params.output ? zodToJsonSchema(params.output) : void 0,
819
+ experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
820
+ runtimeContext: parseClientRuntimeContext(params.runtimeContext),
821
+ clientTools: processClientTools(params.clientTools)
822
+ };
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
829
+ });
830
+ streamResponse.processDataStream = async (options = {}) => {
831
+ await uiUtils.processDataStream({
832
+ stream: streamResponse.body,
833
+ ...options
834
+ });
835
+ };
836
+ streamResponse.processTextStream = async (options) => {
837
+ await uiUtils.processTextStream({
838
+ stream: streamResponse.body,
839
+ onTextPart: options?.onTextPart ?? (() => {
840
+ })
841
+ });
842
+ };
843
+ return streamResponse;
844
+ }
798
845
  /**
799
846
  * Gets details about a specific tool available to the agent
800
847
  * @param toolId - ID of the tool to retrieve
@@ -1292,10 +1339,10 @@ var Workflow = class extends BaseResource {
1292
1339
  if (params?.toDate) {
1293
1340
  searchParams.set("toDate", params.toDate.toISOString());
1294
1341
  }
1295
- if (params?.limit !== void 0) {
1342
+ if (params?.limit !== null && params?.limit !== void 0 && !isNaN(Number(params?.limit))) {
1296
1343
  searchParams.set("limit", String(params.limit));
1297
1344
  }
1298
- if (params?.offset !== void 0) {
1345
+ if (params?.offset !== null && params?.offset !== void 0 && !isNaN(Number(params?.offset))) {
1299
1346
  searchParams.set("offset", String(params.offset));
1300
1347
  }
1301
1348
  if (params?.resourceId) {
@@ -1323,6 +1370,27 @@ var Workflow = class extends BaseResource {
1323
1370
  runExecutionResult(runId) {
1324
1371
  return this.request(`/api/workflows/${this.workflowId}/runs/${runId}/execution-result`);
1325
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
+ }
1326
1394
  /**
1327
1395
  * Creates a new workflow run
1328
1396
  * @param params - Optional object containing the optional runId
@@ -1412,6 +1480,7 @@ var Workflow = class extends BaseResource {
1412
1480
  if (!response.body) {
1413
1481
  throw new Error("Response body is null");
1414
1482
  }
1483
+ let failedChunk = void 0;
1415
1484
  const transformStream = new TransformStream({
1416
1485
  start() {
1417
1486
  },
@@ -1421,10 +1490,13 @@ var Workflow = class extends BaseResource {
1421
1490
  const chunks = decoded.split(RECORD_SEPARATOR2);
1422
1491
  for (const chunk2 of chunks) {
1423
1492
  if (chunk2) {
1493
+ const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
1424
1494
  try {
1425
- const parsedChunk = JSON.parse(chunk2);
1495
+ const parsedChunk = JSON.parse(newChunk);
1426
1496
  controller.enqueue(parsedChunk);
1427
- } catch {
1497
+ failedChunk = void 0;
1498
+ } catch (error) {
1499
+ failedChunk = newChunk;
1428
1500
  }
1429
1501
  }
1430
1502
  }
@@ -1606,6 +1678,54 @@ var MCPTool = class extends BaseResource {
1606
1678
  }
1607
1679
  };
1608
1680
 
1681
+ // src/resources/network-memory-thread.ts
1682
+ var NetworkMemoryThread = class extends BaseResource {
1683
+ constructor(options, threadId, networkId) {
1684
+ super(options);
1685
+ this.threadId = threadId;
1686
+ this.networkId = networkId;
1687
+ }
1688
+ /**
1689
+ * Retrieves the memory thread details
1690
+ * @returns Promise containing thread details including title and metadata
1691
+ */
1692
+ get() {
1693
+ return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`);
1694
+ }
1695
+ /**
1696
+ * Updates the memory thread properties
1697
+ * @param params - Update parameters including title and metadata
1698
+ * @returns Promise containing updated thread details
1699
+ */
1700
+ update(params) {
1701
+ return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
1702
+ method: "PATCH",
1703
+ body: params
1704
+ });
1705
+ }
1706
+ /**
1707
+ * Deletes the memory thread
1708
+ * @returns Promise containing deletion result
1709
+ */
1710
+ delete() {
1711
+ return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
1712
+ method: "DELETE"
1713
+ });
1714
+ }
1715
+ /**
1716
+ * Retrieves messages associated with the thread
1717
+ * @param params - Optional parameters including limit for number of messages to retrieve
1718
+ * @returns Promise containing thread messages and UI messages
1719
+ */
1720
+ getMessages(params) {
1721
+ const query = new URLSearchParams({
1722
+ networkId: this.networkId,
1723
+ ...params?.limit ? { limit: params.limit.toString() } : {}
1724
+ });
1725
+ return this.request(`/api/memory/network/threads/${this.threadId}/messages?${query.toString()}`);
1726
+ }
1727
+ };
1728
+
1609
1729
  // src/resources/vNextNetwork.ts
1610
1730
  var RECORD_SEPARATOR3 = "";
1611
1731
  var VNextNetwork = class extends BaseResource {
@@ -1628,7 +1748,10 @@ var VNextNetwork = class extends BaseResource {
1628
1748
  generate(params) {
1629
1749
  return this.request(`/api/networks/v-next/${this.networkId}/generate`, {
1630
1750
  method: "POST",
1631
- body: params
1751
+ body: {
1752
+ ...params,
1753
+ runtimeContext: parseClientRuntimeContext(params.runtimeContext)
1754
+ }
1632
1755
  });
1633
1756
  }
1634
1757
  /**
@@ -1639,7 +1762,10 @@ var VNextNetwork = class extends BaseResource {
1639
1762
  loop(params) {
1640
1763
  return this.request(`/api/networks/v-next/${this.networkId}/loop`, {
1641
1764
  method: "POST",
1642
- body: params
1765
+ body: {
1766
+ ...params,
1767
+ runtimeContext: parseClientRuntimeContext(params.runtimeContext)
1768
+ }
1643
1769
  });
1644
1770
  }
1645
1771
  async *streamProcessor(stream) {
@@ -1688,7 +1814,10 @@ var VNextNetwork = class extends BaseResource {
1688
1814
  async stream(params, onRecord) {
1689
1815
  const response = await this.request(`/api/networks/v-next/${this.networkId}/stream`, {
1690
1816
  method: "POST",
1691
- body: params,
1817
+ body: {
1818
+ ...params,
1819
+ runtimeContext: parseClientRuntimeContext(params.runtimeContext)
1820
+ },
1692
1821
  stream: true
1693
1822
  });
1694
1823
  if (!response.ok) {
@@ -1705,53 +1834,33 @@ var VNextNetwork = class extends BaseResource {
1705
1834
  }
1706
1835
  }
1707
1836
  }
1708
- };
1709
-
1710
- // src/resources/network-memory-thread.ts
1711
- var NetworkMemoryThread = class extends BaseResource {
1712
- constructor(options, threadId, networkId) {
1713
- super(options);
1714
- this.threadId = threadId;
1715
- this.networkId = networkId;
1716
- }
1717
- /**
1718
- * Retrieves the memory thread details
1719
- * @returns Promise containing thread details including title and metadata
1720
- */
1721
- get() {
1722
- return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`);
1723
- }
1724
- /**
1725
- * Updates the memory thread properties
1726
- * @param params - Update parameters including title and metadata
1727
- * @returns Promise containing updated thread details
1728
- */
1729
- update(params) {
1730
- return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
1731
- method: "PATCH",
1732
- body: params
1733
- });
1734
- }
1735
1837
  /**
1736
- * Deletes the memory thread
1737
- * @returns Promise containing deletion result
1738
- */
1739
- delete() {
1740
- return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
1741
- method: "DELETE"
1742
- });
1743
- }
1744
- /**
1745
- * Retrieves messages associated with the thread
1746
- * @param params - Optional parameters including limit for number of messages to retrieve
1747
- * @returns Promise containing thread messages and UI messages
1838
+ * Streams a response from the v-next network loop
1839
+ * @param params - Stream parameters including message
1840
+ * @returns Promise containing the results
1748
1841
  */
1749
- getMessages(params) {
1750
- const query = new URLSearchParams({
1751
- networkId: this.networkId,
1752
- ...params?.limit ? { limit: params.limit.toString() } : {}
1842
+ async loopStream(params, onRecord) {
1843
+ const response = await this.request(`/api/networks/v-next/${this.networkId}/loop-stream`, {
1844
+ method: "POST",
1845
+ body: {
1846
+ ...params,
1847
+ runtimeContext: parseClientRuntimeContext(params.runtimeContext)
1848
+ },
1849
+ stream: true
1753
1850
  });
1754
- return this.request(`/api/memory/network/threads/${this.threadId}/messages?${query.toString()}`);
1851
+ if (!response.ok) {
1852
+ throw new Error(`Failed to stream vNext network loop: ${response.statusText}`);
1853
+ }
1854
+ if (!response.body) {
1855
+ throw new Error("Response body is null");
1856
+ }
1857
+ for await (const record of this.streamProcessor(response.body)) {
1858
+ if (typeof record === "string") {
1859
+ onRecord(JSON.parse(record));
1860
+ } else {
1861
+ onRecord(record);
1862
+ }
1863
+ }
1755
1864
  }
1756
1865
  };
1757
1866
 
@@ -2149,6 +2258,41 @@ var MastraClient = class extends BaseResource {
2149
2258
  getA2A(agentId) {
2150
2259
  return new A2A(this.options, agentId);
2151
2260
  }
2261
+ /**
2262
+ * Retrieves the working memory for a specific thread (optionally resource-scoped).
2263
+ * @param agentId - ID of the agent.
2264
+ * @param threadId - ID of the thread.
2265
+ * @param resourceId - Optional ID of the resource.
2266
+ * @returns Working memory for the specified thread or resource.
2267
+ */
2268
+ getWorkingMemory({
2269
+ agentId,
2270
+ threadId,
2271
+ resourceId
2272
+ }) {
2273
+ return this.request(`/api/memory/threads/${threadId}/working-memory?agentId=${agentId}&resourceId=${resourceId}`);
2274
+ }
2275
+ /**
2276
+ * Updates the working memory for a specific thread (optionally resource-scoped).
2277
+ * @param agentId - ID of the agent.
2278
+ * @param threadId - ID of the thread.
2279
+ * @param workingMemory - The new working memory content.
2280
+ * @param resourceId - Optional ID of the resource.
2281
+ */
2282
+ updateWorkingMemory({
2283
+ agentId,
2284
+ threadId,
2285
+ workingMemory,
2286
+ resourceId
2287
+ }) {
2288
+ return this.request(`/api/memory/threads/${threadId}/working-memory?agentId=${agentId}`, {
2289
+ method: "POST",
2290
+ body: {
2291
+ workingMemory,
2292
+ resourceId
2293
+ }
2294
+ });
2295
+ }
2152
2296
  };
2153
2297
 
2154
2298
  exports.MastraClient = MastraClient;