@mastra/lance 0.0.0-just-snapshot-20251014192224 → 0.0.0-main-test-20251105183450
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 +227 -3
- package/README.md +61 -4
- package/dist/index.cjs +276 -633
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +276 -633
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +15 -39
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/scores/index.d.ts +5 -5
- package/dist/storage/domains/scores/index.d.ts.map +1 -1
- package/dist/storage/domains/utils.d.ts.map +1 -1
- package/dist/storage/domains/workflows/index.d.ts +4 -11
- package/dist/storage/domains/workflows/index.d.ts.map +1 -1
- package/dist/storage/index.d.ts +21 -84
- package/dist/storage/index.d.ts.map +1 -1
- package/package.json +11 -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.js
CHANGED
|
@@ -1,133 +1,17 @@
|
|
|
1
1
|
import { connect, Index } from '@lancedb/lancedb';
|
|
2
2
|
import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
|
|
3
|
-
import { MastraStorage, StoreOperations,
|
|
3
|
+
import { MastraStorage, StoreOperations, MemoryStorage, TABLE_THREADS, TABLE_MESSAGES, normalizePerPage, calculatePagination, TABLE_RESOURCES, ScoresStorage, TABLE_SCORERS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, ensureDate } from '@mastra/core/storage';
|
|
4
4
|
import { MessageList } from '@mastra/core/agent';
|
|
5
5
|
import { Utf8, Float64, Binary, Float32, Int32, Field, Schema } from 'apache-arrow';
|
|
6
|
-
import { saveScorePayloadSchema } from '@mastra/core/
|
|
6
|
+
import { saveScorePayloadSchema } from '@mastra/core/evals';
|
|
7
7
|
import { MastraVector } from '@mastra/core/vector';
|
|
8
8
|
import { BaseFilterTranslator } from '@mastra/core/vector/filter';
|
|
9
9
|
|
|
10
10
|
// src/storage/index.ts
|
|
11
|
-
var StoreLegacyEvalsLance = class extends LegacyEvalsStorage {
|
|
12
|
-
client;
|
|
13
|
-
constructor({ client }) {
|
|
14
|
-
super();
|
|
15
|
-
this.client = client;
|
|
16
|
-
}
|
|
17
|
-
async getEvalsByAgentName(agentName, type) {
|
|
18
|
-
try {
|
|
19
|
-
const table = await this.client.openTable(TABLE_EVALS);
|
|
20
|
-
const query = table.query().where(`agent_name = '${agentName}'`);
|
|
21
|
-
const records = await query.toArray();
|
|
22
|
-
let filteredRecords = records;
|
|
23
|
-
if (type === "live") {
|
|
24
|
-
filteredRecords = records.filter((record) => record.test_info === null);
|
|
25
|
-
} else if (type === "test") {
|
|
26
|
-
filteredRecords = records.filter((record) => record.test_info !== null);
|
|
27
|
-
}
|
|
28
|
-
return filteredRecords.map((record) => {
|
|
29
|
-
return {
|
|
30
|
-
id: record.id,
|
|
31
|
-
input: record.input,
|
|
32
|
-
output: record.output,
|
|
33
|
-
agentName: record.agent_name,
|
|
34
|
-
metricName: record.metric_name,
|
|
35
|
-
result: JSON.parse(record.result),
|
|
36
|
-
instructions: record.instructions,
|
|
37
|
-
testInfo: record.test_info ? JSON.parse(record.test_info) : null,
|
|
38
|
-
globalRunId: record.global_run_id,
|
|
39
|
-
runId: record.run_id,
|
|
40
|
-
createdAt: new Date(record.created_at).toString()
|
|
41
|
-
};
|
|
42
|
-
});
|
|
43
|
-
} catch (error) {
|
|
44
|
-
throw new MastraError(
|
|
45
|
-
{
|
|
46
|
-
id: "LANCE_STORE_GET_EVALS_BY_AGENT_NAME_FAILED",
|
|
47
|
-
domain: ErrorDomain.STORAGE,
|
|
48
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
49
|
-
details: { agentName }
|
|
50
|
-
},
|
|
51
|
-
error
|
|
52
|
-
);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
async getEvals(options) {
|
|
56
|
-
try {
|
|
57
|
-
const table = await this.client.openTable(TABLE_EVALS);
|
|
58
|
-
const conditions = [];
|
|
59
|
-
if (options.agentName) {
|
|
60
|
-
conditions.push(`agent_name = '${options.agentName}'`);
|
|
61
|
-
}
|
|
62
|
-
if (options.type === "live") {
|
|
63
|
-
conditions.push("length(test_info) = 0");
|
|
64
|
-
} else if (options.type === "test") {
|
|
65
|
-
conditions.push("length(test_info) > 0");
|
|
66
|
-
}
|
|
67
|
-
const startDate = options.dateRange?.start || options.fromDate;
|
|
68
|
-
const endDate = options.dateRange?.end || options.toDate;
|
|
69
|
-
if (startDate) {
|
|
70
|
-
conditions.push(`\`created_at\` >= ${startDate.getTime()}`);
|
|
71
|
-
}
|
|
72
|
-
if (endDate) {
|
|
73
|
-
conditions.push(`\`created_at\` <= ${endDate.getTime()}`);
|
|
74
|
-
}
|
|
75
|
-
let total = 0;
|
|
76
|
-
if (conditions.length > 0) {
|
|
77
|
-
total = await table.countRows(conditions.join(" AND "));
|
|
78
|
-
} else {
|
|
79
|
-
total = await table.countRows();
|
|
80
|
-
}
|
|
81
|
-
const query = table.query();
|
|
82
|
-
if (conditions.length > 0) {
|
|
83
|
-
const whereClause = conditions.join(" AND ");
|
|
84
|
-
query.where(whereClause);
|
|
85
|
-
}
|
|
86
|
-
const records = await query.toArray();
|
|
87
|
-
const evals = records.sort((a, b) => b.created_at - a.created_at).map((record) => {
|
|
88
|
-
return {
|
|
89
|
-
id: record.id,
|
|
90
|
-
input: record.input,
|
|
91
|
-
output: record.output,
|
|
92
|
-
agentName: record.agent_name,
|
|
93
|
-
metricName: record.metric_name,
|
|
94
|
-
result: JSON.parse(record.result),
|
|
95
|
-
instructions: record.instructions,
|
|
96
|
-
testInfo: record.test_info ? JSON.parse(record.test_info) : null,
|
|
97
|
-
globalRunId: record.global_run_id,
|
|
98
|
-
runId: record.run_id,
|
|
99
|
-
createdAt: new Date(record.created_at).toISOString()
|
|
100
|
-
};
|
|
101
|
-
});
|
|
102
|
-
const page = options.page || 0;
|
|
103
|
-
const perPage = options.perPage || 10;
|
|
104
|
-
const pagedEvals = evals.slice(page * perPage, (page + 1) * perPage);
|
|
105
|
-
return {
|
|
106
|
-
evals: pagedEvals,
|
|
107
|
-
total,
|
|
108
|
-
page,
|
|
109
|
-
perPage,
|
|
110
|
-
hasMore: total > (page + 1) * perPage
|
|
111
|
-
};
|
|
112
|
-
} catch (error) {
|
|
113
|
-
throw new MastraError(
|
|
114
|
-
{
|
|
115
|
-
id: "LANCE_STORE_GET_EVALS_FAILED",
|
|
116
|
-
domain: ErrorDomain.STORAGE,
|
|
117
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
118
|
-
details: { agentName: options.agentName ?? "" }
|
|
119
|
-
},
|
|
120
|
-
error
|
|
121
|
-
);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
};
|
|
125
11
|
function getPrimaryKeys(tableName) {
|
|
126
12
|
let primaryId = ["id"];
|
|
127
13
|
if (tableName === TABLE_WORKFLOW_SNAPSHOT) {
|
|
128
14
|
primaryId = ["workflow_name", "run_id"];
|
|
129
|
-
} else if (tableName === TABLE_EVALS) {
|
|
130
|
-
primaryId = ["agent_name", "metric_name", "run_id"];
|
|
131
15
|
}
|
|
132
16
|
return primaryId;
|
|
133
17
|
}
|
|
@@ -247,6 +131,10 @@ var StoreMemoryLance = class extends MemoryStorage {
|
|
|
247
131
|
this.client = client;
|
|
248
132
|
this.operations = operations;
|
|
249
133
|
}
|
|
134
|
+
// Utility to escape single quotes in SQL strings
|
|
135
|
+
escapeSql(str) {
|
|
136
|
+
return str.replace(/'/g, "''");
|
|
137
|
+
}
|
|
250
138
|
async getThreadById({ threadId }) {
|
|
251
139
|
try {
|
|
252
140
|
const thread = await this.operations.load({ tableName: TABLE_THREADS, keys: { id: threadId } });
|
|
@@ -269,26 +157,6 @@ var StoreMemoryLance = class extends MemoryStorage {
|
|
|
269
157
|
);
|
|
270
158
|
}
|
|
271
159
|
}
|
|
272
|
-
async getThreadsByResourceId({ resourceId }) {
|
|
273
|
-
try {
|
|
274
|
-
const table = await this.client.openTable(TABLE_THREADS);
|
|
275
|
-
const query = table.query().where(`\`resourceId\` = '${resourceId}'`);
|
|
276
|
-
const records = await query.toArray();
|
|
277
|
-
return processResultWithTypeConversion(
|
|
278
|
-
records,
|
|
279
|
-
await getTableSchema({ tableName: TABLE_THREADS, client: this.client })
|
|
280
|
-
);
|
|
281
|
-
} catch (error) {
|
|
282
|
-
throw new MastraError(
|
|
283
|
-
{
|
|
284
|
-
id: "LANCE_STORE_GET_THREADS_BY_RESOURCE_ID_FAILED",
|
|
285
|
-
domain: ErrorDomain.STORAGE,
|
|
286
|
-
category: ErrorCategory.THIRD_PARTY
|
|
287
|
-
},
|
|
288
|
-
error
|
|
289
|
-
);
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
160
|
/**
|
|
293
161
|
* Saves a thread to the database. This function doesn't overwrite existing threads.
|
|
294
162
|
* @param thread - The thread to save
|
|
@@ -393,100 +261,174 @@ var StoreMemoryLance = class extends MemoryStorage {
|
|
|
393
261
|
})() : message.content
|
|
394
262
|
};
|
|
395
263
|
}
|
|
396
|
-
async
|
|
397
|
-
|
|
398
|
-
resourceId,
|
|
399
|
-
selectBy,
|
|
400
|
-
format,
|
|
401
|
-
threadConfig
|
|
402
|
-
}) {
|
|
264
|
+
async listMessagesById({ messageIds }) {
|
|
265
|
+
if (messageIds.length === 0) return { messages: [] };
|
|
403
266
|
try {
|
|
404
|
-
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
405
|
-
if (threadConfig) {
|
|
406
|
-
throw new Error("ThreadConfig is not supported by LanceDB storage");
|
|
407
|
-
}
|
|
408
|
-
const limit = resolveMessageLimit({ last: selectBy?.last, defaultLimit: Number.MAX_SAFE_INTEGER });
|
|
409
267
|
const table = await this.client.openTable(TABLE_MESSAGES);
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
const threadIds = [...new Set(selectBy.include.map((item) => item.threadId))];
|
|
413
|
-
for (const threadId2 of threadIds) {
|
|
414
|
-
const threadQuery = table.query().where(`thread_id = '${threadId2}'`);
|
|
415
|
-
let threadRecords = await threadQuery.toArray();
|
|
416
|
-
allRecords.push(...threadRecords);
|
|
417
|
-
}
|
|
418
|
-
} else {
|
|
419
|
-
let query = table.query().where(`\`thread_id\` = '${threadId}'`);
|
|
420
|
-
allRecords = await query.toArray();
|
|
421
|
-
}
|
|
422
|
-
allRecords.sort((a, b) => {
|
|
423
|
-
const dateA = new Date(a.createdAt).getTime();
|
|
424
|
-
const dateB = new Date(b.createdAt).getTime();
|
|
425
|
-
return dateA - dateB;
|
|
426
|
-
});
|
|
427
|
-
if (selectBy?.include && selectBy.include.length > 0) {
|
|
428
|
-
allRecords = this.processMessagesWithContext(allRecords, selectBy.include);
|
|
429
|
-
}
|
|
430
|
-
if (limit !== Number.MAX_SAFE_INTEGER) {
|
|
431
|
-
allRecords = allRecords.slice(-limit);
|
|
432
|
-
}
|
|
268
|
+
const quotedIds = messageIds.map((id) => `'${id}'`).join(", ");
|
|
269
|
+
const allRecords = await table.query().where(`id IN (${quotedIds})`).toArray();
|
|
433
270
|
const messages = processResultWithTypeConversion(
|
|
434
271
|
allRecords,
|
|
435
272
|
await getTableSchema({ tableName: TABLE_MESSAGES, client: this.client })
|
|
436
273
|
);
|
|
437
|
-
const list = new MessageList(
|
|
438
|
-
|
|
439
|
-
|
|
274
|
+
const list = new MessageList().add(
|
|
275
|
+
messages.map(this.normalizeMessage),
|
|
276
|
+
"memory"
|
|
277
|
+
);
|
|
278
|
+
return { messages: list.get.all.db() };
|
|
440
279
|
} catch (error) {
|
|
441
280
|
throw new MastraError(
|
|
442
281
|
{
|
|
443
|
-
id: "
|
|
282
|
+
id: "LANCE_STORE_LIST_MESSAGES_BY_ID_FAILED",
|
|
444
283
|
domain: ErrorDomain.STORAGE,
|
|
445
284
|
category: ErrorCategory.THIRD_PARTY,
|
|
446
285
|
details: {
|
|
447
|
-
|
|
448
|
-
resourceId: resourceId ?? ""
|
|
286
|
+
messageIds: JSON.stringify(messageIds)
|
|
449
287
|
}
|
|
450
288
|
},
|
|
451
289
|
error
|
|
452
290
|
);
|
|
453
291
|
}
|
|
454
292
|
}
|
|
455
|
-
async
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
293
|
+
async listMessages(args) {
|
|
294
|
+
const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
|
|
295
|
+
if (!threadId.trim()) {
|
|
296
|
+
throw new MastraError(
|
|
297
|
+
{
|
|
298
|
+
id: "STORAGE_LANCE_LIST_MESSAGES_INVALID_THREAD_ID",
|
|
299
|
+
domain: ErrorDomain.STORAGE,
|
|
300
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
301
|
+
details: { threadId }
|
|
302
|
+
},
|
|
303
|
+
new Error("threadId must be a non-empty string")
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
const perPage = normalizePerPage(perPageInput, 40);
|
|
307
|
+
const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
460
308
|
try {
|
|
309
|
+
if (page < 0) {
|
|
310
|
+
throw new MastraError(
|
|
311
|
+
{
|
|
312
|
+
id: "STORAGE_LANCE_LIST_MESSAGES_INVALID_PAGE",
|
|
313
|
+
domain: ErrorDomain.STORAGE,
|
|
314
|
+
category: ErrorCategory.USER,
|
|
315
|
+
details: { page }
|
|
316
|
+
},
|
|
317
|
+
new Error("page must be >= 0")
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
const { field, direction } = this.parseOrderBy(orderBy, "ASC");
|
|
461
321
|
const table = await this.client.openTable(TABLE_MESSAGES);
|
|
462
|
-
const
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
322
|
+
const conditions = [`thread_id = '${this.escapeSql(threadId)}'`];
|
|
323
|
+
if (resourceId) {
|
|
324
|
+
conditions.push(`\`resourceId\` = '${this.escapeSql(resourceId)}'`);
|
|
325
|
+
}
|
|
326
|
+
if (filter?.dateRange?.start) {
|
|
327
|
+
const startTime = filter.dateRange.start instanceof Date ? filter.dateRange.start.getTime() : new Date(filter.dateRange.start).getTime();
|
|
328
|
+
conditions.push(`\`createdAt\` >= ${startTime}`);
|
|
329
|
+
}
|
|
330
|
+
if (filter?.dateRange?.end) {
|
|
331
|
+
const endTime = filter.dateRange.end instanceof Date ? filter.dateRange.end.getTime() : new Date(filter.dateRange.end).getTime();
|
|
332
|
+
conditions.push(`\`createdAt\` <= ${endTime}`);
|
|
333
|
+
}
|
|
334
|
+
const whereClause = conditions.join(" AND ");
|
|
335
|
+
const total = await table.countRows(whereClause);
|
|
336
|
+
const query = table.query().where(whereClause);
|
|
337
|
+
let allRecords = await query.toArray();
|
|
338
|
+
allRecords.sort((a, b) => {
|
|
339
|
+
const aValue = field === "createdAt" ? a.createdAt : a[field];
|
|
340
|
+
const bValue = field === "createdAt" ? b.createdAt : b[field];
|
|
341
|
+
if (aValue == null && bValue == null) return 0;
|
|
342
|
+
if (aValue == null) return direction === "ASC" ? -1 : 1;
|
|
343
|
+
if (bValue == null) return direction === "ASC" ? 1 : -1;
|
|
344
|
+
if (typeof aValue === "string" && typeof bValue === "string") {
|
|
345
|
+
return direction === "ASC" ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue);
|
|
346
|
+
}
|
|
347
|
+
return direction === "ASC" ? aValue - bValue : bValue - aValue;
|
|
348
|
+
});
|
|
349
|
+
const paginatedRecords = allRecords.slice(offset, offset + perPage);
|
|
350
|
+
const messages = paginatedRecords.map((row) => this.normalizeMessage(row));
|
|
351
|
+
if (total === 0 && messages.length === 0 && (!include || include.length === 0)) {
|
|
352
|
+
return {
|
|
353
|
+
messages: [],
|
|
354
|
+
total: 0,
|
|
355
|
+
page,
|
|
356
|
+
perPage: perPageForResponse,
|
|
357
|
+
hasMore: false
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
const messageIds = new Set(messages.map((m) => m.id));
|
|
361
|
+
if (include && include.length > 0) {
|
|
362
|
+
const threadIds = [...new Set(include.map((item) => item.threadId || threadId))];
|
|
363
|
+
const allThreadMessages = [];
|
|
364
|
+
for (const tid of threadIds) {
|
|
365
|
+
const threadQuery = table.query().where(`thread_id = '${tid}'`);
|
|
366
|
+
let threadRecords = await threadQuery.toArray();
|
|
367
|
+
allThreadMessages.push(...threadRecords);
|
|
368
|
+
}
|
|
369
|
+
allThreadMessages.sort((a, b) => a.createdAt - b.createdAt);
|
|
370
|
+
const contextMessages = this.processMessagesWithContext(allThreadMessages, include);
|
|
371
|
+
const includedMessages = contextMessages.map((row) => this.normalizeMessage(row));
|
|
372
|
+
for (const includeMsg of includedMessages) {
|
|
373
|
+
if (!messageIds.has(includeMsg.id)) {
|
|
374
|
+
messages.push(includeMsg);
|
|
375
|
+
messageIds.add(includeMsg.id);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
const list = new MessageList().add(messages, "memory");
|
|
380
|
+
let finalMessages = list.get.all.db();
|
|
381
|
+
finalMessages = finalMessages.sort((a, b) => {
|
|
382
|
+
const aValue = field === "createdAt" ? new Date(a.createdAt).getTime() : a[field];
|
|
383
|
+
const bValue = field === "createdAt" ? new Date(b.createdAt).getTime() : b[field];
|
|
384
|
+
if (aValue == null && bValue == null) return 0;
|
|
385
|
+
if (aValue == null) return direction === "ASC" ? -1 : 1;
|
|
386
|
+
if (bValue == null) return direction === "ASC" ? 1 : -1;
|
|
387
|
+
if (typeof aValue === "string" && typeof bValue === "string") {
|
|
388
|
+
return direction === "ASC" ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue);
|
|
389
|
+
}
|
|
390
|
+
return direction === "ASC" ? aValue - bValue : bValue - aValue;
|
|
391
|
+
});
|
|
392
|
+
const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
|
|
393
|
+
const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
|
|
394
|
+
const fetchedAll = perPageInput === false || allThreadMessagesReturned;
|
|
395
|
+
const hasMore = !fetchedAll && offset + perPage < total;
|
|
396
|
+
return {
|
|
397
|
+
messages: finalMessages,
|
|
398
|
+
total,
|
|
399
|
+
page,
|
|
400
|
+
perPage: perPageForResponse,
|
|
401
|
+
hasMore
|
|
402
|
+
};
|
|
471
403
|
} catch (error) {
|
|
472
|
-
|
|
404
|
+
const mastraError = new MastraError(
|
|
473
405
|
{
|
|
474
|
-
id: "
|
|
406
|
+
id: "LANCE_STORE_LIST_MESSAGES_FAILED",
|
|
475
407
|
domain: ErrorDomain.STORAGE,
|
|
476
408
|
category: ErrorCategory.THIRD_PARTY,
|
|
477
409
|
details: {
|
|
478
|
-
|
|
410
|
+
threadId,
|
|
411
|
+
resourceId: resourceId ?? ""
|
|
479
412
|
}
|
|
480
413
|
},
|
|
481
414
|
error
|
|
482
415
|
);
|
|
416
|
+
this.logger?.error?.(mastraError.toString());
|
|
417
|
+
this.logger?.trackException?.(mastraError);
|
|
418
|
+
return {
|
|
419
|
+
messages: [],
|
|
420
|
+
total: 0,
|
|
421
|
+
page,
|
|
422
|
+
perPage: perPageForResponse,
|
|
423
|
+
hasMore: false
|
|
424
|
+
};
|
|
483
425
|
}
|
|
484
426
|
}
|
|
485
427
|
async saveMessages(args) {
|
|
486
428
|
try {
|
|
487
|
-
const { messages
|
|
429
|
+
const { messages } = args;
|
|
488
430
|
if (messages.length === 0) {
|
|
489
|
-
return [];
|
|
431
|
+
return { messages: [] };
|
|
490
432
|
}
|
|
491
433
|
const threadId = messages[0]?.threadId;
|
|
492
434
|
if (!threadId) {
|
|
@@ -522,8 +464,7 @@ var StoreMemoryLance = class extends MemoryStorage {
|
|
|
522
464
|
const updateRecord = { id: threadId, updatedAt: currentTime };
|
|
523
465
|
await threadsTable.mergeInsert("id").whenMatchedUpdateAll().whenNotMatchedInsertAll().execute([updateRecord]);
|
|
524
466
|
const list = new MessageList().add(messages, "memory");
|
|
525
|
-
|
|
526
|
-
return list.get.all.v1();
|
|
467
|
+
return { messages: list.get.all.db() };
|
|
527
468
|
} catch (error) {
|
|
528
469
|
throw new MastraError(
|
|
529
470
|
{
|
|
@@ -535,32 +476,54 @@ var StoreMemoryLance = class extends MemoryStorage {
|
|
|
535
476
|
);
|
|
536
477
|
}
|
|
537
478
|
}
|
|
538
|
-
async
|
|
479
|
+
async listThreadsByResourceId(args) {
|
|
539
480
|
try {
|
|
540
|
-
const { resourceId, page = 0, perPage
|
|
541
|
-
const
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
481
|
+
const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
|
|
482
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
483
|
+
if (page < 0) {
|
|
484
|
+
throw new MastraError(
|
|
485
|
+
{
|
|
486
|
+
id: "STORAGE_LANCE_LIST_THREADS_BY_RESOURCE_ID_INVALID_PAGE",
|
|
487
|
+
domain: ErrorDomain.STORAGE,
|
|
488
|
+
category: ErrorCategory.USER,
|
|
489
|
+
details: { page }
|
|
490
|
+
},
|
|
491
|
+
new Error("page must be >= 0")
|
|
492
|
+
);
|
|
548
493
|
}
|
|
494
|
+
const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
495
|
+
const { field, direction } = this.parseOrderBy(orderBy);
|
|
496
|
+
const table = await this.client.openTable(TABLE_THREADS);
|
|
497
|
+
const total = await table.countRows(`\`resourceId\` = '${this.escapeSql(resourceId)}'`);
|
|
498
|
+
const query = table.query().where(`\`resourceId\` = '${this.escapeSql(resourceId)}'`);
|
|
549
499
|
const records = await query.toArray();
|
|
550
|
-
records.sort((a, b) =>
|
|
500
|
+
records.sort((a, b) => {
|
|
501
|
+
const aValue = ["createdAt", "updatedAt"].includes(field) ? new Date(a[field]).getTime() : a[field];
|
|
502
|
+
const bValue = ["createdAt", "updatedAt"].includes(field) ? new Date(b[field]).getTime() : b[field];
|
|
503
|
+
if (aValue == null && bValue == null) return 0;
|
|
504
|
+
if (aValue == null) return direction === "ASC" ? -1 : 1;
|
|
505
|
+
if (bValue == null) return direction === "ASC" ? 1 : -1;
|
|
506
|
+
if (typeof aValue === "string" && typeof bValue === "string") {
|
|
507
|
+
return direction === "ASC" ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue);
|
|
508
|
+
}
|
|
509
|
+
return direction === "ASC" ? aValue - bValue : bValue - aValue;
|
|
510
|
+
});
|
|
511
|
+
const paginatedRecords = records.slice(offset, offset + perPage);
|
|
551
512
|
const schema = await getTableSchema({ tableName: TABLE_THREADS, client: this.client });
|
|
552
|
-
const threads =
|
|
513
|
+
const threads = paginatedRecords.map(
|
|
514
|
+
(record) => processResultWithTypeConversion(record, schema)
|
|
515
|
+
);
|
|
553
516
|
return {
|
|
554
517
|
threads,
|
|
555
518
|
total,
|
|
556
519
|
page,
|
|
557
|
-
perPage,
|
|
558
|
-
hasMore:
|
|
520
|
+
perPage: perPageForResponse,
|
|
521
|
+
hasMore: offset + perPage < total
|
|
559
522
|
};
|
|
560
523
|
} catch (error) {
|
|
561
524
|
throw new MastraError(
|
|
562
525
|
{
|
|
563
|
-
id: "
|
|
526
|
+
id: "LANCE_STORE_LIST_THREADS_BY_RESOURCE_ID_FAILED",
|
|
564
527
|
domain: ErrorDomain.STORAGE,
|
|
565
528
|
category: ErrorCategory.THIRD_PARTY
|
|
566
529
|
},
|
|
@@ -616,132 +579,8 @@ var StoreMemoryLance = class extends MemoryStorage {
|
|
|
616
579
|
});
|
|
617
580
|
return Array.from(allIndices).sort((a, b) => a - b).map((index) => records[index]);
|
|
618
581
|
}
|
|
619
|
-
async getMessagesPaginated(args) {
|
|
620
|
-
const { threadId, resourceId, selectBy, format = "v1" } = args;
|
|
621
|
-
const page = selectBy?.pagination?.page ?? 0;
|
|
622
|
-
const perPage = selectBy?.pagination?.perPage ?? 10;
|
|
623
|
-
try {
|
|
624
|
-
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
625
|
-
const dateRange = selectBy?.pagination?.dateRange;
|
|
626
|
-
const fromDate = dateRange?.start;
|
|
627
|
-
const toDate = dateRange?.end;
|
|
628
|
-
const table = await this.client.openTable(TABLE_MESSAGES);
|
|
629
|
-
const messages = [];
|
|
630
|
-
if (selectBy?.include && Array.isArray(selectBy.include)) {
|
|
631
|
-
const threadIds = [...new Set(selectBy.include.map((item) => item.threadId))];
|
|
632
|
-
const allThreadMessages = [];
|
|
633
|
-
for (const threadId2 of threadIds) {
|
|
634
|
-
const threadQuery = table.query().where(`thread_id = '${threadId2}'`);
|
|
635
|
-
let threadRecords = await threadQuery.toArray();
|
|
636
|
-
if (fromDate) threadRecords = threadRecords.filter((m) => m.createdAt >= fromDate.getTime());
|
|
637
|
-
if (toDate) threadRecords = threadRecords.filter((m) => m.createdAt <= toDate.getTime());
|
|
638
|
-
allThreadMessages.push(...threadRecords);
|
|
639
|
-
}
|
|
640
|
-
allThreadMessages.sort((a, b) => a.createdAt - b.createdAt);
|
|
641
|
-
const contextMessages = this.processMessagesWithContext(allThreadMessages, selectBy.include);
|
|
642
|
-
messages.push(...contextMessages);
|
|
643
|
-
}
|
|
644
|
-
const conditions = [`thread_id = '${threadId}'`];
|
|
645
|
-
if (resourceId) {
|
|
646
|
-
conditions.push(`\`resourceId\` = '${resourceId}'`);
|
|
647
|
-
}
|
|
648
|
-
if (fromDate) {
|
|
649
|
-
conditions.push(`\`createdAt\` >= ${fromDate.getTime()}`);
|
|
650
|
-
}
|
|
651
|
-
if (toDate) {
|
|
652
|
-
conditions.push(`\`createdAt\` <= ${toDate.getTime()}`);
|
|
653
|
-
}
|
|
654
|
-
let total = 0;
|
|
655
|
-
if (conditions.length > 0) {
|
|
656
|
-
total = await table.countRows(conditions.join(" AND "));
|
|
657
|
-
} else {
|
|
658
|
-
total = await table.countRows();
|
|
659
|
-
}
|
|
660
|
-
if (total === 0 && messages.length === 0) {
|
|
661
|
-
return {
|
|
662
|
-
messages: [],
|
|
663
|
-
total: 0,
|
|
664
|
-
page,
|
|
665
|
-
perPage,
|
|
666
|
-
hasMore: false
|
|
667
|
-
};
|
|
668
|
-
}
|
|
669
|
-
const excludeIds = messages.map((m) => m.id);
|
|
670
|
-
let selectedMessages = [];
|
|
671
|
-
if (selectBy?.last && selectBy.last > 0) {
|
|
672
|
-
const query = table.query();
|
|
673
|
-
if (conditions.length > 0) {
|
|
674
|
-
query.where(conditions.join(" AND "));
|
|
675
|
-
}
|
|
676
|
-
let records = await query.toArray();
|
|
677
|
-
records = records.sort((a, b) => a.createdAt - b.createdAt);
|
|
678
|
-
if (excludeIds.length > 0) {
|
|
679
|
-
records = records.filter((m) => !excludeIds.includes(m.id));
|
|
680
|
-
}
|
|
681
|
-
selectedMessages = records.slice(-selectBy.last);
|
|
682
|
-
} else {
|
|
683
|
-
const query = table.query();
|
|
684
|
-
if (conditions.length > 0) {
|
|
685
|
-
query.where(conditions.join(" AND "));
|
|
686
|
-
}
|
|
687
|
-
let records = await query.toArray();
|
|
688
|
-
records = records.sort((a, b) => a.createdAt - b.createdAt);
|
|
689
|
-
if (excludeIds.length > 0) {
|
|
690
|
-
records = records.filter((m) => !excludeIds.includes(m.id));
|
|
691
|
-
}
|
|
692
|
-
selectedMessages = records.slice(page * perPage, (page + 1) * perPage);
|
|
693
|
-
}
|
|
694
|
-
const allMessages = [...messages, ...selectedMessages];
|
|
695
|
-
const seen = /* @__PURE__ */ new Set();
|
|
696
|
-
const dedupedMessages = allMessages.filter((m) => {
|
|
697
|
-
const key = `${m.id}:${m.thread_id}`;
|
|
698
|
-
if (seen.has(key)) return false;
|
|
699
|
-
seen.add(key);
|
|
700
|
-
return true;
|
|
701
|
-
});
|
|
702
|
-
const formattedMessages = dedupedMessages.map((msg) => {
|
|
703
|
-
const { thread_id, ...rest } = msg;
|
|
704
|
-
return {
|
|
705
|
-
...rest,
|
|
706
|
-
threadId: thread_id,
|
|
707
|
-
content: typeof msg.content === "string" ? (() => {
|
|
708
|
-
try {
|
|
709
|
-
return JSON.parse(msg.content);
|
|
710
|
-
} catch {
|
|
711
|
-
return msg.content;
|
|
712
|
-
}
|
|
713
|
-
})() : msg.content
|
|
714
|
-
};
|
|
715
|
-
});
|
|
716
|
-
const list = new MessageList().add(formattedMessages, "memory");
|
|
717
|
-
return {
|
|
718
|
-
messages: format === "v2" ? list.get.all.v2() : list.get.all.v1(),
|
|
719
|
-
total,
|
|
720
|
-
// Total should be the count of messages matching the filters
|
|
721
|
-
page,
|
|
722
|
-
perPage,
|
|
723
|
-
hasMore: total > (page + 1) * perPage
|
|
724
|
-
};
|
|
725
|
-
} catch (error) {
|
|
726
|
-
const mastraError = new MastraError(
|
|
727
|
-
{
|
|
728
|
-
id: "LANCE_STORE_GET_MESSAGES_PAGINATED_FAILED",
|
|
729
|
-
domain: ErrorDomain.STORAGE,
|
|
730
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
731
|
-
details: {
|
|
732
|
-
threadId,
|
|
733
|
-
resourceId: resourceId ?? ""
|
|
734
|
-
}
|
|
735
|
-
},
|
|
736
|
-
error
|
|
737
|
-
);
|
|
738
|
-
this.logger?.trackException?.(mastraError);
|
|
739
|
-
this.logger?.error?.(mastraError.toString());
|
|
740
|
-
return { messages: [], total: 0, page, perPage, hasMore: false };
|
|
741
|
-
}
|
|
742
|
-
}
|
|
743
582
|
/**
|
|
744
|
-
* Parse message data from LanceDB record format to
|
|
583
|
+
* Parse message data from LanceDB record format to MastraDBMessage format
|
|
745
584
|
*/
|
|
746
585
|
parseMessageData(data) {
|
|
747
586
|
const { thread_id, ...rest } = data;
|
|
@@ -1464,7 +1303,7 @@ var StoreScoresLance = class extends ScoresStorage {
|
|
|
1464
1303
|
);
|
|
1465
1304
|
}
|
|
1466
1305
|
}
|
|
1467
|
-
async
|
|
1306
|
+
async listScoresByScorerId({
|
|
1468
1307
|
scorerId,
|
|
1469
1308
|
pagination,
|
|
1470
1309
|
entityId,
|
|
@@ -1472,9 +1311,10 @@ var StoreScoresLance = class extends ScoresStorage {
|
|
|
1472
1311
|
source
|
|
1473
1312
|
}) {
|
|
1474
1313
|
try {
|
|
1314
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1315
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
1316
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1475
1317
|
const table = await this.client.openTable(TABLE_SCORERS);
|
|
1476
|
-
const { page = 0, perPage = 10 } = pagination || {};
|
|
1477
|
-
const offset = page * perPage;
|
|
1478
1318
|
let query = table.query().where(`\`scorerId\` = '${scorerId}'`);
|
|
1479
1319
|
if (source) {
|
|
1480
1320
|
query = query.where(`\`source\` = '${source}'`);
|
|
@@ -1485,23 +1325,32 @@ var StoreScoresLance = class extends ScoresStorage {
|
|
|
1485
1325
|
if (entityType) {
|
|
1486
1326
|
query = query.where(`\`entityType\` = '${entityType}'`);
|
|
1487
1327
|
}
|
|
1488
|
-
query = query.limit(perPage);
|
|
1489
|
-
if (offset > 0) query.offset(offset);
|
|
1490
|
-
const records = await query.toArray();
|
|
1491
|
-
const schema = await getTableSchema({ tableName: TABLE_SCORERS, client: this.client });
|
|
1492
|
-
const scores = processResultWithTypeConversion(records, schema);
|
|
1493
1328
|
let totalQuery = table.query().where(`\`scorerId\` = '${scorerId}'`);
|
|
1494
1329
|
if (source) {
|
|
1495
1330
|
totalQuery = totalQuery.where(`\`source\` = '${source}'`);
|
|
1496
1331
|
}
|
|
1332
|
+
if (entityId) {
|
|
1333
|
+
totalQuery = totalQuery.where(`\`entityId\` = '${entityId}'`);
|
|
1334
|
+
}
|
|
1335
|
+
if (entityType) {
|
|
1336
|
+
totalQuery = totalQuery.where(`\`entityType\` = '${entityType}'`);
|
|
1337
|
+
}
|
|
1497
1338
|
const allRecords = await totalQuery.toArray();
|
|
1498
1339
|
const total = allRecords.length;
|
|
1340
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1341
|
+
if (perPageInput !== false) {
|
|
1342
|
+
query = query.limit(perPage);
|
|
1343
|
+
if (start > 0) query = query.offset(start);
|
|
1344
|
+
}
|
|
1345
|
+
const records = await query.toArray();
|
|
1346
|
+
const schema = await getTableSchema({ tableName: TABLE_SCORERS, client: this.client });
|
|
1347
|
+
const scores = processResultWithTypeConversion(records, schema);
|
|
1499
1348
|
return {
|
|
1500
1349
|
pagination: {
|
|
1501
1350
|
page,
|
|
1502
|
-
perPage,
|
|
1351
|
+
perPage: perPageForResponse,
|
|
1503
1352
|
total,
|
|
1504
|
-
hasMore:
|
|
1353
|
+
hasMore: end < total
|
|
1505
1354
|
},
|
|
1506
1355
|
scores
|
|
1507
1356
|
};
|
|
@@ -1518,27 +1367,32 @@ var StoreScoresLance = class extends ScoresStorage {
|
|
|
1518
1367
|
);
|
|
1519
1368
|
}
|
|
1520
1369
|
}
|
|
1521
|
-
async
|
|
1370
|
+
async listScoresByRunId({
|
|
1522
1371
|
runId,
|
|
1523
1372
|
pagination
|
|
1524
1373
|
}) {
|
|
1525
1374
|
try {
|
|
1375
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1376
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
1377
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1526
1378
|
const table = await this.client.openTable(TABLE_SCORERS);
|
|
1527
|
-
const
|
|
1528
|
-
const
|
|
1529
|
-
const
|
|
1530
|
-
|
|
1379
|
+
const allRecords = await table.query().where(`\`runId\` = '${runId}'`).toArray();
|
|
1380
|
+
const total = allRecords.length;
|
|
1381
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1382
|
+
let query = table.query().where(`\`runId\` = '${runId}'`);
|
|
1383
|
+
if (perPageInput !== false) {
|
|
1384
|
+
query = query.limit(perPage);
|
|
1385
|
+
if (start > 0) query = query.offset(start);
|
|
1386
|
+
}
|
|
1531
1387
|
const records = await query.toArray();
|
|
1532
1388
|
const schema = await getTableSchema({ tableName: TABLE_SCORERS, client: this.client });
|
|
1533
1389
|
const scores = processResultWithTypeConversion(records, schema);
|
|
1534
|
-
const allRecords = await table.query().where(`\`runId\` = '${runId}'`).toArray();
|
|
1535
|
-
const total = allRecords.length;
|
|
1536
1390
|
return {
|
|
1537
1391
|
pagination: {
|
|
1538
1392
|
page,
|
|
1539
|
-
perPage,
|
|
1393
|
+
perPage: perPageForResponse,
|
|
1540
1394
|
total,
|
|
1541
|
-
hasMore:
|
|
1395
|
+
hasMore: end < total
|
|
1542
1396
|
},
|
|
1543
1397
|
scores
|
|
1544
1398
|
};
|
|
@@ -1555,28 +1409,33 @@ var StoreScoresLance = class extends ScoresStorage {
|
|
|
1555
1409
|
);
|
|
1556
1410
|
}
|
|
1557
1411
|
}
|
|
1558
|
-
async
|
|
1412
|
+
async listScoresByEntityId({
|
|
1559
1413
|
entityId,
|
|
1560
1414
|
entityType,
|
|
1561
1415
|
pagination
|
|
1562
1416
|
}) {
|
|
1563
1417
|
try {
|
|
1418
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1419
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
1420
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1564
1421
|
const table = await this.client.openTable(TABLE_SCORERS);
|
|
1565
|
-
const
|
|
1566
|
-
const
|
|
1567
|
-
const
|
|
1568
|
-
|
|
1422
|
+
const allRecords = await table.query().where(`\`entityId\` = '${entityId}' AND \`entityType\` = '${entityType}'`).toArray();
|
|
1423
|
+
const total = allRecords.length;
|
|
1424
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1425
|
+
let query = table.query().where(`\`entityId\` = '${entityId}' AND \`entityType\` = '${entityType}'`);
|
|
1426
|
+
if (perPageInput !== false) {
|
|
1427
|
+
query = query.limit(perPage);
|
|
1428
|
+
if (start > 0) query = query.offset(start);
|
|
1429
|
+
}
|
|
1569
1430
|
const records = await query.toArray();
|
|
1570
1431
|
const schema = await getTableSchema({ tableName: TABLE_SCORERS, client: this.client });
|
|
1571
1432
|
const scores = processResultWithTypeConversion(records, schema);
|
|
1572
|
-
const allRecords = await table.query().where(`\`entityId\` = '${entityId}' AND \`entityType\` = '${entityType}'`).toArray();
|
|
1573
|
-
const total = allRecords.length;
|
|
1574
1433
|
return {
|
|
1575
1434
|
pagination: {
|
|
1576
1435
|
page,
|
|
1577
|
-
perPage,
|
|
1436
|
+
perPage: perPageForResponse,
|
|
1578
1437
|
total,
|
|
1579
|
-
hasMore:
|
|
1438
|
+
hasMore: end < total
|
|
1580
1439
|
},
|
|
1581
1440
|
scores
|
|
1582
1441
|
};
|
|
@@ -1593,28 +1452,33 @@ var StoreScoresLance = class extends ScoresStorage {
|
|
|
1593
1452
|
);
|
|
1594
1453
|
}
|
|
1595
1454
|
}
|
|
1596
|
-
async
|
|
1455
|
+
async listScoresBySpan({
|
|
1597
1456
|
traceId,
|
|
1598
1457
|
spanId,
|
|
1599
1458
|
pagination
|
|
1600
1459
|
}) {
|
|
1601
1460
|
try {
|
|
1461
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1462
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
1463
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1602
1464
|
const table = await this.client.openTable(TABLE_SCORERS);
|
|
1603
|
-
const
|
|
1604
|
-
const
|
|
1605
|
-
const
|
|
1606
|
-
|
|
1465
|
+
const allRecords = await table.query().where(`\`traceId\` = '${traceId}' AND \`spanId\` = '${spanId}'`).toArray();
|
|
1466
|
+
const total = allRecords.length;
|
|
1467
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1468
|
+
let query = table.query().where(`\`traceId\` = '${traceId}' AND \`spanId\` = '${spanId}'`);
|
|
1469
|
+
if (perPageInput !== false) {
|
|
1470
|
+
query = query.limit(perPage);
|
|
1471
|
+
if (start > 0) query = query.offset(start);
|
|
1472
|
+
}
|
|
1607
1473
|
const records = await query.toArray();
|
|
1608
1474
|
const schema = await getTableSchema({ tableName: TABLE_SCORERS, client: this.client });
|
|
1609
1475
|
const scores = processResultWithTypeConversion(records, schema);
|
|
1610
|
-
const allRecords = await table.query().where(`\`traceId\` = '${traceId}' AND \`spanId\` = '${spanId}'`).toArray();
|
|
1611
|
-
const total = allRecords.length;
|
|
1612
1476
|
return {
|
|
1613
1477
|
pagination: {
|
|
1614
1478
|
page,
|
|
1615
|
-
perPage,
|
|
1479
|
+
perPage: perPageForResponse,
|
|
1616
1480
|
total,
|
|
1617
|
-
hasMore:
|
|
1481
|
+
hasMore: end < total
|
|
1618
1482
|
},
|
|
1619
1483
|
scores
|
|
1620
1484
|
};
|
|
@@ -1632,198 +1496,6 @@ var StoreScoresLance = class extends ScoresStorage {
|
|
|
1632
1496
|
}
|
|
1633
1497
|
}
|
|
1634
1498
|
};
|
|
1635
|
-
var StoreTracesLance = class extends TracesStorage {
|
|
1636
|
-
client;
|
|
1637
|
-
operations;
|
|
1638
|
-
constructor({ client, operations }) {
|
|
1639
|
-
super();
|
|
1640
|
-
this.client = client;
|
|
1641
|
-
this.operations = operations;
|
|
1642
|
-
}
|
|
1643
|
-
async saveTrace({ trace }) {
|
|
1644
|
-
try {
|
|
1645
|
-
const table = await this.client.openTable(TABLE_TRACES);
|
|
1646
|
-
const record = {
|
|
1647
|
-
...trace,
|
|
1648
|
-
attributes: JSON.stringify(trace.attributes),
|
|
1649
|
-
status: JSON.stringify(trace.status),
|
|
1650
|
-
events: JSON.stringify(trace.events),
|
|
1651
|
-
links: JSON.stringify(trace.links),
|
|
1652
|
-
other: JSON.stringify(trace.other)
|
|
1653
|
-
};
|
|
1654
|
-
await table.add([record], { mode: "append" });
|
|
1655
|
-
return trace;
|
|
1656
|
-
} catch (error) {
|
|
1657
|
-
throw new MastraError(
|
|
1658
|
-
{
|
|
1659
|
-
id: "LANCE_STORE_SAVE_TRACE_FAILED",
|
|
1660
|
-
domain: ErrorDomain.STORAGE,
|
|
1661
|
-
category: ErrorCategory.THIRD_PARTY
|
|
1662
|
-
},
|
|
1663
|
-
error
|
|
1664
|
-
);
|
|
1665
|
-
}
|
|
1666
|
-
}
|
|
1667
|
-
async getTraceById({ traceId }) {
|
|
1668
|
-
try {
|
|
1669
|
-
const table = await this.client.openTable(TABLE_TRACES);
|
|
1670
|
-
const query = table.query().where(`id = '${traceId}'`);
|
|
1671
|
-
const records = await query.toArray();
|
|
1672
|
-
return records[0];
|
|
1673
|
-
} catch (error) {
|
|
1674
|
-
throw new MastraError(
|
|
1675
|
-
{
|
|
1676
|
-
id: "LANCE_STORE_GET_TRACE_BY_ID_FAILED",
|
|
1677
|
-
domain: ErrorDomain.STORAGE,
|
|
1678
|
-
category: ErrorCategory.THIRD_PARTY
|
|
1679
|
-
},
|
|
1680
|
-
error
|
|
1681
|
-
);
|
|
1682
|
-
}
|
|
1683
|
-
}
|
|
1684
|
-
async getTraces({
|
|
1685
|
-
name,
|
|
1686
|
-
scope,
|
|
1687
|
-
page = 1,
|
|
1688
|
-
perPage = 10,
|
|
1689
|
-
attributes
|
|
1690
|
-
}) {
|
|
1691
|
-
try {
|
|
1692
|
-
const table = await this.client.openTable(TABLE_TRACES);
|
|
1693
|
-
const query = table.query();
|
|
1694
|
-
if (name) {
|
|
1695
|
-
query.where(`name = '${name}'`);
|
|
1696
|
-
}
|
|
1697
|
-
if (scope) {
|
|
1698
|
-
query.where(`scope = '${scope}'`);
|
|
1699
|
-
}
|
|
1700
|
-
if (attributes) {
|
|
1701
|
-
query.where(`attributes = '${JSON.stringify(attributes)}'`);
|
|
1702
|
-
}
|
|
1703
|
-
const offset = (page - 1) * perPage;
|
|
1704
|
-
query.limit(perPage);
|
|
1705
|
-
if (offset > 0) {
|
|
1706
|
-
query.offset(offset);
|
|
1707
|
-
}
|
|
1708
|
-
const records = await query.toArray();
|
|
1709
|
-
return records.map((record) => {
|
|
1710
|
-
const processed = {
|
|
1711
|
-
...record,
|
|
1712
|
-
attributes: record.attributes ? JSON.parse(record.attributes) : {},
|
|
1713
|
-
status: record.status ? JSON.parse(record.status) : {},
|
|
1714
|
-
events: record.events ? JSON.parse(record.events) : [],
|
|
1715
|
-
links: record.links ? JSON.parse(record.links) : [],
|
|
1716
|
-
other: record.other ? JSON.parse(record.other) : {},
|
|
1717
|
-
startTime: new Date(record.startTime),
|
|
1718
|
-
endTime: new Date(record.endTime),
|
|
1719
|
-
createdAt: new Date(record.createdAt)
|
|
1720
|
-
};
|
|
1721
|
-
if (processed.parentSpanId === null || processed.parentSpanId === void 0) {
|
|
1722
|
-
processed.parentSpanId = "";
|
|
1723
|
-
} else {
|
|
1724
|
-
processed.parentSpanId = String(processed.parentSpanId);
|
|
1725
|
-
}
|
|
1726
|
-
return processed;
|
|
1727
|
-
});
|
|
1728
|
-
} catch (error) {
|
|
1729
|
-
throw new MastraError(
|
|
1730
|
-
{
|
|
1731
|
-
id: "LANCE_STORE_GET_TRACES_FAILED",
|
|
1732
|
-
domain: ErrorDomain.STORAGE,
|
|
1733
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
1734
|
-
details: { name: name ?? "", scope: scope ?? "" }
|
|
1735
|
-
},
|
|
1736
|
-
error
|
|
1737
|
-
);
|
|
1738
|
-
}
|
|
1739
|
-
}
|
|
1740
|
-
async getTracesPaginated(args) {
|
|
1741
|
-
try {
|
|
1742
|
-
const table = await this.client.openTable(TABLE_TRACES);
|
|
1743
|
-
const query = table.query();
|
|
1744
|
-
const conditions = [];
|
|
1745
|
-
if (args.name) {
|
|
1746
|
-
conditions.push(`name = '${args.name}'`);
|
|
1747
|
-
}
|
|
1748
|
-
if (args.scope) {
|
|
1749
|
-
conditions.push(`scope = '${args.scope}'`);
|
|
1750
|
-
}
|
|
1751
|
-
if (args.attributes) {
|
|
1752
|
-
const attributesStr = JSON.stringify(args.attributes);
|
|
1753
|
-
conditions.push(`attributes LIKE '%${attributesStr.replace(/"/g, '\\"')}%'`);
|
|
1754
|
-
}
|
|
1755
|
-
if (args.dateRange?.start) {
|
|
1756
|
-
conditions.push(`\`createdAt\` >= ${args.dateRange.start.getTime()}`);
|
|
1757
|
-
}
|
|
1758
|
-
if (args.dateRange?.end) {
|
|
1759
|
-
conditions.push(`\`createdAt\` <= ${args.dateRange.end.getTime()}`);
|
|
1760
|
-
}
|
|
1761
|
-
if (conditions.length > 0) {
|
|
1762
|
-
const whereClause = conditions.join(" AND ");
|
|
1763
|
-
query.where(whereClause);
|
|
1764
|
-
}
|
|
1765
|
-
let total = 0;
|
|
1766
|
-
if (conditions.length > 0) {
|
|
1767
|
-
const countQuery = table.query().where(conditions.join(" AND "));
|
|
1768
|
-
const allRecords = await countQuery.toArray();
|
|
1769
|
-
total = allRecords.length;
|
|
1770
|
-
} else {
|
|
1771
|
-
total = await table.countRows();
|
|
1772
|
-
}
|
|
1773
|
-
const page = args.page || 0;
|
|
1774
|
-
const perPage = args.perPage || 10;
|
|
1775
|
-
const offset = page * perPage;
|
|
1776
|
-
query.limit(perPage);
|
|
1777
|
-
if (offset > 0) {
|
|
1778
|
-
query.offset(offset);
|
|
1779
|
-
}
|
|
1780
|
-
const records = await query.toArray();
|
|
1781
|
-
const traces = records.map((record) => {
|
|
1782
|
-
const processed = {
|
|
1783
|
-
...record,
|
|
1784
|
-
attributes: record.attributes ? JSON.parse(record.attributes) : {},
|
|
1785
|
-
status: record.status ? JSON.parse(record.status) : {},
|
|
1786
|
-
events: record.events ? JSON.parse(record.events) : [],
|
|
1787
|
-
links: record.links ? JSON.parse(record.links) : [],
|
|
1788
|
-
other: record.other ? JSON.parse(record.other) : {},
|
|
1789
|
-
startTime: new Date(record.startTime),
|
|
1790
|
-
endTime: new Date(record.endTime),
|
|
1791
|
-
createdAt: new Date(record.createdAt)
|
|
1792
|
-
};
|
|
1793
|
-
if (processed.parentSpanId === null || processed.parentSpanId === void 0) {
|
|
1794
|
-
processed.parentSpanId = "";
|
|
1795
|
-
} else {
|
|
1796
|
-
processed.parentSpanId = String(processed.parentSpanId);
|
|
1797
|
-
}
|
|
1798
|
-
return processed;
|
|
1799
|
-
});
|
|
1800
|
-
return {
|
|
1801
|
-
traces,
|
|
1802
|
-
total,
|
|
1803
|
-
page,
|
|
1804
|
-
perPage,
|
|
1805
|
-
hasMore: total > (page + 1) * perPage
|
|
1806
|
-
};
|
|
1807
|
-
} catch (error) {
|
|
1808
|
-
throw new MastraError(
|
|
1809
|
-
{
|
|
1810
|
-
id: "LANCE_STORE_GET_TRACES_PAGINATED_FAILED",
|
|
1811
|
-
domain: ErrorDomain.STORAGE,
|
|
1812
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
1813
|
-
details: { name: args.name ?? "", scope: args.scope ?? "" }
|
|
1814
|
-
},
|
|
1815
|
-
error
|
|
1816
|
-
);
|
|
1817
|
-
}
|
|
1818
|
-
}
|
|
1819
|
-
async batchTraceInsert({ records }) {
|
|
1820
|
-
this.logger.debug("Batch inserting traces", { count: records.length });
|
|
1821
|
-
await this.operations.batchInsert({
|
|
1822
|
-
tableName: TABLE_TRACES,
|
|
1823
|
-
records
|
|
1824
|
-
});
|
|
1825
|
-
}
|
|
1826
|
-
};
|
|
1827
1499
|
function parseWorkflowRun(row) {
|
|
1828
1500
|
let parsedSnapshot = row.snapshot;
|
|
1829
1501
|
if (typeof parsedSnapshot === "string") {
|
|
@@ -1853,7 +1525,7 @@ var StoreWorkflowsLance = class extends WorkflowsStorage {
|
|
|
1853
1525
|
// runId,
|
|
1854
1526
|
// stepId,
|
|
1855
1527
|
// result,
|
|
1856
|
-
//
|
|
1528
|
+
// requestContext,
|
|
1857
1529
|
}) {
|
|
1858
1530
|
throw new Error("Method not implemented.");
|
|
1859
1531
|
}
|
|
@@ -1947,7 +1619,7 @@ var StoreWorkflowsLance = class extends WorkflowsStorage {
|
|
|
1947
1619
|
);
|
|
1948
1620
|
}
|
|
1949
1621
|
}
|
|
1950
|
-
async
|
|
1622
|
+
async listWorkflowRuns(args) {
|
|
1951
1623
|
try {
|
|
1952
1624
|
const table = await this.client.openTable(TABLE_WORKFLOW_SNAPSHOT);
|
|
1953
1625
|
let query = table.query();
|
|
@@ -1971,11 +1643,22 @@ var StoreWorkflowsLance = class extends WorkflowsStorage {
|
|
|
1971
1643
|
} else {
|
|
1972
1644
|
total = await table.countRows();
|
|
1973
1645
|
}
|
|
1974
|
-
if (args?.
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1646
|
+
if (args?.perPage !== void 0 && args?.page !== void 0) {
|
|
1647
|
+
const normalizedPerPage = normalizePerPage(args.perPage, Number.MAX_SAFE_INTEGER);
|
|
1648
|
+
if (args.page < 0 || !Number.isInteger(args.page)) {
|
|
1649
|
+
throw new MastraError(
|
|
1650
|
+
{
|
|
1651
|
+
id: "LANCE_STORE_INVALID_PAGINATION_PARAMS",
|
|
1652
|
+
domain: ErrorDomain.STORAGE,
|
|
1653
|
+
category: ErrorCategory.USER,
|
|
1654
|
+
details: { page: args.page, perPage: args.perPage }
|
|
1655
|
+
},
|
|
1656
|
+
new Error(`Invalid pagination parameters: page=${args.page}, perPage=${args.perPage}`)
|
|
1657
|
+
);
|
|
1658
|
+
}
|
|
1659
|
+
const offset = args.page * normalizedPerPage;
|
|
1660
|
+
query.limit(normalizedPerPage);
|
|
1661
|
+
query.offset(offset);
|
|
1979
1662
|
}
|
|
1980
1663
|
const records = await query.toArray();
|
|
1981
1664
|
return {
|
|
@@ -1985,10 +1668,10 @@ var StoreWorkflowsLance = class extends WorkflowsStorage {
|
|
|
1985
1668
|
} catch (error) {
|
|
1986
1669
|
throw new MastraError(
|
|
1987
1670
|
{
|
|
1988
|
-
id: "
|
|
1671
|
+
id: "LANCE_STORE_LIST_WORKFLOW_RUNS_FAILED",
|
|
1989
1672
|
domain: ErrorDomain.STORAGE,
|
|
1990
1673
|
category: ErrorCategory.THIRD_PARTY,
|
|
1991
|
-
details: {
|
|
1674
|
+
details: { resourceId: args?.resourceId ?? "", workflowName: args?.workflowName ?? "" }
|
|
1992
1675
|
},
|
|
1993
1676
|
error
|
|
1994
1677
|
);
|
|
@@ -2030,10 +1713,8 @@ var LanceStorage = class _LanceStorage extends MastraStorage {
|
|
|
2030
1713
|
instance.stores = {
|
|
2031
1714
|
operations: new StoreOperationsLance({ client: instance.lanceClient }),
|
|
2032
1715
|
workflows: new StoreWorkflowsLance({ client: instance.lanceClient }),
|
|
2033
|
-
traces: new StoreTracesLance({ client: instance.lanceClient, operations }),
|
|
2034
1716
|
scores: new StoreScoresLance({ client: instance.lanceClient }),
|
|
2035
|
-
memory: new StoreMemoryLance({ client: instance.lanceClient, operations })
|
|
2036
|
-
legacyEvals: new StoreLegacyEvalsLance({ client: instance.lanceClient })
|
|
1717
|
+
memory: new StoreMemoryLance({ client: instance.lanceClient, operations })
|
|
2037
1718
|
};
|
|
2038
1719
|
return instance;
|
|
2039
1720
|
} catch (e) {
|
|
@@ -2059,9 +1740,7 @@ var LanceStorage = class _LanceStorage extends MastraStorage {
|
|
|
2059
1740
|
this.stores = {
|
|
2060
1741
|
operations: new StoreOperationsLance({ client: this.lanceClient }),
|
|
2061
1742
|
workflows: new StoreWorkflowsLance({ client: this.lanceClient }),
|
|
2062
|
-
traces: new StoreTracesLance({ client: this.lanceClient, operations }),
|
|
2063
1743
|
scores: new StoreScoresLance({ client: this.lanceClient }),
|
|
2064
|
-
legacyEvals: new StoreLegacyEvalsLance({ client: this.lanceClient }),
|
|
2065
1744
|
memory: new StoreMemoryLance({ client: this.lanceClient, operations })
|
|
2066
1745
|
};
|
|
2067
1746
|
}
|
|
@@ -2096,9 +1775,6 @@ var LanceStorage = class _LanceStorage extends MastraStorage {
|
|
|
2096
1775
|
async getThreadById({ threadId }) {
|
|
2097
1776
|
return this.stores.memory.getThreadById({ threadId });
|
|
2098
1777
|
}
|
|
2099
|
-
async getThreadsByResourceId({ resourceId }) {
|
|
2100
|
-
return this.stores.memory.getThreadsByResourceId({ resourceId });
|
|
2101
|
-
}
|
|
2102
1778
|
/**
|
|
2103
1779
|
* Saves a thread to the database. This function doesn't overwrite existing threads.
|
|
2104
1780
|
* @param thread - The thread to save
|
|
@@ -2124,7 +1800,7 @@ var LanceStorage = class _LanceStorage extends MastraStorage {
|
|
|
2124
1800
|
hasColumn: true,
|
|
2125
1801
|
createTable: true,
|
|
2126
1802
|
deleteMessages: false,
|
|
2127
|
-
|
|
1803
|
+
listScoresBySpan: true
|
|
2128
1804
|
};
|
|
2129
1805
|
}
|
|
2130
1806
|
async getResourceById({ resourceId }) {
|
|
@@ -2188,50 +1864,17 @@ var LanceStorage = class _LanceStorage extends MastraStorage {
|
|
|
2188
1864
|
});
|
|
2189
1865
|
return Array.from(allIndices).sort((a, b) => a - b).map((index) => records[index]);
|
|
2190
1866
|
}
|
|
2191
|
-
async
|
|
2192
|
-
|
|
2193
|
-
resourceId,
|
|
2194
|
-
selectBy,
|
|
2195
|
-
format,
|
|
2196
|
-
threadConfig
|
|
2197
|
-
}) {
|
|
2198
|
-
return this.stores.memory.getMessages({ threadId, resourceId, selectBy, format, threadConfig });
|
|
2199
|
-
}
|
|
2200
|
-
async getMessagesById({
|
|
2201
|
-
messageIds,
|
|
2202
|
-
format
|
|
2203
|
-
}) {
|
|
2204
|
-
return this.stores.memory.getMessagesById({ messageIds, format });
|
|
1867
|
+
async listMessagesById({ messageIds }) {
|
|
1868
|
+
return this.stores.memory.listMessagesById({ messageIds });
|
|
2205
1869
|
}
|
|
2206
1870
|
async saveMessages(args) {
|
|
2207
1871
|
return this.stores.memory.saveMessages(args);
|
|
2208
1872
|
}
|
|
2209
|
-
async getThreadsByResourceIdPaginated(args) {
|
|
2210
|
-
return this.stores.memory.getThreadsByResourceIdPaginated(args);
|
|
2211
|
-
}
|
|
2212
|
-
async getMessagesPaginated(args) {
|
|
2213
|
-
return this.stores.memory.getMessagesPaginated(args);
|
|
2214
|
-
}
|
|
2215
1873
|
async updateMessages(_args) {
|
|
2216
1874
|
return this.stores.memory.updateMessages(_args);
|
|
2217
1875
|
}
|
|
2218
|
-
async
|
|
2219
|
-
return this.stores.
|
|
2220
|
-
}
|
|
2221
|
-
async getTraces(args) {
|
|
2222
|
-
return this.stores.traces.getTraces(args);
|
|
2223
|
-
}
|
|
2224
|
-
async getTracesPaginated(args) {
|
|
2225
|
-
return this.stores.traces.getTracesPaginated(args);
|
|
2226
|
-
}
|
|
2227
|
-
async getEvalsByAgentName(agentName, type) {
|
|
2228
|
-
return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
|
|
2229
|
-
}
|
|
2230
|
-
async getEvals(options) {
|
|
2231
|
-
return this.stores.legacyEvals.getEvals(options);
|
|
2232
|
-
}
|
|
2233
|
-
async getWorkflowRuns(args) {
|
|
2234
|
-
return this.stores.workflows.getWorkflowRuns(args);
|
|
1876
|
+
async listWorkflowRuns(args) {
|
|
1877
|
+
return this.stores.workflows.listWorkflowRuns(args);
|
|
2235
1878
|
}
|
|
2236
1879
|
async getWorkflowRunById(args) {
|
|
2237
1880
|
return this.stores.workflows.getWorkflowRunById(args);
|
|
@@ -2241,9 +1884,9 @@ var LanceStorage = class _LanceStorage extends MastraStorage {
|
|
|
2241
1884
|
runId,
|
|
2242
1885
|
stepId,
|
|
2243
1886
|
result,
|
|
2244
|
-
|
|
1887
|
+
requestContext
|
|
2245
1888
|
}) {
|
|
2246
|
-
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result,
|
|
1889
|
+
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, requestContext });
|
|
2247
1890
|
}
|
|
2248
1891
|
async updateWorkflowState({
|
|
2249
1892
|
workflowName,
|
|
@@ -2269,37 +1912,37 @@ var LanceStorage = class _LanceStorage extends MastraStorage {
|
|
|
2269
1912
|
async getScoreById({ id: _id }) {
|
|
2270
1913
|
return this.stores.scores.getScoreById({ id: _id });
|
|
2271
1914
|
}
|
|
2272
|
-
async
|
|
1915
|
+
async listScoresByScorerId({
|
|
2273
1916
|
scorerId,
|
|
2274
1917
|
source,
|
|
2275
1918
|
entityId,
|
|
2276
1919
|
entityType,
|
|
2277
1920
|
pagination
|
|
2278
1921
|
}) {
|
|
2279
|
-
return this.stores.scores.
|
|
1922
|
+
return this.stores.scores.listScoresByScorerId({ scorerId, source, pagination, entityId, entityType });
|
|
2280
1923
|
}
|
|
2281
1924
|
async saveScore(_score) {
|
|
2282
1925
|
return this.stores.scores.saveScore(_score);
|
|
2283
1926
|
}
|
|
2284
|
-
async
|
|
1927
|
+
async listScoresByRunId({
|
|
2285
1928
|
runId,
|
|
2286
1929
|
pagination
|
|
2287
1930
|
}) {
|
|
2288
|
-
return this.stores.scores.
|
|
1931
|
+
return this.stores.scores.listScoresByRunId({ runId, pagination });
|
|
2289
1932
|
}
|
|
2290
|
-
async
|
|
1933
|
+
async listScoresByEntityId({
|
|
2291
1934
|
entityId,
|
|
2292
1935
|
entityType,
|
|
2293
1936
|
pagination
|
|
2294
1937
|
}) {
|
|
2295
|
-
return this.stores.scores.
|
|
1938
|
+
return this.stores.scores.listScoresByEntityId({ entityId, entityType, pagination });
|
|
2296
1939
|
}
|
|
2297
|
-
async
|
|
1940
|
+
async listScoresBySpan({
|
|
2298
1941
|
traceId,
|
|
2299
1942
|
spanId,
|
|
2300
1943
|
pagination
|
|
2301
1944
|
}) {
|
|
2302
|
-
return this.stores.scores.
|
|
1945
|
+
return this.stores.scores.listScoresBySpan({ traceId, spanId, pagination });
|
|
2303
1946
|
}
|
|
2304
1947
|
};
|
|
2305
1948
|
var LanceFilterTranslator = class extends BaseFilterTranslator {
|