@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.js CHANGED
@@ -1,9 +1,10 @@
1
1
  import { AbstractAgent, EventType } from '@ag-ui/client';
2
2
  import { Observable } from 'rxjs';
3
- import { processDataStream, parsePartialJson } 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,12 +247,13 @@ var BaseResource = class {
246
247
  const response = await fetch(`${baseUrl.replace(/\/$/, "")}${path}`, {
247
248
  ...options,
248
249
  headers: {
249
- ...options.method === "POST" || options.method === "PUT" ? { "content-type": "application/json" } : {},
250
+ ...options.body && (options.method === "POST" || options.method === "PUT") ? { "content-type": "application/json" } : {},
250
251
  ...headers,
251
252
  ...options.headers
252
253
  // TODO: Bring this back once we figure out what we/users need to do to make this work with cross-origin requests
253
254
  // 'x-mastra-client-type': 'js',
254
255
  },
256
+ signal: this.options.abortSignal,
255
257
  body: options.body instanceof FormData ? options.body : options.body ? JSON.stringify(options.body) : void 0
256
258
  });
257
259
  if (!response.ok) {
@@ -375,7 +377,11 @@ var Agent = class extends BaseResource {
375
377
  body: processedParams
376
378
  });
377
379
  if (response.finishReason === "tool-calls") {
378
- for (const toolCall of response.toolCalls) {
380
+ const toolCalls = response.toolCalls;
381
+ if (!toolCalls || !Array.isArray(toolCalls)) {
382
+ return response;
383
+ }
384
+ for (const toolCall of toolCalls) {
379
385
  const clientTool = params.clientTools?.[toolCall.toolName];
380
386
  if (clientTool && clientTool.execute) {
381
387
  const result = await clientTool.execute(
@@ -418,7 +424,8 @@ var Agent = class extends BaseResource {
418
424
  onToolCall,
419
425
  onFinish,
420
426
  getCurrentDate = () => /* @__PURE__ */ new Date(),
421
- lastMessage
427
+ lastMessage,
428
+ streamProtocol
422
429
  }) {
423
430
  const replaceLastMessage = lastMessage?.role === "assistant";
424
431
  let step = replaceLastMessage ? 1 + // find max step in existing tool invocations:
@@ -426,7 +433,7 @@ var Agent = class extends BaseResource {
426
433
  return Math.max(max, toolInvocation.step ?? 0);
427
434
  }, 0) ?? 0) : 0;
428
435
  const message = replaceLastMessage ? structuredClone(lastMessage) : {
429
- id: crypto.randomUUID(),
436
+ id: v4(),
430
437
  createdAt: getCurrentDate(),
431
438
  role: "assistant",
432
439
  content: "",
@@ -471,7 +478,7 @@ var Agent = class extends BaseResource {
471
478
  // changes. This is why we need to add a revision id to ensure that the message
472
479
  // is updated with SWR (without it, the changes get stuck in SWR and are not
473
480
  // forwarded to rendering):
474
- revisionId: crypto.randomUUID()
481
+ revisionId: v4()
475
482
  };
476
483
  update({
477
484
  message: copiedMessage,
@@ -479,228 +486,213 @@ var Agent = class extends BaseResource {
479
486
  replaceLastMessage
480
487
  });
481
488
  }
482
- await processDataStream({
483
- stream,
484
- onTextPart(value) {
485
- if (currentTextPart == null) {
486
- currentTextPart = {
487
- type: "text",
488
- text: value
489
- };
490
- message.parts.push(currentTextPart);
491
- } else {
492
- currentTextPart.text += value;
489
+ if (streamProtocol === "text") {
490
+ await processTextStream({
491
+ stream,
492
+ onTextPart(value) {
493
+ message.content += value;
494
+ execUpdate();
493
495
  }
494
- message.content += value;
495
- execUpdate();
496
- },
497
- onReasoningPart(value) {
498
- if (currentReasoningTextDetail == null) {
499
- currentReasoningTextDetail = { type: "text", text: value };
500
- if (currentReasoningPart != null) {
501
- currentReasoningPart.details.push(currentReasoningTextDetail);
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;
502
510
  }
503
- } else {
504
- currentReasoningTextDetail.text += value;
505
- }
506
- if (currentReasoningPart == null) {
507
- currentReasoningPart = {
508
- type: "reasoning",
509
- reasoning: value,
510
- details: [currentReasoningTextDetail]
511
- };
512
- message.parts.push(currentReasoningPart);
513
- } else {
514
- currentReasoningPart.reasoning += value;
515
- }
516
- message.reasoning = (message.reasoning ?? "") + value;
517
- execUpdate();
518
- },
519
- onReasoningSignaturePart(value) {
520
- if (currentReasoningTextDetail != null) {
521
- currentReasoningTextDetail.signature = value.signature;
522
- }
523
- },
524
- onRedactedReasoningPart(value) {
525
- if (currentReasoningPart == null) {
526
- currentReasoningPart = {
527
- type: "reasoning",
528
- reasoning: "",
529
- details: []
530
- };
531
- message.parts.push(currentReasoningPart);
532
- }
533
- currentReasoningPart.details.push({
534
- type: "redacted",
535
- data: value.data
536
- });
537
- currentReasoningTextDetail = void 0;
538
- execUpdate();
539
- },
540
- onFilePart(value) {
541
- message.parts.push({
542
- type: "file",
543
- mimeType: value.mimeType,
544
- data: value.data
545
- });
546
- execUpdate();
547
- },
548
- onSourcePart(value) {
549
- message.parts.push({
550
- type: "source",
551
- source: value
552
- });
553
- execUpdate();
554
- },
555
- onToolCallStreamingStartPart(value) {
556
- if (message.toolInvocations == null) {
557
- message.toolInvocations = [];
558
- }
559
- partialToolCalls[value.toolCallId] = {
560
- text: "",
561
- step,
562
- toolName: value.toolName,
563
- index: message.toolInvocations.length
564
- };
565
- const invocation = {
566
- state: "partial-call",
567
- step,
568
- toolCallId: value.toolCallId,
569
- toolName: value.toolName,
570
- args: void 0
571
- };
572
- message.toolInvocations.push(invocation);
573
- updateToolInvocationPart(value.toolCallId, invocation);
574
- execUpdate();
575
- },
576
- onToolCallDeltaPart(value) {
577
- const partialToolCall = partialToolCalls[value.toolCallId];
578
- partialToolCall.text += value.argsTextDelta;
579
- const { value: partialArgs } = parsePartialJson(partialToolCall.text);
580
- const invocation = {
581
- state: "partial-call",
582
- step: partialToolCall.step,
583
- toolCallId: value.toolCallId,
584
- toolName: partialToolCall.toolName,
585
- args: partialArgs
586
- };
587
- message.toolInvocations[partialToolCall.index] = invocation;
588
- updateToolInvocationPart(value.toolCallId, invocation);
589
- execUpdate();
590
- },
591
- async onToolCallPart(value) {
592
- const invocation = {
593
- state: "call",
594
- step,
595
- ...value
596
- };
597
- if (partialToolCalls[value.toolCallId] != null) {
598
- message.toolInvocations[partialToolCalls[value.toolCallId].index] = invocation;
599
- } else {
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) {
600
573
  if (message.toolInvocations == null) {
601
574
  message.toolInvocations = [];
602
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
+ };
603
589
  message.toolInvocations.push(invocation);
604
- }
605
- updateToolInvocationPart(value.toolCallId, invocation);
606
- execUpdate();
607
- if (onToolCall) {
608
- const result = await onToolCall({ toolCall: value });
609
- if (result != null) {
610
- const invocation2 = {
611
- state: "result",
612
- step,
613
- ...value,
614
- result
615
- };
616
- message.toolInvocations[message.toolInvocations.length - 1] = invocation2;
617
- updateToolInvocationPart(value.toolCallId, invocation2);
618
- execUpdate();
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);
619
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);
620
692
  }
621
- },
622
- onToolResultPart(value) {
623
- const toolInvocations = message.toolInvocations;
624
- if (toolInvocations == null) {
625
- throw new Error("tool_result must be preceded by a tool_call");
626
- }
627
- const toolInvocationIndex = toolInvocations.findIndex((invocation2) => invocation2.toolCallId === value.toolCallId);
628
- if (toolInvocationIndex === -1) {
629
- throw new Error("tool_result must be preceded by a tool_call with the same toolCallId");
630
- }
631
- const invocation = {
632
- ...toolInvocations[toolInvocationIndex],
633
- state: "result",
634
- ...value
635
- };
636
- toolInvocations[toolInvocationIndex] = invocation;
637
- updateToolInvocationPart(value.toolCallId, invocation);
638
- execUpdate();
639
- },
640
- onDataPart(value) {
641
- data.push(...value);
642
- execUpdate();
643
- },
644
- onMessageAnnotationsPart(value) {
645
- if (messageAnnotations == null) {
646
- messageAnnotations = [...value];
647
- } else {
648
- messageAnnotations.push(...value);
649
- }
650
- execUpdate();
651
- },
652
- onFinishStepPart(value) {
653
- step += 1;
654
- currentTextPart = value.isContinued ? currentTextPart : void 0;
655
- currentReasoningPart = void 0;
656
- currentReasoningTextDetail = void 0;
657
- },
658
- onStartStepPart(value) {
659
- if (!replaceLastMessage) {
660
- message.id = value.messageId;
661
- }
662
- message.parts.push({ type: "step-start" });
663
- execUpdate();
664
- },
665
- onFinishMessagePart(value) {
666
- finishReason = value.finishReason;
667
- if (value.usage != null) {
668
- usage = value.usage;
669
- }
670
- },
671
- onErrorPart(error) {
672
- throw new Error(error);
673
- }
674
- });
675
- onFinish?.({ message, finishReason, usage });
676
- }
677
- /**
678
- * Streams a response from the agent
679
- * @param params - Stream parameters including prompt
680
- * @returns Promise containing the enhanced Response object with processDataStream method
681
- */
682
- async stream(params) {
683
- const processedParams = {
684
- ...params,
685
- output: params.output ? zodToJsonSchema(params.output) : void 0,
686
- experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
687
- runtimeContext: parseClientRuntimeContext(params.runtimeContext),
688
- clientTools: processClientTools(params.clientTools)
689
- };
690
- const { readable, writable } = new TransformStream();
691
- const response = await this.processStreamResponse(processedParams, writable);
692
- const streamResponse = new Response(readable, {
693
- status: response.status,
694
- statusText: response.statusText,
695
- headers: response.headers
696
- });
697
- streamResponse.processDataStream = async (options = {}) => {
698
- await processDataStream({
699
- stream: streamResponse.body,
700
- ...options
701
693
  });
702
- };
703
- return streamResponse;
694
+ onFinish?.({ message, finishReason, usage });
695
+ }
704
696
  }
705
697
  /**
706
698
  * Processes the stream response and handles tool calls
@@ -715,6 +707,7 @@ var Agent = class extends BaseResource {
715
707
  throw new Error("No response body");
716
708
  }
717
709
  try {
710
+ const streamProtocol = processedParams.output ? "text" : "data";
718
711
  let toolCalls = [];
719
712
  let messages = [];
720
713
  const [streamForWritable, streamForProcessing] = response.body.tee();
@@ -768,6 +761,19 @@ var Agent = class extends BaseResource {
768
761
  toolInvocation.state = "result";
769
762
  toolInvocation.result = result;
770
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
+ }
771
777
  const originalMessages = processedParams.messages;
772
778
  const messageArray = Array.isArray(originalMessages) ? originalMessages : [originalMessages];
773
779
  this.processStreamResponse(
@@ -781,17 +787,55 @@ var Agent = class extends BaseResource {
781
787
  }
782
788
  } else {
783
789
  setTimeout(() => {
784
- writable.close();
790
+ if (!writable.locked) {
791
+ writable.close();
792
+ }
785
793
  }, 0);
786
794
  }
787
795
  },
788
- lastMessage: void 0
796
+ lastMessage: void 0,
797
+ streamProtocol
789
798
  });
790
799
  } catch (error) {
791
800
  console.error("Error processing stream response:", error);
792
801
  }
793
802
  return response;
794
803
  }
804
+ /**
805
+ * Streams a response from the agent
806
+ * @param params - Stream parameters including prompt
807
+ * @returns Promise containing the enhanced Response object with processDataStream and processTextStream methods
808
+ */
809
+ async stream(params) {
810
+ const processedParams = {
811
+ ...params,
812
+ output: params.output ? zodToJsonSchema(params.output) : void 0,
813
+ experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
814
+ runtimeContext: parseClientRuntimeContext(params.runtimeContext),
815
+ clientTools: processClientTools(params.clientTools)
816
+ };
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
823
+ });
824
+ streamResponse.processDataStream = async (options = {}) => {
825
+ await processDataStream({
826
+ stream: streamResponse.body,
827
+ ...options
828
+ });
829
+ };
830
+ streamResponse.processTextStream = async (options) => {
831
+ await processTextStream({
832
+ stream: streamResponse.body,
833
+ onTextPart: options?.onTextPart ?? (() => {
834
+ })
835
+ });
836
+ };
837
+ return streamResponse;
838
+ }
795
839
  /**
796
840
  * Gets details about a specific tool available to the agent
797
841
  * @param toolId - ID of the tool to retrieve
@@ -934,6 +978,21 @@ var MemoryThread = class extends BaseResource {
934
978
  });
935
979
  return this.request(`/api/memory/threads/${this.threadId}/messages?${query.toString()}`);
936
980
  }
981
+ /**
982
+ * Retrieves paginated messages associated with the thread with advanced filtering and selection options
983
+ * @param params - Pagination parameters including selectBy criteria, page, perPage, date ranges, and message inclusion options
984
+ * @returns Promise containing paginated thread messages with pagination metadata (total, page, perPage, hasMore)
985
+ */
986
+ getMessagesPaginated({
987
+ selectBy,
988
+ ...rest
989
+ }) {
990
+ const query = new URLSearchParams({
991
+ ...rest,
992
+ ...selectBy ? { selectBy: JSON.stringify(selectBy) } : {}
993
+ });
994
+ return this.request(`/api/memory/threads/${this.threadId}/messages/paginated?${query.toString()}`);
995
+ }
937
996
  };
938
997
 
939
998
  // src/resources/vector.ts
@@ -1289,10 +1348,10 @@ var Workflow = class extends BaseResource {
1289
1348
  if (params?.toDate) {
1290
1349
  searchParams.set("toDate", params.toDate.toISOString());
1291
1350
  }
1292
- if (params?.limit) {
1351
+ if (params?.limit !== null && params?.limit !== void 0 && !isNaN(Number(params?.limit))) {
1293
1352
  searchParams.set("limit", String(params.limit));
1294
1353
  }
1295
- if (params?.offset) {
1354
+ if (params?.offset !== null && params?.offset !== void 0 && !isNaN(Number(params?.offset))) {
1296
1355
  searchParams.set("offset", String(params.offset));
1297
1356
  }
1298
1357
  if (params?.resourceId) {
@@ -1320,6 +1379,27 @@ var Workflow = class extends BaseResource {
1320
1379
  runExecutionResult(runId) {
1321
1380
  return this.request(`/api/workflows/${this.workflowId}/runs/${runId}/execution-result`);
1322
1381
  }
1382
+ /**
1383
+ * Cancels a specific workflow run by its ID
1384
+ * @param runId - The ID of the workflow run to cancel
1385
+ * @returns Promise containing a success message
1386
+ */
1387
+ cancelRun(runId) {
1388
+ return this.request(`/api/workflows/${this.workflowId}/runs/${runId}/cancel`, {
1389
+ method: "POST"
1390
+ });
1391
+ }
1392
+ /**
1393
+ * Sends an event to a specific workflow run by its ID
1394
+ * @param params - Object containing the runId, event and data
1395
+ * @returns Promise containing a success message
1396
+ */
1397
+ sendRunEvent(params) {
1398
+ return this.request(`/api/workflows/${this.workflowId}/runs/${params.runId}/send-event`, {
1399
+ method: "POST",
1400
+ body: { event: params.event, data: params.data }
1401
+ });
1402
+ }
1323
1403
  /**
1324
1404
  * Creates a new workflow run
1325
1405
  * @param params - Optional object containing the optional runId
@@ -1334,6 +1414,14 @@ var Workflow = class extends BaseResource {
1334
1414
  method: "POST"
1335
1415
  });
1336
1416
  }
1417
+ /**
1418
+ * Creates a new workflow run (alias for createRun)
1419
+ * @param params - Optional object containing the optional runId
1420
+ * @returns Promise containing the runId of the created run
1421
+ */
1422
+ createRunAsync(params) {
1423
+ return this.createRun(params);
1424
+ }
1337
1425
  /**
1338
1426
  * Starts a workflow run synchronously without waiting for the workflow to complete
1339
1427
  * @param params - Object containing the runId, inputData and runtimeContext
@@ -1409,6 +1497,7 @@ var Workflow = class extends BaseResource {
1409
1497
  if (!response.body) {
1410
1498
  throw new Error("Response body is null");
1411
1499
  }
1500
+ let failedChunk = void 0;
1412
1501
  const transformStream = new TransformStream({
1413
1502
  start() {
1414
1503
  },
@@ -1418,10 +1507,13 @@ var Workflow = class extends BaseResource {
1418
1507
  const chunks = decoded.split(RECORD_SEPARATOR2);
1419
1508
  for (const chunk2 of chunks) {
1420
1509
  if (chunk2) {
1510
+ const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
1421
1511
  try {
1422
- const parsedChunk = JSON.parse(chunk2);
1512
+ const parsedChunk = JSON.parse(newChunk);
1423
1513
  controller.enqueue(parsedChunk);
1424
- } catch {
1514
+ failedChunk = void 0;
1515
+ } catch (error) {
1516
+ failedChunk = newChunk;
1425
1517
  }
1426
1518
  }
1427
1519
  }
@@ -1603,6 +1695,54 @@ var MCPTool = class extends BaseResource {
1603
1695
  }
1604
1696
  };
1605
1697
 
1698
+ // src/resources/network-memory-thread.ts
1699
+ var NetworkMemoryThread = class extends BaseResource {
1700
+ constructor(options, threadId, networkId) {
1701
+ super(options);
1702
+ this.threadId = threadId;
1703
+ this.networkId = networkId;
1704
+ }
1705
+ /**
1706
+ * Retrieves the memory thread details
1707
+ * @returns Promise containing thread details including title and metadata
1708
+ */
1709
+ get() {
1710
+ return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`);
1711
+ }
1712
+ /**
1713
+ * Updates the memory thread properties
1714
+ * @param params - Update parameters including title and metadata
1715
+ * @returns Promise containing updated thread details
1716
+ */
1717
+ update(params) {
1718
+ return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
1719
+ method: "PATCH",
1720
+ body: params
1721
+ });
1722
+ }
1723
+ /**
1724
+ * Deletes the memory thread
1725
+ * @returns Promise containing deletion result
1726
+ */
1727
+ delete() {
1728
+ return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
1729
+ method: "DELETE"
1730
+ });
1731
+ }
1732
+ /**
1733
+ * Retrieves messages associated with the thread
1734
+ * @param params - Optional parameters including limit for number of messages to retrieve
1735
+ * @returns Promise containing thread messages and UI messages
1736
+ */
1737
+ getMessages(params) {
1738
+ const query = new URLSearchParams({
1739
+ networkId: this.networkId,
1740
+ ...params?.limit ? { limit: params.limit.toString() } : {}
1741
+ });
1742
+ return this.request(`/api/memory/network/threads/${this.threadId}/messages?${query.toString()}`);
1743
+ }
1744
+ };
1745
+
1606
1746
  // src/resources/vNextNetwork.ts
1607
1747
  var RECORD_SEPARATOR3 = "";
1608
1748
  var VNextNetwork = class extends BaseResource {
@@ -1625,7 +1765,10 @@ var VNextNetwork = class extends BaseResource {
1625
1765
  generate(params) {
1626
1766
  return this.request(`/api/networks/v-next/${this.networkId}/generate`, {
1627
1767
  method: "POST",
1628
- body: params
1768
+ body: {
1769
+ ...params,
1770
+ runtimeContext: parseClientRuntimeContext(params.runtimeContext)
1771
+ }
1629
1772
  });
1630
1773
  }
1631
1774
  /**
@@ -1636,7 +1779,10 @@ var VNextNetwork = class extends BaseResource {
1636
1779
  loop(params) {
1637
1780
  return this.request(`/api/networks/v-next/${this.networkId}/loop`, {
1638
1781
  method: "POST",
1639
- body: params
1782
+ body: {
1783
+ ...params,
1784
+ runtimeContext: parseClientRuntimeContext(params.runtimeContext)
1785
+ }
1640
1786
  });
1641
1787
  }
1642
1788
  async *streamProcessor(stream) {
@@ -1685,7 +1831,10 @@ var VNextNetwork = class extends BaseResource {
1685
1831
  async stream(params, onRecord) {
1686
1832
  const response = await this.request(`/api/networks/v-next/${this.networkId}/stream`, {
1687
1833
  method: "POST",
1688
- body: params,
1834
+ body: {
1835
+ ...params,
1836
+ runtimeContext: parseClientRuntimeContext(params.runtimeContext)
1837
+ },
1689
1838
  stream: true
1690
1839
  });
1691
1840
  if (!response.ok) {
@@ -1710,7 +1859,10 @@ var VNextNetwork = class extends BaseResource {
1710
1859
  async loopStream(params, onRecord) {
1711
1860
  const response = await this.request(`/api/networks/v-next/${this.networkId}/loop-stream`, {
1712
1861
  method: "POST",
1713
- body: params,
1862
+ body: {
1863
+ ...params,
1864
+ runtimeContext: parseClientRuntimeContext(params.runtimeContext)
1865
+ },
1714
1866
  stream: true
1715
1867
  });
1716
1868
  if (!response.ok) {
@@ -1729,54 +1881,6 @@ var VNextNetwork = class extends BaseResource {
1729
1881
  }
1730
1882
  };
1731
1883
 
1732
- // src/resources/network-memory-thread.ts
1733
- var NetworkMemoryThread = class extends BaseResource {
1734
- constructor(options, threadId, networkId) {
1735
- super(options);
1736
- this.threadId = threadId;
1737
- this.networkId = networkId;
1738
- }
1739
- /**
1740
- * Retrieves the memory thread details
1741
- * @returns Promise containing thread details including title and metadata
1742
- */
1743
- get() {
1744
- return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`);
1745
- }
1746
- /**
1747
- * Updates the memory thread properties
1748
- * @param params - Update parameters including title and metadata
1749
- * @returns Promise containing updated thread details
1750
- */
1751
- update(params) {
1752
- return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
1753
- method: "PATCH",
1754
- body: params
1755
- });
1756
- }
1757
- /**
1758
- * Deletes the memory thread
1759
- * @returns Promise containing deletion result
1760
- */
1761
- delete() {
1762
- return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
1763
- method: "DELETE"
1764
- });
1765
- }
1766
- /**
1767
- * Retrieves messages associated with the thread
1768
- * @param params - Optional parameters including limit for number of messages to retrieve
1769
- * @returns Promise containing thread messages and UI messages
1770
- */
1771
- getMessages(params) {
1772
- const query = new URLSearchParams({
1773
- networkId: this.networkId,
1774
- ...params?.limit ? { limit: params.limit.toString() } : {}
1775
- });
1776
- return this.request(`/api/memory/network/threads/${this.threadId}/messages?${query.toString()}`);
1777
- }
1778
- };
1779
-
1780
1884
  // src/client.ts
1781
1885
  var MastraClient = class extends BaseResource {
1782
1886
  constructor(options) {
@@ -2171,6 +2275,41 @@ var MastraClient = class extends BaseResource {
2171
2275
  getA2A(agentId) {
2172
2276
  return new A2A(this.options, agentId);
2173
2277
  }
2278
+ /**
2279
+ * Retrieves the working memory for a specific thread (optionally resource-scoped).
2280
+ * @param agentId - ID of the agent.
2281
+ * @param threadId - ID of the thread.
2282
+ * @param resourceId - Optional ID of the resource.
2283
+ * @returns Working memory for the specified thread or resource.
2284
+ */
2285
+ getWorkingMemory({
2286
+ agentId,
2287
+ threadId,
2288
+ resourceId
2289
+ }) {
2290
+ return this.request(`/api/memory/threads/${threadId}/working-memory?agentId=${agentId}&resourceId=${resourceId}`);
2291
+ }
2292
+ /**
2293
+ * Updates the working memory for a specific thread (optionally resource-scoped).
2294
+ * @param agentId - ID of the agent.
2295
+ * @param threadId - ID of the thread.
2296
+ * @param workingMemory - The new working memory content.
2297
+ * @param resourceId - Optional ID of the resource.
2298
+ */
2299
+ updateWorkingMemory({
2300
+ agentId,
2301
+ threadId,
2302
+ workingMemory,
2303
+ resourceId
2304
+ }) {
2305
+ return this.request(`/api/memory/threads/${threadId}/working-memory?agentId=${agentId}`, {
2306
+ method: "POST",
2307
+ body: {
2308
+ workingMemory,
2309
+ resourceId
2310
+ }
2311
+ });
2312
+ }
2174
2313
  };
2175
2314
 
2176
2315
  export { MastraClient };