@mastra/client-js 0.0.0-revert-schema-20250416221206 → 0.0.0-scorers-api-v2-20250801171841

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.
Files changed (83) hide show
  1. package/CHANGELOG.md +1358 -2
  2. package/LICENSE.md +11 -42
  3. package/README.md +2 -1
  4. package/dist/adapters/agui.d.ts +23 -0
  5. package/dist/adapters/agui.d.ts.map +1 -0
  6. package/dist/client.d.ts +265 -0
  7. package/dist/client.d.ts.map +1 -0
  8. package/dist/example.d.ts +2 -0
  9. package/dist/example.d.ts.map +1 -0
  10. package/dist/index.cjs +1775 -86
  11. package/dist/index.cjs.map +1 -0
  12. package/dist/index.d.ts +4 -585
  13. package/dist/index.d.ts.map +1 -0
  14. package/dist/index.js +1775 -90
  15. package/dist/index.js.map +1 -0
  16. package/dist/resources/a2a.d.ts +44 -0
  17. package/dist/resources/a2a.d.ts.map +1 -0
  18. package/dist/resources/agent.d.ts +112 -0
  19. package/dist/resources/agent.d.ts.map +1 -0
  20. package/dist/resources/base.d.ts +13 -0
  21. package/dist/resources/base.d.ts.map +1 -0
  22. package/dist/resources/index.d.ts +11 -0
  23. package/dist/resources/index.d.ts.map +1 -0
  24. package/dist/resources/legacy-workflow.d.ts +87 -0
  25. package/dist/resources/legacy-workflow.d.ts.map +1 -0
  26. package/dist/resources/mcp-tool.d.ts +27 -0
  27. package/dist/resources/mcp-tool.d.ts.map +1 -0
  28. package/dist/resources/memory-thread.d.ts +53 -0
  29. package/dist/resources/memory-thread.d.ts.map +1 -0
  30. package/dist/resources/network-memory-thread.d.ts +47 -0
  31. package/dist/resources/network-memory-thread.d.ts.map +1 -0
  32. package/dist/resources/network.d.ts +30 -0
  33. package/dist/resources/network.d.ts.map +1 -0
  34. package/dist/resources/tool.d.ts +23 -0
  35. package/dist/resources/tool.d.ts.map +1 -0
  36. package/dist/resources/vNextNetwork.d.ts +42 -0
  37. package/dist/resources/vNextNetwork.d.ts.map +1 -0
  38. package/dist/resources/vector.d.ts +48 -0
  39. package/dist/resources/vector.d.ts.map +1 -0
  40. package/dist/resources/workflow.d.ts +154 -0
  41. package/dist/resources/workflow.d.ts.map +1 -0
  42. package/dist/types.d.ts +422 -0
  43. package/dist/types.d.ts.map +1 -0
  44. package/dist/utils/index.d.ts +3 -0
  45. package/dist/utils/index.d.ts.map +1 -0
  46. package/dist/utils/process-client-tools.d.ts +3 -0
  47. package/dist/utils/process-client-tools.d.ts.map +1 -0
  48. package/dist/utils/zod-to-json-schema.d.ts +105 -0
  49. package/dist/utils/zod-to-json-schema.d.ts.map +1 -0
  50. package/integration-tests/agui-adapter.test.ts +122 -0
  51. package/integration-tests/package.json +18 -0
  52. package/integration-tests/src/mastra/index.ts +35 -0
  53. package/integration-tests/vitest.config.ts +9 -0
  54. package/package.json +31 -17
  55. package/src/adapters/agui.test.ts +322 -0
  56. package/src/adapters/agui.ts +239 -0
  57. package/src/client.ts +414 -14
  58. package/src/example.ts +59 -29
  59. package/src/index.test.ts +526 -10
  60. package/src/index.ts +1 -0
  61. package/src/resources/a2a.ts +88 -0
  62. package/src/resources/agent.ts +629 -49
  63. package/src/resources/base.ts +8 -2
  64. package/src/resources/index.ts +4 -1
  65. package/src/resources/legacy-workflow.ts +242 -0
  66. package/src/resources/mcp-tool.ts +48 -0
  67. package/src/resources/memory-thread.test.ts +285 -0
  68. package/src/resources/memory-thread.ts +44 -5
  69. package/src/resources/network-memory-thread.test.ts +269 -0
  70. package/src/resources/network-memory-thread.ts +81 -0
  71. package/src/resources/network.ts +11 -17
  72. package/src/resources/tool.ts +16 -3
  73. package/src/resources/vNextNetwork.ts +194 -0
  74. package/src/resources/workflow.ts +289 -94
  75. package/src/types.ts +300 -22
  76. package/src/utils/index.ts +11 -0
  77. package/src/utils/process-client-tools.ts +32 -0
  78. package/src/utils/zod-to-json-schema.ts +10 -0
  79. package/src/v2-messages.test.ts +180 -0
  80. package/tsconfig.build.json +9 -0
  81. package/tsconfig.json +1 -1
  82. package/tsup.config.ts +22 -0
  83. package/dist/index.d.cts +0 -585
