@mastra/client-js 1.15.0-alpha.5 → 1.15.0

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
@@ -5,6 +5,7 @@ import { standardSchemaToJSONSchema, toStandardSchema } from '@mastra/schema-com
5
5
  import { RequestContext } from '@mastra/core/request-context';
6
6
  export { RequestContext } from '@mastra/core/request-context';
7
7
  import { zodToJsonSchema as zodToJsonSchema$1 } from '@mastra/schema-compat/zod-to-json';
8
+ import { MastraA2AError } from '@mastra/core/a2a';
8
9
 
9
10
  // src/resources/agent.ts
10
11
  function normalizeRoutePath(path) {
@@ -2627,7 +2628,120 @@ var Workflow = class extends BaseResource {
2627
2628
  }
2628
2629
  };
2629
2630
 
2631
+ // src/utils/process-a2a-stream.ts
2632
+ function splitNextEvent(buffer) {
2633
+ const normalizedBuffer = buffer.replace(/\x1E/g, "\n\n");
2634
+ const match = normalizedBuffer.match(/\r?\n\r?\n/);
2635
+ if (!match || match.index === void 0) {
2636
+ return { rest: normalizedBuffer };
2637
+ }
2638
+ const separatorLength = match[0].length;
2639
+ return {
2640
+ eventBlock: normalizedBuffer.slice(0, match.index),
2641
+ rest: normalizedBuffer.slice(match.index + separatorLength)
2642
+ };
2643
+ }
2644
+ function parseEventBlock(eventBlock) {
2645
+ const trimmedBlock = eventBlock.trim();
2646
+ if (!trimmedBlock) {
2647
+ return {};
2648
+ }
2649
+ const lines = trimmedBlock.split(/\r?\n/);
2650
+ const dataLines = lines.filter((line) => line.startsWith("data:")).map((line) => line.slice(5).trimStart());
2651
+ const payload = dataLines.length > 0 ? dataLines.join("\n") : trimmedBlock;
2652
+ if (!payload || payload === "[DONE]") {
2653
+ return { done: true };
2654
+ }
2655
+ let parsed;
2656
+ try {
2657
+ parsed = JSON.parse(payload);
2658
+ } catch (error) {
2659
+ throw new Error(
2660
+ `Failed to parse A2A stream event: ${error instanceof Error ? error.message : "unknown parse error"}`
2661
+ );
2662
+ }
2663
+ if (parsed && typeof parsed === "object" && "error" in parsed && parsed.error) {
2664
+ throw new MastraClientError(200, "OK", `A2A stream error - ${JSON.stringify(parsed.error)}`, parsed.error);
2665
+ }
2666
+ if (parsed && typeof parsed === "object" && "result" in parsed) {
2667
+ return { event: parsed.result };
2668
+ }
2669
+ return { event: parsed };
2670
+ }
2671
+ async function* processA2AStream(stream) {
2672
+ const reader = stream.getReader();
2673
+ const decoder = new TextDecoder();
2674
+ let buffer = "";
2675
+ try {
2676
+ while (true) {
2677
+ const { done, value } = await reader.read();
2678
+ if (done) {
2679
+ buffer += decoder.decode();
2680
+ } else {
2681
+ buffer += decoder.decode(value, { stream: true });
2682
+ }
2683
+ while (true) {
2684
+ const { eventBlock, rest } = splitNextEvent(buffer);
2685
+ buffer = rest;
2686
+ if (!eventBlock) {
2687
+ break;
2688
+ }
2689
+ const parsedEvent = parseEventBlock(eventBlock);
2690
+ if (parsedEvent.done) {
2691
+ return;
2692
+ }
2693
+ if (parsedEvent.event) {
2694
+ yield parsedEvent.event;
2695
+ }
2696
+ }
2697
+ if (done) {
2698
+ break;
2699
+ }
2700
+ }
2701
+ if (buffer.trim()) {
2702
+ const parsedEvent = parseEventBlock(buffer);
2703
+ if (!parsedEvent.done && parsedEvent.event) {
2704
+ yield parsedEvent.event;
2705
+ }
2706
+ }
2707
+ } finally {
2708
+ reader.releaseLock();
2709
+ }
2710
+ }
2711
+
2630
2712
  // src/resources/a2a.ts
