mastra 0.1.57-alpha.88 → 0.1.57-alpha.89

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.
@@ -2,6 +2,10 @@ import { Deployer } from '../deployer.js';
2
2
  export declare class CloudflareDeployer extends Deployer {
3
3
  name: string;
4
4
  installCli(): Promise<void>;
5
+ build({ dir }: {
6
+ dir: string;
7
+ useBanner: boolean;
8
+ }): Promise<void>;
5
9
  writePkgJson(): Promise<void>;
6
10
  writeFiles(): void;
7
11
  deployCommand({ scope }: {
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/commands/deploy/cloudflare/index.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAG1C,qBAAa,kBAAmB,SAAQ,QAAQ;IAC9C,IAAI,SAAgB;IAEd,UAAU;IAMV,YAAY;IA2BlB,UAAU,IAAI,IAAI;IA+BZ,aAAa,CAAC,EAAE,KAAK,EAAE,EAAE;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC;CAajE"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/commands/deploy/cloudflare/index.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAG1C,qBAAa,kBAAmB,SAAQ,QAAQ;IAC9C,IAAI,SAAgB;IAEd,UAAU;IAMV,KAAK,CAAC,EAAE,GAAG,EAAE,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,OAAO,CAAA;KAAE;IAIlD,YAAY;IA2ClB,UAAU,IAAI,IAAI;IAgCZ,aAAa,CAAC,EAAE,KAAK,EAAE,EAAE;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC;CAajE"}
@@ -1,5 +1,5 @@
1
1
  import { execa } from 'execa';
2
- import { writeFileSync } from 'fs';
2
+ import { readFileSync, writeFileSync } from 'fs';
3
3
  import { join } from 'path';
4
4
  import { DepsService } from '../../../services/service.deps.js';
5
5
  import { Deployer } from '../deployer.js';
@@ -14,7 +14,25 @@ export class CloudflareDeployer extends Deployer {
14
14
  const depsService = new DepsService();
15
15
  await depsService.installPackages(['wrangler -g']);
16
16
  }
17
+ async build({ dir }) {
18
+ super.build({ dir, useBanner: false });
19
+ }
17
20
  async writePkgJson() {
21
+ let projectPkg = {
22
+ dependencies: {},
23
+ };
24
+ try {
25
+ projectPkg = JSON.parse(readFileSync(join(this.dotMastraPath, '..', 'package.json'), 'utf-8'));
26
+ }
27
+ catch (_e) {
28
+ // no-op
29
+ }
30
+ const mastraDeps = Object.entries(projectPkg.dependencies)
31
+ .filter(([name]) => name.startsWith('@mastra'))
32
+ .reduce((acc, [name, version]) => {
33
+ acc[name] = version;
34
+ return acc;
35
+ }, {});
18
36
  writeFileSync(join(this.dotMastraPath, 'package.json'), JSON.stringify({
19
37
  name: 'server',
20
38
  version: '1.0.0',
@@ -26,7 +44,7 @@ export class CloudflareDeployer extends Deployer {
26
44
  author: '',
27
45
  license: 'ISC',
28
46
  dependencies: {
29
- '@mastra/core': '0.1.27-alpha.18',
47
+ ...mastraDeps,
30
48
  'itty-router': '5.0.18',
31
49
  superjson: '^2.2.2',
32
50
  'zod-to-json-schema': '^3.24.1',
@@ -37,26 +55,27 @@ export class CloudflareDeployer extends Deployer {
37
55
  const envVars = this.getEnvVars();
38
56
  // TODO ENV KEYS
39
57
  writeFileSync(join(this.dotMastraPath, 'wrangler.toml'), `
40
- name = "mastra"
41
- main = "index.mjs" # Your main worker file
42
- compatibility_date = "2024-12-02"
43
- compatibility_flags = ["nodejs_compat"]
44
-
45
- [build]
46
- command = "npm install"
47
-
48
- [[build.upload]]
49
- type = "javascript_module"
50
- main = "mastra.mjs"
51
-
52
- [observability.logs]
53
- enabled = true
54
-
55
- [vars]
56
- ${Object.entries(envVars || {})
58
+ name = "mastra"
59
+ main = "index.mjs" # Your main worker file
60
+ compatibility_date = "2024-12-02"
61
+ compatibility_flags = ["nodejs_compat"]
62
+ find_additional_modules = true
63
+
64
+ [build]
65
+ command = "npm install"
66
+
67
+ [[build.upload]]
68
+ type = "javascript_module"
69
+ main = "mastra.mjs"
70
+
71
+ [observability.logs]
72
+ enabled = true
73
+
74
+ [vars]
75
+ ${Object.entries(envVars || {})
57
76
  ?.map(([key, value]) => `${key} = "${value}"`)
58
77
  .join('\n')}
59
- `),
78
+ `),
60
79
  writeFileSync(join(this.dotMastraPath, 'index.mjs'), WORKER);
61
80
  }
62
81
  async deployCommand({ scope }) {
@@ -10,8 +10,9 @@ export declare abstract class Deployer {
10
10
  protected getEnvFiles(): string[];
11
11
  getEnvVars(): Record<string, string>;
12
12
  protected parseEnvFile(filePath: string): string[];
13
- build({ dir }: {
13
+ build({ dir, useBanner }: {
14
14
  dir: string;
15
+ useBanner?: boolean;
15
16
  }): Promise<void>;
16
17
  writePkgJson(): void;
17
18
  writeFiles(): void;
@@ -1 +1 @@
1
- {"version":3,"file":"deployer.d.ts","sourceRoot":"","sources":["../../../src/commands/deploy/deployer.ts"],"names":[],"mappings":"AAMA,8BAAsB,QAAQ;IAC5B,KAAK,EAAE,MAAM,CAAC;IACd,aAAa,EAAE,MAAM,CAAC;IACtB,IAAI,EAAE,MAAM,CAAM;gBAEN,EAAE,KAAK,EAAE,EAAE;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE;IAMlC,UAAU;IAIV,OAAO;IASb,SAAS,CAAC,WAAW,IAAI,MAAM,EAAE;IAOjC,UAAU;IAkBV,SAAS,CAAC,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,EAAE;IAS5C,KAAK,CAAC,EAAE,GAAG,EAAE,EAAE;QAAE,GAAG,EAAE,MAAM,CAAA;KAAE;IAQpC,YAAY;IAIZ,UAAU;IAIJ,aAAa,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAC;QAAC,WAAW,CAAC,EAAE,MAAM,CAAA;KAAE;IAItG,MAAM,CAAC,EACX,KAAK,EACL,MAAM,EACN,GAAG,EACH,WAAW,GACZ,EAAE;QACD,GAAG,CAAC,EAAE,MAAM,CAAC;QACb,KAAK,EAAE,MAAM,CAAC;QACd,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,WAAW,CAAC,EAAE,MAAM,CAAC;KACtB;CAUF"}
1
+ {"version":3,"file":"deployer.d.ts","sourceRoot":"","sources":["../../../src/commands/deploy/deployer.ts"],"names":[],"mappings":"AAMA,8BAAsB,QAAQ;IAC5B,KAAK,EAAE,MAAM,CAAC;IACd,aAAa,EAAE,MAAM,CAAC;IACtB,IAAI,EAAE,MAAM,CAAM;gBAEN,EAAE,KAAK,EAAE,EAAE;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE;IAMlC,UAAU;IAIV,OAAO;IASb,SAAS,CAAC,WAAW,IAAI,MAAM,EAAE;IAOjC,UAAU;IAkBV,SAAS,CAAC,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,EAAE;IAS5C,KAAK,CAAC,EAAE,GAAG,EAAE,SAAgB,EAAE,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,SAAS,CAAC,EAAE,OAAO,CAAA;KAAE;IAQ3E,YAAY;IAIZ,UAAU;IAIJ,aAAa,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAC;QAAC,WAAW,CAAC,EAAE,MAAM,CAAA;KAAE;IAItG,MAAM,CAAC,EACX,KAAK,EACL,MAAM,EACN,GAAG,EACH,WAAW,GACZ,EAAE;QACD,GAAG,CAAC,EAAE,MAAM,CAAC;QACb,KAAK,EAAE,MAAM,CAAC;QACd,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,WAAW,CAAC,EAAE,MAAM,CAAC;KACtB;CAUF"}
@@ -49,11 +49,11 @@ export class Deployer {
49
49
  .filter(line => line && !line.startsWith('#'))
50
50
  .filter(line => line.includes('=')); // Only include valid KEY=value pairs
51
51
  }
52
- async build({ dir }) {
52
+ async build({ dir, useBanner = true }) {
53
53
  if (!existsSync(this.dotMastraPath)) {
54
54
  mkdirSync(this.dotMastraPath);
55
55
  }
56
- await bundle(dir);
56
+ await bundle(dir, { useBanner });
57
57
  }
58
58
  writePkgJson() {
59
59
  console.log('Writing package.json...');
@@ -1 +1 @@
1
- {"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../../src/templates/worker.ts"],"names":[],"mappings":"AA0CA,UAAU,gBAAgB;IACxB,SAAS,CAAC,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC;IACvC,sBAAsB,IAAI,IAAI,CAAC;CAChC;;mBAshCsB,OAAO,OAAO,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,OAAO,gBAAgB;;AADlF,wBAQE"}
1
+ {"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../../src/templates/worker.ts"],"names":[],"mappings":"AA8CA,UAAU,gBAAgB;IACxB,SAAS,CAAC,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC;IACvC,sBAAsB,IAAI,IAAI,CAAC;CAChC;;mBA4hCsB,OAAO,OAAO,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,OAAO,gBAAgB;;AADlF,wBAQE"}
@@ -1,9 +1,11 @@
1
1
  import { AutoRouter } from 'itty-router';
2
- import { join } from 'path';
2
+ // import { join } from 'path';
3
3
  import { stringify } from 'superjson';
4
4
  import { pathToFileURL } from 'url';
5
5
  import zodToJsonSchema from 'zod-to-json-schema';
6
- const { mastra } = await import(join(process.cwd(), 'mastra.mjs'));
6
+ // // @ts-ignore
7
+ // import { mastra } from './mastra.mjs';
8
+ // const { mastra } = await import(join(process.cwd(), 'mastra.mjs'));
7
9
  const mastraToolsPaths = process.env.MASTRA_TOOLS_PATH;
8
10
  const toolImports = mastraToolsPaths
9
11
  ? await Promise.all(mastraToolsPaths.split(',').map(async (toolPath) => {
@@ -16,7 +18,13 @@ const tools = toolImports.reduce((acc, toolModule) => {
16
18
  });
17
19
  return acc;
18
20
  }, {});
21
+ async function addMastra(request) {
22
+ // @ts-ignore
23
+ const { mastra } = await import('./mastra.mjs');
24
+ request.mastra = mastra;
25
+ }
19
26
  const router = AutoRouter();
27
+ router.all('*', addMastra);
20
28
  const validateBody = async (body) => {
21
29
  const errorResponse = Object.entries(body).reduce((acc, [key, value]) => {
22
30
  if (!value) {
@@ -34,7 +42,7 @@ router.get('/', () => {
34
42
  headers: { 'Content-Type': 'text/plain' },
35
43
  });
36
44
  });
37
- router.get('/api/agents', async () => {
45
+ router.get('/api/agents', async ({ mastra }) => {
38
46
  try {
39
47
  const agents = mastra.getAgents();
40
48
  const serializedAgents = Object.entries(agents).reduce((acc, [_id, _agent]) => {
@@ -71,7 +79,7 @@ router.get('/api/agents', async () => {
71
79
  });
72
80
  }
73
81
  });
74
- router.get('/api/agents/:agentId', ({ params }) => {
82
+ router.get('/api/agents/:agentId', ({ params, mastra }) => {
75
83
  try {
76
84
  const agentId = decodeURIComponent(params.agentId);
77
85
  const agent = mastra.getAgent(agentId);
@@ -104,7 +112,7 @@ router.get('/api/agents/:agentId', ({ params }) => {
104
112
  });
105
113
  }
106
114
  });
107
- router.post('/api/agents/:agentId/generate', async ({ params, json }) => {
115
+ router.post('/api/agents/:agentId/generate', async ({ params, json, mastra }) => {
108
116
  try {
109
117
  const agentId = decodeURIComponent(params.agentId);
110
118
  const agent = mastra.getAgent(agentId);
@@ -144,7 +152,7 @@ router.post('/api/agents/:agentId/generate', async ({ params, json }) => {
144
152
  });
145
153
  }
146
154
  });
147
- router.post('/api/agents/:agentId/stream', async ({ params, json }) => {
155
+ router.post('/api/agents/:agentId/stream', async ({ params, json, mastra }) => {
148
156
  try {
149
157
  const agentId = decodeURIComponent(params.agentId);
150
158
  const agent = mastra.getAgent(agentId);
@@ -186,7 +194,7 @@ router.post('/api/agents/:agentId/stream', async ({ params, json }) => {
186
194
  });
187
195
  }
188
196
  });
189
- router.post('/api/agents/:agentId/text-object', async ({ params, json }) => {
197
+ router.post('/api/agents/:agentId/text-object', async ({ params, json, mastra }) => {
190
198
  try {
191
199
  const agentId = decodeURIComponent(params.agentId);
192
200
  const agent = mastra.getAgent(agentId);
@@ -229,7 +237,7 @@ router.post('/api/agents/:agentId/text-object', async ({ params, json }) => {
229
237
  });
230
238
  }
231
239
  });
232
- router.post('/api/agents/:agentId/stream-object', async ({ params, json }) => {
240
+ router.post('/api/agents/:agentId/stream-object', async ({ params, json, mastra }) => {
233
241
  try {
234
242
  const agentId = decodeURIComponent(params.agentId);
235
243
  const agent = mastra.getAgent(agentId);
@@ -274,7 +282,7 @@ router.post('/api/agents/:agentId/stream-object', async ({ params, json }) => {
274
282
  });
275
283
  }
276
284
  });
277
- router.post('/api/agents/:agentId/tools/:toolId/execute', async ({ params, json }) => {
285
+ router.post('/api/agents/:agentId/tools/:toolId/execute', async ({ params, json, mastra }) => {
278
286
  try {
279
287
  const agentId = decodeURIComponent(params.agentId);
280
288
  const toolId = decodeURIComponent(params.toolId);
@@ -305,7 +313,7 @@ router.post('/api/agents/:agentId/tools/:toolId/execute', async ({ params, json
305
313
  });
306
314
  }
307
315
  });
308
- router.get('/api/workflows', async () => {
316
+ router.get('/api/workflows', async ({ mastra }) => {
309
317
  try {
310
318
  const workflows = mastra.getWorkflows({ serialized: true });
311
319
  return new Response(JSON.stringify(workflows), {
@@ -325,7 +333,7 @@ router.get('/api/workflows', async () => {
325
333
  });
326
334
  }
327
335
  });
328
- router.get('/api/workflows/:workflowId', async ({ params }) => {
336
+ router.get('/api/workflows/:workflowId', async ({ params, mastra }) => {
329
337
  try {
330
338
  const workflowId = decodeURIComponent(params.workflowId);
331
339
  const workflow = mastra.getWorkflow(workflowId);
@@ -364,7 +372,7 @@ router.get('/api/workflows/:workflowId', async ({ params }) => {
364
372
  });
365
373
  }
366
374
  });
367
- router.post('/workflows/:workflowId/execute', async ({ params, json }) => {
375
+ router.post('/workflows/:workflowId/execute', async ({ params, json, mastra }) => {
368
376
  try {
369
377
  const workflowId = decodeURIComponent(params.workflowId);
370
378
  const workflow = mastra.getWorkflow(workflowId);
@@ -387,7 +395,7 @@ router.post('/workflows/:workflowId/execute', async ({ params, json }) => {
387
395
  });
388
396
  }
389
397
  });
390
- router.get('/api/memory/status', async () => {
398
+ router.get('/api/memory/status', async ({ mastra }) => {
391
399
  try {
392
400
  const memory = mastra.memory;
393
401
  if (!memory) {
@@ -414,7 +422,7 @@ router.get('/api/memory/status', async () => {
414
422
  });
415
423
  }
416
424
  });
417
- router.get('/api/memory/threads', async ({ query }) => {
425
+ router.get('/api/memory/threads', async ({ query, mastra }) => {
418
426
  try {
419
427
  const { resourceid } = query;
420
428
  const memory = mastra.memory;
@@ -444,7 +452,7 @@ router.get('/api/memory/threads', async ({ query }) => {
444
452
  });
445
453
  }
446
454
  });
447
- router.get('/api/memory/threads/:threadId', async ({ params }) => {
455
+ router.get('/api/memory/threads/:threadId', async ({ params, mastra }) => {
448
456
  try {
449
457
  const threadId = decodeURIComponent(params.threadId);
450
458
  const memory = mastra.memory;
@@ -482,7 +490,7 @@ router.get('/api/memory/threads/:threadId', async ({ params }) => {
482
490
  });
483
491
  }
484
492
  });
485
- router.post('/api/memory/threads', async ({ json }) => {
493
+ router.post('/api/memory/threads', async ({ json, mastra }) => {
486
494
  try {
487
495
  const memory = mastra.memory;
488
496
  const { title, metadata, resourceid, threadId } = await json();
@@ -521,7 +529,7 @@ router.post('/api/memory/threads', async ({ json }) => {
521
529
  });
522
530
  }
523
531
  });
524
- router.patch('/api/memory/threads/:threadId', async ({ params, json }) => {
532
+ router.patch('/api/memory/threads/:threadId', async ({ params, json, mastra }) => {
525
533
  try {
526
534
  const threadId = decodeURIComponent(params.threadId);
527
535
  const memory = mastra.memory;
@@ -570,7 +578,7 @@ router.patch('/api/memory/threads/:threadId', async ({ params, json }) => {
570
578
  });
571
579
  }
572
580
  });
573
- router.delete('/api/memory/threads/:threadId', async ({ params }) => {
581
+ router.delete('/api/memory/threads/:threadId', async ({ params, mastra }) => {
574
582
  try {
575
583
  const threadId = decodeURIComponent(params.threadId);
576
584
  const memory = mastra.memory;
@@ -609,7 +617,7 @@ router.delete('/api/memory/threads/:threadId', async ({ params }) => {
609
617
  });
610
618
  }
611
619
  });
612
- router.get('/api/memory/threads/:threadId/messages', async ({ params }) => {
620
+ router.get('/api/memory/threads/:threadId/messages', async ({ params, mastra }) => {
613
621
  try {
614
622
  const threadId = decodeURIComponent(params.threadId);
615
623
  const memory = mastra.memory;
@@ -648,7 +656,7 @@ router.get('/api/memory/threads/:threadId/messages', async ({ params }) => {
648
656
  });
649
657
  }
650
658
  });
651
- router.get('/api/memory/threads/:threadId/context-window', async ({ params, query }) => {
659
+ router.get('/api/memory/threads/:threadId/context-window', async ({ params, query, mastra }) => {
652
660
  try {
653
661
  const threadId = decodeURIComponent(params.threadId);
654
662
  const { startDate, endDate, format } = query;
@@ -688,7 +696,7 @@ router.get('/api/memory/threads/:threadId/context-window', async ({ params, quer
688
696
  });
689
697
  }
690
698
  });
691
- router.post('/api/memory/save-messages', async ({ json }) => {
699
+ router.post('/api/memory/save-messages', async ({ json, mastra }) => {
692
700
  try {
693
701
  const memory = mastra.memory;
694
702
  const { messages } = await json();
@@ -742,7 +750,7 @@ router.post('/api/memory/save-messages', async ({ json }) => {
742
750
  });
743
751
  }
744
752
  });
745
- router.post('/api/memory/threads/:threadId/tool-result', async ({ params, json }) => {
753
+ router.post('/api/memory/threads/:threadId/tool-result', async ({ params, json, mastra }) => {
746
754
  try {
747
755
  const threadId = decodeURIComponent(params.threadId);
748
756
  const memory = mastra.memory;
@@ -791,7 +799,7 @@ router.post('/api/memory/threads/:threadId/tool-result', async ({ params, json }
791
799
  });
792
800
  }
793
801
  });
794
- router.post('/api/memory/validate-tool-call-args', async ({ json }) => {
802
+ router.post('/api/memory/validate-tool-call-args', async ({ json, mastra }) => {
795
803
  try {
796
804
  const memory = mastra.memory;
797
805
  const { hashedArgs } = await json();
@@ -830,7 +838,7 @@ router.post('/api/memory/validate-tool-call-args', async ({ json }) => {
830
838
  });
831
839
  }
832
840
  });
833
- router.post('/api/syncs/:syncId/execute', async ({ params, json }) => {
841
+ router.post('/api/syncs/:syncId/execute', async ({ params, json, mastra }) => {
834
842
  try {
835
843
  const syncId = decodeURIComponent(params.syncId);
836
844
  const { runId, params: syncParams } = await json();
@@ -861,7 +869,7 @@ router.post('/api/syncs/:syncId/execute', async ({ params, json }) => {
861
869
  });
862
870
  }
863
871
  });
864
- router.get('/api/logs', async () => {
872
+ router.get('/api/logs', async ({ mastra }) => {
865
873
  try {
866
874
  const logs = await mastra.getLogs();
867
875
  return new Response(JSON.stringify(logs), {
@@ -881,7 +889,7 @@ router.get('/api/logs', async () => {
881
889
  });
882
890
  }
883
891
  });
884
- router.get('/api/logs/:runId', async ({ params }) => {
892
+ router.get('/api/logs/:runId', async ({ params, mastra }) => {
885
893
  try {
886
894
  const runId = decodeURIComponent(params.runId);
887
895
  const logs = await mastra.getLogsByRunId(runId);
@@ -951,7 +959,7 @@ router.get('/api/tools/:toolId', async ({ params }) => {
951
959
  });
952
960
  }
953
961
  });
954
- router.post('/api/tools/:toolId/execute', async ({ params, json }) => {
962
+ router.post('/api/tools/:toolId/execute', async ({ params, json, mastra }) => {
955
963
  try {
956
964
  const toolId = decodeURIComponent(params.toolId);
957
965
  const tool = Object.values(tools || {}).find((tool) => tool.id === toolId);
@@ -20,30 +20,15 @@ export declare function bundleServer(entryPoint: string): Promise<esbuild.BuildR
20
20
  logOverride: {
21
21
  'commonjs-variable-in-esm': "silent";
22
22
  };
23
+ plugins: {
24
+ name: string;
25
+ setup(build: any): void;
26
+ }[];
23
27
  }>>;
24
- export declare function bundle(dirPath: string, options?: {
28
+ export declare function bundle(dirPath: string, { useBanner, ...options }?: {
25
29
  outfile?: string;
26
30
  entryFile?: string;
27
31
  buildName?: string;
28
- }): Promise<esbuild.BuildResult<{
29
- entryPoints: string[];
30
- bundle: true;
31
- platform: "node";
32
- format: "esm";
33
- outfile: string;
34
- target: string;
35
- sourcemap: true;
36
- minify: true;
37
- metafile: true;
38
- logLevel: "error";
39
- mainFields: string[];
40
- conditions: string[];
41
- banner: {
42
- js: string;
43
- };
44
- logOverride: {
45
- 'commonjs-variable-in-esm': "silent";
46
- };
47
- external: string[];
48
- }>>;
32
+ useBanner?: boolean;
33
+ }): Promise<esbuild.BuildResult<esbuild.BuildOptions>>;
49
34
  //# sourceMappingURL=bundle.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"bundle.d.ts","sourceRoot":"","sources":["../../src/utils/bundle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AAanC,wBAAsB,YAAY,CAAC,UAAU,EAAE,MAAM;;;;;;;;;;;;;;;;;;;;;IAwFpD;AAED,wBAAsB,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;IAAE,OAAO,CAAC,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAAE;;;;;;;;;;;;;;;;;;;;IAuGnH"}
1
+ {"version":3,"file":"bundle.d.ts","sourceRoot":"","sources":["../../src/utils/bundle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AAcnC,wBAAsB,YAAY,CAAC,UAAU,EAAE,MAAM;;;;;;;;;;;;;;;;;;;;;;;qBAYlC,GAAG;;IAyGrB;AAED,wBAAsB,MAAM,CAC1B,OAAO,EAAE,MAAM,EACf,EACE,SAAgB,EAChB,GAAG,OAAO,EACX,GAAE;IAAE,OAAO,CAAC,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,OAAO,CAAA;CAAO,sDA4G1F"}
@@ -13,6 +13,27 @@ export async function bundleServer(entryPoint) {
13
13
  upsertMastraDir();
14
14
  const outfile = join(process.cwd(), '.mastra', 'server.mjs');
15
15
  const cliNodeModules = join(path.dirname(path.dirname(__dirname)), 'node_modules');
16
+ let missingMastraDependency = false;
17
+ const externalIfMissingPlugin = {
18
+ name: 'external-if-missing',
19
+ setup(build) {
20
+ build.onResolve({ filter: /^.*$/ }, (args) => {
21
+ if (!args.importer.endsWith('.mastra/index.mjs'))
22
+ return;
23
+ try {
24
+ const resolvedPath = import.meta.resolve(args.path);
25
+ if (!resolvedPath || resolvedPath.includes('_npx/')) {
26
+ missingMastraDependency = true;
27
+ return { path: args.path, external: true };
28
+ }
29
+ }
30
+ catch (e) {
31
+ missingMastraDependency = true;
32
+ return { path: args.path, external: true };
33
+ }
34
+ });
35
+ },
36
+ };
16
37
  const result = await esbuild.build({
17
38
  entryPoints: [entryPoint],
18
39
  bundle: true,
@@ -76,7 +97,12 @@ export async function bundleServer(entryPoint) {
76
97
  logOverride: {
77
98
  'commonjs-variable-in-esm': 'silent',
78
99
  },
100
+ plugins: [externalIfMissingPlugin],
79
101
  });
102
+ if (missingMastraDependency) {
103
+ console.error(`Missing Mastra dependency. Please install the mastra package in your project or globally using npm i -g mastra`);
104
+ process.exit(1);
105
+ }
80
106
  // Output build metadata
81
107
  await esbuild.analyzeMetafile(result.metafile);
82
108
  return result;
@@ -93,14 +119,14 @@ export async function bundleServer(entryPoint) {
93
119
  process.exit(1);
94
120
  }
95
121
  }
96
- export async function bundle(dirPath, options) {
122
+ export async function bundle(dirPath, { useBanner = true, ...options } = {}) {
97
123
  try {
98
124
  // Ensure .mastra directory exists
99
125
  upsertMastraDir();
100
126
  const fileService = new FileService();
101
127
  const entryPoint = fileService.getFirstExistingFile([join(dirPath, `${options?.entryFile || 'index'}.ts`)]);
102
128
  const outfile = options?.outfile || join(process.cwd(), '.mastra', 'mastra.mjs');
103
- const result = await esbuild.build({
129
+ const esbuildConfig = {
104
130
  entryPoints: [entryPoint],
105
131
  bundle: true,
106
132
  platform: 'node',
@@ -113,16 +139,6 @@ export async function bundle(dirPath, options) {
113
139
  logLevel: 'error',
114
140
  mainFields: ['module', 'main'],
115
141
  conditions: ['import', 'node'],
116
- banner: {
117
- js: `
118
- import { createRequire } from "module";
119
- import { fileURLToPath } from 'url';
120
- import path from 'path';
121
- const require = createRequire(import.meta.url);
122
- const __filename = fileURLToPath(import.meta.url);
123
- const __dirname = path.dirname(__filename);
124
- `,
125
- },
126
142
  logOverride: {
127
143
  'commonjs-variable-in-esm': 'silent',
128
144
  },
@@ -174,7 +190,19 @@ export async function bundle(dirPath, options) {
174
190
  '@mastra/rag',
175
191
  '@mastra/stabilityai',
176
192
  ],
177
- });
193
+ };
194
+ if (useBanner) {
195
+ esbuildConfig.banner = {
196
+ js: `
197
+ import { createRequire } from "module";
198
+ import { fileURLToPath } from 'url';
199
+ import path from 'path';
200
+ const require = createRequire(String(pathToFileURL(__filename)));
201
+ const __dirname = path.dirname(__filename);
202
+ `,
203
+ };
204
+ }
205
+ const result = await esbuild.build(esbuildConfig);
178
206
  // Log build results
179
207
  logger.success(`${options?.buildName} Build completed successfully`);
180
208
  // Output build metadata
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mastra",
3
- "version": "0.1.57-alpha.88",
3
+ "version": "0.1.57-alpha.89",
4
4
  "license": "MIT",
5
5
  "description": "cli for mastra",
6
6
  "type": "module",
@@ -121,7 +121,7 @@
121
121
  "build": "npm-run-all build:tsc copy-starter-files build:playground",
122
122
  "build:tsc": "tsc",
123
123
  "build:dev": "npx tsc --watch",
124
- "copy-starter-files": "cpy src/starter-files dist/",
124
+ "copy-starter-files": "cpy 'src/starter-files/**/*' dist/starter-files",
125
125
  "build:playground": "cd src/playground && vite build",
126
126
  "dev:playground": "cd src/playground && vite",
127
127
  "init": "npx tsx src/index.ts init",