@mastra/client-js 0.0.0-tool-call-parts-20250630193309 → 0.0.0-transpile-packages-20250724123433

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 }; }
@@ -252,12 +253,13 @@ var BaseResource = class {
252
253
  const response = await fetch(`${baseUrl.replace(/\/$/, "")}${path}`, {
253
254
  ...options,
254
255
  headers: {
255
- ...options.method === "POST" || options.method === "PUT" ? { "content-type": "application/json" } : {},
256
+ ...options.body && (options.method === "POST" || options.method === "PUT") ? { "content-type": "application/json" } : {},
256
257
  ...headers,
257
258
  ...options.headers
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,6 +713,7 @@ 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
718
  let messages = [];
726
719
  const [streamForWritable, streamForProcessing] = response.body.tee();
@@ -774,6 +767,19 @@ var Agent = class extends BaseResource {
774
767
  toolInvocation.state = "result";
775
768
  toolInvocation.result = result;
776
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
+ }
777
783
  const originalMessages = processedParams.messages;
778
784
  const messageArray = Array.isArray(originalMessages) ? originalMessages : [originalMessages];
779
785
  this.processStreamResponse(
@@ -787,17 +793,55 @@ var Agent = class extends BaseResource {
787
793
  }
788
794
  } else {
789
795
  setTimeout(() => {
790
- writable.close();
796
+ if (!writable.locked) {
797
+ writable.close();
798
+ }
791
799
  }, 0);
792
800
  }
793
801
  },
794
- lastMessage: void 0
802
+ lastMessage: void 0,
803
+ streamProtocol
795
804
  });
796
805
  } catch (error) {
797
806
  console.error("Error processing stream response:", error);
798
807
  }
799
808
  return response;
800
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
+ }
801
845
  /**
802
846
  * Gets details about a specific tool available to the agent
803
847
  * @param toolId - ID of the tool to retrieve
@@ -940,6 +984,21 @@ var MemoryThread = class extends BaseResource {
940
984
  });
941
985
  return this.request(`/api/memory/threads/${this.threadId}/messages?${query.toString()}`);
942
986
  }
987
+ /**
988
+ * Retrieves paginated messages associated with the thread with advanced filtering and selection options
989
+ * @param params - Pagination parameters including selectBy criteria, page, perPage, date ranges, and message inclusion options
990
+ * @returns Promise containing paginated thread messages with pagination metadata (total, page, perPage, hasMore)
991
+ */
992
+ getMessagesPaginated({
993
+ selectBy,
994
+ ...rest
995
+ }) {
996
+ const query = new URLSearchParams({
997
+ ...rest,
998
+ ...selectBy ? { selectBy: JSON.stringify(selectBy) } : {}
999
+ });
1000
+ return this.request(`/api/memory/threads/${this.threadId}/messages/paginated?${query.toString()}`);
1001
+ }
943
1002
  };
944
1003
 
945
1004
  // src/resources/vector.ts
