@mastra/client-js 0.0.0-vector-query-sources-20250516172905 → 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.
- package/.turbo/turbo-build.log +18 -0
- package/CHANGELOG.md +1318 -2
- package/LICENSE.md +11 -42
- package/README.md +2 -1
- package/dist/adapters/agui.d.ts +23 -0
- package/dist/adapters/agui.d.ts.map +1 -0
- package/dist/client.d.ts +274 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/example.d.ts +2 -0
- package/dist/example.d.ts.map +1 -0
- package/dist/index.cjs +1801 -137
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +4 -883
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1803 -139
- package/dist/index.js.map +1 -0
- package/dist/resources/a2a.d.ts +41 -0
- package/dist/resources/a2a.d.ts.map +1 -0
- package/dist/resources/agent.d.ts +130 -0
- package/dist/resources/agent.d.ts.map +1 -0
- package/dist/resources/base.d.ts +13 -0
- package/dist/resources/base.d.ts.map +1 -0
- package/dist/resources/index.d.ts +12 -0
- package/dist/resources/index.d.ts.map +1 -0
- package/dist/resources/legacy-workflow.d.ts +87 -0
- package/dist/resources/legacy-workflow.d.ts.map +1 -0
- package/dist/resources/mcp-tool.d.ts +27 -0
- package/dist/resources/mcp-tool.d.ts.map +1 -0
- package/dist/resources/memory-thread.d.ts +53 -0
- package/dist/resources/memory-thread.d.ts.map +1 -0
- package/dist/resources/network-memory-thread.d.ts +47 -0
- package/dist/resources/network-memory-thread.d.ts.map +1 -0
- package/dist/resources/network.d.ts +30 -0
- package/dist/resources/network.d.ts.map +1 -0
- package/dist/resources/observability.d.ts +19 -0
- package/dist/resources/observability.d.ts.map +1 -0
- package/dist/resources/tool.d.ts +23 -0
- package/dist/resources/tool.d.ts.map +1 -0
- package/dist/resources/vNextNetwork.d.ts +42 -0
- package/dist/resources/vNextNetwork.d.ts.map +1 -0
- package/dist/resources/vector.d.ts +48 -0
- package/dist/resources/vector.d.ts.map +1 -0
- package/dist/resources/workflow.d.ts +154 -0
- package/dist/resources/workflow.d.ts.map +1 -0
- package/dist/types.d.ts +449 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/process-client-tools.d.ts +3 -0
- package/dist/utils/process-client-tools.d.ts.map +1 -0
- package/dist/utils/process-mastra-stream.d.ts +7 -0
- package/dist/utils/process-mastra-stream.d.ts.map +1 -0
- package/dist/utils/zod-to-json-schema.d.ts +3 -0
- package/dist/utils/zod-to-json-schema.d.ts.map +1 -0
- package/eslint.config.js +6 -1
- package/integration-tests/agui-adapter.test.ts +122 -0
- package/integration-tests/package.json +18 -0
- package/integration-tests/src/mastra/index.ts +35 -0
- package/integration-tests/vitest.config.ts +9 -0
- package/package.json +32 -19
- package/src/adapters/agui.test.ts +116 -3
- package/src/adapters/agui.ts +30 -12
- package/src/client.ts +333 -24
- package/src/example.ts +46 -15
- package/src/index.test.ts +429 -6
- package/src/index.ts +1 -0
- package/src/resources/a2a.ts +35 -25
- package/src/resources/agent.ts +1284 -20
- package/src/resources/base.ts +8 -1
- package/src/resources/index.ts +3 -2
- package/src/resources/{vnext-workflow.ts → legacy-workflow.ts} +124 -143
- package/src/resources/memory-thread.test.ts +285 -0
- package/src/resources/memory-thread.ts +37 -1
- package/src/resources/network-memory-thread.test.ts +269 -0
- package/src/resources/network-memory-thread.ts +81 -0
- package/src/resources/network.ts +7 -7
- package/src/resources/observability.ts +53 -0
- package/src/resources/tool.ts +4 -3
- package/src/resources/vNextNetwork.ts +194 -0
- package/src/resources/workflow.ts +255 -96
- package/src/types.ts +262 -36
- package/src/utils/index.ts +11 -0
- package/src/utils/process-client-tools.ts +32 -0
- package/src/utils/process-mastra-stream.test.ts +353 -0
- package/src/utils/process-mastra-stream.ts +49 -0
- package/src/utils/zod-to-json-schema.ts +23 -3
- package/src/v2-messages.test.ts +180 -0
- package/tsconfig.build.json +9 -0
- package/tsconfig.json +2 -2
- package/tsup.config.ts +17 -0
- 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
|
-
|
|
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
|
-
*
|
|
61
|
-
*
|
|
62
|
-
* @
|
|
63
|
-
|
|
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
|
-
|
|
66
|
-
return this.request(`/api/workflows/${this.workflowId}/
|
|
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
|
-
* @
|
|
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}/
|
|
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
|
|
204
|
+
* @param params - Object containing the runId, inputData and runtimeContext
|
|
91
205
|
* @returns Promise containing success message
|
|
92
206
|
*/
|
|
93
|
-
start(params: {
|
|
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?.
|
|
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
|
|
103
|
-
* @
|
|
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
|
-
|
|
225
|
+
step,
|
|
109
226
|
runId,
|
|
110
|
-
|
|
227
|
+
resumeData,
|
|
228
|
+
...rest
|
|
111
229
|
}: {
|
|
112
|
-
|
|
230
|
+
step: string | string[];
|
|
113
231
|
runId: string;
|
|
114
|
-
|
|
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
|
-
|
|
120
|
-
|
|
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
|
|
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: {
|
|
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
|
|
267
|
+
body: { inputData: params.inputData, runtimeContext },
|
|
140
268
|
});
|
|
141
269
|
}
|
|
142
270
|
|
|
143
271
|
/**
|
|
144
|
-
*
|
|
145
|
-
* @param params - Object containing the runId,
|
|
146
|
-
* @returns Promise containing the workflow
|
|
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
|
-
|
|
149
|
-
|
|
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
|
-
|
|
160
|
-
|
|
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
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
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
|
-
|
|
174
|
-
|
|
175
|
-
|
|
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
|
-
|
|
180
|
-
|
|
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 =
|
|
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
|
-
//
|
|
190
|
-
|
|
312
|
+
// Split by record separator
|
|
313
|
+
const chunks = decoded.split(RECORD_SEPARATOR);
|
|
191
314
|
|
|
192
|
-
// Process
|
|
315
|
+
// Process each chunk
|
|
193
316
|
for (const chunk of chunks) {
|
|
194
317
|
if (chunk) {
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
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
|
|
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
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
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:
|
|
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
|
-
|
|
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
|
}
|