@mastra/client-js 0.0.0-tool-call-parts-20250630193309 → 0.0.0-transpile-packages-20250724123433

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.
@@ -24,12 +24,15 @@ export class BaseResource {
24
24
  const response = await fetch(`${baseUrl.replace(/\/$/, '')}${path}`, {
25
25
  ...options,
26
26
  headers: {
27
- ...(options.method === 'POST' || options.method === 'PUT' ? { 'content-type': 'application/json' } : {}),
27
+ ...(options.body && (options.method === 'POST' || options.method === 'PUT')
28
+ ? { 'content-type': 'application/json' }
29
+ : {}),
28
30
  ...headers,
29
31
  ...options.headers,
30
32
  // TODO: Bring this back once we figure out what we/users need to do to make this work with cross-origin requests
31
33
  // 'x-mastra-client-type': 'js',
32
34
  },
35
+ signal: this.options.abortSignal,
33
36
  body:
34
37
  options.body instanceof FormData ? options.body : options.body ? JSON.stringify(options.body) : undefined,
35
38
  });
@@ -5,6 +5,8 @@ import type {
5
5
  ClientOptions,
6
6
  UpdateMemoryThreadParams,
7
7
  GetMemoryThreadMessagesParams,
8
+ GetMemoryThreadMessagesPaginatedParams,
9
+ GetMemoryThreadMessagesPaginatedResponse,
8
10
  } from '../types';
9
11
 
10
12
  import { BaseResource } from './base';
@@ -60,4 +62,20 @@ export class MemoryThread extends BaseResource {
60
62
  });
61
63
  return this.request(`/api/memory/threads/${this.threadId}/messages?${query.toString()}`);
62
64
  }
65
+
66
+ /**
67
+ * Retrieves paginated messages associated with the thread with advanced filtering and selection options
68
+ * @param params - Pagination parameters including selectBy criteria, page, perPage, date ranges, and message inclusion options
69
+ * @returns Promise containing paginated thread messages with pagination metadata (total, page, perPage, hasMore)
70
+ */
71
+ getMessagesPaginated({
72
+ selectBy,
73
+ ...rest
74
+ }: GetMemoryThreadMessagesPaginatedParams): Promise<GetMemoryThreadMessagesPaginatedResponse> {
75
+ const query = new URLSearchParams({
76
+ ...rest,
77
+ ...(selectBy ? { selectBy: JSON.stringify(selectBy) } : {}),
78
+ });
79
+ return this.request(`/api/memory/threads/${this.threadId}/messages/paginated?${query.toString()}`);
80
+ }
63
81
  }
@@ -10,6 +10,8 @@ import type {
10
10
  } from '../types';
11
11
 
12
12
  import { BaseResource } from './base';
13
+ import { parseClientRuntimeContext } from '../utils';
14
+ import type { RuntimeContext } from '@mastra/core/runtime-context';
13
15
 
14
16
  const RECORD_SEPARATOR = '\x1E';
15
17
 
