@mastra/client-js 1.45.1-alpha.1 → 1.46.0-alpha.4

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
@@ -670,7 +670,10 @@ async function executeToolCallAndRespond({ response, params, agentId, resourceId
670
670
  content: [toolResultContent]
671
671
  }];
672
672
  const updatedMessages = threadId ? newMessages : [...Array.isArray(params.messages) ? params.messages : [], ...newMessages];
673
- const respondOptions = { ...params };
673
+ const respondOptions = {
674
+ ...params,
675
+ clientTools: params.clientToolsResolver?.() ?? params.clientTools
676
+ };
674
677
  delete respondOptions.messages;
675
678
  return respondFn(updatedMessages, respondOptions);
676
679
  }
@@ -749,11 +752,13 @@ var AgentVoice = class extends BaseResource {
749
752
  var Agent = class extends BaseResource {
750
753
  agentId;
751
754
  version;
755
+ routeOverrides;
752
756
  voice;
753
- constructor(options, agentId, version) {
757
+ constructor(options, agentId, version, routeOverrides) {
754
758
  super(options);
755
759
  this.agentId = agentId;
756
760
  this.version = version;
761
+ this.routeOverrides = routeOverrides;
757
762
  this.voice = new AgentVoice(options, this.agentId, this.version);
758
763
  }
759
764
  getQueryString(requestContext, delimiter = "?") {
@@ -821,7 +826,8 @@ var Agent = class extends BaseResource {
821
826
  streamOptions: {
822
827
  ...streamOptions,
823
828
  requestContext: parseClientRequestContext(streamOptions.requestContext),
824
- clientTools: processClientTools(streamOptions.clientTools)
829
+ clientTools: processClientTools(streamOptions.clientToolsResolver?.() ?? streamOptions.clientTools),
830
+ clientToolsResolver: void 0
825
831
  }
826
832
  }
827
833
  },
@@ -998,7 +1004,11 @@ var Agent = class extends BaseResource {
998
1004
  resourceId,
999
1005
  threadId
1000
1006
  });
1001
- const activeClientTools = activeRuntimeOptions?.clientTools;
1007
+ if (!activeRuntimeOptions) {
1008
+ agent.deleteSignalRuntimeOptions(runId);
1009
+ return;
1010
+ }
1011
+ const activeClientTools = activeRuntimeOptions.clientToolsResolver?.() ?? activeRuntimeOptions.clientTools;
1002
1012
  if (!activeClientTools) {
1003
1013
  agent.deleteSignalRuntimeOptions(runId);
1004
1014
  return;
@@ -1291,7 +1301,7 @@ var Agent = class extends BaseResource {
1291
1301
  output: params.output ? zodToJsonSchema$1(params.output) : void 0,
1292
1302
  experimental_output: params.experimental_output ? zodToJsonSchema$1(params.experimental_output) : void 0,
1293
1303
  requestContext: parseClientRequestContext(params.requestContext),
1294
- clientTools: processClientTools(params.clientTools)
1304
+ clientTools: processClientTools(params.clientToolsResolver?.() ?? params.clientTools)
1295
1305
  };
1296
1306
  const { resourceId, threadId, requestContext } = processedParams;
1297
1307
  const response = await this.request(`/agents/${this.agentId}/generate-legacy`, {
@@ -1302,7 +1312,7 @@ var Agent = class extends BaseResource {
1302
1312
  const toolCalls = response.toolCalls;
1303
1313
  if (!toolCalls || !Array.isArray(toolCalls)) return response;
1304
1314
  for (const toolCall of toolCalls) {
1305
- const clientTool = params.clientTools?.[toolCall.toolName];
1315
+ const clientTool = processedParams.clientTools?.[toolCall.toolName];
1306
1316
  if (clientTool && clientTool.execute) {
1307
1317
  const { result, observability } = await executeClientToolWithObservability({
1308
1318
  clientTool,
@@ -1346,10 +1356,11 @@ var Agent = class extends BaseResource {
1346
1356
  ...options,
1347
1357
  messages
1348
1358
  };
1359
+ const resolvedClientTools = params.clientToolsResolver?.() ?? params.clientTools;
1349
1360
  const processedParams = {
1350
1361
  ...params,
1351
1362
  requestContext: parseClientRequestContext(params.requestContext),
1352
- clientTools: processClientTools(params.clientTools),
1363
+ clientTools: processClientTools(resolvedClientTools),
1353
1364
  structuredOutput: params.structuredOutput ? {
1354
1365
  ...params.structuredOutput,
1355
1366
  schema: standardSchemaToJSONSchema(toStandardSchema(params.structuredOutput.schema))
@@ -1365,7 +1376,10 @@ var Agent = class extends BaseResource {
1365
1376
  });
1366
1377
  if (response.finishReason === "tool-calls") return executeToolCallAndRespond({
1367
1378
  response,
1368
- params,
1379
+ params: {
1380
+ ...params,
1381
+ clientTools: resolvedClientTools
1382
+ },
1369
1383
  agentId: this.agentId,
1370
1384
  resourceId,
1371
1385
  threadId,
@@ -1532,12 +1546,18 @@ var Agent = class extends BaseResource {
1532
1546
  execUpdate();
1533
1547
  },
1534
1548
  async onToolCallPart(value) {
1549
+ const partialToolCall = partialToolCalls[value.toolCallId];
1550
+ const streamedArgs = partialToolCall?.text ? parsePartialJson(partialToolCall.text).value : void 0;
1551
+ const toolCall = streamedArgs === void 0 ? value : {
1552
+ ...value,
1553
+ args: streamedArgs
1554
+ };
1535
1555
  const invocation = {
1536
1556
  state: "call",
1537
1557
  step,
1538
- ...value
1558
+ ...toolCall
1539
1559
  };
1540
- if (partialToolCalls[value.toolCallId] != null) message.toolInvocations[partialToolCalls[value.toolCallId].index] = invocation;
1560
+ if (partialToolCall != null) message.toolInvocations[partialToolCalls[value.toolCallId].index] = invocation;
1541
1561
  else {
1542
1562
  if (message.toolInvocations == null) message.toolInvocations = [];
1543
1563
  message.toolInvocations.push(invocation);
@@ -1545,12 +1565,12 @@ var Agent = class extends BaseResource {
1545
1565
  updateToolInvocationPart(value.toolCallId, invocation, value.providerMetadata);
1546
1566
  execUpdate();
1547
1567
  if (onToolCall) {
1548
- const result = await onToolCall({ toolCall: value });
1568
+ const result = await onToolCall({ toolCall });
1549
1569
  if (result != null) {
1550
1570
  const invocation = {
1551
1571
  state: "result",
1552
1572
  step,
1553
- ...value,
1573
+ ...toolCall,
1554
1574
  result
1555
1575
  };
1556
1576
  message.toolInvocations[message.toolInvocations.length - 1] = invocation;
@@ -1618,7 +1638,7 @@ var Agent = class extends BaseResource {
1618
1638
  output: params.output ? zodToJsonSchema$1(params.output) : void 0,
1619
1639
  experimental_output: params.experimental_output ? zodToJsonSchema$1(params.experimental_output) : void 0,
1620
1640
  requestContext: parseClientRequestContext(params.requestContext),
1621
- clientTools: processClientTools(params.clientTools)
1641
+ clientTools: processClientTools(params.clientToolsResolver?.() ?? params.clientTools)
1622
1642
  };
1623
1643
  const { readable, writable } = new TransformStream();
1624
1644
  const response = await this.processStreamResponseLegacy(processedParams, writable);
@@ -1751,12 +1771,18 @@ var Agent = class extends BaseResource {
1751
1771
  execUpdate();
1752
1772
  break;
1753
1773
  case "tool-call": {
1774
+ const partialToolCall = partialToolCalls[chunk.payload.toolCallId];
1775
+ const streamedArgs = partialToolCall?.text ? parsePartialJson(partialToolCall.text).value : void 0;
1776
+ const toolCall = streamedArgs === void 0 ? chunk.payload : {
1777
+ ...chunk.payload,
1778
+ args: streamedArgs
1779
+ };
1754
1780
  const invocation = {
1755
1781
  state: "call",
1756
1782
  step,
1757
- ...chunk.payload
1783
+ ...toolCall
1758
1784
  };
1759
- if (partialToolCalls[chunk.payload.toolCallId] != null) message.toolInvocations[partialToolCalls[chunk.payload.toolCallId].index] = invocation;
1785
+ if (partialToolCall != null) message.toolInvocations[partialToolCalls[chunk.payload.toolCallId].index] = invocation;
1760
1786
  else {
1761
1787
  if (message.toolInvocations == null) message.toolInvocations = [];
1762
1788
  message.toolInvocations.push(invocation);
@@ -1764,12 +1790,12 @@ var Agent = class extends BaseResource {
1764
1790
  updateToolInvocationPart(chunk.payload.toolCallId, invocation, chunk.payload.providerMetadata);
1765
1791
  execUpdate();
1766
1792
  if (onToolCall) {
1767
- const result = await onToolCall({ toolCall: chunk.payload });
1793
+ const result = await onToolCall({ toolCall });
1768
1794
  if (result != null) {
1769
1795
  const invocation = {
1770
1796
  state: "result",
1771
1797
  step,
1772
- ...chunk.payload,
1798
+ ...toolCall,
1773
1799
  result
1774
1800
  };
1775
1801
  message.toolInvocations[message.toolInvocations.length - 1] = invocation;
@@ -1872,7 +1898,8 @@ var Agent = class extends BaseResource {
1872
1898
  const { messages: _messages, ...resumeStreamBody } = processedParams;
1873
1899
  requestBody = resumeStreamBody;
1874
1900
  }
1875
- const response = await this.request(`/agents/${this.agentId}/${route}`, {
1901
+ const requestPath = route === "stream" && this.routeOverrides?.stream ? this.routeOverrides.stream : `/agents/${this.agentId}/${route}`;
1902
+ const response = await this.request(requestPath, {
1876
1903
  method: "POST",
1877
1904
  body: requestBody,
1878
1905
  stream: true
@@ -2042,7 +2069,8 @@ var Agent = class extends BaseResource {
2042
2069
  try {
2043
2070
  await this.processStreamResponse({
2044
2071
  ...processedParams,
2045
- messages: updatedMessages
2072
+ messages: updatedMessages,
2073
+ clientTools: processClientTools(processedParams.clientToolsResolver?.() ?? processedParams.clientTools)
2046
2074
  }, controller, recursionRoute);
2047
2075
  } catch (error) {
2048
2076
  console.error("Error processing recursive stream response:", error);
@@ -2162,7 +2190,7 @@ var Agent = class extends BaseResource {
2162
2190
  const processedParams = {
2163
2191
  ...params,
2164
2192
  requestContext: parseClientRequestContext(params.requestContext),
2165
- clientTools: processClientTools(params.clientTools),
2193
+ clientTools: processClientTools(params.clientToolsResolver?.() ?? params.clientTools),
2166
2194
  structuredOutput
2167
2195
  };
2168
2196
  let readableController;
@@ -2196,7 +2224,7 @@ var Agent = class extends BaseResource {
2196
2224
  const processedParams = {
2197
2225
  ...params,
2198
2226
  requestContext: parseClientRequestContext(params.requestContext),
2199
- clientTools: processClientTools(params.clientTools),
2227
+ clientTools: processClientTools(params.clientToolsResolver?.() ?? params.clientTools),
2200
2228
  structuredOutput
2201
2229
  };
2202
2230
  let readableController;
@@ -2345,7 +2373,7 @@ var Agent = class extends BaseResource {
2345
2373
  ...options,
2346
2374
  resumeData,
2347
2375
  requestContext: parseClientRequestContext(options.requestContext),
2348
- clientTools: processClientTools(options.clientTools),
2376
+ clientTools: processClientTools(options.clientToolsResolver?.() ?? options.clientTools),
2349
2377
  structuredOutput: options.structuredOutput ? {
2350
2378
  ...options.structuredOutput,
2351
2379
  schema: standardSchemaToJSONSchema(toStandardSchema(options.structuredOutput.schema))
@@ -2408,7 +2436,7 @@ var Agent = class extends BaseResource {
2408
2436
  ...options,
2409
2437
  resumeData,
2410
2438
  requestContext: parseClientRequestContext(options.requestContext),
2411
- clientTools: processClientTools(options.clientTools),
2439
+ clientTools: processClientTools(options.clientToolsResolver?.() ?? options.clientTools),
2412
2440
  structuredOutput: options.structuredOutput ? {
2413
2441
  ...options.structuredOutput,
2414
2442
  schema: standardSchemaToJSONSchema(toStandardSchema(options.structuredOutput.schema))
@@ -2468,7 +2496,7 @@ var Agent = class extends BaseResource {
2468
2496
  const { resource, thread } = memory ?? {};
2469
2497
  const threadId = processedParams.threadId ?? (typeof thread === "string" ? thread : thread?.id);
2470
2498
  const resourceId = processedParams.resourceId ?? resource;
2471
- const response = await this.request(`/agents/${this.agentId}/stream-legacy`, {
2499
+ const response = await this.request(this.routeOverrides?.stream ?? `/agents/${this.agentId}/stream-legacy`, {
2472
2500
  method: "POST",
2473
2501
  body: processedParams,
2474
2502
  stream: true
@@ -2547,6 +2575,7 @@ var Agent = class extends BaseResource {
2547
2575
  ];
2548
2576
  this.processStreamResponseLegacy({
2549
2577
  ...processedParams,
2578
+ clientTools: processClientTools(processedParams.clientToolsResolver?.() ?? processedParams.clientTools),
2550
2579
  messages: updatedMessages
2551
2580
  }, writable).catch((error) => {
2552
2581
  console.error("Error processing stream response:", error);
@@ -2760,6 +2789,24 @@ var MemoryThread = class extends BaseResource {
2760
2789
  body
2761
2790
  });
2762
2791
  }
2792
+ /**
2793
+ * Transfers ownership of the thread (and all of its messages) to a different resource.
2794
+ *
2795
+ * This is a privileged operation: the server rejects it when the caller is resource-scoped
2796
+ * (i.e. a per-user/tenant context). Unlike `update`, it does not reset the thread's `createdAt`.
2797
+ * @param params - Transfer parameters including the target `resourceId`, optional `agentId`, and request context.
2798
+ * @returns Promise containing the transferred thread with its new `resourceId`
2799
+ */
2800
+ transfer(params) {
2801
+ const { agentId, requestContext, ...body } = params;
2802
+ const resolvedAgentId = agentId ?? this.agentId;
2803
+ const agentIdParam = resolvedAgentId ? `?agentId=${resolvedAgentId}` : "";
2804
+ const contextParam = requestContextQueryString(requestContext, agentIdParam ? "&" : "?");
2805
+ return this.request(`/memory/threads/${this.threadId}/transfer${agentIdParam}${contextParam}`, {
2806
+ method: "POST",
2807
+ body
2808
+ });
2809
+ }
2763
2810
  };
2764
2811
  /**
2765
2812
  * Backwards-compat helper: `deleteMessages` historically accepted a `RequestContext` (or plain
@@ -6317,6 +6364,25 @@ function agentControllerMessageText(message) {
6317
6364
  }
6318
6365
  //#endregion
6319
6366
  //#region src/client.ts
6367
+ /**
6368
+ * Provides typed access to agents, workflows, and other APIs on a running Mastra server.
6369
+ * Point `baseUrl` at your server and configure authentication when required.
6370
+ *
6371
+ * @example
6372
+ * ```typescript
6373
+ * import { MastraClient } from '@mastra/client-js';
6374
+ *
6375
+ * const client = new MastraClient({ baseUrl: 'http://localhost:4111' });
6376
+ * ```
6377
+ *
6378
+ * @see For documentation bundled with your installed package, locate
6379
+ * `@mastra/client-js/package.json` with your project's resolver or package-manager
6380
+ * tooling, then read `dist/docs/SKILL.md` from that package root and follow its
6381
+ * reference links. Use package-manager tools for virtual or archived packages.
6382
+ *
6383
+ * @see [Client documentation](https://mastra.ai/reference/client-js/mastra-client)
6384
+ * if packaged docs are unavailable.
6385
+ */
6320
6386
  var MastraClient = class extends BaseResource {
6321
6387
  observability;
6322
6388
  conversations;
@@ -6351,8 +6417,8 @@ var MastraClient = class extends BaseResource {
6351
6417
  * @param version - Optional version selector for stored agent overrides
6352
6418
  * @returns Agent instance
6353
6419
  */
6354
- getAgent(agentId, version) {
6355
- return new Agent(this.options, agentId, version);
6420
+ getAgent(agentId, version, routeOverrides) {
6421
+ return new Agent(this.options, agentId, version, routeOverrides);
6356
6422
  }
6357
6423
  /**
6358
6424
  * Lists the agent controllers hosted on the connected Mastra instance.
@@ -7095,6 +7161,12 @@ var MastraClient = class extends BaseResource {
7095
7161
  return new DynamicWorkflow(this.options, dynamicWorkflowId);
7096
7162
  }
7097
7163
  /**
7164
+ * Retrieves workflow builder settings for UI gating.
7165
+ */
7166
+ getWorkflowBuilderSettings() {
7167
+ return this.request("/editor/workflow-builder/settings");
7168
+ }
7169
+ /**
7098
7170
  * Lists all stored prompt blocks with optional pagination
7099
7171
  * @param params - Optional pagination and ordering parameters
7100
7172
  * @returns Promise containing paginated list of stored prompt blocks
@@ -7424,10 +7496,12 @@ var MastraClient = class extends BaseResource {
7424
7496
  /**
7425
7497
  * Lists all datasets with optional pagination
7426
7498
  */
7427
- listDatasets(pagination) {
7499
+ listDatasets(params) {
7428
7500
  const searchParams = new URLSearchParams();
7429
- if (pagination?.page !== void 0) searchParams.set("page", String(pagination.page));
7430
- if (pagination?.perPage !== void 0) searchParams.set("perPage", String(pagination.perPage));
7501
+ if (params?.page !== void 0) searchParams.set("page", String(params.page));
7502
+ if (params?.perPage !== void 0) searchParams.set("perPage", String(params.perPage));
7503
+ if (params?.targetType !== void 0) searchParams.set("targetType", params.targetType);
7504
+ for (const id of params?.targetIds ?? []) searchParams.append("targetIds", id);
7431
7505
  const qs = searchParams.toString();
7432
7506
  return this.request(`/datasets${qs ? `?${qs}` : ""}`);
7433
7507
  }
@@ -7597,6 +7671,8 @@ var MastraClient = class extends BaseResource {
7597
7671
  if (params?.comparisonId !== void 0) searchParams.set("comparisonId", params.comparisonId);
7598
7672
  if (params?.variantId !== void 0) searchParams.set("variantId", params.variantId);
7599
7673
  if (params?.trialIndex !== void 0) searchParams.set("trialIndex", String(params.trialIndex));
7674
+ if (params?.targetType !== void 0) searchParams.set("targetType", params.targetType);
7675
+ if (params?.targetId !== void 0) searchParams.set("targetId", params.targetId);
7600
7676
  const qs = searchParams.toString();
7601
7677
  return this.request(`/experiments${qs ? `?${qs}` : ""}`);
7602
7678
  }
@@ -7617,6 +7693,8 @@ var MastraClient = class extends BaseResource {
7617
7693
  if (params?.comparisonId !== void 0) searchParams.set("comparisonId", params.comparisonId);
7618
7694
  if (params?.variantId !== void 0) searchParams.set("variantId", params.variantId);
7619
7695
  if (params?.trialIndex !== void 0) searchParams.set("trialIndex", String(params.trialIndex));
7696
+ if (params?.targetType !== void 0) searchParams.set("targetType", params.targetType);
7697
+ if (params?.targetId !== void 0) searchParams.set("targetId", params.targetId);
7620
7698
  const qs = searchParams.toString();
7621
7699
  return this.request(`/datasets/${encodeURIComponent(datasetId)}/experiments${qs ? `?${qs}` : ""}`);
7622
7700
  }