@mastra/client-js 1.0.0-beta.1 → 1.0.0-beta.11

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
@@ -3,8 +3,7 @@ import { v4 } from '@lukeed/uuid';
3
3
  import { getErrorFromUnknown } from '@mastra/core/error';
4
4
  import { RequestContext } from '@mastra/core/request-context';
5
5
  import { isVercelTool } from '@mastra/core/tools/is-vercel-tool';
6
- import { z } from 'zod';
7
- import originalZodToJsonSchema from 'zod-to-json-schema';
6
+ import { zodToJsonSchema as zodToJsonSchema$1 } from '@mastra/schema-compat/zod-to-json';
8
7
 
9
8
  // src/resources/agent.ts
10
9
  function parseClientRequestContext(requestContext) {
@@ -37,11 +36,7 @@ function zodToJsonSchema(zodSchema) {
37
36
  if (!isZodType(zodSchema)) {
38
37
  return zodSchema;
39
38
  }
40
- if ("toJSONSchema" in z) {
41
- const fn = "toJSONSchema";
42
- return z[fn].call(z, zodSchema);
43
- }
44
- return originalZodToJsonSchema(zodSchema, { $refStrategy: "relative" });
39
+ return zodToJsonSchema$1(zodSchema);
45
40
  }
46
41
 
47
42
  // src/utils/process-client-tools.ts
@@ -144,11 +139,20 @@ var BaseResource = class {
144
139
  */
145
140
  async request(path, options = {}) {
146
141
  let lastError = null;
147
- const { baseUrl, retries = 3, backoffMs = 100, maxBackoffMs = 1e3, headers = {}, credentials } = this.options;
142
+ const {
143
+ baseUrl,
144
+ retries = 3,
145
+ backoffMs = 100,
146
+ maxBackoffMs = 1e3,
147
+ headers = {},
148
+ credentials,
149
+ fetch: customFetch
150
+ } = this.options;
151
+ const fetchFn = customFetch || fetch;
148
152
  let delay = backoffMs;
149
153
  for (let attempt = 0; attempt <= retries; attempt++) {
150
154
  try {
151
- const response = await fetch(`${baseUrl.replace(/\/$/, "")}${path}`, {
155
+ const response = await fetchFn(`${baseUrl.replace(/\/$/, "")}${path}`, {
152
156
  ...options,
153
157
  headers: {
154
158
  ...options.body && !(options.body instanceof FormData) && (options.method === "POST" || options.method === "PUT") ? { "content-type": "application/json" } : {},
@@ -787,7 +791,7 @@ var Agent = class extends BaseResource {
787
791
  case "tripwire": {
788
792
  message.parts.push({
789
793
  type: "text",
790
- text: chunk.payload.tripwireReason
794
+ text: chunk.payload.reason
791
795
  });
792
796
  execUpdate();
793
797
  break;
@@ -1452,7 +1456,7 @@ var MemoryThread = class extends BaseResource {
1452
1456
  if (include) queryParams.include = JSON.stringify(include);
1453
1457
  const query = new URLSearchParams(queryParams);
1454
1458
  const queryString = query.toString();
1455
- const url = `/api/memory/threads/${this.threadId}/messages${queryString ? `?${queryString}` : ""}${requestContextQueryString(requestContext, queryString ? "&" : "?")}`;
1459
+ const url = `/api/memory/threads/${this.threadId}/messages?agentId=${this.agentId}${queryString ? `&${queryString}` : ""}${requestContextQueryString(requestContext, "&")}`;
1456
1460
  return this.request(url);
1457
1461
  }
1458
1462
  /**
@@ -1611,15 +1615,21 @@ var Workflow = class extends BaseResource {
1611
1615
  if (params?.toDate) {
1612
1616
  searchParams.set("toDate", params.toDate.toISOString());
1613
1617
  }
1614
- if (params?.perPage !== null && params?.perPage !== void 0) {
1615
- if (params.perPage === false) {
1616
- searchParams.set("perPage", "false");
1617
- } else if (typeof params.perPage === "number" && params.perPage > 0 && Number.isInteger(params.perPage)) {
1618
- searchParams.set("perPage", String(params.perPage));
1618
+ if (params?.page !== void 0) {
1619
+ searchParams.set("page", String(params.page));
1620
+ }
1621
+ if (params?.perPage !== void 0) {
1622
+ searchParams.set("perPage", String(params.perPage));
1623
+ }
1624
+ if (params?.limit !== null && params?.limit !== void 0) {
1625
+ if (params.limit === false) {
1626
+ searchParams.set("limit", "false");
1627
+ } else if (typeof params.limit === "number" && params.limit > 0 && Number.isInteger(params.limit)) {
1628
+ searchParams.set("limit", String(params.limit));
1619
1629
  }
1620
1630
  }
1621
- if (params?.page !== null && params?.page !== void 0 && !isNaN(Number(params?.page))) {
1622
- searchParams.set("page", String(params.page));
1631
+ if (params?.offset !== null && params?.offset !== void 0 && !isNaN(Number(params?.offset))) {
1632
+ searchParams.set("offset", String(params.offset));
1623
1633
  }
1624
1634
  if (params?.resourceId) {
1625
1635
  searchParams.set("resourceId", params.resourceId);
@@ -1642,6 +1652,16 @@ var Workflow = class extends BaseResource {
1642
1652
  runById(runId, requestContext) {
1643
1653
  return this.request(`/api/workflows/${this.workflowId}/runs/${runId}${requestContextQueryString(requestContext)}`);
1644
1654
  }
1655
+ /**
1656
+ * Deletes a specific workflow run by its ID
1657
+ * @param runId - The ID of the workflow run to delete
1658
+ * @returns Promise containing a success message
1659
+ */
1660
+ deleteRunById(runId) {
1661
+ return this.request(`/api/workflows/${this.workflowId}/runs/${runId}`, {
1662
+ method: "DELETE"
1663
+ });
1664
+ }
1645
1665
  /**
1646
1666
  * Retrieves the execution result for a specific workflow run by its ID
1647
1667
  * @param runId - The ID of the workflow run to retrieve the execution result for
@@ -1686,6 +1706,7 @@ var Workflow = class extends BaseResource {
1686
1706
  return this.start({
1687
1707
  runId,
1688
1708
  inputData: p.inputData,
1709
+ initialState: p.initialState,
1689
1710
  requestContext: p.requestContext,
1690
1711
  tracingOptions: p.tracingOptions
1691
1712
  });
@@ -1694,12 +1715,18 @@ var Workflow = class extends BaseResource {
1694
1715
  return this.startAsync({
1695
1716
  runId,
1696
1717
  inputData: p.inputData,
1718
+ initialState: p.initialState,
1697
1719
  requestContext: p.requestContext,
1698
1720
  tracingOptions: p.tracingOptions
1699
1721
  });
1700
1722
  },
1701
1723
  stream: async (p) => {
1702
- return this.stream({ runId, inputData: p.inputData, requestContext: p.requestContext });
1724
+ return this.stream({
1725
+ runId,
1726
+ inputData: p.inputData,
1727
+ initialState: p.initialState,
1728
+ requestContext: p.requestContext
1729
+ });
1703
1730
  },
1704
1731
  resume: async (p) => {
1705
1732
  return this.resume({
@@ -1731,14 +1758,19 @@ var Workflow = class extends BaseResource {
1731
1758
  }
1732
1759
  /**
1733
1760
  * Starts a workflow run synchronously without waiting for the workflow to complete
1734
- * @param params - Object containing the runId, inputData and requestContext
1761
+ * @param params - Object containing the runId, inputData, initialState and requestContext
1735
1762
  * @returns Promise containing success message
1736
1763
  */
1737
1764
  start(params) {
1738
1765
  const requestContext = parseClientRequestContext(params.requestContext);
1739
1766
  return this.request(`/api/workflows/${this.workflowId}/start?runId=${params.runId}`, {
1740
1767
  method: "POST",
1741
- body: { inputData: params?.inputData, requestContext, tracingOptions: params.tracingOptions }
1768
+ body: {
1769
+ inputData: params?.inputData,
1770
+ initialState: params?.initialState,
1771
+ requestContext,
1772
+ tracingOptions: params.tracingOptions
1773
+ }
1742
1774
  });
1743
1775
  }
1744
1776
  /**
@@ -1766,7 +1798,7 @@ var Workflow = class extends BaseResource {
1766
1798
  }
1767
1799
  /**
1768
1800
  * Starts a workflow run asynchronously and returns a promise that resolves when the workflow is complete
1769
- * @param params - Object containing the optional runId, inputData and requestContext
1801
+ * @param params - Object containing the optional runId, inputData, initialState and requestContext
1770
1802
  * @returns Promise containing the workflow execution results
1771
1803
  */
1772
1804
  startAsync(params) {
@@ -1777,12 +1809,17 @@ var Workflow = class extends BaseResource {
1777
1809
  const requestContext = parseClientRequestContext(params.requestContext);
1778
1810
  return this.request(`/api/workflows/${this.workflowId}/start-async?${searchParams.toString()}`, {
1779
1811
  method: "POST",
1780
- body: { inputData: params.inputData, requestContext, tracingOptions: params.tracingOptions }
1812
+ body: {
1813
+ inputData: params.inputData,
1814
+ initialState: params.initialState,
1815
+ requestContext,
1816
+ tracingOptions: params.tracingOptions
1817
+ }
1781
1818
  });
1782
1819
  }
1783
1820
  /**
1784
1821
  * Starts a workflow run and returns a stream
1785
- * @param params - Object containing the optional runId, inputData and requestContext
1822
+ * @param params - Object containing the optional runId, inputData, initialState and requestContext
1786
1823
  * @returns Promise containing the workflow execution results
1787
1824
  */
1788
1825
  async stream(params) {
@@ -1795,7 +1832,12 @@ var Workflow = class extends BaseResource {
1795
1832
  `/api/workflows/${this.workflowId}/stream?${searchParams.toString()}`,
1796
1833
  {
1797
1834
  method: "POST",
1798
- body: { inputData: params.inputData, requestContext, tracingOptions: params.tracingOptions },
1835
+ body: {
1836
+ inputData: params.inputData,
1837
+ initialState: params.initialState,
1838
+ requestContext,
1839
+ tracingOptions: params.tracingOptions
1840
+ },
1799
1841
  stream: true
1800
1842
  }
1801
1843
  );
@@ -1880,7 +1922,7 @@ var Workflow = class extends BaseResource {
1880
1922
  }
1881
1923
  /**
1882
1924
  * Starts a workflow run and returns a stream
1883
- * @param params - Object containing the optional runId, inputData and requestContext
1925
+ * @param params - Object containing the optional runId, inputData, initialState and requestContext
1884
1926
  * @returns Promise containing the workflow execution results
1885
1927
  */
1886
1928
  async streamVNext(params) {
@@ -1895,6 +1937,7 @@ var Workflow = class extends BaseResource {
1895
1937
  method: "POST",
1896
1938
  body: {
1897
1939
  inputData: params.inputData,
1940
+ initialState: params.initialState,
1898
1941
  requestContext,
1899
1942
  closeOnSuspend: params.closeOnSuspend,
1900
1943
  tracingOptions: params.tracingOptions
@@ -2075,6 +2118,142 @@ var Workflow = class extends BaseResource {
2075
2118
  }
2076
2119
  });
2077
2120
  }
2121
+ /**
2122
+ * Restarts an active workflow run synchronously without waiting for the workflow to complete
2123
+ * @param params - Object containing the runId and requestContext
2124
+ * @returns Promise containing success message
2125
+ */
2126
+ restart(params) {
2127
+ const requestContext = parseClientRequestContext(params.requestContext);
2128
+ return this.request(`/api/workflows/${this.workflowId}/restart?runId=${params.runId}`, {
2129
+ method: "POST",
2130
+ body: {
2131
+ requestContext,
2132
+ tracingOptions: params.tracingOptions
2133
+ }
2134
+ });
2135
+ }
2136
+ /**
2137
+ * Restarts an active workflow run asynchronously
2138
+ * @param params - Object containing the runId and requestContext
2139
+ * @returns Promise containing the workflow restart results
2140
+ */
2141
+ restartAsync(params) {
2142
+ const requestContext = parseClientRequestContext(params.requestContext);
2143
+ return this.request(`/api/workflows/${this.workflowId}/restart-async?runId=${params.runId}`, {
2144
+ method: "POST",
2145
+ body: {
2146
+ requestContext,
2147
+ tracingOptions: params.tracingOptions
2148
+ }
2149
+ });
2150
+ }
2151
+ /**
2152
+ * Restart all active workflow runs synchronously without waiting for the workflow to complete
2153
+ * @returns Promise containing success message
2154
+ */
2155
+ restartAllActiveWorkflowRuns() {
2156
+ return this.request(`/api/workflows/${this.workflowId}/restart-all-active-workflow-runs`, {
2157
+ method: "POST"
2158
+ });
2159
+ }
2160
+ /**
2161
+ * Restart all active workflow runs asynchronously
2162
+ * @returns Promise containing success message
2163
+ */
2164
+ restartAllActiveWorkflowRunsAsync() {
2165
+ return this.request(`/api/workflows/${this.workflowId}/restart-all-active-workflow-runs-async`, {
2166
+ method: "POST"
2167
+ });
2168
+ }
2169
+ /**
2170
+ * Time travels a workflow run synchronously without waiting for the workflow to complete
2171
+ * @param params - Object containing the runId, step, inputData, resumeData, initialState, context, nestedStepsContext, requestContext and tracingOptions
2172
+ * @returns Promise containing success message
2173
+ */
2174
+ timeTravel({
2175
+ runId,
2176
+ requestContext: paramsRequestContext,
2177
+ ...params
2178
+ }) {
2179
+ const requestContext = parseClientRequestContext(paramsRequestContext);
2180
+ return this.request(`/api/workflows/${this.workflowId}/time-travel?runId=${runId}`, {
2181
+ method: "POST",
2182
+ body: {
2183
+ ...params,
2184
+ requestContext
2185
+ }
2186
+ });
2187
+ }
2188
+ /**
2189
+ * Time travels a workflow run asynchronously
2190
+ * @param params - Object containing the runId, step, inputData, resumeData, initialState, context, nestedStepsContext, requestContext and tracingOptions
2191
+ * @returns Promise containing the workflow time travel results
2192
+ */
2193
+ timeTravelAsync({
2194
+ runId,
2195
+ requestContext: paramsRequestContext,
2196
+ ...params
2197
+ }) {
2198
+ const requestContext = parseClientRequestContext(paramsRequestContext);
2199
+ return this.request(`/api/workflows/${this.workflowId}/time-travel-async?runId=${runId}`, {
2200
+ method: "POST",
2201
+ body: {
2202
+ ...params,
2203
+ requestContext
2204
+ }
2205
+ });
2206
+ }
2207
+ /**
2208
+ * Time travels a workflow run and returns a stream
2209
+ * @param params - Object containing the runId, step, inputData, resumeData, initialState, context, nestedStepsContext, requestContext and tracingOptions
2210
+ * @returns Promise containing the workflow execution results
2211
+ */
2212
+ async timeTravelStream({ runId, requestContext: paramsRequestContext, ...params }) {
2213
+ const requestContext = parseClientRequestContext(paramsRequestContext);
2214
+ const response = await this.request(
2215
+ `/api/workflows/${this.workflowId}/time-travel-stream?runId=${runId}`,
2216
+ {
2217
+ method: "POST",
2218
+ body: {
2219
+ ...params,
2220
+ requestContext
2221
+ },
2222
+ stream: true
2223
+ }
2224
+ );
2225
+ if (!response.ok) {
2226
+ throw new Error(`Failed to time travel workflow: ${response.statusText}`);
2227
+ }
2228
+ if (!response.body) {
2229
+ throw new Error("Response body is null");
2230
+ }
2231
+ let failedChunk = void 0;
2232
+ const transformStream = new TransformStream({
2233
+ start() {
2234
+ },
2235
+ async transform(chunk, controller) {
2236
+ try {
2237
+ const decoded = new TextDecoder().decode(chunk);
2238
+ const chunks = decoded.split(RECORD_SEPARATOR);
2239
+ for (const chunk2 of chunks) {
2240
+ if (chunk2) {
2241
+ const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
2242
+ try {
2243
+ const parsedChunk = JSON.parse(newChunk);
2244
+ controller.enqueue(parsedChunk);
2245
+ failedChunk = void 0;
2246
+ } catch {
2247
+ failedChunk = newChunk;
2248
+ }
2249
+ }
2250
+ }
2251
+ } catch {
2252
+ }
2253
+ }
2254
+ });
2255
+ return response.body.pipeThrough(transformStream);
2256
+ }
2078
2257
  };
2079
2258
 
2080
2259
  // src/resources/a2a.ts
@@ -2117,7 +2296,8 @@ var A2A = class extends BaseResource {
2117
2296
  body: {
2118
2297
  method: "message/stream",
2119
2298
  params
2120
- }
2299
+ },
2300
+ stream: true
2121
2301
  });
2122
2302
  return response;
2123
2303
  }
@@ -2548,6 +2728,16 @@ var AgentBuilder = class extends BaseResource {
2548
2728
  if (params?.page !== void 0) {
2549
2729
  searchParams.set("page", String(params.page));
2550
2730
  }
2731
+ if (params?.limit !== null && params?.limit !== void 0) {
2732
+ if (params.limit === false) {
2733
+ searchParams.set("limit", "false");
2734
+ } else if (typeof params.limit === "number" && params.limit > 0 && Number.isInteger(params.limit)) {
2735
+ searchParams.set("limit", String(params.limit));
2736
+ }
2737
+ }
2738
+ if (params?.offset !== null && params?.offset !== void 0 && !isNaN(Number(params?.offset))) {
2739
+ searchParams.set("offset", String(params.offset));
2740
+ }
2551
2741
  if (params?.resourceId) {
2552
2742
  searchParams.set("resourceId", params.resourceId);
2553
2743
  }
@@ -2654,6 +2844,41 @@ var Observability = class extends BaseResource {
2654
2844
  }
2655
2845
  };
2656
2846
 
2847
+ // src/resources/stored-agent.ts
2848
+ var StoredAgent = class extends BaseResource {
2849
+ constructor(options, storedAgentId) {
2850
+ super(options);
2851
+ this.storedAgentId = storedAgentId;
2852
+ }
2853
+ /**
2854
+ * Retrieves details about the stored agent
2855
+ * @returns Promise containing stored agent details
2856
+ */
2857
+ details() {
2858
+ return this.request(`/api/stored/agents/${encodeURIComponent(this.storedAgentId)}`);
2859
+ }
2860
+ /**
2861
+ * Updates the stored agent with the provided fields
2862
+ * @param params - Fields to update
2863
+ * @returns Promise containing the updated stored agent
2864
+ */
2865
+ update(params) {
2866
+ return this.request(`/api/stored/agents/${encodeURIComponent(this.storedAgentId)}`, {
2867
+ method: "PATCH",
2868
+ body: params
2869
+ });
2870
+ }
2871
+ /**
2872
+ * Deletes the stored agent
2873
+ * @returns Promise containing deletion confirmation
2874
+ */
2875
+ delete() {
2876
+ return this.request(`/api/stored/agents/${encodeURIComponent(this.storedAgentId)}`, {
2877
+ method: "DELETE"
2878
+ });
2879
+ }
2880
+ };
2881
+
2657
2882
  // src/client.ts
2658
2883
  var MastraClient = class extends BaseResource {
2659
2884
  observability;
@@ -2666,12 +2891,15 @@ var MastraClient = class extends BaseResource {
2666
2891
  * @param requestContext - Optional request context to pass as query parameter
2667
2892
  * @returns Promise containing map of agent IDs to agent details
2668
2893
  */
2669
- listAgents(requestContext) {
2894
+ listAgents(requestContext, partial) {
2670
2895
  const requestContextParam = base64RequestContext(parseClientRequestContext(requestContext));
2671
2896
  const searchParams = new URLSearchParams();
2672
2897
  if (requestContextParam) {
2673
2898
  searchParams.set("requestContext", requestContextParam);
2674
2899
  }
2900
+ if (partial) {
2901
+ searchParams.set("partial", "true");
2902
+ }
2675
2903
  const queryString = searchParams.toString();
2676
2904
  return this.request(`/api/agents${queryString ? `?${queryString}` : ""}`);
2677
2905
  }
@@ -2813,12 +3041,15 @@ var MastraClient = class extends BaseResource {
2813
3041
  * @param requestContext - Optional request context to pass as query parameter
2814
3042
  * @returns Promise containing map of workflow IDs to workflow details
2815
3043
  */
2816
- listWorkflows(requestContext) {
3044
+ listWorkflows(requestContext, partial) {
2817
3045
  const requestContextParam = base64RequestContext(parseClientRequestContext(requestContext));
2818
3046
  const searchParams = new URLSearchParams();
2819
3047
  if (requestContextParam) {
2820
3048
  searchParams.set("requestContext", requestContextParam);
2821
3049
  }
3050
+ if (partial) {
3051
+ searchParams.set("partial", "true");
3052
+ }
2822
3053
  const queryString = searchParams.toString();
2823
3054
  return this.request(`/api/workflows${queryString ? `?${queryString}` : ""}`);
2824
3055
  }
@@ -2948,16 +3179,22 @@ var MastraClient = class extends BaseResource {
2948
3179
  }
2949
3180
  /**
2950
3181
  * Retrieves a list of available MCP servers.
2951
- * @param params - Optional parameters for pagination (perPage, page).
3182
+ * @param params - Optional parameters for pagination (page, perPage, or deprecated offset, limit).
2952
3183
  * @returns Promise containing the list of MCP servers and pagination info.
2953
3184
  */
2954
3185
  getMcpServers(params) {
2955
3186
  const searchParams = new URLSearchParams();
3187
+ if (params?.page !== void 0) {
3188
+ searchParams.set("page", String(params.page));
3189
+ }
2956
3190
  if (params?.perPage !== void 0) {
2957
3191
  searchParams.set("perPage", String(params.perPage));
2958
3192
  }
2959
- if (params?.page !== void 0) {
2960
- searchParams.set("page", String(params.page));
3193
+ if (params?.limit !== void 0) {
3194
+ searchParams.set("limit", String(params.limit));
3195
+ }
3196
+ if (params?.offset !== void 0) {
3197
+ searchParams.set("offset", String(params.offset));
2961
3198
  }
2962
3199
  const queryString = searchParams.toString();
2963
3200
  return this.request(`/api/mcp/v0/servers${queryString ? `?${queryString}` : ""}`);
@@ -3157,6 +3394,52 @@ var MastraClient = class extends BaseResource {
3157
3394
  score(params) {
3158
3395
  return this.observability.score(params);
3159
3396
  }
3397
+ // ============================================================================
3398
+ // Stored Agents
3399
+ // ============================================================================
3400
+ /**
3401
+ * Lists all stored agents with optional pagination
3402
+ * @param params - Optional pagination and ordering parameters
3403
+ * @returns Promise containing paginated list of stored agents
3404
+ */
3405
+ listStoredAgents(params) {
3406
+ const searchParams = new URLSearchParams();
3407
+ if (params?.page !== void 0) {
3408
+ searchParams.set("page", String(params.page));
3409
+ }
3410
+ if (params?.perPage !== void 0) {
3411
+ searchParams.set("perPage", String(params.perPage));
3412
+ }
3413
+ if (params?.orderBy) {
3414
+ if (params.orderBy.field) {
3415
+ searchParams.set("orderBy[field]", params.orderBy.field);
3416
+ }
3417
+ if (params.orderBy.direction) {
3418
+ searchParams.set("orderBy[direction]", params.orderBy.direction);
3419
+ }
3420
+ }
3421
+ const queryString = searchParams.toString();
3422
+ return this.request(`/api/stored/agents${queryString ? `?${queryString}` : ""}`);
3423
+ }
3424
+ /**
3425
+ * Creates a new stored agent
3426
+ * @param params - Agent configuration including id, name, instructions, model, etc.
3427
+ * @returns Promise containing the created stored agent
3428
+ */
3429
+ createStoredAgent(params) {
3430
+ return this.request("/api/stored/agents", {
3431
+ method: "POST",
3432
+ body: params
3433
+ });
3434
+ }
3435
+ /**
3436
+ * Gets a stored agent instance by ID for further operations (details, update, delete)
3437
+ * @param storedAgentId - ID of the stored agent to retrieve
3438
+ * @returns StoredAgent instance
3439
+ */
3440
+ getStoredAgent(storedAgentId) {
3441
+ return new StoredAgent(this.options, storedAgentId);
3442
+ }
3160
3443
  };
3161
3444
 
3162
3445
  // src/tools.ts