@mastra/client-js 0.10.10 → 0.10.11-alpha.1

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.
@@ -1,5 +1,5 @@
1
1
 
2
- > @mastra/client-js@0.10.10-alpha.4 build /home/runner/work/mastra/mastra/client-sdks/client-js
2
+ > @mastra/client-js@0.10.11-alpha.1 build /home/runner/work/mastra/mastra/client-sdks/client-js
3
3
  > tsup src/index.ts --format esm,cjs --dts --clean --treeshake=smallest --splitting
4
4
 
5
5
  CLI Building entry: src/index.ts
@@ -9,11 +9,11 @@
9
9
  CLI Cleaning output folder
10
10
  ESM Build start
11
11
  CJS Build start
12
- ESM dist/index.js 68.51 KB
13
- ESM ⚡️ Build success in 2252ms
14
- CJS dist/index.cjs 68.80 KB
15
- CJS ⚡️ Build success in 2266ms
12
+ CJS dist/index.cjs 69.98 KB
13
+ CJS ⚡️ Build success in 1503ms
14
+ ESM dist/index.js 69.70 KB
15
+ ESM ⚡️ Build success in 1504ms
16
16
  DTS Build start
17
- DTS ⚡️ Build success in 18063ms
18
- DTS dist/index.d.ts 40.65 KB
19
- DTS dist/index.d.cts 40.65 KB
17
+ DTS ⚡️ Build success in 16685ms
18
+ DTS dist/index.d.ts 40.80 KB
19
+ DTS dist/index.d.cts 40.80 KB
package/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # @mastra/client-js
2
2
 
3
+ ## 0.10.11-alpha.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [4d5583d]
8
+ - @mastra/core@0.10.12-alpha.1
9
+
10
+ ## 0.10.11-alpha.0
11
+
12
+ ### Patch Changes
13
+
14
+ - 9468be4: Fix error on agent generate while using antropic
15
+ - 44731a4: Client SDK - Expose processTextStream util for agent textstream with structured output
16
+ - Updated dependencies [b4a9811]
17
+ - @mastra/core@0.10.12-alpha.0
18
+
3
19
  ## 0.10.10
4
20
 
5
21
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -382,7 +382,11 @@ var Agent = class extends BaseResource {
382
382
  body: processedParams
383
383
  });
