@mastra/core 0.2.0-alpha.99 → 0.2.0

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 (84) hide show
  1. package/dist/agent/index.d.ts +6 -8
  2. package/dist/agent/index.js +15 -7
  3. package/dist/{telemetry-oCUM52DG.d.ts → base-BbtPAA6f.d.ts} +50 -8
  4. package/dist/{index-Cwb-5AzX.d.ts → base-Bpb7Dmwe.d.ts} +367 -320
  5. package/dist/base.d.ts +3 -43
  6. package/dist/base.js +3 -3
  7. package/dist/bundler/index.d.ts +3 -4
  8. package/dist/bundler/index.js +4 -4
  9. package/dist/{chunk-MCB4M5W4.js → chunk-22LC46YN.js} +3 -9
  10. package/dist/{chunk-A7SNFYQB.js → chunk-2JL6DQMZ.js} +20 -28
  11. package/dist/chunk-2SAHBQEF.js +3 -0
  12. package/dist/chunk-3HBFW3Q7.js +24 -0
  13. package/dist/{chunk-KNPBNSJ7.js → chunk-55GTEVHJ.js} +12 -13
  14. package/dist/chunk-65VPTVVP.js +218 -0
  15. package/dist/chunk-AWEACB2T.js +66 -0
  16. package/dist/chunk-C6A6W6XS.js +49 -0
  17. package/dist/chunk-FGZVE4CM.js +404 -0
  18. package/dist/{chunk-DHCULRJM.js → chunk-J3W3IHDO.js} +114 -88
  19. package/dist/chunk-K36NSQWH.js +10 -0
  20. package/dist/{chunk-EO3TIPGQ.js → chunk-K4DFI76V.js} +382 -370
  21. package/dist/{chunk-42DYOLDV.js → chunk-MEISIZMP.js} +13 -21
  22. package/dist/chunk-MLWGYRJR.js +87 -0
  23. package/dist/{chunk-ICMEXHKD.js → chunk-O2VP5JBC.js} +48 -55
  24. package/dist/{chunk-ZJOMHCWE.js → chunk-OJ26F3J4.js} +98 -153
  25. package/dist/chunk-RG66XEJT.js +8 -0
  26. package/dist/chunk-SB37QG7O.js +1203 -0
  27. package/dist/chunk-SDBM53G4.js +32 -0
  28. package/dist/{chunk-4LJFWC2Q.js → chunk-SIFBBGY6.js} +59 -85
  29. package/dist/chunk-U6J2FOU4.js +624 -0
  30. package/dist/chunk-VB7CO5ND.js +31 -0
  31. package/dist/{chunk-C55JWGDU.js → chunk-ZJOXJFJI.js} +43 -15
  32. package/dist/deployer/index.d.ts +2 -4
  33. package/dist/deployer/index.js +5 -5
  34. package/dist/eval/index.d.ts +8 -13
  35. package/dist/eval/index.js +3 -3
  36. package/dist/filter/index.js +2 -2
  37. package/dist/hooks/index.d.ts +13 -18
  38. package/dist/hooks/index.js +2 -2
  39. package/dist/{index-CBZ2mk2H.d.ts → index-B2JCcAQt.d.ts} +1 -1
  40. package/dist/index.d.ts +15 -15
  41. package/dist/index.js +43 -69
  42. package/dist/integration/index.d.ts +8 -10
  43. package/dist/integration/index.js +6 -3
  44. package/dist/llm/index.d.ts +6 -8
  45. package/dist/llm/index.js +1 -1
  46. package/dist/logger/index.d.ts +1 -1
  47. package/dist/logger/index.js +2 -2
  48. package/dist/mastra/index.d.ts +10 -13
  49. package/dist/mastra/index.js +20 -9
  50. package/dist/memory/index.d.ts +8 -10
  51. package/dist/memory/index.js +11 -9
  52. package/dist/relevance/index.js +16 -8
  53. package/dist/storage/index.d.ts +21 -10
  54. package/dist/storage/index.js +8 -7
  55. package/dist/telemetry/index.d.ts +35 -5
  56. package/dist/telemetry/index.js +3 -2
  57. package/dist/telemetry/otel-vendor.d.ts +7 -0
  58. package/dist/telemetry/otel-vendor.js +8 -0
  59. package/dist/tools/index.d.ts +6 -8
  60. package/dist/tools/index.js +2 -2
  61. package/dist/tts/index.d.ts +2 -4
  62. package/dist/tts/index.js +6 -5
  63. package/dist/{metric-BWeQNZt6.d.ts → types-m9RryK9a.d.ts} +6 -1
  64. package/dist/utils.js +2 -2
  65. package/dist/vector/index.d.ts +4 -6
  66. package/dist/vector/index.js +4 -4
  67. package/dist/vector/libsql/index.d.ts +2 -4
  68. package/dist/vector/libsql/index.js +6 -6
  69. package/dist/{workflow-DTtv7_Eq.d.ts → workflow-Cy8UTGCt.d.ts} +3 -6
  70. package/dist/workflows/index.d.ts +7 -9
  71. package/dist/workflows/index.js +4 -4
  72. package/package.json +14 -10
  73. package/dist/chunk-4ZUSEHLH.js +0 -285
  74. package/dist/chunk-AJJZUHB4.js +0 -14
  75. package/dist/chunk-G4MCO7XF.js +0 -70
  76. package/dist/chunk-HBTQNIAX.js +0 -90
  77. package/dist/chunk-HPXWJBQK.js +0 -222
  78. package/dist/chunk-JJ57BXQR.js +0 -16
  79. package/dist/chunk-JP37ODNX.js +0 -36
  80. package/dist/chunk-K3N7KJHH.js +0 -52
  81. package/dist/chunk-MDM2JS2U.js +0 -1288
  82. package/dist/chunk-VOUPGVRD.js +0 -27
  83. package/dist/chunk-Z7JFMQZZ.js +0 -551
  84. /package/dist/{chunk-AE3H2QEY.js → chunk-VDOJTUYY.js} +0 -0
