@mastra/client-js 1.24.0 → 1.25.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
@@ -297,6 +297,40 @@ function requestContextQueryString(requestContext, delimiter = "?") {
297
297
  const queryString = searchParams.toString();
298
298
  return queryString ? `${delimiter}${queryString}` : "";
299
299
  }
300
+
301
+ // src/utils/client-tool-model-output.ts
302
+ function normalizeModelOutput(output) {
303
+ if (output == null || typeof output !== "object") return output;
304
+ const obj = output;
305
+ if (obj.type !== "content" || !Array.isArray(obj.value)) return output;
306
+ return {
307
+ ...obj,
308
+ value: obj.value.map((item) => {
309
+ if (item == null || typeof item !== "object") return item;
310
+ const part = item;
311
+ if (part.type === "image-url" && typeof part.url === "string") {
312
+ const semicolonIndex = part.url.indexOf(";");
313
+ const commaIndex = part.url.indexOf(",");
314
+ const mediaTypeEnd = semicolonIndex === -1 ? commaIndex : commaIndex === -1 ? semicolonIndex : Math.min(semicolonIndex, commaIndex);
315
+ const mediaType = typeof part.mediaType === "string" && part.mediaType ? part.mediaType : part.url.startsWith("data:") ? part.url.slice(5, mediaTypeEnd === -1 ? void 0 : mediaTypeEnd) || "image/jpeg" : "image/jpeg";
316
+ return { type: "media", data: part.url, mediaType };
317
+ }
318
+ if (part.type === "image-data" && typeof part.data === "string") {
319
+ return { type: "media", data: part.data, mediaType: part.mediaType ?? "image/jpeg" };
320
+ }
321
+ if (part.type === "file-data" && typeof part.data === "string") {
322
+ return { type: "media", data: part.data, mediaType: part.mediaType ?? "application/octet-stream" };
323
+ }
324
+ return part;
325
+ })
326
+ };
327
+ }
328
+ async function getClientToolModelOutput(clientTool, result) {
329
+ const toModelOutput = clientTool.toModelOutput;
330
+ if (typeof toModelOutput !== "function" || result == null) return void 0;
331
+ const modelOutput = await toModelOutput(result);
332
+ return modelOutput == null ? void 0 : normalizeModelOutput(modelOutput);
333
+ }
300
334
  function isZodType(value) {
301
335
  return typeof value === "object" && value !== null && "_def" in value && "parse" in value && typeof value.parse === "function" && "safeParse" in value && typeof value.safeParse === "function";
302
336
  }
@@ -591,11 +625,15 @@ async function executeToolCallAndRespond({
591
625
  }
592
626
  }
593
627
  });
628
+ const modelOutput = await getClientToolModelOutput(clientTool, result);
594
629
  const toolResultContent = {
595
630
  type: "tool-result",
596
631
  toolCallId: toolCall.payload.toolCallId,
597
632
  toolName: toolCall.payload.toolName,
598
- result
633
+ result,
634
+ // Carry the client-side toModelOutput result so the server uses it
635
+ // when building the model prompt, while result keeps the raw value.
636
+ ...modelOutput != null ? { providerOptions: { mastra: { modelOutput } } } : {}
599
637
  };
600
638
  if (observability) {
601
639
  toolResultContent.__mastraObservability = observability;
@@ -943,6 +981,7 @@ var Agent = class extends BaseResource {
943
981
  const clientTool = activeClientTools[toolCall.toolName];
944
982
  if (!clientTool || typeof clientTool.execute !== "function") continue;
945
983
  let result;
984
+ let modelOutput;
946
985
  let observability;
947
986
  try {
948
987
  const execution = await executeClientToolWithObservability({
@@ -966,8 +1005,14 @@ var Agent = class extends BaseResource {
966
1005
  });
967
1006
  result = execution.result;
968
1007
  observability = execution.observability;
1008
+ try {
1009
+ modelOutput = await getClientToolModelOutput(clientTool, result);
1010
+ } catch {
1011
+ modelOutput = void 0;
1012
+ }
969
1013
  } catch (error) {
970
1014
  result = { error: String(error) };
1015
+ modelOutput = void 0;
971
1016
  }
972
1017
  const toolResultContent = {
973
1018
  type: "tool-result",
@@ -993,6 +1038,9 @@ var Agent = class extends BaseResource {
993
1038
  toolCallId: toolCall.toolCallId,
994
1039
  toolName: toolCall.toolName,
995
1040
  output: { type: "json", value: result },
1041
+ // Carry the client-side toModelOutput result so the server uses it
1042
+ // when building the model prompt, while output keeps the raw result.
1043
+ ...modelOutput != null ? { providerOptions: { mastra: { modelOutput } } } : {},
996
1044
  ...observability ? { __mastraObservability: observability } : {}
997
1045
  };
998
1046
  toolResultMessages.push({
@@ -1974,6 +2022,7 @@ var Agent = class extends BaseResource {
1974
2022
  shouldExecuteClientTool = true;
1975
2023
  const runId = streamRunId ?? toolCall2.toolCallId;
1976
2024
  let result;
2025
+ let modelOutput;
1977
2026
  let observability;
1978
2027
  let synthetic;
1979
2028
  try {
@@ -1996,6 +2045,7 @@ var Agent = class extends BaseResource {
1996
2045
  }
1997
2046
  }
1998
2047
  }));
2048
+ modelOutput = await getClientToolModelOutput(clientTool, result);
1999
2049
  synthetic = {
2000
2050
  type: "tool-result",
2001
2051
  runId,
@@ -2054,6 +2104,14 @@ var Agent = class extends BaseResource {
2054
2104
  result,
2055
2105
  ...observability ? { __mastraObservability: observability } : {}
2056
2106
  };
2107
+ if (modelOutput != null) {
2108
+ const partWithMetadata = toolInvocationPart;
2109
+ const existingMastra = partWithMetadata.providerMetadata?.mastra != null && typeof partWithMetadata.providerMetadata.mastra === "object" ? partWithMetadata.providerMetadata.mastra : {};
2110
+ partWithMetadata.providerMetadata = {
2111
+ ...partWithMetadata.providerMetadata,
2112
+ mastra: { ...existingMastra, modelOutput }
2113
+ };
2114
+ }
2057
2115
  }
2058
2116
  const toolInvocation = lastMessage?.toolInvocations?.find(
2059
2117
  (toolInvocation2) => toolInvocation2.toolCallId === toolCall2.toolCallId
@@ -4809,6 +4867,15 @@ var StoredAgent = class extends BaseResource {
4809
4867
  body: params
4810
4868
  });
4811
4869
  }
4870
+ /**
4871
+ * Opens a source-provider change request for deterministic agent JSON without mutating storage.
4872
+ */
4873
+ openChangeRequest(params) {
4874
+ return this.request(`/stored/agents/${encodeURIComponent(this.storedAgentId)}/change-request`, {
4875
+ method: "POST",
4876
+ body: params
4877
+ });
4878
+ }
4812
4879
  /**
4813
4880
  * Deletes the stored agent
4814
4881
  * @param requestContext - Optional request context to pass as query parameter