@mastra/client-js 0.0.0-vector-sources-20250516175436 → 0.0.0-vector-query-tool-provider-options-20250828222356

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