@@ -1,27 +0,0 @@
1
- import { __name, __publicField } from './chunk-AJJZUHB4.js';
2
-
3
- // src/tools/tool.ts
4
- var _Tool = class _Tool {
5
- constructor(opts) {
6
- __publicField(this, "id");
7
- __publicField(this, "description");
8
- __publicField(this, "inputSchema");
9
- __publicField(this, "outputSchema");
10
- __publicField(this, "execute");
11
- __publicField(this, "mastra");
12
- this.id = opts.id;
13
- this.description = opts.description;
14
- this.inputSchema = opts.inputSchema;
15
- this.outputSchema = opts.outputSchema;
16
- this.execute = opts.execute;
17
- this.mastra = opts.mastra;
18
- }
19
- };
20
- __name(_Tool, "Tool");
21
- var Tool = _Tool;
22
- function createTool(opts) {
23
- return new Tool(opts);
24
- }
25
- __name(createTool, "createTool");
26
-
27
- export { Tool, createTool };
@@ -1,551 +0,0 @@
1
- import { MastraBase } from './chunk-G4MCO7XF.js';
2
- import { __name, __publicField } from './chunk-AJJZUHB4.js';
3
- import { createClient } from '@libsql/client';
4
- import { join } from 'path';
5
-
6
- // src/storage/base.ts
7
- var _MastraStorage = class _MastraStorage extends MastraBase {
8
- constructor({ name }) {
9
- super({
10
- component: "STORAGE",
11
- name
12
- });
13
- __publicField(this, "hasInit", false);
14
- }
15
- async __getThreadById({ threadId }) {
16
- await this.init();
17
- return this.getThreadById({
18
- threadId
19
- });
20
- }
21
- async __getThreadsByResourceId({ resourceId }) {
22
- await this.init();
23
- return this.getThreadsByResourceId({
24
- resourceId
25
- });
26
- }
27
- async __saveThread({ thread }) {
28
- await this.init();
29
- return this.saveThread({
30
- thread
31
- });
32
- }
33
- async __updateThread({ id, title, metadata }) {
34
- await this.init();
35
- return this.updateThread({
36
- id,
37
- title,
38
- metadata
39
- });
40
- }
41
- async __deleteThread({ threadId }) {
42
- await this.init();
43
- return this.deleteThread({
44
- threadId
45
- });
46
- }
47
- async __getMessages({ threadId, selectBy, threadConfig }) {
48
- await this.init();
49
- return this.getMessages({
50
- threadId,
51
- selectBy,
52
- threadConfig
53
- });
54
- }
55
- async __saveMessages({ messages }) {
56
- await this.init();
57
- return this.saveMessages({
58
- messages
59
- });
60
- }
61
- async init() {
62
- if (this.hasInit) {
63
- return;
64
- }
65
- await this.createTable({
66
- tableName: _MastraStorage.TABLE_WORKFLOW_SNAPSHOT,
67
- schema: {
68
- workflow_name: {
69
- type: "text"
70
- },
71
- run_id: {
72
- type: "text"
73
- },
74
- snapshot: {
75
- type: "text"
76
- },
77
- createdAt: {
78
- type: "timestamp"
79
- },
80
- updatedAt: {
81
- type: "timestamp"
82
- }
83
- }
84
- });
85
- await this.createTable({
86
- tableName: _MastraStorage.TABLE_EVALS,
87
- schema: {
88
- meta: {
89
- type: "text"
90
- },
91
- result: {
92
- type: "text"
93
- },
94
- input: {
95
- type: "text"
96
- },
97
- output: {
98
- type: "text"
99
- },
100
- createdAt: {
101
- type: "timestamp"
102
- }
103
- }
104
- });
105
- await this.createTable({
106
- tableName: _MastraStorage.TABLE_THREADS,
107
- schema: {
108
- id: {
109
- type: "text",
110
- nullable: false,
111
- primaryKey: true
112
- },
113
- resourceId: {
114
- type: "text",
115
- nullable: false
116
- },
117
- title: {
118
- type: "text",
119
- nullable: false
120
- },
121
- metadata: {
122
- type: "text",
123
- nullable: true
124
- },
125
- createdAt: {
126
- type: "timestamp",
127
- nullable: false
128
- },
129
- updatedAt: {
130
- type: "timestamp",
131
- nullable: false
132
- }
133
- }
134
- });
135
- await this.createTable({
136
- tableName: _MastraStorage.TABLE_MESSAGES,
137
- schema: {
138
- id: {
139
- type: "text",
140
- nullable: false,
141
- primaryKey: true
142
- },
143
- thread_id: {
144
- type: "text",
145
- nullable: false
146
- },
147
- content: {
148
- type: "text",
149
- nullable: false
150
- },
151
- role: {
152
- type: "text",
153
- nullable: false
154
- },
155
- type: {
156
- type: "text",
157
- nullable: false
158
- },
159
- createdAt: {
160
- type: "timestamp",
161
- nullable: false
162
- }
163
- }
164
- });
165
- this.hasInit = true;
166
- }
167
- async persistWorkflowSnapshot({ workflowName, runId, snapshot }) {
168
- await this.init();
169
- const data = {
170
- workflow_name: workflowName,
171
- run_id: runId,
172
- snapshot,
173
- createdAt: /* @__PURE__ */ new Date(),
174
- updatedAt: /* @__PURE__ */ new Date()
175
- };
176
- this.logger.debug("Persisting workflow snapshot", {
177
- workflowName,
178
- runId,
179
- data
180
- });
181
- await this.insert({
182
- tableName: _MastraStorage.TABLE_WORKFLOW_SNAPSHOT,
183
- record: data
184
- });
185
- }
186
- async loadWorkflowSnapshot({ workflowName, runId }) {
187
- if (!this.hasInit) {
188
- await this.init();
189
- }
190
- this.logger.debug("Loading workflow snapshot", {
191
- workflowName,
192
- runId
193
- });
194
- const d = await this.load({
195
- tableName: _MastraStorage.TABLE_WORKFLOW_SNAPSHOT,
196
- keys: {
197
- workflow_name: workflowName,
198
- run_id: runId
199
- }
200
- });
201
- return d ? d.snapshot : null;
202
- }
203
- };
204
- __name(_MastraStorage, "MastraStorage");
205
- __publicField(_MastraStorage, "TABLE_WORKFLOW_SNAPSHOT", "mastra_workflow_snapshot");
206
- __publicField(_MastraStorage, "TABLE_EVALS", "mastra_evals");
207
- __publicField(_MastraStorage, "TABLE_MESSAGES", "mastra_messages");
208
- __publicField(_MastraStorage, "TABLE_THREADS", "mastra_threads");
209
- var MastraStorage = _MastraStorage;
210
- var _DefaultStorage = class _DefaultStorage extends MastraStorage {
211
- constructor({ config }) {
212
- super({
213
- name: `MastraStorageLibSql`
214
- });
215
- __publicField(this, "client");
216
- this.client = createClient({
217
- url: this.rewriteDbUrl(config.url),
218
- authToken: config.authToken
219
- });
220
- }
221
- rewriteDbUrl(url) {
222
- if (url.startsWith("file:") && !url.startsWith("file:/")) {
223
- const cwd = process.cwd();
224
- const relativePath = url.slice("file:".length);
225
- if (cwd.endsWith(".mastra") || cwd.endsWith(".mastra/")) {
226
- const baseDir = join(cwd, `..`);
227
- const fullPath = join(baseDir, relativePath);
228
- this.logger.debug(`Initializing LibSQL db with url ${url} with relative file path from inside .mastra directory. Rewriting relative file url to "file:${fullPath}". This ensures it's outside the .mastra directory. If the db is stored inside .mastra it will be deleted when Mastra re-bundles code.`);
229
- return `file:${fullPath}`;
230
- }
231
- }
232
- return url;
233
- }
234
- getCreateTableSQL(tableName, schema) {
235
- const columns = Object.entries(schema).map(([name, col]) => {
236
- let type = col.type.toUpperCase();
237
- if (type === "TEXT") type = "TEXT";
238
- if (type === "TIMESTAMP") type = "TEXT";
239
- const nullable = col.nullable ? "" : "NOT NULL";
240
- const primaryKey = col.primaryKey ? "PRIMARY KEY" : "";
241
- return `${name} ${type} ${nullable} ${primaryKey}`.trim();
242
- });
243
- if (tableName === MastraStorage.TABLE_WORKFLOW_SNAPSHOT) {
244
- const stmnt = `CREATE TABLE IF NOT EXISTS ${tableName} (
245
- ${columns.join(",\n")},
246
- PRIMARY KEY (workflow_name, run_id)
247
- )`;
248
- this.logger.info(stmnt);
249
- return stmnt;
250
- }
251
- return `CREATE TABLE IF NOT EXISTS ${tableName} (${columns.join(", ")})`;
252
- }
253
- async createTable({ tableName, schema }) {
254
- try {
255
- this.logger.debug(`Creating table ${tableName}`);
256
- const sql = this.getCreateTableSQL(tableName, schema);
257
- await this.client.execute(sql);
258
- } catch (error) {
259
- this.logger.error(`Error creating table ${tableName}: ${error}`);
260
- throw error;
261
- }
262
- }
263
- async clearTable({ tableName }) {
264
- try {
265
- await this.client.execute(`DELETE FROM ${tableName}`);
266
- } catch (e) {
267
- if (e instanceof Error) {
268
- this.logger.error(e.message);
269
- }
270
- }
271
- }
272
- async insert({ tableName, record }) {
273
- try {
274
- const columns = Object.keys(record);
275
- const values = Object.values(record).map((v) => {
276
- if (typeof v === `undefined`) {
277
- return null;
278
- }
279
- return typeof v === "object" ? JSON.stringify(v) : v;
280
- });
281
- const placeholders = values.map(() => "?").join(", ");
282
- await this.client.execute({
283
- sql: `INSERT OR REPLACE INTO ${tableName} (${columns.join(", ")}) VALUES (${placeholders})`,
284
- args: values
285
- });
286
- } catch (error) {
287
- this.logger.error(`Error upserting into table ${tableName}: ${error}`);
288
- throw error;
289
- }
290
- }
291
- async load({ tableName, keys }) {
292
- const conditions = Object.entries(keys).map(([key]) => `${key} = ?`).join(" AND ");
293
- const values = Object.values(keys);
294
- const result = await this.client.execute({
295
- sql: `SELECT * FROM ${tableName} WHERE ${conditions} LIMIT 1`,
296
- args: values
297
- });
298
- if (!result.rows || result.rows.length === 0) {
299
- return null;
300
- }
301
- const row = result.rows[0];
302
- const parsed = Object.fromEntries(Object.entries(row || {}).map(([k, v]) => {
303
- try {
304
- return [
305
- k,
306
- typeof v === "string" ? JSON.parse(v) : v
307
- ];
308
- } catch {
309
- return [
310
- k,
311
- v
312
- ];
313
- }
314
- }));
315
- return parsed;
316
- }
317
- async getThreadById({ threadId }) {
318
- const result = await this.load({
319
- tableName: MastraStorage.TABLE_THREADS,
320
- keys: {
321
- id: threadId
322
- }
323
- });
324
- if (!result) {
325
- return null;
326
- }
327
- return {
328
- ...result,
329
- metadata: typeof result.metadata === "string" ? JSON.parse(result.metadata) : result.metadata
330
- };
331
- }
332
- async getThreadsByResourceId({ resourceId }) {
333
- const result = await this.client.execute({
334
- sql: `SELECT * FROM ${MastraStorage.TABLE_THREADS} WHERE resourceId = ?`,
335
- args: [
336
- resourceId
337
- ]
338
- });
339
- if (!result.rows) {
340
- return [];
341
- }
342
- return result.rows.map((thread) => ({
343
- id: thread.id,
344
- resourceId: thread.resourceId,
345
- title: thread.title,
346
- createdAt: thread.createdAt,
347
- updatedAt: thread.updatedAt,
348
- metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata
349
- }));
350
- }
351
- async saveThread({ thread }) {
352
- await this.insert({
353
- tableName: MastraStorage.TABLE_THREADS,
354
- record: {
355
- ...thread,
356
- metadata: JSON.stringify(thread.metadata)
357
- }
358
- });
359
- return thread;
360
- }
361
- async updateThread({ id, title, metadata }) {
362
- const thread = await this.getThreadById({
363
- threadId: id
364
- });
365
- if (!thread) {
366
- throw new Error(`Thread ${id} not found`);
367
- }
368
- const updatedThread = {
369
- ...thread,
370
- title,
371
- metadata: {
372
- ...thread.metadata,
373
- ...metadata
374
- }
375
- };
376
- await this.client.execute({
377
- sql: `UPDATE ${MastraStorage.TABLE_THREADS} SET title = ?, metadata = ? WHERE id = ?`,
378
- args: [
379
- title,
380
- JSON.stringify(updatedThread.metadata),
381
- id
382
- ]
383
- });
384
- return updatedThread;
385
- }
386
- async deleteThread({ threadId }) {
387
- await this.client.execute({
388
- sql: `DELETE FROM ${MastraStorage.TABLE_THREADS} WHERE id = ?`,
389
- args: [
390
- threadId
391
- ]
392
- });
393
- }
394
- parseRow(row) {
395
- let content = row.content;
396
- try {
397
- content = JSON.parse(row.content);
398
- } catch (e) {
399
- }
400
- return {
401
- id: row.id,
402
- content,
403
- role: row.role,
404
- type: row.type,
405
- createdAt: new Date(row.createdAt),
406
- threadId: row.thread_id
407
- };
408
- }
409
- async getMessages({ threadId, selectBy }) {
410
- try {
411
- const messages = [];
412
- const limit = typeof selectBy?.last === `number` ? selectBy.last : 40;
413
- if (selectBy?.include?.length) {
414
- const includeIds = selectBy.include.map((i) => i.id);
415
- const maxPrev = Math.max(...selectBy.include.map((i) => i.withPreviousMessages || 0));
416
- const maxNext = Math.max(...selectBy.include.map((i) => i.withNextMessages || 0));
417
- const includeResult = await this.client.execute({
418
- sql: `
419
- WITH numbered_messages AS (
420
- SELECT
421
- id,
422
- content,
423
- role,
424
- type,
425
- "createdAt",
426
- thread_id,
427
- ROW_NUMBER() OVER (ORDER BY "createdAt" ASC) as row_num
428
- FROM "${MastraStorage.TABLE_MESSAGES}"
429
- WHERE thread_id = ?
430
- ),
431
- target_positions AS (
432
- SELECT row_num as target_pos
433
- FROM numbered_messages
434
- WHERE id IN (${includeIds.map(() => "?").join(", ")})
435
- )
436
- SELECT DISTINCT m.*
437
- FROM numbered_messages m
438
- CROSS JOIN target_positions t
439
- WHERE m.row_num BETWEEN (t.target_pos - ?) AND (t.target_pos + ?)
440
- ORDER BY m."createdAt" ASC
441
- `,
442
- args: [
443
- threadId,
444
- ...includeIds,
445
- maxPrev,
446
- maxNext
447
- ]
448
- });
449
- if (includeResult.rows) {
450
- messages.push(...includeResult.rows.map((row) => this.parseRow(row)));
451
- }
452
- }
453
- const excludeIds = messages.map((m) => m.id);
454
- const remainingSql = `
455
- SELECT
456
- id,
457
- content,
458
- role,
459
- type,
460
- "createdAt",
461
- thread_id
462
- FROM "${MastraStorage.TABLE_MESSAGES}"
463
- WHERE thread_id = ?
464
- ${excludeIds.length ? `AND id NOT IN (${excludeIds.map(() => "?").join(", ")})` : ""}
465
- ORDER BY "createdAt" DESC
466
- LIMIT ?
467
- `;
468
- const remainingArgs = [
469
- threadId,
470
- ...excludeIds.length ? excludeIds : [],
471
- limit
472
- ];
473
- const remainingResult = await this.client.execute({
474
- sql: remainingSql,
475
- args: remainingArgs
476
- });
477
- if (remainingResult.rows) {
478
- messages.push(...remainingResult.rows.map((row) => this.parseRow(row)));
479
- }
480
- messages.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime());
481
- return messages;
482
- } catch (error) {
483
- this.logger.error("Error getting messages:", error);
484
- throw error;
485
- }
486
- }
487
- async saveMessages({ messages }) {
488
- if (messages.length === 0) return messages;
489
- const tx = await this.client.transaction("write");
490
- try {
491
- const threadId = messages[0]?.threadId;
492
- if (!threadId) {
493
- throw new Error("Thread ID is required");
494
- }
495
- for (const message of messages) {
496
- const time = message.createdAt || /* @__PURE__ */ new Date();
497
- await tx.execute({
498
- sql: `INSERT INTO ${MastraStorage.TABLE_MESSAGES} (id, thread_id, content, role, type, createdAt)
499
- VALUES (?, ?, ?, ?, ?, ?)`,
500
- args: [
501
- message.id,
502
- threadId,
503
- typeof message.content === "object" ? JSON.stringify(message.content) : message.content,
504
- message.role,
505
- message.type,
506
- time instanceof Date ? time.toISOString() : time
507
- ]
508
- });
509
- }
510
- await tx.commit();
511
- return messages;
512
- } catch (error) {
513
- console.error("Failed to save messages in database", error);
514
- await tx.rollback();
515
- throw error;
516
- }
517
- }
518
- async getEvalsByAgentName(agentName, type) {
519
- try {
520
- const baseQuery = `SELECT * FROM ${MastraStorage.TABLE_EVALS} WHERE meta->>'$.agentName' = ?`;
521
- const typeCondition = type === "test" ? " AND meta->>'$.testPath' IS NOT NULL" : type === "live" ? " AND meta->>'$.testPath' IS NULL" : "";
522
- const result = await this.client.execute({
523
- sql: `${baseQuery}${typeCondition} ORDER BY createdAt DESC`,
524
- args: [
525
- agentName
526
- ]
527
- });
528
- if (!result.rows) {
529
- return [];
530
- }
531
- return result.rows.map((row) => ({
532
- ...row,
533
- result: typeof row.result === "string" ? JSON.parse(row.result) : row.result,
534
- meta: typeof row.meta === "string" ? JSON.parse(row.meta) : row.meta,
535
- input: row.input,
536
- output: row.output,
537
- createdAt: row.createdAt
538
- }));
539
- } catch (error) {
540
- if (error instanceof Error && error.message.includes("no such table")) {
541
- return [];
542
- }
543
- console.error("Failed to get evals for the specified agent", error);
544
- throw error;
545
- }
546
- }
547
- };
548
- __name(_DefaultStorage, "DefaultStorage");
549
- var DefaultStorage = _DefaultStorage;
550
-
551
- export { DefaultStorage, MastraStorage };
File without changes