@mastra/client-js 0.0.0-trigger-playground-ui-package-20250506151043 → 0.0.0-update-scorers-api-20250801170445

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.
Files changed (83) hide show
  1. package/CHANGELOG.md +1110 -2
  2. package/LICENSE.md +11 -42
  3. package/README.md +2 -1
  4. package/dist/adapters/agui.d.ts +23 -0
  5. package/dist/adapters/agui.d.ts.map +1 -0
  6. package/dist/client.d.ts +265 -0
  7. package/dist/client.d.ts.map +1 -0
  8. package/dist/example.d.ts +2 -0
  9. package/dist/example.d.ts.map +1 -0
  10. package/dist/index.cjs +1403 -131
  11. package/dist/index.cjs.map +1 -0
  12. package/dist/index.d.ts +4 -730
  13. package/dist/index.d.ts.map +1 -0
  14. package/dist/index.js +1401 -133
  15. package/dist/index.js.map +1 -0
  16. package/dist/resources/a2a.d.ts +44 -0
  17. package/dist/resources/a2a.d.ts.map +1 -0
  18. package/dist/resources/agent.d.ts +112 -0
  19. package/dist/resources/agent.d.ts.map +1 -0
  20. package/dist/resources/base.d.ts +13 -0
  21. package/dist/resources/base.d.ts.map +1 -0
  22. package/dist/resources/index.d.ts +11 -0
  23. package/dist/resources/index.d.ts.map +1 -0
  24. package/dist/resources/legacy-workflow.d.ts +87 -0
  25. package/dist/resources/legacy-workflow.d.ts.map +1 -0
  26. package/dist/resources/mcp-tool.d.ts +27 -0
  27. package/dist/resources/mcp-tool.d.ts.map +1 -0
  28. package/dist/resources/memory-thread.d.ts +53 -0
  29. package/dist/resources/memory-thread.d.ts.map +1 -0
  30. package/dist/resources/network-memory-thread.d.ts +47 -0
  31. package/dist/resources/network-memory-thread.d.ts.map +1 -0
  32. package/dist/resources/network.d.ts +30 -0
  33. package/dist/resources/network.d.ts.map +1 -0
  34. package/dist/resources/tool.d.ts +23 -0
  35. package/dist/resources/tool.d.ts.map +1 -0
  36. package/dist/resources/vNextNetwork.d.ts +42 -0
  37. package/dist/resources/vNextNetwork.d.ts.map +1 -0
  38. package/dist/resources/vector.d.ts +48 -0
  39. package/dist/resources/vector.d.ts.map +1 -0
  40. package/dist/resources/workflow.d.ts +154 -0
  41. package/dist/resources/workflow.d.ts.map +1 -0
  42. package/dist/types.d.ts +422 -0
  43. package/dist/types.d.ts.map +1 -0
  44. package/dist/utils/index.d.ts +3 -0
  45. package/dist/utils/index.d.ts.map +1 -0
  46. package/dist/utils/process-client-tools.d.ts +3 -0
  47. package/dist/utils/process-client-tools.d.ts.map +1 -0
  48. package/dist/utils/zod-to-json-schema.d.ts +105 -0
  49. package/dist/utils/zod-to-json-schema.d.ts.map +1 -0
  50. package/integration-tests/agui-adapter.test.ts +122 -0
  51. package/integration-tests/package.json +18 -0
  52. package/integration-tests/src/mastra/index.ts +35 -0
  53. package/integration-tests/vitest.config.ts +9 -0
  54. package/package.json +28 -19
  55. package/src/adapters/agui.test.ts +164 -9
  56. package/src/adapters/agui.ts +31 -11
  57. package/src/client.ts +386 -22
  58. package/src/example.ts +59 -29
  59. package/src/index.test.ts +522 -6
  60. package/src/index.ts +1 -0
  61. package/src/resources/a2a.ts +88 -0
  62. package/src/resources/agent.ts +629 -48
  63. package/src/resources/base.ts +7 -1
  64. package/src/resources/index.ts +4 -2
  65. package/src/resources/{vnext-workflow.ts → legacy-workflow.ts} +124 -139
  66. package/src/resources/mcp-tool.ts +48 -0
  67. package/src/resources/memory-thread.test.ts +285 -0
  68. package/src/resources/memory-thread.ts +49 -3
  69. package/src/resources/network-memory-thread.test.ts +269 -0
  70. package/src/resources/network-memory-thread.ts +81 -0
  71. package/src/resources/network.ts +10 -16
  72. package/src/resources/tool.ts +9 -2
  73. package/src/resources/vNextNetwork.ts +194 -0
  74. package/src/resources/workflow.ts +255 -96
  75. package/src/types.ts +265 -25
  76. package/src/utils/index.ts +11 -0
  77. package/src/utils/process-client-tools.ts +32 -0
  78. package/src/utils/zod-to-json-schema.ts +10 -0
  79. package/src/v2-messages.test.ts +180 -0
  80. package/tsconfig.build.json +9 -0
  81. package/tsconfig.json +1 -1
  82. package/tsup.config.ts +22 -0
  83. package/dist/index.d.cts +0 -730
