@mastra/client-js 0.0.0-vnextWorkflows-20250422142014 → 0.0.0-workflow-deno-20250616130925
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/.turbo/turbo-build.log +19 -0
- package/CHANGELOG.md +662 -2
- package/dist/index.cjs +889 -55
- package/dist/index.d.cts +408 -41
- package/dist/index.d.ts +408 -41
- package/dist/index.js +885 -55
- package/package.json +23 -13
- package/src/adapters/agui.test.ts +180 -0
- package/src/adapters/agui.ts +239 -0
- package/src/client.ts +200 -13
- package/src/example.ts +30 -31
- package/src/index.test.ts +125 -5
- package/src/resources/a2a.ts +88 -0
- package/src/resources/agent.ts +49 -37
- package/src/resources/base.ts +2 -2
- package/src/resources/index.ts +4 -1
- package/src/resources/legacy-workflow.ts +242 -0
- package/src/resources/mcp-tool.ts +48 -0
- package/src/resources/memory-thread.ts +8 -5
- package/src/resources/network.ts +6 -12
- package/src/resources/tool.ts +16 -3
- package/src/resources/workflow.ts +254 -96
- package/src/types.ts +134 -19
- package/src/utils/index.ts +11 -0
- package/src/utils/process-client-tools.ts +31 -0
- package/src/utils/zod-to-json-schema.ts +10 -0
|
@@ -1,5 +1,16 @@
|
|
|
1
|
-
import type {
|
|
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,57 @@ export class Workflow extends BaseResource {
|
|
|
21
103
|
}
|
|
22
104
|
|
|
23
105
|
/**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* @
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
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) {
|
|
119
|
+
searchParams.set('limit', String(params.limit));
|
|
120
|
+
}
|
|
121
|
+
if (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`);
|
|
34
151
|
}
|
|
35
152
|
|
|
36
153
|
/**
|
|
37
154
|
* Creates a new workflow run
|
|
38
|
-
* @
|
|
155
|
+
* @param params - Optional object containing the optional runId
|
|
156
|
+
* @returns Promise containing the runId of the created run
|
|
39
157
|
*/
|
|
40
158
|
createRun(params?: { runId?: string }): Promise<{ runId: string }> {
|
|
41
159
|
const searchParams = new URLSearchParams();
|
|
@@ -44,150 +162,162 @@ export class Workflow extends BaseResource {
|
|
|
44
162
|
searchParams.set('runId', params.runId);
|
|
45
163
|
}
|
|
46
164
|
|
|
47
|
-
return this.request(`/api/workflows/${this.workflowId}/
|
|
165
|
+
return this.request(`/api/workflows/${this.workflowId}/create-run?${searchParams.toString()}`, {
|
|
48
166
|
method: 'POST',
|
|
49
167
|
});
|
|
50
168
|
}
|
|
51
169
|
|
|
52
170
|
/**
|
|
53
171
|
* Starts a workflow run synchronously without waiting for the workflow to complete
|
|
54
|
-
* @param params - Object containing the runId and
|
|
172
|
+
* @param params - Object containing the runId, inputData and runtimeContext
|
|
55
173
|
* @returns Promise containing success message
|
|
56
174
|
*/
|
|
57
|
-
start(params: {
|
|
175
|
+
start(params: {
|
|
176
|
+
runId: string;
|
|
177
|
+
inputData: Record<string, any>;
|
|
178
|
+
runtimeContext?: RuntimeContext | Record<string, any>;
|
|
179
|
+
}): Promise<{ message: string }> {
|
|
180
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
58
181
|
return this.request(`/api/workflows/${this.workflowId}/start?runId=${params.runId}`, {
|
|
59
182
|
method: 'POST',
|
|
60
|
-
body: params?.
|
|
183
|
+
body: { inputData: params?.inputData, runtimeContext },
|
|
61
184
|
});
|
|
62
185
|
}
|
|
63
186
|
|
|
64
187
|
/**
|
|
65
188
|
* Resumes a suspended workflow step synchronously without waiting for the workflow to complete
|
|
66
|
-
* @param
|
|
67
|
-
* @
|
|
68
|
-
* @param context - Context to resume the workflow with
|
|
69
|
-
* @returns Promise containing the workflow resume results
|
|
189
|
+
* @param params - Object containing the runId, step, resumeData and runtimeContext
|
|
190
|
+
* @returns Promise containing success message
|
|
70
191
|
*/
|
|
71
192
|
resume({
|
|
72
|
-
|
|
193
|
+
step,
|
|
73
194
|
runId,
|
|
74
|
-
|
|
195
|
+
resumeData,
|
|
196
|
+
...rest
|
|
75
197
|
}: {
|
|
76
|
-
|
|
198
|
+
step: string | string[];
|
|
77
199
|
runId: string;
|
|
78
|
-
|
|
200
|
+
resumeData?: Record<string, any>;
|
|
201
|
+
runtimeContext?: RuntimeContext | Record<string, any>;
|
|
79
202
|
}): Promise<{ message: string }> {
|
|
203
|
+
const runtimeContext = parseClientRuntimeContext(rest.runtimeContext);
|
|
80
204
|
return this.request(`/api/workflows/${this.workflowId}/resume?runId=${runId}`, {
|
|
81
205
|
method: 'POST',
|
|
206
|
+
stream: true,
|
|
82
207
|
body: {
|
|
83
|
-
|
|
84
|
-
|
|
208
|
+
step,
|
|
209
|
+
resumeData,
|
|
210
|
+
runtimeContext,
|
|
85
211
|
},
|
|
86
212
|
});
|
|
87
213
|
}
|
|
88
214
|
|
|
89
215
|
/**
|
|
90
216
|
* 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
|
|
217
|
+
* @param params - Object containing the optional runId, inputData and runtimeContext
|
|
92
218
|
* @returns Promise containing the workflow execution results
|
|
93
219
|
*/
|
|
94
|
-
startAsync(params: {
|
|
220
|
+
startAsync(params: {
|
|
221
|
+
runId?: string;
|
|
222
|
+
inputData: Record<string, any>;
|
|
223
|
+
runtimeContext?: RuntimeContext | Record<string, any>;
|
|
224
|
+
}): Promise<WorkflowRunResult> {
|
|
95
225
|
const searchParams = new URLSearchParams();
|
|
96
226
|
|
|
97
227
|
if (!!params?.runId) {
|
|
98
228
|
searchParams.set('runId', params.runId);
|
|
99
229
|
}
|
|
100
230
|
|
|
231
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
232
|
+
|
|
101
233
|
return this.request(`/api/workflows/${this.workflowId}/start-async?${searchParams.toString()}`, {
|
|
102
234
|
method: 'POST',
|
|
103
|
-
body: params
|
|
235
|
+
body: { inputData: params.inputData, runtimeContext },
|
|
104
236
|
});
|
|
105
237
|
}
|
|
106
238
|
|
|
107
239
|
/**
|
|
108
|
-
*
|
|
109
|
-
* @param params - Object containing the runId,
|
|
110
|
-
* @returns Promise containing the workflow
|
|
240
|
+
* Starts a vNext workflow run and returns a stream
|
|
241
|
+
* @param params - Object containing the optional runId, inputData and runtimeContext
|
|
242
|
+
* @returns Promise containing the vNext workflow execution results
|
|
111
243
|
*/
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
method: 'POST',
|
|
115
|
-
body: {
|
|
116
|
-
stepId: params.stepId,
|
|
117
|
-
context: params.context,
|
|
118
|
-
},
|
|
119
|
-
});
|
|
120
|
-
}
|
|
244
|
+
async stream(params: { runId?: string; inputData: Record<string, any>; runtimeContext?: RuntimeContext }) {
|
|
245
|
+
const searchParams = new URLSearchParams();
|
|
121
246
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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();
|
|
247
|
+
if (!!params?.runId) {
|
|
248
|
+
searchParams.set('runId', params.runId);
|
|
249
|
+
}
|
|
131
250
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
251
|
+
const runtimeContext = params.runtimeContext ? Object.fromEntries(params.runtimeContext.entries()) : undefined;
|
|
252
|
+
const response: Response = await this.request(
|
|
253
|
+
`/api/workflows/${this.workflowId}/stream?${searchParams.toString()}`,
|
|
254
|
+
{
|
|
255
|
+
method: 'POST',
|
|
256
|
+
body: { inputData: params.inputData, runtimeContext },
|
|
257
|
+
stream: true,
|
|
258
|
+
},
|
|
259
|
+
);
|
|
136
260
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
const { done, value } = await reader.read();
|
|
141
|
-
doneReading = done;
|
|
261
|
+
if (!response.ok) {
|
|
262
|
+
throw new Error(`Failed to stream vNext workflow: ${response.statusText}`);
|
|
263
|
+
}
|
|
142
264
|
|
|
143
|
-
|
|
144
|
-
|
|
265
|
+
if (!response.body) {
|
|
266
|
+
throw new Error('Response body is null');
|
|
267
|
+
}
|
|
145
268
|
|
|
269
|
+
// Create a transform stream that processes the response body
|
|
270
|
+
const transformStream = new TransformStream<ArrayBuffer, WorkflowWatchResult>({
|
|
271
|
+
start() {},
|
|
272
|
+
async transform(chunk, controller) {
|
|
146
273
|
try {
|
|
147
274
|
// Decode binary data to text
|
|
148
|
-
const decoded =
|
|
275
|
+
const decoded = new TextDecoder().decode(chunk);
|
|
149
276
|
|
|
150
|
-
// Split
|
|
151
|
-
const chunks =
|
|
152
|
-
|
|
153
|
-
// The last chunk might be incomplete, so save it for the next iteration
|
|
154
|
-
buffer = chunks.pop() || '';
|
|
277
|
+
// Split by record separator
|
|
278
|
+
const chunks = decoded.split(RECORD_SEPARATOR);
|
|
155
279
|
|
|
156
|
-
// Process
|
|
280
|
+
// Process each chunk
|
|
157
281
|
for (const chunk of chunks) {
|
|
158
282
|
if (chunk) {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
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
|
-
}
|
|
283
|
+
try {
|
|
284
|
+
const parsedChunk = JSON.parse(chunk);
|
|
285
|
+
controller.enqueue(parsedChunk);
|
|
286
|
+
} catch {
|
|
287
|
+
// Silently ignore parsing errors
|
|
168
288
|
}
|
|
169
289
|
}
|
|
170
290
|
}
|
|
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
291
|
} catch {
|
|
182
|
-
//
|
|
292
|
+
// Silently ignore processing errors
|
|
183
293
|
}
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
294
|
+
},
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
// Pipe the response body through the transform stream
|
|
298
|
+
return response.body.pipeThrough(transformStream);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Resumes a suspended workflow step asynchronously and returns a promise that resolves when the workflow is complete
|
|
303
|
+
* @param params - Object containing the runId, step, resumeData and runtimeContext
|
|
304
|
+
* @returns Promise containing the workflow resume results
|
|
305
|
+
*/
|
|
306
|
+
resumeAsync(params: {
|
|
307
|
+
runId: string;
|
|
308
|
+
step: string | string[];
|
|
309
|
+
resumeData?: Record<string, any>;
|
|
310
|
+
runtimeContext?: RuntimeContext | Record<string, any>;
|
|
311
|
+
}): Promise<WorkflowRunResult> {
|
|
312
|
+
const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
|
|
313
|
+
return this.request(`/api/workflows/${this.workflowId}/resume-async?runId=${params.runId}`, {
|
|
314
|
+
method: 'POST',
|
|
315
|
+
body: {
|
|
316
|
+
step: params.step,
|
|
317
|
+
resumeData: params.resumeData,
|
|
318
|
+
runtimeContext,
|
|
319
|
+
},
|
|
320
|
+
});
|
|
191
321
|
}
|
|
192
322
|
|
|
193
323
|
/**
|
|
@@ -195,7 +325,7 @@ export class Workflow extends BaseResource {
|
|
|
195
325
|
* @param runId - Optional run ID to filter the watch stream
|
|
196
326
|
* @returns AsyncGenerator that yields parsed records from the workflow watch stream
|
|
197
327
|
*/
|
|
198
|
-
async watch({ runId }: { runId?: string }, onRecord: (record:
|
|
328
|
+
async watch({ runId }: { runId?: string }, onRecord: (record: WorkflowWatchResult) => void) {
|
|
199
329
|
const response: Response = await this.request(`/api/workflows/${this.workflowId}/watch?runId=${runId}`, {
|
|
200
330
|
stream: true,
|
|
201
331
|
});
|
|
@@ -209,7 +339,35 @@ export class Workflow extends BaseResource {
|
|
|
209
339
|
}
|
|
210
340
|
|
|
211
341
|
for await (const record of this.streamProcessor(response.body)) {
|
|
212
|
-
|
|
342
|
+
if (typeof record === 'string') {
|
|
343
|
+
onRecord(JSON.parse(record));
|
|
344
|
+
} else {
|
|
345
|
+
onRecord(record);
|
|
346
|
+
}
|
|
213
347
|
}
|
|
214
348
|
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Creates a new ReadableStream from an iterable or async iterable of objects,
|
|
352
|
+
* serializing each as JSON and separating them with the record separator (\x1E).
|
|
353
|
+
*
|
|
354
|
+
* @param records - An iterable or async iterable of objects to stream
|
|
355
|
+
* @returns A ReadableStream emitting the records as JSON strings separated by the record separator
|
|
356
|
+
*/
|
|
357
|
+
static createRecordStream(records: Iterable<any> | AsyncIterable<any>): ReadableStream {
|
|
358
|
+
const encoder = new TextEncoder();
|
|
359
|
+
return new ReadableStream({
|
|
360
|
+
async start(controller) {
|
|
361
|
+
try {
|
|
362
|
+
for await (const record of records as AsyncIterable<any>) {
|
|
363
|
+
const json = JSON.stringify(record) + RECORD_SEPARATOR;
|
|
364
|
+
controller.enqueue(encoder.encode(json));
|
|
365
|
+
}
|
|
366
|
+
controller.close();
|
|
367
|
+
} catch (err) {
|
|
368
|
+
controller.error(err);
|
|
369
|
+
}
|
|
370
|
+
},
|
|
371
|
+
});
|
|
372
|
+
}
|
|
215
373
|
}
|