@mastra/client-js 0.0.0-storage-20250225005900 → 0.0.0-trigger-playground-ui-package-20250506151043
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/CHANGELOG.md +983 -3
- package/{LICENSE → LICENSE.md} +3 -1
- package/README.md +6 -3
- package/dist/index.cjs +1146 -0
- package/dist/index.d.cts +730 -0
- package/dist/index.d.ts +730 -0
- package/dist/index.js +1144 -0
- package/package.json +30 -19
- package/src/adapters/agui.test.ts +167 -0
- package/src/adapters/agui.ts +219 -0
- package/src/client.ts +65 -11
- package/src/example.ts +65 -43
- package/src/index.test.ts +173 -60
- package/src/resources/agent.ts +92 -4
- package/src/resources/base.ts +5 -3
- package/src/resources/index.ts +2 -0
- package/src/resources/memory-thread.ts +1 -8
- package/src/resources/network.ts +92 -0
- package/src/resources/tool.ts +9 -3
- package/src/resources/vnext-workflow.ts +257 -0
- package/src/resources/workflow.ts +192 -9
- package/src/types.ts +116 -17
- package/.turbo/turbo-build.log +0 -16
- package/dist/index.d.mts +0 -405
- package/dist/index.mjs +0 -487
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import type { RuntimeContext } from '@mastra/core/runtime-context';
|
|
2
|
+
import type {
|
|
3
|
+
ClientOptions,
|
|
4
|
+
GetVNextWorkflowResponse,
|
|
5
|
+
GetWorkflowRunsParams,
|
|
6
|
+
GetWorkflowRunsResponse,
|
|
7
|
+
VNextWorkflowRunResult,
|
|
8
|
+
VNextWorkflowWatchResult,
|
|
9
|
+
} from '../types';
|
|
10
|
+
|
|
11
|
+
import { BaseResource } from './base';
|
|
12
|
+
|
|
13
|
+
const RECORD_SEPARATOR = '\x1E';
|
|
14
|
+
|
|
15
|
+
export class VNextWorkflow extends BaseResource {
|
|
16
|
+
constructor(
|
|
17
|
+
options: ClientOptions,
|
|
18
|
+
private workflowId: string,
|
|
19
|
+
) {
|
|
20
|
+
super(options);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Creates an async generator that processes a readable stream and yields vNext workflow records
|
|
25
|
+
* separated by the Record Separator character (\x1E)
|
|
26
|
+
*
|
|
27
|
+
* @param stream - The readable stream to process
|
|
28
|
+
* @returns An async generator that yields parsed records
|
|
29
|
+
*/
|
|
30
|
+
private async *streamProcessor(stream: ReadableStream): AsyncGenerator<VNextWorkflowWatchResult, void, unknown> {
|
|
31
|
+
const reader = stream.getReader();
|
|
32
|
+
|
|
33
|
+
// Track if we've finished reading from the stream
|
|
34
|
+
let doneReading = false;
|
|
35
|
+
// Buffer to accumulate partial chunks
|
|
36
|
+
let buffer = '';
|
|
37
|
+
|
|
38
|
+
try {
|
|
39
|
+
while (!doneReading) {
|
|
40
|
+
// Read the next chunk from the stream
|
|
41
|
+
const { done, value } = await reader.read();
|
|
42
|
+
doneReading = done;
|
|
43
|
+
|
|
44
|
+
// Skip processing if we're done and there's no value
|
|
45
|
+
if (done && !value) continue;
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
// Decode binary data to text
|
|
49
|
+
const decoded = value ? new TextDecoder().decode(value) : '';
|
|
50
|
+
|
|
51
|
+
// Split the combined buffer and new data by record separator
|
|
52
|
+
const chunks = (buffer + decoded).split(RECORD_SEPARATOR);
|
|
53
|
+
|
|
54
|
+
// The last chunk might be incomplete, so save it for the next iteration
|
|
55
|
+
buffer = chunks.pop() || '';
|
|
56
|
+
|
|
57
|
+
// Process complete chunks
|
|
58
|
+
for (const chunk of chunks) {
|
|
59
|
+
if (chunk) {
|
|
60
|
+
// Only process non-empty chunks
|
|
61
|
+
if (typeof chunk === 'string') {
|
|
62
|
+
try {
|
|
63
|
+
const parsedChunk = JSON.parse(chunk);
|
|
64
|
+
yield parsedChunk;
|
|
65
|
+
} catch {
|
|
66
|
+
// Silently ignore parsing errors to maintain stream processing
|
|
67
|
+
// This allows the stream to continue even if one record is malformed
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
} catch {
|
|
73
|
+
// Silently ignore parsing errors to maintain stream processing
|
|
74
|
+
// This allows the stream to continue even if one record is malformed
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Process any remaining data in the buffer after stream is done
|
|
79
|
+
if (buffer) {
|
|
80
|
+
try {
|
|
81
|
+
yield JSON.parse(buffer);
|
|
82
|
+
} catch {
|
|
83
|
+
// Ignore parsing error for final chunk
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
} finally {
|
|
87
|
+
// Always ensure we clean up the reader
|
|
88
|
+
reader.cancel().catch(() => {
|
|
89
|
+
// Ignore cancel errors
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Retrieves details about the vNext workflow
|
|
96
|
+
* @returns Promise containing vNext workflow details including steps and graphs
|
|
97
|
+
*/
|
|
98
|
+
details(): Promise<GetVNextWorkflowResponse> {
|
|
99
|
+
return this.request(`/api/workflows/v-next/${this.workflowId}`);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Retrieves all runs for a vNext workflow
|
|
104
|
+
* @param params - Parameters for filtering runs
|
|
105
|
+
* @returns Promise containing vNext workflow runs array
|
|
106
|
+
*/
|
|
107
|
+
runs(params?: GetWorkflowRunsParams): Promise<GetWorkflowRunsResponse> {
|
|
108
|
+
const searchParams = new URLSearchParams();
|
|
109
|
+
if (params?.fromDate) {
|
|
110
|
+
searchParams.set('fromDate', params.fromDate.toISOString());
|
|
111
|
+
}
|
|
112
|
+
if (params?.toDate) {
|
|
113
|
+
searchParams.set('toDate', params.toDate.toISOString());
|
|
114
|
+
}
|
|
115
|
+
if (params?.limit) {
|
|
116
|
+
searchParams.set('limit', String(params.limit));
|
|
117
|
+
}
|
|
118
|
+
if (params?.offset) {
|
|
119
|
+
searchParams.set('offset', String(params.offset));
|
|
120
|
+
}
|
|
121
|
+
if (params?.resourceId) {
|
|
122
|
+
searchParams.set('resourceId', params.resourceId);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (searchParams.size) {
|
|
126
|
+
return this.request(`/api/workflows/v-next/${this.workflowId}/runs?${searchParams}`);
|
|
127
|
+
} else {
|
|
128
|
+
return this.request(`/api/workflows/v-next/${this.workflowId}/runs`);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Creates a new vNext workflow run
|
|
134
|
+
* @param params - Optional object containing the optional runId
|
|
135
|
+
* @returns Promise containing the runId of the created run
|
|
136
|
+
*/
|
|
137
|
+
createRun(params?: { runId?: string }): Promise<{ runId: string }> {
|
|
138
|
+
const searchParams = new URLSearchParams();
|
|
139
|
+
|
|
140
|
+
if (!!params?.runId) {
|
|
141
|
+
searchParams.set('runId', params.runId);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return this.request(`/api/workflows/v-next/${this.workflowId}/create-run?${searchParams.toString()}`, {
|
|
145
|
+
method: 'POST',
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Starts a vNext workflow run synchronously without waiting for the workflow to complete
|
|
151
|
+
* @param params - Object containing the runId, inputData and runtimeContext
|
|
152
|
+
* @returns Promise containing success message
|
|
153
|
+
*/
|
|
154
|
+
start(params: {
|
|
155
|
+
runId: string;
|
|
156
|
+
inputData: Record<string, any>;
|
|
157
|
+
runtimeContext?: RuntimeContext;
|
|
158
|
+
}): Promise<{ message: string }> {
|
|
159
|
+
return this.request(`/api/workflows/v-next/${this.workflowId}/start?runId=${params.runId}`, {
|
|
160
|
+
method: 'POST',
|
|
161
|
+
body: { inputData: params?.inputData, runtimeContext: params.runtimeContext },
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Resumes a suspended vNext workflow step synchronously without waiting for the vNext workflow to complete
|
|
167
|
+
* @param params - Object containing the runId, step, resumeData and runtimeContext
|
|
168
|
+
* @returns Promise containing success message
|
|
169
|
+
*/
|
|
170
|
+
resume({
|
|
171
|
+
step,
|
|
172
|
+
runId,
|
|
173
|
+
resumeData,
|
|
174
|
+
runtimeContext,
|
|
175
|
+
}: {
|
|
176
|
+
step: string | string[];
|
|
177
|
+
runId: string;
|
|
178
|
+
resumeData?: Record<string, any>;
|
|
179
|
+
runtimeContext?: RuntimeContext;
|
|
180
|
+
}): Promise<{ message: string }> {
|
|
181
|
+
return this.request(`/api/workflows/v-next/${this.workflowId}/resume?runId=${runId}`, {
|
|
182
|
+
method: 'POST',
|
|
183
|
+
stream: true,
|
|
184
|
+
body: {
|
|
185
|
+
step,
|
|
186
|
+
resumeData,
|
|
187
|
+
runtimeContext,
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Starts a vNext workflow run asynchronously and returns a promise that resolves when the vNext workflow is complete
|
|
194
|
+
* @param params - Object containing the optional runId, inputData and runtimeContext
|
|
195
|
+
* @returns Promise containing the vNext workflow execution results
|
|
196
|
+
*/
|
|
197
|
+
startAsync(params: {
|
|
198
|
+
runId?: string;
|
|
199
|
+
inputData: Record<string, any>;
|
|
200
|
+
runtimeContext?: RuntimeContext;
|
|
201
|
+
}): Promise<VNextWorkflowRunResult> {
|
|
202
|
+
const searchParams = new URLSearchParams();
|
|
203
|
+
|
|
204
|
+
if (!!params?.runId) {
|
|
205
|
+
searchParams.set('runId', params.runId);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
return this.request(`/api/workflows/v-next/${this.workflowId}/start-async?${searchParams.toString()}`, {
|
|
209
|
+
method: 'POST',
|
|
210
|
+
body: { inputData: params.inputData, runtimeContext: params.runtimeContext },
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Resumes a suspended vNext workflow step asynchronously and returns a promise that resolves when the vNext workflow is complete
|
|
216
|
+
* @param params - Object containing the runId, step, resumeData and runtimeContext
|
|
217
|
+
* @returns Promise containing the vNext workflow resume results
|
|
218
|
+
*/
|
|
219
|
+
resumeAsync(params: {
|
|
220
|
+
runId: string;
|
|
221
|
+
step: string | string[];
|
|
222
|
+
resumeData?: Record<string, any>;
|
|
223
|
+
runtimeContext?: RuntimeContext;
|
|
224
|
+
}): Promise<VNextWorkflowRunResult> {
|
|
225
|
+
return this.request(`/api/workflows/v-next/${this.workflowId}/resume-async?runId=${params.runId}`, {
|
|
226
|
+
method: 'POST',
|
|
227
|
+
body: {
|
|
228
|
+
step: params.step,
|
|
229
|
+
resumeData: params.resumeData,
|
|
230
|
+
runtimeContext: params.runtimeContext,
|
|
231
|
+
},
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Watches vNext workflow transitions in real-time
|
|
237
|
+
* @param runId - Optional run ID to filter the watch stream
|
|
238
|
+
* @returns AsyncGenerator that yields parsed records from the vNext workflow watch stream
|
|
239
|
+
*/
|
|
240
|
+
async watch({ runId }: { runId?: string }, onRecord: (record: VNextWorkflowWatchResult) => void) {
|
|
241
|
+
const response: Response = await this.request(`/api/workflows/v-next/${this.workflowId}/watch?runId=${runId}`, {
|
|
242
|
+
stream: true,
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
if (!response.ok) {
|
|
246
|
+
throw new Error(`Failed to watch vNext workflow: ${response.statusText}`);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
if (!response.body) {
|
|
250
|
+
throw new Error('Response body is null');
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
254
|
+
onRecord(record);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
@@ -1,7 +1,15 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
GetWorkflowResponse,
|
|
3
|
+
ClientOptions,
|
|
4
|
+
WorkflowRunResult,
|
|
5
|
+
GetWorkflowRunsResponse,
|
|
6
|
+
GetWorkflowRunsParams,
|
|
7
|
+
} from '../types';
|
|
2
8
|
|
|
3
9
|
import { BaseResource } from './base';
|
|
4
10
|
|
|
11
|
+
const RECORD_SEPARATOR = '\x1E';
|
|
12
|
+
|
|
5
13
|
export class Workflow extends BaseResource {
|
|
6
14
|
constructor(
|
|
7
15
|
options: ClientOptions,
|
|
@@ -19,11 +27,42 @@ export class Workflow extends BaseResource {
|
|
|
19
27
|
}
|
|
20
28
|
|
|
21
29
|
/**
|
|
30
|
+
* Retrieves all runs for a workflow
|
|
31
|
+
* @param params - Parameters for filtering runs
|
|
32
|
+
* @returns Promise containing workflow runs array
|
|
33
|
+
*/
|
|
34
|
+
runs(params?: GetWorkflowRunsParams): Promise<GetWorkflowRunsResponse> {
|
|
35
|
+
const searchParams = new URLSearchParams();
|
|
36
|
+
if (params?.fromDate) {
|
|
37
|
+
searchParams.set('fromDate', params.fromDate.toISOString());
|
|
38
|
+
}
|
|
39
|
+
if (params?.toDate) {
|
|
40
|
+
searchParams.set('toDate', params.toDate.toISOString());
|
|
41
|
+
}
|
|
42
|
+
if (params?.limit) {
|
|
43
|
+
searchParams.set('limit', String(params.limit));
|
|
44
|
+
}
|
|
45
|
+
if (params?.offset) {
|
|
46
|
+
searchParams.set('offset', String(params.offset));
|
|
47
|
+
}
|
|
48
|
+
if (params?.resourceId) {
|
|
49
|
+
searchParams.set('resourceId', params.resourceId);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (searchParams.size) {
|
|
53
|
+
return this.request(`/api/workflows/${this.workflowId}/runs?${searchParams}`);
|
|
54
|
+
} else {
|
|
55
|
+
return this.request(`/api/workflows/${this.workflowId}/runs`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* @deprecated Use `startAsync` instead
|
|
22
61
|
* Executes the workflow with the provided parameters
|
|
23
62
|
* @param params - Parameters required for workflow execution
|
|
24
63
|
* @returns Promise containing the workflow execution results
|
|
25
64
|
*/
|
|
26
|
-
execute(params: Record<string, any>): Promise<
|
|
65
|
+
execute(params: Record<string, any>): Promise<WorkflowRunResult> {
|
|
27
66
|
return this.request(`/api/workflows/${this.workflowId}/execute`, {
|
|
28
67
|
method: 'POST',
|
|
29
68
|
body: params,
|
|
@@ -31,7 +70,35 @@ export class Workflow extends BaseResource {
|
|
|
31
70
|
}
|
|
32
71
|
|
|
33
72
|
/**
|
|
34
|
-
*
|
|
73
|
+
* Creates a new workflow run
|
|
74
|
+
* @returns Promise containing the generated run ID
|
|
75
|
+
*/
|
|
76
|
+
createRun(params?: { runId?: string }): Promise<{ runId: string }> {
|
|
77
|
+
const searchParams = new URLSearchParams();
|
|
78
|
+
|
|
79
|
+
if (!!params?.runId) {
|
|
80
|
+
searchParams.set('runId', params.runId);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return this.request(`/api/workflows/${this.workflowId}/createRun?${searchParams.toString()}`, {
|
|
84
|
+
method: 'POST',
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Starts a workflow run synchronously without waiting for the workflow to complete
|
|
90
|
+
* @param params - Object containing the runId and triggerData
|
|
91
|
+
* @returns Promise containing success message
|
|
92
|
+
*/
|
|
93
|
+
start(params: { runId: string; triggerData: Record<string, any> }): Promise<{ message: string }> {
|
|
94
|
+
return this.request(`/api/workflows/${this.workflowId}/start?runId=${params.runId}`, {
|
|
95
|
+
method: 'POST',
|
|
96
|
+
body: params?.triggerData,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Resumes a suspended workflow step synchronously without waiting for the workflow to complete
|
|
35
102
|
* @param stepId - ID of the step to resume
|
|
36
103
|
* @param runId - ID of the workflow run
|
|
37
104
|
* @param context - Context to resume the workflow with
|
|
@@ -45,24 +112,140 @@ export class Workflow extends BaseResource {
|
|
|
45
112
|
stepId: string;
|
|
46
113
|
runId: string;
|
|
47
114
|
context: Record<string, any>;
|
|
48
|
-
}): Promise<
|
|
49
|
-
return this.request(`/api/workflows/${this.workflowId}/resume`, {
|
|
115
|
+
}): Promise<{ message: string }> {
|
|
116
|
+
return this.request(`/api/workflows/${this.workflowId}/resume?runId=${runId}`, {
|
|
50
117
|
method: 'POST',
|
|
51
118
|
body: {
|
|
52
119
|
stepId,
|
|
53
|
-
runId,
|
|
54
120
|
context,
|
|
55
121
|
},
|
|
56
122
|
});
|
|
57
123
|
}
|
|
58
124
|
|
|
125
|
+
/**
|
|
126
|
+
* 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
|
|
128
|
+
* @returns Promise containing the workflow execution results
|
|
129
|
+
*/
|
|
130
|
+
startAsync(params: { runId?: string; triggerData: Record<string, any> }): Promise<WorkflowRunResult> {
|
|
131
|
+
const searchParams = new URLSearchParams();
|
|
132
|
+
|
|
133
|
+
if (!!params?.runId) {
|
|
134
|
+
searchParams.set('runId', params.runId);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return this.request(`/api/workflows/${this.workflowId}/start-async?${searchParams.toString()}`, {
|
|
138
|
+
method: 'POST',
|
|
139
|
+
body: params?.triggerData,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
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
|
|
147
|
+
*/
|
|
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
|
+
}
|
|
157
|
+
|
|
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();
|
|
167
|
+
|
|
168
|
+
// Track if we've finished reading from the stream
|
|
169
|
+
let doneReading = false;
|
|
170
|
+
// Buffer to accumulate partial chunks
|
|
171
|
+
let buffer = '';
|
|
172
|
+
|
|
173
|
+
try {
|
|
174
|
+
while (!doneReading) {
|
|
175
|
+
// Read the next chunk from the stream
|
|
176
|
+
const { done, value } = await reader.read();
|
|
177
|
+
doneReading = done;
|
|
178
|
+
|
|
179
|
+
// Skip processing if we're done and there's no value
|
|
180
|
+
if (done && !value) continue;
|
|
181
|
+
|
|
182
|
+
try {
|
|
183
|
+
// 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);
|
|
188
|
+
|
|
189
|
+
// The last chunk might be incomplete, so save it for the next iteration
|
|
190
|
+
buffer = chunks.pop() || '';
|
|
191
|
+
|
|
192
|
+
// Process complete chunks
|
|
193
|
+
for (const chunk of chunks) {
|
|
194
|
+
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
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
} catch {
|
|
208
|
+
// Silently ignore parsing errors to maintain stream processing
|
|
209
|
+
// This allows the stream to continue even if one record is malformed
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
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
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
59
229
|
/**
|
|
60
230
|
* Watches workflow transitions in real-time
|
|
61
|
-
* @
|
|
231
|
+
* @param runId - Optional run ID to filter the watch stream
|
|
232
|
+
* @returns AsyncGenerator that yields parsed records from the workflow watch stream
|
|
62
233
|
*/
|
|
63
|
-
watch(
|
|
64
|
-
|
|
234
|
+
async watch({ runId }: { runId?: string }, onRecord: (record: WorkflowRunResult) => void) {
|
|
235
|
+
const response: Response = await this.request(`/api/workflows/${this.workflowId}/watch?runId=${runId}`, {
|
|
65
236
|
stream: true,
|
|
66
237
|
});
|
|
238
|
+
|
|
239
|
+
if (!response.ok) {
|
|
240
|
+
throw new Error(`Failed to watch workflow: ${response.statusText}`);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (!response.body) {
|
|
244
|
+
throw new Error('Response body is null');
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
248
|
+
onRecord(record);
|
|
249
|
+
}
|
|
67
250
|
}
|
|
68
251
|
}
|
package/src/types.ts
CHANGED
|
@@ -7,10 +7,14 @@ import type {
|
|
|
7
7
|
StepGraph,
|
|
8
8
|
StorageThreadType,
|
|
9
9
|
BaseLogMessage,
|
|
10
|
-
|
|
10
|
+
WorkflowRunResult as CoreWorkflowRunResult,
|
|
11
|
+
WorkflowRuns,
|
|
11
12
|
} from '@mastra/core';
|
|
13
|
+
|
|
14
|
+
import type { AgentGenerateOptions, AgentStreamOptions } from '@mastra/core/agent';
|
|
15
|
+
import type { NewWorkflow, WatchEvent, WorkflowResult as VNextWorkflowResult } from '@mastra/core/workflows/vNext';
|
|
12
16
|
import type { JSONSchema7 } from 'json-schema';
|
|
13
|
-
import { ZodSchema } from 'zod';
|
|
17
|
+
import type { ZodSchema } from 'zod';
|
|
14
18
|
|
|
15
19
|
export interface ClientOptions {
|
|
16
20
|
/** Base URL for API requests */
|
|
@@ -23,6 +27,7 @@ export interface ClientOptions {
|
|
|
23
27
|
maxBackoffMs?: number;
|
|
24
28
|
/** Custom headers to include with requests */
|
|
25
29
|
headers?: Record<string, string>;
|
|
30
|
+
/** Abort signal for request */
|
|
26
31
|
}
|
|
27
32
|
|
|
28
33
|
export interface RequestOptions {
|
|
@@ -30,6 +35,7 @@ export interface RequestOptions {
|
|
|
30
35
|
headers?: Record<string, string>;
|
|
31
36
|
body?: any;
|
|
32
37
|
stream?: boolean;
|
|
38
|
+
signal?: AbortSignal;
|
|
33
39
|
}
|
|
34
40
|
|
|
35
41
|
export interface GetAgentResponse {
|
|
@@ -37,24 +43,22 @@ export interface GetAgentResponse {
|
|
|
37
43
|
instructions: string;
|
|
38
44
|
tools: Record<string, GetToolResponse>;
|
|
39
45
|
provider: string;
|
|
46
|
+
modelId: string;
|
|
40
47
|
}
|
|
41
48
|
|
|
42
|
-
export
|
|
43
|
-
messages: string | string[] | CoreMessage[];
|
|
44
|
-
|
|
45
|
-
resourceid?: string;
|
|
46
|
-
output?: OutputType | T;
|
|
47
|
-
}
|
|
49
|
+
export type GenerateParams<T extends JSONSchema7 | ZodSchema | undefined = undefined> = {
|
|
50
|
+
messages: string | string[] | CoreMessage[] | AiMessageType[];
|
|
51
|
+
} & Partial<AgentGenerateOptions<T>>;
|
|
48
52
|
|
|
49
|
-
export
|
|
50
|
-
messages: string | string[] | CoreMessage[];
|
|
51
|
-
|
|
52
|
-
resourceid?: string;
|
|
53
|
-
output?: OutputType | T;
|
|
54
|
-
}
|
|
53
|
+
export type StreamParams<T extends JSONSchema7 | ZodSchema | undefined = undefined> = {
|
|
54
|
+
messages: string | string[] | CoreMessage[] | AiMessageType[];
|
|
55
|
+
} & Omit<AgentStreamOptions<T>, 'onFinish' | 'onStepFinish' | 'telemetry'>;
|
|
55
56
|
|
|
56
57
|
export interface GetEvalsByAgentIdResponse extends GetAgentResponse {
|
|
57
58
|
evals: any[];
|
|
59
|
+
instructions: string;
|
|
60
|
+
name: string;
|
|
61
|
+
id: string;
|
|
58
62
|
}
|
|
59
63
|
|
|
60
64
|
export interface GetToolResponse {
|
|
@@ -70,8 +74,46 @@ export interface GetWorkflowResponse {
|
|
|
70
74
|
steps: Record<string, StepAction<any, any, any, any>>;
|
|
71
75
|
stepGraph: StepGraph;
|
|
72
76
|
stepSubscriberGraph: Record<string, StepGraph>;
|
|
77
|
+
workflowId?: string;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface GetWorkflowRunsParams {
|
|
81
|
+
fromDate?: Date;
|
|
82
|
+
toDate?: Date;
|
|
83
|
+
limit?: number;
|
|
84
|
+
offset?: number;
|
|
85
|
+
resourceId?: string;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export type GetWorkflowRunsResponse = WorkflowRuns;
|
|
89
|
+
|
|
90
|
+
export type WorkflowRunResult = {
|
|
91
|
+
activePaths: Record<string, { status: string; suspendPayload?: any; stepPath: string[] }>;
|
|
92
|
+
results: CoreWorkflowRunResult<any, any, any>['results'];
|
|
93
|
+
timestamp: number;
|
|
94
|
+
runId: string;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export interface GetVNextWorkflowResponse {
|
|
98
|
+
name: string;
|
|
99
|
+
steps: {
|
|
100
|
+
[key: string]: {
|
|
101
|
+
id: string;
|
|
102
|
+
description: string;
|
|
103
|
+
inputSchema: string;
|
|
104
|
+
outputSchema: string;
|
|
105
|
+
resumeSchema: string;
|
|
106
|
+
suspendSchema: string;
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
stepGraph: NewWorkflow['serializedStepGraph'];
|
|
110
|
+
inputSchema: string;
|
|
111
|
+
outputSchema: string;
|
|
73
112
|
}
|
|
74
113
|
|
|
114
|
+
export type VNextWorkflowWatchResult = WatchEvent & { runId: string };
|
|
115
|
+
|
|
116
|
+
export type VNextWorkflowRunResult = VNextWorkflowResult<any, any>;
|
|
75
117
|
export interface UpsertVectorParams {
|
|
76
118
|
indexName: string;
|
|
77
119
|
vectors: number[][];
|
|
@@ -112,7 +154,7 @@ export type SaveMessageToMemoryResponse = MessageType[];
|
|
|
112
154
|
export interface CreateMemoryThreadParams {
|
|
113
155
|
title: string;
|
|
114
156
|
metadata: Record<string, any>;
|
|
115
|
-
|
|
157
|
+
resourceId: string;
|
|
116
158
|
threadId: string;
|
|
117
159
|
agentId: string;
|
|
118
160
|
}
|
|
@@ -129,7 +171,7 @@ export type GetMemoryThreadResponse = StorageThreadType[];
|
|
|
129
171
|
export interface UpdateMemoryThreadParams {
|
|
130
172
|
title: string;
|
|
131
173
|
metadata: Record<string, any>;
|
|
132
|
-
|
|
174
|
+
resourceId: string;
|
|
133
175
|
}
|
|
134
176
|
|
|
135
177
|
export interface GetMemoryThreadMessagesResponse {
|
|
@@ -150,8 +192,48 @@ export type GetLogsResponse = BaseLogMessage[];
|
|
|
150
192
|
|
|
151
193
|
export type RequestFunction = (path: string, options?: RequestOptions) => Promise<any>;
|
|
152
194
|
|
|
195
|
+
type SpanStatus = {
|
|
196
|
+
code: number;
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
type SpanOther = {
|
|
200
|
+
droppedAttributesCount: number;
|
|
201
|
+
droppedEventsCount: number;
|
|
202
|
+
droppedLinksCount: number;
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
type SpanEventAttributes = {
|
|
206
|
+
key: string;
|
|
207
|
+
value: { [key: string]: string | number | boolean | null };
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
type SpanEvent = {
|
|
211
|
+
attributes: SpanEventAttributes[];
|
|
212
|
+
name: string;
|
|
213
|
+
timeUnixNano: string;
|
|
214
|
+
droppedAttributesCount: number;
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
type Span = {
|
|
218
|
+
id: string;
|
|
219
|
+
parentSpanId: string | null;
|
|
220
|
+
traceId: string;
|
|
221
|
+
name: string;
|
|
222
|
+
scope: string;
|
|
223
|
+
kind: number;
|
|
224
|
+
status: SpanStatus;
|
|
225
|
+
events: SpanEvent[];
|
|
226
|
+
links: any[];
|
|
227
|
+
attributes: Record<string, string | number | boolean | null>;
|
|
228
|
+
startTime: number;
|
|
229
|
+
endTime: number;
|
|
230
|
+
duration: number;
|
|
231
|
+
other: SpanOther;
|
|
232
|
+
createdAt: string;
|
|
233
|
+
};
|
|
234
|
+
|
|
153
235
|
export interface GetTelemetryResponse {
|
|
154
|
-
traces:
|
|
236
|
+
traces: Span[];
|
|
155
237
|
}
|
|
156
238
|
|
|
157
239
|
export interface GetTelemetryParams {
|
|
@@ -160,4 +242,21 @@ export interface GetTelemetryParams {
|
|
|
160
242
|
page?: number;
|
|
161
243
|
perPage?: number;
|
|
162
244
|
attribute?: Record<string, string>;
|
|
245
|
+
fromDate?: Date;
|
|
246
|
+
toDate?: Date;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export interface GetNetworkResponse {
|
|
250
|
+
name: string;
|
|
251
|
+
instructions: string;
|
|
252
|
+
agents: Array<{
|
|
253
|
+
name: string;
|
|
254
|
+
provider: string;
|
|
255
|
+
modelId: string;
|
|
256
|
+
}>;
|
|
257
|
+
routingModel: {
|
|
258
|
+
provider: string;
|
|
259
|
+
modelId: string;
|
|
260
|
+
};
|
|
261
|
+
state?: Record<string, any>;
|
|
163
262
|
}
|