@mastra/client-js 0.16.4 → 0.16.5

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,5 +1,6 @@
1
1
  import { processDataStream, parsePartialJson } from '@ai-sdk/ui-utils';
2
2
  import { v4 } from '@lukeed/uuid';
3
+ import { getErrorFromUnknown } from '@mastra/core/error';
3
4
  import { RuntimeContext } from '@mastra/core/runtime-context';
4
5
  import { isVercelTool } from '@mastra/core/tools/is-vercel-tool';
5
6
  import { z } from 'zod';
@@ -94,11 +95,15 @@ async function sharedProcessMastraStream({
94
95
  console.info("\u{1F3C1} Stream finished");
95
96
  return;
96
97
  }
98
+ let json;
97
99
  try {
98
- const json = JSON.parse(data);
99
- await onChunk(json);
100
+ json = JSON.parse(data);
100
101
  } catch (error) {
101
102
  console.error("\u274C JSON parse error:", error, "Data:", data);
103
+ continue;
104
+ }
105
+ if (json) {
106
+ await onChunk(json);
102
107
  }
103
108
  }
104
109
  }
@@ -945,7 +950,10 @@ var Agent = class extends BaseResource {
945
950
  break;
946
951
  }
947
952
  case "error": {
948
- throw new Error(chunk.payload.error);
953
+ throw getErrorFromUnknown(chunk.payload.error, {
954
+ fallbackMessage: "Unknown error in stream",
955
+ supportSerialization: false
956
+ });
949
957
  }
950
958
  case "data": {
951
959
  data.push(...chunk.payload.data);
@@ -972,8 +980,8 @@ var Agent = class extends BaseResource {
972
980
  });
973
981
  onFinish?.({ message, finishReason, usage });
974
982
  }
975
- async processStreamResponse(processedParams, writable) {
976
- const response = await this.request(`/api/agents/${this.agentId}/stream`, {
983
+ async processStreamResponse(processedParams, writable, route = "stream") {
984
+ const response = await this.request(`/api/agents/${this.agentId}/${route}`, {
977
985
  method: "POST",
978
986
  body: processedParams,
979
987
  stream: true
@@ -1158,6 +1166,42 @@ var Agent = class extends BaseResource {
1158
1166
  };
1159
1167
  return streamResponse;
1160
1168
  }
1169
+ async approveToolCall(params) {
1170
+ const { readable, writable } = new TransformStream();
1171
+ const response = await this.processStreamResponse(params, writable, "approve-tool-call");
1172
+ const streamResponse = new Response(readable, {
1173
+ status: response.status,
1174
+ statusText: response.statusText,
1175
+ headers: response.headers
1176
+ });
1177
+ streamResponse.processDataStream = async ({
1178
+ onChunk
1179
+ }) => {
1180
+ await processMastraStream({
1181
+ stream: streamResponse.body,
1182
+ onChunk
1183
+ });
1184
+ };
1185
+ return streamResponse;
1186
+ }
1187
+ async declineToolCall(params) {
1188
+ const { readable, writable } = new TransformStream();
1189
+ const response = await this.processStreamResponse(params, writable, "decline-tool-call");
1190
+ const streamResponse = new Response(readable, {
1191
+ status: response.status,
1192
+ statusText: response.statusText,
1193
+ headers: response.headers
1194
+ });
1195
+ streamResponse.processDataStream = async ({
1196
+ onChunk
1197
+ }) => {
1198
+ await processMastraStream({
1199
+ stream: streamResponse.body,
1200
+ onChunk
1201
+ });
1202
+ };
1203
+ return streamResponse;
1204
+ }
1161
1205
  /**
1162
1206
  * Processes the stream response and handles tool calls
1163
1207
  */