@@ -1295,10 +1354,10 @@ var Workflow = class extends BaseResource {
1295
1354
  if (params?.toDate) {
1296
1355
  searchParams.set("toDate", params.toDate.toISOString());
1297
1356
  }
1298
- if (params?.limit) {
1357
+ if (params?.limit !== null && params?.limit !== void 0 && !isNaN(Number(params?.limit))) {
1299
1358
  searchParams.set("limit", String(params.limit));
1300
1359
  }
1301
- if (params?.offset) {
1360
+ if (params?.offset !== null && params?.offset !== void 0 && !isNaN(Number(params?.offset))) {
1302
1361
  searchParams.set("offset", String(params.offset));
1303
1362
  }
1304
1363
  if (params?.resourceId) {
@@ -1326,6 +1385,27 @@ var Workflow = class extends BaseResource {
1326
1385
  runExecutionResult(runId) {
1327
1386
  return this.request(`/api/workflows/${this.workflowId}/runs/${runId}/execution-result`);
1328
1387
  }
1388
+ /**
1389
+ * Cancels a specific workflow run by its ID
1390
+ * @param runId - The ID of the workflow run to cancel
1391
+ * @returns Promise containing a success message
1392
+ */
1393
+ cancelRun(runId) {
1394
+ return this.request(`/api/workflows/${this.workflowId}/runs/${runId}/cancel`, {
1395
+ method: "POST"
1396
+ });
1397
+ }
1398
+ /**
1399
+ * Sends an event to a specific workflow run by its ID
1400
+ * @param params - Object containing the runId, event and data
1401
+ * @returns Promise containing a success message
1402
+ */
1403
+ sendRunEvent(params) {
1404
+ return this.request(`/api/workflows/${this.workflowId}/runs/${params.runId}/send-event`, {
1405
+ method: "POST",
1406
+ body: { event: params.event, data: params.data }
1407
+ });
1408
+ }
1329
1409
  /**
1330
1410
  * Creates a new workflow run
1331
1411
  * @param params - Optional object containing the optional runId
@@ -1340,6 +1420,14 @@ var Workflow = class extends BaseResource {
1340
1420
  method: "POST"
1341
1421
  });
1342
1422
  }
1423
+ /**
1424
+ * Creates a new workflow run (alias for createRun)
1425
+ * @param params - Optional object containing the optional runId
1426
+ * @returns Promise containing the runId of the created run
1427
+ */
1428
+ createRunAsync(params) {
1429
+ return this.createRun(params);
1430
+ }
1343
1431
  /**
1344
1432
  * Starts a workflow run synchronously without waiting for the workflow to complete
1345
1433
  * @param params - Object containing the runId, inputData and runtimeContext
@@ -1415,6 +1503,7 @@ var Workflow = class extends BaseResource {
1415
1503
  if (!response.body) {
1416
1504
  throw new Error("Response body is null");
1417
1505
  }
1506
+ let failedChunk = void 0;
1418
1507
  const transformStream = new TransformStream({
1419
1508
  start() {
1420
1509
  },
@@ -1424,10 +1513,13 @@ var Workflow = class extends BaseResource {
1424
1513
  const chunks = decoded.split(RECORD_SEPARATOR2);
1425
1514
  for (const chunk2 of chunks) {
1426
1515
  if (chunk2) {
1516
+ const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
1427
1517
  try {
1428
- const parsedChunk = JSON.parse(chunk2);
1518
+ const parsedChunk = JSON.parse(newChunk);
1429
1519
  controller.enqueue(parsedChunk);
1430
- } catch {
1520
+ failedChunk = void 0;
1521
+ } catch (error) {
1522
+ failedChunk = newChunk;
1431
1523
  }
1432
1524
  }
1433
1525
  }
@@ -1609,6 +1701,54 @@ var MCPTool = class extends BaseResource {
1609
1701
  }
1610
1702
  };
1611
1703
 
1704
+ // src/resources/network-memory-thread.ts
1705
+ var NetworkMemoryThread = class extends BaseResource {
1706
+ constructor(options, threadId, networkId) {
1707
+ super(options);
1708
+ this.threadId = threadId;
1709
+ this.networkId = networkId;
1710
+ }
1711
+ /**
1712
+ * Retrieves the memory thread details
1713
+ * @returns Promise containing thread details including title and metadata
1714
+ */
1715
+ get() {
1716
+ return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`);
1717
+ }
1718
+ /**
1719
+ * Updates the memory thread properties
1720
+ * @param params - Update parameters including title and metadata
1721
+ * @returns Promise containing updated thread details
1722
+ */
1723
+ update(params) {
1724
+ return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
1725
+ method: "PATCH",
1726
+ body: params
1727
+ });
1728
+ }
1729
+ /**
1730
+ * Deletes the memory thread
1731
+ * @returns Promise containing deletion result
1732
+ */
1733
+ delete() {
1734
+ return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
1735
+ method: "DELETE"
1736
+ });
1737
+ }
1738
+ /**
1739
+ * Retrieves messages associated with the thread
1740
+ * @param params - Optional parameters including limit for number of messages to retrieve
1741
+ * @returns Promise containing thread messages and UI messages
1742
+ */
1743
+ getMessages(params) {
1744
+ const query = new URLSearchParams({
1745
+ networkId: this.networkId,
1746
+ ...params?.limit ? { limit: params.limit.toString() } : {}
1747
+ });
1748
+ return this.request(`/api/memory/network/threads/${this.threadId}/messages?${query.toString()}`);
1749
+ }
1750
+ };
1751
+
1612
1752
  // src/resources/vNextNetwork.ts
1613
1753
  var RECORD_SEPARATOR3 = "";
1614
1754
  var VNextNetwork = class extends BaseResource {
@@ -1631,7 +1771,10 @@ var VNextNetwork = class extends BaseResource {
1631
1771
  generate(params) {
1632
1772
  return this.request(`/api/networks/v-next/${this.networkId}/generate`, {
1633
1773
  method: "POST",
1634
- body: params
1774
+ body: {
1775
+ ...params,
1776
+ runtimeContext: parseClientRuntimeContext(params.runtimeContext)
1777
+ }
1635
1778
  });
1636
1779
  }
1637
1780
  /**
@@ -1642,7 +1785,10 @@ var VNextNetwork = class extends BaseResource {
1642
1785
  loop(params) {
1643
1786
  return this.request(`/api/networks/v-next/${this.networkId}/loop`, {
1644
1787
  method: "POST",
1645
- body: params
1788
+ body: {
1789
+ ...params,
1790
+ runtimeContext: parseClientRuntimeContext(params.runtimeContext)
1791
+ }
1646
1792
  });
1647
1793
  }
1648
1794
  async *streamProcessor(stream) {
@@ -1691,7 +1837,10 @@ var VNextNetwork = class extends BaseResource {
1691
1837
  async stream(params, onRecord) {
1692
1838
  const response = await this.request(`/api/networks/v-next/${this.networkId}/stream`, {
1693
1839
  method: "POST",
1694
- body: params,
1840
+ body: {
1841
+ ...params,
1842
+ runtimeContext: parseClientRuntimeContext(params.runtimeContext)
1843
+ },
1695
1844
  stream: true
1696
1845
  });
1697
1846
  if (!response.ok) {
@@ -1716,7 +1865,10 @@ var VNextNetwork = class extends BaseResource {
1716
1865
  async loopStream(params, onRecord) {
1717
1866
  const response = await this.request(`/api/networks/v-next/${this.networkId}/loop-stream`, {
1718
1867
  method: "POST",
1719
- body: params,
1868
+ body: {
1869
+ ...params,
1870
+ runtimeContext: parseClientRuntimeContext(params.runtimeContext)
1871
+ },
1720
1872
  stream: true
1721
1873
  });
1722
1874
  if (!response.ok) {
@@ -1735,54 +1887,6 @@ var VNextNetwork = class extends BaseResource {
1735
1887
  }
1736
1888
  };
1737
1889
 
1738
- // src/resources/network-memory-thread.ts
1739
- var NetworkMemoryThread = class extends BaseResource {
1740
- constructor(options, threadId, networkId) {
1741
- super(options);
1742
- this.threadId = threadId;
1743
- this.networkId = networkId;
1744
- }
1745
- /**
1746
- * Retrieves the memory thread details
1747
- * @returns Promise containing thread details including title and metadata
1748
- */
1749
- get() {
1750
- return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`);
1751
- }
1752
- /**
1753
- * Updates the memory thread properties
1754
- * @param params - Update parameters including title and metadata
1755
- * @returns Promise containing updated thread details
1756
- */
1757
- update(params) {
1758
- return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
1759
- method: "PATCH",
1760
- body: params
1761
- });
1762
- }
1763
- /**
1764
- * Deletes the memory thread
1765
- * @returns Promise containing deletion result
1766
- */
1767
- delete() {
1768
- return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
1769
- method: "DELETE"
1770
- });
1771
- }
1772
- /**
1773
- * Retrieves messages associated with the thread
1774
- * @param params - Optional parameters including limit for number of messages to retrieve
1775
- * @returns Promise containing thread messages and UI messages
1776
- */
1777
- getMessages(params) {
1778
- const query = new URLSearchParams({
1779
- networkId: this.networkId,
1780
- ...params?.limit ? { limit: params.limit.toString() } : {}
1781
- });
1782
- return this.request(`/api/memory/network/threads/${this.threadId}/messages?${query.toString()}`);
1783
- }
1784
- };
1785
-
1786
1890
  // src/client.ts
