@mastra/client-js 0.0.0-switch-to-core-20250424015131 → 0.0.0-vector-query-sources-20250516172905

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.
@@ -0,0 +1,261 @@
1
+ import type { RuntimeContext } from '@mastra/core/runtime-context';
2
+ import type {
3
+ ClientOptions,
4
+ GetVNextWorkflowResponse,
5
+ GetVNextWorkflowRunsResponse,
6
+ GetWorkflowRunsParams,
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<GetVNextWorkflowRunsResponse> {
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
+ const runtimeContext = params.runtimeContext ? Object.fromEntries(params.runtimeContext.entries()) : undefined;
160
+ return this.request(`/api/workflows/v-next/${this.workflowId}/start?runId=${params.runId}`, {
161
+ method: 'POST',
162
+ body: { inputData: params?.inputData, runtimeContext },
163
+ });
164
+ }
165
+
166
+ /**
167
+ * Resumes a suspended vNext workflow step synchronously without waiting for the vNext workflow to complete
168
+ * @param params - Object containing the runId, step, resumeData and runtimeContext
169
+ * @returns Promise containing success message
170
+ */
171
+ resume({
172
+ step,
173
+ runId,
174
+ resumeData,
175
+ ...rest
176
+ }: {
177
+ step: string | string[];
178
+ runId: string;
179
+ resumeData?: Record<string, any>;
180
+ runtimeContext?: RuntimeContext;
181
+ }): Promise<{ message: string }> {
182
+ const runtimeContext = rest.runtimeContext ? Object.fromEntries(rest.runtimeContext.entries()) : undefined;
183
+ return this.request(`/api/workflows/v-next/${this.workflowId}/resume?runId=${runId}`, {
184
+ method: 'POST',
185
+ stream: true,
186
+ body: {
187
+ step,
188
+ resumeData,
189
+ runtimeContext,
190
+ },
191
+ });
192
+ }
193
+
194
+ /**
195
+ * Starts a vNext workflow run asynchronously and returns a promise that resolves when the vNext workflow is complete
196
+ * @param params - Object containing the optional runId, inputData and runtimeContext
197
+ * @returns Promise containing the vNext workflow execution results
198
+ */
199
+ startAsync(params: {
200
+ runId?: string;
201
+ inputData: Record<string, any>;
202
+ runtimeContext?: RuntimeContext;
203
+ }): Promise<VNextWorkflowRunResult> {
204
+ const searchParams = new URLSearchParams();
205
+
206
+ if (!!params?.runId) {
207
+ searchParams.set('runId', params.runId);
208
+ }
209
+
210
+ const runtimeContext = params.runtimeContext ? Object.fromEntries(params.runtimeContext.entries()) : undefined;
211
+ return this.request(`/api/workflows/v-next/${this.workflowId}/start-async?${searchParams.toString()}`, {
212
+ method: 'POST',
213
+ body: { inputData: params.inputData, runtimeContext },
214
+ });
215
+ }
216
+
217
+ /**
218
+ * Resumes a suspended vNext workflow step asynchronously and returns a promise that resolves when the vNext workflow is complete
219
+ * @param params - Object containing the runId, step, resumeData and runtimeContext
220
+ * @returns Promise containing the vNext workflow resume results
221
+ */
222
+ resumeAsync(params: {
223
+ runId: string;
224
+ step: string | string[];
225
+ resumeData?: Record<string, any>;
226
+ runtimeContext?: RuntimeContext;
227
+ }): Promise<VNextWorkflowRunResult> {
228
+ const runtimeContext = params.runtimeContext ? Object.fromEntries(params.runtimeContext.entries()) : undefined;
229
+ return this.request(`/api/workflows/v-next/${this.workflowId}/resume-async?runId=${params.runId}`, {
230
+ method: 'POST',
231
+ body: {
232
+ step: params.step,
233
+ resumeData: params.resumeData,
234
+ runtimeContext,
235
+ },
236
+ });
237
+ }
238
+
239
+ /**
240
+ * Watches vNext workflow transitions in real-time
241
+ * @param runId - Optional run ID to filter the watch stream
242
+ * @returns AsyncGenerator that yields parsed records from the vNext workflow watch stream
243
+ */
244
+ async watch({ runId }: { runId?: string }, onRecord: (record: VNextWorkflowWatchResult) => void) {
245
+ const response: Response = await this.request(`/api/workflows/v-next/${this.workflowId}/watch?runId=${runId}`, {
246
+ stream: true,
247
+ });
248
+
249
+ if (!response.ok) {
250
+ throw new Error(`Failed to watch vNext workflow: ${response.statusText}`);
251
+ }
252
+
253
+ if (!response.body) {
254
+ throw new Error('Response body is null');
255
+ }
256
+
257
+ for await (const record of this.streamProcessor(response.body)) {
258
+ onRecord(record);
259
+ }
260
+ }
261
+ }
@@ -1,4 +1,10 @@
1
- import type { GetWorkflowResponse, ClientOptions, WorkflowRunResult } from '../types';
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
 