package/src/types.ts CHANGED
@@ -1,18 +1,28 @@
1
1
  import type {
2
- MessageType,
2
+ MastraMessageV1,
3
3
  AiMessageType,
4
4
  CoreMessage,
5
5
  QueryResult,
6
- StepAction,
7
- StepGraph,
8
6
  StorageThreadType,
9
- BaseLogMessage,
10
- WorkflowRunResult as CoreWorkflowRunResult,
11
7
  WorkflowRuns,
8
+ WorkflowRun,
9
+ LegacyWorkflowRuns,
10
+ StorageGetMessagesArg,
11
+ PaginationInfo,
12
+ MastraMessageV2,
12
13
  } from '@mastra/core';
14
+ import type { AgentGenerateOptions, AgentStreamOptions, ToolsInput, UIMessageWithMetadata } from '@mastra/core/agent';
15
+ import type { BaseLogMessage, LogLevel } from '@mastra/core/logger';
13
16
 
14
- import type { AgentGenerateOptions, AgentStreamOptions } from '@mastra/core/agent';
15
- import type { NewWorkflow, WatchEvent, WorkflowResult as VNextWorkflowResult } from '@mastra/core/workflows/vNext';
17
+ import type { MCPToolType, ServerInfo } from '@mastra/core/mcp';
18
+ import type { RuntimeContext } from '@mastra/core/runtime-context';
19
+ import type { MastraScorer, MastraScorerEntry, ScoreRowData } from '@mastra/core/scores';
20
+ import type { Workflow, WatchEvent, WorkflowResult } from '@mastra/core/workflows';
21
+ import type {
22
+ StepAction,
23
+ StepGraph,
24
+ LegacyWorkflowRunResult as CoreLegacyWorkflowRunResult,
25
+ } from '@mastra/core/workflows/legacy';
16
26
  import type { JSONSchema7 } from 'json-schema';
17
27
  import type { ZodSchema } from 'zod';
18
28
 
@@ -28,6 +38,7 @@ export interface ClientOptions {
28
38
  /** Custom headers to include with requests */
29
39
  headers?: Record<string, string>;
30
40
  /** Abort signal for request */
41
+ abortSignal?: AbortSignal;
31
42
  }
32
43
 
33
44
  export interface RequestOptions {
@@ -35,24 +46,48 @@ export interface RequestOptions {
35
46
  headers?: Record<string, string>;
36
47
  body?: any;
37
48
  stream?: boolean;
38
- signal?: AbortSignal;
39
49
  }
40
50
 
51
+ type WithoutMethods<T> = {
52
+ [K in keyof T as T[K] extends (...args: any[]) => any
53
+ ? never
54
+ : T[K] extends { (): any }
55
+ ? never
56
+ : T[K] extends undefined | ((...args: any[]) => any)
57
+ ? never
58
+ : K]: T[K];
59
+ };
60
+
41
61
  export interface GetAgentResponse {
42
62
  name: string;
43
63
  instructions: string;
44
64
  tools: Record<string, GetToolResponse>;
65
+ workflows: Record<string, GetWorkflowResponse>;
45
66
  provider: string;
46
67
  modelId: string;
68
+ defaultGenerateOptions: WithoutMethods<AgentGenerateOptions>;
69
+ defaultStreamOptions: WithoutMethods<AgentStreamOptions>;
47
70
  }
48
71
 
49
72
  export type GenerateParams<T extends JSONSchema7 | ZodSchema | undefined = undefined> = {
50
- messages: string | string[] | CoreMessage[] | AiMessageType[];
51
- } & Partial<AgentGenerateOptions<T>>;
73
+ messages: string | string[] | CoreMessage[] | AiMessageType[] | UIMessageWithMetadata[];
74
+ output?: T;
75
+ experimental_output?: T;
76
+ runtimeContext?: RuntimeContext | Record<string, any>;
77
+ clientTools?: ToolsInput;
78
+ } & WithoutMethods<
79
+ Omit<AgentGenerateOptions<T>, 'output' | 'experimental_output' | 'runtimeContext' | 'clientTools' | 'abortSignal'>
80
+ >;
52
81
 