@@ -1,5 +1,16 @@
1
- import type { GetWorkflowResponse, ClientOptions, WorkflowRunResult } from '../types';
1
+ import type { RuntimeContext } from '@mastra/core/runtime-context';
2
+ import type {
3
+ ClientOptions,
4
+ GetWorkflowResponse,
5
+ GetWorkflowRunsResponse,
6
+ GetWorkflowRunsParams,
7
+ WorkflowRunResult,
8
+ WorkflowWatchResult,
9
+ GetWorkflowRunByIdResponse,
10
+ GetWorkflowRunExecutionResultResponse,
11
+ } from '../types';
2
12
 
13
+ import { parseClientRuntimeContext } from '../utils';
3
14
  import { BaseResource } from './base';
4
15
 
5
16
  const RECORD_SEPARATOR = '\x1E';
@@ -12,6 +23,77 @@ export class Workflow extends BaseResource {
12
23
  super(options);
13
24
  }
14
25
 
26
+ /**
27
+ * Creates an async generator that processes a readable stream and yields workflow records
28
+ * separated by the Record Separator character (\x1E)
29
+ *
30
+ * @param stream - The readable stream to process
31
+ * @returns An async generator that yields parsed records
32
+ */
33
+ private async *streamProcessor(stream: ReadableStream): AsyncGenerator<WorkflowWatchResult, void, unknown> {
34
+ const reader = stream.getReader();
35
+
36
+ // Track if we've finished reading from the stream
37
+ let doneReading = false;
38
+ // Buffer to accumulate partial chunks
39
+ let buffer = '';
40
+
41
+ try {
42
+ while (!doneReading) {
43
+ // Read the next chunk from the stream
44
+ const { done, value } = await reader.read();
45
+ doneReading = done;
46
+
47
+ // Skip processing if we're done and there's no value
48
+ if (done && !value) continue;
49
+
50
+ try {
51
+ // Decode binary data to text
52
+ const decoded = value ? new TextDecoder().decode(value) : '';
53
+
54
+ // Split the combined buffer and new data by record separator
55
+ const chunks = (buffer + decoded).split(RECORD_SEPARATOR);
56
+
57
+ // The last chunk might be incomplete, so save it for the next iteration
58
+ buffer = chunks.pop() || '';
59
+
60
+ // Process complete chunks
61
+ for (const chunk of chunks) {
62
+ if (chunk) {
63
+ // Only process non-empty chunks
64
+ if (typeof chunk === 'string') {
65
+ try {
66
+ const parsedChunk = JSON.parse(chunk);
67
+ yield parsedChunk;
68
+ } catch {
69
+ // Silently ignore parsing errors to maintain stream processing
70
+ // This allows the stream to continue even if one record is malformed
71
+ }
72
+ }
73
+ }
74
+ }
75
+ } catch {
76
+ // Silently ignore parsing errors to maintain stream processing
77
+ // This allows the stream to continue even if one record is malformed
78
+ }
79
+ }
80
+
81
+ // Process any remaining data in the buffer after stream is done
82
+ if (buffer) {
83
+ try {
84
+ yield JSON.parse(buffer);
85
+ } catch {
86
+ // Ignore parsing error for final chunk
87
+ }
88
+ }
89
+ } finally {
90
+ // Always ensure we clean up the reader
91
+ reader.cancel().catch(() => {
92
+ // Ignore cancel errors
93
+ });
94
+ }
95
+ }
96
+
15
97
  /**
16
98
  * Retrieves details about the workflow
17
99
  * @returns Promise containing workflow details including steps and graphs
@@ -21,21 +103,80 @@ export class Workflow extends BaseResource {
21
103
  }
22
104
 
23
105
  /**
24
- * @deprecated Use `startAsync` instead
25
- * Executes the workflow with the provided parameters
26
- * @param params - Parameters required for workflow execution
27
- * @returns Promise containing the workflow execution results
106
+ * Retrieves all runs for a workflow
107
+ * @param params - Parameters for filtering runs
108
+ * @returns Promise containing workflow runs array
28
109
  */
