@mastra/client-js 0.16.9 → 0.16.10

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
@@ -92,7 +92,6 @@ async function sharedProcessMastraStream({
92
92
  if (line.startsWith("data: ")) {
93
93
  const data = line.slice(6);
94
94
  if (data === "[DONE]") {
95
- console.info("\u{1F3C1} Stream finished");
96
95
  return;
97
96
  }
98
97
  let json;
@@ -209,11 +208,11 @@ async function executeToolCallAndRespond({
209
208
  return response;
210
209
  }
211
210
  for (const toolCall of toolCalls) {
212
- const clientTool = params.clientTools?.[toolCall.toolName];
211
+ const clientTool = params.clientTools?.[toolCall.payload.toolName];
213
212
  if (clientTool && clientTool.execute) {
214
213
  const result = await clientTool.execute(
215
214
  {
216
- context: toolCall?.args,
215
+ context: toolCall?.payload.args,
217
216
  runId,
218
217
  resourceId,
219
218
  threadId,
@@ -224,7 +223,7 @@ async function executeToolCallAndRespond({
224
223
  },
225
224
  {
226
225
  messages: response.messages,
227
- toolCallId: toolCall?.toolCallId
226
+ toolCallId: toolCall?.payload.toolCallId
228
227
  }
229
228
  );
230
229
  const updatedMessages = [
@@ -234,8 +233,8 @@ async function executeToolCallAndRespond({
234
233
  content: [
235
234
  {
236
235
  type: "tool-result",
237
- toolCallId: toolCall.toolCallId,
238
- toolName: toolCall.toolName,
236
+ toolCallId: toolCall.payload.toolCallId,
237
+ toolName: toolCall.payload.toolName,
239
238
  result
240
239
  }
241
240
  ]
@@ -248,6 +247,7 @@ async function executeToolCallAndRespond({
248
247
  }
249
248
  }
250
249
  }
250
+ return response;
251
251
  }
252
252
  var AgentVoice = class extends BaseResource {
253
253
  constructor(options, agentId) {
@@ -980,7 +980,7 @@ var Agent = class extends BaseResource {
980
980
  });
981
981
  onFinish?.({ message, finishReason, usage });
982
982
  }
983
- async processStreamResponse(processedParams, writable, route = "stream") {
983
+ async processStreamResponse(processedParams, controller, route = "stream") {
984
984
  const response = await this.request(`/api/agents/${this.agentId}/${route}`, {
985
985
  method: "POST",
986
986
  body: processedParams,
@@ -992,29 +992,30 @@ var Agent = class extends BaseResource {
992
992
  try {
993
993
  let toolCalls = [];
994
994
  let messages = [];
995
- const [streamForWritable, streamForProcessing] = response.body.tee();
996
- streamForWritable.pipeTo(
995
+ const [streamForController, streamForProcessing] = response.body.tee();
996
+ const pipePromise = streamForController.pipeTo(
997
997
  new WritableStream({
998
998
  async write(chunk) {
999
- let writer;
1000
999
  try {
1001
- writer = writable.getWriter();
1002
1000
  const text = new TextDecoder().decode(chunk);
1003
1001
  const lines = text.split("\n\n");
1004
- const readableLines = lines.filter((line) => line !== "[DONE]").join("\n\n");
1005
- await writer.write(new TextEncoder().encode(readableLines));
1006
- } catch {
1007
- await writer?.write(chunk);
1008
- } finally {
1009
- writer?.releaseLock();
1002
+ const readableLines = lines.filter((line) => line.trim() !== "[DONE]" && line.trim() !== "data: [DONE]").join("\n\n");
1003
+ if (readableLines) {
1004
+ const encoded = new TextEncoder().encode(readableLines);
1005
+ controller.enqueue(encoded);
1006
+ }
1007
+ } catch (error) {
1008
+ console.error("Error enqueueing to controller:", error);
1009
+ controller.enqueue(chunk);
1010
1010
  }
1011
1011
  }
1012
- }),
1013
- {
1014
- preventClose: true
1015
- }
1012
+ })
1016
1013
  ).catch((error) => {
1017
- console.error("Error piping to writable stream:", error);
1014
+ console.error("Error piping to controller:", error);
1015
+ try {
1016
+ controller.close();
1017
+ } catch {
1018
+ }
1018
1019
  });
1019
1020
  this.processChatResponse_vNext({
1020
1021
  stream: streamForProcessing,
@@ -1074,31 +1075,36 @@ var Agent = class extends BaseResource {
1074
1075
  toolInvocation.result = result;
1075
1076
  }
1076
1077
  const updatedMessages = lastMessage != null ? [...messages.filter((m) => m.id !== lastMessage.id), lastMessage] : [...messages];
1077
- this.processStreamResponse(
1078
- {
1079
- ...processedParams,
1080
- messages: updatedMessages
1081
- },
1082
- writable
1083
- ).catch((error) => {
1084
- console.error("Error processing stream response:", error);
1085
- });
1078
+ try {
1079
+ await this.processStreamResponse(
1080
+ {
1081
+ ...processedParams,
1082
+ messages: updatedMessages
1083
+ },
1084
+ controller
1085
+ );
1086
+ } catch (error) {
1087
+ console.error("Error processing recursive stream response:", error);
1088
+ }
1086
1089
  }
1087
1090
  }
1088
1091
  if (!shouldExecuteClientTool) {
1089
- setTimeout(() => {
1090
- writable.close();
1091
- }, 0);
1092
+ await pipePromise;
1093
+ controller.close();
1092
1094
  }
1093
1095
  } else {
1094
- setTimeout(() => {
1095
- writable.close();
1096
- }, 0);
1096
+ await pipePromise;
1097
+ controller.close();
1097
1098
  }
1098
1099
  },
1099
1100
  lastMessage: void 0
1100
- }).catch((error) => {
1101
+ }).catch(async (error) => {
1101
1102
  console.error("Error processing stream response:", error);
1103
+ try {
1104
+ await pipePromise;
1105
+ controller.close();
1106
+ } catch {
1107
+ }
1102
1108
  });
1103
1109
  } catch (error) {
1104
1110
  console.error("Error processing stream response:", error);
@@ -1149,8 +1155,13 @@ var Agent = class extends BaseResource {
1149
1155
  schema: zodToJsonSchema(params.structuredOutput.schema)
1150
1156
  } : void 0
1151
1157
  };
1152
- const { readable, writable } = new TransformStream();
1153
- const response = await this.processStreamResponse(processedParams, writable);
1158
+ let readableController;
1159
+ const readable = new ReadableStream({
1160
+ start(controller) {
1161
+ readableController = controller;
1162
+ }
1163
+ });
1164
+ const response = await this.processStreamResponse(processedParams, readableController);
1154
1165
  const streamResponse = new Response(readable, {
1155
1166
  status: response.status,
1156
1167
  statusText: response.statusText,
@@ -1167,8 +1178,13 @@ var Agent = class extends BaseResource {
1167
1178
  return streamResponse;
1168
1179
  }
1169
1180
  async approveToolCall(params) {
1170
- const { readable, writable } = new TransformStream();
1171
- const response = await this.processStreamResponse(params, writable, "approve-tool-call");
1181
+ let readableController;
1182
+ const readable = new ReadableStream({
1183
+ start(controller) {
1184
+ readableController = controller;
1185
+ }
1186
+ });
1187
+ const response = await this.processStreamResponse(params, readableController, "approve-tool-call");
1172
1188
  const streamResponse = new Response(readable, {
1173
1189
  status: response.status,
1174
1190
  statusText: response.statusText,
@@ -1185,8 +1201,13 @@ var Agent = class extends BaseResource {
1185
1201
  return streamResponse;
1186
1202
  }
1187
1203
  async declineToolCall(params) {
1188
- const { readable, writable } = new TransformStream();
1189
- const response = await this.processStreamResponse(params, writable, "decline-tool-call");
1204
+ let readableController;
1205
+ const readable = new ReadableStream({
1206
+ start(controller) {
1207
+ readableController = controller;
1208
+ }
1209
+ });
1210
+ const response = await this.processStreamResponse(params, readableController, "decline-tool-call");
1190
1211
  const streamResponse = new Response(readable, {
1191
1212
  status: response.status,
1192
1213
  statusText: response.statusText,