@@ -20,6 +26,36 @@ export class Workflow extends BaseResource {
20
26
  return this.request(`/api/workflows/${this.workflowId}`);
21
27
  }
22
28
 
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
+
23
59
  /**
24
60
  * @deprecated Use `startAsync` instead
25
61
  * Executes the workflow with the provided parameters
@@ -168,7 +204,7 @@ export class Workflow extends BaseResource {
168
204
  }
169
205
  }
170
206
  }
171
- } catch (error) {
207
+ } catch {
172
208
  // Silently ignore parsing errors to maintain stream processing
173
209
  // This allows the stream to continue even if one record is malformed
174
210
  }
package/src/types.ts CHANGED
@@ -8,9 +8,14 @@ import type {
8
8
  StorageThreadType,
9
9
  BaseLogMessage,
10
10
  WorkflowRunResult as CoreWorkflowRunResult,
11
+ VNextWorkflowRuns,
12
+ WorkflowRuns,
11
13
  } from '@mastra/core';
12
14
 
13
15
  import type { AgentGenerateOptions, AgentStreamOptions } from '@mastra/core/agent';
16
+ import type { RuntimeContext } from '@mastra/core/runtime-context';
17
+ import type { ServerInfo } from '@mastra/core/mcp';
18
+ import type { NewWorkflow, WatchEvent, WorkflowResult as VNextWorkflowResult } from '@mastra/core/workflows/vNext';
14
19
  import type { JSONSchema7 } from 'json-schema';
15
20
  import type { ZodSchema } from 'zod';
16
21
 
@@ -36,24 +41,44 @@ export interface RequestOptions {
36
41
  signal?: AbortSignal;
37
42
  }
38
43
 
44
+ type WithoutMethods<T> = {
45
+ [K in keyof T as T[K] extends (...args: any[]) => any
46
+ ? never
47
+ : T[K] extends { (): any }
48
+ ? never
49
+ : T[K] extends undefined | ((...args: any[]) => any)
50
+ ? never
51
+ : K]: T[K];
52
+ };
53
+
39
54
  export interface GetAgentResponse {
40
55
  name: string;
41
56
  instructions: string;
42
57
  tools: Record<string, GetToolResponse>;
58
+ workflows: Record<string, GetWorkflowResponse>;
43
59
  provider: string;
44
60
  modelId: string;
45
61
  }
46
62
 
47
63
  export type GenerateParams<T extends JSONSchema7 | ZodSchema | undefined = undefined> = {
48
64
  messages: string | string[] | CoreMessage[] | AiMessageType[];
49
- } & Partial<AgentGenerateOptions<T>>;
65
+ output?: T;
66
+ experimental_output?: T;
67
+ runtimeContext?: RuntimeContext;
68
+ } & WithoutMethods<Omit<AgentGenerateOptions<T>, 'output' | 'experimental_output' | 'runtimeContext'>>;
50
69
 
51
70
  export type StreamParams<T extends JSONSchema7 | ZodSchema | undefined = undefined> = {
52
71
  messages: string | string[] | CoreMessage[] | AiMessageType[];
53
- } & Omit<AgentStreamOptions<T>, 'onFinish' | 'onStepFinish' | 'telemetry'>;
72
+ output?: T;
73
+ experimental_output?: T;
74
+ runtimeContext?: RuntimeContext;
75
+ } & WithoutMethods<Omit<AgentStreamOptions<T>, 'output' | 'experimental_output' | 'runtimeContext'>>;
54
76
 
55
77
  export interface GetEvalsByAgentIdResponse extends GetAgentResponse {
56
78
  evals: any[];
79
+ instructions: string;
80
+ name: string;
81
+ id: string;
57
82
  }
58
83
 
59
84
  export interface GetToolResponse {
@@ -72,12 +97,45 @@ export interface GetWorkflowResponse {
72
97
  workflowId?: string;
73
98
  }
74
99
 
100
+ export interface GetWorkflowRunsParams {
101
+ fromDate?: Date;
102
+ toDate?: Date;
103
+ limit?: number;
104
+ offset?: number;
105
+ resourceId?: string;
106
+ }
107
+
108
+ export type GetWorkflowRunsResponse = WorkflowRuns;
109
+
110
+ export type GetVNextWorkflowRunsResponse = VNextWorkflowRuns;
111
+
75
112
  export type WorkflowRunResult = {
76
113
  activePaths: Record<string, { status: string; suspendPayload?: any; stepPath: string[] }>;
77
114
  results: CoreWorkflowRunResult<any, any, any>['results'];
78
115
  timestamp: number;
79
116
  runId: string;
80
117
  };
118
+
119
+ export interface GetVNextWorkflowResponse {
120
+ name: string;
121
+ steps: {
122
+ [key: string]: {
123
+ id: string;
124
+ description: string;
125
+ inputSchema: string;
126
+ outputSchema: string;
127
+ resumeSchema: string;
128
+ suspendSchema: string;
129
+ };
130
+ };
131
+ stepGraph: NewWorkflow['serializedStepGraph'];
132
+ inputSchema: string;
133
+ outputSchema: string;
134
+ }
135
+
136
+ export type VNextWorkflowWatchResult = WatchEvent & { runId: string };
137
+
138
+ export type VNextWorkflowRunResult = VNextWorkflowResult<any, any>;
81
139
  export interface UpsertVectorParams {
82
140
  indexName: string;
83
141
  vectors: number[][];
@@ -138,6 +196,13 @@ export interface UpdateMemoryThreadParams {
138
196
  resourceId: string;
139
197
  }
140
198
 
199
+ export interface GetMemoryThreadMessagesParams {
200
+ /**
201
+ * Limit the number of messages to retrieve (default: 40)
202
+ */
203
+ limit?: number;
204
+ }
205
+
141
206
  export interface GetMemoryThreadMessagesResponse {
142
207
  messages: CoreMessage[];
143
208
  uiMessages: AiMessageType[];
@@ -206,6 +271,8 @@ export interface GetTelemetryParams {
206
271
  page?: number;
207
272
  perPage?: number;
208
273
  attribute?: Record<string, string>;
274
+ fromDate?: Date;
275
+ toDate?: Date;
209
276
  }
210
277
 
211
278
  export interface GetNetworkResponse {
@@ -222,3 +289,20 @@ export interface GetNetworkResponse {
222
289
  };
223
290
  state?: Record<string, any>;
224
291
  }
292
+
293
+ export interface McpServerListResponse {
294
+ servers: ServerInfo[];
295
+ next: string | null;
296
+ total_count: number;
297
+ }
298
+
299
+ export interface McpToolInfo {
300
+ id: string;
301
+ name: string;
302
+ description?: string;
303
+ inputSchema: string;
304
+ }
305
+
306
+ export interface McpServerToolListResponse {
307
+ tools: McpToolInfo[];
308
+ }
@@ -0,0 +1,10 @@
1
+ import { ZodSchema } from 'zod';
2
+ import originalZodToJsonSchema from 'zod-to-json-schema';
3
+
4
+ export function zodToJsonSchema<T extends ZodSchema | any>(zodSchema: T) {
5
+ if (!(zodSchema instanceof ZodSchema)) {
6
+ return zodSchema;
7
+ }
8
+
9
+ return originalZodToJsonSchema(zodSchema, { $refStrategy: 'none' });
10
+ }