29
- execute(params: Record<string, any>): Promise<WorkflowRunResult> {
30
- return this.request(`/api/workflows/${this.workflowId}/execute`, {
110
+ runs(params?: GetWorkflowRunsParams): Promise<GetWorkflowRunsResponse> {
111
+ const searchParams = new URLSearchParams();
112
+ if (params?.fromDate) {
113
+ searchParams.set('fromDate', params.fromDate.toISOString());
114
+ }
115
+ if (params?.toDate) {
116
+ searchParams.set('toDate', params.toDate.toISOString());
117
+ }
118
+ if (params?.limit !== null && params?.limit !== undefined && !isNaN(Number(params?.limit))) {
119
+ searchParams.set('limit', String(params.limit));
120
+ }
121
+ if (params?.offset !== null && params?.offset !== undefined && !isNaN(Number(params?.offset))) {
122
+ searchParams.set('offset', String(params.offset));
123
+ }
124
+ if (params?.resourceId) {
125
+ searchParams.set('resourceId', params.resourceId);
126
+ }
127
+
128
+ if (searchParams.size) {
129
+ return this.request(`/api/workflows/${this.workflowId}/runs?${searchParams}`);
130
+ } else {
131
+ return this.request(`/api/workflows/${this.workflowId}/runs`);
132
+ }
133
+ }
134
+
135
+ /**
136
+ * Retrieves a specific workflow run by its ID
137
+ * @param runId - The ID of the workflow run to retrieve
138
+ * @returns Promise containing the workflow run details
139
+ */
140
+ runById(runId: string): Promise<GetWorkflowRunByIdResponse> {
141
+ return this.request(`/api/workflows/${this.workflowId}/runs/${runId}`);
142
+ }
143
+
144
+ /**
145
+ * Retrieves the execution result for a specific workflow run by its ID
146
+ * @param runId - The ID of the workflow run to retrieve the execution result for
147
+ * @returns Promise containing the workflow run execution result
148
+ */
149
+ runExecutionResult(runId: string): Promise<GetWorkflowRunExecutionResultResponse> {
150
+ return this.request(`/api/workflows/${this.workflowId}/runs/${runId}/execution-result`);
151
+ }
152
+
153
+ /**
154
+ * Cancels a specific workflow run by its ID
155
+ * @param runId - The ID of the workflow run to cancel
156
+ * @returns Promise containing a success message
157
+ */
158
+ cancelRun(runId: string): Promise<{ message: string }> {
159
+ return this.request(`/api/workflows/${this.workflowId}/runs/${runId}/cancel`, {
31
160
  method: 'POST',
32
- body: params,
161
+ });
162
+ }
163
+
164
+ /**
165
+ * Sends an event to a specific workflow run by its ID
166
+ * @param params - Object containing the runId, event and data
167
+ * @returns Promise containing a success message
168
+ */
169
+ sendRunEvent(params: { runId: string; event: string; data: unknown }): Promise<{ message: string }> {
170
+ return this.request(`/api/workflows/${this.workflowId}/runs/${params.runId}/send-event`, {
171
+ method: 'POST',
172
+ body: { event: params.event, data: params.data },
33
173
  });
34
174
  }
35
175
 
36
176
  /**
37
177
  * Creates a new workflow run
38
- * @returns Promise containing the generated run ID
178
+ * @param params - Optional object containing the optional runId
179
+ * @returns Promise containing the runId of the created run
39
180
  */
40
181
  createRun(params?: { runId?: string }): Promise<{ runId: string }> {
41
182
  const searchParams = new URLSearchParams();
@@ -44,150 +185,176 @@ export class Workflow extends BaseResource {
44
185
  searchParams.set('runId', params.runId);
45
186
  }
46
187
 
47
- return this.request(`/api/workflows/${this.workflowId}/createRun?${searchParams.toString()}`, {
188
+ return this.request(`/api/workflows/${this.workflowId}/create-run?${searchParams.toString()}`, {
48
189
  method: 'POST',
49
190
  });
50
191
  }
51
192
 
193
+ /**
194
+ * Creates a new workflow run (alias for createRun)
195
+ * @param params - Optional object containing the optional runId
196
+ * @returns Promise containing the runId of the created run
197
+ */
198
+ createRunAsync(params?: { runId?: string }): Promise<{ runId: string }> {
199
+ return this.createRun(params);
200
+ }
201
+
52
202
  /**
53
203
  * Starts a workflow run synchronously without waiting for the workflow to complete
54
- * @param params - Object containing the runId and triggerData
204
+ * @param params - Object containing the runId, inputData and runtimeContext
55
205
  * @returns Promise containing success message
56
206
  */
57
- start(params: { runId: string; triggerData: Record<string, any> }): Promise<{ message: string }> {
207
+ start(params: {
208
+ runId: string;
209
+ inputData: Record<string, any>;
210
+ runtimeContext?: RuntimeContext | Record<string, any>;
211
+ }): Promise<{ message: string }> {
212
+ const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
58
213
  return this.request(`/api/workflows/${this.workflowId}/start?runId=${params.runId}`, {
59
214
  method: 'POST',
60
- body: params?.triggerData,
215
+ body: { inputData: params?.inputData, runtimeContext },
61
216
  });
62
217
  }
63
218
 
64
219
  /**
65
220
  * Resumes a suspended workflow step synchronously without waiting for the workflow to complete
66
- * @param stepId - ID of the step to resume
67
- * @param runId - ID of the workflow run
68
- * @param context - Context to resume the workflow with
69
- * @returns Promise containing the workflow resume results
221
+ * @param params - Object containing the runId, step, resumeData and runtimeContext
222
+ * @returns Promise containing success message
70
223
  */
71
224
  resume({
72
- stepId,
225
+ step,
73
226
  runId,
74
- context,
227
+ resumeData,
228
+ ...rest
75
229
  }: {
76
- stepId: string;
230
+ step: string | string[];
77
231
  runId: string;
78
- context: Record<string, any>;
232
+ resumeData?: Record<string, any>;
233
+ runtimeContext?: RuntimeContext | Record<string, any>;
79
234
  }): Promise<{ message: string }> {
235
+ const runtimeContext = parseClientRuntimeContext(rest.runtimeContext);
80
236
  return this.request(`/api/workflows/${this.workflowId}/resume?runId=${runId}`, {
81
237
  method: 'POST',
238
+ stream: true,
82
239
  body: {
83
- stepId,
84
- context,
240
+ step,
241
+ resumeData,
242
+ runtimeContext,
85
243
  },
86
244
  });
87
245
  }
88
246
 
89
247
  /**
90
248
  * Starts a workflow run asynchronously and returns a promise that resolves when the workflow is complete
91
- * @param params - Object containing the optional runId and triggerData
249
+ * @param params - Object containing the optional runId, inputData and runtimeContext
92
250
  * @returns Promise containing the workflow execution results
93
251
  */
94
- startAsync(params: { runId?: string; triggerData: Record<string, any> }): Promise<WorkflowRunResult> {
252
+ startAsync(params: {
253
+ runId?: string;
254
+ inputData: Record<string, any>;
255
+ runtimeContext?: RuntimeContext | Record<string, any>;
256
+ }): Promise<WorkflowRunResult> {
95
257
  const searchParams = new URLSearchParams();
96
258
 
97
259
  if (!!params?.runId) {
98
260
  searchParams.set('runId', params.runId);
99
261
  }
100
262
 
263
+ const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
264
+
101
265
  return this.request(`/api/workflows/${this.workflowId}/start-async?${searchParams.toString()}`, {
102
266
  method: 'POST',
103
- body: params?.triggerData,
267
+ body: { inputData: params.inputData, runtimeContext },
104
268
  });
105
269
  }
106
270
 
107
271
  /**
108
- * Resumes a suspended workflow step asynchronously and returns a promise that resolves when the workflow is complete
109
- * @param params - Object containing the runId, stepId, and context
110
- * @returns Promise containing the workflow resume results
272
+ * Starts a workflow run and returns a stream
273
+ * @param params - Object containing the optional runId, inputData and runtimeContext
274
+ * @returns Promise containing the workflow execution results
111
275
  */
112
- resumeAsync(params: { runId: string; stepId: string; context: Record<string, any> }): Promise<WorkflowRunResult> {
113
- return this.request(`/api/workflows/${this.workflowId}/resume-async?runId=${params.runId}`, {
114
- method: 'POST',
115
- body: {
116
- stepId: params.stepId,
117
- context: params.context,
118
- },
119
- });
120
- }
276
+ async stream(params: { runId?: string; inputData: Record<string, any>; runtimeContext?: RuntimeContext }) {
277
+ const searchParams = new URLSearchParams();
121
278
 
122
- /**
123
- * Creates an async generator that processes a readable stream and yields records
124
- * separated by the Record Separator character (\x1E)
125
- *
126
- * @param stream - The readable stream to process
127
- * @returns An async generator that yields parsed records
128
- */
129
- private async *streamProcessor(stream: ReadableStream): AsyncGenerator<WorkflowRunResult, void, unknown> {
130
- const reader = stream.getReader();
279
+ if (!!params?.runId) {
280
+ searchParams.set('runId', params.runId);
281
+ }
131
282
 
132
- // Track if we've finished reading from the stream
133
- let doneReading = false;
134
- // Buffer to accumulate partial chunks
135
- let buffer = '';
283
+ const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
284
+ const response: Response = await this.request(
285
+ `/api/workflows/${this.workflowId}/stream?${searchParams.toString()}`,
286
+ {
287
+ method: 'POST',
288
+ body: { inputData: params.inputData, runtimeContext },
289
+ stream: true,
290
+ },
291
+ );
136
292
 
137
- try {
138
- while (!doneReading) {
139
- // Read the next chunk from the stream
140
- const { done, value } = await reader.read();
141
- doneReading = done;
293
+ if (!response.ok) {
294
+ throw new Error(`Failed to stream vNext workflow: ${response.statusText}`);
295
+ }
142
296
 
143
- // Skip processing if we're done and there's no value
144
- if (done && !value) continue;
297
+ if (!response.body) {
298
+ throw new Error('Response body is null');
299
+ }
300
+
301
+ //using undefined instead of empty string to avoid parsing errors
302
+ let failedChunk: string | undefined = undefined;
145
303
 
304
+ // Create a transform stream that processes the response body
305
+ const transformStream = new TransformStream<ArrayBuffer, { type: string; payload: any }>({
306
+ start() {},
307
+ async transform(chunk, controller) {
146
308
  try {
147
309
  // Decode binary data to text
148
- const decoded = value ? new TextDecoder().decode(value) : '';
149
-
150
- // Split the combined buffer and new data by record separator
151
- const chunks = (buffer + decoded).split(RECORD_SEPARATOR);
310
+ const decoded = new TextDecoder().decode(chunk);
152
311
 
153
- // The last chunk might be incomplete, so save it for the next iteration
154
- buffer = chunks.pop() || '';
312
+ // Split by record separator
313
+ const chunks = decoded.split(RECORD_SEPARATOR);
155
314
 
156
- // Process complete chunks
315
+ // Process each chunk
157
316
  for (const chunk of chunks) {
158
317
  if (chunk) {
159
- // Only process non-empty chunks
160
- if (typeof chunk === 'string') {
161
- try {
162
- const parsedChunk = JSON.parse(chunk);
163
- yield parsedChunk;
164
- } catch {
165
- // Silently ignore parsing errors to maintain stream processing
166
- // This allows the stream to continue even if one record is malformed
167
- }
318
+ const newChunk: string = failedChunk ? failedChunk + chunk : chunk;
319
+ try {
320
+ const parsedChunk = JSON.parse(newChunk);
321
+ controller.enqueue(parsedChunk);
322
+ failedChunk = undefined;
323
+ } catch (error) {
324
+ failedChunk = newChunk;
168
325
  }
169
326
  }
170
327
  }
171
- } catch (error) {
172
- // Silently ignore parsing errors to maintain stream processing
173
- // This allows the stream to continue even if one record is malformed
174
- }
175
- }
176
-
177
- // Process any remaining data in the buffer after stream is done
178
- if (buffer) {
179
- try {
180
- yield JSON.parse(buffer);
181
328
  } catch {
182
- // Ignore parsing error for final chunk
329
+ // Silently ignore processing errors
183
330
  }
184
- }
185
- } finally {
186
- // Always ensure we clean up the reader
187
- reader.cancel().catch(() => {
188
- // Ignore cancel errors
189
- });
190
- }
331
+ },
332
+ });
333
+
334
+ // Pipe the response body through the transform stream
335
+ return response.body.pipeThrough(transformStream);
336
+ }
337
+
338
+ /**
339
+ * Resumes a suspended workflow step asynchronously and returns a promise that resolves when the workflow is complete
340
+ * @param params - Object containing the runId, step, resumeData and runtimeContext
341
+ * @returns Promise containing the workflow resume results
342
+ */
343
+ resumeAsync(params: {
344
+ runId: string;
345
+ step: string | string[];
346
+ resumeData?: Record<string, any>;
347
+ runtimeContext?: RuntimeContext | Record<string, any>;
348
+ }): Promise<WorkflowRunResult> {
349
+ const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
350
+ return this.request(`/api/workflows/${this.workflowId}/resume-async?runId=${params.runId}`, {
351
+ method: 'POST',
352
+ body: {
353
+ step: params.step,
354
+ resumeData: params.resumeData,
355
+ runtimeContext,
356
+ },
357
+ });
191
358
  }
192
359
 
193
360
  /**
@@ -195,7 +362,7 @@ export class Workflow extends BaseResource {
195
362
  * @param runId - Optional run ID to filter the watch stream
196
363
  * @returns AsyncGenerator that yields parsed records from the workflow watch stream
197
364
  */
198
- async watch({ runId }: { runId?: string }, onRecord: (record: WorkflowRunResult) => void) {
365
+ async watch({ runId }: { runId?: string }, onRecord: (record: WorkflowWatchResult) => void) {
199
366
  const response: Response = await this.request(`/api/workflows/${this.workflowId}/watch?runId=${runId}`, {
200
367
  stream: true,
201
368
  });
@@ -209,7 +376,35 @@ export class Workflow extends BaseResource {
209
376
  }
210
377
 
211
378
  for await (const record of this.streamProcessor(response.body)) {
212
- onRecord(record);
379
+ if (typeof record === 'string') {
380
+ onRecord(JSON.parse(record));
381
+ } else {
382
+ onRecord(record);
383
+ }
213
384
  }
214
385
  }
386
+
387
+ /**
388
+ * Creates a new ReadableStream from an iterable or async iterable of objects,
389
+ * serializing each as JSON and separating them with the record separator (\x1E).
390
+ *
391
+ * @param records - An iterable or async iterable of objects to stream
392
+ * @returns A ReadableStream emitting the records as JSON strings separated by the record separator
393
+ */
394
+ static createRecordStream(records: Iterable<any> | AsyncIterable<any>): ReadableStream {
395
+ const encoder = new TextEncoder();
396
+ return new ReadableStream({
397
+ async start(controller) {
398
+ try {
399
+ for await (const record of records as AsyncIterable<any>) {
400
+ const json = JSON.stringify(record) + RECORD_SEPARATOR;
401
+ controller.enqueue(encoder.encode(json));
402
+ }
403
+ controller.close();
404
+ } catch (err) {
405
+ controller.error(err);
406
+ }
407
+ },
408
+ });
409
+ }
215
410
  }