@mastra/lance 0.0.0-monorepo-binary-20251013210052 → 0.0.0-netlify-no-bundle-20251127120354
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.
- package/CHANGELOG.md +474 -3
- package/README.md +61 -4
- package/dist/index.cjs +517 -694
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +517 -694
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +15 -39
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/scores/index.d.ts +6 -5
- package/dist/storage/domains/scores/index.d.ts.map +1 -1
- package/dist/storage/domains/utils.d.ts.map +1 -1
- package/dist/storage/domains/workflows/index.d.ts +4 -11
- package/dist/storage/domains/workflows/index.d.ts.map +1 -1
- package/dist/storage/index.d.ts +27 -88
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/vector/filter.d.ts +5 -5
- package/dist/vector/index.d.ts +6 -3
- package/dist/vector/index.d.ts.map +1 -1
- package/package.json +15 -10
- package/dist/storage/domains/legacy-evals/index.d.ts +0 -25
- package/dist/storage/domains/legacy-evals/index.d.ts.map +0 -1
- package/dist/storage/domains/traces/index.d.ts +0 -34
- package/dist/storage/domains/traces/index.d.ts.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -5,131 +5,15 @@ var error = require('@mastra/core/error');
|
|
|
5
5
|
var storage = require('@mastra/core/storage');
|
|
6
6
|
var agent = require('@mastra/core/agent');
|
|
7
7
|
var apacheArrow = require('apache-arrow');
|
|
8
|
-
var
|
|
8
|
+
var evals = require('@mastra/core/evals');
|
|
9
9
|
var vector = require('@mastra/core/vector');
|
|
10
10
|
var filter = require('@mastra/core/vector/filter');
|
|
11
11
|
|
|
12
12
|
// src/storage/index.ts
|
|
13
|
-
var StoreLegacyEvalsLance = class extends storage.LegacyEvalsStorage {
|
|
14
|
-
client;
|
|
15
|
-
constructor({ client }) {
|
|
16
|
-
super();
|
|
17
|
-
this.client = client;
|
|
18
|
-
}
|
|
19
|
-
async getEvalsByAgentName(agentName, type) {
|
|
20
|
-
try {
|
|
21
|
-
const table = await this.client.openTable(storage.TABLE_EVALS);
|
|
22
|
-
const query = table.query().where(`agent_name = '${agentName}'`);
|
|
23
|
-
const records = await query.toArray();
|
|
24
|
-
let filteredRecords = records;
|
|
25
|
-
if (type === "live") {
|
|
26
|
-
filteredRecords = records.filter((record) => record.test_info === null);
|
|
27
|
-
} else if (type === "test") {
|
|
28
|
-
filteredRecords = records.filter((record) => record.test_info !== null);
|
|
29
|
-
}
|
|
30
|
-
return filteredRecords.map((record) => {
|
|
31
|
-
return {
|
|
32
|
-
id: record.id,
|
|
33
|
-
input: record.input,
|
|
34
|
-
output: record.output,
|
|
35
|
-
agentName: record.agent_name,
|
|
36
|
-
metricName: record.metric_name,
|
|
37
|
-
result: JSON.parse(record.result),
|
|
38
|
-
instructions: record.instructions,
|
|
39
|
-
testInfo: record.test_info ? JSON.parse(record.test_info) : null,
|
|
40
|
-
globalRunId: record.global_run_id,
|
|
41
|
-
runId: record.run_id,
|
|
42
|
-
createdAt: new Date(record.created_at).toString()
|
|
43
|
-
};
|
|
44
|
-
});
|
|
45
|
-
} catch (error$1) {
|
|
46
|
-
throw new error.MastraError(
|
|
47
|
-
{
|
|
48
|
-
id: "LANCE_STORE_GET_EVALS_BY_AGENT_NAME_FAILED",
|
|
49
|
-
domain: error.ErrorDomain.STORAGE,
|
|
50
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
51
|
-
details: { agentName }
|
|
52
|
-
},
|
|
53
|
-
error$1
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
async getEvals(options) {
|
|
58
|
-
try {
|
|
59
|
-
const table = await this.client.openTable(storage.TABLE_EVALS);
|
|
60
|
-
const conditions = [];
|
|
61
|
-
if (options.agentName) {
|
|
62
|
-
conditions.push(`agent_name = '${options.agentName}'`);
|
|
63
|
-
}
|
|
64
|
-
if (options.type === "live") {
|
|
65
|
-
conditions.push("length(test_info) = 0");
|
|
66
|
-
} else if (options.type === "test") {
|
|
67
|
-
conditions.push("length(test_info) > 0");
|
|
68
|
-
}
|
|
69
|
-
const startDate = options.dateRange?.start || options.fromDate;
|
|
70
|
-
const endDate = options.dateRange?.end || options.toDate;
|
|
71
|
-
if (startDate) {
|
|
72
|
-
conditions.push(`\`created_at\` >= ${startDate.getTime()}`);
|
|
73
|
-
}
|
|
74
|
-
if (endDate) {
|
|
75
|
-
conditions.push(`\`created_at\` <= ${endDate.getTime()}`);
|
|
76
|
-
}
|
|
77
|
-
let total = 0;
|
|
78
|
-
if (conditions.length > 0) {
|
|
79
|
-
total = await table.countRows(conditions.join(" AND "));
|
|
80
|
-
} else {
|
|
81
|
-
total = await table.countRows();
|
|
82
|
-
}
|
|
83
|
-
const query = table.query();
|
|
84
|
-
if (conditions.length > 0) {
|
|
85
|
-
const whereClause = conditions.join(" AND ");
|
|
86
|
-
query.where(whereClause);
|
|
87
|
-
}
|
|
88
|
-
const records = await query.toArray();
|
|
89
|
-
const evals = records.sort((a, b) => b.created_at - a.created_at).map((record) => {
|
|
90
|
-
return {
|
|
91
|
-
id: record.id,
|
|
92
|
-
input: record.input,
|
|
93
|
-
output: record.output,
|
|
94
|
-
agentName: record.agent_name,
|
|
95
|
-
metricName: record.metric_name,
|
|
96
|
-
result: JSON.parse(record.result),
|
|
97
|
-
instructions: record.instructions,
|
|
98
|
-
testInfo: record.test_info ? JSON.parse(record.test_info) : null,
|
|
99
|
-
globalRunId: record.global_run_id,
|
|
100
|
-
runId: record.run_id,
|
|
101
|
-
createdAt: new Date(record.created_at).toISOString()
|
|
102
|
-
};
|
|
103
|
-
});
|
|
104
|
-
const page = options.page || 0;
|
|
105
|
-
const perPage = options.perPage || 10;
|
|
106
|
-
const pagedEvals = evals.slice(page * perPage, (page + 1) * perPage);
|
|
107
|
-
return {
|
|
108
|
-
evals: pagedEvals,
|
|
109
|
-
total,
|
|
110
|
-
page,
|
|
111
|
-
perPage,
|
|
112
|
-
hasMore: total > (page + 1) * perPage
|
|
113
|
-
};
|
|
114
|
-
} catch (error$1) {
|
|
115
|
-
throw new error.MastraError(
|
|
116
|
-
{
|
|
117
|
-
id: "LANCE_STORE_GET_EVALS_FAILED",
|
|
118
|
-
domain: error.ErrorDomain.STORAGE,
|
|
119
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
120
|
-
details: { agentName: options.agentName ?? "" }
|
|
121
|
-
},
|
|
122
|
-
error$1
|
|
123
|
-
);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
};
|
|
127
13
|
function getPrimaryKeys(tableName) {
|
|
128
14
|
let primaryId = ["id"];
|
|
129
15
|
if (tableName === storage.TABLE_WORKFLOW_SNAPSHOT) {
|
|
130
16
|
primaryId = ["workflow_name", "run_id"];
|
|
131
|
-
} else if (tableName === storage.TABLE_EVALS) {
|
|
132
|
-
primaryId = ["agent_name", "metric_name", "run_id"];
|
|
133
17
|
}
|
|
134
18
|
return primaryId;
|
|
135
19
|
}
|
|
@@ -249,6 +133,10 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
249
133
|
this.client = client;
|
|
250
134
|
this.operations = operations;
|
|
251
135
|
}
|
|
136
|
+
// Utility to escape single quotes in SQL strings
|
|
137
|
+
escapeSql(str) {
|
|
138
|
+
return str.replace(/'/g, "''");
|
|
139
|
+
}
|
|
252
140
|
async getThreadById({ threadId }) {
|
|
253
141
|
try {
|
|
254
142
|
const thread = await this.operations.load({ tableName: storage.TABLE_THREADS, keys: { id: threadId } });
|
|
@@ -271,26 +159,6 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
271
159
|
);
|
|
272
160
|
}
|
|
273
161
|
}
|
|
274
|
-
async getThreadsByResourceId({ resourceId }) {
|
|
275
|
-
try {
|
|
276
|
-
const table = await this.client.openTable(storage.TABLE_THREADS);
|
|
277
|
-
const query = table.query().where(`\`resourceId\` = '${resourceId}'`);
|
|
278
|
-
const records = await query.toArray();
|
|
279
|
-
return processResultWithTypeConversion(
|
|
280
|
-
records,
|
|
281
|
-
await getTableSchema({ tableName: storage.TABLE_THREADS, client: this.client })
|
|
282
|
-
);
|
|
283
|
-
} catch (error$1) {
|
|
284
|
-
throw new error.MastraError(
|
|
285
|
-
{
|
|
286
|
-
id: "LANCE_STORE_GET_THREADS_BY_RESOURCE_ID_FAILED",
|
|
287
|
-
domain: error.ErrorDomain.STORAGE,
|
|
288
|
-
category: error.ErrorCategory.THIRD_PARTY
|
|
289
|
-
},
|
|
290
|
-
error$1
|
|
291
|
-
);
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
162
|
/**
|
|
295
163
|
* Saves a thread to the database. This function doesn't overwrite existing threads.
|
|
296
164
|
* @param thread - The thread to save
|
|
@@ -395,100 +263,174 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
395
263
|
})() : message.content
|
|
396
264
|
};
|
|
397
265
|
}
|
|
398
|
-
async
|
|
399
|
-
|
|
400
|
-
resourceId,
|
|
401
|
-
selectBy,
|
|
402
|
-
format,
|
|
403
|
-
threadConfig
|
|
404
|
-
}) {
|
|
266
|
+
async listMessagesById({ messageIds }) {
|
|
267
|
+
if (messageIds.length === 0) return { messages: [] };
|
|
405
268
|
try {
|
|
406
|
-
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
407
|
-
if (threadConfig) {
|
|
408
|
-
throw new Error("ThreadConfig is not supported by LanceDB storage");
|
|
409
|
-
}
|
|
410
|
-
const limit = storage.resolveMessageLimit({ last: selectBy?.last, defaultLimit: Number.MAX_SAFE_INTEGER });
|
|
411
269
|
const table = await this.client.openTable(storage.TABLE_MESSAGES);
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
const threadIds = [...new Set(selectBy.include.map((item) => item.threadId))];
|
|
415
|
-
for (const threadId2 of threadIds) {
|
|
416
|
-
const threadQuery = table.query().where(`thread_id = '${threadId2}'`);
|
|
417
|
-
let threadRecords = await threadQuery.toArray();
|
|
418
|
-
allRecords.push(...threadRecords);
|
|
419
|
-
}
|
|
420
|
-
} else {
|
|
421
|
-
let query = table.query().where(`\`thread_id\` = '${threadId}'`);
|
|
422
|
-
allRecords = await query.toArray();
|
|
423
|
-
}
|
|
424
|
-
allRecords.sort((a, b) => {
|
|
425
|
-
const dateA = new Date(a.createdAt).getTime();
|
|
426
|
-
const dateB = new Date(b.createdAt).getTime();
|
|
427
|
-
return dateA - dateB;
|
|
428
|
-
});
|
|
429
|
-
if (selectBy?.include && selectBy.include.length > 0) {
|
|
430
|
-
allRecords = this.processMessagesWithContext(allRecords, selectBy.include);
|
|
431
|
-
}
|
|
432
|
-
if (limit !== Number.MAX_SAFE_INTEGER) {
|
|
433
|
-
allRecords = allRecords.slice(-limit);
|
|
434
|
-
}
|
|
270
|
+
const quotedIds = messageIds.map((id) => `'${id}'`).join(", ");
|
|
271
|
+
const allRecords = await table.query().where(`id IN (${quotedIds})`).toArray();
|
|
435
272
|
const messages = processResultWithTypeConversion(
|
|
436
273
|
allRecords,
|
|
437
274
|
await getTableSchema({ tableName: storage.TABLE_MESSAGES, client: this.client })
|
|
438
275
|
);
|
|
439
|
-
const list = new agent.MessageList(
|
|
440
|
-
|
|
441
|
-
|
|
276
|
+
const list = new agent.MessageList().add(
|
|
277
|
+
messages.map(this.normalizeMessage),
|
|
278
|
+
"memory"
|
|
279
|
+
);
|
|
280
|
+
return { messages: list.get.all.db() };
|
|
442
281
|
} catch (error$1) {
|
|
443
282
|
throw new error.MastraError(
|
|
444
283
|
{
|
|
445
|
-
id: "
|
|
284
|
+
id: "LANCE_STORE_LIST_MESSAGES_BY_ID_FAILED",
|
|
446
285
|
domain: error.ErrorDomain.STORAGE,
|
|
447
286
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
448
287
|
details: {
|
|
449
|
-
|
|
450
|
-
resourceId: resourceId ?? ""
|
|
288
|
+
messageIds: JSON.stringify(messageIds)
|
|
451
289
|
}
|
|
452
290
|
},
|
|
453
291
|
error$1
|
|
454
292
|
);
|
|
455
293
|
}
|
|
456
294
|
}
|
|
457
|
-
async
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
295
|
+
async listMessages(args) {
|
|
296
|
+
const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
|
|
297
|
+
if (!threadId.trim()) {
|
|
298
|
+
throw new error.MastraError(
|
|
299
|
+
{
|
|
300
|
+
id: "STORAGE_LANCE_LIST_MESSAGES_INVALID_THREAD_ID",
|
|
301
|
+
domain: error.ErrorDomain.STORAGE,
|
|
302
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
303
|
+
details: { threadId }
|
|
304
|
+
},
|
|
305
|
+
new Error("threadId must be a non-empty string")
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
const perPage = storage.normalizePerPage(perPageInput, 40);
|
|
309
|
+
const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
462
310
|
try {
|
|
311
|
+
if (page < 0) {
|
|
312
|
+
throw new error.MastraError(
|
|
313
|
+
{
|
|
314
|
+
id: "STORAGE_LANCE_LIST_MESSAGES_INVALID_PAGE",
|
|
315
|
+
domain: error.ErrorDomain.STORAGE,
|
|
316
|
+
category: error.ErrorCategory.USER,
|
|
317
|
+
details: { page }
|
|
318
|
+
},
|
|
319
|
+
new Error("page must be >= 0")
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
const { field, direction } = this.parseOrderBy(orderBy, "ASC");
|
|
463
323
|
const table = await this.client.openTable(storage.TABLE_MESSAGES);
|
|
464
|
-
const
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
324
|
+
const conditions = [`thread_id = '${this.escapeSql(threadId)}'`];
|
|
325
|
+
if (resourceId) {
|
|
326
|
+
conditions.push(`\`resourceId\` = '${this.escapeSql(resourceId)}'`);
|
|
327
|
+
}
|
|
328
|
+
if (filter?.dateRange?.start) {
|
|
329
|
+
const startTime = filter.dateRange.start instanceof Date ? filter.dateRange.start.getTime() : new Date(filter.dateRange.start).getTime();
|
|
330
|
+
conditions.push(`\`createdAt\` >= ${startTime}`);
|
|
331
|
+
}
|
|
332
|
+
if (filter?.dateRange?.end) {
|
|
333
|
+
const endTime = filter.dateRange.end instanceof Date ? filter.dateRange.end.getTime() : new Date(filter.dateRange.end).getTime();
|
|
334
|
+
conditions.push(`\`createdAt\` <= ${endTime}`);
|
|
335
|
+
}
|
|
336
|
+
const whereClause = conditions.join(" AND ");
|
|
337
|
+
const total = await table.countRows(whereClause);
|
|
338
|
+
const query = table.query().where(whereClause);
|
|
339
|
+
let allRecords = await query.toArray();
|
|
340
|
+
allRecords.sort((a, b) => {
|
|
341
|
+
const aValue = field === "createdAt" ? a.createdAt : a[field];
|
|
342
|
+
const bValue = field === "createdAt" ? b.createdAt : b[field];
|
|
343
|
+
if (aValue == null && bValue == null) return 0;
|
|
344
|
+
if (aValue == null) return direction === "ASC" ? -1 : 1;
|
|
345
|
+
if (bValue == null) return direction === "ASC" ? 1 : -1;
|
|
346
|
+
if (typeof aValue === "string" && typeof bValue === "string") {
|
|
347
|
+
return direction === "ASC" ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue);
|
|
348
|
+
}
|
|
349
|
+
return direction === "ASC" ? aValue - bValue : bValue - aValue;
|
|
350
|
+
});
|
|
351
|
+
const paginatedRecords = allRecords.slice(offset, offset + perPage);
|
|
352
|
+
const messages = paginatedRecords.map((row) => this.normalizeMessage(row));
|
|
353
|
+
if (total === 0 && messages.length === 0 && (!include || include.length === 0)) {
|
|
354
|
+
return {
|
|
355
|
+
messages: [],
|
|
356
|
+
total: 0,
|
|
357
|
+
page,
|
|
358
|
+
perPage: perPageForResponse,
|
|
359
|
+
hasMore: false
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
const messageIds = new Set(messages.map((m) => m.id));
|
|
363
|
+
if (include && include.length > 0) {
|
|
364
|
+
const threadIds = [...new Set(include.map((item) => item.threadId || threadId))];
|
|
365
|
+
const allThreadMessages = [];
|
|
366
|
+
for (const tid of threadIds) {
|
|
367
|
+
const threadQuery = table.query().where(`thread_id = '${tid}'`);
|
|
368
|
+
let threadRecords = await threadQuery.toArray();
|
|
369
|
+
allThreadMessages.push(...threadRecords);
|
|
370
|
+
}
|
|
371
|
+
allThreadMessages.sort((a, b) => a.createdAt - b.createdAt);
|
|
372
|
+
const contextMessages = this.processMessagesWithContext(allThreadMessages, include);
|
|
373
|
+
const includedMessages = contextMessages.map((row) => this.normalizeMessage(row));
|
|
374
|
+
for (const includeMsg of includedMessages) {
|
|
375
|
+
if (!messageIds.has(includeMsg.id)) {
|
|
376
|
+
messages.push(includeMsg);
|
|
377
|
+
messageIds.add(includeMsg.id);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
const list = new agent.MessageList().add(messages, "memory");
|
|
382
|
+
let finalMessages = list.get.all.db();
|
|
383
|
+
finalMessages = finalMessages.sort((a, b) => {
|
|
384
|
+
const aValue = field === "createdAt" ? new Date(a.createdAt).getTime() : a[field];
|
|
385
|
+
const bValue = field === "createdAt" ? new Date(b.createdAt).getTime() : b[field];
|
|
386
|
+
if (aValue == null && bValue == null) return 0;
|
|
387
|
+
if (aValue == null) return direction === "ASC" ? -1 : 1;
|
|
388
|
+
if (bValue == null) return direction === "ASC" ? 1 : -1;
|
|
389
|
+
if (typeof aValue === "string" && typeof bValue === "string") {
|
|
390
|
+
return direction === "ASC" ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue);
|
|
391
|
+
}
|
|
392
|
+
return direction === "ASC" ? aValue - bValue : bValue - aValue;
|
|
393
|
+
});
|
|
394
|
+
const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
|
|
395
|
+
const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
|
|
396
|
+
const fetchedAll = perPageInput === false || allThreadMessagesReturned;
|
|
397
|
+
const hasMore = !fetchedAll && offset + perPage < total;
|
|
398
|
+
return {
|
|
399
|
+
messages: finalMessages,
|
|
400
|
+
total,
|
|
401
|
+
page,
|
|
402
|
+
perPage: perPageForResponse,
|
|
403
|
+
hasMore
|
|
404
|
+
};
|
|
473
405
|
} catch (error$1) {
|
|
474
|
-
|
|
406
|
+
const mastraError = new error.MastraError(
|
|
475
407
|
{
|
|
476
|
-
id: "
|
|
408
|
+
id: "LANCE_STORE_LIST_MESSAGES_FAILED",
|
|
477
409
|
domain: error.ErrorDomain.STORAGE,
|
|
478
410
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
479
411
|
details: {
|
|
480
|
-
|
|
412
|
+
threadId,
|
|
413
|
+
resourceId: resourceId ?? ""
|
|
481
414
|
}
|
|
482
415
|
},
|
|
483
416
|
error$1
|
|
484
417
|
);
|
|
418
|
+
this.logger?.error?.(mastraError.toString());
|
|
419
|
+
this.logger?.trackException?.(mastraError);
|
|
420
|
+
return {
|
|
421
|
+
messages: [],
|
|
422
|
+
total: 0,
|
|
423
|
+
page,
|
|
424
|
+
perPage: perPageForResponse,
|
|
425
|
+
hasMore: false
|
|
426
|
+
};
|
|
485
427
|
}
|
|
486
428
|
}
|
|
487
429
|
async saveMessages(args) {
|
|
488
430
|
try {
|
|
489
|
-
const { messages
|
|
431
|
+
const { messages } = args;
|
|
490
432
|
if (messages.length === 0) {
|
|
491
|
-
return [];
|
|
433
|
+
return { messages: [] };
|
|
492
434
|
}
|
|
493
435
|
const threadId = messages[0]?.threadId;
|
|
494
436
|
if (!threadId) {
|
|
@@ -524,8 +466,7 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
524
466
|
const updateRecord = { id: threadId, updatedAt: currentTime };
|
|
525
467
|
await threadsTable.mergeInsert("id").whenMatchedUpdateAll().whenNotMatchedInsertAll().execute([updateRecord]);
|
|
526
468
|
const list = new agent.MessageList().add(messages, "memory");
|
|
527
|
-
|
|
528
|
-
return list.get.all.v1();
|
|
469
|
+
return { messages: list.get.all.db() };
|
|
529
470
|
} catch (error$1) {
|
|
530
471
|
throw new error.MastraError(
|
|
531
472
|
{
|
|
@@ -537,32 +478,54 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
537
478
|
);
|
|
538
479
|
}
|
|
539
480
|
}
|
|
540
|
-
async
|
|
481
|
+
async listThreadsByResourceId(args) {
|
|
541
482
|
try {
|
|
542
|
-
const { resourceId, page = 0, perPage
|
|
543
|
-
const
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
483
|
+
const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
|
|
484
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
485
|
+
if (page < 0) {
|
|
486
|
+
throw new error.MastraError(
|
|
487
|
+
{
|
|
488
|
+
id: "STORAGE_LANCE_LIST_THREADS_BY_RESOURCE_ID_INVALID_PAGE",
|
|
489
|
+
domain: error.ErrorDomain.STORAGE,
|
|
490
|
+
category: error.ErrorCategory.USER,
|
|
491
|
+
details: { page }
|
|
492
|
+
},
|
|
493
|
+
new Error("page must be >= 0")
|
|
494
|
+
);
|
|
550
495
|
}
|
|
496
|
+
const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
497
|
+
const { field, direction } = this.parseOrderBy(orderBy);
|
|
498
|
+
const table = await this.client.openTable(storage.TABLE_THREADS);
|
|
499
|
+
const total = await table.countRows(`\`resourceId\` = '${this.escapeSql(resourceId)}'`);
|
|
500
|
+
const query = table.query().where(`\`resourceId\` = '${this.escapeSql(resourceId)}'`);
|
|
551
501
|
const records = await query.toArray();
|
|
552
|
-
records.sort((a, b) =>
|
|
502
|
+
records.sort((a, b) => {
|
|
503
|
+
const aValue = ["createdAt", "updatedAt"].includes(field) ? new Date(a[field]).getTime() : a[field];
|
|
504
|
+
const bValue = ["createdAt", "updatedAt"].includes(field) ? new Date(b[field]).getTime() : b[field];
|
|
505
|
+
if (aValue == null && bValue == null) return 0;
|
|
506
|
+
if (aValue == null) return direction === "ASC" ? -1 : 1;
|
|
507
|
+
if (bValue == null) return direction === "ASC" ? 1 : -1;
|
|
508
|
+
if (typeof aValue === "string" && typeof bValue === "string") {
|
|
509
|
+
return direction === "ASC" ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue);
|
|
510
|
+
}
|
|
511
|
+
return direction === "ASC" ? aValue - bValue : bValue - aValue;
|
|
512
|
+
});
|
|
513
|
+
const paginatedRecords = records.slice(offset, offset + perPage);
|
|
553
514
|
const schema = await getTableSchema({ tableName: storage.TABLE_THREADS, client: this.client });
|
|
554
|
-
const threads =
|
|
515
|
+
const threads = paginatedRecords.map(
|
|
516
|
+
(record) => processResultWithTypeConversion(record, schema)
|
|
517
|
+
);
|
|
555
518
|
return {
|
|
556
519
|
threads,
|
|
557
520
|
total,
|
|
558
521
|
page,
|
|
559
|
-
perPage,
|
|
560
|
-
hasMore:
|
|
522
|
+
perPage: perPageForResponse,
|
|
523
|
+
hasMore: offset + perPage < total
|
|
561
524
|
};
|
|
562
525
|
} catch (error$1) {
|
|
563
526
|
throw new error.MastraError(
|
|
564
527
|
{
|
|
565
|
-
id: "
|
|
528
|
+
id: "LANCE_STORE_LIST_THREADS_BY_RESOURCE_ID_FAILED",
|
|
566
529
|
domain: error.ErrorDomain.STORAGE,
|
|
567
530
|
category: error.ErrorCategory.THIRD_PARTY
|
|
568
531
|
},
|
|
@@ -618,132 +581,8 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
618
581
|
});
|
|
619
582
|
return Array.from(allIndices).sort((a, b) => a - b).map((index) => records[index]);
|
|
620
583
|
}
|
|
621
|
-
async getMessagesPaginated(args) {
|
|
622
|
-
const { threadId, resourceId, selectBy, format = "v1" } = args;
|
|
623
|
-
const page = selectBy?.pagination?.page ?? 0;
|
|
624
|
-
const perPage = selectBy?.pagination?.perPage ?? 10;
|
|
625
|
-
try {
|
|
626
|
-
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
627
|
-
const dateRange = selectBy?.pagination?.dateRange;
|
|
628
|
-
const fromDate = dateRange?.start;
|
|
629
|
-
const toDate = dateRange?.end;
|
|
630
|
-
const table = await this.client.openTable(storage.TABLE_MESSAGES);
|
|
631
|
-
const messages = [];
|
|
632
|
-
if (selectBy?.include && Array.isArray(selectBy.include)) {
|
|
633
|
-
const threadIds = [...new Set(selectBy.include.map((item) => item.threadId))];
|
|
634
|
-
const allThreadMessages = [];
|
|
635
|
-
for (const threadId2 of threadIds) {
|
|
636
|
-
const threadQuery = table.query().where(`thread_id = '${threadId2}'`);
|
|
637
|
-
let threadRecords = await threadQuery.toArray();
|
|
638
|
-
if (fromDate) threadRecords = threadRecords.filter((m) => m.createdAt >= fromDate.getTime());
|
|
639
|
-
if (toDate) threadRecords = threadRecords.filter((m) => m.createdAt <= toDate.getTime());
|
|
640
|
-
allThreadMessages.push(...threadRecords);
|
|
641
|
-
}
|
|
642
|
-
allThreadMessages.sort((a, b) => a.createdAt - b.createdAt);
|
|
643
|
-
const contextMessages = this.processMessagesWithContext(allThreadMessages, selectBy.include);
|
|
644
|
-
messages.push(...contextMessages);
|
|
645
|
-
}
|
|
646
|
-
const conditions = [`thread_id = '${threadId}'`];
|
|
647
|
-
if (resourceId) {
|
|
648
|
-
conditions.push(`\`resourceId\` = '${resourceId}'`);
|
|
649
|
-
}
|
|
650
|
-
if (fromDate) {
|
|
651
|
-
conditions.push(`\`createdAt\` >= ${fromDate.getTime()}`);
|
|
652
|
-
}
|
|
653
|
-
if (toDate) {
|
|
654
|
-
conditions.push(`\`createdAt\` <= ${toDate.getTime()}`);
|
|
655
|
-
}
|
|
656
|
-
let total = 0;
|
|
657
|
-
if (conditions.length > 0) {
|
|
658
|
-
total = await table.countRows(conditions.join(" AND "));
|
|
659
|
-
} else {
|
|
660
|
-
total = await table.countRows();
|
|
661
|
-
}
|
|
662
|
-
if (total === 0 && messages.length === 0) {
|
|
663
|
-
return {
|
|
664
|
-
messages: [],
|
|
665
|
-
total: 0,
|
|
666
|
-
page,
|
|
667
|
-
perPage,
|
|
668
|
-
hasMore: false
|
|
669
|
-
};
|
|
670
|
-
}
|
|
671
|
-
const excludeIds = messages.map((m) => m.id);
|
|
672
|
-
let selectedMessages = [];
|
|
673
|
-
if (selectBy?.last && selectBy.last > 0) {
|
|
674
|
-
const query = table.query();
|
|
675
|
-
if (conditions.length > 0) {
|
|
676
|
-
query.where(conditions.join(" AND "));
|
|
677
|
-
}
|
|
678
|
-
let records = await query.toArray();
|
|
679
|
-
records = records.sort((a, b) => a.createdAt - b.createdAt);
|
|
680
|
-
if (excludeIds.length > 0) {
|
|
681
|
-
records = records.filter((m) => !excludeIds.includes(m.id));
|
|
682
|
-
}
|
|
683
|
-
selectedMessages = records.slice(-selectBy.last);
|
|
684
|
-
} else {
|
|
685
|
-
const query = table.query();
|
|
686
|
-
if (conditions.length > 0) {
|
|
687
|
-
query.where(conditions.join(" AND "));
|
|
688
|
-
}
|
|
689
|
-
let records = await query.toArray();
|
|
690
|
-
records = records.sort((a, b) => a.createdAt - b.createdAt);
|
|
691
|
-
if (excludeIds.length > 0) {
|
|
692
|
-
records = records.filter((m) => !excludeIds.includes(m.id));
|
|
693
|
-
}
|
|
694
|
-
selectedMessages = records.slice(page * perPage, (page + 1) * perPage);
|
|
695
|
-
}
|
|
696
|
-
const allMessages = [...messages, ...selectedMessages];
|
|
697
|
-
const seen = /* @__PURE__ */ new Set();
|
|
698
|
-
const dedupedMessages = allMessages.filter((m) => {
|
|
699
|
-
const key = `${m.id}:${m.thread_id}`;
|
|
700
|
-
if (seen.has(key)) return false;
|
|
701
|
-
seen.add(key);
|
|
702
|
-
return true;
|
|
703
|
-
});
|
|
704
|
-
const formattedMessages = dedupedMessages.map((msg) => {
|
|
705
|
-
const { thread_id, ...rest } = msg;
|
|
706
|
-
return {
|
|
707
|
-
...rest,
|
|
708
|
-
threadId: thread_id,
|
|
709
|
-
content: typeof msg.content === "string" ? (() => {
|
|
710
|
-
try {
|
|
711
|
-
return JSON.parse(msg.content);
|
|
712
|
-
} catch {
|
|
713
|
-
return msg.content;
|
|
714
|
-
}
|
|
715
|
-
})() : msg.content
|
|
716
|
-
};
|
|
717
|
-
});
|
|
718
|
-
const list = new agent.MessageList().add(formattedMessages, "memory");
|
|
719
|
-
return {
|
|
720
|
-
messages: format === "v2" ? list.get.all.v2() : list.get.all.v1(),
|
|
721
|
-
total,
|
|
722
|
-
// Total should be the count of messages matching the filters
|
|
723
|
-
page,
|
|
724
|
-
perPage,
|
|
725
|
-
hasMore: total > (page + 1) * perPage
|
|
726
|
-
};
|
|
727
|
-
} catch (error$1) {
|
|
728
|
-
const mastraError = new error.MastraError(
|
|
729
|
-
{
|
|
730
|
-
id: "LANCE_STORE_GET_MESSAGES_PAGINATED_FAILED",
|
|
731
|
-
domain: error.ErrorDomain.STORAGE,
|
|
732
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
733
|
-
details: {
|
|
734
|
-
threadId,
|
|
735
|
-
resourceId: resourceId ?? ""
|
|
736
|
-
}
|
|
737
|
-
},
|
|
738
|
-
error$1
|
|
739
|
-
);
|
|
740
|
-
this.logger?.trackException?.(mastraError);
|
|
741
|
-
this.logger?.error?.(mastraError.toString());
|
|
742
|
-
return { messages: [], total: 0, page, perPage, hasMore: false };
|
|
743
|
-
}
|
|
744
|
-
}
|
|
745
584
|
/**
|
|
746
|
-
* Parse message data from LanceDB record format to
|
|
585
|
+
* Parse message data from LanceDB record format to MastraDBMessage format
|
|
747
586
|
*/
|
|
748
587
|
parseMessageData(data) {
|
|
749
588
|
const { thread_id, ...rest } = data;
|
|
@@ -1401,7 +1240,7 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1401
1240
|
async saveScore(score) {
|
|
1402
1241
|
let validatedScore;
|
|
1403
1242
|
try {
|
|
1404
|
-
validatedScore =
|
|
1243
|
+
validatedScore = evals.saveScorePayloadSchema.parse(score);
|
|
1405
1244
|
} catch (error$1) {
|
|
1406
1245
|
throw new error.MastraError(
|
|
1407
1246
|
{
|
|
@@ -1429,6 +1268,8 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1429
1268
|
filteredScore[key] = JSON.stringify(filteredScore[key]);
|
|
1430
1269
|
}
|
|
1431
1270
|
}
|
|
1271
|
+
filteredScore.createdAt = /* @__PURE__ */ new Date();
|
|
1272
|
+
filteredScore.updatedAt = /* @__PURE__ */ new Date();
|
|
1432
1273
|
filteredScore.id = id;
|
|
1433
1274
|
await table.add([filteredScore], { mode: "append" });
|
|
1434
1275
|
return { score };
|
|
@@ -1451,8 +1292,7 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1451
1292
|
const query = table.query().where(`id = '${id}'`).limit(1);
|
|
1452
1293
|
const records = await query.toArray();
|
|
1453
1294
|
if (records.length === 0) return null;
|
|
1454
|
-
|
|
1455
|
-
return processResultWithTypeConversion(records[0], schema);
|
|
1295
|
+
return await this.transformScoreRow(records[0]);
|
|
1456
1296
|
} catch (error$1) {
|
|
1457
1297
|
throw new error.MastraError(
|
|
1458
1298
|
{
|
|
@@ -1466,7 +1306,17 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1466
1306
|
);
|
|
1467
1307
|
}
|
|
1468
1308
|
}
|
|
1469
|
-
async
|
|
1309
|
+
async transformScoreRow(row) {
|
|
1310
|
+
const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
|
|
1311
|
+
const transformed = processResultWithTypeConversion(row, schema);
|
|
1312
|
+
const result = {
|
|
1313
|
+
...transformed,
|
|
1314
|
+
createdAt: row.createdAt,
|
|
1315
|
+
updatedAt: row.updatedAt
|
|
1316
|
+
};
|
|
1317
|
+
return result;
|
|
1318
|
+
}
|
|
1319
|
+
async listScoresByScorerId({
|
|
1470
1320
|
scorerId,
|
|
1471
1321
|
pagination,
|
|
1472
1322
|
entityId,
|
|
@@ -1474,9 +1324,10 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1474
1324
|
source
|
|
1475
1325
|
}) {
|
|
1476
1326
|
try {
|
|
1327
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1328
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
1329
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1477
1330
|
const table = await this.client.openTable(storage.TABLE_SCORERS);
|
|
1478
|
-
const { page = 0, perPage = 10 } = pagination || {};
|
|
1479
|
-
const offset = page * perPage;
|
|
1480
1331
|
let query = table.query().where(`\`scorerId\` = '${scorerId}'`);
|
|
1481
1332
|
if (source) {
|
|
1482
1333
|
query = query.where(`\`source\` = '${source}'`);
|
|
@@ -1487,23 +1338,31 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1487
1338
|
if (entityType) {
|
|
1488
1339
|
query = query.where(`\`entityType\` = '${entityType}'`);
|
|
1489
1340
|
}
|
|
1490
|
-
query = query.limit(perPage);
|
|
1491
|
-
if (offset > 0) query.offset(offset);
|
|
1492
|
-
const records = await query.toArray();
|
|
1493
|
-
const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
|
|
1494
|
-
const scores = processResultWithTypeConversion(records, schema);
|
|
1495
1341
|
let totalQuery = table.query().where(`\`scorerId\` = '${scorerId}'`);
|
|
1496
1342
|
if (source) {
|
|
1497
1343
|
totalQuery = totalQuery.where(`\`source\` = '${source}'`);
|
|
1498
1344
|
}
|
|
1345
|
+
if (entityId) {
|
|
1346
|
+
totalQuery = totalQuery.where(`\`entityId\` = '${entityId}'`);
|
|
1347
|
+
}
|
|
1348
|
+
if (entityType) {
|
|
1349
|
+
totalQuery = totalQuery.where(`\`entityType\` = '${entityType}'`);
|
|
1350
|
+
}
|
|
1499
1351
|
const allRecords = await totalQuery.toArray();
|
|
1500
1352
|
const total = allRecords.length;
|
|
1353
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1354
|
+
if (perPageInput !== false) {
|
|
1355
|
+
query = query.limit(perPage);
|
|
1356
|
+
if (start > 0) query = query.offset(start);
|
|
1357
|
+
}
|
|
1358
|
+
const records = await query.toArray();
|
|
1359
|
+
const scores = await Promise.all(records.map(async (record) => await this.transformScoreRow(record)));
|
|
1501
1360
|
return {
|
|
1502
1361
|
pagination: {
|
|
1503
1362
|
page,
|
|
1504
|
-
perPage,
|
|
1363
|
+
perPage: perPageForResponse,
|
|
1505
1364
|
total,
|
|
1506
|
-
hasMore:
|
|
1365
|
+
hasMore: end < total
|
|
1507
1366
|
},
|
|
1508
1367
|
scores
|
|
1509
1368
|
};
|
|
@@ -1520,27 +1379,31 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1520
1379
|
);
|
|
1521
1380
|
}
|
|
1522
1381
|
}
|
|
1523
|
-
async
|
|
1382
|
+
async listScoresByRunId({
|
|
1524
1383
|
runId,
|
|
1525
1384
|
pagination
|
|
1526
1385
|
}) {
|
|
1527
1386
|
try {
|
|
1387
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1388
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
1389
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1528
1390
|
const table = await this.client.openTable(storage.TABLE_SCORERS);
|
|
1529
|
-
const { page = 0, perPage = 10 } = pagination || {};
|
|
1530
|
-
const offset = page * perPage;
|
|
1531
|
-
const query = table.query().where(`\`runId\` = '${runId}'`).limit(perPage);
|
|
1532
|
-
if (offset > 0) query.offset(offset);
|
|
1533
|
-
const records = await query.toArray();
|
|
1534
|
-
const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
|
|
1535
|
-
const scores = processResultWithTypeConversion(records, schema);
|
|
1536
1391
|
const allRecords = await table.query().where(`\`runId\` = '${runId}'`).toArray();
|
|
1537
1392
|
const total = allRecords.length;
|
|
1393
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1394
|
+
let query = table.query().where(`\`runId\` = '${runId}'`);
|
|
1395
|
+
if (perPageInput !== false) {
|
|
1396
|
+
query = query.limit(perPage);
|
|
1397
|
+
if (start > 0) query = query.offset(start);
|
|
1398
|
+
}
|
|
1399
|
+
const records = await query.toArray();
|
|
1400
|
+
const scores = await Promise.all(records.map(async (record) => await this.transformScoreRow(record)));
|
|
1538
1401
|
return {
|
|
1539
1402
|
pagination: {
|
|
1540
1403
|
page,
|
|
1541
|
-
perPage,
|
|
1404
|
+
perPage: perPageForResponse,
|
|
1542
1405
|
total,
|
|
1543
|
-
hasMore:
|
|
1406
|
+
hasMore: end < total
|
|
1544
1407
|
},
|
|
1545
1408
|
scores
|
|
1546
1409
|
};
|
|
@@ -1557,28 +1420,32 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1557
1420
|
);
|
|
1558
1421
|
}
|
|
1559
1422
|
}
|
|
1560
|
-
async
|
|
1423
|
+
async listScoresByEntityId({
|
|
1561
1424
|
entityId,
|
|
1562
1425
|
entityType,
|
|
1563
1426
|
pagination
|
|
1564
1427
|
}) {
|
|
1565
1428
|
try {
|
|
1429
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1430
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
1431
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1566
1432
|
const table = await this.client.openTable(storage.TABLE_SCORERS);
|
|
1567
|
-
const { page = 0, perPage = 10 } = pagination || {};
|
|
1568
|
-
const offset = page * perPage;
|
|
1569
|
-
const query = table.query().where(`\`entityId\` = '${entityId}' AND \`entityType\` = '${entityType}'`).limit(perPage);
|
|
1570
|
-
if (offset > 0) query.offset(offset);
|
|
1571
|
-
const records = await query.toArray();
|
|
1572
|
-
const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
|
|
1573
|
-
const scores = processResultWithTypeConversion(records, schema);
|
|
1574
1433
|
const allRecords = await table.query().where(`\`entityId\` = '${entityId}' AND \`entityType\` = '${entityType}'`).toArray();
|
|
1575
1434
|
const total = allRecords.length;
|
|
1435
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1436
|
+
let query = table.query().where(`\`entityId\` = '${entityId}' AND \`entityType\` = '${entityType}'`);
|
|
1437
|
+
if (perPageInput !== false) {
|
|
1438
|
+
query = query.limit(perPage);
|
|
1439
|
+
if (start > 0) query = query.offset(start);
|
|
1440
|
+
}
|
|
1441
|
+
const records = await query.toArray();
|
|
1442
|
+
const scores = await Promise.all(records.map(async (record) => await this.transformScoreRow(record)));
|
|
1576
1443
|
return {
|
|
1577
1444
|
pagination: {
|
|
1578
1445
|
page,
|
|
1579
|
-
perPage,
|
|
1446
|
+
perPage: perPageForResponse,
|
|
1580
1447
|
total,
|
|
1581
|
-
hasMore:
|
|
1448
|
+
hasMore: end < total
|
|
1582
1449
|
},
|
|
1583
1450
|
scores
|
|
1584
1451
|
};
|
|
@@ -1595,28 +1462,32 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1595
1462
|
);
|
|
1596
1463
|
}
|
|
1597
1464
|
}
|
|
1598
|
-
async
|
|
1465
|
+
async listScoresBySpan({
|
|
1599
1466
|
traceId,
|
|
1600
1467
|
spanId,
|
|
1601
1468
|
pagination
|
|
1602
1469
|
}) {
|
|
1603
1470
|
try {
|
|
1471
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1472
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
1473
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1604
1474
|
const table = await this.client.openTable(storage.TABLE_SCORERS);
|
|
1605
|
-
const { page = 0, perPage = 10 } = pagination || {};
|
|
1606
|
-
const offset = page * perPage;
|
|
1607
|
-
const query = table.query().where(`\`traceId\` = '${traceId}' AND \`spanId\` = '${spanId}'`).limit(perPage);
|
|
1608
|
-
if (offset > 0) query.offset(offset);
|
|
1609
|
-
const records = await query.toArray();
|
|
1610
|
-
const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
|
|
1611
|
-
const scores = processResultWithTypeConversion(records, schema);
|
|
1612
1475
|
const allRecords = await table.query().where(`\`traceId\` = '${traceId}' AND \`spanId\` = '${spanId}'`).toArray();
|
|
1613
1476
|
const total = allRecords.length;
|
|
1477
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1478
|
+
let query = table.query().where(`\`traceId\` = '${traceId}' AND \`spanId\` = '${spanId}'`);
|
|
1479
|
+
if (perPageInput !== false) {
|
|
1480
|
+
query = query.limit(perPage);
|
|
1481
|
+
if (start > 0) query = query.offset(start);
|
|
1482
|
+
}
|
|
1483
|
+
const records = await query.toArray();
|
|
1484
|
+
const scores = await Promise.all(records.map(async (record) => await this.transformScoreRow(record)));
|
|
1614
1485
|
return {
|
|
1615
1486
|
pagination: {
|
|
1616
1487
|
page,
|
|
1617
|
-
perPage,
|
|
1488
|
+
perPage: perPageForResponse,
|
|
1618
1489
|
total,
|
|
1619
|
-
hasMore:
|
|
1490
|
+
hasMore: end < total
|
|
1620
1491
|
},
|
|
1621
1492
|
scores
|
|
1622
1493
|
};
|
|
@@ -1634,198 +1505,6 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1634
1505
|
}
|
|
1635
1506
|
}
|
|
1636
1507
|
};
|
|
1637
|
-
var StoreTracesLance = class extends storage.TracesStorage {
|
|
1638
|
-
client;
|
|
1639
|
-
operations;
|
|
1640
|
-
constructor({ client, operations }) {
|
|
1641
|
-
super();
|
|
1642
|
-
this.client = client;
|
|
1643
|
-
this.operations = operations;
|
|
1644
|
-
}
|
|
1645
|
-
async saveTrace({ trace }) {
|
|
1646
|
-
try {
|
|
1647
|
-
const table = await this.client.openTable(storage.TABLE_TRACES);
|
|
1648
|
-
const record = {
|
|
1649
|
-
...trace,
|
|
1650
|
-
attributes: JSON.stringify(trace.attributes),
|
|
1651
|
-
status: JSON.stringify(trace.status),
|
|
1652
|
-
events: JSON.stringify(trace.events),
|
|
1653
|
-
links: JSON.stringify(trace.links),
|
|
1654
|
-
other: JSON.stringify(trace.other)
|
|
1655
|
-
};
|
|
1656
|
-
await table.add([record], { mode: "append" });
|
|
1657
|
-
return trace;
|
|
1658
|
-
} catch (error$1) {
|
|
1659
|
-
throw new error.MastraError(
|
|
1660
|
-
{
|
|
1661
|
-
id: "LANCE_STORE_SAVE_TRACE_FAILED",
|
|
1662
|
-
domain: error.ErrorDomain.STORAGE,
|
|
1663
|
-
category: error.ErrorCategory.THIRD_PARTY
|
|
1664
|
-
},
|
|
1665
|
-
error$1
|
|
1666
|
-
);
|
|
1667
|
-
}
|
|
1668
|
-
}
|
|
1669
|
-
async getTraceById({ traceId }) {
|
|
1670
|
-
try {
|
|
1671
|
-
const table = await this.client.openTable(storage.TABLE_TRACES);
|
|
1672
|
-
const query = table.query().where(`id = '${traceId}'`);
|
|
1673
|
-
const records = await query.toArray();
|
|
1674
|
-
return records[0];
|
|
1675
|
-
} catch (error$1) {
|
|
1676
|
-
throw new error.MastraError(
|
|
1677
|
-
{
|
|
1678
|
-
id: "LANCE_STORE_GET_TRACE_BY_ID_FAILED",
|
|
1679
|
-
domain: error.ErrorDomain.STORAGE,
|
|
1680
|
-
category: error.ErrorCategory.THIRD_PARTY
|
|
1681
|
-
},
|
|
1682
|
-
error$1
|
|
1683
|
-
);
|
|
1684
|
-
}
|
|
1685
|
-
}
|
|
1686
|
-
async getTraces({
|
|
1687
|
-
name,
|
|
1688
|
-
scope,
|
|
1689
|
-
page = 1,
|
|
1690
|
-
perPage = 10,
|
|
1691
|
-
attributes
|
|
1692
|
-
}) {
|
|
1693
|
-
try {
|
|
1694
|
-
const table = await this.client.openTable(storage.TABLE_TRACES);
|
|
1695
|
-
const query = table.query();
|
|
1696
|
-
if (name) {
|
|
1697
|
-
query.where(`name = '${name}'`);
|
|
1698
|
-
}
|
|
1699
|
-
if (scope) {
|
|
1700
|
-
query.where(`scope = '${scope}'`);
|
|
1701
|
-
}
|
|
1702
|
-
if (attributes) {
|
|
1703
|
-
query.where(`attributes = '${JSON.stringify(attributes)}'`);
|
|
1704
|
-
}
|
|
1705
|
-
const offset = (page - 1) * perPage;
|
|
1706
|
-
query.limit(perPage);
|
|
1707
|
-
if (offset > 0) {
|
|
1708
|
-
query.offset(offset);
|
|
1709
|
-
}
|
|
1710
|
-
const records = await query.toArray();
|
|
1711
|
-
return records.map((record) => {
|
|
1712
|
-
const processed = {
|
|
1713
|
-
...record,
|
|
1714
|
-
attributes: record.attributes ? JSON.parse(record.attributes) : {},
|
|
1715
|
-
status: record.status ? JSON.parse(record.status) : {},
|
|
1716
|
-
events: record.events ? JSON.parse(record.events) : [],
|
|
1717
|
-
links: record.links ? JSON.parse(record.links) : [],
|
|
1718
|
-
other: record.other ? JSON.parse(record.other) : {},
|
|
1719
|
-
startTime: new Date(record.startTime),
|
|
1720
|
-
endTime: new Date(record.endTime),
|
|
1721
|
-
createdAt: new Date(record.createdAt)
|
|
1722
|
-
};
|
|
1723
|
-
if (processed.parentSpanId === null || processed.parentSpanId === void 0) {
|
|
1724
|
-
processed.parentSpanId = "";
|
|
1725
|
-
} else {
|
|
1726
|
-
processed.parentSpanId = String(processed.parentSpanId);
|
|
1727
|
-
}
|
|
1728
|
-
return processed;
|
|
1729
|
-
});
|
|
1730
|
-
} catch (error$1) {
|
|
1731
|
-
throw new error.MastraError(
|
|
1732
|
-
{
|
|
1733
|
-
id: "LANCE_STORE_GET_TRACES_FAILED",
|
|
1734
|
-
domain: error.ErrorDomain.STORAGE,
|
|
1735
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
1736
|
-
details: { name: name ?? "", scope: scope ?? "" }
|
|
1737
|
-
},
|
|
1738
|
-
error$1
|
|
1739
|
-
);
|
|
1740
|
-
}
|
|
1741
|
-
}
|
|
1742
|
-
async getTracesPaginated(args) {
|
|
1743
|
-
try {
|
|
1744
|
-
const table = await this.client.openTable(storage.TABLE_TRACES);
|
|
1745
|
-
const query = table.query();
|
|
1746
|
-
const conditions = [];
|
|
1747
|
-
if (args.name) {
|
|
1748
|
-
conditions.push(`name = '${args.name}'`);
|
|
1749
|
-
}
|
|
1750
|
-
if (args.scope) {
|
|
1751
|
-
conditions.push(`scope = '${args.scope}'`);
|
|
1752
|
-
}
|
|
1753
|
-
if (args.attributes) {
|
|
1754
|
-
const attributesStr = JSON.stringify(args.attributes);
|
|
1755
|
-
conditions.push(`attributes LIKE '%${attributesStr.replace(/"/g, '\\"')}%'`);
|
|
1756
|
-
}
|
|
1757
|
-
if (args.dateRange?.start) {
|
|
1758
|
-
conditions.push(`\`createdAt\` >= ${args.dateRange.start.getTime()}`);
|
|
1759
|
-
}
|
|
1760
|
-
if (args.dateRange?.end) {
|
|
1761
|
-
conditions.push(`\`createdAt\` <= ${args.dateRange.end.getTime()}`);
|
|
1762
|
-
}
|
|
1763
|
-
if (conditions.length > 0) {
|
|
1764
|
-
const whereClause = conditions.join(" AND ");
|
|
1765
|
-
query.where(whereClause);
|
|
1766
|
-
}
|
|
1767
|
-
let total = 0;
|
|
1768
|
-
if (conditions.length > 0) {
|
|
1769
|
-
const countQuery = table.query().where(conditions.join(" AND "));
|
|
1770
|
-
const allRecords = await countQuery.toArray();
|
|
1771
|
-
total = allRecords.length;
|
|
1772
|
-
} else {
|
|
1773
|
-
total = await table.countRows();
|
|
1774
|
-
}
|
|
1775
|
-
const page = args.page || 0;
|
|
1776
|
-
const perPage = args.perPage || 10;
|
|
1777
|
-
const offset = page * perPage;
|
|
1778
|
-
query.limit(perPage);
|
|
1779
|
-
if (offset > 0) {
|
|
1780
|
-
query.offset(offset);
|
|
1781
|
-
}
|
|
1782
|
-
const records = await query.toArray();
|
|
1783
|
-
const traces = records.map((record) => {
|
|
1784
|
-
const processed = {
|
|
1785
|
-
...record,
|
|
1786
|
-
attributes: record.attributes ? JSON.parse(record.attributes) : {},
|
|
1787
|
-
status: record.status ? JSON.parse(record.status) : {},
|
|
1788
|
-
events: record.events ? JSON.parse(record.events) : [],
|
|
1789
|
-
links: record.links ? JSON.parse(record.links) : [],
|
|
1790
|
-
other: record.other ? JSON.parse(record.other) : {},
|
|
1791
|
-
startTime: new Date(record.startTime),
|
|
1792
|
-
endTime: new Date(record.endTime),
|
|
1793
|
-
createdAt: new Date(record.createdAt)
|
|
1794
|
-
};
|
|
1795
|
-
if (processed.parentSpanId === null || processed.parentSpanId === void 0) {
|
|
1796
|
-
processed.parentSpanId = "";
|
|
1797
|
-
} else {
|
|
1798
|
-
processed.parentSpanId = String(processed.parentSpanId);
|
|
1799
|
-
}
|
|
1800
|
-
return processed;
|
|
1801
|
-
});
|
|
1802
|
-
return {
|
|
1803
|
-
traces,
|
|
1804
|
-
total,
|
|
1805
|
-
page,
|
|
1806
|
-
perPage,
|
|
1807
|
-
hasMore: total > (page + 1) * perPage
|
|
1808
|
-
};
|
|
1809
|
-
} catch (error$1) {
|
|
1810
|
-
throw new error.MastraError(
|
|
1811
|
-
{
|
|
1812
|
-
id: "LANCE_STORE_GET_TRACES_PAGINATED_FAILED",
|
|
1813
|
-
domain: error.ErrorDomain.STORAGE,
|
|
1814
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
1815
|
-
details: { name: args.name ?? "", scope: args.scope ?? "" }
|
|
1816
|
-
},
|
|
1817
|
-
error$1
|
|
1818
|
-
);
|
|
1819
|
-
}
|
|
1820
|
-
}
|
|
1821
|
-
async batchTraceInsert({ records }) {
|
|
1822
|
-
this.logger.debug("Batch inserting traces", { count: records.length });
|
|
1823
|
-
await this.operations.batchInsert({
|
|
1824
|
-
tableName: storage.TABLE_TRACES,
|
|
1825
|
-
records
|
|
1826
|
-
});
|
|
1827
|
-
}
|
|
1828
|
-
};
|
|
1829
1508
|
function parseWorkflowRun(row) {
|
|
1830
1509
|
let parsedSnapshot = row.snapshot;
|
|
1831
1510
|
if (typeof parsedSnapshot === "string") {
|
|
@@ -1855,7 +1534,7 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
|
|
|
1855
1534
|
// runId,
|
|
1856
1535
|
// stepId,
|
|
1857
1536
|
// result,
|
|
1858
|
-
//
|
|
1537
|
+
// requestContext,
|
|
1859
1538
|
}) {
|
|
1860
1539
|
throw new Error("Method not implemented.");
|
|
1861
1540
|
}
|
|
@@ -1883,11 +1562,13 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
|
|
|
1883
1562
|
} else {
|
|
1884
1563
|
createdAt = now;
|
|
1885
1564
|
}
|
|
1565
|
+
const { status, value, ...rest } = snapshot;
|
|
1886
1566
|
const record = {
|
|
1887
1567
|
workflow_name: workflowName,
|
|
1888
1568
|
run_id: runId,
|
|
1889
1569
|
resourceId,
|
|
1890
|
-
snapshot: JSON.stringify(
|
|
1570
|
+
snapshot: JSON.stringify({ status, value, ...rest }),
|
|
1571
|
+
// this is to ensure status is always just before value, for when querying the db by status
|
|
1891
1572
|
createdAt,
|
|
1892
1573
|
updatedAt: now
|
|
1893
1574
|
};
|
|
@@ -1949,7 +1630,7 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
|
|
|
1949
1630
|
);
|
|
1950
1631
|
}
|
|
1951
1632
|
}
|
|
1952
|
-
async
|
|
1633
|
+
async listWorkflowRuns(args) {
|
|
1953
1634
|
try {
|
|
1954
1635
|
const table = await this.client.openTable(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
1955
1636
|
let query = table.query();
|
|
@@ -1957,6 +1638,10 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
|
|
|
1957
1638
|
if (args?.workflowName) {
|
|
1958
1639
|
conditions.push(`workflow_name = '${args.workflowName.replace(/'/g, "''")}'`);
|
|
1959
1640
|
}
|
|
1641
|
+
if (args?.status) {
|
|
1642
|
+
const escapedStatus = args.status.replace(/\\/g, "\\\\").replace(/'/g, "''").replace(/%/g, "\\%").replace(/_/g, "\\_");
|
|
1643
|
+
conditions.push(`\`snapshot\` LIKE '%"status":"${escapedStatus}","value"%'`);
|
|
1644
|
+
}
|
|
1960
1645
|
if (args?.resourceId) {
|
|
1961
1646
|
conditions.push(`\`resourceId\` = '${args.resourceId}'`);
|
|
1962
1647
|
}
|
|
@@ -1973,11 +1658,22 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
|
|
|
1973
1658
|
} else {
|
|
1974
1659
|
total = await table.countRows();
|
|
1975
1660
|
}
|
|
1976
|
-
if (args?.
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1661
|
+
if (args?.perPage !== void 0 && args?.page !== void 0) {
|
|
1662
|
+
const normalizedPerPage = storage.normalizePerPage(args.perPage, Number.MAX_SAFE_INTEGER);
|
|
1663
|
+
if (args.page < 0 || !Number.isInteger(args.page)) {
|
|
1664
|
+
throw new error.MastraError(
|
|
1665
|
+
{
|
|
1666
|
+
id: "LANCE_STORE_INVALID_PAGINATION_PARAMS",
|
|
1667
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1668
|
+
category: error.ErrorCategory.USER,
|
|
1669
|
+
details: { page: args.page, perPage: args.perPage }
|
|
1670
|
+
},
|
|
1671
|
+
new Error(`Invalid pagination parameters: page=${args.page}, perPage=${args.perPage}`)
|
|
1672
|
+
);
|
|
1673
|
+
}
|
|
1674
|
+
const offset = args.page * normalizedPerPage;
|
|
1675
|
+
query.limit(normalizedPerPage);
|
|
1676
|
+
query.offset(offset);
|
|
1981
1677
|
}
|
|
1982
1678
|
const records = await query.toArray();
|
|
1983
1679
|
return {
|
|
@@ -1987,10 +1683,10 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
|
|
|
1987
1683
|
} catch (error$1) {
|
|
1988
1684
|
throw new error.MastraError(
|
|
1989
1685
|
{
|
|
1990
|
-
id: "
|
|
1686
|
+
id: "LANCE_STORE_LIST_WORKFLOW_RUNS_FAILED",
|
|
1991
1687
|
domain: error.ErrorDomain.STORAGE,
|
|
1992
1688
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
1993
|
-
details: {
|
|
1689
|
+
details: { resourceId: args?.resourceId ?? "", workflowName: args?.workflowName ?? "" }
|
|
1994
1690
|
},
|
|
1995
1691
|
error$1
|
|
1996
1692
|
);
|
|
@@ -2004,6 +1700,8 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
2004
1700
|
lanceClient;
|
|
2005
1701
|
/**
|
|
2006
1702
|
* Creates a new instance of LanceStorage
|
|
1703
|
+
* @param id The unique identifier for this storage instance
|
|
1704
|
+
* @param name The name for this storage instance
|
|
2007
1705
|
* @param uri The URI to connect to LanceDB
|
|
2008
1706
|
* @param options connection options
|
|
2009
1707
|
*
|
|
@@ -2011,31 +1709,29 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
2011
1709
|
*
|
|
2012
1710
|
* Connect to a local database
|
|
2013
1711
|
* ```ts
|
|
2014
|
-
* const store = await LanceStorage.create('/path/to/db');
|
|
1712
|
+
* const store = await LanceStorage.create('my-storage-id', 'MyStorage', '/path/to/db');
|
|
2015
1713
|
* ```
|
|
2016
1714
|
*
|
|
2017
1715
|
* Connect to a LanceDB cloud database
|
|
2018
1716
|
* ```ts
|
|
2019
|
-
* const store = await LanceStorage.create('db://host:port');
|
|
1717
|
+
* const store = await LanceStorage.create('my-storage-id', 'MyStorage', 'db://host:port');
|
|
2020
1718
|
* ```
|
|
2021
1719
|
*
|
|
2022
1720
|
* Connect to a cloud database
|
|
2023
1721
|
* ```ts
|
|
2024
|
-
* const store = await LanceStorage.create('s3://bucket/db', { storageOptions: { timeout: '60s' } });
|
|
1722
|
+
* const store = await LanceStorage.create('my-storage-id', 'MyStorage', 's3://bucket/db', { storageOptions: { timeout: '60s' } });
|
|
2025
1723
|
* ```
|
|
2026
1724
|
*/
|
|
2027
|
-
static async create(name, uri, options) {
|
|
2028
|
-
const instance = new _LanceStorage(name);
|
|
1725
|
+
static async create(id, name, uri, options) {
|
|
1726
|
+
const instance = new _LanceStorage(id, name);
|
|
2029
1727
|
try {
|
|
2030
1728
|
instance.lanceClient = await lancedb.connect(uri, options);
|
|
2031
1729
|
const operations = new StoreOperationsLance({ client: instance.lanceClient });
|
|
2032
1730
|
instance.stores = {
|
|
2033
1731
|
operations: new StoreOperationsLance({ client: instance.lanceClient }),
|
|
2034
1732
|
workflows: new StoreWorkflowsLance({ client: instance.lanceClient }),
|
|
2035
|
-
traces: new StoreTracesLance({ client: instance.lanceClient, operations }),
|
|
2036
1733
|
scores: new StoreScoresLance({ client: instance.lanceClient }),
|
|
2037
|
-
memory: new StoreMemoryLance({ client: instance.lanceClient, operations })
|
|
2038
|
-
legacyEvals: new StoreLegacyEvalsLance({ client: instance.lanceClient })
|
|
1734
|
+
memory: new StoreMemoryLance({ client: instance.lanceClient, operations })
|
|
2039
1735
|
};
|
|
2040
1736
|
return instance;
|
|
2041
1737
|
} catch (e) {
|
|
@@ -2055,15 +1751,13 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
2055
1751
|
* @internal
|
|
2056
1752
|
* Private constructor to enforce using the create factory method
|
|
2057
1753
|
*/
|
|
2058
|
-
constructor(name) {
|
|
2059
|
-
super({ name });
|
|
1754
|
+
constructor(id, name) {
|
|
1755
|
+
super({ id, name });
|
|
2060
1756
|
const operations = new StoreOperationsLance({ client: this.lanceClient });
|
|
2061
1757
|
this.stores = {
|
|
2062
1758
|
operations: new StoreOperationsLance({ client: this.lanceClient }),
|
|
2063
1759
|
workflows: new StoreWorkflowsLance({ client: this.lanceClient }),
|
|
2064
|
-
traces: new StoreTracesLance({ client: this.lanceClient, operations }),
|
|
2065
1760
|
scores: new StoreScoresLance({ client: this.lanceClient }),
|
|
2066
|
-
legacyEvals: new StoreLegacyEvalsLance({ client: this.lanceClient }),
|
|
2067
1761
|
memory: new StoreMemoryLance({ client: this.lanceClient, operations })
|
|
2068
1762
|
};
|
|
2069
1763
|
}
|
|
@@ -2098,9 +1792,6 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
2098
1792
|
async getThreadById({ threadId }) {
|
|
2099
1793
|
return this.stores.memory.getThreadById({ threadId });
|
|
2100
1794
|
}
|
|
2101
|
-
async getThreadsByResourceId({ resourceId }) {
|
|
2102
|
-
return this.stores.memory.getThreadsByResourceId({ resourceId });
|
|
2103
|
-
}
|
|
2104
1795
|
/**
|
|
2105
1796
|
* Saves a thread to the database. This function doesn't overwrite existing threads.
|
|
2106
1797
|
* @param thread - The thread to save
|
|
@@ -2126,7 +1817,7 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
2126
1817
|
hasColumn: true,
|
|
2127
1818
|
createTable: true,
|
|
2128
1819
|
deleteMessages: false,
|
|
2129
|
-
|
|
1820
|
+
listScoresBySpan: true
|
|
2130
1821
|
};
|
|
2131
1822
|
}
|
|
2132
1823
|
async getResourceById({ resourceId }) {
|
|
@@ -2190,50 +1881,17 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
2190
1881
|
});
|
|
2191
1882
|
return Array.from(allIndices).sort((a, b) => a - b).map((index) => records[index]);
|
|
2192
1883
|
}
|
|
2193
|
-
async
|
|
2194
|
-
|
|
2195
|
-
resourceId,
|
|
2196
|
-
selectBy,
|
|
2197
|
-
format,
|
|
2198
|
-
threadConfig
|
|
2199
|
-
}) {
|
|
2200
|
-
return this.stores.memory.getMessages({ threadId, resourceId, selectBy, format, threadConfig });
|
|
2201
|
-
}
|
|
2202
|
-
async getMessagesById({
|
|
2203
|
-
messageIds,
|
|
2204
|
-
format
|
|
2205
|
-
}) {
|
|
2206
|
-
return this.stores.memory.getMessagesById({ messageIds, format });
|
|
1884
|
+
async listMessagesById({ messageIds }) {
|
|
1885
|
+
return this.stores.memory.listMessagesById({ messageIds });
|
|
2207
1886
|
}
|
|
2208
1887
|
async saveMessages(args) {
|
|
2209
1888
|
return this.stores.memory.saveMessages(args);
|
|
2210
1889
|
}
|
|
2211
|
-
async getThreadsByResourceIdPaginated(args) {
|
|
2212
|
-
return this.stores.memory.getThreadsByResourceIdPaginated(args);
|
|
2213
|
-
}
|
|
2214
|
-
async getMessagesPaginated(args) {
|
|
2215
|
-
return this.stores.memory.getMessagesPaginated(args);
|
|
2216
|
-
}
|
|
2217
1890
|
async updateMessages(_args) {
|
|
2218
1891
|
return this.stores.memory.updateMessages(_args);
|
|
2219
1892
|
}
|
|
2220
|
-
async
|
|
2221
|
-
return this.stores.
|
|
2222
|
-
}
|
|
2223
|
-
async getTraces(args) {
|
|
2224
|
-
return this.stores.traces.getTraces(args);
|
|
2225
|
-
}
|
|
2226
|
-
async getTracesPaginated(args) {
|
|
2227
|
-
return this.stores.traces.getTracesPaginated(args);
|
|
2228
|
-
}
|
|
2229
|
-
async getEvalsByAgentName(agentName, type) {
|
|
2230
|
-
return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
|
|
2231
|
-
}
|
|
2232
|
-
async getEvals(options) {
|
|
2233
|
-
return this.stores.legacyEvals.getEvals(options);
|
|
2234
|
-
}
|
|
2235
|
-
async getWorkflowRuns(args) {
|
|
2236
|
-
return this.stores.workflows.getWorkflowRuns(args);
|
|
1893
|
+
async listWorkflowRuns(args) {
|
|
1894
|
+
return this.stores.workflows.listWorkflowRuns(args);
|
|
2237
1895
|
}
|
|
2238
1896
|
async getWorkflowRunById(args) {
|
|
2239
1897
|
return this.stores.workflows.getWorkflowRunById(args);
|
|
@@ -2243,9 +1901,9 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
2243
1901
|
runId,
|
|
2244
1902
|
stepId,
|
|
2245
1903
|
result,
|
|
2246
|
-
|
|
1904
|
+
requestContext
|
|
2247
1905
|
}) {
|
|
2248
|
-
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result,
|
|
1906
|
+
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, requestContext });
|
|
2249
1907
|
}
|
|
2250
1908
|
async updateWorkflowState({
|
|
2251
1909
|
workflowName,
|
|
@@ -2271,37 +1929,37 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
2271
1929
|
async getScoreById({ id: _id }) {
|
|
2272
1930
|
return this.stores.scores.getScoreById({ id: _id });
|
|
2273
1931
|
}
|
|
2274
|
-
async
|
|
1932
|
+
async listScoresByScorerId({
|
|
2275
1933
|
scorerId,
|
|
2276
1934
|
source,
|
|
2277
1935
|
entityId,
|
|
2278
1936
|
entityType,
|
|
2279
1937
|
pagination
|
|
2280
1938
|
}) {
|
|
2281
|
-
return this.stores.scores.
|
|
1939
|
+
return this.stores.scores.listScoresByScorerId({ scorerId, source, pagination, entityId, entityType });
|
|
2282
1940
|
}
|
|
2283
1941
|
async saveScore(_score) {
|
|
2284
1942
|
return this.stores.scores.saveScore(_score);
|
|
2285
1943
|
}
|
|
2286
|
-
async
|
|
1944
|
+
async listScoresByRunId({
|
|
2287
1945
|
runId,
|
|
2288
1946
|
pagination
|
|
2289
1947
|
}) {
|
|
2290
|
-
return this.stores.scores.
|
|
1948
|
+
return this.stores.scores.listScoresByRunId({ runId, pagination });
|
|
2291
1949
|
}
|
|
2292
|
-
async
|
|
1950
|
+
async listScoresByEntityId({
|
|
2293
1951
|
entityId,
|
|
2294
1952
|
entityType,
|
|
2295
1953
|
pagination
|
|
2296
1954
|
}) {
|
|
2297
|
-
return this.stores.scores.
|
|
1955
|
+
return this.stores.scores.listScoresByEntityId({ entityId, entityType, pagination });
|
|
2298
1956
|
}
|
|
2299
|
-
async
|
|
1957
|
+
async listScoresBySpan({
|
|
2300
1958
|
traceId,
|
|
2301
1959
|
spanId,
|
|
2302
1960
|
pagination
|
|
2303
1961
|
}) {
|
|
2304
|
-
return this.stores.scores.
|
|
1962
|
+
return this.stores.scores.listScoresBySpan({ traceId, spanId, pagination });
|
|
2305
1963
|
}
|
|
2306
1964
|
};
|
|
2307
1965
|
var LanceFilterTranslator = class extends filter.BaseFilterTranslator {
|
|
@@ -2650,7 +2308,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2650
2308
|
* ```
|
|
2651
2309
|
*/
|
|
2652
2310
|
static async create(uri, options) {
|
|
2653
|
-
const instance = new _LanceVectorStore();
|
|
2311
|
+
const instance = new _LanceVectorStore(options?.id || crypto.randomUUID());
|
|
2654
2312
|
try {
|
|
2655
2313
|
instance.lanceClient = await lancedb.connect(uri, options);
|
|
2656
2314
|
return instance;
|
|
@@ -2670,8 +2328,8 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2670
2328
|
* @internal
|
|
2671
2329
|
* Private constructor to enforce using the create factory method
|
|
2672
2330
|
*/
|
|
2673
|
-
constructor() {
|
|
2674
|
-
super();
|
|
2331
|
+
constructor(id) {
|
|
2332
|
+
super({ id });
|
|
2675
2333
|
}
|
|
2676
2334
|
close() {
|
|
2677
2335
|
if (this.lanceClient) {
|
|
@@ -3194,7 +2852,44 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
3194
2852
|
);
|
|
3195
2853
|
}
|
|
3196
2854
|
}
|
|
3197
|
-
async updateVector(
|
|
2855
|
+
async updateVector(params) {
|
|
2856
|
+
const { indexName, update } = params;
|
|
2857
|
+
if ("id" in params && "filter" in params && params.id && params.filter) {
|
|
2858
|
+
throw new error.MastraError({
|
|
2859
|
+
id: "STORAGE_LANCE_VECTOR_UPDATE_VECTOR_INVALID_ARGS",
|
|
2860
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2861
|
+
category: error.ErrorCategory.USER,
|
|
2862
|
+
text: "id and filter are mutually exclusive",
|
|
2863
|
+
details: { indexName }
|
|
2864
|
+
});
|
|
2865
|
+
}
|
|
2866
|
+
if (!("id" in params || "filter" in params) || !params.id && !params.filter) {
|
|
2867
|
+
throw new error.MastraError({
|
|
2868
|
+
id: "STORAGE_LANCE_VECTOR_UPDATE_VECTOR_INVALID_ARGS",
|
|
2869
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2870
|
+
category: error.ErrorCategory.USER,
|
|
2871
|
+
text: "Either id or filter must be provided",
|
|
2872
|
+
details: { indexName }
|
|
2873
|
+
});
|
|
2874
|
+
}
|
|
2875
|
+
if ("filter" in params && params.filter && Object.keys(params.filter).length === 0) {
|
|
2876
|
+
throw new error.MastraError({
|
|
2877
|
+
id: "STORAGE_LANCE_VECTOR_UPDATE_VECTOR_INVALID_ARGS",
|
|
2878
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2879
|
+
category: error.ErrorCategory.USER,
|
|
2880
|
+
text: "Cannot update with empty filter",
|
|
2881
|
+
details: { indexName }
|
|
2882
|
+
});
|
|
2883
|
+
}
|
|
2884
|
+
if (!update.vector && !update.metadata) {
|
|
2885
|
+
throw new error.MastraError({
|
|
2886
|
+
id: "STORAGE_LANCE_VECTOR_UPDATE_VECTOR_INVALID_ARGS",
|
|
2887
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2888
|
+
category: error.ErrorCategory.USER,
|
|
2889
|
+
text: "No updates provided",
|
|
2890
|
+
details: { indexName }
|
|
2891
|
+
});
|
|
2892
|
+
}
|
|
3198
2893
|
try {
|
|
3199
2894
|
if (!this.lanceClient) {
|
|
3200
2895
|
throw new Error("LanceDB client not initialized. Use LanceVectorStore.create() to create an instance");
|
|
@@ -3202,21 +2897,6 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
3202
2897
|
if (!indexName) {
|
|
3203
2898
|
throw new Error("indexName is required");
|
|
3204
2899
|
}
|
|
3205
|
-
if (!id) {
|
|
3206
|
-
throw new Error("id is required");
|
|
3207
|
-
}
|
|
3208
|
-
} catch (err) {
|
|
3209
|
-
throw new error.MastraError(
|
|
3210
|
-
{
|
|
3211
|
-
id: "STORAGE_LANCE_VECTOR_UPDATE_VECTOR_FAILED_INVALID_ARGS",
|
|
3212
|
-
domain: error.ErrorDomain.STORAGE,
|
|
3213
|
-
category: error.ErrorCategory.USER,
|
|
3214
|
-
details: { indexName, id }
|
|
3215
|
-
},
|
|
3216
|
-
err
|
|
3217
|
-
);
|
|
3218
|
-
}
|
|
3219
|
-
try {
|
|
3220
2900
|
const tables = await this.lanceClient.tableNames();
|
|
3221
2901
|
for (const tableName of tables) {
|
|
3222
2902
|
this.logger.debug("Checking table:" + tableName);
|
|
@@ -3226,39 +2906,66 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
3226
2906
|
const hasColumn = schema.fields.some((field) => field.name === indexName);
|
|
3227
2907
|
if (hasColumn) {
|
|
3228
2908
|
this.logger.debug(`Found column ${indexName} in table ${tableName}`);
|
|
3229
|
-
|
|
3230
|
-
if (
|
|
3231
|
-
|
|
2909
|
+
let whereClause;
|
|
2910
|
+
if ("id" in params && params.id) {
|
|
2911
|
+
whereClause = `id = '${params.id}'`;
|
|
2912
|
+
} else if ("filter" in params && params.filter) {
|
|
2913
|
+
const translator = new LanceFilterTranslator();
|
|
2914
|
+
const processFilterKeys = (filter) => {
|
|
2915
|
+
const processedFilter = {};
|
|
2916
|
+
Object.entries(filter).forEach(([key, value]) => {
|
|
2917
|
+
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
2918
|
+
Object.entries(value).forEach(([nestedKey, nestedValue]) => {
|
|
2919
|
+
processedFilter[`metadata_${key}_${nestedKey}`] = nestedValue;
|
|
2920
|
+
});
|
|
2921
|
+
} else {
|
|
2922
|
+
processedFilter[`metadata_${key}`] = value;
|
|
2923
|
+
}
|
|
2924
|
+
});
|
|
2925
|
+
return processedFilter;
|
|
2926
|
+
};
|
|
2927
|
+
const prefixedFilter = processFilterKeys(params.filter);
|
|
2928
|
+
whereClause = translator.translate(prefixedFilter) || "";
|
|
2929
|
+
if (!whereClause) {
|
|
2930
|
+
throw new Error("Failed to translate filter to SQL");
|
|
2931
|
+
}
|
|
2932
|
+
} else {
|
|
2933
|
+
throw new Error("Either id or filter must be provided");
|
|
3232
2934
|
}
|
|
3233
|
-
const
|
|
3234
|
-
|
|
3235
|
-
|
|
3236
|
-
|
|
3237
|
-
|
|
3238
|
-
|
|
3239
|
-
|
|
3240
|
-
|
|
3241
|
-
|
|
3242
|
-
|
|
3243
|
-
|
|
2935
|
+
const existingRecords = await table.query().where(whereClause).select(schema.fields.map((field) => field.name)).toArray();
|
|
2936
|
+
if (existingRecords.length === 0) {
|
|
2937
|
+
this.logger.info(`No records found matching criteria in table ${tableName}`);
|
|
2938
|
+
return;
|
|
2939
|
+
}
|
|
2940
|
+
const updatedRecords = existingRecords.map((record) => {
|
|
2941
|
+
const rowData = {};
|
|
2942
|
+
Object.entries(record).forEach(([key, value]) => {
|
|
2943
|
+
if (key !== "_distance") {
|
|
2944
|
+
if (key === indexName) {
|
|
2945
|
+
if (update.vector) {
|
|
2946
|
+
rowData[key] = update.vector;
|
|
3244
2947
|
} else {
|
|
3245
|
-
|
|
2948
|
+
if (Array.isArray(value)) {
|
|
2949
|
+
rowData[key] = [...value];
|
|
2950
|
+
} else if (typeof value === "object" && value !== null) {
|
|
2951
|
+
rowData[key] = Array.from(value);
|
|
2952
|
+
} else {
|
|
2953
|
+
rowData[key] = value;
|
|
2954
|
+
}
|
|
3246
2955
|
}
|
|
2956
|
+
} else {
|
|
2957
|
+
rowData[key] = value;
|
|
3247
2958
|
}
|
|
3248
|
-
} else {
|
|
3249
|
-
rowData[key] = value;
|
|
3250
2959
|
}
|
|
2960
|
+
});
|
|
2961
|
+
if (update.metadata) {
|
|
2962
|
+
Object.entries(update.metadata).forEach(([key, value]) => {
|
|
2963
|
+
rowData[`metadata_${key}`] = value;
|
|
2964
|
+
});
|
|
3251
2965
|
}
|
|
2966
|
+
return rowData;
|
|
3252
2967
|
});
|
|
3253
|
-
|
|
3254
|
-
rowData[indexName] = update.vector;
|
|
3255
|
-
}
|
|
3256
|
-
if (update.metadata) {
|
|
3257
|
-
Object.entries(update.metadata).forEach(([key, value]) => {
|
|
3258
|
-
rowData[`metadata_${key}`] = value;
|
|
3259
|
-
});
|
|
3260
|
-
}
|
|
3261
|
-
await table.add([rowData], { mode: "overwrite" });
|
|
2968
|
+
await table.add(updatedRecords, { mode: "overwrite" });
|
|
3262
2969
|
return;
|
|
3263
2970
|
}
|
|
3264
2971
|
} catch (err) {
|
|
@@ -3268,12 +2975,19 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
3268
2975
|
}
|
|
3269
2976
|
throw new Error(`No table found with column/index '${indexName}'`);
|
|
3270
2977
|
} catch (error$1) {
|
|
2978
|
+
if (error$1 instanceof error.MastraError) throw error$1;
|
|
3271
2979
|
throw new error.MastraError(
|
|
3272
2980
|
{
|
|
3273
2981
|
id: "STORAGE_LANCE_VECTOR_UPDATE_VECTOR_FAILED",
|
|
3274
2982
|
domain: error.ErrorDomain.STORAGE,
|
|
3275
2983
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
3276
|
-
details: {
|
|
2984
|
+
details: {
|
|
2985
|
+
indexName,
|
|
2986
|
+
..."id" in params && params.id && { id: params.id },
|
|
2987
|
+
..."filter" in params && params.filter && { filter: JSON.stringify(params.filter) },
|
|
2988
|
+
hasVector: !!update.vector,
|
|
2989
|
+
hasMetadata: !!update.metadata
|
|
2990
|
+
}
|
|
3277
2991
|
},
|
|
3278
2992
|
error$1
|
|
3279
2993
|
);
|
|
@@ -3296,7 +3010,10 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
3296
3010
|
id: "STORAGE_LANCE_VECTOR_DELETE_VECTOR_FAILED_INVALID_ARGS",
|
|
3297
3011
|
domain: error.ErrorDomain.STORAGE,
|
|
3298
3012
|
category: error.ErrorCategory.USER,
|
|
3299
|
-
details: {
|
|
3013
|
+
details: {
|
|
3014
|
+
indexName,
|
|
3015
|
+
...id && { id }
|
|
3016
|
+
}
|
|
3300
3017
|
},
|
|
3301
3018
|
err
|
|
3302
3019
|
);
|
|
@@ -3326,7 +3043,10 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
3326
3043
|
id: "STORAGE_LANCE_VECTOR_DELETE_VECTOR_FAILED",
|
|
3327
3044
|
domain: error.ErrorDomain.STORAGE,
|
|
3328
3045
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
3329
|
-
details: {
|
|
3046
|
+
details: {
|
|
3047
|
+
indexName,
|
|
3048
|
+
...id && { id }
|
|
3049
|
+
}
|
|
3330
3050
|
},
|
|
3331
3051
|
error$1
|
|
3332
3052
|
);
|
|
@@ -3357,6 +3077,109 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
3357
3077
|
});
|
|
3358
3078
|
return result;
|
|
3359
3079
|
}
|
|
3080
|
+
async deleteVectors({ indexName, filter, ids }) {
|
|
3081
|
+
if (ids && filter) {
|
|
3082
|
+
throw new error.MastraError({
|
|
3083
|
+
id: "STORAGE_LANCE_VECTOR_DELETE_VECTORS_INVALID_ARGS",
|
|
3084
|
+
domain: error.ErrorDomain.STORAGE,
|
|
3085
|
+
category: error.ErrorCategory.USER,
|
|
3086
|
+
text: "ids and filter are mutually exclusive",
|
|
3087
|
+
details: { indexName }
|
|
3088
|
+
});
|
|
3089
|
+
}
|
|
3090
|
+
if (!ids && !filter) {
|
|
3091
|
+
throw new error.MastraError({
|
|
3092
|
+
id: "STORAGE_LANCE_VECTOR_DELETE_VECTORS_INVALID_ARGS",
|
|
3093
|
+
domain: error.ErrorDomain.STORAGE,
|
|
3094
|
+
category: error.ErrorCategory.USER,
|
|
3095
|
+
text: "Either filter or ids must be provided",
|
|
3096
|
+
details: { indexName }
|
|
3097
|
+
});
|
|
3098
|
+
}
|
|
3099
|
+
if (ids && ids.length === 0) {
|
|
3100
|
+
throw new error.MastraError({
|
|
3101
|
+
id: "STORAGE_LANCE_VECTOR_DELETE_VECTORS_INVALID_ARGS",
|
|
3102
|
+
domain: error.ErrorDomain.STORAGE,
|
|
3103
|
+
category: error.ErrorCategory.USER,
|
|
3104
|
+
text: "Cannot delete with empty ids array",
|
|
3105
|
+
details: { indexName }
|
|
3106
|
+
});
|
|
3107
|
+
}
|
|
3108
|
+
if (filter && Object.keys(filter).length === 0) {
|
|
3109
|
+
throw new error.MastraError({
|
|
3110
|
+
id: "STORAGE_LANCE_VECTOR_DELETE_VECTORS_INVALID_ARGS",
|
|
3111
|
+
domain: error.ErrorDomain.STORAGE,
|
|
3112
|
+
category: error.ErrorCategory.USER,
|
|
3113
|
+
text: "Cannot delete with empty filter",
|
|
3114
|
+
details: { indexName }
|
|
3115
|
+
});
|
|
3116
|
+
}
|
|
3117
|
+
try {
|
|
3118
|
+
if (!this.lanceClient) {
|
|
3119
|
+
throw new Error("LanceDB client not initialized. Use LanceVectorStore.create() to create an instance");
|
|
3120
|
+
}
|
|
3121
|
+
if (!indexName) {
|
|
3122
|
+
throw new Error("indexName is required");
|
|
3123
|
+
}
|
|
3124
|
+
const tables = await this.lanceClient.tableNames();
|
|
3125
|
+
for (const tableName of tables) {
|
|
3126
|
+
this.logger.debug("Checking table:" + tableName);
|
|
3127
|
+
const table = await this.lanceClient.openTable(tableName);
|
|
3128
|
+
try {
|
|
3129
|
+
const schema = await table.schema();
|
|
3130
|
+
const hasColumn = schema.fields.some((field) => field.name === indexName);
|
|
3131
|
+
if (hasColumn) {
|
|
3132
|
+
this.logger.debug(`Found column ${indexName} in table ${tableName}`);
|
|
3133
|
+
if (ids) {
|
|
3134
|
+
const idsConditions = ids.map((id) => `id = '${id}'`).join(" OR ");
|
|
3135
|
+
await table.delete(idsConditions);
|
|
3136
|
+
} else if (filter) {
|
|
3137
|
+
const translator = new LanceFilterTranslator();
|
|
3138
|
+
const processFilterKeys = (filter2) => {
|
|
3139
|
+
const processedFilter = {};
|
|
3140
|
+
Object.entries(filter2).forEach(([key, value]) => {
|
|
3141
|
+
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
3142
|
+
Object.entries(value).forEach(([nestedKey, nestedValue]) => {
|
|
3143
|
+
processedFilter[`metadata_${key}_${nestedKey}`] = nestedValue;
|
|
3144
|
+
});
|
|
3145
|
+
} else {
|
|
3146
|
+
processedFilter[`metadata_${key}`] = value;
|
|
3147
|
+
}
|
|
3148
|
+
});
|
|
3149
|
+
return processedFilter;
|
|
3150
|
+
};
|
|
3151
|
+
const prefixedFilter = processFilterKeys(filter);
|
|
3152
|
+
const whereClause = translator.translate(prefixedFilter);
|
|
3153
|
+
if (!whereClause) {
|
|
3154
|
+
throw new Error("Failed to translate filter to SQL");
|
|
3155
|
+
}
|
|
3156
|
+
await table.delete(whereClause);
|
|
3157
|
+
}
|
|
3158
|
+
return;
|
|
3159
|
+
}
|
|
3160
|
+
} catch (err) {
|
|
3161
|
+
this.logger.error(`Error checking schema for table ${tableName}:` + err);
|
|
3162
|
+
continue;
|
|
3163
|
+
}
|
|
3164
|
+
}
|
|
3165
|
+
throw new Error(`No table found with column/index '${indexName}'`);
|
|
3166
|
+
} catch (error$1) {
|
|
3167
|
+
if (error$1 instanceof error.MastraError) throw error$1;
|
|
3168
|
+
throw new error.MastraError(
|
|
3169
|
+
{
|
|
3170
|
+
id: "STORAGE_LANCE_VECTOR_DELETE_VECTORS_FAILED",
|
|
3171
|
+
domain: error.ErrorDomain.STORAGE,
|
|
3172
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
3173
|
+
details: {
|
|
3174
|
+
indexName,
|
|
3175
|
+
...filter && { filter: JSON.stringify(filter) },
|
|
3176
|
+
...ids && { idsCount: ids.length }
|
|
3177
|
+
}
|
|
3178
|
+
},
|
|
3179
|
+
error$1
|
|
3180
|
+
);
|
|
3181
|
+
}
|
|
3182
|
+
}
|
|
3360
3183
|
};
|
|
3361
3184
|
|
|
3362
3185
|
exports.LanceStorage = LanceStorage;
|