@@ -37,7 +39,10 @@ export class VNextNetwork extends BaseResource {
37
39
  generate(params: GenerateOrStreamVNextNetworkParams): Promise<GenerateVNextNetworkResponse> {
38
40
  return this.request(`/api/networks/v-next/${this.networkId}/generate`, {
39
41
  method: 'POST',
40
- body: params,
42
+ body: {
43
+ ...params,
44
+ runtimeContext: parseClientRuntimeContext(params.runtimeContext),
45
+ },
41
46
  });
42
47
  }
43
48
 
@@ -46,10 +51,16 @@ export class VNextNetwork extends BaseResource {
46
51
  * @param params - Generation parameters including message
47
52
  * @returns Promise containing the generated response
48
53
  */
49
- loop(params: { message: string }): Promise<LoopVNextNetworkResponse> {
54
+ loop(params: {
55
+ message: string;
56
+ runtimeContext?: RuntimeContext | Record<string, any>;
57
+ }): Promise<LoopVNextNetworkResponse> {
50
58
  return this.request(`/api/networks/v-next/${this.networkId}/loop`, {
51
59
  method: 'POST',
52
- body: params,
60
+ body: {
61
+ ...params,
62
+ runtimeContext: parseClientRuntimeContext(params.runtimeContext),
63
+ },
53
64
  });
54
65
  }
55
66
 
@@ -125,7 +136,10 @@ export class VNextNetwork extends BaseResource {
125
136
  async stream(params: GenerateOrStreamVNextNetworkParams, onRecord: (record: WatchEvent) => void) {
126
137
  const response: Response = await this.request(`/api/networks/v-next/${this.networkId}/stream`, {
127
138
  method: 'POST',
128
- body: params,
139
+ body: {
140
+ ...params,
141
+ runtimeContext: parseClientRuntimeContext(params.runtimeContext),
142
+ },
129
143
  stream: true,
130
144
  });
131
145
 
@@ -154,7 +168,10 @@ export class VNextNetwork extends BaseResource {
154
168
  async loopStream(params: LoopStreamVNextNetworkParams, onRecord: (record: WatchEvent) => void) {
155
169
  const response: Response = await this.request(`/api/networks/v-next/${this.networkId}/loop-stream`, {
156
170
  method: 'POST',
157
- body: params,
171
+ body: {
172
+ ...params,
173
+ runtimeContext: parseClientRuntimeContext(params.runtimeContext),
174
+ },
158
175
  stream: true,
159
176
  });
160
177
 
@@ -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) {
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) {
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
@@ -167,6 +190,15 @@ export class Workflow extends BaseResource {
167
190
  });
168
191
  }
169
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
+
170
202
  /**
171
203
  * Starts a workflow run synchronously without waiting for the workflow to complete
172
204
  * @param params - Object containing the runId, inputData and runtimeContext
@@ -266,6 +298,9 @@ export class Workflow extends BaseResource {
266
298
  throw new Error('Response body is null');
267
299
  }
268
300
 
301
+ //using undefined instead of empty string to avoid parsing errors
302
+ let failedChunk: string | undefined = undefined;
303
+
269
304
  // Create a transform stream that processes the response body
270
305
  const transformStream = new TransformStream<ArrayBuffer, { type: string; payload: any }>({
271
306
  start() {},
@@ -280,11 +315,13 @@ export class Workflow extends BaseResource {
280
315
  // Process each chunk
281
316
  for (const chunk of chunks) {
282
317
  if (chunk) {
318
+ const newChunk: string = failedChunk ? failedChunk + chunk : chunk;
283
319
  try {
284
- const parsedChunk = JSON.parse(chunk);
320
+ const parsedChunk = JSON.parse(newChunk);
285
321
  controller.enqueue(parsedChunk);
286
- } catch {
287
- // Silently ignore parsing errors
322
+ failedChunk = undefined;
323
+ } catch (error) {
324
+ failedChunk = newChunk;
288
325
  }
289
326
  }
290
327
  }
package/src/types.ts CHANGED
@@ -7,6 +7,9 @@ import type {
7
7
  WorkflowRuns,
8
8
  WorkflowRun,
9
9
  LegacyWorkflowRuns,
10
+ StorageGetMessagesArg,
11
+ PaginationInfo,
12
+ MastraMessageV2,
10
13
  } from '@mastra/core';
11
14
  import type { AgentGenerateOptions, AgentStreamOptions, ToolsInput } from '@mastra/core/agent';
12
15
  import type { BaseLogMessage, LogLevel } from '@mastra/core/logger';
@@ -34,6 +37,7 @@ export interface ClientOptions {
34
37
  /** Custom headers to include with requests */
35
38
  headers?: Record<string, string>;
36
39
  /** Abort signal for request */
40
+ abortSignal?: AbortSignal;
37
41
  }
38
42
 
39
43
  export interface RequestOptions {
@@ -41,7 +45,6 @@ export interface RequestOptions {
41
45
  headers?: Record<string, string>;
42
46
  body?: any;
43
47
  stream?: boolean;
44
- signal?: AbortSignal;
45
48
  }
46
49
 
47
50
  type WithoutMethods<T> = {
@@ -71,7 +74,9 @@ export type GenerateParams<T extends JSONSchema7 | ZodSchema | undefined = undef
71
74
  experimental_output?: T;
72
75
  runtimeContext?: RuntimeContext | Record<string, any>;
73
76
  clientTools?: ToolsInput;
74
- } & WithoutMethods<Omit<AgentGenerateOptions<T>, 'output' | 'experimental_output' | 'runtimeContext' | 'clientTools'>>;
77
+ } & WithoutMethods<
78
+ Omit<AgentGenerateOptions<T>, 'output' | 'experimental_output' | 'runtimeContext' | 'clientTools' | 'abortSignal'>
79
+ >;
75
80
 
76
81
  export type StreamParams<T extends JSONSchema7 | ZodSchema | undefined = undefined> = {
77
82
  messages: string | string[] | CoreMessage[] | AiMessageType[];
@@ -79,7 +84,9 @@ export type StreamParams<T extends JSONSchema7 | ZodSchema | undefined = undefin
79
84
  experimental_output?: T;
80
85
  runtimeContext?: RuntimeContext | Record<string, any>;
81
86
  clientTools?: ToolsInput;
82
- } & WithoutMethods<Omit<AgentStreamOptions<T>, 'output' | 'experimental_output' | 'runtimeContext' | 'clientTools'>>;
87
+ } & WithoutMethods<
88
+ Omit<AgentStreamOptions<T>, 'output' | 'experimental_output' | 'runtimeContext' | 'clientTools' | 'abortSignal'>
89
+ >;
83
90
 
84
91
  export interface GetEvalsByAgentIdResponse extends GetAgentResponse {
85
92
  evals: any[];
@@ -140,6 +147,17 @@ export interface GetWorkflowResponse {
140
147
  suspendSchema: string;
141
148
  };
142
149
  };
150
+ allSteps: {
151
+ [key: string]: {
152
+ id: string;
153
+ description: string;
154
+ inputSchema: string;
155
+ outputSchema: string;
156
+ resumeSchema: string;
157
+ suspendSchema: string;
158
+ isWorkflow: boolean;
159
+ };
160
+ };
143
161
  stepGraph: Workflow['serializedStepGraph'];
144
162
  inputSchema: string;
145
163
  outputSchema: string;
@@ -233,11 +251,17 @@ export interface GetMemoryThreadMessagesParams {
233
251
  limit?: number;
234
252
  }
235
253
 
254
+ export type GetMemoryThreadMessagesPaginatedParams = Omit<StorageGetMessagesArg, 'threadConfig' | 'threadId'>;
255
+
236
256
  export interface GetMemoryThreadMessagesResponse {
237
257
  messages: CoreMessage[];
238
258
  uiMessages: AiMessageType[];
239
259
  }
240
260
 
261
+ export type GetMemoryThreadMessagesPaginatedResponse = PaginationInfo & {
262
+ messages: MastraMessageV1[] | MastraMessageV2[];
263
+ };
264
+
241
265
  export interface GetLogsParams {
242
266
  transportId: string;
243
267
  fromDate?: Date;
@@ -375,6 +399,7 @@ export interface GenerateOrStreamVNextNetworkParams {
375
399
  message: string;
376
400
  threadId?: string;
377
401
  resourceId?: string;
402
+ runtimeContext?: RuntimeContext | Record<string, any>;
378
403
  }
379
404
 
380
405
  export interface LoopStreamVNextNetworkParams {
@@ -382,12 +407,23 @@ export interface LoopStreamVNextNetworkParams {
382
407
  threadId?: string;
383
408
  resourceId?: string;
384
409
  maxIterations?: number;
410
+ runtimeContext?: RuntimeContext | Record<string, any>;
385
411
  }
386
412
 
387
413
  export interface LoopVNextNetworkResponse {
388
414
  status: 'success';
389
415
  result: {
390
- text: string;
416
+ task: string;
417
+ resourceId: string;
418
+ resourceType: 'agent' | 'workflow' | 'none' | 'tool';
419
+ result: string;
420
+ iteration: number;
421
+ isOneOff: boolean;
422
+ prompt: string;
423
+ threadId?: string | undefined;
424
+ threadResourceId?: string | undefined;
425
+ isComplete?: boolean | undefined;
426
+ completionReason?: string | undefined;
391
427
  };
392
428
  steps: WorkflowResult<any, any>['steps'];
393
429
  }