53
82
  export type StreamParams<T extends JSONSchema7 | ZodSchema | undefined = undefined> = {
54
- messages: string | string[] | CoreMessage[] | AiMessageType[];
55
- } & Omit<AgentStreamOptions<T>, 'onFinish' | 'onStepFinish' | 'telemetry'>;
83
+ messages: string | string[] | CoreMessage[] | AiMessageType[] | UIMessageWithMetadata[];
84
+ output?: T;
85
+ experimental_output?: T;
86
+ runtimeContext?: RuntimeContext | Record<string, any>;
87
+ clientTools?: ToolsInput;
88
+ } & WithoutMethods<
89
+ Omit<AgentStreamOptions<T>, 'output' | 'experimental_output' | 'runtimeContext' | 'clientTools' | 'abortSignal'>
90
+ >;
56
91
 
57
92
  export interface GetEvalsByAgentIdResponse extends GetAgentResponse {
58
93
  evals: any[];
@@ -68,7 +103,7 @@ export interface GetToolResponse {
68
103
  outputSchema: string;
69
104
  }
70
105
 
71
- export interface GetWorkflowResponse {
106
+ export interface GetLegacyWorkflowResponse {
72
107
  name: string;
73
108
  triggerSchema: string;
74
109
  steps: Record<string, StepAction<any, any, any, any>>;
@@ -85,17 +120,24 @@ export interface GetWorkflowRunsParams {
85
120
  resourceId?: string;
86
121
  }
87
122
 
123
+ export type GetLegacyWorkflowRunsResponse = LegacyWorkflowRuns;
124
+
88
125
  export type GetWorkflowRunsResponse = WorkflowRuns;
89
126
 
90
- export type WorkflowRunResult = {
127
+ export type GetWorkflowRunByIdResponse = WorkflowRun;
128
+
129
+ export type GetWorkflowRunExecutionResultResponse = WatchEvent['payload']['workflowState'];
130
+
131
+ export type LegacyWorkflowRunResult = {
91
132
  activePaths: Record<string, { status: string; suspendPayload?: any; stepPath: string[] }>;
92
- results: CoreWorkflowRunResult<any, any, any>['results'];
133
+ results: CoreLegacyWorkflowRunResult<any, any, any>['results'];
93
134
  timestamp: number;
94
135
  runId: string;
95
136
  };
96
137
 
97
- export interface GetVNextWorkflowResponse {
138
+ export interface GetWorkflowResponse {
98
139
  name: string;
140
+ description?: string;
99
141
  steps: {
100
142
  [key: string]: {
101
143
  id: string;
@@ -106,14 +148,25 @@ export interface GetVNextWorkflowResponse {
106
148
  suspendSchema: string;
107
149
  };
108
150
  };
109
- stepGraph: NewWorkflow['serializedStepGraph'];
151
+ allSteps: {
152
+ [key: string]: {
153
+ id: string;
154
+ description: string;
155
+ inputSchema: string;
156
+ outputSchema: string;
157
+ resumeSchema: string;
158
+ suspendSchema: string;
159
+ isWorkflow: boolean;
160
+ };
161
+ };
162
+ stepGraph: Workflow['serializedStepGraph'];
110
163
  inputSchema: string;
111
164
  outputSchema: string;
112
165
  }
113
166
 
114
- export type VNextWorkflowWatchResult = WatchEvent & { runId: string };
167
+ export type WorkflowWatchResult = WatchEvent & { runId: string };
115
168
 
116
- export type VNextWorkflowRunResult = VNextWorkflowResult<any, any>;
169
+ export type WorkflowRunResult = WorkflowResult<any, any>;
117
170
  export interface UpsertVectorParams {
118
171
  indexName: string;
119
172
  vectors: number[][];
@@ -145,20 +198,33 @@ export interface GetVectorIndexResponse {
145
198
  }
146
199
 
147
200
  export interface SaveMessageToMemoryParams {
148
- messages: MessageType[];
201
+ messages: (MastraMessageV1 | MastraMessageV2)[];
149
202
  agentId: string;
150
203
  }
151
204
 
152
- export type SaveMessageToMemoryResponse = MessageType[];
205
+ export interface SaveNetworkMessageToMemoryParams {
206
+ messages: (MastraMessageV1 | MastraMessageV2)[];
207
+ networkId: string;
208
+ }
209
+
210
+ export type SaveMessageToMemoryResponse = (MastraMessageV1 | MastraMessageV2)[];
153
211
 
154
212
  export interface CreateMemoryThreadParams {
155
- title: string;
156
- metadata: Record<string, any>;
213
+ title?: string;
214
+ metadata?: Record<string, any>;
157
215
  resourceId: string;
158
- threadId: string;
216
+ threadId?: string;
159
217
  agentId: string;
160
218
  }
161
219
 
220
+ export interface CreateNetworkMemoryThreadParams {
221
+ title?: string;
222
+ metadata?: Record<string, any>;
223
+ resourceId: string;
224
+ threadId?: string;
225
+ networkId: string;
226
+ }
227
+
162
228
  export type CreateMemoryThreadResponse = StorageThreadType;
163
229
 
164
230
  export interface GetMemoryThreadParams {
@@ -166,6 +232,11 @@ export interface GetMemoryThreadParams {
166
232
  agentId: string;
167
233
  }
168
234
 
235
+ export interface GetNetworkMemoryThreadParams {
236
+ resourceId: string;
237
+ networkId: string;
238
+ }
239
+
169
240
  export type GetMemoryThreadResponse = StorageThreadType[];
170
241
 
171
242
  export interface UpdateMemoryThreadParams {
@@ -174,21 +245,52 @@ export interface UpdateMemoryThreadParams {
174
245
  resourceId: string;
175
246
  }
176
247
 
248
+ export interface GetMemoryThreadMessagesParams {
249
+ /**
250
+ * Limit the number of messages to retrieve (default: 40)
251
+ */
252
+ limit?: number;
253
+ }
254
+
255
+ export type GetMemoryThreadMessagesPaginatedParams = Omit<StorageGetMessagesArg, 'threadConfig' | 'threadId'>;
256
+
177
257
  export interface GetMemoryThreadMessagesResponse {
178
258
  messages: CoreMessage[];
179
259
  uiMessages: AiMessageType[];
180
260
  }
181
261
 
262
+ export type GetMemoryThreadMessagesPaginatedResponse = PaginationInfo & {
263
+ messages: MastraMessageV1[] | MastraMessageV2[];
264
+ };
265
+
182
266
  export interface GetLogsParams {
183
267
  transportId: string;
268
+ fromDate?: Date;
269
+ toDate?: Date;
270
+ logLevel?: LogLevel;
271
+ filters?: Record<string, string>;
272
+ page?: number;
273
+ perPage?: number;
184
274
  }
185
275
 
186
276
  export interface GetLogParams {
187
277
  runId: string;
188
278
  transportId: string;
279
+ fromDate?: Date;
280
+ toDate?: Date;
281
+ logLevel?: LogLevel;
282
+ filters?: Record<string, string>;
283
+ page?: number;
284
+ perPage?: number;
189
285
  }
190
286
 
191
- export type GetLogsResponse = BaseLogMessage[];
287
+ export type GetLogsResponse = {
288
+ logs: BaseLogMessage[];
289
+ total: number;
290
+ page: number;
291
+ perPage: number;
292
+ hasMore: boolean;
293
+ };
192
294
 
193
295
  export type RequestFunction = (path: string, options?: RequestOptions) => Promise<any>;
194
296
 
@@ -247,6 +349,7 @@ export interface GetTelemetryParams {
247
349
  }
248
350
 
249
351
  export interface GetNetworkResponse {
352
+ id: string;
250
353
  name: string;
251
354
  instructions: string;
252
355
  agents: Array<{
@@ -260,3 +363,140 @@ export interface GetNetworkResponse {
260
363
  };
261
364
  state?: Record<string, any>;
262
365
  }
366
+
367
+ export interface GetVNextNetworkResponse {
368
+ id: string;
369
+ name: string;
370
+ instructions: string;
371
+ agents: Array<{
372
+ name: string;
373
+ provider: string;
374
+ modelId: string;
375
+ }>;
376
+ routingModel: {
377
+ provider: string;
378
+ modelId: string;
379
+ };
380
+ workflows: Array<{
381
+ name: string;
382
+ description: string;
383
+ inputSchema: string | undefined;
384
+ outputSchema: string | undefined;
385
+ }>;
386
+ tools: Array<{
387
+ id: string;
388
+ description: string;
389
+ }>;
390
+ }
391
+
392
+ export interface GenerateVNextNetworkResponse {
393
+ task: string;
394
+ result: string;
395
+ resourceId: string;
396
+ resourceType: 'none' | 'tool' | 'agent' | 'workflow';
397
+ }
398
+
399
+ export interface GenerateOrStreamVNextNetworkParams {
400
+ message: string;
401
+ threadId?: string;
402
+ resourceId?: string;
403
+ runtimeContext?: RuntimeContext | Record<string, any>;
404
+ }
405
+
406
+ export interface LoopStreamVNextNetworkParams {
407
+ message: string;
408
+ threadId?: string;
409
+ resourceId?: string;
410
+ maxIterations?: number;
411
+ runtimeContext?: RuntimeContext | Record<string, any>;
412
+ }
413
+
414
+ export interface LoopVNextNetworkResponse {
415
+ status: 'success';
416
+ result: {
417
+ task: string;
418
+ resourceId: string;
419
+ resourceType: 'agent' | 'workflow' | 'none' | 'tool';
420
+ result: string;
421
+ iteration: number;
422
+ isOneOff: boolean;
423
+ prompt: string;
424
+ threadId?: string | undefined;
425
+ threadResourceId?: string | undefined;
426
+ isComplete?: boolean | undefined;
427
+ completionReason?: string | undefined;
428
+ };
429
+ steps: WorkflowResult<any, any>['steps'];
430
+ }
431
+
432
+ export interface McpServerListResponse {
433
+ servers: ServerInfo[];
434
+ next: string | null;
435
+ total_count: number;
436
+ }
437
+
438
+ export interface McpToolInfo {
439
+ id: string;
440
+ name: string;
441
+ description?: string;
442
+ inputSchema: string;
443
+ toolType?: MCPToolType;
444
+ }
445
+
446
+ export interface McpServerToolListResponse {
447
+ tools: McpToolInfo[];
448
+ }
449
+
450
+ export type ClientScoreRowData = Omit<ScoreRowData, 'createdAt' | 'updatedAt'> & {
451
+ createdAt: string;
452
+ updatedAt: string;
453
+ };
454
+
455
+ // Scores-related types
456
+ export interface GetScoresByRunIdParams {
457
+ runId: string;
458
+ page?: number;
459
+ perPage?: number;
460
+ }
461
+
462
+ export interface GetScoresByScorerIdParams {
463
+ scorerId: string;
464
+ entityId?: string;
465
+ entityType?: string;
466
+ page?: number;
467
+ perPage?: number;
468
+ }
469
+
470
+ export interface GetScoresByEntityIdParams {
471
+ entityId: string;
472
+ entityType: string;
473
+ page?: number;
474
+ perPage?: number;
475
+ }
476
+
477
+ export interface SaveScoreParams {
478
+ score: Omit<ScoreRowData, 'id' | 'createdAt' | 'updatedAt'>;
479
+ }
480
+
481
+ export interface GetScoresResponse {
482
+ pagination: {
483
+ total: number;
484
+ page: number;
485
+ perPage: number;
486
+ hasMore: boolean;
487
+ };
488
+ scores: ClientScoreRowData[];
489
+ }
490
+
491
+ export interface SaveScoreResponse {
492
+ score: ClientScoreRowData;
493
+ }
494
+
495
+ export type GetScorerResponse = MastraScorerEntry & {
496
+ agentIds: string[];
497
+ workflowIds: string[];
498
+ };
499
+
500
+ export interface GetScorersResponse {
501
+ scorers: Array<GetScorerResponse>;
502
+ }
@@ -0,0 +1,11 @@
1
+ import { RuntimeContext } from '@mastra/core/runtime-context';
2
+
3
+ export function parseClientRuntimeContext(runtimeContext?: RuntimeContext | Record<string, any>) {
4
+ if (runtimeContext) {
5
+ if (runtimeContext instanceof RuntimeContext) {
6
+ return Object.fromEntries(runtimeContext.entries());
7
+ }
8
+ return runtimeContext;
9
+ }
10
+ return undefined;
11
+ }
@@ -0,0 +1,32 @@
1
+ import { isVercelTool } from '@mastra/core/tools/is-vercel-tool';
2
+ import { zodToJsonSchema } from './zod-to-json-schema';
3
+ import type { ToolsInput } from '@mastra/core/agent';
4
+
5
+ export function processClientTools(clientTools: ToolsInput | undefined): ToolsInput | undefined {
6
+ if (!clientTools) {
7
+ return undefined;
8
+ }
9
+
10
+ return Object.fromEntries(
11
+ Object.entries(clientTools).map(([key, value]) => {
12
+ if (isVercelTool(value)) {
13
+ return [
14
+ key,
15
+ {
16
+ ...value,
17
+ parameters: value.parameters ? zodToJsonSchema(value.parameters) : undefined,
18
+ },
19
+ ];
20
+ } else {
21
+ return [
22
+ key,
23
+ {
24
+ ...value,
25
+ inputSchema: value.inputSchema ? zodToJsonSchema(value.inputSchema) : undefined,
26
+ outputSchema: value.outputSchema ? zodToJsonSchema(value.outputSchema) : undefined,
27
+ },
28
+ ];
29
+ }
30
+ }),
31
+ );
32
+ }
@@ -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
+ }
@@ -0,0 +1,180 @@
1
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
2
+ import type { MastraMessageV1, MastraMessageV2 } from '@mastra/core';
3
+ import { MastraClient } from './client';
4
+
5
+ describe('V2 Message Format Support', () => {
6
+ let client: MastraClient;
7
+ const agentId = 'test-agent';
8
+
9
+ beforeEach(() => {
10
+ global.fetch = vi.fn();
11
+ client = new MastraClient({
12
+ baseUrl: 'http://localhost:3000',
13
+ });
14
+ });
15
+
16
+ it('should send v1 messages successfully', async () => {
17
+ const v1Messages: MastraMessageV1[] = [
18
+ {
19
+ id: 'msg-v1-1',
20
+ role: 'user',
21
+ content: 'Hello from v1!',
22
+ type: 'text',
23
+ createdAt: new Date(),
24
+ threadId: 'thread-123',
25
+ resourceId: 'resource-123',
26
+ },
27
+ ];
28
+
29
+ (global.fetch as any).mockResolvedValueOnce({
30
+ ok: true,
31
+ json: async () => v1Messages,
32
+ });
33
+
34
+ const result = await client.saveMessageToMemory({
35
+ agentId,
36
+ messages: v1Messages,
37
+ });
38
+
39
+ expect(result).toEqual(v1Messages);
40
+ expect(global.fetch).toHaveBeenCalledWith(
41
+ expect.stringContaining('/api/memory/save-messages'),
42
+ expect.objectContaining({
43
+ method: 'POST',
44
+ body: JSON.stringify({ agentId, messages: v1Messages }),
45
+ }),
46
+ );
47
+ });
48
+
49
+ it('should send v2 messages successfully', async () => {
50
+ const v2Messages: MastraMessageV2[] = [
51
+ {
52
+ id: 'msg-v2-1',
53
+ role: 'assistant',
54
+ createdAt: new Date(),
55
+ threadId: 'thread-123',
56
+ resourceId: 'resource-123',
57
+ content: {
58
+ format: 2,
59
+ parts: [{ type: 'text', text: 'Hello from v2!' }],
60
+ content: 'Hello from v2!',
61
+ },
62
+ },
63
+ ];
64
+
65
+ (global.fetch as any).mockResolvedValueOnce({
66
+ ok: true,
67
+ json: async () => v2Messages,
68
+ });
69
+
70
+ const result = await client.saveMessageToMemory({
71
+ agentId,
72
+ messages: v2Messages,
73
+ });
74
+
75
+ expect(result).toEqual(v2Messages);
76
+ expect(global.fetch).toHaveBeenCalledWith(
77
+ expect.stringContaining('/api/memory/save-messages'),
78
+ expect.objectContaining({
79
+ method: 'POST',
80
+ body: JSON.stringify({ agentId, messages: v2Messages }),
81
+ }),
82
+ );
83
+ });
84
+
85
+ it('should send mixed v1 and v2 messages successfully', async () => {
86
+ const mixedMessages: (MastraMessageV1 | MastraMessageV2)[] = [
87
+ {
88
+ id: 'msg-v1-1',
89
+ role: 'user',
90
+ content: 'Question in v1 format',
91
+ type: 'text',
92
+ createdAt: new Date(),
93
+ threadId: 'thread-123',
94
+ resourceId: 'resource-123',
95
+ },
96
+ {
97
+ id: 'msg-v2-1',
98
+ role: 'assistant',
99
+ createdAt: new Date(),
100
+ threadId: 'thread-123',
101
+ resourceId: 'resource-123',
102
+ content: {
103
+ format: 2,
104
+ parts: [
105
+ { type: 'text', text: 'Answer in v2 format' },
106
+ {
107
+ type: 'tool-invocation',
108
+ toolInvocation: {
109
+ state: 'result' as const,
110
+ toolCallId: 'call-123',
111
+ toolName: 'calculator',
112
+ args: { a: 1, b: 2 },
113
+ result: '3',
114
+ },
115
+ },
116
+ ],
117
+ toolInvocations: [
118
+ {
119
+ state: 'result' as const,
120
+ toolCallId: 'call-123',
121
+ toolName: 'calculator',
122
+ args: { a: 1, b: 2 },
123
+ result: '3',
124
+ },
125
+ ],
126
+ },
127
+ },
128
+ ];
129
+
130
+ (global.fetch as any).mockResolvedValueOnce({
131
+ ok: true,
132
+ json: async () => mixedMessages,
133
+ });
134
+
135
+ const result = await client.saveMessageToMemory({
136
+ agentId,
137
+ messages: mixedMessages,
138
+ });
139
+
140
+ expect(result).toEqual(mixedMessages);
141
+ expect(global.fetch).toHaveBeenCalledWith(
142
+ expect.stringContaining('/api/memory/save-messages'),
143
+ expect.objectContaining({
144
+ method: 'POST',
145
+ body: JSON.stringify({ agentId, messages: mixedMessages }),
146
+ }),
147
+ );
148
+ });
149
+
150
+ it('should handle v2 messages with attachments', async () => {
151
+ const v2MessageWithAttachments: MastraMessageV2 = {
152
+ id: 'msg-v2-att',
153
+ role: 'user',
154
+ createdAt: new Date(),
155
+ threadId: 'thread-123',
156
+ resourceId: 'resource-123',
157
+ content: {
158
+ format: 2,
159
+ parts: [
160
+ { type: 'text', text: 'Check out this image:' },
161
+ { type: 'file', data: 'data:image/png;base64,iVBORw0...', mimeType: 'image/png' },
162
+ ],
163
+ experimental_attachments: [{ url: 'data:image/png;base64,iVBORw0...', contentType: 'image/png' }],
164
+ },
165
+ };
166
+
167
+ (global.fetch as any).mockResolvedValueOnce({
168
+ ok: true,
169
+ json: async () => [v2MessageWithAttachments],
170
+ });
171
+
172
+ const result = await client.saveMessageToMemory({
173
+ agentId,
174
+ messages: [v2MessageWithAttachments],
175
+ });
176
+
177
+ expect(result).toHaveLength(1);
178
+ expect(result[0]).toEqual(v2MessageWithAttachments);
179
+ });
180
+ });
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": ["./tsconfig.json", "../../tsconfig.build.json"],
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ "rootDir": "./src"
6
+ },
7
+ "include": ["src/**/*"],
8
+ "exclude": ["node_modules", "**/*.test.ts", "src/**/*.mock.ts"]
9
+ }
package/tsconfig.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "extends": "../../tsconfig.node.json",
3
- "include": ["src/**/*"],
3
+ "include": ["src/**/*", "tsup.config.ts"],
4
4
  "exclude": ["node_modules", "**/*.test.ts"]
5
5
  }
package/tsup.config.ts ADDED
@@ -0,0 +1,22 @@
1
+ import { spawn } from 'child_process';
2
+ import { promisify } from 'util';
3
+ import { defineConfig } from 'tsup';
4
+
5
+ const exec = promisify(spawn);
6
+
7
+ export default defineConfig({
8
+ entry: ['src/index.ts'],
9
+ format: ['esm', 'cjs'],
10
+ clean: true,
11
+ dts: false,
12
+ splitting: true,
13
+ treeshake: {
14
+ preset: 'smallest',
15
+ },
16
+ sourcemap: true,
17
+ onSuccess: async () => {
18
+ await exec('pnpm', ['tsc', '-p', 'tsconfig.build.json'], {
19
+ stdio: 'inherit',
20
+ });
21
+ },
22
+ });