2713
+ function createA2AJsonRpcError(response) {
2714
+ const error = response.error;
2715
+ const message = error?.message ?? "Unknown A2A JSON-RPC error";
2716
+ return typeof error?.code === "number" ? new MastraA2AError(error.code, message, error.data) : new MastraClientError(200, "OK", `A2A JSON-RPC error - ${message}`, error);
2717
+ }
2718
+ function unwrapA2AResult(response) {
2719
+ if ("error" in response && response.error) {
2720
+ throw createA2AJsonRpcError(response);
2721
+ }
2722
+ if ("result" in response) {
2723
+ return response.result;
2724
+ }
2725
+ throw new MastraClientError(200, "OK", "A2A JSON-RPC response did not include a result", response);
2726
+ }
2727
+ async function requireResponseBody(response, method) {
2728
+ if (response.body) {
2729
+ return response.body;
2730
+ }
2731
+ const headerSummary = Array.from(response.headers.entries()).map(([key, value]) => `${key}: ${value}`).join(", ");
2732
+ let responseText = "";
2733
+ try {
2734
+ responseText = await response.text();
2735
+ } catch {
2736
+ }
2737
+ const details = [
2738
+ `A2A ${method} stream response did not include a body`,
2739
+ `(status: ${response.status} ${response.statusText})`,
2740
+ headerSummary ? `headers: ${headerSummary}` : "",
2741
+ responseText ? `body: ${responseText}` : ""
2742
+ ].filter(Boolean).join(" ");
2743
+ throw new MastraClientError(response.status, response.statusText, details);
2744
+ }
2631
2745
  var A2A = class extends BaseResource {
2632
2746
  constructor(options, agentId) {
2633
2747
  super(options);
@@ -2635,19 +2749,41 @@ var A2A = class extends BaseResource {
2635
2749
  }
2636
2750
  agentId;
2637
2751
  /**
2638
- * Get the agent card with metadata about the agent
2752
+ * Get the agent card with metadata about the agent.
2639
2753
  * @returns Promise containing the agent card information
2640
2754
  */
2641
- async getCard() {
2755
+ async getAgentCard() {
2642
2756
  return this.request(`/.well-known/${this.agentId}/agent-card.json`);
2643
2757
  }
2644
2758
  /**
2645
- * Send a message to the agent and gets a message or task response
2759
+ * @deprecated Use getAgentCard() instead.
2760
+ */
2761
+ async getCard() {
2762
+ return this.getAgentCard();
2763
+ }
2764
+ /**
2765
+ * Get the authenticated extended agent card.
2766
+ * @returns Promise containing the authenticated extended agent card
2767
+ */
2768
+ async getExtendedAgentCard() {
2769
+ const response = await this.request(`/a2a/${this.agentId}`, {
2770
+ method: "POST",
2771
+ body: {
2772
+ jsonrpc: "2.0",
2773
+ id: crypto.randomUUID(),
2774
+ method: "agent/getAuthenticatedExtendedCard"
2775
+ }
2776
+ });
2777
+ return unwrapA2AResult(response);
2778
+ }
2779
+ /**
2780
+ * @deprecated Use sendMessageStream() for the streaming experience.
2781
+ * Send a message to the agent and gets a message or task response.
2646
2782
  * @param params - Parameters for the task
2647
- * @returns Promise containing the response
2783
+ * @returns Promise containing the JSON-RPC response envelope
2648
2784
  */
2649
2785
  async sendMessage(params) {
2650
- const response = await this.request(`/a2a/${this.agentId}`, {
2786
+ return this.request(`/a2a/${this.agentId}`, {
2651
2787
  method: "POST",
2652
2788
  body: {
2653
2789
  jsonrpc: "2.0",
@@ -2656,15 +2792,14 @@ var A2A = class extends BaseResource {
2656
2792
  params
2657
2793
  }
2658
2794
  });
2659
- return response;
2660
2795
  }
2661
2796
  /**
2662
- * Sends a message to an agent to initiate/continue a task and subscribes
2797
+ * Sends a message to an agent to initiate or continue a task and subscribes
2663
2798
  * the client to real-time updates for that task via Server-Sent Events (SSE).
2664
2799
  * @param params - Parameters for the task
2665
- * @returns A stream of Server-Sent Events. Each SSE `data` field contains a `SendStreamingMessageResponse`
2800
+ * @returns An async generator of typed A2A stream events
2666
2801
  */
2667
- async sendStreamingMessage(params) {
2802
+ async *sendMessageStream(params) {
2668
2803
  const response = await this.request(`/a2a/${this.agentId}`, {
2669
2804
  method: "POST",
2670
2805
  body: {
@@ -2675,15 +2810,31 @@ var A2A = class extends BaseResource {
2675
2810
  },
2676
2811
  stream: true
2677
2812
  });
2678
- return response;
2813
+ yield* processA2AStream(await requireResponseBody(response, "message/stream"));
2679
2814
  }
2680
2815
  /**
2681
- * Get the status and result of a task
2816
+ * @deprecated Use sendMessageStream() instead.
2817
+ */
2818
+ async sendStreamingMessage(params) {
2819
+ return this.request(`/a2a/${this.agentId}`, {
2820
+ method: "POST",
2821
+ body: {
2822
+ jsonrpc: "2.0",
2823
+ id: crypto.randomUUID(),
2824
+ method: "message/stream",
2825
+ params
2826
+ },
2827
+ stream: true
2828
+ });
2829
+ }
2830
+ /**
2831
+ * @deprecated
2832
+ * Get the status and result of a task.
2682
2833
  * @param params - Parameters for querying the task
2683
- * @returns Promise containing the task response
2834
+ * @returns Promise containing the JSON-RPC response envelope
2684
2835
  */
2685
2836
  async getTask(params) {
2686
- const response = await this.request(`/a2a/${this.agentId}`, {
2837
+ return this.request(`/a2a/${this.agentId}`, {
2687
2838
  method: "POST",
2688
2839
  body: {
2689
2840
  jsonrpc: "2.0",
@@ -2692,10 +2843,10 @@ var A2A = class extends BaseResource {
2692
2843
  params
2693
2844
  }
2694
2845
  });
2695
- return response;
2696
2846
  }
2697
2847
  /**
2698
- * Cancel a running task
2848
+ * @deprecated
2849
+ * Cancel a running task.
2699
2850
  * @param params - Parameters identifying the task to cancel
2700
2851
  * @returns Promise containing the task response
2701
2852
  */
@@ -2710,6 +2861,92 @@ var A2A = class extends BaseResource {
2710
2861
  }
2711
2862
  });
2712
2863
  }
2864
+ /**
2865
+ * Resume a task stream for an existing task.
2866
+ * @param params - Parameters identifying the task to resubscribe to
2867
+ * @returns An async generator of typed A2A stream events
2868
+ */
2869
+ async *resubscribeTask(params) {
2870
+ const response = await this.request(`/a2a/${this.agentId}`, {
2871
+ method: "POST",
2872
+ body: {
2873
+ jsonrpc: "2.0",
2874
+ id: crypto.randomUUID(),
2875
+ method: "tasks/resubscribe",
2876
+ params
2877
+ },
2878
+ stream: true
2879
+ });
2880
+ yield* processA2AStream(await requireResponseBody(response, "tasks/resubscribe"));
2881
+ }
2882
+ /**
2883
+ * Set push notification config for a task.
2884
+ * @param params - Push notification configuration for the task
2885
+ * @returns Promise containing the push notification configuration
2886
+ */
2887
+ async setTaskPushNotificationConfig(params) {
2888
+ const response = await this.request(`/a2a/${this.agentId}`, {
2889
+ method: "POST",
2890
+ body: {
2891
+ jsonrpc: "2.0",
2892
+ id: crypto.randomUUID(),
2893
+ method: "tasks/pushNotificationConfig/set",
2894
+ params
2895
+ }
2896
+ });
2897
+ return unwrapA2AResult(response);
2898
+ }
2899
+ /**
2900
+ * Get push notification config for a task.
2901
+ * @param params - Parameters identifying the task
2902
+ * @returns Promise containing the push notification configuration
2903
+ */
2904
+ async getTaskPushNotificationConfig(params) {
2905
+ const response = await this.request(`/a2a/${this.agentId}`, {
2906
+ method: "POST",
2907
+ body: {
2908
+ jsonrpc: "2.0",
2909
+ id: crypto.randomUUID(),
2910
+ method: "tasks/pushNotificationConfig/get",
2911
+ params
2912
+ }
2913
+ });
2914
+ return unwrapA2AResult(response);
2915
+ }
2916
+ /**
2917
+ * List push notification configs for a task.
2918
+ * @param params - Parameters identifying the task
2919
+ * @returns Promise containing the push notification configurations
2920
+ */
2921
+ async listTaskPushNotificationConfig(params) {
2922
+ const response = await this.request(`/a2a/${this.agentId}`, {
2923
+ method: "POST",
2924
+ body: {
2925
+ jsonrpc: "2.0",
2926
+ id: crypto.randomUUID(),
2927
+ method: "tasks/pushNotificationConfig/list",
2928
+ params
2929
+ }
2930
+ });
2931
+ return unwrapA2AResult(response);
2932
+ }
2933
+ /**
2934
+ * Delete a push notification config for a task.
2935
+ * @param params - Parameters identifying the config to delete
2936
+ * @returns Promise that resolves when the config is deleted
2937
+ */
2938
+ async deleteTaskPushNotificationConfig(params) {
2939
+ const response = await this.request(`/a2a/${this.agentId}`, {
2940
+ method: "POST",
2941
+ body: {
2942
+ jsonrpc: "2.0",
2943
+ id: crypto.randomUUID(),
2944
+ method: "tasks/pushNotificationConfig/delete",
2945
+ params
2946
+ }
2947
+ });
2948
+ unwrapA2AResult(response);
2949
+ }
2713
2950
  };
2714
2951
 
2715
2952
  // src/resources/mcp-tool.ts