@mastra/client-js 1.0.0-beta.0 → 1.0.0-beta.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.
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;
@@ -208,14 +207,14 @@ async function executeToolCallAndRespond({
208
207
  return response;
209
208
  }
210
209
  for (const toolCall of toolCalls) {
211
- const clientTool = params.clientTools?.[toolCall.toolName];
210
+ const clientTool = params.clientTools?.[toolCall.payload.toolName];
212
211
  if (clientTool && clientTool.execute) {
213
- const result = await clientTool.execute(toolCall?.args, {
212
+ const result = await clientTool.execute(toolCall?.payload.args, {
214
213
  requestContext,
215
214
  tracingContext: { currentSpan: void 0 },
216
215
  agent: {
217
216
  messages: response.messages,
218
- toolCallId: toolCall?.toolCallId,
217
+ toolCallId: toolCall?.payload.toolCallId,
219
218
  suspend: async () => {
220
219
  },
221
220
  threadId,
@@ -229,8 +228,8 @@ async function executeToolCallAndRespond({
229
228
  content: [
230
229
  {
231
230
  type: "tool-result",
232
- toolCallId: toolCall.toolCallId,
233
- toolName: toolCall.toolName,
231
+ toolCallId: toolCall.payload.toolCallId,
232
+ toolName: toolCall.payload.toolName,
234
233
  result
235
234
  }
236
235
  ]
@@ -243,6 +242,7 @@ async function executeToolCallAndRespond({
243
242
  }
244
243
  }
245
244
  }
245
+ return response;
246
246
  }
247
247
  var AgentVoice = class extends BaseResource {
248
248
  constructor(options, agentId) {
@@ -975,7 +975,7 @@ var Agent = class extends BaseResource {
975
975
  });
976
976
  onFinish?.({ message, finishReason, usage });
977
977
  }
978
- async processStreamResponse(processedParams, writable, route = "stream") {
978
+ async processStreamResponse(processedParams, controller, route = "stream") {
979
979
  const response = await this.request(`/api/agents/${this.agentId}/${route}`, {
980
980
  method: "POST",
981
981
  body: processedParams,
@@ -987,29 +987,30 @@ var Agent = class extends BaseResource {
987
987
  try {
988
988
  let toolCalls = [];
989
989
  let messages = [];
990
- const [streamForWritable, streamForProcessing] = response.body.tee();
991
- streamForWritable.pipeTo(
990
+ const [streamForController, streamForProcessing] = response.body.tee();
991
+ const pipePromise = streamForController.pipeTo(
992
992
  new WritableStream({
993
993
  async write(chunk) {
994
- let writer;
995
994
  try {
996
- writer = writable.getWriter();
997
995
  const text = new TextDecoder().decode(chunk);
998
996
  const lines = text.split("\n\n");
999
- const readableLines = lines.filter((line) => line !== "[DONE]").join("\n\n");
1000
- await writer.write(new TextEncoder().encode(readableLines));
1001
- } catch {
1002
- await writer?.write(chunk);
1003
- } finally {
1004
- writer?.releaseLock();
997
+ const readableLines = lines.filter((line) => line.trim() !== "[DONE]" && line.trim() !== "data: [DONE]").join("\n\n");
998
+ if (readableLines) {
999
+ const encoded = new TextEncoder().encode(readableLines);
1000
+ controller.enqueue(encoded);
1001
+ }
1002
+ } catch (error) {
1003
+ console.error("Error enqueueing to controller:", error);
1004
+ controller.enqueue(chunk);
1005
1005
  }
1006
1006
  }
1007
- }),
1008
- {
1009
- preventClose: true
1010
- }
1007
+ })
1011
1008
  ).catch((error) => {
1012
- console.error("Error piping to writable stream:", error);
1009
+ console.error("Error piping to controller:", error);
1010
+ try {
1011
+ controller.close();
1012
+ } catch {
1013
+ }
1013
1014
  });
1014
1015
  this.processChatResponse_vNext({
1015
1016
  stream: streamForProcessing,
@@ -1065,31 +1066,36 @@ var Agent = class extends BaseResource {
1065
1066
  toolInvocation.result = result;
1066
1067
  }
1067
1068
  const updatedMessages = lastMessage != null ? [...messages.filter((m) => m.id !== lastMessage.id), lastMessage] : [...messages];
1068
- this.processStreamResponse(
1069
- {
1070
- ...processedParams,
1071
- messages: updatedMessages
1072
- },
1073
- writable
1074
- ).catch((error) => {
1075
- console.error("Error processing stream response:", error);
1076
- });
1069
+ try {
1070
+ await this.processStreamResponse(
1071
+ {
1072
+ ...processedParams,
1073
+ messages: updatedMessages
1074
+ },
1075
+ controller
1076
+ );
1077
+ } catch (error) {
1078
+ console.error("Error processing recursive stream response:", error);
1079
+ }
1077
1080
  }
1078
1081
  }
1079
1082
  if (!shouldExecuteClientTool) {
1080
- setTimeout(() => {
1081
- writable.close();
1082
- }, 0);
1083
+ await pipePromise;
1084
+ controller.close();
1083
1085
  }
1084
1086
  } else {
1085
- setTimeout(() => {
1086
- writable.close();
1087
- }, 0);
1087
+ await pipePromise;
1088
+ controller.close();
1088
1089
  }
1089
1090
  },
1090
1091
  lastMessage: void 0
1091
- }).catch((error) => {
1092
+ }).catch(async (error) => {
1092
1093
  console.error("Error processing stream response:", error);
1094
+ try {
1095
+ await pipePromise;
1096
+ controller.close();
1097
+ } catch {
1098
+ }
1093
1099
  });
1094
1100
  } catch (error) {
1095
1101
  console.error("Error processing stream response:", error);
@@ -1139,8 +1145,13 @@ var Agent = class extends BaseResource {
1139
1145
  schema: zodToJsonSchema(params.structuredOutput.schema)
1140
1146
  } : void 0
1141
1147
  };
1142
- const { readable, writable } = new TransformStream();
1143
- const response = await this.processStreamResponse(processedParams, writable);
1148
+ let readableController;
1149
+ const readable = new ReadableStream({
1150
+ start(controller) {
1151
+ readableController = controller;
1152
+ }
1153
+ });
1154
+ const response = await this.processStreamResponse(processedParams, readableController);
1144
1155
  const streamResponse = new Response(readable, {
1145
1156
  status: response.status,
1146
1157
  statusText: response.statusText,
@@ -1157,8 +1168,13 @@ var Agent = class extends BaseResource {
1157
1168
  return streamResponse;
1158
1169
  }
1159
1170
  async approveToolCall(params) {
1160
- const { readable, writable } = new TransformStream();
1161
- const response = await this.processStreamResponse(params, writable, "approve-tool-call");
1171
+ let readableController;
1172
+ const readable = new ReadableStream({
1173
+ start(controller) {
1174
+ readableController = controller;
1175
+ }
1176
+ });
1177
+ const response = await this.processStreamResponse(params, readableController, "approve-tool-call");
1162
1178
  const streamResponse = new Response(readable, {
1163
1179
  status: response.status,
1164
1180
  statusText: response.statusText,
@@ -1175,8 +1191,13 @@ var Agent = class extends BaseResource {
1175
1191
  return streamResponse;
1176
1192
  }
1177
1193
  async declineToolCall(params) {
1178
- const { readable, writable } = new TransformStream();
1179
- const response = await this.processStreamResponse(params, writable, "decline-tool-call");
1194
+ let readableController;
1195
+ const readable = new ReadableStream({
1196
+ start(controller) {
1197
+ readableController = controller;
1198
+ }
1199
+ });
1200
+ const response = await this.processStreamResponse(params, readableController, "decline-tool-call");
1180
1201
  const streamResponse = new Response(readable, {
1181
1202
  status: response.status,
1182
1203
  statusText: response.statusText,