@mastra/core 0.0.0-commonjs-20250227130920

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 (137) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +180 -0
  3. package/dist/agent/index.cjs +1865 -0
  4. package/dist/agent/index.d.cts +15 -0
  5. package/dist/agent/index.d.ts +15 -0
  6. package/dist/agent/index.js +1 -0
  7. package/dist/base-D90KQ4XI.d.ts +139 -0
  8. package/dist/base-hs9NDAZ2.d.cts +139 -0
  9. package/dist/base-nKCMCNrM.d.ts +920 -0
  10. package/dist/base-nhesrHv3.d.cts +920 -0
  11. package/dist/base.cjs +138 -0
  12. package/dist/base.d.cts +6 -0
  13. package/dist/base.d.ts +6 -0
  14. package/dist/base.js +1 -0
  15. package/dist/bundler/index.cjs +158 -0
  16. package/dist/bundler/index.d.cts +28 -0
  17. package/dist/bundler/index.d.ts +28 -0
  18. package/dist/bundler/index.js +1 -0
  19. package/dist/chunk-33GSTUNK.js +620 -0
  20. package/dist/chunk-4YRYBCOZ.js +10 -0
  21. package/dist/chunk-55NFNRKO.js +10 -0
  22. package/dist/chunk-5XPCMNGW.js +215 -0
  23. package/dist/chunk-B3M27AMP.js +1479 -0
  24. package/dist/chunk-BB4KXGBU.js +83 -0
  25. package/dist/chunk-C6A6W6XS.js +77 -0
  26. package/dist/chunk-HQ55LN2U.js +318 -0
  27. package/dist/chunk-KNVTCZW7.js +416 -0
  28. package/dist/chunk-LH47WVJL.js +61 -0
  29. package/dist/chunk-NGD2HQYW.js +346 -0
  30. package/dist/chunk-NUDAZEOG.js +35 -0
  31. package/dist/chunk-OZ4XVJ6F.js +49 -0
  32. package/dist/chunk-PHMSPCTC.js +145 -0
  33. package/dist/chunk-PNZK456O.js +88 -0
  34. package/dist/chunk-QAAJAHDB.js +37 -0
  35. package/dist/chunk-RG66XEJT.js +8 -0
  36. package/dist/chunk-SIFBBGY6.js +190 -0
  37. package/dist/chunk-SVEAENO7.js +22 -0
  38. package/dist/chunk-SY5244IR.js +1499 -0
  39. package/dist/chunk-W5HVJX45.js +402 -0
  40. package/dist/chunk-WIBGG4X6.js +173 -0
  41. package/dist/chunk-ZDWFBE5L.js +1 -0
  42. package/dist/chunk-ZINPRHAN.js +22 -0
  43. package/dist/deployer/index.cjs +165 -0
  44. package/dist/deployer/index.d.cts +19 -0
  45. package/dist/deployer/index.d.ts +19 -0
  46. package/dist/deployer/index.js +1 -0
  47. package/dist/eval/index.cjs +110 -0
  48. package/dist/eval/index.d.cts +28 -0
  49. package/dist/eval/index.d.ts +28 -0
  50. package/dist/eval/index.js +1 -0
  51. package/dist/filter/index.cjs +192 -0
  52. package/dist/filter/index.d.cts +90 -0
  53. package/dist/filter/index.d.ts +90 -0
  54. package/dist/filter/index.js +1 -0
  55. package/dist/hooks/index.cjs +87 -0
  56. package/dist/hooks/index.d.cts +33 -0
  57. package/dist/hooks/index.d.ts +33 -0
  58. package/dist/hooks/index.js +1 -0
  59. package/dist/index-mKY1XrpK.d.cts +90 -0
  60. package/dist/index-mKY1XrpK.d.ts +90 -0
  61. package/dist/index.cjs +6844 -0
  62. package/dist/index.d.cts +97 -0
  63. package/dist/index.d.ts +97 -0
  64. package/dist/index.js +119 -0
  65. package/dist/integration/index.cjs +113 -0
  66. package/dist/integration/index.d.cts +52 -0
  67. package/dist/integration/index.d.ts +52 -0
  68. package/dist/integration/index.js +1 -0
  69. package/dist/llm/index.cjs +2 -0
  70. package/dist/llm/index.d.cts +15 -0
  71. package/dist/llm/index.d.ts +15 -0
  72. package/dist/llm/index.js +1 -0
  73. package/dist/logger/index.cjs +159 -0
  74. package/dist/logger/index.d.cts +3 -0
  75. package/dist/logger/index.d.ts +3 -0
  76. package/dist/logger/index.js +1 -0
  77. package/dist/mastra/index.cjs +1741 -0
  78. package/dist/mastra/index.d.cts +67 -0
  79. package/dist/mastra/index.d.ts +67 -0
  80. package/dist/mastra/index.js +1 -0
  81. package/dist/memory/index.cjs +1907 -0
  82. package/dist/memory/index.d.cts +15 -0
  83. package/dist/memory/index.d.ts +15 -0
  84. package/dist/memory/index.js +1 -0
  85. package/dist/relevance/index.cjs +1927 -0
  86. package/dist/relevance/index.d.cts +21 -0
  87. package/dist/relevance/index.d.ts +21 -0
  88. package/dist/relevance/index.js +1 -0
  89. package/dist/storage/index.cjs +361 -0
  90. package/dist/storage/index.d.cts +15 -0
  91. package/dist/storage/index.d.ts +15 -0
  92. package/dist/storage/index.js +2 -0
  93. package/dist/storage/libsql/index.cjs +770 -0
  94. package/dist/storage/libsql/index.d.cts +81 -0
  95. package/dist/storage/libsql/index.d.ts +81 -0
  96. package/dist/storage/libsql/index.js +1 -0
  97. package/dist/telemetry/index.cjs +413 -0
  98. package/dist/telemetry/index.d.cts +51 -0
  99. package/dist/telemetry/index.d.ts +51 -0
  100. package/dist/telemetry/index.js +1 -0
  101. package/dist/telemetry/otel-vendor.cjs +52 -0
  102. package/dist/telemetry/otel-vendor.d.cts +7 -0
  103. package/dist/telemetry/otel-vendor.d.ts +7 -0
  104. package/dist/telemetry/otel-vendor.js +7 -0
  105. package/dist/tools/index.cjs +25 -0
  106. package/dist/tools/index.d.cts +29 -0
  107. package/dist/tools/index.d.ts +29 -0
  108. package/dist/tools/index.js +1 -0
  109. package/dist/tts/index.cjs +328 -0
  110. package/dist/tts/index.d.cts +28 -0
  111. package/dist/tts/index.d.ts +28 -0
  112. package/dist/tts/index.js +1 -0
  113. package/dist/types-m9RryK9a.d.cts +14 -0
  114. package/dist/types-m9RryK9a.d.ts +14 -0
  115. package/dist/utils.cjs +179 -0
  116. package/dist/utils.d.cts +26 -0
  117. package/dist/utils.d.ts +26 -0
  118. package/dist/utils.js +1 -0
  119. package/dist/vector/index.cjs +145 -0
  120. package/dist/vector/index.d.cts +30 -0
  121. package/dist/vector/index.d.ts +30 -0
  122. package/dist/vector/index.js +1 -0
  123. package/dist/vector/libsql/index.cjs +951 -0
  124. package/dist/vector/libsql/index.d.cts +29 -0
  125. package/dist/vector/libsql/index.d.ts +29 -0
  126. package/dist/vector/libsql/index.js +1 -0
  127. package/dist/voice/index.cjs +369 -0
  128. package/dist/voice/index.d.cts +67 -0
  129. package/dist/voice/index.d.ts +67 -0
  130. package/dist/voice/index.js +76 -0
  131. package/dist/workflow-DqQ4pON_.d.cts +84 -0
  132. package/dist/workflow-Ng_F_Zaf.d.ts +84 -0
  133. package/dist/workflows/index.cjs +1628 -0
  134. package/dist/workflows/index.d.cts +48 -0
  135. package/dist/workflows/index.d.ts +48 -0
  136. package/dist/workflows/index.js +1 -0
  137. package/package.json +162 -0