1787
1891
  var MastraClient = class extends BaseResource {
1788
1892
  constructor(options) {
@@ -2177,6 +2281,41 @@ var MastraClient = class extends BaseResource {
2177
2281
  getA2A(agentId) {
2178
2282
  return new A2A(this.options, agentId);
2179
2283
  }
2284
+ /**
2285
+ * Retrieves the working memory for a specific thread (optionally resource-scoped).
2286
+ * @param agentId - ID of the agent.
2287
+ * @param threadId - ID of the thread.
2288
+ * @param resourceId - Optional ID of the resource.
2289
+ * @returns Working memory for the specified thread or resource.
2290
+ */
2291
+ getWorkingMemory({
2292
+ agentId,
2293
+ threadId,
2294
+ resourceId
2295
+ }) {
2296
+ return this.request(`/api/memory/threads/${threadId}/working-memory?agentId=${agentId}&resourceId=${resourceId}`);
2297
+ }
2298
+ /**
2299
+ * Updates the working memory for a specific thread (optionally resource-scoped).
2300
+ * @param agentId - ID of the agent.
2301
+ * @param threadId - ID of the thread.
2302
+ * @param workingMemory - The new working memory content.
2303
+ * @param resourceId - Optional ID of the resource.
2304
+ */
2305
+ updateWorkingMemory({
2306
+ agentId,
2307
+ threadId,
2308
+ workingMemory,
2309
+ resourceId
2310
+ }) {
2311
+ return this.request(`/api/memory/threads/${threadId}/working-memory?agentId=${agentId}`, {
2312
+ method: "POST",
2313
+ body: {
2314
+ workingMemory,
2315
+ resourceId
2316
+ }
2317
+ });
2318
+ }
2180
2319
  };
2181
2320
 
2182
2321
  exports.MastraClient = MastraClient;