@mastra/lance 0.0.0-vector-extension-schema-20250922130418 → 0.0.0-vnext-20251104230439
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 +236 -3
- package/dist/index.cjs +325 -553
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +326 -554
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +18 -39
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/scores/index.d.ts +12 -4
- package/dist/storage/domains/scores/index.d.ts.map +1 -1
- package/dist/storage/domains/utils.d.ts.map +1 -1
- package/dist/storage/domains/workflows/index.d.ts +4 -11
- package/dist/storage/domains/workflows/index.d.ts.map +1 -1
- package/dist/storage/index.d.ts +31 -82
- package/dist/storage/index.d.ts.map +1 -1
- package/package.json +8 -8
- package/dist/storage/domains/legacy-evals/index.d.ts +0 -25
- package/dist/storage/domains/legacy-evals/index.d.ts.map +0 -1
- package/dist/storage/domains/traces/index.d.ts +0 -34
- package/dist/storage/domains/traces/index.d.ts.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -5,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
|
}
|
|
@@ -248,6 +133,10 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
248
133
|
this.client = client;
|
|
249
134
|
this.operations = operations;
|
|
250
135
|
}
|
|
136
|
+
// Utility to escape single quotes in SQL strings
|
|
137
|
+
escapeSql(str) {
|
|
138
|
+
return str.replace(/'/g, "''");
|
|
139
|
+
}
|
|
251
140
|
async getThreadById({ threadId }) {
|
|
252
141
|
try {
|
|
253
142
|
const thread = await this.operations.load({ tableName: storage.TABLE_THREADS, keys: { id: threadId } });
|
|
@@ -270,26 +159,6 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
270
159
|
);
|
|
271
160
|
}
|
|
272
161
|
}
|
|
273
|
-
async getThreadsByResourceId({ resourceId }) {
|
|
274
|
-
try {
|
|
275
|
-
const table = await this.client.openTable(storage.TABLE_THREADS);
|
|
276
|
-
const query = table.query().where(`\`resourceId\` = '${resourceId}'`);
|
|
277
|
-
const records = await query.toArray();
|
|
278
|
-
return processResultWithTypeConversion(
|
|
279
|
-
records,
|
|
280
|
-
await getTableSchema({ tableName: storage.TABLE_THREADS, client: this.client })
|
|
281
|
-
);
|
|
282
|
-
} catch (error$1) {
|
|
283
|
-
throw new error.MastraError(
|
|
284
|
-
{
|
|
285
|
-
id: "LANCE_STORE_GET_THREADS_BY_RESOURCE_ID_FAILED",
|
|
286
|
-
domain: error.ErrorDomain.STORAGE,
|
|
287
|
-
category: error.ErrorCategory.THIRD_PARTY
|
|
288
|
-
},
|
|
289
|
-
error$1
|
|
290
|
-
);
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
162
|
/**
|
|
294
163
|
* Saves a thread to the database. This function doesn't overwrite existing threads.
|
|
295
164
|
* @param thread - The thread to save
|
|
@@ -398,7 +267,6 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
398
267
|
threadId,
|
|
399
268
|
resourceId,
|
|
400
269
|
selectBy,
|
|
401
|
-
format,
|
|
402
270
|
threadConfig
|
|
403
271
|
}) {
|
|
404
272
|
try {
|
|
@@ -435,9 +303,11 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
435
303
|
allRecords,
|
|
436
304
|
await getTableSchema({ tableName: storage.TABLE_MESSAGES, client: this.client })
|
|
437
305
|
);
|
|
438
|
-
const list = new agent.MessageList({ threadId, resourceId }).add(
|
|
439
|
-
|
|
440
|
-
|
|
306
|
+
const list = new agent.MessageList({ threadId, resourceId }).add(
|
|
307
|
+
messages.map(this.normalizeMessage),
|
|
308
|
+
"memory"
|
|
309
|
+
);
|
|
310
|
+
return { messages: list.get.all.db() };
|
|
441
311
|
} catch (error$1) {
|
|
442
312
|
throw new error.MastraError(
|
|
443
313
|
{
|
|
@@ -453,11 +323,8 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
453
323
|
);
|
|
454
324
|
}
|
|
455
325
|
}
|
|
456
|
-
async
|
|
457
|
-
messageIds
|
|
458
|
-
format
|
|
459
|
-
}) {
|
|
460
|
-
if (messageIds.length === 0) return [];
|
|
326
|
+
async listMessagesById({ messageIds }) {
|
|
327
|
+
if (messageIds.length === 0) return { messages: [] };
|
|
461
328
|
try {
|
|
462
329
|
const table = await this.client.openTable(storage.TABLE_MESSAGES);
|
|
463
330
|
const quotedIds = messageIds.map((id) => `'${id}'`).join(", ");
|
|
@@ -466,13 +333,15 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
466
333
|
allRecords,
|
|
467
334
|
await getTableSchema({ tableName: storage.TABLE_MESSAGES, client: this.client })
|
|
468
335
|
);
|
|
469
|
-
const list = new agent.MessageList().add(
|
|
470
|
-
|
|
471
|
-
|
|
336
|
+
const list = new agent.MessageList().add(
|
|
337
|
+
messages.map(this.normalizeMessage),
|
|
338
|
+
"memory"
|
|
339
|
+
);
|
|
340
|
+
return { messages: list.get.all.db() };
|
|
472
341
|
} catch (error$1) {
|
|
473
342
|
throw new error.MastraError(
|
|
474
343
|
{
|
|
475
|
-
id: "
|
|
344
|
+
id: "LANCE_STORE_LIST_MESSAGES_BY_ID_FAILED",
|
|
476
345
|
domain: error.ErrorDomain.STORAGE,
|
|
477
346
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
478
347
|
details: {
|
|
@@ -483,11 +352,145 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
483
352
|
);
|
|
484
353
|
}
|
|
485
354
|
}
|
|
355
|
+
async listMessages(args) {
|
|
356
|
+
const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
|
|
357
|
+
if (!threadId.trim()) {
|
|
358
|
+
throw new error.MastraError(
|
|
359
|
+
{
|
|
360
|
+
id: "STORAGE_LANCE_LIST_MESSAGES_INVALID_THREAD_ID",
|
|
361
|
+
domain: error.ErrorDomain.STORAGE,
|
|
362
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
363
|
+
details: { threadId }
|
|
364
|
+
},
|
|
365
|
+
new Error("threadId must be a non-empty string")
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
const perPage = storage.normalizePerPage(perPageInput, 40);
|
|
369
|
+
const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
370
|
+
try {
|
|
371
|
+
if (page < 0) {
|
|
372
|
+
throw new error.MastraError(
|
|
373
|
+
{
|
|
374
|
+
id: "STORAGE_LANCE_LIST_MESSAGES_INVALID_PAGE",
|
|
375
|
+
domain: error.ErrorDomain.STORAGE,
|
|
376
|
+
category: error.ErrorCategory.USER,
|
|
377
|
+
details: { page }
|
|
378
|
+
},
|
|
379
|
+
new Error("page must be >= 0")
|
|
380
|
+
);
|
|
381
|
+
}
|
|
382
|
+
const { field, direction } = this.parseOrderBy(orderBy);
|
|
383
|
+
const table = await this.client.openTable(storage.TABLE_MESSAGES);
|
|
384
|
+
const conditions = [`thread_id = '${this.escapeSql(threadId)}'`];
|
|
385
|
+
if (resourceId) {
|
|
386
|
+
conditions.push(`\`resourceId\` = '${this.escapeSql(resourceId)}'`);
|
|
387
|
+
}
|
|
388
|
+
if (filter?.dateRange?.start) {
|
|
389
|
+
const startTime = filter.dateRange.start instanceof Date ? filter.dateRange.start.getTime() : new Date(filter.dateRange.start).getTime();
|
|
390
|
+
conditions.push(`\`createdAt\` >= ${startTime}`);
|
|
391
|
+
}
|
|
392
|
+
if (filter?.dateRange?.end) {
|
|
393
|
+
const endTime = filter.dateRange.end instanceof Date ? filter.dateRange.end.getTime() : new Date(filter.dateRange.end).getTime();
|
|
394
|
+
conditions.push(`\`createdAt\` <= ${endTime}`);
|
|
395
|
+
}
|
|
396
|
+
const whereClause = conditions.join(" AND ");
|
|
397
|
+
const total = await table.countRows(whereClause);
|
|
398
|
+
const query = table.query().where(whereClause);
|
|
399
|
+
let allRecords = await query.toArray();
|
|
400
|
+
allRecords.sort((a, b) => {
|
|
401
|
+
const aValue = field === "createdAt" ? a.createdAt : a[field];
|
|
402
|
+
const bValue = field === "createdAt" ? b.createdAt : b[field];
|
|
403
|
+
if (aValue == null && bValue == null) return 0;
|
|
404
|
+
if (aValue == null) return direction === "ASC" ? -1 : 1;
|
|
405
|
+
if (bValue == null) return direction === "ASC" ? 1 : -1;
|
|
406
|
+
if (typeof aValue === "string" && typeof bValue === "string") {
|
|
407
|
+
return direction === "ASC" ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue);
|
|
408
|
+
}
|
|
409
|
+
return direction === "ASC" ? aValue - bValue : bValue - aValue;
|
|
410
|
+
});
|
|
411
|
+
const paginatedRecords = allRecords.slice(offset, offset + perPage);
|
|
412
|
+
const messages = paginatedRecords.map((row) => this.normalizeMessage(row));
|
|
413
|
+
if (total === 0 && messages.length === 0 && (!include || include.length === 0)) {
|
|
414
|
+
return {
|
|
415
|
+
messages: [],
|
|
416
|
+
total: 0,
|
|
417
|
+
page,
|
|
418
|
+
perPage: perPageForResponse,
|
|
419
|
+
hasMore: false
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
const messageIds = new Set(messages.map((m) => m.id));
|
|
423
|
+
if (include && include.length > 0) {
|
|
424
|
+
const threadIds = [...new Set(include.map((item) => item.threadId || threadId))];
|
|
425
|
+
const allThreadMessages = [];
|
|
426
|
+
for (const tid of threadIds) {
|
|
427
|
+
const threadQuery = table.query().where(`thread_id = '${tid}'`);
|
|
428
|
+
let threadRecords = await threadQuery.toArray();
|
|
429
|
+
allThreadMessages.push(...threadRecords);
|
|
430
|
+
}
|
|
431
|
+
allThreadMessages.sort((a, b) => a.createdAt - b.createdAt);
|
|
432
|
+
const contextMessages = this.processMessagesWithContext(allThreadMessages, include);
|
|
433
|
+
const includedMessages = contextMessages.map((row) => this.normalizeMessage(row));
|
|
434
|
+
for (const includeMsg of includedMessages) {
|
|
435
|
+
if (!messageIds.has(includeMsg.id)) {
|
|
436
|
+
messages.push(includeMsg);
|
|
437
|
+
messageIds.add(includeMsg.id);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
const list = new agent.MessageList().add(messages, "memory");
|
|
442
|
+
let finalMessages = list.get.all.db();
|
|
443
|
+
finalMessages = finalMessages.sort((a, b) => {
|
|
444
|
+
const aValue = field === "createdAt" ? new Date(a.createdAt).getTime() : a[field];
|
|
445
|
+
const bValue = field === "createdAt" ? new Date(b.createdAt).getTime() : b[field];
|
|
446
|
+
if (aValue == null && bValue == null) return 0;
|
|
447
|
+
if (aValue == null) return direction === "ASC" ? -1 : 1;
|
|
448
|
+
if (bValue == null) return direction === "ASC" ? 1 : -1;
|
|
449
|
+
if (typeof aValue === "string" && typeof bValue === "string") {
|
|
450
|
+
return direction === "ASC" ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue);
|
|
451
|
+
}
|
|
452
|
+
return direction === "ASC" ? aValue - bValue : bValue - aValue;
|
|
453
|
+
});
|
|
454
|
+
const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
|
|
455
|
+
const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
|
|
456
|
+
const fetchedAll = perPageInput === false || allThreadMessagesReturned;
|
|
457
|
+
const hasMore = !fetchedAll && offset + perPage < total;
|
|
458
|
+
return {
|
|
459
|
+
messages: finalMessages,
|
|
460
|
+
total,
|
|
461
|
+
page,
|
|
462
|
+
perPage: perPageForResponse,
|
|
463
|
+
hasMore
|
|
464
|
+
};
|
|
465
|
+
} catch (error$1) {
|
|
466
|
+
const mastraError = new error.MastraError(
|
|
467
|
+
{
|
|
468
|
+
id: "LANCE_STORE_LIST_MESSAGES_FAILED",
|
|
469
|
+
domain: error.ErrorDomain.STORAGE,
|
|
470
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
471
|
+
details: {
|
|
472
|
+
threadId,
|
|
473
|
+
resourceId: resourceId ?? ""
|
|
474
|
+
}
|
|
475
|
+
},
|
|
476
|
+
error$1
|
|
477
|
+
);
|
|
478
|
+
this.logger?.error?.(mastraError.toString());
|
|
479
|
+
this.logger?.trackException?.(mastraError);
|
|
480
|
+
return {
|
|
481
|
+
messages: [],
|
|
482
|
+
total: 0,
|
|
483
|
+
page,
|
|
484
|
+
perPage: perPageForResponse,
|
|
485
|
+
hasMore: false
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
}
|
|
486
489
|
async saveMessages(args) {
|
|
487
490
|
try {
|
|
488
|
-
const { messages
|
|
491
|
+
const { messages } = args;
|
|
489
492
|
if (messages.length === 0) {
|
|
490
|
-
return [];
|
|
493
|
+
return { messages: [] };
|
|
491
494
|
}
|
|
492
495
|
const threadId = messages[0]?.threadId;
|
|
493
496
|
if (!threadId) {
|
|
@@ -523,8 +526,7 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
523
526
|
const updateRecord = { id: threadId, updatedAt: currentTime };
|
|
524
527
|
await threadsTable.mergeInsert("id").whenMatchedUpdateAll().whenNotMatchedInsertAll().execute([updateRecord]);
|
|
525
528
|
const list = new agent.MessageList().add(messages, "memory");
|
|
526
|
-
|
|
527
|
-
return list.get.all.v1();
|
|
529
|
+
return { messages: list.get.all.db() };
|
|
528
530
|
} catch (error$1) {
|
|
529
531
|
throw new error.MastraError(
|
|
530
532
|
{
|
|
@@ -536,32 +538,54 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
536
538
|
);
|
|
537
539
|
}
|
|
538
540
|
}
|
|
539
|
-
async
|
|
541
|
+
async listThreadsByResourceId(args) {
|
|
540
542
|
try {
|
|
541
|
-
const { resourceId, page = 0, perPage
|
|
542
|
-
const
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
543
|
+
const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
|
|
544
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
545
|
+
if (page < 0) {
|
|
546
|
+
throw new error.MastraError(
|
|
547
|
+
{
|
|
548
|
+
id: "STORAGE_LANCE_LIST_THREADS_BY_RESOURCE_ID_INVALID_PAGE",
|
|
549
|
+
domain: error.ErrorDomain.STORAGE,
|
|
550
|
+
category: error.ErrorCategory.USER,
|
|
551
|
+
details: { page }
|
|
552
|
+
},
|
|
553
|
+
new Error("page must be >= 0")
|
|
554
|
+
);
|
|
549
555
|
}
|
|
556
|
+
const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
557
|
+
const { field, direction } = this.parseOrderBy(orderBy);
|
|
558
|
+
const table = await this.client.openTable(storage.TABLE_THREADS);
|
|
559
|
+
const total = await table.countRows(`\`resourceId\` = '${this.escapeSql(resourceId)}'`);
|
|
560
|
+
const query = table.query().where(`\`resourceId\` = '${this.escapeSql(resourceId)}'`);
|
|
550
561
|
const records = await query.toArray();
|
|
551
|
-
records.sort((a, b) =>
|
|
562
|
+
records.sort((a, b) => {
|
|
563
|
+
const aValue = ["createdAt", "updatedAt"].includes(field) ? new Date(a[field]).getTime() : a[field];
|
|
564
|
+
const bValue = ["createdAt", "updatedAt"].includes(field) ? new Date(b[field]).getTime() : b[field];
|
|
565
|
+
if (aValue == null && bValue == null) return 0;
|
|
566
|
+
if (aValue == null) return direction === "ASC" ? -1 : 1;
|
|
567
|
+
if (bValue == null) return direction === "ASC" ? 1 : -1;
|
|
568
|
+
if (typeof aValue === "string" && typeof bValue === "string") {
|
|
569
|
+
return direction === "ASC" ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue);
|
|
570
|
+
}
|
|
571
|
+
return direction === "ASC" ? aValue - bValue : bValue - aValue;
|
|
572
|
+
});
|
|
573
|
+
const paginatedRecords = records.slice(offset, offset + perPage);
|
|
552
574
|
const schema = await getTableSchema({ tableName: storage.TABLE_THREADS, client: this.client });
|
|
553
|
-
const threads =
|
|
575
|
+
const threads = paginatedRecords.map(
|
|
576
|
+
(record) => processResultWithTypeConversion(record, schema)
|
|
577
|
+
);
|
|
554
578
|
return {
|
|
555
579
|
threads,
|
|
556
580
|
total,
|
|
557
581
|
page,
|
|
558
|
-
perPage,
|
|
559
|
-
hasMore:
|
|
582
|
+
perPage: perPageForResponse,
|
|
583
|
+
hasMore: offset + perPage < total
|
|
560
584
|
};
|
|
561
585
|
} catch (error$1) {
|
|
562
586
|
throw new error.MastraError(
|
|
563
587
|
{
|
|
564
|
-
id: "
|
|
588
|
+
id: "LANCE_STORE_LIST_THREADS_BY_RESOURCE_ID_FAILED",
|
|
565
589
|
domain: error.ErrorDomain.STORAGE,
|
|
566
590
|
category: error.ErrorCategory.THIRD_PARTY
|
|
567
591
|
},
|
|
@@ -617,132 +641,8 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
617
641
|
});
|
|
618
642
|
return Array.from(allIndices).sort((a, b) => a - b).map((index) => records[index]);
|
|
619
643
|
}
|
|
620
|
-
async getMessagesPaginated(args) {
|
|
621
|
-
const { threadId, resourceId, selectBy, format = "v1" } = args;
|
|
622
|
-
const page = selectBy?.pagination?.page ?? 0;
|
|
623
|
-
const perPage = selectBy?.pagination?.perPage ?? 10;
|
|
624
|
-
try {
|
|
625
|
-
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
626
|
-
const dateRange = selectBy?.pagination?.dateRange;
|
|
627
|
-
const fromDate = dateRange?.start;
|
|
628
|
-
const toDate = dateRange?.end;
|
|
629
|
-
const table = await this.client.openTable(storage.TABLE_MESSAGES);
|
|
630
|
-
const messages = [];
|
|
631
|
-
if (selectBy?.include && Array.isArray(selectBy.include)) {
|
|
632
|
-
const threadIds = [...new Set(selectBy.include.map((item) => item.threadId))];
|
|
633
|
-
const allThreadMessages = [];
|
|
634
|
-
for (const threadId2 of threadIds) {
|
|
635
|
-
const threadQuery = table.query().where(`thread_id = '${threadId2}'`);
|
|
636
|
-
let threadRecords = await threadQuery.toArray();
|
|
637
|
-
if (fromDate) threadRecords = threadRecords.filter((m) => m.createdAt >= fromDate.getTime());
|
|
638
|
-
if (toDate) threadRecords = threadRecords.filter((m) => m.createdAt <= toDate.getTime());
|
|
639
|
-
allThreadMessages.push(...threadRecords);
|
|
640
|
-
}
|
|
641
|
-
allThreadMessages.sort((a, b) => a.createdAt - b.createdAt);
|
|
642
|
-
const contextMessages = this.processMessagesWithContext(allThreadMessages, selectBy.include);
|
|
643
|
-
messages.push(...contextMessages);
|
|
644
|
-
}
|
|
645
|
-
const conditions = [`thread_id = '${threadId}'`];
|
|
646
|
-
if (resourceId) {
|
|
647
|
-
conditions.push(`\`resourceId\` = '${resourceId}'`);
|
|
648
|
-
}
|
|
649
|
-
if (fromDate) {
|
|
650
|
-
conditions.push(`\`createdAt\` >= ${fromDate.getTime()}`);
|
|
651
|
-
}
|
|
652
|
-
if (toDate) {
|
|
653
|
-
conditions.push(`\`createdAt\` <= ${toDate.getTime()}`);
|
|
654
|
-
}
|
|
655
|
-
let total = 0;
|
|
656
|
-
if (conditions.length > 0) {
|
|
657
|
-
total = await table.countRows(conditions.join(" AND "));
|
|
658
|
-
} else {
|
|
659
|
-
total = await table.countRows();
|
|
660
|
-
}
|
|
661
|
-
if (total === 0 && messages.length === 0) {
|
|
662
|
-
return {
|
|
663
|
-
messages: [],
|
|
664
|
-
total: 0,
|
|
665
|
-
page,
|
|
666
|
-
perPage,
|
|
667
|
-
hasMore: false
|
|
668
|
-
};
|
|
669
|
-
}
|
|
670
|
-
const excludeIds = messages.map((m) => m.id);
|
|
671
|
-
let selectedMessages = [];
|
|
672
|
-
if (selectBy?.last && selectBy.last > 0) {
|
|
673
|
-
const query = table.query();
|
|
674
|
-
if (conditions.length > 0) {
|
|
675
|
-
query.where(conditions.join(" AND "));
|
|
676
|
-
}
|
|
677
|
-
let records = await query.toArray();
|
|
678
|
-
records = records.sort((a, b) => a.createdAt - b.createdAt);
|
|
679
|
-
if (excludeIds.length > 0) {
|
|
680
|
-
records = records.filter((m) => !excludeIds.includes(m.id));
|
|
681
|
-
}
|
|
682
|
-
selectedMessages = records.slice(-selectBy.last);
|
|
683
|
-
} else {
|
|
684
|
-
const query = table.query();
|
|
685
|
-
if (conditions.length > 0) {
|
|
686
|
-
query.where(conditions.join(" AND "));
|
|
687
|
-
}
|
|
688
|
-
let records = await query.toArray();
|
|
689
|
-
records = records.sort((a, b) => a.createdAt - b.createdAt);
|
|
690
|
-
if (excludeIds.length > 0) {
|
|
691
|
-
records = records.filter((m) => !excludeIds.includes(m.id));
|
|
692
|
-
}
|
|
693
|
-
selectedMessages = records.slice(page * perPage, (page + 1) * perPage);
|
|
694
|
-
}
|
|
695
|
-
const allMessages = [...messages, ...selectedMessages];
|
|
696
|
-
const seen = /* @__PURE__ */ new Set();
|
|
697
|
-
const dedupedMessages = allMessages.filter((m) => {
|
|
698
|
-
const key = `${m.id}:${m.thread_id}`;
|
|
699
|
-
if (seen.has(key)) return false;
|
|
700
|
-
seen.add(key);
|
|
701
|
-
return true;
|
|
702
|
-
});
|
|
703
|
-
const formattedMessages = dedupedMessages.map((msg) => {
|
|
704
|
-
const { thread_id, ...rest } = msg;
|
|
705
|
-
return {
|
|
706
|
-
...rest,
|
|
707
|
-
threadId: thread_id,
|
|
708
|
-
content: typeof msg.content === "string" ? (() => {
|
|
709
|
-
try {
|
|
710
|
-
return JSON.parse(msg.content);
|
|
711
|
-
} catch {
|
|
712
|
-
return msg.content;
|
|
713
|
-
}
|
|
714
|
-
})() : msg.content
|
|
715
|
-
};
|
|
716
|
-
});
|
|
717
|
-
const list = new agent.MessageList().add(formattedMessages, "memory");
|
|
718
|
-
return {
|
|
719
|
-
messages: format === "v2" ? list.get.all.v2() : list.get.all.v1(),
|
|
720
|
-
total,
|
|
721
|
-
// Total should be the count of messages matching the filters
|
|
722
|
-
page,
|
|
723
|
-
perPage,
|
|
724
|
-
hasMore: total > (page + 1) * perPage
|
|
725
|
-
};
|
|
726
|
-
} catch (error$1) {
|
|
727
|
-
const mastraError = new error.MastraError(
|
|
728
|
-
{
|
|
729
|
-
id: "LANCE_STORE_GET_MESSAGES_PAGINATED_FAILED",
|
|
730
|
-
domain: error.ErrorDomain.STORAGE,
|
|
731
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
732
|
-
details: {
|
|
733
|
-
threadId,
|
|
734
|
-
resourceId: resourceId ?? ""
|
|
735
|
-
}
|
|
736
|
-
},
|
|
737
|
-
error$1
|
|
738
|
-
);
|
|
739
|
-
this.logger?.trackException?.(mastraError);
|
|
740
|
-
this.logger?.error?.(mastraError.toString());
|
|
741
|
-
return { messages: [], total: 0, page, perPage, hasMore: false };
|
|
742
|
-
}
|
|
743
|
-
}
|
|
744
644
|
/**
|
|
745
|
-
* Parse message data from LanceDB record format to
|
|
645
|
+
* Parse message data from LanceDB record format to MastraDBMessage format
|
|
746
646
|
*/
|
|
747
647
|
parseMessageData(data) {
|
|
748
648
|
const { thread_id, ...rest } = data;
|
|
@@ -1398,13 +1298,27 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1398
1298
|
this.client = client;
|
|
1399
1299
|
}
|
|
1400
1300
|
async saveScore(score) {
|
|
1301
|
+
let validatedScore;
|
|
1302
|
+
try {
|
|
1303
|
+
validatedScore = evals.saveScorePayloadSchema.parse(score);
|
|
1304
|
+
} catch (error$1) {
|
|
1305
|
+
throw new error.MastraError(
|
|
1306
|
+
{
|
|
1307
|
+
id: "LANCE_STORAGE_SAVE_SCORE_FAILED",
|
|
1308
|
+
text: "Failed to save score in LanceStorage",
|
|
1309
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1310
|
+
category: error.ErrorCategory.THIRD_PARTY
|
|
1311
|
+
},
|
|
1312
|
+
error$1
|
|
1313
|
+
);
|
|
1314
|
+
}
|
|
1401
1315
|
try {
|
|
1402
1316
|
const id = crypto.randomUUID();
|
|
1403
1317
|
const table = await this.client.openTable(storage.TABLE_SCORERS);
|
|
1404
1318
|
const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
|
|
1405
1319
|
const allowedFields = new Set(schema.fields.map((f) => f.name));
|
|
1406
1320
|
const filteredScore = {};
|
|
1407
|
-
Object.keys(
|
|
1321
|
+
Object.keys(validatedScore).forEach((key) => {
|
|
1408
1322
|
if (allowedFields.has(key)) {
|
|
1409
1323
|
filteredScore[key] = score[key];
|
|
1410
1324
|
}
|
|
@@ -1451,7 +1365,7 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1451
1365
|
);
|
|
1452
1366
|
}
|
|
1453
1367
|
}
|
|
1454
|
-
async
|
|
1368
|
+
async listScoresByScorerId({
|
|
1455
1369
|
scorerId,
|
|
1456
1370
|
pagination,
|
|
1457
1371
|
entityId,
|
|
@@ -1459,9 +1373,10 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1459
1373
|
source
|
|
1460
1374
|
}) {
|
|
1461
1375
|
try {
|
|
1376
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1377
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
1378
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1462
1379
|
const table = await this.client.openTable(storage.TABLE_SCORERS);
|
|
1463
|
-
const { page = 0, perPage = 10 } = pagination || {};
|
|
1464
|
-
const offset = page * perPage;
|
|
1465
1380
|
let query = table.query().where(`\`scorerId\` = '${scorerId}'`);
|
|
1466
1381
|
if (source) {
|
|
1467
1382
|
query = query.where(`\`source\` = '${source}'`);
|
|
@@ -1472,23 +1387,32 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1472
1387
|
if (entityType) {
|
|
1473
1388
|
query = query.where(`\`entityType\` = '${entityType}'`);
|
|
1474
1389
|
}
|
|
1475
|
-
query = query.limit(perPage);
|
|
1476
|
-
if (offset > 0) query.offset(offset);
|
|
1477
|
-
const records = await query.toArray();
|
|
1478
|
-
const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
|
|
1479
|
-
const scores = processResultWithTypeConversion(records, schema);
|
|
1480
1390
|
let totalQuery = table.query().where(`\`scorerId\` = '${scorerId}'`);
|
|
1481
1391
|
if (source) {
|
|
1482
1392
|
totalQuery = totalQuery.where(`\`source\` = '${source}'`);
|
|
1483
1393
|
}
|
|
1394
|
+
if (entityId) {
|
|
1395
|
+
totalQuery = totalQuery.where(`\`entityId\` = '${entityId}'`);
|
|
1396
|
+
}
|
|
1397
|
+
if (entityType) {
|
|
1398
|
+
totalQuery = totalQuery.where(`\`entityType\` = '${entityType}'`);
|
|
1399
|
+
}
|
|
1484
1400
|
const allRecords = await totalQuery.toArray();
|
|
1485
1401
|
const total = allRecords.length;
|
|
1402
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1403
|
+
if (perPageInput !== false) {
|
|
1404
|
+
query = query.limit(perPage);
|
|
1405
|
+
if (start > 0) query = query.offset(start);
|
|
1406
|
+
}
|
|
1407
|
+
const records = await query.toArray();
|
|
1408
|
+
const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
|
|
1409
|
+
const scores = processResultWithTypeConversion(records, schema);
|
|
1486
1410
|
return {
|
|
1487
1411
|
pagination: {
|
|
1488
1412
|
page,
|
|
1489
|
-
perPage,
|
|
1413
|
+
perPage: perPageForResponse,
|
|
1490
1414
|
total,
|
|
1491
|
-
hasMore:
|
|
1415
|
+
hasMore: end < total
|
|
1492
1416
|
},
|
|
1493
1417
|
scores
|
|
1494
1418
|
};
|
|
@@ -1505,27 +1429,32 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1505
1429
|
);
|
|
1506
1430
|
}
|
|
1507
1431
|
}
|
|
1508
|
-
async
|
|
1432
|
+
async listScoresByRunId({
|
|
1509
1433
|
runId,
|
|
1510
1434
|
pagination
|
|
1511
1435
|
}) {
|
|
1512
1436
|
try {
|
|
1437
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1438
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
1439
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1513
1440
|
const table = await this.client.openTable(storage.TABLE_SCORERS);
|
|
1514
|
-
const
|
|
1515
|
-
const
|
|
1516
|
-
const
|
|
1517
|
-
|
|
1441
|
+
const allRecords = await table.query().where(`\`runId\` = '${runId}'`).toArray();
|
|
1442
|
+
const total = allRecords.length;
|
|
1443
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1444
|
+
let query = table.query().where(`\`runId\` = '${runId}'`);
|
|
1445
|
+
if (perPageInput !== false) {
|
|
1446
|
+
query = query.limit(perPage);
|
|
1447
|
+
if (start > 0) query = query.offset(start);
|
|
1448
|
+
}
|
|
1518
1449
|
const records = await query.toArray();
|
|
1519
1450
|
const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
|
|
1520
1451
|
const scores = processResultWithTypeConversion(records, schema);
|
|
1521
|
-
const allRecords = await table.query().where(`\`runId\` = '${runId}'`).toArray();
|
|
1522
|
-
const total = allRecords.length;
|
|
1523
1452
|
return {
|
|
1524
1453
|
pagination: {
|
|
1525
1454
|
page,
|
|
1526
|
-
perPage,
|
|
1455
|
+
perPage: perPageForResponse,
|
|
1527
1456
|
total,
|
|
1528
|
-
hasMore:
|
|
1457
|
+
hasMore: end < total
|
|
1529
1458
|
},
|
|
1530
1459
|
scores
|
|
1531
1460
|
};
|
|
@@ -1542,28 +1471,33 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1542
1471
|
);
|
|
1543
1472
|
}
|
|
1544
1473
|
}
|
|
1545
|
-
async
|
|
1474
|
+
async listScoresByEntityId({
|
|
1546
1475
|
entityId,
|
|
1547
1476
|
entityType,
|
|
1548
1477
|
pagination
|
|
1549
1478
|
}) {
|
|
1550
1479
|
try {
|
|
1480
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1481
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
1482
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1551
1483
|
const table = await this.client.openTable(storage.TABLE_SCORERS);
|
|
1552
|
-
const
|
|
1553
|
-
const
|
|
1554
|
-
const
|
|
1555
|
-
|
|
1484
|
+
const allRecords = await table.query().where(`\`entityId\` = '${entityId}' AND \`entityType\` = '${entityType}'`).toArray();
|
|
1485
|
+
const total = allRecords.length;
|
|
1486
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1487
|
+
let query = table.query().where(`\`entityId\` = '${entityId}' AND \`entityType\` = '${entityType}'`);
|
|
1488
|
+
if (perPageInput !== false) {
|
|
1489
|
+
query = query.limit(perPage);
|
|
1490
|
+
if (start > 0) query = query.offset(start);
|
|
1491
|
+
}
|
|
1556
1492
|
const records = await query.toArray();
|
|
1557
1493
|
const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
|
|
1558
1494
|
const scores = processResultWithTypeConversion(records, schema);
|
|
1559
|
-
const allRecords = await table.query().where(`\`entityId\` = '${entityId}' AND \`entityType\` = '${entityType}'`).toArray();
|
|
1560
|
-
const total = allRecords.length;
|
|
1561
1495
|
return {
|
|
1562
1496
|
pagination: {
|
|
1563
1497
|
page,
|
|
1564
|
-
perPage,
|
|
1498
|
+
perPage: perPageForResponse,
|
|
1565
1499
|
total,
|
|
1566
|
-
hasMore:
|
|
1500
|
+
hasMore: end < total
|
|
1567
1501
|
},
|
|
1568
1502
|
scores
|
|
1569
1503
|
};
|
|
@@ -1580,198 +1514,49 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1580
1514
|
);
|
|
1581
1515
|
}
|
|
1582
1516
|
}
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
constructor({ client, operations }) {
|
|
1588
|
-
super();
|
|
1589
|
-
this.client = client;
|
|
1590
|
-
this.operations = operations;
|
|
1591
|
-
}
|
|
1592
|
-
async saveTrace({ trace }) {
|
|
1593
|
-
try {
|
|
1594
|
-
const table = await this.client.openTable(storage.TABLE_TRACES);
|
|
1595
|
-
const record = {
|
|
1596
|
-
...trace,
|
|
1597
|
-
attributes: JSON.stringify(trace.attributes),
|
|
1598
|
-
status: JSON.stringify(trace.status),
|
|
1599
|
-
events: JSON.stringify(trace.events),
|
|
1600
|
-
links: JSON.stringify(trace.links),
|
|
1601
|
-
other: JSON.stringify(trace.other)
|
|
1602
|
-
};
|
|
1603
|
-
await table.add([record], { mode: "append" });
|
|
1604
|
-
return trace;
|
|
1605
|
-
} catch (error$1) {
|
|
1606
|
-
throw new error.MastraError(
|
|
1607
|
-
{
|
|
1608
|
-
id: "LANCE_STORE_SAVE_TRACE_FAILED",
|
|
1609
|
-
domain: error.ErrorDomain.STORAGE,
|
|
1610
|
-
category: error.ErrorCategory.THIRD_PARTY
|
|
1611
|
-
},
|
|
1612
|
-
error$1
|
|
1613
|
-
);
|
|
1614
|
-
}
|
|
1615
|
-
}
|
|
1616
|
-
async getTraceById({ traceId }) {
|
|
1617
|
-
try {
|
|
1618
|
-
const table = await this.client.openTable(storage.TABLE_TRACES);
|
|
1619
|
-
const query = table.query().where(`id = '${traceId}'`);
|
|
1620
|
-
const records = await query.toArray();
|
|
1621
|
-
return records[0];
|
|
1622
|
-
} catch (error$1) {
|
|
1623
|
-
throw new error.MastraError(
|
|
1624
|
-
{
|
|
1625
|
-
id: "LANCE_STORE_GET_TRACE_BY_ID_FAILED",
|
|
1626
|
-
domain: error.ErrorDomain.STORAGE,
|
|
1627
|
-
category: error.ErrorCategory.THIRD_PARTY
|
|
1628
|
-
},
|
|
1629
|
-
error$1
|
|
1630
|
-
);
|
|
1631
|
-
}
|
|
1632
|
-
}
|
|
1633
|
-
async getTraces({
|
|
1634
|
-
name,
|
|
1635
|
-
scope,
|
|
1636
|
-
page = 1,
|
|
1637
|
-
perPage = 10,
|
|
1638
|
-
attributes
|
|
1517
|
+
async listScoresBySpan({
|
|
1518
|
+
traceId,
|
|
1519
|
+
spanId,
|
|
1520
|
+
pagination
|
|
1639
1521
|
}) {
|
|
1640
1522
|
try {
|
|
1641
|
-
const
|
|
1642
|
-
const
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
}
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
}
|
|
1649
|
-
if (
|
|
1650
|
-
query
|
|
1651
|
-
|
|
1652
|
-
const offset = (page - 1) * perPage;
|
|
1653
|
-
query.limit(perPage);
|
|
1654
|
-
if (offset > 0) {
|
|
1655
|
-
query.offset(offset);
|
|
1656
|
-
}
|
|
1657
|
-
const records = await query.toArray();
|
|
1658
|
-
return records.map((record) => {
|
|
1659
|
-
const processed = {
|
|
1660
|
-
...record,
|
|
1661
|
-
attributes: record.attributes ? JSON.parse(record.attributes) : {},
|
|
1662
|
-
status: record.status ? JSON.parse(record.status) : {},
|
|
1663
|
-
events: record.events ? JSON.parse(record.events) : [],
|
|
1664
|
-
links: record.links ? JSON.parse(record.links) : [],
|
|
1665
|
-
other: record.other ? JSON.parse(record.other) : {},
|
|
1666
|
-
startTime: new Date(record.startTime),
|
|
1667
|
-
endTime: new Date(record.endTime),
|
|
1668
|
-
createdAt: new Date(record.createdAt)
|
|
1669
|
-
};
|
|
1670
|
-
if (processed.parentSpanId === null || processed.parentSpanId === void 0) {
|
|
1671
|
-
processed.parentSpanId = "";
|
|
1672
|
-
} else {
|
|
1673
|
-
processed.parentSpanId = String(processed.parentSpanId);
|
|
1674
|
-
}
|
|
1675
|
-
return processed;
|
|
1676
|
-
});
|
|
1677
|
-
} catch (error$1) {
|
|
1678
|
-
throw new error.MastraError(
|
|
1679
|
-
{
|
|
1680
|
-
id: "LANCE_STORE_GET_TRACES_FAILED",
|
|
1681
|
-
domain: error.ErrorDomain.STORAGE,
|
|
1682
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
1683
|
-
details: { name: name ?? "", scope: scope ?? "" }
|
|
1684
|
-
},
|
|
1685
|
-
error$1
|
|
1686
|
-
);
|
|
1687
|
-
}
|
|
1688
|
-
}
|
|
1689
|
-
async getTracesPaginated(args) {
|
|
1690
|
-
try {
|
|
1691
|
-
const table = await this.client.openTable(storage.TABLE_TRACES);
|
|
1692
|
-
const query = table.query();
|
|
1693
|
-
const conditions = [];
|
|
1694
|
-
if (args.name) {
|
|
1695
|
-
conditions.push(`name = '${args.name}'`);
|
|
1696
|
-
}
|
|
1697
|
-
if (args.scope) {
|
|
1698
|
-
conditions.push(`scope = '${args.scope}'`);
|
|
1699
|
-
}
|
|
1700
|
-
if (args.attributes) {
|
|
1701
|
-
const attributesStr = JSON.stringify(args.attributes);
|
|
1702
|
-
conditions.push(`attributes LIKE '%${attributesStr.replace(/"/g, '\\"')}%'`);
|
|
1703
|
-
}
|
|
1704
|
-
if (args.dateRange?.start) {
|
|
1705
|
-
conditions.push(`\`createdAt\` >= ${args.dateRange.start.getTime()}`);
|
|
1706
|
-
}
|
|
1707
|
-
if (args.dateRange?.end) {
|
|
1708
|
-
conditions.push(`\`createdAt\` <= ${args.dateRange.end.getTime()}`);
|
|
1709
|
-
}
|
|
1710
|
-
if (conditions.length > 0) {
|
|
1711
|
-
const whereClause = conditions.join(" AND ");
|
|
1712
|
-
query.where(whereClause);
|
|
1713
|
-
}
|
|
1714
|
-
let total = 0;
|
|
1715
|
-
if (conditions.length > 0) {
|
|
1716
|
-
const countQuery = table.query().where(conditions.join(" AND "));
|
|
1717
|
-
const allRecords = await countQuery.toArray();
|
|
1718
|
-
total = allRecords.length;
|
|
1719
|
-
} else {
|
|
1720
|
-
total = await table.countRows();
|
|
1721
|
-
}
|
|
1722
|
-
const page = args.page || 0;
|
|
1723
|
-
const perPage = args.perPage || 10;
|
|
1724
|
-
const offset = page * perPage;
|
|
1725
|
-
query.limit(perPage);
|
|
1726
|
-
if (offset > 0) {
|
|
1727
|
-
query.offset(offset);
|
|
1523
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1524
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
1525
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1526
|
+
const table = await this.client.openTable(storage.TABLE_SCORERS);
|
|
1527
|
+
const allRecords = await table.query().where(`\`traceId\` = '${traceId}' AND \`spanId\` = '${spanId}'`).toArray();
|
|
1528
|
+
const total = allRecords.length;
|
|
1529
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1530
|
+
let query = table.query().where(`\`traceId\` = '${traceId}' AND \`spanId\` = '${spanId}'`);
|
|
1531
|
+
if (perPageInput !== false) {
|
|
1532
|
+
query = query.limit(perPage);
|
|
1533
|
+
if (start > 0) query = query.offset(start);
|
|
1728
1534
|
}
|
|
1729
1535
|
const records = await query.toArray();
|
|
1730
|
-
const
|
|
1731
|
-
|
|
1732
|
-
...record,
|
|
1733
|
-
attributes: record.attributes ? JSON.parse(record.attributes) : {},
|
|
1734
|
-
status: record.status ? JSON.parse(record.status) : {},
|
|
1735
|
-
events: record.events ? JSON.parse(record.events) : [],
|
|
1736
|
-
links: record.links ? JSON.parse(record.links) : [],
|
|
1737
|
-
other: record.other ? JSON.parse(record.other) : {},
|
|
1738
|
-
startTime: new Date(record.startTime),
|
|
1739
|
-
endTime: new Date(record.endTime),
|
|
1740
|
-
createdAt: new Date(record.createdAt)
|
|
1741
|
-
};
|
|
1742
|
-
if (processed.parentSpanId === null || processed.parentSpanId === void 0) {
|
|
1743
|
-
processed.parentSpanId = "";
|
|
1744
|
-
} else {
|
|
1745
|
-
processed.parentSpanId = String(processed.parentSpanId);
|
|
1746
|
-
}
|
|
1747
|
-
return processed;
|
|
1748
|
-
});
|
|
1536
|
+
const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
|
|
1537
|
+
const scores = processResultWithTypeConversion(records, schema);
|
|
1749
1538
|
return {
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1539
|
+
pagination: {
|
|
1540
|
+
page,
|
|
1541
|
+
perPage: perPageForResponse,
|
|
1542
|
+
total,
|
|
1543
|
+
hasMore: end < total
|
|
1544
|
+
},
|
|
1545
|
+
scores
|
|
1755
1546
|
};
|
|
1756
1547
|
} catch (error$1) {
|
|
1757
1548
|
throw new error.MastraError(
|
|
1758
1549
|
{
|
|
1759
|
-
id: "
|
|
1550
|
+
id: "LANCE_STORAGE_GET_SCORES_BY_SPAN_FAILED",
|
|
1551
|
+
text: "Failed to get scores by traceId and spanId in LanceStorage",
|
|
1760
1552
|
domain: error.ErrorDomain.STORAGE,
|
|
1761
1553
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
1762
|
-
details: {
|
|
1554
|
+
details: { error: error$1?.message }
|
|
1763
1555
|
},
|
|
1764
1556
|
error$1
|
|
1765
1557
|
);
|
|
1766
1558
|
}
|
|
1767
1559
|
}
|
|
1768
|
-
async batchTraceInsert({ records }) {
|
|
1769
|
-
this.logger.debug("Batch inserting traces", { count: records.length });
|
|
1770
|
-
await this.operations.batchInsert({
|
|
1771
|
-
tableName: storage.TABLE_TRACES,
|
|
1772
|
-
records
|
|
1773
|
-
});
|
|
1774
|
-
}
|
|
1775
1560
|
};
|
|
1776
1561
|
function parseWorkflowRun(row) {
|
|
1777
1562
|
let parsedSnapshot = row.snapshot;
|
|
@@ -1802,7 +1587,7 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
|
|
|
1802
1587
|
// runId,
|
|
1803
1588
|
// stepId,
|
|
1804
1589
|
// result,
|
|
1805
|
-
//
|
|
1590
|
+
// requestContext,
|
|
1806
1591
|
}) {
|
|
1807
1592
|
throw new Error("Method not implemented.");
|
|
1808
1593
|
}
|
|
@@ -1896,7 +1681,7 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
|
|
|
1896
1681
|
);
|
|
1897
1682
|
}
|
|
1898
1683
|
}
|
|
1899
|
-
async
|
|
1684
|
+
async listWorkflowRuns(args) {
|
|
1900
1685
|
try {
|
|
1901
1686
|
const table = await this.client.openTable(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
1902
1687
|
let query = table.query();
|
|
@@ -1920,11 +1705,22 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
|
|
|
1920
1705
|
} else {
|
|
1921
1706
|
total = await table.countRows();
|
|
1922
1707
|
}
|
|
1923
|
-
if (args?.
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1708
|
+
if (args?.perPage !== void 0 && args?.page !== void 0) {
|
|
1709
|
+
const normalizedPerPage = storage.normalizePerPage(args.perPage, Number.MAX_SAFE_INTEGER);
|
|
1710
|
+
if (args.page < 0 || !Number.isInteger(args.page)) {
|
|
1711
|
+
throw new error.MastraError(
|
|
1712
|
+
{
|
|
1713
|
+
id: "LANCE_STORE_INVALID_PAGINATION_PARAMS",
|
|
1714
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1715
|
+
category: error.ErrorCategory.USER,
|
|
1716
|
+
details: { page: args.page, perPage: args.perPage }
|
|
1717
|
+
},
|
|
1718
|
+
new Error(`Invalid pagination parameters: page=${args.page}, perPage=${args.perPage}`)
|
|
1719
|
+
);
|
|
1720
|
+
}
|
|
1721
|
+
const offset = args.page * normalizedPerPage;
|
|
1722
|
+
query.limit(normalizedPerPage);
|
|
1723
|
+
query.offset(offset);
|
|
1928
1724
|
}
|
|
1929
1725
|
const records = await query.toArray();
|
|
1930
1726
|
return {
|
|
@@ -1934,10 +1730,10 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
|
|
|
1934
1730
|
} catch (error$1) {
|
|
1935
1731
|
throw new error.MastraError(
|
|
1936
1732
|
{
|
|
1937
|
-
id: "
|
|
1733
|
+
id: "LANCE_STORE_LIST_WORKFLOW_RUNS_FAILED",
|
|
1938
1734
|
domain: error.ErrorDomain.STORAGE,
|
|
1939
1735
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
1940
|
-
details: {
|
|
1736
|
+
details: { resourceId: args?.resourceId ?? "", workflowName: args?.workflowName ?? "" }
|
|
1941
1737
|
},
|
|
1942
1738
|
error$1
|
|
1943
1739
|
);
|
|
@@ -1979,10 +1775,8 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
1979
1775
|
instance.stores = {
|
|
1980
1776
|
operations: new StoreOperationsLance({ client: instance.lanceClient }),
|
|
1981
1777
|
workflows: new StoreWorkflowsLance({ client: instance.lanceClient }),
|
|
1982
|
-
traces: new StoreTracesLance({ client: instance.lanceClient, operations }),
|
|
1983
1778
|
scores: new StoreScoresLance({ client: instance.lanceClient }),
|
|
1984
|
-
memory: new StoreMemoryLance({ client: instance.lanceClient, operations })
|
|
1985
|
-
legacyEvals: new StoreLegacyEvalsLance({ client: instance.lanceClient })
|
|
1779
|
+
memory: new StoreMemoryLance({ client: instance.lanceClient, operations })
|
|
1986
1780
|
};
|
|
1987
1781
|
return instance;
|
|
1988
1782
|
} catch (e) {
|
|
@@ -2008,9 +1802,7 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
2008
1802
|
this.stores = {
|
|
2009
1803
|
operations: new StoreOperationsLance({ client: this.lanceClient }),
|
|
2010
1804
|
workflows: new StoreWorkflowsLance({ client: this.lanceClient }),
|
|
2011
|
-
traces: new StoreTracesLance({ client: this.lanceClient, operations }),
|
|
2012
1805
|
scores: new StoreScoresLance({ client: this.lanceClient }),
|
|
2013
|
-
legacyEvals: new StoreLegacyEvalsLance({ client: this.lanceClient }),
|
|
2014
1806
|
memory: new StoreMemoryLance({ client: this.lanceClient, operations })
|
|
2015
1807
|
};
|
|
2016
1808
|
}
|
|
@@ -2045,9 +1837,6 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
2045
1837
|
async getThreadById({ threadId }) {
|
|
2046
1838
|
return this.stores.memory.getThreadById({ threadId });
|
|
2047
1839
|
}
|
|
2048
|
-
async getThreadsByResourceId({ resourceId }) {
|
|
2049
|
-
return this.stores.memory.getThreadsByResourceId({ resourceId });
|
|
2050
|
-
}
|
|
2051
1840
|
/**
|
|
2052
1841
|
* Saves a thread to the database. This function doesn't overwrite existing threads.
|
|
2053
1842
|
* @param thread - The thread to save
|
|
@@ -2072,7 +1861,8 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
2072
1861
|
resourceWorkingMemory: true,
|
|
2073
1862
|
hasColumn: true,
|
|
2074
1863
|
createTable: true,
|
|
2075
|
-
deleteMessages: false
|
|
1864
|
+
deleteMessages: false,
|
|
1865
|
+
listScoresBySpan: true
|
|
2076
1866
|
};
|
|
2077
1867
|
}
|
|
2078
1868
|
async getResourceById({ resourceId }) {
|
|
@@ -2140,46 +1930,21 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
2140
1930
|
threadId,
|
|
2141
1931
|
resourceId,
|
|
2142
1932
|
selectBy,
|
|
2143
|
-
format,
|
|
2144
1933
|
threadConfig
|
|
2145
1934
|
}) {
|
|
2146
|
-
return this.stores.memory.getMessages({ threadId, resourceId, selectBy,
|
|
1935
|
+
return this.stores.memory.getMessages({ threadId, resourceId, selectBy, threadConfig });
|
|
2147
1936
|
}
|
|
2148
|
-
async
|
|
2149
|
-
messageIds
|
|
2150
|
-
format
|
|
2151
|
-
}) {
|
|
2152
|
-
return this.stores.memory.getMessagesById({ messageIds, format });
|
|
1937
|
+
async listMessagesById({ messageIds }) {
|
|
1938
|
+
return this.stores.memory.listMessagesById({ messageIds });
|
|
2153
1939
|
}
|
|
2154
1940
|
async saveMessages(args) {
|
|
2155
1941
|
return this.stores.memory.saveMessages(args);
|
|
2156
1942
|
}
|
|
2157
|
-
async getThreadsByResourceIdPaginated(args) {
|
|
2158
|
-
return this.stores.memory.getThreadsByResourceIdPaginated(args);
|
|
2159
|
-
}
|
|
2160
|
-
async getMessagesPaginated(args) {
|
|
2161
|
-
return this.stores.memory.getMessagesPaginated(args);
|
|
2162
|
-
}
|
|
2163
1943
|
async updateMessages(_args) {
|
|
2164
1944
|
return this.stores.memory.updateMessages(_args);
|
|
2165
1945
|
}
|
|
2166
|
-
async
|
|
2167
|
-
return this.stores.
|
|
2168
|
-
}
|
|
2169
|
-
async getTraces(args) {
|
|
2170
|
-
return this.stores.traces.getTraces(args);
|
|
2171
|
-
}
|
|
2172
|
-
async getTracesPaginated(args) {
|
|
2173
|
-
return this.stores.traces.getTracesPaginated(args);
|
|
2174
|
-
}
|
|
2175
|
-
async getEvalsByAgentName(agentName, type) {
|
|
2176
|
-
return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
|
|
2177
|
-
}
|
|
2178
|
-
async getEvals(options) {
|
|
2179
|
-
return this.stores.legacyEvals.getEvals(options);
|
|
2180
|
-
}
|
|
2181
|
-
async getWorkflowRuns(args) {
|
|
2182
|
-
return this.stores.workflows.getWorkflowRuns(args);
|
|
1946
|
+
async listWorkflowRuns(args) {
|
|
1947
|
+
return this.stores.workflows.listWorkflowRuns(args);
|
|
2183
1948
|
}
|
|
2184
1949
|
async getWorkflowRunById(args) {
|
|
2185
1950
|
return this.stores.workflows.getWorkflowRunById(args);
|
|
@@ -2189,9 +1954,9 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
2189
1954
|
runId,
|
|
2190
1955
|
stepId,
|
|
2191
1956
|
result,
|
|
2192
|
-
|
|
1957
|
+
requestContext
|
|
2193
1958
|
}) {
|
|
2194
|
-
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result,
|
|
1959
|
+
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, requestContext });
|
|
2195
1960
|
}
|
|
2196
1961
|
async updateWorkflowState({
|
|
2197
1962
|
workflowName,
|
|
@@ -2217,30 +1982,37 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
2217
1982
|
async getScoreById({ id: _id }) {
|
|
2218
1983
|
return this.stores.scores.getScoreById({ id: _id });
|
|
2219
1984
|
}
|
|
2220
|
-
async
|
|
1985
|
+
async listScoresByScorerId({
|
|
2221
1986
|
scorerId,
|
|
2222
1987
|
source,
|
|
2223
1988
|
entityId,
|
|
2224
1989
|
entityType,
|
|
2225
1990
|
pagination
|
|
2226
1991
|
}) {
|
|
2227
|
-
return this.stores.scores.
|
|
1992
|
+
return this.stores.scores.listScoresByScorerId({ scorerId, source, pagination, entityId, entityType });
|
|
2228
1993
|
}
|
|
2229
1994
|
async saveScore(_score) {
|
|
2230
1995
|
return this.stores.scores.saveScore(_score);
|
|
2231
1996
|
}
|
|
2232
|
-
async
|
|
1997
|
+
async listScoresByRunId({
|
|
2233
1998
|
runId,
|
|
2234
1999
|
pagination
|
|
2235
2000
|
}) {
|
|
2236
|
-
return this.stores.scores.
|
|
2001
|
+
return this.stores.scores.listScoresByRunId({ runId, pagination });
|
|
2237
2002
|
}
|
|
2238
|
-
async
|
|
2003
|
+
async listScoresByEntityId({
|
|
2239
2004
|
entityId,
|
|
2240
2005
|
entityType,
|
|
2241
2006
|
pagination
|
|
2242
2007
|
}) {
|
|
2243
|
-
return this.stores.scores.
|
|
2008
|
+
return this.stores.scores.listScoresByEntityId({ entityId, entityType, pagination });
|
|
2009
|
+
}
|
|
2010
|
+
async listScoresBySpan({
|
|
2011
|
+
traceId,
|
|
2012
|
+
spanId,
|
|
2013
|
+
pagination
|
|
2014
|
+
}) {
|
|
2015
|
+
return this.stores.scores.listScoresBySpan({ traceId, spanId, pagination });
|
|
2244
2016
|
}
|
|
2245
2017
|
};
|
|
2246
2018
|
var LanceFilterTranslator = class extends filter.BaseFilterTranslator {
|