@@ -0,0 +1,416 @@
1
+ import { MastraStorage } from './chunk-5XPCMNGW.js';
2
+ import { TABLE_WORKFLOW_SNAPSHOT, TABLE_THREADS, TABLE_MESSAGES, TABLE_EVALS, TABLE_TRACES } from './chunk-RG66XEJT.js';
3
+ import { join } from 'node:path';
4
+ import { createClient } from '@libsql/client';
5
+
6
+ function safelyParseJSON(jsonString) {
7
+ try {
8
+ return JSON.parse(jsonString);
9
+ } catch {
10
+ return {};
11
+ }
12
+ }
13
+ var LibSQLStore = class extends MastraStorage {
14
+ client;
15
+ constructor({ config }) {
16
+ super({ name: `LibSQLStore` });
17
+ this.client = createClient({
18
+ url: this.rewriteDbUrl(config.url),
19
+ authToken: config.authToken
20
+ });
21
+ }
22
+ rewriteDbUrl(url) {
23
+ if (url.startsWith("file:") && !url.startsWith("file:/")) {
24
+ const cwd = process.cwd();
25
+ const relativePath = url.slice("file:".length);
26
+ if (cwd.endsWith(".mastra") || cwd.endsWith(".mastra/")) {
27
+ const baseDir = join(cwd, `..`);
28
+ const fullPath = join(baseDir, relativePath);
29
+ this.logger.debug(
30
+ `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.`
31
+ );
32
+ return `file:${fullPath}`;
33
+ }
34
+ }
35
+ return url;
36
+ }
37
+ getCreateTableSQL(tableName, schema) {
38
+ const columns = Object.entries(schema).map(([name, col]) => {
39
+ let type = col.type.toUpperCase();
40
+ if (type === "TEXT") type = "TEXT";
41
+ if (type === "TIMESTAMP") type = "TEXT";
42
+ const nullable = col.nullable ? "" : "NOT NULL";
43
+ const primaryKey = col.primaryKey ? "PRIMARY KEY" : "";
44
+ return `${name} ${type} ${nullable} ${primaryKey}`.trim();
45
+ });
46
+ if (tableName === TABLE_WORKFLOW_SNAPSHOT) {
47
+ const stmnt = `CREATE TABLE IF NOT EXISTS ${tableName} (
48
+ ${columns.join(",\n")},
49
+ PRIMARY KEY (workflow_name, run_id)
50
+ )`;
51
+ return stmnt;
52
+ }
53
+ return `CREATE TABLE IF NOT EXISTS ${tableName} (${columns.join(", ")})`;
54
+ }
55
+ async createTable({
56
+ tableName,
57
+ schema
58
+ }) {
59
+ try {
60
+ this.logger.debug(`Creating database table`, { tableName, operation: "schema init" });
61
+ const sql = this.getCreateTableSQL(tableName, schema);
62
+ await this.client.execute(sql);
63
+ } catch (error) {
64
+ this.logger.error(`Error creating table ${tableName}: ${error}`);
65
+ throw error;
66
+ }
67
+ }
68
+ async clearTable({ tableName }) {
69
+ try {
70
+ await this.client.execute(`DELETE FROM ${tableName}`);
71
+ } catch (e) {
72
+ if (e instanceof Error) {
73
+ this.logger.error(e.message);
74
+ }
75
+ }
76
+ }
77
+ prepareStatement({ tableName, record }) {
78
+ const columns = Object.keys(record);
79
+ const values = Object.values(record).map((v) => {
80
+ if (typeof v === `undefined`) {
81
+ return null;
82
+ }
83
+ return typeof v === "object" ? JSON.stringify(v) : v;
84
+ });
85
+ const placeholders = values.map(() => "?").join(", ");
86
+ return {
87
+ sql: `INSERT OR REPLACE INTO ${tableName} (${columns.join(", ")}) VALUES (${placeholders})`,
88
+ args: values
89
+ };
90
+ }
91
+ async insert({ tableName, record }) {
92
+ try {
93
+ await this.client.execute(this.prepareStatement({ tableName, record }));
94
+ } catch (error) {
95
+ this.logger.error(`Error upserting into table ${tableName}: ${error}`);
96
+ throw error;
97
+ }
98
+ }
99
+ async batchInsert({ tableName, records }) {
100
+ if (records.length === 0) return;
101
+ try {
102
+ const batchStatements = records.map((r) => this.prepareStatement({ tableName, record: r }));
103
+ await this.client.batch(batchStatements, "write");
104
+ } catch (error) {
105
+ this.logger.error(`Error upserting into table ${tableName}: ${error}`);
106
+ throw error;
107
+ }
108
+ }
109
+ async load({ tableName, keys }) {
110
+ const conditions = Object.entries(keys).map(([key]) => `${key} = ?`).join(" AND ");
111
+ const values = Object.values(keys);
112
+ const result = await this.client.execute({
113
+ sql: `SELECT * FROM ${tableName} WHERE ${conditions} ORDER BY createdAt DESC LIMIT 1`,
114
+ args: values
115
+ });
116
+ if (!result.rows || result.rows.length === 0) {
117
+ return null;
118
+ }
119
+ const row = result.rows[0];
120
+ const parsed = Object.fromEntries(
121
+ Object.entries(row || {}).map(([k, v]) => {
122
+ try {
123
+ return [k, typeof v === "string" ? JSON.parse(v) : v];
124
+ } catch {
125
+ return [k, v];
126
+ }
127
+ })
128
+ );
129
+ return parsed;
130
+ }
131
+ async getThreadById({ threadId }) {
132
+ const result = await this.load({
133
+ tableName: TABLE_THREADS,
134
+ keys: { id: threadId }
135
+ });
136
+ if (!result) {
137
+ return null;
138
+ }
139
+ return {
140
+ ...result,
141
+ metadata: typeof result.metadata === "string" ? JSON.parse(result.metadata) : result.metadata
142
+ };
143
+ }
144
+ async getThreadsByResourceId({ resourceId }) {
145
+ const result = await this.client.execute({
146
+ sql: `SELECT * FROM ${TABLE_THREADS} WHERE resourceId = ?`,
147
+ args: [resourceId]
148
+ });
149
+ if (!result.rows) {
150
+ return [];
151
+ }
152
+ return result.rows.map((thread) => ({
153
+ id: thread.id,
154
+ resourceId: thread.resourceId,
155
+ title: thread.title,
156
+ createdAt: thread.createdAt,
157
+ updatedAt: thread.updatedAt,
158
+ metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata
159
+ }));
160
+ }
161
+ async saveThread({ thread }) {
162
+ await this.insert({
163
+ tableName: TABLE_THREADS,
164
+ record: {
165
+ ...thread,
166
+ metadata: JSON.stringify(thread.metadata)
167
+ }
168
+ });
169
+ return thread;
170
+ }
171
+ async updateThread({
172
+ id,
173
+ title,
174
+ metadata
175
+ }) {
176
+ const thread = await this.getThreadById({ threadId: id });
177
+ if (!thread) {
178
+ throw new Error(`Thread ${id} not found`);
179
+ }
180
+ const updatedThread = {
181
+ ...thread,
182
+ title,
183
+ metadata: {
184
+ ...thread.metadata,
185
+ ...metadata
186
+ }
187
+ };
188
+ await this.client.execute({
189
+ sql: `UPDATE ${TABLE_THREADS} SET title = ?, metadata = ? WHERE id = ?`,
190
+ args: [title, JSON.stringify(updatedThread.metadata), id]
191
+ });
192
+ return updatedThread;
193
+ }
194
+ async deleteThread({ threadId }) {
195
+ await this.client.execute({
196
+ sql: `DELETE FROM ${TABLE_THREADS} WHERE id = ?`,
197
+ args: [threadId]
198
+ });
199
+ }
200
+ parseRow(row) {
201
+ let content = row.content;
202
+ try {
203
+ content = JSON.parse(row.content);
204
+ } catch {
205
+ }
206
+ return {
207
+ id: row.id,
208
+ content,
209
+ role: row.role,
210
+ type: row.type,
211
+ createdAt: new Date(row.createdAt),
212
+ threadId: row.thread_id
213
+ };
214
+ }
215
+ async getMessages({ threadId, selectBy }) {
216
+ try {
217
+ const messages = [];
218
+ const limit = typeof selectBy?.last === `number` ? selectBy.last : 40;
219
+ if (selectBy?.include?.length) {
220
+ const includeIds = selectBy.include.map((i) => i.id);
221
+ const maxPrev = Math.max(...selectBy.include.map((i) => i.withPreviousMessages || 0));
222
+ const maxNext = Math.max(...selectBy.include.map((i) => i.withNextMessages || 0));
223
+ const includeResult = await this.client.execute({
224
+ sql: `
225
+ WITH numbered_messages AS (
226
+ SELECT
227
+ id,
228
+ content,
229
+ role,
230
+ type,
231
+ "createdAt",
232
+ thread_id,
233
+ ROW_NUMBER() OVER (ORDER BY "createdAt" ASC) as row_num
234
+ FROM "${TABLE_MESSAGES}"
235
+ WHERE thread_id = ?
236
+ ),
237
+ target_positions AS (
238
+ SELECT row_num as target_pos
239
+ FROM numbered_messages
240
+ WHERE id IN (${includeIds.map(() => "?").join(", ")})
241
+ )
242
+ SELECT DISTINCT m.*
243
+ FROM numbered_messages m
244
+ CROSS JOIN target_positions t
245
+ WHERE m.row_num BETWEEN (t.target_pos - ?) AND (t.target_pos + ?)
246
+ ORDER BY m."createdAt" ASC
247
+ `,
248
+ args: [threadId, ...includeIds, maxPrev, maxNext]
249
+ });
250
+ if (includeResult.rows) {
251
+ messages.push(...includeResult.rows.map((row) => this.parseRow(row)));
252
+ }
253
+ }
254
+ const excludeIds = messages.map((m) => m.id);
255
+ const remainingSql = `
256
+ SELECT
257
+ id,
258
+ content,
259
+ role,
260
+ type,
261
+ "createdAt",
262
+ thread_id
263
+ FROM "${TABLE_MESSAGES}"
264
+ WHERE thread_id = ?
265
+ ${excludeIds.length ? `AND id NOT IN (${excludeIds.map(() => "?").join(", ")})` : ""}
266
+ ORDER BY "createdAt" DESC
267
+ LIMIT ?
268
+ `;
269
+ const remainingArgs = [threadId, ...excludeIds.length ? excludeIds : [], limit];
270
+ const remainingResult = await this.client.execute({
271
+ sql: remainingSql,
272
+ args: remainingArgs
273
+ });
274
+ if (remainingResult.rows) {
275
+ messages.push(...remainingResult.rows.map((row) => this.parseRow(row)));
276
+ }
277
+ messages.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime());
278
+ return messages;
279
+ } catch (error) {
280
+ this.logger.error("Error getting messages:", error);
281
+ throw error;
282
+ }
283
+ }
284
+ async saveMessages({ messages }) {
285
+ if (messages.length === 0) return messages;
286
+ const tx = await this.client.transaction("write");
287
+ try {
288
+ const threadId = messages[0]?.threadId;
289
+ if (!threadId) {
290
+ throw new Error("Thread ID is required");
291
+ }
292
+ for (const message of messages) {
293
+ const time = message.createdAt || /* @__PURE__ */ new Date();
294
+ await tx.execute({
295
+ sql: `INSERT INTO ${TABLE_MESSAGES} (id, thread_id, content, role, type, createdAt)
296
+ VALUES (?, ?, ?, ?, ?, ?)`,
297
+ args: [
298
+ message.id,
299
+ threadId,
300
+ typeof message.content === "object" ? JSON.stringify(message.content) : message.content,
301
+ message.role,
302
+ message.type,
303
+ time instanceof Date ? time.toISOString() : time
304
+ ]
305
+ });
306
+ }
307
+ await tx.commit();
308
+ return messages;
309
+ } catch (error) {
310
+ this.logger.error("Failed to save messages in database: " + error?.message);
311
+ await tx.rollback();
312
+ throw error;
313
+ }
314
+ }
315
+ transformEvalRow(row) {
316
+ const resultValue = JSON.parse(row.result);
317
+ const testInfoValue = row.test_info ? JSON.parse(row.test_info) : void 0;
318
+ if (!resultValue || typeof resultValue !== "object" || !("score" in resultValue)) {
319
+ throw new Error(`Invalid MetricResult format: ${JSON.stringify(resultValue)}`);
320
+ }
321
+ return {
322
+ input: row.input,
323
+ output: row.output,
324
+ result: resultValue,
325
+ agentName: row.agent_name,
326
+ metricName: row.metric_name,
327
+ instructions: row.instructions,
328
+ testInfo: testInfoValue,
329
+ globalRunId: row.global_run_id,
330
+ runId: row.run_id,
331
+ createdAt: row.created_at
332
+ };
333
+ }
334
+ async getEvalsByAgentName(agentName, type) {
335
+ try {
336
+ const baseQuery = `SELECT * FROM ${TABLE_EVALS} WHERE agent_name = ?`;
337
+ const typeCondition = type === "test" ? " AND test_info IS NOT NULL AND test_info->>'testPath' IS NOT NULL" : type === "live" ? " AND (test_info IS NULL OR test_info->>'testPath' IS NULL)" : "";
338
+ const result = await this.client.execute({
339
+ sql: `${baseQuery}${typeCondition} ORDER BY created_at DESC`,
340
+ args: [agentName]
341
+ });
342
+ return result.rows?.map((row) => this.transformEvalRow(row)) ?? [];
343
+ } catch (error) {
344
+ if (error instanceof Error && error.message.includes("no such table")) {
345
+ return [];
346
+ }
347
+ this.logger.error("Failed to get evals for the specified agent: " + error?.message);
348
+ throw error;
349
+ }
350
+ }
351
+ // TODO: add types
352
+ async getTraces({
353
+ name,
354
+ scope,
355
+ page,
356
+ perPage,
357
+ attributes
358
+ } = {
359
+ page: 0,
360
+ perPage: 100
361
+ }) {
362
+ const limit = perPage;
363
+ const offset = page * perPage;
364
+ const args = [];
365
+ const conditions = [];
366
+ if (name) {
367
+ conditions.push("name LIKE CONCAT(?, '%')");
368
+ }
369
+ if (scope) {
370
+ conditions.push("scope = ?");
371
+ }
372
+ if (attributes) {
373
+ Object.keys(attributes).forEach((key) => {
374
+ conditions.push(`attributes->>'$.${key}' = ?`);
375
+ });
376
+ }
377
+ const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
378
+ if (name) {
379
+ args.push(name);
380
+ }
381
+ if (scope) {
382
+ args.push(scope);
383
+ }
384
+ if (attributes) {
385
+ for (const [_key, value] of Object.entries(attributes)) {
386
+ args.push(value);
387
+ }
388
+ }
389
+ args.push(limit, offset);
390
+ const result = await this.client.execute({
391
+ sql: `SELECT * FROM ${TABLE_TRACES} ${whereClause} ORDER BY "startTime" DESC LIMIT ? OFFSET ?`,
392
+ args
393
+ });
394
+ if (!result.rows) {
395
+ return [];
396
+ }
397
+ return result.rows.map((row) => ({
398
+ id: row.id,
399
+ parentSpanId: row.parentSpanId,
400
+ traceId: row.traceId,
401
+ name: row.name,
402
+ scope: row.scope,
403
+ kind: row.kind,
404
+ status: safelyParseJSON(row.status),
405
+ events: safelyParseJSON(row.events),
406
+ links: safelyParseJSON(row.links),
407
+ attributes: safelyParseJSON(row.attributes),
408
+ startTime: row.startTime,
409
+ endTime: row.endTime,
410
+ other: safelyParseJSON(row.other),
411
+ createdAt: row.createdAt
412
+ }));
413
+ }
414
+ };
415
+
416
+ export { LibSQLStore };
@@ -0,0 +1,61 @@
1
+ import { Agent } from './chunk-SY5244IR.js';
2
+ import { CohereClient } from 'cohere-ai';
3
+
4
+ var CohereRelevanceScorer = class {
5
+ client;
6
+ model;
7
+ constructor(model, apiKey) {
8
+ this.client = new CohereClient({
9
+ token: apiKey || process.env.COHERE_API_KEY || ""
10
+ });
11
+ this.model = model;
12
+ }
13
+ async getRelevanceScore(query, text) {
14
+ const response = await this.client.rerank({
15
+ query,
16
+ documents: [text],
17
+ model: this.model,
18
+ topN: 1
19
+ });
20
+ return response.results[0].relevanceScore;
21
+ }
22
+ };
23
+
24
+ // src/relevance/relevance-score-provider.ts
25
+ function createSimilarityPrompt(query, text) {
26
+ return `Rate the semantic similarity between the following the query and the text on a scale from 0 to 1 (decimals allowed), where 1 means exactly the same meaning and 0 means completely different:
27
+
28
+ Query: ${query}
29
+
30
+ Text: ${text}
31
+
32
+ Relevance score (0-1):`;
33
+ }
34
+
35
+ // src/relevance/mastra-agent/index.ts
36
+ var MastraAgentRelevanceScorer = class {
37
+ agent;
38
+ constructor(name, model) {
39
+ this.agent = new Agent({
40
+ name: `Relevance Scorer ${name}`,
41
+ instructions: `You are a specialized agent for evaluating the relevance of text to queries.
42
+ Your task is to rate how well a text passage answers a given query.
43
+ Output only a number between 0 and 1, where:
44
+ 1.0 = Perfectly relevant, directly answers the query
45
+ 0.0 = Completely irrelevant
46
+ Consider:
47
+ - Direct relevance to the question
48
+ - Completeness of information
49
+ - Quality and specificity
50
+ Always return just the number, no explanation.`,
51
+ model
52
+ });
53
+ }
54
+ async getRelevanceScore(query, text) {
55
+ const prompt = createSimilarityPrompt(query, text);
56
+ const response = await this.agent.generate(prompt);
57
+ return parseFloat(response.text);
58
+ }
59
+ };
60
+
61
+ export { CohereRelevanceScorer, MastraAgentRelevanceScorer, createSimilarityPrompt };