@mastra/client-js 0.0.0-fix-fetching-workflow-snapshots-20250625000954 → 0.0.0-fix-traces-pagination-plus-share-for-cloud-20250717083008
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 +8 -8
- package/CHANGELOG.md +283 -2
- package/LICENSE.md +11 -42
- package/dist/index.cjs +468 -324
- package/dist/index.d.cts +119 -39
- package/dist/index.d.ts +119 -39
- package/dist/index.js +469 -325
- package/package.json +6 -5
- package/src/client.ts +48 -2
- package/src/example.ts +45 -17
- package/src/resources/agent.ts +382 -317
- package/src/resources/base.ts +1 -0
- package/src/resources/vNextNetwork.ts +51 -4
- package/src/resources/workflow.ts +34 -6
- package/src/types.ts +31 -3
package/src/resources/base.ts
CHANGED
|
@@ -30,6 +30,7 @@ export class BaseResource {
|
|
|
30
30
|
// TODO: Bring this back once we figure out what we/users need to do to make this work with cross-origin requests
|
|
31
31
|
// 'x-mastra-client-type': 'js',
|
|
32
32
|
},
|
|
33
|
+
signal: this.options.abortSignal,
|
|
33
34
|
body:
|
|
34
35
|
options.body instanceof FormData ? options.body : options.body ? JSON.stringify(options.body) : undefined,
|
|
35
36
|
});
|
|
@@ -6,9 +6,12 @@ import type {
|
|
|
6
6
|
GenerateVNextNetworkResponse,
|
|
7
7
|
LoopVNextNetworkResponse,
|
|
8
8
|
GenerateOrStreamVNextNetworkParams,
|
|
9
|
+
LoopStreamVNextNetworkParams,
|
|
9
10
|
} from '../types';
|
|
10
11
|
|
|
11
12
|
import { BaseResource } from './base';
|
|
13
|
+
import { parseClientRuntimeContext } from '../utils';
|
|
14
|
+
import type { RuntimeContext } from '@mastra/core/runtime-context';
|
|
12
15
|
|
|
13
16
|
const RECORD_SEPARATOR = '\x1E';
|
|
14
17
|
|
|
@@ -36,7 +39,10 @@ export class VNextNetwork extends BaseResource {
|
|
|
36
39
|
generate(params: GenerateOrStreamVNextNetworkParams): Promise<GenerateVNextNetworkResponse> {
|
|
37
40
|
return this.request(`/api/networks/v-next/${this.networkId}/generate`, {
|
|
38
41
|
method: 'POST',
|
|
39
|
-
body:
|
|
42
|
+
body: {
|
|
43
|
+
...params,
|
|
44
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
45
|
+
},
|
|
40
46
|
});
|
|
41
47
|
}
|
|
42
48
|
|
|
@@ -45,10 +51,16 @@ export class VNextNetwork extends BaseResource {
|
|
|
45
51
|
* @param params - Generation parameters including message
|
|
46
52
|
* @returns Promise containing the generated response
|
|
47
53
|
*/
|
|
48
|
-
loop(params: {
|
|
54
|
+
loop(params: {
|
|
55
|
+
message: string;
|
|
56
|
+
runtimeContext?: RuntimeContext | Record<string, any>;
|
|
57
|
+
}): Promise<LoopVNextNetworkResponse> {
|
|
49
58
|
return this.request(`/api/networks/v-next/${this.networkId}/loop`, {
|
|
50
59
|
method: 'POST',
|
|
51
|
-
body:
|
|
60
|
+
body: {
|
|
61
|
+
...params,
|
|
62
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
63
|
+
},
|
|
52
64
|
});
|
|
53
65
|
}
|
|
54
66
|
|
|
@@ -124,7 +136,10 @@ export class VNextNetwork extends BaseResource {
|
|
|
124
136
|
async stream(params: GenerateOrStreamVNextNetworkParams, onRecord: (record: WatchEvent) => void) {
|
|
125
137
|
const response: Response = await this.request(`/api/networks/v-next/${this.networkId}/stream`, {
|
|
126
138
|
method: 'POST',
|
|
127
|
-
body:
|
|
139
|
+
body: {
|
|
140
|
+
...params,
|
|
141
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
142
|
+
},
|
|
128
143
|
stream: true,
|
|
129
144
|
});
|
|
130
145
|
|
|
@@ -144,4 +159,36 @@ export class VNextNetwork extends BaseResource {
|
|
|
144
159
|
}
|
|
145
160
|
}
|
|
146
161
|
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Streams a response from the v-next network loop
|
|
165
|
+
* @param params - Stream parameters including message
|
|
166
|
+
* @returns Promise containing the results
|
|
167
|
+
*/
|
|
168
|
+
async loopStream(params: LoopStreamVNextNetworkParams, onRecord: (record: WatchEvent) => void) {
|
|
169
|
+
const response: Response = await this.request(`/api/networks/v-next/${this.networkId}/loop-stream`, {
|
|
170
|
+
method: 'POST',
|
|
171
|
+
body: {
|
|
172
|
+
...params,
|
|
173
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
174
|
+
},
|
|
175
|
+
stream: true,
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
if (!response.ok) {
|
|
179
|
+
throw new Error(`Failed to stream vNext network loop: ${response.statusText}`);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (!response.body) {
|
|
183
|
+
throw new Error('Response body is null');
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
187
|
+
if (typeof record === 'string') {
|
|
188
|
+
onRecord(JSON.parse(record));
|
|
189
|
+
} else {
|
|
190
|
+
onRecord(record);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
147
194
|
}
|
|
@@ -115,10 +115,10 @@ export class Workflow extends BaseResource {
|
|
|
115
115
|
if (params?.toDate) {
|
|
116
116
|
searchParams.set('toDate', params.toDate.toISOString());
|
|
117
117
|
}
|
|
118
|
-
if (params?.limit !== undefined) {
|
|
118
|
+
if (params?.limit !== null && params?.limit !== undefined && !isNaN(Number(params?.limit))) {
|
|
119
119
|
searchParams.set('limit', String(params.limit));
|
|
120
120
|
}
|
|
121
|
-
if (params?.offset !== undefined) {
|
|
121
|
+
if (params?.offset !== null && params?.offset !== undefined && !isNaN(Number(params?.offset))) {
|
|
122
122
|
searchParams.set('offset', String(params.offset));
|
|
123
123
|
}
|
|
124
124
|
if (params?.resourceId) {
|
|
@@ -150,6 +150,29 @@ export class Workflow extends BaseResource {
|
|
|
150
150
|
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}/execution-result`);
|
|
151
151
|
}
|
|
152
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
|
|
168
|
+
*/
|
|
169
|
+
sendRunEvent(params: { runId: string; event: string; data: unknown }): Promise<{ message: string }> {
|
|
170
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${params.runId}/send-event`, {
|
|
171
|
+
method: 'POST',
|
|
172
|
+
body: { event: params.event, data: params.data },
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
|
|
153
176
|
/**
|
|
154
177
|
* Creates a new workflow run
|
|
155
178
|
* @param params - Optional object containing the optional runId
|
|
@@ -266,8 +289,11 @@ export class Workflow extends BaseResource {
|
|
|
266
289
|
throw new Error('Response body is null');
|
|
267
290
|
}
|
|
268
291
|
|
|
292
|
+
//using undefined instead of empty string to avoid parsing errors
|
|
293
|
+
let failedChunk: string | undefined = undefined;
|
|
294
|
+
|
|
269
295
|
// Create a transform stream that processes the response body
|
|
270
|
-
const transformStream = new TransformStream<ArrayBuffer,
|
|
296
|
+
const transformStream = new TransformStream<ArrayBuffer, { type: string; payload: any }>({
|
|
271
297
|
start() {},
|
|
272
298
|
async transform(chunk, controller) {
|
|
273
299
|
try {
|
|
@@ -280,11 +306,13 @@ export class Workflow extends BaseResource {
|
|
|
280
306
|
// Process each chunk
|
|
281
307
|
for (const chunk of chunks) {
|
|
282
308
|
if (chunk) {
|
|
309
|
+
const newChunk: string = failedChunk ? failedChunk + chunk : chunk;
|
|
283
310
|
try {
|
|
284
|
-
const parsedChunk = JSON.parse(
|
|
311
|
+
const parsedChunk = JSON.parse(newChunk);
|
|
285
312
|
controller.enqueue(parsedChunk);
|
|
286
|
-
|
|
287
|
-
|
|
313
|
+
failedChunk = undefined;
|
|
314
|
+
} catch (error) {
|
|
315
|
+
failedChunk = newChunk;
|
|
288
316
|
}
|
|
289
317
|
}
|
|
290
318
|
}
|
package/src/types.ts
CHANGED
|
@@ -34,6 +34,7 @@ export interface ClientOptions {
|
|
|
34
34
|
/** Custom headers to include with requests */
|
|
35
35
|
headers?: Record<string, string>;
|
|
36
36
|
/** Abort signal for request */
|
|
37
|
+
abortSignal?: AbortSignal;
|
|
37
38
|
}
|
|
38
39
|
|
|
39
40
|
export interface RequestOptions {
|
|
@@ -41,7 +42,6 @@ export interface RequestOptions {
|
|
|
41
42
|
headers?: Record<string, string>;
|
|
42
43
|
body?: any;
|
|
43
44
|
stream?: boolean;
|
|
44
|
-
signal?: AbortSignal;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
type WithoutMethods<T> = {
|
|
@@ -71,7 +71,9 @@ export type GenerateParams<T extends JSONSchema7 | ZodSchema | undefined = undef
|
|
|
71
71
|
experimental_output?: T;
|
|
72
72
|
runtimeContext?: RuntimeContext | Record<string, any>;
|
|
73
73
|
clientTools?: ToolsInput;
|
|
74
|
-
} & WithoutMethods<
|
|
74
|
+
} & WithoutMethods<
|
|
75
|
+
Omit<AgentGenerateOptions<T>, 'output' | 'experimental_output' | 'runtimeContext' | 'clientTools' | 'abortSignal'>
|
|
76
|
+
>;
|
|
75
77
|
|
|
76
78
|
export type StreamParams<T extends JSONSchema7 | ZodSchema | undefined = undefined> = {
|
|
77
79
|
messages: string | string[] | CoreMessage[] | AiMessageType[];
|
|
@@ -79,7 +81,9 @@ export type StreamParams<T extends JSONSchema7 | ZodSchema | undefined = undefin
|
|
|
79
81
|
experimental_output?: T;
|
|
80
82
|
runtimeContext?: RuntimeContext | Record<string, any>;
|
|
81
83
|
clientTools?: ToolsInput;
|
|
82
|
-
} & WithoutMethods<
|
|
84
|
+
} & WithoutMethods<
|
|
85
|
+
Omit<AgentStreamOptions<T>, 'output' | 'experimental_output' | 'runtimeContext' | 'clientTools' | 'abortSignal'>
|
|
86
|
+
>;
|
|
83
87
|
|
|
84
88
|
export interface GetEvalsByAgentIdResponse extends GetAgentResponse {
|
|
85
89
|
evals: any[];
|
|
@@ -140,6 +144,17 @@ export interface GetWorkflowResponse {
|
|
|
140
144
|
suspendSchema: string;
|
|
141
145
|
};
|
|
142
146
|
};
|
|
147
|
+
allSteps: {
|
|
148
|
+
[key: string]: {
|
|
149
|
+
id: string;
|
|
150
|
+
description: string;
|
|
151
|
+
inputSchema: string;
|
|
152
|
+
outputSchema: string;
|
|
153
|
+
resumeSchema: string;
|
|
154
|
+
suspendSchema: string;
|
|
155
|
+
isWorkflow: boolean;
|
|
156
|
+
};
|
|
157
|
+
};
|
|
143
158
|
stepGraph: Workflow['serializedStepGraph'];
|
|
144
159
|
inputSchema: string;
|
|
145
160
|
outputSchema: string;
|
|
@@ -358,6 +373,10 @@ export interface GetVNextNetworkResponse {
|
|
|
358
373
|
inputSchema: string | undefined;
|
|
359
374
|
outputSchema: string | undefined;
|
|
360
375
|
}>;
|
|
376
|
+
tools: Array<{
|
|
377
|
+
id: string;
|
|
378
|
+
description: string;
|
|
379
|
+
}>;
|
|
361
380
|
}
|
|
362
381
|
|
|
363
382
|
export interface GenerateVNextNetworkResponse {
|
|
@@ -371,6 +390,15 @@ export interface GenerateOrStreamVNextNetworkParams {
|
|
|
371
390
|
message: string;
|
|
372
391
|
threadId?: string;
|
|
373
392
|
resourceId?: string;
|
|
393
|
+
runtimeContext?: RuntimeContext | Record<string, any>;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
export interface LoopStreamVNextNetworkParams {
|
|
397
|
+
message: string;
|
|
398
|
+
threadId?: string;
|
|
399
|
+
resourceId?: string;
|
|
400
|
+
maxIterations?: number;
|
|
401
|
+
runtimeContext?: RuntimeContext | Record<string, any>;
|
|
374
402
|
}
|
|
375
403
|
|
|
376
404
|
export interface LoopVNextNetworkResponse {
|