@mastra/lance 0.0.0-scorers-api-v2-20250801171841 → 0.0.0-scorers-logs-20251208093427
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 +982 -3
- package/README.md +64 -7
- package/dist/index.cjs +725 -720
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +726 -721
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +18 -33
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/operations/index.d.ts.map +1 -1
- package/dist/storage/domains/scores/index.d.ts +24 -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 +23 -11
- package/dist/storage/domains/workflows/index.d.ts.map +1 -1
- package/dist/storage/index.d.ts +87 -81
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/vector/filter.d.ts +5 -5
- package/dist/vector/index.d.ts +8 -5
- package/dist/vector/index.d.ts.map +1 -1
- package/package.json +29 -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/eslint.config.js +0 -6
- package/src/index.ts +0 -2
- package/src/storage/domains/legacy-evals/index.ts +0 -156
- package/src/storage/domains/memory/index.ts +0 -947
- package/src/storage/domains/operations/index.ts +0 -489
- package/src/storage/domains/scores/index.ts +0 -221
- package/src/storage/domains/traces/index.ts +0 -212
- package/src/storage/domains/utils.ts +0 -158
- package/src/storage/domains/workflows/index.ts +0 -207
- package/src/storage/index.test.ts +0 -10
- package/src/storage/index.ts +0 -442
- package/src/vector/filter.test.ts +0 -295
- package/src/vector/filter.ts +0 -443
- package/src/vector/index.test.ts +0 -1493
- package/src/vector/index.ts +0 -941
- package/src/vector/types.ts +0 -16
- package/tsconfig.build.json +0 -9
- package/tsconfig.json +0 -5
- package/tsup.config.ts +0 -22
- package/vitest.config.ts +0 -11
package/dist/index.cjs
CHANGED
|
@@ -5,130 +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 evals = require('@mastra/core/evals');
|
|
8
9
|
var vector = require('@mastra/core/vector');
|
|
9
10
|
var filter = require('@mastra/core/vector/filter');
|
|
10
11
|
|
|
11
12
|
// src/storage/index.ts
|
|
12
|
-
var StoreLegacyEvalsLance = class extends storage.LegacyEvalsStorage {
|
|
13
|
-
client;
|
|
14
|
-
constructor({ client }) {
|
|
15
|
-
super();
|
|
16
|
-
this.client = client;
|
|
17
|
-
}
|
|
18
|
-
async getEvalsByAgentName(agentName, type) {
|
|
19
|
-
try {
|
|
20
|
-
const table = await this.client.openTable(storage.TABLE_EVALS);
|
|
21
|
-
const query = table.query().where(`agent_name = '${agentName}'`);
|
|
22
|
-
const records = await query.toArray();
|
|
23
|
-
let filteredRecords = records;
|
|
24
|
-
if (type === "live") {
|
|
25
|
-
filteredRecords = records.filter((record) => record.test_info === null);
|
|
26
|
-
} else if (type === "test") {
|
|
27
|
-
filteredRecords = records.filter((record) => record.test_info !== null);
|
|
28
|
-
}
|
|
29
|
-
return filteredRecords.map((record) => {
|
|
30
|
-
return {
|
|
31
|
-
id: record.id,
|
|
32
|
-
input: record.input,
|
|
33
|
-
output: record.output,
|
|
34
|
-
agentName: record.agent_name,
|
|
35
|
-
metricName: record.metric_name,
|
|
36
|
-
result: JSON.parse(record.result),
|
|
37
|
-
instructions: record.instructions,
|
|
38
|
-
testInfo: record.test_info ? JSON.parse(record.test_info) : null,
|
|
39
|
-
globalRunId: record.global_run_id,
|
|
40
|
-
runId: record.run_id,
|
|
41
|
-
createdAt: new Date(record.created_at).toString()
|
|
42
|
-
};
|
|
43
|
-
});
|
|
44
|
-
} catch (error$1) {
|
|
45
|
-
throw new error.MastraError(
|
|
46
|
-
{
|
|
47
|
-
id: "LANCE_STORE_GET_EVALS_BY_AGENT_NAME_FAILED",
|
|
48
|
-
domain: error.ErrorDomain.STORAGE,
|
|
49
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
50
|
-
details: { agentName }
|
|
51
|
-
},
|
|
52
|
-
error$1
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
async getEvals(options) {
|
|
57
|
-
try {
|
|
58
|
-
const table = await this.client.openTable(storage.TABLE_EVALS);
|
|
59
|
-
const conditions = [];
|
|
60
|
-
if (options.agentName) {
|
|
61
|
-
conditions.push(`agent_name = '${options.agentName}'`);
|
|
62
|
-
}
|
|
63
|
-
if (options.type === "live") {
|
|
64
|
-
conditions.push("length(test_info) = 0");
|
|
65
|
-
} else if (options.type === "test") {
|
|
66
|
-
conditions.push("length(test_info) > 0");
|
|
67
|
-
}
|
|
68
|
-
const startDate = options.dateRange?.start || options.fromDate;
|
|
69
|
-
const endDate = options.dateRange?.end || options.toDate;
|
|
70
|
-
if (startDate) {
|
|
71
|
-
conditions.push(`\`created_at\` >= ${startDate.getTime()}`);
|
|
72
|
-
}
|
|
73
|
-
if (endDate) {
|
|
74
|
-
conditions.push(`\`created_at\` <= ${endDate.getTime()}`);
|
|
75
|
-
}
|
|
76
|
-
let total = 0;
|
|
77
|
-
if (conditions.length > 0) {
|
|
78
|
-
total = await table.countRows(conditions.join(" AND "));
|
|
79
|
-
} else {
|
|
80
|
-
total = await table.countRows();
|
|
81
|
-
}
|
|
82
|
-
const query = table.query();
|
|
83
|
-
if (conditions.length > 0) {
|
|
84
|
-
const whereClause = conditions.join(" AND ");
|
|
85
|
-
query.where(whereClause);
|
|
86
|
-
}
|
|
87
|
-
const records = await query.toArray();
|
|
88
|
-
const evals = records.sort((a, b) => b.created_at - a.created_at).map((record) => {
|
|
89
|
-
return {
|
|
90
|
-
id: record.id,
|
|
91
|
-
input: record.input,
|
|
92
|
-
output: record.output,
|
|
93
|
-
agentName: record.agent_name,
|
|
94
|
-
metricName: record.metric_name,
|
|
95
|
-
result: JSON.parse(record.result),
|
|
96
|
-
instructions: record.instructions,
|
|
97
|
-
testInfo: record.test_info ? JSON.parse(record.test_info) : null,
|
|
98
|
-
globalRunId: record.global_run_id,
|
|
99
|
-
runId: record.run_id,
|
|
100
|
-
createdAt: new Date(record.created_at).toISOString()
|
|
101
|
-
};
|
|
102
|
-
});
|
|
103
|
-
const page = options.page || 0;
|
|
104
|
-
const perPage = options.perPage || 10;
|
|
105
|
-
const pagedEvals = evals.slice(page * perPage, (page + 1) * perPage);
|
|
106
|
-
return {
|
|
107
|
-
evals: pagedEvals,
|
|
108
|
-
total,
|
|
109
|
-
page,
|
|
110
|
-
perPage,
|
|
111
|
-
hasMore: total > (page + 1) * perPage
|
|
112
|
-
};
|
|
113
|
-
} catch (error$1) {
|
|
114
|
-
throw new error.MastraError(
|
|
115
|
-
{
|
|
116
|
-
id: "LANCE_STORE_GET_EVALS_FAILED",
|
|
117
|
-
domain: error.ErrorDomain.STORAGE,
|
|
118
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
119
|
-
details: { agentName: options.agentName ?? "" }
|
|
120
|
-
},
|
|
121
|
-
error$1
|
|
122
|
-
);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
};
|
|
126
13
|
function getPrimaryKeys(tableName) {
|
|
127
14
|
let primaryId = ["id"];
|
|
128
15
|
if (tableName === storage.TABLE_WORKFLOW_SNAPSHOT) {
|
|
129
16
|
primaryId = ["workflow_name", "run_id"];
|
|
130
|
-
} else if (tableName === storage.TABLE_EVALS) {
|
|
131
|
-
primaryId = ["agent_name", "metric_name", "run_id"];
|
|
132
17
|
}
|
|
133
18
|
return primaryId;
|
|
134
19
|
}
|
|
@@ -189,7 +74,6 @@ function processResultWithTypeConversion(rawResult, tableSchema) {
|
|
|
189
74
|
} else if (fieldTypeStr.includes("float64") && ["createdAt", "updatedAt"].includes(key)) {
|
|
190
75
|
processedResult[key] = new Date(processedResult[key]);
|
|
191
76
|
}
|
|
192
|
-
console.log(key, "processedResult", processedResult);
|
|
193
77
|
}
|
|
194
78
|
return processedResult;
|
|
195
79
|
}
|
|
@@ -207,7 +91,7 @@ async function getTableSchema({
|
|
|
207
91
|
} catch (validationError) {
|
|
208
92
|
throw new error.MastraError(
|
|
209
93
|
{
|
|
210
|
-
id: "
|
|
94
|
+
id: storage.createStorageErrorId("LANCE", "GET_TABLE_SCHEMA", "INVALID_ARGS"),
|
|
211
95
|
domain: error.ErrorDomain.STORAGE,
|
|
212
96
|
category: error.ErrorCategory.USER,
|
|
213
97
|
text: validationError.message,
|
|
@@ -230,7 +114,7 @@ async function getTableSchema({
|
|
|
230
114
|
} catch (error$1) {
|
|
231
115
|
throw new error.MastraError(
|
|
232
116
|
{
|
|
233
|
-
id: "
|
|
117
|
+
id: storage.createStorageErrorId("LANCE", "GET_TABLE_SCHEMA", "FAILED"),
|
|
234
118
|
domain: error.ErrorDomain.STORAGE,
|
|
235
119
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
236
120
|
details: { tableName }
|
|
@@ -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 } });
|
|
@@ -263,27 +151,7 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
263
151
|
} catch (error$1) {
|
|
264
152
|
throw new error.MastraError(
|
|
265
153
|
{
|
|
266
|
-
id: "
|
|
267
|
-
domain: error.ErrorDomain.STORAGE,
|
|
268
|
-
category: error.ErrorCategory.THIRD_PARTY
|
|
269
|
-
},
|
|
270
|
-
error$1
|
|
271
|
-
);
|
|
272
|
-
}
|
|
273
|
-
}
|
|
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",
|
|
154
|
+
id: storage.createStorageErrorId("LANCE", "GET_THREAD_BY_ID", "FAILED"),
|
|
287
155
|
domain: error.ErrorDomain.STORAGE,
|
|
288
156
|
category: error.ErrorCategory.THIRD_PARTY
|
|
289
157
|
},
|
|
@@ -305,7 +173,7 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
305
173
|
} catch (error$1) {
|
|
306
174
|
throw new error.MastraError(
|
|
307
175
|
{
|
|
308
|
-
id: "
|
|
176
|
+
id: storage.createStorageErrorId("LANCE", "SAVE_THREAD", "FAILED"),
|
|
309
177
|
domain: error.ErrorDomain.STORAGE,
|
|
310
178
|
category: error.ErrorCategory.THIRD_PARTY
|
|
311
179
|
},
|
|
@@ -347,7 +215,7 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
347
215
|
}
|
|
348
216
|
throw new error.MastraError(
|
|
349
217
|
{
|
|
350
|
-
id: "
|
|
218
|
+
id: storage.createStorageErrorId("LANCE", "UPDATE_THREAD", "FAILED"),
|
|
351
219
|
domain: error.ErrorDomain.STORAGE,
|
|
352
220
|
category: error.ErrorCategory.THIRD_PARTY
|
|
353
221
|
},
|
|
@@ -357,7 +225,7 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
357
225
|
}
|
|
358
226
|
throw new error.MastraError(
|
|
359
227
|
{
|
|
360
|
-
id: "
|
|
228
|
+
id: storage.createStorageErrorId("LANCE", "UPDATE_THREAD", "FAILED"),
|
|
361
229
|
domain: error.ErrorDomain.STORAGE,
|
|
362
230
|
category: error.ErrorCategory.THIRD_PARTY
|
|
363
231
|
},
|
|
@@ -373,7 +241,7 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
373
241
|
} catch (error$1) {
|
|
374
242
|
throw new error.MastraError(
|
|
375
243
|
{
|
|
376
|
-
id: "
|
|
244
|
+
id: storage.createStorageErrorId("LANCE", "DELETE_THREAD", "FAILED"),
|
|
377
245
|
domain: error.ErrorDomain.STORAGE,
|
|
378
246
|
category: error.ErrorCategory.THIRD_PARTY
|
|
379
247
|
},
|
|
@@ -381,79 +249,190 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
381
249
|
);
|
|
382
250
|
}
|
|
383
251
|
}
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
252
|
+
normalizeMessage(message) {
|
|
253
|
+
const { thread_id, ...rest } = message;
|
|
254
|
+
return {
|
|
255
|
+
...rest,
|
|
256
|
+
threadId: thread_id,
|
|
257
|
+
content: typeof message.content === "string" ? (() => {
|
|
258
|
+
try {
|
|
259
|
+
return JSON.parse(message.content);
|
|
260
|
+
} catch {
|
|
261
|
+
return message.content;
|
|
262
|
+
}
|
|
263
|
+
})() : message.content
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
async listMessagesById({ messageIds }) {
|
|
267
|
+
if (messageIds.length === 0) return { messages: [] };
|
|
391
268
|
try {
|
|
392
|
-
if (threadConfig) {
|
|
393
|
-
throw new Error("ThreadConfig is not supported by LanceDB storage");
|
|
394
|
-
}
|
|
395
|
-
const limit = storage.resolveMessageLimit({ last: selectBy?.last, defaultLimit: Number.MAX_SAFE_INTEGER });
|
|
396
269
|
const table = await this.client.openTable(storage.TABLE_MESSAGES);
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
const threadIds = [...new Set(selectBy.include.map((item) => item.threadId))];
|
|
400
|
-
for (const threadId2 of threadIds) {
|
|
401
|
-
const threadQuery = table.query().where(`thread_id = '${threadId2}'`);
|
|
402
|
-
let threadRecords = await threadQuery.toArray();
|
|
403
|
-
allRecords.push(...threadRecords);
|
|
404
|
-
}
|
|
405
|
-
} else {
|
|
406
|
-
let query = table.query().where(`\`thread_id\` = '${threadId}'`);
|
|
407
|
-
allRecords = await query.toArray();
|
|
408
|
-
}
|
|
409
|
-
allRecords.sort((a, b) => {
|
|
410
|
-
const dateA = new Date(a.createdAt).getTime();
|
|
411
|
-
const dateB = new Date(b.createdAt).getTime();
|
|
412
|
-
return dateA - dateB;
|
|
413
|
-
});
|
|
414
|
-
if (selectBy?.include && selectBy.include.length > 0) {
|
|
415
|
-
allRecords = this.processMessagesWithContext(allRecords, selectBy.include);
|
|
416
|
-
}
|
|
417
|
-
if (limit !== Number.MAX_SAFE_INTEGER) {
|
|
418
|
-
allRecords = allRecords.slice(-limit);
|
|
419
|
-
}
|
|
270
|
+
const quotedIds = messageIds.map((id) => `'${id}'`).join(", ");
|
|
271
|
+
const allRecords = await table.query().where(`id IN (${quotedIds})`).toArray();
|
|
420
272
|
const messages = processResultWithTypeConversion(
|
|
421
273
|
allRecords,
|
|
422
274
|
await getTableSchema({ tableName: storage.TABLE_MESSAGES, client: this.client })
|
|
423
275
|
);
|
|
424
|
-
const
|
|
425
|
-
|
|
276
|
+
const list = new agent.MessageList().add(
|
|
277
|
+
messages.map(this.normalizeMessage),
|
|
278
|
+
"memory"
|
|
279
|
+
);
|
|
280
|
+
return { messages: list.get.all.db() };
|
|
281
|
+
} catch (error$1) {
|
|
282
|
+
throw new error.MastraError(
|
|
283
|
+
{
|
|
284
|
+
id: storage.createStorageErrorId("LANCE", "LIST_MESSAGES_BY_ID", "FAILED"),
|
|
285
|
+
domain: error.ErrorDomain.STORAGE,
|
|
286
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
287
|
+
details: {
|
|
288
|
+
messageIds: JSON.stringify(messageIds)
|
|
289
|
+
}
|
|
290
|
+
},
|
|
291
|
+
error$1
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
async listMessages(args) {
|
|
296
|
+
const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
|
|
297
|
+
const threadIds = Array.isArray(threadId) ? threadId : [threadId];
|
|
298
|
+
if (threadIds.length === 0 || threadIds.some((id) => !id.trim())) {
|
|
299
|
+
throw new error.MastraError(
|
|
300
|
+
{
|
|
301
|
+
id: storage.createStorageErrorId("LANCE", "LIST_MESSAGES", "INVALID_THREAD_ID"),
|
|
302
|
+
domain: error.ErrorDomain.STORAGE,
|
|
303
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
304
|
+
details: { threadId: Array.isArray(threadId) ? threadId.join(",") : threadId }
|
|
305
|
+
},
|
|
306
|
+
new Error("threadId must be a non-empty string or array of non-empty strings")
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
const perPage = storage.normalizePerPage(perPageInput, 40);
|
|
310
|
+
const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
311
|
+
try {
|
|
312
|
+
if (page < 0) {
|
|
313
|
+
throw new error.MastraError(
|
|
314
|
+
{
|
|
315
|
+
id: storage.createStorageErrorId("LANCE", "LIST_MESSAGES", "INVALID_PAGE"),
|
|
316
|
+
domain: error.ErrorDomain.STORAGE,
|
|
317
|
+
category: error.ErrorCategory.USER,
|
|
318
|
+
details: { page }
|
|
319
|
+
},
|
|
320
|
+
new Error("page must be >= 0")
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
const { field, direction } = this.parseOrderBy(orderBy, "ASC");
|
|
324
|
+
const table = await this.client.openTable(storage.TABLE_MESSAGES);
|
|
325
|
+
const threadCondition = threadIds.length === 1 ? `thread_id = '${this.escapeSql(threadIds[0])}'` : `thread_id IN (${threadIds.map((t) => `'${this.escapeSql(t)}'`).join(", ")})`;
|
|
326
|
+
const conditions = [threadCondition];
|
|
327
|
+
if (resourceId) {
|
|
328
|
+
conditions.push(`\`resourceId\` = '${this.escapeSql(resourceId)}'`);
|
|
329
|
+
}
|
|
330
|
+
if (filter?.dateRange?.start) {
|
|
331
|
+
const startTime = filter.dateRange.start instanceof Date ? filter.dateRange.start.getTime() : new Date(filter.dateRange.start).getTime();
|
|
332
|
+
conditions.push(`\`createdAt\` >= ${startTime}`);
|
|
333
|
+
}
|
|
334
|
+
if (filter?.dateRange?.end) {
|
|
335
|
+
const endTime = filter.dateRange.end instanceof Date ? filter.dateRange.end.getTime() : new Date(filter.dateRange.end).getTime();
|
|
336
|
+
conditions.push(`\`createdAt\` <= ${endTime}`);
|
|
337
|
+
}
|
|
338
|
+
const whereClause = conditions.join(" AND ");
|
|
339
|
+
const total = await table.countRows(whereClause);
|
|
340
|
+
const query = table.query().where(whereClause);
|
|
341
|
+
let allRecords = await query.toArray();
|
|
342
|
+
allRecords.sort((a, b) => {
|
|
343
|
+
const aValue = field === "createdAt" ? a.createdAt : a[field];
|
|
344
|
+
const bValue = field === "createdAt" ? b.createdAt : b[field];
|
|
345
|
+
if (aValue == null && bValue == null) return 0;
|
|
346
|
+
if (aValue == null) return direction === "ASC" ? -1 : 1;
|
|
347
|
+
if (bValue == null) return direction === "ASC" ? 1 : -1;
|
|
348
|
+
if (typeof aValue === "string" && typeof bValue === "string") {
|
|
349
|
+
return direction === "ASC" ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue);
|
|
350
|
+
}
|
|
351
|
+
return direction === "ASC" ? aValue - bValue : bValue - aValue;
|
|
352
|
+
});
|
|
353
|
+
const paginatedRecords = allRecords.slice(offset, offset + perPage);
|
|
354
|
+
const messages = paginatedRecords.map((row) => this.normalizeMessage(row));
|
|
355
|
+
if (total === 0 && messages.length === 0 && (!include || include.length === 0)) {
|
|
426
356
|
return {
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
} catch {
|
|
433
|
-
return msg.content;
|
|
434
|
-
}
|
|
435
|
-
})() : msg.content
|
|
357
|
+
messages: [],
|
|
358
|
+
total: 0,
|
|
359
|
+
page,
|
|
360
|
+
perPage: perPageForResponse,
|
|
361
|
+
hasMore: false
|
|
436
362
|
};
|
|
363
|
+
}
|
|
364
|
+
const messageIds = new Set(messages.map((m) => m.id));
|
|
365
|
+
if (include && include.length > 0) {
|
|
366
|
+
const threadIds2 = [...new Set(include.map((item) => item.threadId || threadId))];
|
|
367
|
+
const allThreadMessages = [];
|
|
368
|
+
for (const tid of threadIds2) {
|
|
369
|
+
const threadQuery = table.query().where(`thread_id = '${tid}'`);
|
|
370
|
+
let threadRecords = await threadQuery.toArray();
|
|
371
|
+
allThreadMessages.push(...threadRecords);
|
|
372
|
+
}
|
|
373
|
+
allThreadMessages.sort((a, b) => a.createdAt - b.createdAt);
|
|
374
|
+
const contextMessages = this.processMessagesWithContext(allThreadMessages, include);
|
|
375
|
+
const includedMessages = contextMessages.map((row) => this.normalizeMessage(row));
|
|
376
|
+
for (const includeMsg of includedMessages) {
|
|
377
|
+
if (!messageIds.has(includeMsg.id)) {
|
|
378
|
+
messages.push(includeMsg);
|
|
379
|
+
messageIds.add(includeMsg.id);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
const list = new agent.MessageList().add(messages, "memory");
|
|
384
|
+
let finalMessages = list.get.all.db();
|
|
385
|
+
finalMessages = finalMessages.sort((a, b) => {
|
|
386
|
+
const aValue = field === "createdAt" ? new Date(a.createdAt).getTime() : a[field];
|
|
387
|
+
const bValue = field === "createdAt" ? new Date(b.createdAt).getTime() : b[field];
|
|
388
|
+
if (aValue == null && bValue == null) return 0;
|
|
389
|
+
if (aValue == null) return direction === "ASC" ? -1 : 1;
|
|
390
|
+
if (bValue == null) return direction === "ASC" ? 1 : -1;
|
|
391
|
+
if (typeof aValue === "string" && typeof bValue === "string") {
|
|
392
|
+
return direction === "ASC" ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue);
|
|
393
|
+
}
|
|
394
|
+
return direction === "ASC" ? aValue - bValue : bValue - aValue;
|
|
437
395
|
});
|
|
438
|
-
const
|
|
439
|
-
|
|
440
|
-
|
|
396
|
+
const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
|
|
397
|
+
const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
|
|
398
|
+
const fetchedAll = perPageInput === false || allThreadMessagesReturned;
|
|
399
|
+
const hasMore = !fetchedAll && offset + perPage < total;
|
|
400
|
+
return {
|
|
401
|
+
messages: finalMessages,
|
|
402
|
+
total,
|
|
403
|
+
page,
|
|
404
|
+
perPage: perPageForResponse,
|
|
405
|
+
hasMore
|
|
406
|
+
};
|
|
441
407
|
} catch (error$1) {
|
|
442
|
-
|
|
408
|
+
const mastraError = new error.MastraError(
|
|
443
409
|
{
|
|
444
|
-
id: "
|
|
410
|
+
id: storage.createStorageErrorId("LANCE", "LIST_MESSAGES", "FAILED"),
|
|
445
411
|
domain: error.ErrorDomain.STORAGE,
|
|
446
|
-
category: error.ErrorCategory.THIRD_PARTY
|
|
412
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
413
|
+
details: {
|
|
414
|
+
threadId: Array.isArray(threadId) ? threadId.join(",") : threadId,
|
|
415
|
+
resourceId: resourceId ?? ""
|
|
416
|
+
}
|
|
447
417
|
},
|
|
448
418
|
error$1
|
|
449
419
|
);
|
|
420
|
+
this.logger?.error?.(mastraError.toString());
|
|
421
|
+
this.logger?.trackException?.(mastraError);
|
|
422
|
+
return {
|
|
423
|
+
messages: [],
|
|
424
|
+
total: 0,
|
|
425
|
+
page,
|
|
426
|
+
perPage: perPageForResponse,
|
|
427
|
+
hasMore: false
|
|
428
|
+
};
|
|
450
429
|
}
|
|
451
430
|
}
|
|
452
431
|
async saveMessages(args) {
|
|
453
432
|
try {
|
|
454
|
-
const { messages
|
|
433
|
+
const { messages } = args;
|
|
455
434
|
if (messages.length === 0) {
|
|
456
|
-
return [];
|
|
435
|
+
return { messages: [] };
|
|
457
436
|
}
|
|
458
437
|
const threadId = messages[0]?.threadId;
|
|
459
438
|
if (!threadId) {
|
|
@@ -489,12 +468,11 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
489
468
|
const updateRecord = { id: threadId, updatedAt: currentTime };
|
|
490
469
|
await threadsTable.mergeInsert("id").whenMatchedUpdateAll().whenNotMatchedInsertAll().execute([updateRecord]);
|
|
491
470
|
const list = new agent.MessageList().add(messages, "memory");
|
|
492
|
-
|
|
493
|
-
return list.get.all.v1();
|
|
471
|
+
return { messages: list.get.all.db() };
|
|
494
472
|
} catch (error$1) {
|
|
495
473
|
throw new error.MastraError(
|
|
496
474
|
{
|
|
497
|
-
id: "
|
|
475
|
+
id: storage.createStorageErrorId("LANCE", "SAVE_MESSAGES", "FAILED"),
|
|
498
476
|
domain: error.ErrorDomain.STORAGE,
|
|
499
477
|
category: error.ErrorCategory.THIRD_PARTY
|
|
500
478
|
},
|
|
@@ -502,32 +480,54 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
502
480
|
);
|
|
503
481
|
}
|
|
504
482
|
}
|
|
505
|
-
async
|
|
483
|
+
async listThreadsByResourceId(args) {
|
|
506
484
|
try {
|
|
507
|
-
const { resourceId, page = 0, perPage
|
|
508
|
-
const
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
485
|
+
const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
|
|
486
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
487
|
+
if (page < 0) {
|
|
488
|
+
throw new error.MastraError(
|
|
489
|
+
{
|
|
490
|
+
id: storage.createStorageErrorId("LANCE", "LIST_THREADS_BY_RESOURCE_ID", "INVALID_PAGE"),
|
|
491
|
+
domain: error.ErrorDomain.STORAGE,
|
|
492
|
+
category: error.ErrorCategory.USER,
|
|
493
|
+
details: { page }
|
|
494
|
+
},
|
|
495
|
+
new Error("page must be >= 0")
|
|
496
|
+
);
|
|
515
497
|
}
|
|
498
|
+
const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
499
|
+
const { field, direction } = this.parseOrderBy(orderBy);
|
|
500
|
+
const table = await this.client.openTable(storage.TABLE_THREADS);
|
|
501
|
+
const total = await table.countRows(`\`resourceId\` = '${this.escapeSql(resourceId)}'`);
|
|
502
|
+
const query = table.query().where(`\`resourceId\` = '${this.escapeSql(resourceId)}'`);
|
|
516
503
|
const records = await query.toArray();
|
|
517
|
-
records.sort((a, b) =>
|
|
504
|
+
records.sort((a, b) => {
|
|
505
|
+
const aValue = ["createdAt", "updatedAt"].includes(field) ? new Date(a[field]).getTime() : a[field];
|
|
506
|
+
const bValue = ["createdAt", "updatedAt"].includes(field) ? new Date(b[field]).getTime() : b[field];
|
|
507
|
+
if (aValue == null && bValue == null) return 0;
|
|
508
|
+
if (aValue == null) return direction === "ASC" ? -1 : 1;
|
|
509
|
+
if (bValue == null) return direction === "ASC" ? 1 : -1;
|
|
510
|
+
if (typeof aValue === "string" && typeof bValue === "string") {
|
|
511
|
+
return direction === "ASC" ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue);
|
|
512
|
+
}
|
|
513
|
+
return direction === "ASC" ? aValue - bValue : bValue - aValue;
|
|
514
|
+
});
|
|
515
|
+
const paginatedRecords = records.slice(offset, offset + perPage);
|
|
518
516
|
const schema = await getTableSchema({ tableName: storage.TABLE_THREADS, client: this.client });
|
|
519
|
-
const threads =
|
|
517
|
+
const threads = paginatedRecords.map(
|
|
518
|
+
(record) => processResultWithTypeConversion(record, schema)
|
|
519
|
+
);
|
|
520
520
|
return {
|
|
521
521
|
threads,
|
|
522
522
|
total,
|
|
523
523
|
page,
|
|
524
|
-
perPage,
|
|
525
|
-
hasMore:
|
|
524
|
+
perPage: perPageForResponse,
|
|
525
|
+
hasMore: offset + perPage < total
|
|
526
526
|
};
|
|
527
527
|
} catch (error$1) {
|
|
528
528
|
throw new error.MastraError(
|
|
529
529
|
{
|
|
530
|
-
id: "
|
|
530
|
+
id: storage.createStorageErrorId("LANCE", "LIST_THREADS_BY_RESOURCE_ID", "FAILED"),
|
|
531
531
|
domain: error.ErrorDomain.STORAGE,
|
|
532
532
|
category: error.ErrorCategory.THIRD_PARTY
|
|
533
533
|
},
|
|
@@ -583,127 +583,8 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
583
583
|
});
|
|
584
584
|
return Array.from(allIndices).sort((a, b) => a - b).map((index) => records[index]);
|
|
585
585
|
}
|
|
586
|
-
async getMessagesPaginated(args) {
|
|
587
|
-
try {
|
|
588
|
-
const { threadId, resourceId, selectBy, format = "v1" } = args;
|
|
589
|
-
if (!threadId) {
|
|
590
|
-
throw new Error("Thread ID is required for getMessagesPaginated");
|
|
591
|
-
}
|
|
592
|
-
const page = selectBy?.pagination?.page ?? 0;
|
|
593
|
-
const perPage = selectBy?.pagination?.perPage ?? 10;
|
|
594
|
-
const dateRange = selectBy?.pagination?.dateRange;
|
|
595
|
-
const fromDate = dateRange?.start;
|
|
596
|
-
const toDate = dateRange?.end;
|
|
597
|
-
const table = await this.client.openTable(storage.TABLE_MESSAGES);
|
|
598
|
-
const messages = [];
|
|
599
|
-
if (selectBy?.include && Array.isArray(selectBy.include)) {
|
|
600
|
-
const threadIds = [...new Set(selectBy.include.map((item) => item.threadId))];
|
|
601
|
-
const allThreadMessages = [];
|
|
602
|
-
for (const threadId2 of threadIds) {
|
|
603
|
-
const threadQuery = table.query().where(`thread_id = '${threadId2}'`);
|
|
604
|
-
let threadRecords = await threadQuery.toArray();
|
|
605
|
-
if (fromDate) threadRecords = threadRecords.filter((m) => m.createdAt >= fromDate.getTime());
|
|
606
|
-
if (toDate) threadRecords = threadRecords.filter((m) => m.createdAt <= toDate.getTime());
|
|
607
|
-
allThreadMessages.push(...threadRecords);
|
|
608
|
-
}
|
|
609
|
-
allThreadMessages.sort((a, b) => a.createdAt - b.createdAt);
|
|
610
|
-
const contextMessages = this.processMessagesWithContext(allThreadMessages, selectBy.include);
|
|
611
|
-
messages.push(...contextMessages);
|
|
612
|
-
}
|
|
613
|
-
const conditions = [`thread_id = '${threadId}'`];
|
|
614
|
-
if (resourceId) {
|
|
615
|
-
conditions.push(`\`resourceId\` = '${resourceId}'`);
|
|
616
|
-
}
|
|
617
|
-
if (fromDate) {
|
|
618
|
-
conditions.push(`\`createdAt\` >= ${fromDate.getTime()}`);
|
|
619
|
-
}
|
|
620
|
-
if (toDate) {
|
|
621
|
-
conditions.push(`\`createdAt\` <= ${toDate.getTime()}`);
|
|
622
|
-
}
|
|
623
|
-
let total = 0;
|
|
624
|
-
if (conditions.length > 0) {
|
|
625
|
-
total = await table.countRows(conditions.join(" AND "));
|
|
626
|
-
} else {
|
|
627
|
-
total = await table.countRows();
|
|
628
|
-
}
|
|
629
|
-
if (total === 0 && messages.length === 0) {
|
|
630
|
-
return {
|
|
631
|
-
messages: [],
|
|
632
|
-
total: 0,
|
|
633
|
-
page,
|
|
634
|
-
perPage,
|
|
635
|
-
hasMore: false
|
|
636
|
-
};
|
|
637
|
-
}
|
|
638
|
-
const excludeIds = messages.map((m) => m.id);
|
|
639
|
-
let selectedMessages = [];
|
|
640
|
-
if (selectBy?.last && selectBy.last > 0) {
|
|
641
|
-
const query = table.query();
|
|
642
|
-
if (conditions.length > 0) {
|
|
643
|
-
query.where(conditions.join(" AND "));
|
|
644
|
-
}
|
|
645
|
-
let records = await query.toArray();
|
|
646
|
-
records = records.sort((a, b) => a.createdAt - b.createdAt);
|
|
647
|
-
if (excludeIds.length > 0) {
|
|
648
|
-
records = records.filter((m) => !excludeIds.includes(m.id));
|
|
649
|
-
}
|
|
650
|
-
selectedMessages = records.slice(-selectBy.last);
|
|
651
|
-
} else {
|
|
652
|
-
const query = table.query();
|
|
653
|
-
if (conditions.length > 0) {
|
|
654
|
-
query.where(conditions.join(" AND "));
|
|
655
|
-
}
|
|
656
|
-
let records = await query.toArray();
|
|
657
|
-
records = records.sort((a, b) => a.createdAt - b.createdAt);
|
|
658
|
-
if (excludeIds.length > 0) {
|
|
659
|
-
records = records.filter((m) => !excludeIds.includes(m.id));
|
|
660
|
-
}
|
|
661
|
-
selectedMessages = records.slice(page * perPage, (page + 1) * perPage);
|
|
662
|
-
}
|
|
663
|
-
const allMessages = [...messages, ...selectedMessages];
|
|
664
|
-
const seen = /* @__PURE__ */ new Set();
|
|
665
|
-
const dedupedMessages = allMessages.filter((m) => {
|
|
666
|
-
const key = `${m.id}:${m.thread_id}`;
|
|
667
|
-
if (seen.has(key)) return false;
|
|
668
|
-
seen.add(key);
|
|
669
|
-
return true;
|
|
670
|
-
});
|
|
671
|
-
const formattedMessages = dedupedMessages.map((msg) => {
|
|
672
|
-
const { thread_id, ...rest } = msg;
|
|
673
|
-
return {
|
|
674
|
-
...rest,
|
|
675
|
-
threadId: thread_id,
|
|
676
|
-
content: typeof msg.content === "string" ? (() => {
|
|
677
|
-
try {
|
|
678
|
-
return JSON.parse(msg.content);
|
|
679
|
-
} catch {
|
|
680
|
-
return msg.content;
|
|
681
|
-
}
|
|
682
|
-
})() : msg.content
|
|
683
|
-
};
|
|
684
|
-
});
|
|
685
|
-
const list = new agent.MessageList().add(formattedMessages, "memory");
|
|
686
|
-
return {
|
|
687
|
-
messages: format === "v2" ? list.get.all.v2() : list.get.all.v1(),
|
|
688
|
-
total,
|
|
689
|
-
// Total should be the count of messages matching the filters
|
|
690
|
-
page,
|
|
691
|
-
perPage,
|
|
692
|
-
hasMore: total > (page + 1) * perPage
|
|
693
|
-
};
|
|
694
|
-
} catch (error$1) {
|
|
695
|
-
throw new error.MastraError(
|
|
696
|
-
{
|
|
697
|
-
id: "LANCE_STORE_GET_MESSAGES_PAGINATED_FAILED",
|
|
698
|
-
domain: error.ErrorDomain.STORAGE,
|
|
699
|
-
category: error.ErrorCategory.THIRD_PARTY
|
|
700
|
-
},
|
|
701
|
-
error$1
|
|
702
|
-
);
|
|
703
|
-
}
|
|
704
|
-
}
|
|
705
586
|
/**
|
|
706
|
-
* Parse message data from LanceDB record format to
|
|
587
|
+
* Parse message data from LanceDB record format to MastraDBMessage format
|
|
707
588
|
*/
|
|
708
589
|
parseMessageData(data) {
|
|
709
590
|
const { thread_id, ...rest } = data;
|
|
@@ -781,7 +662,7 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
781
662
|
} catch (error$1) {
|
|
782
663
|
throw new error.MastraError(
|
|
783
664
|
{
|
|
784
|
-
id: "
|
|
665
|
+
id: storage.createStorageErrorId("LANCE", "UPDATE_MESSAGES", "FAILED"),
|
|
785
666
|
domain: error.ErrorDomain.STORAGE,
|
|
786
667
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
787
668
|
details: { count: messages.length }
|
|
@@ -858,7 +739,7 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
858
739
|
} catch (error$1) {
|
|
859
740
|
throw new error.MastraError(
|
|
860
741
|
{
|
|
861
|
-
id: "
|
|
742
|
+
id: storage.createStorageErrorId("LANCE", "GET_RESOURCE_BY_ID", "FAILED"),
|
|
862
743
|
domain: error.ErrorDomain.STORAGE,
|
|
863
744
|
category: error.ErrorCategory.THIRD_PARTY
|
|
864
745
|
},
|
|
@@ -882,7 +763,7 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
882
763
|
} catch (error$1) {
|
|
883
764
|
throw new error.MastraError(
|
|
884
765
|
{
|
|
885
|
-
id: "
|
|
766
|
+
id: storage.createStorageErrorId("LANCE", "SAVE_RESOURCE", "FAILED"),
|
|
886
767
|
domain: error.ErrorDomain.STORAGE,
|
|
887
768
|
category: error.ErrorCategory.THIRD_PARTY
|
|
888
769
|
},
|
|
@@ -936,7 +817,7 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
936
817
|
}
|
|
937
818
|
throw new error.MastraError(
|
|
938
819
|
{
|
|
939
|
-
id: "
|
|
820
|
+
id: storage.createStorageErrorId("LANCE", "UPDATE_RESOURCE", "FAILED"),
|
|
940
821
|
domain: error.ErrorDomain.STORAGE,
|
|
941
822
|
category: error.ErrorCategory.THIRD_PARTY
|
|
942
823
|
},
|
|
@@ -1027,7 +908,7 @@ var StoreOperationsLance = class extends storage.StoreOperations {
|
|
|
1027
908
|
} catch (error$1) {
|
|
1028
909
|
throw new error.MastraError(
|
|
1029
910
|
{
|
|
1030
|
-
id: "
|
|
911
|
+
id: storage.createStorageErrorId("LANCE", "CREATE_TABLE", "INVALID_ARGS"),
|
|
1031
912
|
domain: error.ErrorDomain.STORAGE,
|
|
1032
913
|
category: error.ErrorCategory.USER,
|
|
1033
914
|
details: { tableName }
|
|
@@ -1045,7 +926,7 @@ var StoreOperationsLance = class extends storage.StoreOperations {
|
|
|
1045
926
|
}
|
|
1046
927
|
throw new error.MastraError(
|
|
1047
928
|
{
|
|
1048
|
-
id: "
|
|
929
|
+
id: storage.createStorageErrorId("LANCE", "CREATE_TABLE", "FAILED"),
|
|
1049
930
|
domain: error.ErrorDomain.STORAGE,
|
|
1050
931
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
1051
932
|
details: { tableName }
|
|
@@ -1065,7 +946,7 @@ var StoreOperationsLance = class extends storage.StoreOperations {
|
|
|
1065
946
|
} catch (validationError) {
|
|
1066
947
|
throw new error.MastraError(
|
|
1067
948
|
{
|
|
1068
|
-
id: "
|
|
949
|
+
id: storage.createStorageErrorId("LANCE", "DROP_TABLE", "INVALID_ARGS"),
|
|
1069
950
|
domain: error.ErrorDomain.STORAGE,
|
|
1070
951
|
category: error.ErrorCategory.USER,
|
|
1071
952
|
text: validationError.message,
|
|
@@ -1083,7 +964,7 @@ var StoreOperationsLance = class extends storage.StoreOperations {
|
|
|
1083
964
|
}
|
|
1084
965
|
throw new error.MastraError(
|
|
1085
966
|
{
|
|
1086
|
-
id: "
|
|
967
|
+
id: storage.createStorageErrorId("LANCE", "DROP_TABLE", "FAILED"),
|
|
1087
968
|
domain: error.ErrorDomain.STORAGE,
|
|
1088
969
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
1089
970
|
details: { tableName }
|
|
@@ -1114,7 +995,7 @@ var StoreOperationsLance = class extends storage.StoreOperations {
|
|
|
1114
995
|
} catch (validationError) {
|
|
1115
996
|
throw new error.MastraError(
|
|
1116
997
|
{
|
|
1117
|
-
id: "
|
|
998
|
+
id: storage.createStorageErrorId("LANCE", "ALTER_TABLE", "INVALID_ARGS"),
|
|
1118
999
|
domain: error.ErrorDomain.STORAGE,
|
|
1119
1000
|
category: error.ErrorCategory.USER,
|
|
1120
1001
|
text: validationError.message,
|
|
@@ -1149,7 +1030,7 @@ var StoreOperationsLance = class extends storage.StoreOperations {
|
|
|
1149
1030
|
} catch (error$1) {
|
|
1150
1031
|
throw new error.MastraError(
|
|
1151
1032
|
{
|
|
1152
|
-
id: "
|
|
1033
|
+
id: storage.createStorageErrorId("LANCE", "ALTER_TABLE", "FAILED"),
|
|
1153
1034
|
domain: error.ErrorDomain.STORAGE,
|
|
1154
1035
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
1155
1036
|
details: { tableName }
|
|
@@ -1169,7 +1050,7 @@ var StoreOperationsLance = class extends storage.StoreOperations {
|
|
|
1169
1050
|
} catch (validationError) {
|
|
1170
1051
|
throw new error.MastraError(
|
|
1171
1052
|
{
|
|
1172
|
-
id: "
|
|
1053
|
+
id: storage.createStorageErrorId("LANCE", "CLEAR_TABLE", "INVALID_ARGS"),
|
|
1173
1054
|
domain: error.ErrorDomain.STORAGE,
|
|
1174
1055
|
category: error.ErrorCategory.USER,
|
|
1175
1056
|
text: validationError.message,
|
|
@@ -1184,7 +1065,7 @@ var StoreOperationsLance = class extends storage.StoreOperations {
|
|
|
1184
1065
|
} catch (error$1) {
|
|
1185
1066
|
throw new error.MastraError(
|
|
1186
1067
|
{
|
|
1187
|
-
id: "
|
|
1068
|
+
id: storage.createStorageErrorId("LANCE", "CLEAR_TABLE", "FAILED"),
|
|
1188
1069
|
domain: error.ErrorDomain.STORAGE,
|
|
1189
1070
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
1190
1071
|
details: { tableName }
|
|
@@ -1207,7 +1088,7 @@ var StoreOperationsLance = class extends storage.StoreOperations {
|
|
|
1207
1088
|
} catch (validationError) {
|
|
1208
1089
|
throw new error.MastraError(
|
|
1209
1090
|
{
|
|
1210
|
-
id: "
|
|
1091
|
+
id: storage.createStorageErrorId("LANCE", "INSERT", "INVALID_ARGS"),
|
|
1211
1092
|
domain: error.ErrorDomain.STORAGE,
|
|
1212
1093
|
category: error.ErrorCategory.USER,
|
|
1213
1094
|
text: validationError.message,
|
|
@@ -1226,12 +1107,11 @@ var StoreOperationsLance = class extends storage.StoreOperations {
|
|
|
1226
1107
|
processedRecord[key] = JSON.stringify(processedRecord[key]);
|
|
1227
1108
|
}
|
|
1228
1109
|
}
|
|
1229
|
-
console.log(await table.schema());
|
|
1230
1110
|
await table.mergeInsert(primaryId).whenMatchedUpdateAll().whenNotMatchedInsertAll().execute([processedRecord]);
|
|
1231
1111
|
} catch (error$1) {
|
|
1232
1112
|
throw new error.MastraError(
|
|
1233
1113
|
{
|
|
1234
|
-
id: "
|
|
1114
|
+
id: storage.createStorageErrorId("LANCE", "INSERT", "FAILED"),
|
|
1235
1115
|
domain: error.ErrorDomain.STORAGE,
|
|
1236
1116
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
1237
1117
|
details: { tableName }
|
|
@@ -1254,7 +1134,7 @@ var StoreOperationsLance = class extends storage.StoreOperations {
|
|
|
1254
1134
|
} catch (validationError) {
|
|
1255
1135
|
throw new error.MastraError(
|
|
1256
1136
|
{
|
|
1257
|
-
id: "
|
|
1137
|
+
id: storage.createStorageErrorId("LANCE", "BATCH_INSERT", "INVALID_ARGS"),
|
|
1258
1138
|
domain: error.ErrorDomain.STORAGE,
|
|
1259
1139
|
category: error.ErrorCategory.USER,
|
|
1260
1140
|
text: validationError.message,
|
|
@@ -1276,12 +1156,11 @@ var StoreOperationsLance = class extends storage.StoreOperations {
|
|
|
1276
1156
|
}
|
|
1277
1157
|
return processedRecord;
|
|
1278
1158
|
});
|
|
1279
|
-
console.log(processedRecords);
|
|
1280
1159
|
await table.mergeInsert(primaryId).whenMatchedUpdateAll().whenNotMatchedInsertAll().execute(processedRecords);
|
|
1281
1160
|
} catch (error$1) {
|
|
1282
1161
|
throw new error.MastraError(
|
|
1283
1162
|
{
|
|
1284
|
-
id: "
|
|
1163
|
+
id: storage.createStorageErrorId("LANCE", "BATCH_INSERT", "FAILED"),
|
|
1285
1164
|
domain: error.ErrorDomain.STORAGE,
|
|
1286
1165
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
1287
1166
|
details: { tableName }
|
|
@@ -1304,7 +1183,7 @@ var StoreOperationsLance = class extends storage.StoreOperations {
|
|
|
1304
1183
|
} catch (validationError) {
|
|
1305
1184
|
throw new error.MastraError(
|
|
1306
1185
|
{
|
|
1307
|
-
id: "
|
|
1186
|
+
id: storage.createStorageErrorId("LANCE", "LOAD", "INVALID_ARGS"),
|
|
1308
1187
|
domain: error.ErrorDomain.STORAGE,
|
|
1309
1188
|
category: error.ErrorCategory.USER,
|
|
1310
1189
|
text: validationError.message,
|
|
@@ -1343,7 +1222,7 @@ var StoreOperationsLance = class extends storage.StoreOperations {
|
|
|
1343
1222
|
if (error$1 instanceof error.MastraError) throw error$1;
|
|
1344
1223
|
throw new error.MastraError(
|
|
1345
1224
|
{
|
|
1346
|
-
id: "
|
|
1225
|
+
id: storage.createStorageErrorId("LANCE", "LOAD", "FAILED"),
|
|
1347
1226
|
domain: error.ErrorDomain.STORAGE,
|
|
1348
1227
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
1349
1228
|
details: { tableName, keyCount: Object.keys(keys).length, firstKey: Object.keys(keys)[0] ?? "" }
|
|
@@ -1360,28 +1239,53 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1360
1239
|
this.client = client;
|
|
1361
1240
|
}
|
|
1362
1241
|
async saveScore(score) {
|
|
1242
|
+
let validatedScore;
|
|
1243
|
+
try {
|
|
1244
|
+
validatedScore = evals.saveScorePayloadSchema.parse(score);
|
|
1245
|
+
} catch (error$1) {
|
|
1246
|
+
throw new error.MastraError(
|
|
1247
|
+
{
|
|
1248
|
+
id: storage.createStorageErrorId("LANCE", "SAVE_SCORE", "VALIDATION_FAILED"),
|
|
1249
|
+
text: "Failed to save score in LanceStorage",
|
|
1250
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1251
|
+
category: error.ErrorCategory.USER,
|
|
1252
|
+
details: {
|
|
1253
|
+
scorer: score.scorer?.id ?? "unknown",
|
|
1254
|
+
entityId: score.entityId ?? "unknown",
|
|
1255
|
+
entityType: score.entityType ?? "unknown",
|
|
1256
|
+
traceId: score.traceId ?? "",
|
|
1257
|
+
spanId: score.spanId ?? ""
|
|
1258
|
+
}
|
|
1259
|
+
},
|
|
1260
|
+
error$1
|
|
1261
|
+
);
|
|
1262
|
+
}
|
|
1263
|
+
const id = crypto.randomUUID();
|
|
1264
|
+
const now = /* @__PURE__ */ new Date();
|
|
1363
1265
|
try {
|
|
1364
1266
|
const table = await this.client.openTable(storage.TABLE_SCORERS);
|
|
1365
1267
|
const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
|
|
1366
1268
|
const allowedFields = new Set(schema.fields.map((f) => f.name));
|
|
1367
1269
|
const filteredScore = {};
|
|
1368
|
-
Object.keys(
|
|
1270
|
+
for (const key of Object.keys(validatedScore)) {
|
|
1369
1271
|
if (allowedFields.has(key)) {
|
|
1370
|
-
filteredScore[key] =
|
|
1272
|
+
filteredScore[key] = validatedScore[key];
|
|
1371
1273
|
}
|
|
1372
|
-
}
|
|
1274
|
+
}
|
|
1373
1275
|
for (const key in filteredScore) {
|
|
1374
1276
|
if (filteredScore[key] !== null && typeof filteredScore[key] === "object" && !(filteredScore[key] instanceof Date)) {
|
|
1375
1277
|
filteredScore[key] = JSON.stringify(filteredScore[key]);
|
|
1376
1278
|
}
|
|
1377
1279
|
}
|
|
1378
|
-
|
|
1280
|
+
filteredScore.id = id;
|
|
1281
|
+
filteredScore.createdAt = now;
|
|
1282
|
+
filteredScore.updatedAt = now;
|
|
1379
1283
|
await table.add([filteredScore], { mode: "append" });
|
|
1380
|
-
return { score };
|
|
1284
|
+
return { score: { ...validatedScore, id, createdAt: now, updatedAt: now } };
|
|
1381
1285
|
} catch (error$1) {
|
|
1382
1286
|
throw new error.MastraError(
|
|
1383
1287
|
{
|
|
1384
|
-
id: "
|
|
1288
|
+
id: storage.createStorageErrorId("LANCE", "SAVE_SCORE", "FAILED"),
|
|
1385
1289
|
text: "Failed to save score in LanceStorage",
|
|
1386
1290
|
domain: error.ErrorDomain.STORAGE,
|
|
1387
1291
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
@@ -1397,12 +1301,11 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1397
1301
|
const query = table.query().where(`id = '${id}'`).limit(1);
|
|
1398
1302
|
const records = await query.toArray();
|
|
1399
1303
|
if (records.length === 0) return null;
|
|
1400
|
-
|
|
1401
|
-
return processResultWithTypeConversion(records[0], schema);
|
|
1304
|
+
return await this.transformScoreRow(records[0]);
|
|
1402
1305
|
} catch (error$1) {
|
|
1403
1306
|
throw new error.MastraError(
|
|
1404
1307
|
{
|
|
1405
|
-
id: "
|
|
1308
|
+
id: storage.createStorageErrorId("LANCE", "GET_SCORE_BY_ID", "FAILED"),
|
|
1406
1309
|
text: "Failed to get score by id in LanceStorage",
|
|
1407
1310
|
domain: error.ErrorDomain.STORAGE,
|
|
1408
1311
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
@@ -1412,34 +1315,76 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1412
1315
|
);
|
|
1413
1316
|
}
|
|
1414
1317
|
}
|
|
1415
|
-
|
|
1318
|
+
/**
|
|
1319
|
+
* LanceDB-specific score row transformation.
|
|
1320
|
+
*
|
|
1321
|
+
* Note: This implementation does NOT use coreTransformScoreRow because:
|
|
1322
|
+
* 1. LanceDB stores schema information in the table itself (requires async fetch)
|
|
1323
|
+
* 2. Uses processResultWithTypeConversion utility for LanceDB-specific type handling
|
|
1324
|
+
*/
|
|
1325
|
+
async transformScoreRow(row) {
|
|
1326
|
+
const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
|
|
1327
|
+
const transformed = processResultWithTypeConversion(row, schema);
|
|
1328
|
+
return {
|
|
1329
|
+
...transformed,
|
|
1330
|
+
createdAt: row.createdAt,
|
|
1331
|
+
updatedAt: row.updatedAt
|
|
1332
|
+
};
|
|
1333
|
+
}
|
|
1334
|
+
async listScoresByScorerId({
|
|
1416
1335
|
scorerId,
|
|
1417
|
-
pagination
|
|
1336
|
+
pagination,
|
|
1337
|
+
entityId,
|
|
1338
|
+
entityType,
|
|
1339
|
+
source
|
|
1418
1340
|
}) {
|
|
1419
1341
|
try {
|
|
1342
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1343
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
1344
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1420
1345
|
const table = await this.client.openTable(storage.TABLE_SCORERS);
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1346
|
+
let query = table.query().where(`\`scorerId\` = '${scorerId}'`);
|
|
1347
|
+
if (source) {
|
|
1348
|
+
query = query.where(`\`source\` = '${source}'`);
|
|
1349
|
+
}
|
|
1350
|
+
if (entityId) {
|
|
1351
|
+
query = query.where(`\`entityId\` = '${entityId}'`);
|
|
1352
|
+
}
|
|
1353
|
+
if (entityType) {
|
|
1354
|
+
query = query.where(`\`entityType\` = '${entityType}'`);
|
|
1355
|
+
}
|
|
1356
|
+
let totalQuery = table.query().where(`\`scorerId\` = '${scorerId}'`);
|
|
1357
|
+
if (source) {
|
|
1358
|
+
totalQuery = totalQuery.where(`\`source\` = '${source}'`);
|
|
1359
|
+
}
|
|
1360
|
+
if (entityId) {
|
|
1361
|
+
totalQuery = totalQuery.where(`\`entityId\` = '${entityId}'`);
|
|
1362
|
+
}
|
|
1363
|
+
if (entityType) {
|
|
1364
|
+
totalQuery = totalQuery.where(`\`entityType\` = '${entityType}'`);
|
|
1365
|
+
}
|
|
1366
|
+
const allRecords = await totalQuery.toArray();
|
|
1429
1367
|
const total = allRecords.length;
|
|
1368
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1369
|
+
if (perPageInput !== false) {
|
|
1370
|
+
query = query.limit(perPage);
|
|
1371
|
+
if (start > 0) query = query.offset(start);
|
|
1372
|
+
}
|
|
1373
|
+
const records = await query.toArray();
|
|
1374
|
+
const scores = await Promise.all(records.map(async (record) => await this.transformScoreRow(record)));
|
|
1430
1375
|
return {
|
|
1431
1376
|
pagination: {
|
|
1432
1377
|
page,
|
|
1433
|
-
perPage,
|
|
1378
|
+
perPage: perPageForResponse,
|
|
1434
1379
|
total,
|
|
1435
|
-
hasMore:
|
|
1380
|
+
hasMore: end < total
|
|
1436
1381
|
},
|
|
1437
1382
|
scores
|
|
1438
1383
|
};
|
|
1439
1384
|
} catch (error$1) {
|
|
1440
1385
|
throw new error.MastraError(
|
|
1441
1386
|
{
|
|
1442
|
-
id: "
|
|
1387
|
+
id: storage.createStorageErrorId("LANCE", "LIST_SCORES_BY_SCORER_ID", "FAILED"),
|
|
1443
1388
|
text: "Failed to get scores by scorerId in LanceStorage",
|
|
1444
1389
|
domain: error.ErrorDomain.STORAGE,
|
|
1445
1390
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
@@ -1449,34 +1394,38 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1449
1394
|
);
|
|
1450
1395
|
}
|
|
1451
1396
|
}
|
|
1452
|
-
async
|
|
1397
|
+
async listScoresByRunId({
|
|
1453
1398
|
runId,
|
|
1454
1399
|
pagination
|
|
1455
1400
|
}) {
|
|
1456
1401
|
try {
|
|
1402
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1403
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
1404
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1457
1405
|
const table = await this.client.openTable(storage.TABLE_SCORERS);
|
|
1458
|
-
const { page = 0, perPage = 10 } = pagination || {};
|
|
1459
|
-
const offset = page * perPage;
|
|
1460
|
-
const query = table.query().where(`\`runId\` = '${runId}'`).limit(perPage);
|
|
1461
|
-
if (offset > 0) query.offset(offset);
|
|
1462
|
-
const records = await query.toArray();
|
|
1463
|
-
const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
|
|
1464
|
-
const scores = processResultWithTypeConversion(records, schema);
|
|
1465
1406
|
const allRecords = await table.query().where(`\`runId\` = '${runId}'`).toArray();
|
|
1466
1407
|
const total = allRecords.length;
|
|
1408
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1409
|
+
let query = table.query().where(`\`runId\` = '${runId}'`);
|
|
1410
|
+
if (perPageInput !== false) {
|
|
1411
|
+
query = query.limit(perPage);
|
|
1412
|
+
if (start > 0) query = query.offset(start);
|
|
1413
|
+
}
|
|
1414
|
+
const records = await query.toArray();
|
|
1415
|
+
const scores = await Promise.all(records.map(async (record) => await this.transformScoreRow(record)));
|
|
1467
1416
|
return {
|
|
1468
1417
|
pagination: {
|
|
1469
1418
|
page,
|
|
1470
|
-
perPage,
|
|
1419
|
+
perPage: perPageForResponse,
|
|
1471
1420
|
total,
|
|
1472
|
-
hasMore:
|
|
1421
|
+
hasMore: end < total
|
|
1473
1422
|
},
|
|
1474
1423
|
scores
|
|
1475
1424
|
};
|
|
1476
1425
|
} catch (error$1) {
|
|
1477
1426
|
throw new error.MastraError(
|
|
1478
1427
|
{
|
|
1479
|
-
id: "
|
|
1428
|
+
id: storage.createStorageErrorId("LANCE", "LIST_SCORES_BY_RUN_ID", "FAILED"),
|
|
1480
1429
|
text: "Failed to get scores by runId in LanceStorage",
|
|
1481
1430
|
domain: error.ErrorDomain.STORAGE,
|
|
1482
1431
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
@@ -1486,35 +1435,39 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1486
1435
|
);
|
|
1487
1436
|
}
|
|
1488
1437
|
}
|
|
1489
|
-
async
|
|
1438
|
+
async listScoresByEntityId({
|
|
1490
1439
|
entityId,
|
|
1491
1440
|
entityType,
|
|
1492
1441
|
pagination
|
|
1493
1442
|
}) {
|
|
1494
1443
|
try {
|
|
1444
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1445
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
1446
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1495
1447
|
const table = await this.client.openTable(storage.TABLE_SCORERS);
|
|
1496
|
-
const { page = 0, perPage = 10 } = pagination || {};
|
|
1497
|
-
const offset = page * perPage;
|
|
1498
|
-
const query = table.query().where(`\`entityId\` = '${entityId}' AND \`entityType\` = '${entityType}'`).limit(perPage);
|
|
1499
|
-
if (offset > 0) query.offset(offset);
|
|
1500
|
-
const records = await query.toArray();
|
|
1501
|
-
const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
|
|
1502
|
-
const scores = processResultWithTypeConversion(records, schema);
|
|
1503
1448
|
const allRecords = await table.query().where(`\`entityId\` = '${entityId}' AND \`entityType\` = '${entityType}'`).toArray();
|
|
1504
1449
|
const total = allRecords.length;
|
|
1450
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1451
|
+
let query = table.query().where(`\`entityId\` = '${entityId}' AND \`entityType\` = '${entityType}'`);
|
|
1452
|
+
if (perPageInput !== false) {
|
|
1453
|
+
query = query.limit(perPage);
|
|
1454
|
+
if (start > 0) query = query.offset(start);
|
|
1455
|
+
}
|
|
1456
|
+
const records = await query.toArray();
|
|
1457
|
+
const scores = await Promise.all(records.map(async (record) => await this.transformScoreRow(record)));
|
|
1505
1458
|
return {
|
|
1506
1459
|
pagination: {
|
|
1507
1460
|
page,
|
|
1508
|
-
perPage,
|
|
1461
|
+
perPage: perPageForResponse,
|
|
1509
1462
|
total,
|
|
1510
|
-
hasMore:
|
|
1463
|
+
hasMore: end < total
|
|
1511
1464
|
},
|
|
1512
1465
|
scores
|
|
1513
1466
|
};
|
|
1514
1467
|
} catch (error$1) {
|
|
1515
1468
|
throw new error.MastraError(
|
|
1516
1469
|
{
|
|
1517
|
-
id: "
|
|
1470
|
+
id: storage.createStorageErrorId("LANCE", "LIST_SCORES_BY_ENTITY_ID", "FAILED"),
|
|
1518
1471
|
text: "Failed to get scores by entityId and entityType in LanceStorage",
|
|
1519
1472
|
domain: error.ErrorDomain.STORAGE,
|
|
1520
1473
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
@@ -1524,198 +1477,48 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1524
1477
|
);
|
|
1525
1478
|
}
|
|
1526
1479
|
}
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
constructor({ client, operations }) {
|
|
1532
|
-
super();
|
|
1533
|
-
this.client = client;
|
|
1534
|
-
this.operations = operations;
|
|
1535
|
-
}
|
|
1536
|
-
async saveTrace({ trace }) {
|
|
1537
|
-
try {
|
|
1538
|
-
const table = await this.client.openTable(storage.TABLE_TRACES);
|
|
1539
|
-
const record = {
|
|
1540
|
-
...trace,
|
|
1541
|
-
attributes: JSON.stringify(trace.attributes),
|
|
1542
|
-
status: JSON.stringify(trace.status),
|
|
1543
|
-
events: JSON.stringify(trace.events),
|
|
1544
|
-
links: JSON.stringify(trace.links),
|
|
1545
|
-
other: JSON.stringify(trace.other)
|
|
1546
|
-
};
|
|
1547
|
-
await table.add([record], { mode: "append" });
|
|
1548
|
-
return trace;
|
|
1549
|
-
} catch (error$1) {
|
|
1550
|
-
throw new error.MastraError(
|
|
1551
|
-
{
|
|
1552
|
-
id: "LANCE_STORE_SAVE_TRACE_FAILED",
|
|
1553
|
-
domain: error.ErrorDomain.STORAGE,
|
|
1554
|
-
category: error.ErrorCategory.THIRD_PARTY
|
|
1555
|
-
},
|
|
1556
|
-
error$1
|
|
1557
|
-
);
|
|
1558
|
-
}
|
|
1559
|
-
}
|
|
1560
|
-
async getTraceById({ traceId }) {
|
|
1561
|
-
try {
|
|
1562
|
-
const table = await this.client.openTable(storage.TABLE_TRACES);
|
|
1563
|
-
const query = table.query().where(`id = '${traceId}'`);
|
|
1564
|
-
const records = await query.toArray();
|
|
1565
|
-
return records[0];
|
|
1566
|
-
} catch (error$1) {
|
|
1567
|
-
throw new error.MastraError(
|
|
1568
|
-
{
|
|
1569
|
-
id: "LANCE_STORE_GET_TRACE_BY_ID_FAILED",
|
|
1570
|
-
domain: error.ErrorDomain.STORAGE,
|
|
1571
|
-
category: error.ErrorCategory.THIRD_PARTY
|
|
1572
|
-
},
|
|
1573
|
-
error$1
|
|
1574
|
-
);
|
|
1575
|
-
}
|
|
1576
|
-
}
|
|
1577
|
-
async getTraces({
|
|
1578
|
-
name,
|
|
1579
|
-
scope,
|
|
1580
|
-
page = 1,
|
|
1581
|
-
perPage = 10,
|
|
1582
|
-
attributes
|
|
1480
|
+
async listScoresBySpan({
|
|
1481
|
+
traceId,
|
|
1482
|
+
spanId,
|
|
1483
|
+
pagination
|
|
1583
1484
|
}) {
|
|
1584
1485
|
try {
|
|
1585
|
-
const
|
|
1586
|
-
const
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
}
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
}
|
|
1593
|
-
if (
|
|
1594
|
-
query
|
|
1595
|
-
|
|
1596
|
-
const offset = (page - 1) * perPage;
|
|
1597
|
-
query.limit(perPage);
|
|
1598
|
-
if (offset > 0) {
|
|
1599
|
-
query.offset(offset);
|
|
1600
|
-
}
|
|
1601
|
-
const records = await query.toArray();
|
|
1602
|
-
return records.map((record) => {
|
|
1603
|
-
const processed = {
|
|
1604
|
-
...record,
|
|
1605
|
-
attributes: record.attributes ? JSON.parse(record.attributes) : {},
|
|
1606
|
-
status: record.status ? JSON.parse(record.status) : {},
|
|
1607
|
-
events: record.events ? JSON.parse(record.events) : [],
|
|
1608
|
-
links: record.links ? JSON.parse(record.links) : [],
|
|
1609
|
-
other: record.other ? JSON.parse(record.other) : {},
|
|
1610
|
-
startTime: new Date(record.startTime),
|
|
1611
|
-
endTime: new Date(record.endTime),
|
|
1612
|
-
createdAt: new Date(record.createdAt)
|
|
1613
|
-
};
|
|
1614
|
-
if (processed.parentSpanId === null || processed.parentSpanId === void 0) {
|
|
1615
|
-
processed.parentSpanId = "";
|
|
1616
|
-
} else {
|
|
1617
|
-
processed.parentSpanId = String(processed.parentSpanId);
|
|
1618
|
-
}
|
|
1619
|
-
return processed;
|
|
1620
|
-
});
|
|
1621
|
-
} catch (error$1) {
|
|
1622
|
-
throw new error.MastraError(
|
|
1623
|
-
{
|
|
1624
|
-
id: "LANCE_STORE_GET_TRACES_FAILED",
|
|
1625
|
-
domain: error.ErrorDomain.STORAGE,
|
|
1626
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
1627
|
-
details: { name: name ?? "", scope: scope ?? "" }
|
|
1628
|
-
},
|
|
1629
|
-
error$1
|
|
1630
|
-
);
|
|
1631
|
-
}
|
|
1632
|
-
}
|
|
1633
|
-
async getTracesPaginated(args) {
|
|
1634
|
-
try {
|
|
1635
|
-
const table = await this.client.openTable(storage.TABLE_TRACES);
|
|
1636
|
-
const query = table.query();
|
|
1637
|
-
const conditions = [];
|
|
1638
|
-
if (args.name) {
|
|
1639
|
-
conditions.push(`name = '${args.name}'`);
|
|
1640
|
-
}
|
|
1641
|
-
if (args.scope) {
|
|
1642
|
-
conditions.push(`scope = '${args.scope}'`);
|
|
1643
|
-
}
|
|
1644
|
-
if (args.attributes) {
|
|
1645
|
-
const attributesStr = JSON.stringify(args.attributes);
|
|
1646
|
-
conditions.push(`attributes LIKE '%${attributesStr.replace(/"/g, '\\"')}%'`);
|
|
1647
|
-
}
|
|
1648
|
-
if (args.dateRange?.start) {
|
|
1649
|
-
conditions.push(`\`createdAt\` >= ${args.dateRange.start.getTime()}`);
|
|
1650
|
-
}
|
|
1651
|
-
if (args.dateRange?.end) {
|
|
1652
|
-
conditions.push(`\`createdAt\` <= ${args.dateRange.end.getTime()}`);
|
|
1653
|
-
}
|
|
1654
|
-
if (conditions.length > 0) {
|
|
1655
|
-
const whereClause = conditions.join(" AND ");
|
|
1656
|
-
query.where(whereClause);
|
|
1657
|
-
}
|
|
1658
|
-
let total = 0;
|
|
1659
|
-
if (conditions.length > 0) {
|
|
1660
|
-
const countQuery = table.query().where(conditions.join(" AND "));
|
|
1661
|
-
const allRecords = await countQuery.toArray();
|
|
1662
|
-
total = allRecords.length;
|
|
1663
|
-
} else {
|
|
1664
|
-
total = await table.countRows();
|
|
1665
|
-
}
|
|
1666
|
-
const page = args.page || 0;
|
|
1667
|
-
const perPage = args.perPage || 10;
|
|
1668
|
-
const offset = page * perPage;
|
|
1669
|
-
query.limit(perPage);
|
|
1670
|
-
if (offset > 0) {
|
|
1671
|
-
query.offset(offset);
|
|
1486
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1487
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
1488
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1489
|
+
const table = await this.client.openTable(storage.TABLE_SCORERS);
|
|
1490
|
+
const allRecords = await table.query().where(`\`traceId\` = '${traceId}' AND \`spanId\` = '${spanId}'`).toArray();
|
|
1491
|
+
const total = allRecords.length;
|
|
1492
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1493
|
+
let query = table.query().where(`\`traceId\` = '${traceId}' AND \`spanId\` = '${spanId}'`);
|
|
1494
|
+
if (perPageInput !== false) {
|
|
1495
|
+
query = query.limit(perPage);
|
|
1496
|
+
if (start > 0) query = query.offset(start);
|
|
1672
1497
|
}
|
|
1673
1498
|
const records = await query.toArray();
|
|
1674
|
-
const
|
|
1675
|
-
const processed = {
|
|
1676
|
-
...record,
|
|
1677
|
-
attributes: record.attributes ? JSON.parse(record.attributes) : {},
|
|
1678
|
-
status: record.status ? JSON.parse(record.status) : {},
|
|
1679
|
-
events: record.events ? JSON.parse(record.events) : [],
|
|
1680
|
-
links: record.links ? JSON.parse(record.links) : [],
|
|
1681
|
-
other: record.other ? JSON.parse(record.other) : {},
|
|
1682
|
-
startTime: new Date(record.startTime),
|
|
1683
|
-
endTime: new Date(record.endTime),
|
|
1684
|
-
createdAt: new Date(record.createdAt)
|
|
1685
|
-
};
|
|
1686
|
-
if (processed.parentSpanId === null || processed.parentSpanId === void 0) {
|
|
1687
|
-
processed.parentSpanId = "";
|
|
1688
|
-
} else {
|
|
1689
|
-
processed.parentSpanId = String(processed.parentSpanId);
|
|
1690
|
-
}
|
|
1691
|
-
return processed;
|
|
1692
|
-
});
|
|
1499
|
+
const scores = await Promise.all(records.map(async (record) => await this.transformScoreRow(record)));
|
|
1693
1500
|
return {
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1501
|
+
pagination: {
|
|
1502
|
+
page,
|
|
1503
|
+
perPage: perPageForResponse,
|
|
1504
|
+
total,
|
|
1505
|
+
hasMore: end < total
|
|
1506
|
+
},
|
|
1507
|
+
scores
|
|
1699
1508
|
};
|
|
1700
1509
|
} catch (error$1) {
|
|
1701
1510
|
throw new error.MastraError(
|
|
1702
1511
|
{
|
|
1703
|
-
id: "
|
|
1512
|
+
id: storage.createStorageErrorId("LANCE", "LIST_SCORES_BY_SPAN", "FAILED"),
|
|
1513
|
+
text: "Failed to get scores by traceId and spanId in LanceStorage",
|
|
1704
1514
|
domain: error.ErrorDomain.STORAGE,
|
|
1705
1515
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
1706
|
-
details: {
|
|
1516
|
+
details: { error: error$1?.message }
|
|
1707
1517
|
},
|
|
1708
1518
|
error$1
|
|
1709
1519
|
);
|
|
1710
1520
|
}
|
|
1711
1521
|
}
|
|
1712
|
-
async batchTraceInsert({ records }) {
|
|
1713
|
-
this.logger.debug("Batch inserting traces", { count: records.length });
|
|
1714
|
-
await this.operations.batchInsert({
|
|
1715
|
-
tableName: storage.TABLE_TRACES,
|
|
1716
|
-
records
|
|
1717
|
-
});
|
|
1718
|
-
}
|
|
1719
1522
|
};
|
|
1720
1523
|
function parseWorkflowRun(row) {
|
|
1721
1524
|
let parsedSnapshot = row.snapshot;
|
|
@@ -1741,9 +1544,26 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
|
|
|
1741
1544
|
super();
|
|
1742
1545
|
this.client = client;
|
|
1743
1546
|
}
|
|
1547
|
+
updateWorkflowResults({
|
|
1548
|
+
// workflowName,
|
|
1549
|
+
// runId,
|
|
1550
|
+
// stepId,
|
|
1551
|
+
// result,
|
|
1552
|
+
// requestContext,
|
|
1553
|
+
}) {
|
|
1554
|
+
throw new Error("Method not implemented.");
|
|
1555
|
+
}
|
|
1556
|
+
updateWorkflowState({
|
|
1557
|
+
// workflowName,
|
|
1558
|
+
// runId,
|
|
1559
|
+
// opts,
|
|
1560
|
+
}) {
|
|
1561
|
+
throw new Error("Method not implemented.");
|
|
1562
|
+
}
|
|
1744
1563
|
async persistWorkflowSnapshot({
|
|
1745
1564
|
workflowName,
|
|
1746
1565
|
runId,
|
|
1566
|
+
resourceId,
|
|
1747
1567
|
snapshot
|
|
1748
1568
|
}) {
|
|
1749
1569
|
try {
|
|
@@ -1757,10 +1577,13 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
|
|
|
1757
1577
|
} else {
|
|
1758
1578
|
createdAt = now;
|
|
1759
1579
|
}
|
|
1580
|
+
const { status, value, ...rest } = snapshot;
|
|
1760
1581
|
const record = {
|
|
1761
1582
|
workflow_name: workflowName,
|
|
1762
1583
|
run_id: runId,
|
|
1763
|
-
|
|
1584
|
+
resourceId,
|
|
1585
|
+
snapshot: JSON.stringify({ status, value, ...rest }),
|
|
1586
|
+
// this is to ensure status is always just before value, for when querying the db by status
|
|
1764
1587
|
createdAt,
|
|
1765
1588
|
updatedAt: now
|
|
1766
1589
|
};
|
|
@@ -1768,7 +1591,7 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
|
|
|
1768
1591
|
} catch (error$1) {
|
|
1769
1592
|
throw new error.MastraError(
|
|
1770
1593
|
{
|
|
1771
|
-
id: "
|
|
1594
|
+
id: storage.createStorageErrorId("LANCE", "PERSIST_WORKFLOW_SNAPSHOT", "FAILED"),
|
|
1772
1595
|
domain: error.ErrorDomain.STORAGE,
|
|
1773
1596
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
1774
1597
|
details: { workflowName, runId }
|
|
@@ -1789,7 +1612,7 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
|
|
|
1789
1612
|
} catch (error$1) {
|
|
1790
1613
|
throw new error.MastraError(
|
|
1791
1614
|
{
|
|
1792
|
-
id: "
|
|
1615
|
+
id: storage.createStorageErrorId("LANCE", "LOAD_WORKFLOW_SNAPSHOT", "FAILED"),
|
|
1793
1616
|
domain: error.ErrorDomain.STORAGE,
|
|
1794
1617
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
1795
1618
|
details: { workflowName, runId }
|
|
@@ -1813,7 +1636,7 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
|
|
|
1813
1636
|
} catch (error$1) {
|
|
1814
1637
|
throw new error.MastraError(
|
|
1815
1638
|
{
|
|
1816
|
-
id: "
|
|
1639
|
+
id: storage.createStorageErrorId("LANCE", "GET_WORKFLOW_RUN_BY_ID", "FAILED"),
|
|
1817
1640
|
domain: error.ErrorDomain.STORAGE,
|
|
1818
1641
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
1819
1642
|
details: { runId: args.runId, workflowName: args.workflowName ?? "" }
|
|
@@ -1822,7 +1645,7 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
|
|
|
1822
1645
|
);
|
|
1823
1646
|
}
|
|
1824
1647
|
}
|
|
1825
|
-
async
|
|
1648
|
+
async listWorkflowRuns(args) {
|
|
1826
1649
|
try {
|
|
1827
1650
|
const table = await this.client.openTable(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
1828
1651
|
let query = table.query();
|
|
@@ -1830,6 +1653,10 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
|
|
|
1830
1653
|
if (args?.workflowName) {
|
|
1831
1654
|
conditions.push(`workflow_name = '${args.workflowName.replace(/'/g, "''")}'`);
|
|
1832
1655
|
}
|
|
1656
|
+
if (args?.status) {
|
|
1657
|
+
const escapedStatus = args.status.replace(/\\/g, "\\\\").replace(/'/g, "''").replace(/%/g, "\\%").replace(/_/g, "\\_");
|
|
1658
|
+
conditions.push(`\`snapshot\` LIKE '%"status":"${escapedStatus}","value"%'`);
|
|
1659
|
+
}
|
|
1833
1660
|
if (args?.resourceId) {
|
|
1834
1661
|
conditions.push(`\`resourceId\` = '${args.resourceId}'`);
|
|
1835
1662
|
}
|
|
@@ -1846,11 +1673,22 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
|
|
|
1846
1673
|
} else {
|
|
1847
1674
|
total = await table.countRows();
|
|
1848
1675
|
}
|
|
1849
|
-
if (args?.
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1676
|
+
if (args?.perPage !== void 0 && args?.page !== void 0) {
|
|
1677
|
+
const normalizedPerPage = storage.normalizePerPage(args.perPage, Number.MAX_SAFE_INTEGER);
|
|
1678
|
+
if (args.page < 0 || !Number.isInteger(args.page)) {
|
|
1679
|
+
throw new error.MastraError(
|
|
1680
|
+
{
|
|
1681
|
+
id: storage.createStorageErrorId("LANCE", "LIST_WORKFLOW_RUNS", "INVALID_PAGINATION"),
|
|
1682
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1683
|
+
category: error.ErrorCategory.USER,
|
|
1684
|
+
details: { page: args.page, perPage: args.perPage }
|
|
1685
|
+
},
|
|
1686
|
+
new Error(`Invalid pagination parameters: page=${args.page}, perPage=${args.perPage}`)
|
|
1687
|
+
);
|
|
1688
|
+
}
|
|
1689
|
+
const offset = args.page * normalizedPerPage;
|
|
1690
|
+
query.limit(normalizedPerPage);
|
|
1691
|
+
query.offset(offset);
|
|
1854
1692
|
}
|
|
1855
1693
|
const records = await query.toArray();
|
|
1856
1694
|
return {
|
|
@@ -1860,10 +1698,10 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
|
|
|
1860
1698
|
} catch (error$1) {
|
|
1861
1699
|
throw new error.MastraError(
|
|
1862
1700
|
{
|
|
1863
|
-
id: "
|
|
1701
|
+
id: storage.createStorageErrorId("LANCE", "LIST_WORKFLOW_RUNS", "FAILED"),
|
|
1864
1702
|
domain: error.ErrorDomain.STORAGE,
|
|
1865
1703
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
1866
|
-
details: {
|
|
1704
|
+
details: { resourceId: args?.resourceId ?? "", workflowName: args?.workflowName ?? "" }
|
|
1867
1705
|
},
|
|
1868
1706
|
error$1
|
|
1869
1707
|
);
|
|
@@ -1877,48 +1715,54 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
1877
1715
|
lanceClient;
|
|
1878
1716
|
/**
|
|
1879
1717
|
* Creates a new instance of LanceStorage
|
|
1718
|
+
* @param id The unique identifier for this storage instance
|
|
1719
|
+
* @param name The name for this storage instance
|
|
1880
1720
|
* @param uri The URI to connect to LanceDB
|
|
1881
|
-
* @param
|
|
1721
|
+
* @param connectionOptions connection options for LanceDB
|
|
1722
|
+
* @param storageOptions storage options including disableInit
|
|
1882
1723
|
*
|
|
1883
1724
|
* Usage:
|
|
1884
1725
|
*
|
|
1885
1726
|
* Connect to a local database
|
|
1886
1727
|
* ```ts
|
|
1887
|
-
* const store = await LanceStorage.create('/path/to/db');
|
|
1728
|
+
* const store = await LanceStorage.create('my-storage-id', 'MyStorage', '/path/to/db');
|
|
1888
1729
|
* ```
|
|
1889
1730
|
*
|
|
1890
1731
|
* Connect to a LanceDB cloud database
|
|
1891
1732
|
* ```ts
|
|
1892
|
-
* const store = await LanceStorage.create('db://host:port');
|
|
1733
|
+
* const store = await LanceStorage.create('my-storage-id', 'MyStorage', 'db://host:port');
|
|
1893
1734
|
* ```
|
|
1894
1735
|
*
|
|
1895
1736
|
* Connect to a cloud database
|
|
1896
1737
|
* ```ts
|
|
1897
|
-
* const store = await LanceStorage.create('s3://bucket/db', { storageOptions: { timeout: '60s' } });
|
|
1738
|
+
* const store = await LanceStorage.create('my-storage-id', 'MyStorage', 's3://bucket/db', { storageOptions: { timeout: '60s' } });
|
|
1739
|
+
* ```
|
|
1740
|
+
*
|
|
1741
|
+
* Disable auto-init for runtime (after CI/CD has run migrations)
|
|
1742
|
+
* ```ts
|
|
1743
|
+
* const store = await LanceStorage.create('my-storage-id', 'MyStorage', '/path/to/db', undefined, { disableInit: true });
|
|
1898
1744
|
* ```
|
|
1899
1745
|
*/
|
|
1900
|
-
static async create(name, uri,
|
|
1901
|
-
const instance = new _LanceStorage(name);
|
|
1746
|
+
static async create(id, name, uri, connectionOptions, storageOptions) {
|
|
1747
|
+
const instance = new _LanceStorage(id, name, storageOptions?.disableInit);
|
|
1902
1748
|
try {
|
|
1903
|
-
instance.lanceClient = await lancedb.connect(uri,
|
|
1749
|
+
instance.lanceClient = await lancedb.connect(uri, connectionOptions);
|
|
1904
1750
|
const operations = new StoreOperationsLance({ client: instance.lanceClient });
|
|
1905
1751
|
instance.stores = {
|
|
1906
1752
|
operations: new StoreOperationsLance({ client: instance.lanceClient }),
|
|
1907
1753
|
workflows: new StoreWorkflowsLance({ client: instance.lanceClient }),
|
|
1908
|
-
traces: new StoreTracesLance({ client: instance.lanceClient, operations }),
|
|
1909
1754
|
scores: new StoreScoresLance({ client: instance.lanceClient }),
|
|
1910
|
-
memory: new StoreMemoryLance({ client: instance.lanceClient, operations })
|
|
1911
|
-
legacyEvals: new StoreLegacyEvalsLance({ client: instance.lanceClient })
|
|
1755
|
+
memory: new StoreMemoryLance({ client: instance.lanceClient, operations })
|
|
1912
1756
|
};
|
|
1913
1757
|
return instance;
|
|
1914
1758
|
} catch (e) {
|
|
1915
1759
|
throw new error.MastraError(
|
|
1916
1760
|
{
|
|
1917
|
-
id: "
|
|
1761
|
+
id: storage.createStorageErrorId("LANCE", "CONNECT", "FAILED"),
|
|
1918
1762
|
domain: error.ErrorDomain.STORAGE,
|
|
1919
1763
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
1920
1764
|
text: `Failed to connect to LanceDB: ${e.message || e}`,
|
|
1921
|
-
details: { uri, optionsProvided: !!
|
|
1765
|
+
details: { uri, optionsProvided: !!connectionOptions }
|
|
1922
1766
|
},
|
|
1923
1767
|
e
|
|
1924
1768
|
);
|
|
@@ -1928,15 +1772,13 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
1928
1772
|
* @internal
|
|
1929
1773
|
* Private constructor to enforce using the create factory method
|
|
1930
1774
|
*/
|
|
1931
|
-
constructor(name) {
|
|
1932
|
-
super({ name });
|
|
1775
|
+
constructor(id, name, disableInit) {
|
|
1776
|
+
super({ id, name, disableInit });
|
|
1933
1777
|
const operations = new StoreOperationsLance({ client: this.lanceClient });
|
|
1934
1778
|
this.stores = {
|
|
1935
1779
|
operations: new StoreOperationsLance({ client: this.lanceClient }),
|
|
1936
1780
|
workflows: new StoreWorkflowsLance({ client: this.lanceClient }),
|
|
1937
|
-
traces: new StoreTracesLance({ client: this.lanceClient, operations }),
|
|
1938
1781
|
scores: new StoreScoresLance({ client: this.lanceClient }),
|
|
1939
|
-
legacyEvals: new StoreLegacyEvalsLance({ client: this.lanceClient }),
|
|
1940
1782
|
memory: new StoreMemoryLance({ client: this.lanceClient, operations })
|
|
1941
1783
|
};
|
|
1942
1784
|
}
|
|
@@ -1971,9 +1813,6 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
1971
1813
|
async getThreadById({ threadId }) {
|
|
1972
1814
|
return this.stores.memory.getThreadById({ threadId });
|
|
1973
1815
|
}
|
|
1974
|
-
async getThreadsByResourceId({ resourceId }) {
|
|
1975
|
-
return this.stores.memory.getThreadsByResourceId({ resourceId });
|
|
1976
|
-
}
|
|
1977
1816
|
/**
|
|
1978
1817
|
* Saves a thread to the database. This function doesn't overwrite existing threads.
|
|
1979
1818
|
* @param thread - The thread to save
|
|
@@ -1998,7 +1837,8 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
1998
1837
|
resourceWorkingMemory: true,
|
|
1999
1838
|
hasColumn: true,
|
|
2000
1839
|
createTable: true,
|
|
2001
|
-
deleteMessages: false
|
|
1840
|
+
deleteMessages: false,
|
|
1841
|
+
listScoresBySpan: true
|
|
2002
1842
|
};
|
|
2003
1843
|
}
|
|
2004
1844
|
async getResourceById({ resourceId }) {
|
|
@@ -2062,54 +1902,44 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
2062
1902
|
});
|
|
2063
1903
|
return Array.from(allIndices).sort((a, b) => a - b).map((index) => records[index]);
|
|
2064
1904
|
}
|
|
2065
|
-
async
|
|
2066
|
-
|
|
2067
|
-
resourceId,
|
|
2068
|
-
selectBy,
|
|
2069
|
-
format,
|
|
2070
|
-
threadConfig
|
|
2071
|
-
}) {
|
|
2072
|
-
return this.stores.memory.getMessages({ threadId, resourceId, selectBy, format, threadConfig });
|
|
1905
|
+
async listMessagesById({ messageIds }) {
|
|
1906
|
+
return this.stores.memory.listMessagesById({ messageIds });
|
|
2073
1907
|
}
|
|
2074
1908
|
async saveMessages(args) {
|
|
2075
1909
|
return this.stores.memory.saveMessages(args);
|
|
2076
1910
|
}
|
|
2077
|
-
async getThreadsByResourceIdPaginated(args) {
|
|
2078
|
-
return this.stores.memory.getThreadsByResourceIdPaginated(args);
|
|
2079
|
-
}
|
|
2080
|
-
async getMessagesPaginated(args) {
|
|
2081
|
-
return this.stores.memory.getMessagesPaginated(args);
|
|
2082
|
-
}
|
|
2083
1911
|
async updateMessages(_args) {
|
|
2084
1912
|
return this.stores.memory.updateMessages(_args);
|
|
2085
1913
|
}
|
|
2086
|
-
async
|
|
2087
|
-
return this.stores.
|
|
2088
|
-
}
|
|
2089
|
-
async getTraces(args) {
|
|
2090
|
-
return this.stores.traces.getTraces(args);
|
|
2091
|
-
}
|
|
2092
|
-
async getTracesPaginated(args) {
|
|
2093
|
-
return this.stores.traces.getTracesPaginated(args);
|
|
2094
|
-
}
|
|
2095
|
-
async getEvalsByAgentName(agentName, type) {
|
|
2096
|
-
return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
|
|
2097
|
-
}
|
|
2098
|
-
async getEvals(options) {
|
|
2099
|
-
return this.stores.legacyEvals.getEvals(options);
|
|
2100
|
-
}
|
|
2101
|
-
async getWorkflowRuns(args) {
|
|
2102
|
-
return this.stores.workflows.getWorkflowRuns(args);
|
|
1914
|
+
async listWorkflowRuns(args) {
|
|
1915
|
+
return this.stores.workflows.listWorkflowRuns(args);
|
|
2103
1916
|
}
|
|
2104
1917
|
async getWorkflowRunById(args) {
|
|
2105
1918
|
return this.stores.workflows.getWorkflowRunById(args);
|
|
2106
1919
|
}
|
|
1920
|
+
async updateWorkflowResults({
|
|
1921
|
+
workflowName,
|
|
1922
|
+
runId,
|
|
1923
|
+
stepId,
|
|
1924
|
+
result,
|
|
1925
|
+
requestContext
|
|
1926
|
+
}) {
|
|
1927
|
+
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, requestContext });
|
|
1928
|
+
}
|
|
1929
|
+
async updateWorkflowState({
|
|
1930
|
+
workflowName,
|
|
1931
|
+
runId,
|
|
1932
|
+
opts
|
|
1933
|
+
}) {
|
|
1934
|
+
return this.stores.workflows.updateWorkflowState({ workflowName, runId, opts });
|
|
1935
|
+
}
|
|
2107
1936
|
async persistWorkflowSnapshot({
|
|
2108
1937
|
workflowName,
|
|
2109
1938
|
runId,
|
|
1939
|
+
resourceId,
|
|
2110
1940
|
snapshot
|
|
2111
1941
|
}) {
|
|
2112
|
-
return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, snapshot });
|
|
1942
|
+
return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot });
|
|
2113
1943
|
}
|
|
2114
1944
|
async loadWorkflowSnapshot({
|
|
2115
1945
|
workflowName,
|
|
@@ -2120,27 +1950,37 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
2120
1950
|
async getScoreById({ id: _id }) {
|
|
2121
1951
|
return this.stores.scores.getScoreById({ id: _id });
|
|
2122
1952
|
}
|
|
2123
|
-
async
|
|
1953
|
+
async listScoresByScorerId({
|
|
2124
1954
|
scorerId,
|
|
1955
|
+
source,
|
|
1956
|
+
entityId,
|
|
1957
|
+
entityType,
|
|
2125
1958
|
pagination
|
|
2126
1959
|
}) {
|
|
2127
|
-
return this.stores.scores.
|
|
1960
|
+
return this.stores.scores.listScoresByScorerId({ scorerId, source, pagination, entityId, entityType });
|
|
2128
1961
|
}
|
|
2129
|
-
async saveScore(
|
|
2130
|
-
return this.stores.scores.saveScore(
|
|
1962
|
+
async saveScore(score) {
|
|
1963
|
+
return this.stores.scores.saveScore(score);
|
|
2131
1964
|
}
|
|
2132
|
-
async
|
|
1965
|
+
async listScoresByRunId({
|
|
2133
1966
|
runId,
|
|
2134
1967
|
pagination
|
|
2135
1968
|
}) {
|
|
2136
|
-
return this.stores.scores.
|
|
1969
|
+
return this.stores.scores.listScoresByRunId({ runId, pagination });
|
|
2137
1970
|
}
|
|
2138
|
-
async
|
|
1971
|
+
async listScoresByEntityId({
|
|
2139
1972
|
entityId,
|
|
2140
1973
|
entityType,
|
|
2141
1974
|
pagination
|
|
2142
1975
|
}) {
|
|
2143
|
-
return this.stores.scores.
|
|
1976
|
+
return this.stores.scores.listScoresByEntityId({ entityId, entityType, pagination });
|
|
1977
|
+
}
|
|
1978
|
+
async listScoresBySpan({
|
|
1979
|
+
traceId,
|
|
1980
|
+
spanId,
|
|
1981
|
+
pagination
|
|
1982
|
+
}) {
|
|
1983
|
+
return this.stores.scores.listScoresBySpan({ traceId, spanId, pagination });
|
|
2144
1984
|
}
|
|
2145
1985
|
};
|
|
2146
1986
|
var LanceFilterTranslator = class extends filter.BaseFilterTranslator {
|
|
@@ -2489,14 +2329,14 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2489
2329
|
* ```
|
|
2490
2330
|
*/
|
|
2491
2331
|
static async create(uri, options) {
|
|
2492
|
-
const instance = new _LanceVectorStore();
|
|
2332
|
+
const instance = new _LanceVectorStore(options?.id || crypto.randomUUID());
|
|
2493
2333
|
try {
|
|
2494
2334
|
instance.lanceClient = await lancedb.connect(uri, options);
|
|
2495
2335
|
return instance;
|
|
2496
2336
|
} catch (e) {
|
|
2497
2337
|
throw new error.MastraError(
|
|
2498
2338
|
{
|
|
2499
|
-
id: "
|
|
2339
|
+
id: storage.createVectorErrorId("LANCE", "CONNECT", "FAILED"),
|
|
2500
2340
|
domain: error.ErrorDomain.STORAGE,
|
|
2501
2341
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
2502
2342
|
details: { uri }
|
|
@@ -2509,8 +2349,8 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2509
2349
|
* @internal
|
|
2510
2350
|
* Private constructor to enforce using the create factory method
|
|
2511
2351
|
*/
|
|
2512
|
-
constructor() {
|
|
2513
|
-
super();
|
|
2352
|
+
constructor(id) {
|
|
2353
|
+
super({ id });
|
|
2514
2354
|
}
|
|
2515
2355
|
close() {
|
|
2516
2356
|
if (this.lanceClient) {
|
|
@@ -2539,7 +2379,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2539
2379
|
} catch (error$1) {
|
|
2540
2380
|
throw new error.MastraError(
|
|
2541
2381
|
{
|
|
2542
|
-
id: "
|
|
2382
|
+
id: storage.createVectorErrorId("LANCE", "QUERY", "INVALID_ARGS"),
|
|
2543
2383
|
domain: error.ErrorDomain.STORAGE,
|
|
2544
2384
|
category: error.ErrorCategory.USER,
|
|
2545
2385
|
text: "LanceDB client not initialized. Use LanceVectorStore.create() to create an instance",
|
|
@@ -2587,7 +2427,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2587
2427
|
} catch (error$1) {
|
|
2588
2428
|
throw new error.MastraError(
|
|
2589
2429
|
{
|
|
2590
|
-
id: "
|
|
2430
|
+
id: storage.createVectorErrorId("LANCE", "QUERY", "FAILED"),
|
|
2591
2431
|
domain: error.ErrorDomain.STORAGE,
|
|
2592
2432
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
2593
2433
|
details: { tableName, includeVector, columnsCount: columns?.length, includeAllColumns }
|
|
@@ -2639,7 +2479,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2639
2479
|
} catch (error$1) {
|
|
2640
2480
|
throw new error.MastraError(
|
|
2641
2481
|
{
|
|
2642
|
-
id: "
|
|
2482
|
+
id: storage.createVectorErrorId("LANCE", "UPSERT", "INVALID_ARGS"),
|
|
2643
2483
|
domain: error.ErrorDomain.STORAGE,
|
|
2644
2484
|
category: error.ErrorCategory.USER,
|
|
2645
2485
|
text: "LanceDB client not initialized. Use LanceVectorStore.create() to create an instance",
|
|
@@ -2675,7 +2515,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2675
2515
|
} catch (error$1) {
|
|
2676
2516
|
throw new error.MastraError(
|
|
2677
2517
|
{
|
|
2678
|
-
id: "
|
|
2518
|
+
id: storage.createVectorErrorId("LANCE", "UPSERT", "FAILED"),
|
|
2679
2519
|
domain: error.ErrorDomain.STORAGE,
|
|
2680
2520
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
2681
2521
|
details: { tableName, vectorCount: vectors.length, metadataCount: metadata.length, idsCount: ids.length }
|
|
@@ -2702,7 +2542,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2702
2542
|
async createTable(tableName, data, options) {
|
|
2703
2543
|
if (!this.lanceClient) {
|
|
2704
2544
|
throw new error.MastraError({
|
|
2705
|
-
id: "
|
|
2545
|
+
id: storage.createVectorErrorId("LANCE", "CREATE_TABLE", "INVALID_ARGS"),
|
|
2706
2546
|
domain: error.ErrorDomain.STORAGE,
|
|
2707
2547
|
category: error.ErrorCategory.USER,
|
|
2708
2548
|
text: "LanceDB client not initialized. Use LanceVectorStore.create() to create an instance",
|
|
@@ -2717,7 +2557,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2717
2557
|
} catch (error$1) {
|
|
2718
2558
|
throw new error.MastraError(
|
|
2719
2559
|
{
|
|
2720
|
-
id: "
|
|
2560
|
+
id: storage.createVectorErrorId("LANCE", "CREATE_TABLE", "FAILED"),
|
|
2721
2561
|
domain: error.ErrorDomain.STORAGE,
|
|
2722
2562
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
2723
2563
|
details: { tableName }
|
|
@@ -2729,7 +2569,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2729
2569
|
async listTables() {
|
|
2730
2570
|
if (!this.lanceClient) {
|
|
2731
2571
|
throw new error.MastraError({
|
|
2732
|
-
id: "
|
|
2572
|
+
id: storage.createVectorErrorId("LANCE", "LIST_TABLES", "INVALID_ARGS"),
|
|
2733
2573
|
domain: error.ErrorDomain.STORAGE,
|
|
2734
2574
|
category: error.ErrorCategory.USER,
|
|
2735
2575
|
text: "LanceDB client not initialized. Use LanceVectorStore.create() to create an instance",
|
|
@@ -2741,7 +2581,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2741
2581
|
} catch (error$1) {
|
|
2742
2582
|
throw new error.MastraError(
|
|
2743
2583
|
{
|
|
2744
|
-
id: "
|
|
2584
|
+
id: storage.createVectorErrorId("LANCE", "LIST_TABLES", "FAILED"),
|
|
2745
2585
|
domain: error.ErrorDomain.STORAGE,
|
|
2746
2586
|
category: error.ErrorCategory.THIRD_PARTY
|
|
2747
2587
|
},
|
|
@@ -2752,7 +2592,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2752
2592
|
async getTableSchema(tableName) {
|
|
2753
2593
|
if (!this.lanceClient) {
|
|
2754
2594
|
throw new error.MastraError({
|
|
2755
|
-
id: "
|
|
2595
|
+
id: storage.createVectorErrorId("LANCE", "GET_TABLE_SCHEMA", "INVALID_ARGS"),
|
|
2756
2596
|
domain: error.ErrorDomain.STORAGE,
|
|
2757
2597
|
category: error.ErrorCategory.USER,
|
|
2758
2598
|
text: "LanceDB client not initialized. Use LanceVectorStore.create() to create an instance",
|
|
@@ -2765,7 +2605,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2765
2605
|
} catch (error$1) {
|
|
2766
2606
|
throw new error.MastraError(
|
|
2767
2607
|
{
|
|
2768
|
-
id: "
|
|
2608
|
+
id: storage.createVectorErrorId("LANCE", "GET_TABLE_SCHEMA", "FAILED"),
|
|
2769
2609
|
domain: error.ErrorDomain.STORAGE,
|
|
2770
2610
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
2771
2611
|
details: { tableName }
|
|
@@ -2800,7 +2640,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2800
2640
|
} catch (err) {
|
|
2801
2641
|
throw new error.MastraError(
|
|
2802
2642
|
{
|
|
2803
|
-
id: "
|
|
2643
|
+
id: storage.createVectorErrorId("LANCE", "CREATE_INDEX", "INVALID_ARGS"),
|
|
2804
2644
|
domain: error.ErrorDomain.STORAGE,
|
|
2805
2645
|
category: error.ErrorCategory.USER,
|
|
2806
2646
|
details: { tableName: tableName || "", indexName, dimension, metric }
|
|
@@ -2845,7 +2685,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2845
2685
|
} catch (error$1) {
|
|
2846
2686
|
throw new error.MastraError(
|
|
2847
2687
|
{
|
|
2848
|
-
id: "
|
|
2688
|
+
id: storage.createVectorErrorId("LANCE", "CREATE_INDEX", "FAILED"),
|
|
2849
2689
|
domain: error.ErrorDomain.STORAGE,
|
|
2850
2690
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
2851
2691
|
details: { tableName: tableName || "", indexName, dimension }
|
|
@@ -2857,7 +2697,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2857
2697
|
async listIndexes() {
|
|
2858
2698
|
if (!this.lanceClient) {
|
|
2859
2699
|
throw new error.MastraError({
|
|
2860
|
-
id: "
|
|
2700
|
+
id: storage.createVectorErrorId("LANCE", "LIST_INDEXES", "INVALID_ARGS"),
|
|
2861
2701
|
domain: error.ErrorDomain.STORAGE,
|
|
2862
2702
|
category: error.ErrorCategory.USER,
|
|
2863
2703
|
text: "LanceDB client not initialized. Use LanceVectorStore.create() to create an instance",
|
|
@@ -2876,7 +2716,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2876
2716
|
} catch (error$1) {
|
|
2877
2717
|
throw new error.MastraError(
|
|
2878
2718
|
{
|
|
2879
|
-
id: "
|
|
2719
|
+
id: storage.createVectorErrorId("LANCE", "LIST_INDEXES", "FAILED"),
|
|
2880
2720
|
domain: error.ErrorDomain.STORAGE,
|
|
2881
2721
|
category: error.ErrorCategory.THIRD_PARTY
|
|
2882
2722
|
},
|
|
@@ -2895,7 +2735,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2895
2735
|
} catch (err) {
|
|
2896
2736
|
throw new error.MastraError(
|
|
2897
2737
|
{
|
|
2898
|
-
id: "
|
|
2738
|
+
id: storage.createVectorErrorId("LANCE", "DESCRIBE_INDEX", "INVALID_ARGS"),
|
|
2899
2739
|
domain: error.ErrorDomain.STORAGE,
|
|
2900
2740
|
category: error.ErrorCategory.USER,
|
|
2901
2741
|
details: { indexName }
|
|
@@ -2930,7 +2770,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2930
2770
|
} catch (error$1) {
|
|
2931
2771
|
throw new error.MastraError(
|
|
2932
2772
|
{
|
|
2933
|
-
id: "
|
|
2773
|
+
id: storage.createVectorErrorId("LANCE", "DESCRIBE_INDEX", "FAILED"),
|
|
2934
2774
|
domain: error.ErrorDomain.STORAGE,
|
|
2935
2775
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
2936
2776
|
details: { indexName }
|
|
@@ -2950,7 +2790,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2950
2790
|
} catch (err) {
|
|
2951
2791
|
throw new error.MastraError(
|
|
2952
2792
|
{
|
|
2953
|
-
id: "
|
|
2793
|
+
id: storage.createVectorErrorId("LANCE", "DELETE_INDEX", "INVALID_ARGS"),
|
|
2954
2794
|
domain: error.ErrorDomain.STORAGE,
|
|
2955
2795
|
category: error.ErrorCategory.USER,
|
|
2956
2796
|
details: { indexName }
|
|
@@ -2973,7 +2813,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2973
2813
|
} catch (error$1) {
|
|
2974
2814
|
throw new error.MastraError(
|
|
2975
2815
|
{
|
|
2976
|
-
id: "
|
|
2816
|
+
id: storage.createVectorErrorId("LANCE", "DELETE_INDEX", "FAILED"),
|
|
2977
2817
|
domain: error.ErrorDomain.STORAGE,
|
|
2978
2818
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
2979
2819
|
details: { indexName }
|
|
@@ -2988,7 +2828,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2988
2828
|
async deleteAllTables() {
|
|
2989
2829
|
if (!this.lanceClient) {
|
|
2990
2830
|
throw new error.MastraError({
|
|
2991
|
-
id: "
|
|
2831
|
+
id: storage.createVectorErrorId("LANCE", "DELETE_ALL_TABLES", "INVALID_ARGS"),
|
|
2992
2832
|
domain: error.ErrorDomain.STORAGE,
|
|
2993
2833
|
category: error.ErrorCategory.USER,
|
|
2994
2834
|
details: { methodName: "deleteAllTables" },
|
|
@@ -3000,7 +2840,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
3000
2840
|
} catch (error$1) {
|
|
3001
2841
|
throw new error.MastraError(
|
|
3002
2842
|
{
|
|
3003
|
-
id: "
|
|
2843
|
+
id: storage.createVectorErrorId("LANCE", "DELETE_ALL_TABLES", "FAILED"),
|
|
3004
2844
|
domain: error.ErrorDomain.STORAGE,
|
|
3005
2845
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
3006
2846
|
details: { methodName: "deleteAllTables" }
|
|
@@ -3012,7 +2852,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
3012
2852
|
async deleteTable(tableName) {
|
|
3013
2853
|
if (!this.lanceClient) {
|
|
3014
2854
|
throw new error.MastraError({
|
|
3015
|
-
id: "
|
|
2855
|
+
id: storage.createVectorErrorId("LANCE", "DELETE_TABLE", "INVALID_ARGS"),
|
|
3016
2856
|
domain: error.ErrorDomain.STORAGE,
|
|
3017
2857
|
category: error.ErrorCategory.USER,
|
|
3018
2858
|
details: { tableName },
|
|
@@ -3024,7 +2864,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
3024
2864
|
} catch (error$1) {
|
|
3025
2865
|
throw new error.MastraError(
|
|
3026
2866
|
{
|
|
3027
|
-
id: "
|
|
2867
|
+
id: storage.createVectorErrorId("LANCE", "DELETE_TABLE", "FAILED"),
|
|
3028
2868
|
domain: error.ErrorDomain.STORAGE,
|
|
3029
2869
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
3030
2870
|
details: { tableName }
|
|
@@ -3033,7 +2873,44 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
3033
2873
|
);
|
|
3034
2874
|
}
|
|
3035
2875
|
}
|
|
3036
|
-
async updateVector(
|
|
2876
|
+
async updateVector(params) {
|
|
2877
|
+
const { indexName, update } = params;
|
|
2878
|
+
if ("id" in params && "filter" in params && params.id && params.filter) {
|
|
2879
|
+
throw new error.MastraError({
|
|
2880
|
+
id: storage.createVectorErrorId("LANCE", "UPDATE_VECTOR", "MUTUALLY_EXCLUSIVE"),
|
|
2881
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2882
|
+
category: error.ErrorCategory.USER,
|
|
2883
|
+
text: "id and filter are mutually exclusive",
|
|
2884
|
+
details: { indexName }
|
|
2885
|
+
});
|
|
2886
|
+
}
|
|
2887
|
+
if (!("id" in params || "filter" in params) || !params.id && !params.filter) {
|
|
2888
|
+
throw new error.MastraError({
|
|
2889
|
+
id: storage.createVectorErrorId("LANCE", "UPDATE_VECTOR", "NO_TARGET"),
|
|
2890
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2891
|
+
category: error.ErrorCategory.USER,
|
|
2892
|
+
text: "Either id or filter must be provided",
|
|
2893
|
+
details: { indexName }
|
|
2894
|
+
});
|
|
2895
|
+
}
|
|
2896
|
+
if ("filter" in params && params.filter && Object.keys(params.filter).length === 0) {
|
|
2897
|
+
throw new error.MastraError({
|
|
2898
|
+
id: storage.createVectorErrorId("LANCE", "UPDATE_VECTOR", "EMPTY_FILTER"),
|
|
2899
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2900
|
+
category: error.ErrorCategory.USER,
|
|
2901
|
+
text: "Cannot update with empty filter",
|
|
2902
|
+
details: { indexName }
|
|
2903
|
+
});
|
|
2904
|
+
}
|
|
2905
|
+
if (!update.vector && !update.metadata) {
|
|
2906
|
+
throw new error.MastraError({
|
|
2907
|
+
id: storage.createVectorErrorId("LANCE", "UPDATE_VECTOR", "NO_PAYLOAD"),
|
|
2908
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2909
|
+
category: error.ErrorCategory.USER,
|
|
2910
|
+
text: "No updates provided",
|
|
2911
|
+
details: { indexName }
|
|
2912
|
+
});
|
|
2913
|
+
}
|
|
3037
2914
|
try {
|
|
3038
2915
|
if (!this.lanceClient) {
|
|
3039
2916
|
throw new Error("LanceDB client not initialized. Use LanceVectorStore.create() to create an instance");
|
|
@@ -3041,21 +2918,6 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
3041
2918
|
if (!indexName) {
|
|
3042
2919
|
throw new Error("indexName is required");
|
|
3043
2920
|
}
|
|
3044
|
-
if (!id) {
|
|
3045
|
-
throw new Error("id is required");
|
|
3046
|
-
}
|
|
3047
|
-
} catch (err) {
|
|
3048
|
-
throw new error.MastraError(
|
|
3049
|
-
{
|
|
3050
|
-
id: "STORAGE_LANCE_VECTOR_UPDATE_VECTOR_FAILED_INVALID_ARGS",
|
|
3051
|
-
domain: error.ErrorDomain.STORAGE,
|
|
3052
|
-
category: error.ErrorCategory.USER,
|
|
3053
|
-
details: { indexName, id }
|
|
3054
|
-
},
|
|
3055
|
-
err
|
|
3056
|
-
);
|
|
3057
|
-
}
|
|
3058
|
-
try {
|
|
3059
2921
|
const tables = await this.lanceClient.tableNames();
|
|
3060
2922
|
for (const tableName of tables) {
|
|
3061
2923
|
this.logger.debug("Checking table:" + tableName);
|
|
@@ -3065,39 +2927,66 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
3065
2927
|
const hasColumn = schema.fields.some((field) => field.name === indexName);
|
|
3066
2928
|
if (hasColumn) {
|
|
3067
2929
|
this.logger.debug(`Found column ${indexName} in table ${tableName}`);
|
|
3068
|
-
|
|
3069
|
-
if (
|
|
3070
|
-
|
|
2930
|
+
let whereClause;
|
|
2931
|
+
if ("id" in params && params.id) {
|
|
2932
|
+
whereClause = `id = '${params.id}'`;
|
|
2933
|
+
} else if ("filter" in params && params.filter) {
|
|
2934
|
+
const translator = new LanceFilterTranslator();
|
|
2935
|
+
const processFilterKeys = (filter) => {
|
|
2936
|
+
const processedFilter = {};
|
|
2937
|
+
Object.entries(filter).forEach(([key, value]) => {
|
|
2938
|
+
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
2939
|
+
Object.entries(value).forEach(([nestedKey, nestedValue]) => {
|
|
2940
|
+
processedFilter[`metadata_${key}_${nestedKey}`] = nestedValue;
|
|
2941
|
+
});
|
|
2942
|
+
} else {
|
|
2943
|
+
processedFilter[`metadata_${key}`] = value;
|
|
2944
|
+
}
|
|
2945
|
+
});
|
|
2946
|
+
return processedFilter;
|
|
2947
|
+
};
|
|
2948
|
+
const prefixedFilter = processFilterKeys(params.filter);
|
|
2949
|
+
whereClause = translator.translate(prefixedFilter) || "";
|
|
2950
|
+
if (!whereClause) {
|
|
2951
|
+
throw new Error("Failed to translate filter to SQL");
|
|
2952
|
+
}
|
|
2953
|
+
} else {
|
|
2954
|
+
throw new Error("Either id or filter must be provided");
|
|
3071
2955
|
}
|
|
3072
|
-
const
|
|
3073
|
-
|
|
3074
|
-
|
|
3075
|
-
|
|
3076
|
-
|
|
3077
|
-
|
|
3078
|
-
|
|
3079
|
-
|
|
3080
|
-
|
|
3081
|
-
|
|
3082
|
-
|
|
2956
|
+
const existingRecords = await table.query().where(whereClause).select(schema.fields.map((field) => field.name)).toArray();
|
|
2957
|
+
if (existingRecords.length === 0) {
|
|
2958
|
+
this.logger.info(`No records found matching criteria in table ${tableName}`);
|
|
2959
|
+
return;
|
|
2960
|
+
}
|
|
2961
|
+
const updatedRecords = existingRecords.map((record) => {
|
|
2962
|
+
const rowData = {};
|
|
2963
|
+
Object.entries(record).forEach(([key, value]) => {
|
|
2964
|
+
if (key !== "_distance") {
|
|
2965
|
+
if (key === indexName) {
|
|
2966
|
+
if (update.vector) {
|
|
2967
|
+
rowData[key] = update.vector;
|
|
3083
2968
|
} else {
|
|
3084
|
-
|
|
2969
|
+
if (Array.isArray(value)) {
|
|
2970
|
+
rowData[key] = [...value];
|
|
2971
|
+
} else if (typeof value === "object" && value !== null) {
|
|
2972
|
+
rowData[key] = Array.from(value);
|
|
2973
|
+
} else {
|
|
2974
|
+
rowData[key] = value;
|
|
2975
|
+
}
|
|
3085
2976
|
}
|
|
2977
|
+
} else {
|
|
2978
|
+
rowData[key] = value;
|
|
3086
2979
|
}
|
|
3087
|
-
} else {
|
|
3088
|
-
rowData[key] = value;
|
|
3089
2980
|
}
|
|
2981
|
+
});
|
|
2982
|
+
if (update.metadata) {
|
|
2983
|
+
Object.entries(update.metadata).forEach(([key, value]) => {
|
|
2984
|
+
rowData[`metadata_${key}`] = value;
|
|
2985
|
+
});
|
|
3090
2986
|
}
|
|
2987
|
+
return rowData;
|
|
3091
2988
|
});
|
|
3092
|
-
|
|
3093
|
-
rowData[indexName] = update.vector;
|
|
3094
|
-
}
|
|
3095
|
-
if (update.metadata) {
|
|
3096
|
-
Object.entries(update.metadata).forEach(([key, value]) => {
|
|
3097
|
-
rowData[`metadata_${key}`] = value;
|
|
3098
|
-
});
|
|
3099
|
-
}
|
|
3100
|
-
await table.add([rowData], { mode: "overwrite" });
|
|
2989
|
+
await table.add(updatedRecords, { mode: "overwrite" });
|
|
3101
2990
|
return;
|
|
3102
2991
|
}
|
|
3103
2992
|
} catch (err) {
|
|
@@ -3107,12 +2996,19 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
3107
2996
|
}
|
|
3108
2997
|
throw new Error(`No table found with column/index '${indexName}'`);
|
|
3109
2998
|
} catch (error$1) {
|
|
2999
|
+
if (error$1 instanceof error.MastraError) throw error$1;
|
|
3110
3000
|
throw new error.MastraError(
|
|
3111
3001
|
{
|
|
3112
|
-
id: "
|
|
3002
|
+
id: storage.createVectorErrorId("LANCE", "UPDATE_VECTOR", "FAILED"),
|
|
3113
3003
|
domain: error.ErrorDomain.STORAGE,
|
|
3114
3004
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
3115
|
-
details: {
|
|
3005
|
+
details: {
|
|
3006
|
+
indexName,
|
|
3007
|
+
..."id" in params && params.id && { id: params.id },
|
|
3008
|
+
..."filter" in params && params.filter && { filter: JSON.stringify(params.filter) },
|
|
3009
|
+
hasVector: !!update.vector,
|
|
3010
|
+
hasMetadata: !!update.metadata
|
|
3011
|
+
}
|
|
3116
3012
|
},
|
|
3117
3013
|
error$1
|
|
3118
3014
|
);
|
|
@@ -3132,10 +3028,13 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
3132
3028
|
} catch (err) {
|
|
3133
3029
|
throw new error.MastraError(
|
|
3134
3030
|
{
|
|
3135
|
-
id: "
|
|
3031
|
+
id: storage.createVectorErrorId("LANCE", "DELETE_VECTOR", "INVALID_ARGS"),
|
|
3136
3032
|
domain: error.ErrorDomain.STORAGE,
|
|
3137
3033
|
category: error.ErrorCategory.USER,
|
|
3138
|
-
details: {
|
|
3034
|
+
details: {
|
|
3035
|
+
indexName,
|
|
3036
|
+
...id && { id }
|
|
3037
|
+
}
|
|
3139
3038
|
},
|
|
3140
3039
|
err
|
|
3141
3040
|
);
|
|
@@ -3162,10 +3061,13 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
3162
3061
|
} catch (error$1) {
|
|
3163
3062
|
throw new error.MastraError(
|
|
3164
3063
|
{
|
|
3165
|
-
id: "
|
|
3064
|
+
id: storage.createVectorErrorId("LANCE", "DELETE_VECTOR", "FAILED"),
|
|
3166
3065
|
domain: error.ErrorDomain.STORAGE,
|
|
3167
3066
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
3168
|
-
details: {
|
|
3067
|
+
details: {
|
|
3068
|
+
indexName,
|
|
3069
|
+
...id && { id }
|
|
3070
|
+
}
|
|
3169
3071
|
},
|
|
3170
3072
|
error$1
|
|
3171
3073
|
);
|
|
@@ -3196,6 +3098,109 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
3196
3098
|
});
|
|
3197
3099
|
return result;
|
|
3198
3100
|
}
|
|
3101
|
+
async deleteVectors({ indexName, filter, ids }) {
|
|
3102
|
+
if (ids && filter) {
|
|
3103
|
+
throw new error.MastraError({
|
|
3104
|
+
id: storage.createVectorErrorId("LANCE", "DELETE_VECTORS", "MUTUALLY_EXCLUSIVE"),
|
|
3105
|
+
domain: error.ErrorDomain.STORAGE,
|
|
3106
|
+
category: error.ErrorCategory.USER,
|
|
3107
|
+
text: "ids and filter are mutually exclusive",
|
|
3108
|
+
details: { indexName }
|
|
3109
|
+
});
|
|
3110
|
+
}
|
|
3111
|
+
if (!ids && !filter) {
|
|
3112
|
+
throw new error.MastraError({
|
|
3113
|
+
id: storage.createVectorErrorId("LANCE", "DELETE_VECTORS", "NO_TARGET"),
|
|
3114
|
+
domain: error.ErrorDomain.STORAGE,
|
|
3115
|
+
category: error.ErrorCategory.USER,
|
|
3116
|
+
text: "Either filter or ids must be provided",
|
|
3117
|
+
details: { indexName }
|
|
3118
|
+
});
|
|
3119
|
+
}
|
|
3120
|
+
if (ids && ids.length === 0) {
|
|
3121
|
+
throw new error.MastraError({
|
|
3122
|
+
id: storage.createVectorErrorId("LANCE", "DELETE_VECTORS", "EMPTY_IDS"),
|
|
3123
|
+
domain: error.ErrorDomain.STORAGE,
|
|
3124
|
+
category: error.ErrorCategory.USER,
|
|
3125
|
+
text: "Cannot delete with empty ids array",
|
|
3126
|
+
details: { indexName }
|
|
3127
|
+
});
|
|
3128
|
+
}
|
|
3129
|
+
if (filter && Object.keys(filter).length === 0) {
|
|
3130
|
+
throw new error.MastraError({
|
|
3131
|
+
id: storage.createVectorErrorId("LANCE", "DELETE_VECTORS", "EMPTY_FILTER"),
|
|
3132
|
+
domain: error.ErrorDomain.STORAGE,
|
|
3133
|
+
category: error.ErrorCategory.USER,
|
|
3134
|
+
text: "Cannot delete with empty filter",
|
|
3135
|
+
details: { indexName }
|
|
3136
|
+
});
|
|
3137
|
+
}
|
|
3138
|
+
try {
|
|
3139
|
+
if (!this.lanceClient) {
|
|
3140
|
+
throw new Error("LanceDB client not initialized. Use LanceVectorStore.create() to create an instance");
|
|
3141
|
+
}
|
|
3142
|
+
if (!indexName) {
|
|
3143
|
+
throw new Error("indexName is required");
|
|
3144
|
+
}
|
|
3145
|
+
const tables = await this.lanceClient.tableNames();
|
|
3146
|
+
for (const tableName of tables) {
|
|
3147
|
+
this.logger.debug("Checking table:" + tableName);
|
|
3148
|
+
const table = await this.lanceClient.openTable(tableName);
|
|
3149
|
+
try {
|
|
3150
|
+
const schema = await table.schema();
|
|
3151
|
+
const hasColumn = schema.fields.some((field) => field.name === indexName);
|
|
3152
|
+
if (hasColumn) {
|
|
3153
|
+
this.logger.debug(`Found column ${indexName} in table ${tableName}`);
|
|
3154
|
+
if (ids) {
|
|
3155
|
+
const idsConditions = ids.map((id) => `id = '${id}'`).join(" OR ");
|
|
3156
|
+
await table.delete(idsConditions);
|
|
3157
|
+
} else if (filter) {
|
|
3158
|
+
const translator = new LanceFilterTranslator();
|
|
3159
|
+
const processFilterKeys = (filter2) => {
|
|
3160
|
+
const processedFilter = {};
|
|
3161
|
+
Object.entries(filter2).forEach(([key, value]) => {
|
|
3162
|
+
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
3163
|
+
Object.entries(value).forEach(([nestedKey, nestedValue]) => {
|
|
3164
|
+
processedFilter[`metadata_${key}_${nestedKey}`] = nestedValue;
|
|
3165
|
+
});
|
|
3166
|
+
} else {
|
|
3167
|
+
processedFilter[`metadata_${key}`] = value;
|
|
3168
|
+
}
|
|
3169
|
+
});
|
|
3170
|
+
return processedFilter;
|
|
3171
|
+
};
|
|
3172
|
+
const prefixedFilter = processFilterKeys(filter);
|
|
3173
|
+
const whereClause = translator.translate(prefixedFilter);
|
|
3174
|
+
if (!whereClause) {
|
|
3175
|
+
throw new Error("Failed to translate filter to SQL");
|
|
3176
|
+
}
|
|
3177
|
+
await table.delete(whereClause);
|
|
3178
|
+
}
|
|
3179
|
+
return;
|
|
3180
|
+
}
|
|
3181
|
+
} catch (err) {
|
|
3182
|
+
this.logger.error(`Error checking schema for table ${tableName}:` + err);
|
|
3183
|
+
continue;
|
|
3184
|
+
}
|
|
3185
|
+
}
|
|
3186
|
+
throw new Error(`No table found with column/index '${indexName}'`);
|
|
3187
|
+
} catch (error$1) {
|
|
3188
|
+
if (error$1 instanceof error.MastraError) throw error$1;
|
|
3189
|
+
throw new error.MastraError(
|
|
3190
|
+
{
|
|
3191
|
+
id: storage.createVectorErrorId("LANCE", "DELETE_VECTORS", "FAILED"),
|
|
3192
|
+
domain: error.ErrorDomain.STORAGE,
|
|
3193
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
3194
|
+
details: {
|
|
3195
|
+
indexName,
|
|
3196
|
+
...filter && { filter: JSON.stringify(filter) },
|
|
3197
|
+
...ids && { idsCount: ids.length }
|
|
3198
|
+
}
|
|
3199
|
+
},
|
|
3200
|
+
error$1
|
|
3201
|
+
);
|
|
3202
|
+
}
|
|
3203
|
+
}
|
|
3199
3204
|
};
|
|
3200
3205
|
|
|
3201
3206
|
exports.LanceStorage = LanceStorage;
|