384
384
  if (response.finishReason === "tool-calls") {
385
- for (const toolCall of response.toolCalls) {
385
+ const toolCalls = response.toolCalls;
386
+ if (!toolCalls || !Array.isArray(toolCalls)) {
387
+ return response;
388
+ }
389
+ for (const toolCall of toolCalls) {
386
390
  const clientTool = params.clientTools?.[toolCall.toolName];
387
391
  if (clientTool && clientTool.execute) {
388
392
  const result = await clientTool.execute(
@@ -425,7 +429,8 @@ var Agent = class extends BaseResource {
425
429
  onToolCall,
426
430
  onFinish,
427
431
  getCurrentDate = () => /* @__PURE__ */ new Date(),
428
- lastMessage
432
+ lastMessage,
433
+ streamProtocol
429
434
  }) {
430
435
  const replaceLastMessage = lastMessage?.role === "assistant";
431
436
  let step = replaceLastMessage ? 1 + // find max step in existing tool invocations:
@@ -486,228 +491,213 @@ var Agent = class extends BaseResource {
486
491
  replaceLastMessage
487
492
  });
488
493
  }
489
- await uiUtils.processDataStream({
490
- stream,
491
- onTextPart(value) {
492
- if (currentTextPart == null) {
493
- currentTextPart = {
494
- type: "text",
495
- text: value
496
- };
497
- message.parts.push(currentTextPart);
498
- } else {
499
- currentTextPart.text += value;
494
+ if (streamProtocol === "text") {
495
+ await uiUtils.processTextStream({
496
+ stream,
497
+ onTextPart(value) {
498
+ message.content += value;
499
+ execUpdate();
500
500
  }
501
- message.content += value;
502
- execUpdate();
503
- },
504
- onReasoningPart(value) {
505
- if (currentReasoningTextDetail == null) {
506
- currentReasoningTextDetail = { type: "text", text: value };
507
- if (currentReasoningPart != null) {
508
- currentReasoningPart.details.push(currentReasoningTextDetail);
501
+ });
502
+ onFinish?.({ message, finishReason, usage });
503
+ } else {
504
+ await uiUtils.processDataStream({
505
+ stream,
506
+ onTextPart(value) {
507
+ if (currentTextPart == null) {
508
+ currentTextPart = {
509
+ type: "text",
510
+ text: value
511
+ };
512
+ message.parts.push(currentTextPart);
513
+ } else {
514
+ currentTextPart.text += value;
509
515
  }
510
- } else {
511
- currentReasoningTextDetail.text += value;
512
- }
513
- if (currentReasoningPart == null) {
514
- currentReasoningPart = {
515
- type: "reasoning",
516
- reasoning: value,
517
- details: [currentReasoningTextDetail]
518
- };
519
- message.parts.push(currentReasoningPart);
520
- } else {
521
- currentReasoningPart.reasoning += value;
522
- }
523
- message.reasoning = (message.reasoning ?? "") + value;
524
- execUpdate();
525
- },
526
- onReasoningSignaturePart(value) {
527
- if (currentReasoningTextDetail != null) {
528
- currentReasoningTextDetail.signature = value.signature;
529
- }
530
- },
531
- onRedactedReasoningPart(value) {
532
- if (currentReasoningPart == null) {
533
- currentReasoningPart = {
534
- type: "reasoning",
535
- reasoning: "",
536
- details: []
537
- };
538
- message.parts.push(currentReasoningPart);
539
- }
540
- currentReasoningPart.details.push({
541
- type: "redacted",
542
- data: value.data
543
- });
544
- currentReasoningTextDetail = void 0;
545
- execUpdate();
546
- },
547
- onFilePart(value) {
548
- message.parts.push({
549
- type: "file",
550
- mimeType: value.mimeType,
551
- data: value.data
552
- });
553
- execUpdate();
554
- },
555
- onSourcePart(value) {
556
- message.parts.push({
557
- type: "source",
558
- source: value
559
- });
560
- execUpdate();
561
- },
562
- onToolCallStreamingStartPart(value) {
563
- if (message.toolInvocations == null) {
564
- message.toolInvocations = [];
565
- }
566
- partialToolCalls[value.toolCallId] = {
567
- text: "",
568
- step,
569
- toolName: value.toolName,
570
- index: message.toolInvocations.length
571
- };
572
- const invocation = {
573
- state: "partial-call",
574
- step,
575
- toolCallId: value.toolCallId,
576
- toolName: value.toolName,
577
- args: void 0
578
- };
579
- message.toolInvocations.push(invocation);
580
- updateToolInvocationPart(value.toolCallId, invocation);
581
- execUpdate();
582
- },
583
- onToolCallDeltaPart(value) {
584
- const partialToolCall = partialToolCalls[value.toolCallId];
585
- partialToolCall.text += value.argsTextDelta;
586
- const { value: partialArgs } = uiUtils.parsePartialJson(partialToolCall.text);
587
- const invocation = {
588
- state: "partial-call",
589
- step: partialToolCall.step,
590
- toolCallId: value.toolCallId,
591
- toolName: partialToolCall.toolName,
592
- args: partialArgs
593
- };
594
- message.toolInvocations[partialToolCall.index] = invocation;
595
- updateToolInvocationPart(value.toolCallId, invocation);
596
- execUpdate();
597
- },
598
- async onToolCallPart(value) {
599
- const invocation = {
600
- state: "call",
601
- step,
602
- ...value
603
- };
604
- if (partialToolCalls[value.toolCallId] != null) {
605
- message.toolInvocations[partialToolCalls[value.toolCallId].index] = invocation;
606
- } else {
516
+ message.content += value;
517
+ execUpdate();
518
+ },
519
+ onReasoningPart(value) {
520
+ if (currentReasoningTextDetail == null) {
521
+ currentReasoningTextDetail = { type: "text", text: value };
522
+ if (currentReasoningPart != null) {
523
+ currentReasoningPart.details.push(currentReasoningTextDetail);
524
+ }
525
+ } else {
526
+ currentReasoningTextDetail.text += value;
527
+ }
528
+ if (currentReasoningPart == null) {
529
+ currentReasoningPart = {
530
+ type: "reasoning",
531
+ reasoning: value,
532
+ details: [currentReasoningTextDetail]
533
+ };
534
+ message.parts.push(currentReasoningPart);
535
+ } else {
536
+ currentReasoningPart.reasoning += value;
537
+ }
538
+ message.reasoning = (message.reasoning ?? "") + value;
539
+ execUpdate();
540
+ },
541
+ onReasoningSignaturePart(value) {
542
+ if (currentReasoningTextDetail != null) {
543
+ currentReasoningTextDetail.signature = value.signature;
544
+ }
545
+ },
546
+ onRedactedReasoningPart(value) {
547
+ if (currentReasoningPart == null) {
548
+ currentReasoningPart = {
549
+ type: "reasoning",
550
+ reasoning: "",
551
+ details: []
552
+ };
553
+ message.parts.push(currentReasoningPart);
554
+ }
555
+ currentReasoningPart.details.push({
556
+ type: "redacted",
557
+ data: value.data
558
+ });
559
+ currentReasoningTextDetail = void 0;
560
+ execUpdate();
561
+ },
562
+ onFilePart(value) {
563
+ message.parts.push({
564
+ type: "file",
565
+ mimeType: value.mimeType,
566
+ data: value.data
567
+ });
568
+ execUpdate();
569
+ },
570
+ onSourcePart(value) {
571
+ message.parts.push({
572
+ type: "source",
573
+ source: value
574
+ });
575
+ execUpdate();
576
+ },
577
+ onToolCallStreamingStartPart(value) {
607
578
  if (message.toolInvocations == null) {
608
579
  message.toolInvocations = [];
609
580
  }
581
+ partialToolCalls[value.toolCallId] = {
582
+ text: "",
583
+ step,
584
+ toolName: value.toolName,
585
+ index: message.toolInvocations.length
586
+ };
587
+ const invocation = {
588
+ state: "partial-call",
589
+ step,
590
+ toolCallId: value.toolCallId,
591
+ toolName: value.toolName,
592
+ args: void 0
593
+ };
610
594
  message.toolInvocations.push(invocation);
611
- }
612
- updateToolInvocationPart(value.toolCallId, invocation);
613
- execUpdate();
614
- if (onToolCall) {
615
- const result = await onToolCall({ toolCall: value });
616
- if (result != null) {
617
- const invocation2 = {
618
- state: "result",
619
- step,
620
- ...value,
621
- result
622
- };
623
- message.toolInvocations[message.toolInvocations.length - 1] = invocation2;
624
- updateToolInvocationPart(value.toolCallId, invocation2);
625
- execUpdate();
595
+ updateToolInvocationPart(value.toolCallId, invocation);
596
+ execUpdate();
597
+ },
598
+ onToolCallDeltaPart(value) {
599
+ const partialToolCall = partialToolCalls[value.toolCallId];
600
+ partialToolCall.text += value.argsTextDelta;
601
+ const { value: partialArgs } = uiUtils.parsePartialJson(partialToolCall.text);
602
+ const invocation = {
603
+ state: "partial-call",
604
+ step: partialToolCall.step,
605
+ toolCallId: value.toolCallId,
606
+ toolName: partialToolCall.toolName,
607
+ args: partialArgs
608
+ };
609
+ message.toolInvocations[partialToolCall.index] = invocation;
610
+ updateToolInvocationPart(value.toolCallId, invocation);
611
+ execUpdate();
612
+ },
613
+ async onToolCallPart(value) {
614
+ const invocation = {
615
+ state: "call",
616
+ step,
617
+ ...value
618
+ };
619
+ if (partialToolCalls[value.toolCallId] != null) {
620
+ message.toolInvocations[partialToolCalls[value.toolCallId].index] = invocation;
621
+ } else {
622
+ if (message.toolInvocations == null) {
623
+ message.toolInvocations = [];
624
+ }
625
+ message.toolInvocations.push(invocation);
626
626
  }
627
+ updateToolInvocationPart(value.toolCallId, invocation);
628
+ execUpdate();
629
+ if (onToolCall) {
630
+ const result = await onToolCall({ toolCall: value });
631
+ if (result != null) {
632
+ const invocation2 = {
633
+ state: "result",
634
+ step,
635
+ ...value,
636
+ result
637
+ };
638
+ message.toolInvocations[message.toolInvocations.length - 1] = invocation2;
639
+ updateToolInvocationPart(value.toolCallId, invocation2);
640
+ execUpdate();
641
+ }
642
+ }
643
+ },
644
+ onToolResultPart(value) {
645
+ const toolInvocations = message.toolInvocations;
646
+ if (toolInvocations == null) {
647
+ throw new Error("tool_result must be preceded by a tool_call");
648
+ }
649
+ const toolInvocationIndex = toolInvocations.findIndex(
650
+ (invocation2) => invocation2.toolCallId === value.toolCallId
651
+ );
652
+ if (toolInvocationIndex === -1) {
653
+ throw new Error("tool_result must be preceded by a tool_call with the same toolCallId");
654
+ }
655
+ const invocation = {
656
+ ...toolInvocations[toolInvocationIndex],
657
+ state: "result",
658
+ ...value
659
+ };
660
+ toolInvocations[toolInvocationIndex] = invocation;
661
+ updateToolInvocationPart(value.toolCallId, invocation);
662
+ execUpdate();
663
+ },
664
+ onDataPart(value) {
665
+ data.push(...value);
666
+ execUpdate();
667
+ },
668
+ onMessageAnnotationsPart(value) {
669
+ if (messageAnnotations == null) {
670
+ messageAnnotations = [...value];
671
+ } else {
672
+ messageAnnotations.push(...value);
673
+ }
674
+ execUpdate();
675
+ },
676
+ onFinishStepPart(value) {
677
+ step += 1;
678
+ currentTextPart = value.isContinued ? currentTextPart : void 0;
679
+ currentReasoningPart = void 0;
680
+ currentReasoningTextDetail = void 0;
681
+ },
682
+ onStartStepPart(value) {
683
+ if (!replaceLastMessage) {
684
+ message.id = value.messageId;
685
+ }
686
+ message.parts.push({ type: "step-start" });
687
+ execUpdate();
688
+ },
689
+ onFinishMessagePart(value) {
690
+ finishReason = value.finishReason;
691
+ if (value.usage != null) {
692
+ usage = value.usage;
693
+ }
694
+ },
695
+ onErrorPart(error) {
696
+ throw new Error(error);
627
697
  }
628
- },
629
- onToolResultPart(value) {
630
- const toolInvocations = message.toolInvocations;
631
- if (toolInvocations == null) {
632
- throw new Error("tool_result must be preceded by a tool_call");
633
- }
634
- const toolInvocationIndex = toolInvocations.findIndex((invocation2) => invocation2.toolCallId === value.toolCallId);
635
- if (toolInvocationIndex === -1) {
636
- throw new Error("tool_result must be preceded by a tool_call with the same toolCallId");
637
- }
638
- const invocation = {
639
- ...toolInvocations[toolInvocationIndex],
640
- state: "result",
641
- ...value
642
- };
643
- toolInvocations[toolInvocationIndex] = invocation;
644
- updateToolInvocationPart(value.toolCallId, invocation);
645
- execUpdate();
646
- },
647
- onDataPart(value) {
648
- data.push(...value);
649
- execUpdate();
650
- },
651
- onMessageAnnotationsPart(value) {
652
- if (messageAnnotations == null) {
653
- messageAnnotations = [...value];
654
- } else {
655
- messageAnnotations.push(...value);
656
- }
657
- execUpdate();
658
- },
659
- onFinishStepPart(value) {
660
- step += 1;
661
- currentTextPart = value.isContinued ? currentTextPart : void 0;
662
- currentReasoningPart = void 0;
663
- currentReasoningTextDetail = void 0;
664
- },
665
- onStartStepPart(value) {
666
- if (!replaceLastMessage) {
667
- message.id = value.messageId;
668
- }
669
- message.parts.push({ type: "step-start" });
670
- execUpdate();
671
- },
672
- onFinishMessagePart(value) {
673
- finishReason = value.finishReason;
674
- if (value.usage != null) {
675
- usage = value.usage;
676
- }
677
- },
678
- onErrorPart(error) {
679
- throw new Error(error);
680
- }
681
- });
682
- onFinish?.({ message, finishReason, usage });
683
- }
684
- /**
685
- * Streams a response from the agent
686
- * @param params - Stream parameters including prompt
687
- * @returns Promise containing the enhanced Response object with processDataStream method
688
- */
689
- async stream(params) {
690
- const processedParams = {
691
- ...params,
692
- output: params.output ? zodToJsonSchema(params.output) : void 0,
693
- experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
694
- runtimeContext: parseClientRuntimeContext(params.runtimeContext),
695
- clientTools: processClientTools(params.clientTools)
696
- };
697
- const { readable, writable } = new TransformStream();
698
- const response = await this.processStreamResponse(processedParams, writable);
699
- const streamResponse = new Response(readable, {
700
- status: response.status,
701
- statusText: response.statusText,
702
- headers: response.headers
703
- });
704
- streamResponse.processDataStream = async (options = {}) => {
705
- await uiUtils.processDataStream({
706
- stream: streamResponse.body,
707
- ...options
708
698
  });
709
- };
710
- return streamResponse;
699
+ onFinish?.({ message, finishReason, usage });
700
+ }
711
701
  }
712
702
  /**
713
703
  * Processes the stream response and handles tool calls
@@ -722,6 +712,7 @@ var Agent = class extends BaseResource {
722
712
  throw new Error("No response body");
723
713
  }
724
714
  try {
715
+ const streamProtocol = processedParams.output ? "text" : "data";
725
716
  let toolCalls = [];
726
717
  let messages = [];
727
718
  const [streamForWritable, streamForProcessing] = response.body.tee();
@@ -801,17 +792,55 @@ var Agent = class extends BaseResource {
801
792
  }
802
793
  } else {
803
794
  setTimeout(() => {
804
- writable.close();
795
+ if (!writable.locked) {
796
+ writable.close();
797
+ }
805
798
  }, 0);
806
799
  }
807
800
  },
808
- lastMessage: void 0
801
+ lastMessage: void 0,
802
+ streamProtocol
809
803
  });
810
804
  } catch (error) {
811
805
  console.error("Error processing stream response:", error);
812
806
  }
813
807
  return response;
814
808
  }
809
+ /**
810
+ * Streams a response from the agent
811
+ * @param params - Stream parameters including prompt
812
+ * @returns Promise containing the enhanced Response object with processDataStream and processTextStream methods
813
+ */
814
+ async stream(params) {
815
+ const processedParams = {
816
+ ...params,
817
+ output: params.output ? zodToJsonSchema(params.output) : void 0,
818
+ experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
819
+ runtimeContext: parseClientRuntimeContext(params.runtimeContext),
820
+ clientTools: processClientTools(params.clientTools)
821
+ };
822
+ const { readable, writable } = new TransformStream();
823
+ const response = await this.processStreamResponse(processedParams, writable);
824
+ const streamResponse = new Response(readable, {
825
+ status: response.status,
826
+ statusText: response.statusText,
827
+ headers: response.headers
828
+ });
829
+ streamResponse.processDataStream = async (options = {}) => {
830
+ await uiUtils.processDataStream({
831
+ stream: streamResponse.body,
832
+ ...options
833
+ });
834
+ };
835
+ streamResponse.processTextStream = async (options) => {
836
+ await uiUtils.processTextStream({
837
+ stream: streamResponse.body,
838
+ onTextPart: options?.onTextPart ?? (() => {
839
+ })
840
+ });
841
+ };
842
+ return streamResponse;
843
+ }
815
844
  /**
816
845
  * Gets details about a specific tool available to the agent
817
846
  * @param toolId - ID of the tool to retrieve
package/dist/index.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { AbstractAgent } from '@ag-ui/client';
2
2
  import { ServerInfo, MCPToolType, ServerDetailInfo } from '@mastra/core/mcp';
3
- import { processDataStream } from '@ai-sdk/ui-utils';
3
+ import { processDataStream, processTextStream } from '@ai-sdk/ui-utils';
4
4
  import { CoreMessage, AiMessageType, StorageThreadType, MastraMessageV1, LegacyWorkflowRuns, WorkflowRuns, WorkflowRun, QueryResult, GenerateReturn } from '@mastra/core';
5
5
  import { JSONSchema7 } from 'json-schema';
6
6
  import { ZodSchema } from 'zod';
@@ -440,18 +440,19 @@ declare class Agent extends BaseResource {
440
440
  experimental_output: T;
441
441
  }): Promise<GenerateReturn<T>>;
442
442
  private processChatResponse;
443
+ /**
444
+ * Processes the stream response and handles tool calls
445
+ */
446
+ private processStreamResponse;
443
447
  /**
444
448
  * Streams a response from the agent
445
449
  * @param params - Stream parameters including prompt
446
- * @returns Promise containing the enhanced Response object with processDataStream method
450
+ * @returns Promise containing the enhanced Response object with processDataStream and processTextStream methods
447
451
  */
448
452
  stream<T extends JSONSchema7 | ZodSchema | undefined = undefined>(params: StreamParams<T>): Promise<Response & {
449
453
  processDataStream: (options?: Omit<Parameters<typeof processDataStream>[0], 'stream'>) => Promise<void>;
454
+ processTextStream: (options?: Omit<Parameters<typeof processTextStream>[0], 'stream'>) => Promise<void>;
450
455
  }>;
451
- /**
452
- * Processes the stream response and handles tool calls
453
- */
454
- private processStreamResponse;
455
456
  /**
456
457
  * Gets details about a specific tool available to the agent
457
458
  * @param toolId - ID of the tool to retrieve
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { AbstractAgent } from '@ag-ui/client';
2
2
  import { ServerInfo, MCPToolType, ServerDetailInfo } from '@mastra/core/mcp';
3
- import { processDataStream } from '@ai-sdk/ui-utils';
3
+ import { processDataStream, processTextStream } from '@ai-sdk/ui-utils';
4
4
  import { CoreMessage, AiMessageType, StorageThreadType, MastraMessageV1, LegacyWorkflowRuns, WorkflowRuns, WorkflowRun, QueryResult, GenerateReturn } from '@mastra/core';
5
5
  import { JSONSchema7 } from 'json-schema';
6
6
  import { ZodSchema } from 'zod';
@@ -440,18 +440,19 @@ declare class Agent extends BaseResource {
440
440
  experimental_output: T;
441
441
  }): Promise<GenerateReturn<T>>;
442
442
  private processChatResponse;
443
+ /**
444
+ * Processes the stream response and handles tool calls
445
+ */
446
+ private processStreamResponse;
443
447
  /**
444
448
  * Streams a response from the agent
445
449
  * @param params - Stream parameters including prompt
446
- * @returns Promise containing the enhanced Response object with processDataStream method
450
+ * @returns Promise containing the enhanced Response object with processDataStream and processTextStream methods
447
451
  */
448
452
  stream<T extends JSONSchema7 | ZodSchema | undefined = undefined>(params: StreamParams<T>): Promise<Response & {
449
453
  processDataStream: (options?: Omit<Parameters<typeof processDataStream>[0], 'stream'>) => Promise<void>;
454
+ processTextStream: (options?: Omit<Parameters<typeof processTextStream>[0], 'stream'>) => Promise<void>;
450
455
  }>;
451
- /**
452
- * Processes the stream response and handles tool calls
453
- */
454
- private processStreamResponse;
455
456
  /**
456
457
  * Gets details about a specific tool available to the agent
457
458
  * @param toolId - ID of the tool to retrieve