@mastra/lance 0.0.0-playground-studio-cloud-20251031080052 → 0.0.0-refactor-agent-information-for-recomposable-ui-20251112151814
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 +407 -3
- package/README.md +61 -4
- package/dist/index.cjs +295 -441
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +295 -441
- 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 +27 -74
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/vector/index.d.ts +3 -1
- package/dist/vector/index.d.ts.map +1 -1
- package/package.json +8 -5
- 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/index.cjs
CHANGED
|
@@ -5,131 +5,15 @@ var error = require('@mastra/core/error');
|
|
|
5
5
|
var storage = require('@mastra/core/storage');
|
|
6
6
|
var agent = require('@mastra/core/agent');
|
|
7
7
|
var apacheArrow = require('apache-arrow');
|
|
8
|
-
var
|
|
8
|
+
var evals = require('@mastra/core/evals');
|
|
9
9
|
var vector = require('@mastra/core/vector');
|
|
10
10
|
var filter = require('@mastra/core/vector/filter');
|
|
11
11
|
|
|
12
12
|
// src/storage/index.ts
|
|
13
|
-
var StoreLegacyEvalsLance = class extends storage.LegacyEvalsStorage {
|
|
14
|
-
client;
|
|
15
|
-
constructor({ client }) {
|
|
16
|
-
super();
|
|
17
|
-
this.client = client;
|
|
18
|
-
}
|
|
19
|
-
async getEvalsByAgentName(agentName, type) {
|
|
20
|
-
try {
|
|
21
|
-
const table = await this.client.openTable(storage.TABLE_EVALS);
|
|
22
|
-
const query = table.query().where(`agent_name = '${agentName}'`);
|
|
23
|
-
const records = await query.toArray();
|
|
24
|
-
let filteredRecords = records;
|
|
25
|
-
if (type === "live") {
|
|
26
|
-
filteredRecords = records.filter((record) => record.test_info === null);
|
|
27
|
-
} else if (type === "test") {
|
|
28
|
-
filteredRecords = records.filter((record) => record.test_info !== null);
|
|
29
|
-
}
|
|
30
|
-
return filteredRecords.map((record) => {
|
|
31
|
-
return {
|
|
32
|
-
id: record.id,
|
|
33
|
-
input: record.input,
|
|
34
|
-
output: record.output,
|
|
35
|
-
agentName: record.agent_name,
|
|
36
|
-
metricName: record.metric_name,
|
|
37
|
-
result: JSON.parse(record.result),
|
|
38
|
-
instructions: record.instructions,
|
|
39
|
-
testInfo: record.test_info ? JSON.parse(record.test_info) : null,
|
|
40
|
-
globalRunId: record.global_run_id,
|
|
41
|
-
runId: record.run_id,
|
|
42
|
-
createdAt: new Date(record.created_at).toString()
|
|
43
|
-
};
|
|
44
|
-
});
|
|
45
|
-
} catch (error$1) {
|
|
46
|
-
throw new error.MastraError(
|
|
47
|
-
{
|
|
48
|
-
id: "LANCE_STORE_GET_EVALS_BY_AGENT_NAME_FAILED",
|
|
49
|
-
domain: error.ErrorDomain.STORAGE,
|
|
50
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
51
|
-
details: { agentName }
|
|
52
|
-
},
|
|
53
|
-
error$1
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
async getEvals(options) {
|
|
58
|
-
try {
|
|
59
|
-
const table = await this.client.openTable(storage.TABLE_EVALS);
|
|
60
|
-
const conditions = [];
|
|
61
|
-
if (options.agentName) {
|
|
62
|
-
conditions.push(`agent_name = '${options.agentName}'`);
|
|
63
|
-
}
|
|
64
|
-
if (options.type === "live") {
|
|
65
|
-
conditions.push("test_info IS NULL");
|
|
66
|
-
} else if (options.type === "test") {
|
|
67
|
-
conditions.push("test_info IS NOT NULL");
|
|
68
|
-
}
|
|
69
|
-
const startDate = options.dateRange?.start || options.fromDate;
|
|
70
|
-
const endDate = options.dateRange?.end || options.toDate;
|
|
71
|
-
if (startDate) {
|
|
72
|
-
conditions.push(`\`created_at\` >= ${startDate.getTime()}`);
|
|
73
|
-
}
|
|
74
|
-
if (endDate) {
|
|
75
|
-
conditions.push(`\`created_at\` <= ${endDate.getTime()}`);
|
|
76
|
-
}
|
|
77
|
-
let total = 0;
|
|
78
|
-
if (conditions.length > 0) {
|
|
79
|
-
total = await table.countRows(conditions.join(" AND "));
|
|
80
|
-
} else {
|
|
81
|
-
total = await table.countRows();
|
|
82
|
-
}
|
|
83
|
-
const query = table.query();
|
|
84
|
-
if (conditions.length > 0) {
|
|
85
|
-
const whereClause = conditions.join(" AND ");
|
|
86
|
-
query.where(whereClause);
|
|
87
|
-
}
|
|
88
|
-
const records = await query.toArray();
|
|
89
|
-
const evals = records.sort((a, b) => b.created_at - a.created_at).map((record) => {
|
|
90
|
-
return {
|
|
91
|
-
id: record.id,
|
|
92
|
-
input: record.input,
|
|
93
|
-
output: record.output,
|
|
94
|
-
agentName: record.agent_name,
|
|
95
|
-
metricName: record.metric_name,
|
|
96
|
-
result: JSON.parse(record.result),
|
|
97
|
-
instructions: record.instructions,
|
|
98
|
-
testInfo: record.test_info ? JSON.parse(record.test_info) : null,
|
|
99
|
-
globalRunId: record.global_run_id,
|
|
100
|
-
runId: record.run_id,
|
|
101
|
-
createdAt: new Date(record.created_at).toISOString()
|
|
102
|
-
};
|
|
103
|
-
});
|
|
104
|
-
const page = options.page || 0;
|
|
105
|
-
const perPage = options.perPage || 10;
|
|
106
|
-
const pagedEvals = evals.slice(page * perPage, (page + 1) * perPage);
|
|
107
|
-
return {
|
|
108
|
-
evals: pagedEvals,
|
|
109
|
-
total,
|
|
110
|
-
page,
|
|
111
|
-
perPage,
|
|
112
|
-
hasMore: total > (page + 1) * perPage
|
|
113
|
-
};
|
|
114
|
-
} catch (error$1) {
|
|
115
|
-
throw new error.MastraError(
|
|
116
|
-
{
|
|
117
|
-
id: "LANCE_STORE_GET_EVALS_FAILED",
|
|
118
|
-
domain: error.ErrorDomain.STORAGE,
|
|
119
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
120
|
-
details: { agentName: options.agentName ?? "" }
|
|
121
|
-
},
|
|
122
|
-
error$1
|
|
123
|
-
);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
};
|
|
127
13
|
function getPrimaryKeys(tableName) {
|
|
128
14
|
let primaryId = ["id"];
|
|
129
15
|
if (tableName === storage.TABLE_WORKFLOW_SNAPSHOT) {
|
|
130
16
|
primaryId = ["workflow_name", "run_id"];
|
|
131
|
-
} else if (tableName === storage.TABLE_EVALS) {
|
|
132
|
-
primaryId = ["agent_name", "metric_name", "run_id"];
|
|
133
17
|
}
|
|
134
18
|
return primaryId;
|
|
135
19
|
}
|
|
@@ -249,6 +133,10 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
249
133
|
this.client = client;
|
|
250
134
|
this.operations = operations;
|
|
251
135
|
}
|
|
136
|
+
// Utility to escape single quotes in SQL strings
|
|
137
|
+
escapeSql(str) {
|
|
138
|
+
return str.replace(/'/g, "''");
|
|
139
|
+
}
|
|
252
140
|
async getThreadById({ threadId }) {
|
|
253
141
|
try {
|
|
254
142
|
const thread = await this.operations.load({ tableName: storage.TABLE_THREADS, keys: { id: threadId } });
|
|
@@ -271,26 +159,6 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
271
159
|
);
|
|
272
160
|
}
|
|
273
161
|
}
|
|
274
|
-
async getThreadsByResourceId({ resourceId }) {
|
|
275
|
-
try {
|
|
276
|
-
const table = await this.client.openTable(storage.TABLE_THREADS);
|
|
277
|
-
const query = table.query().where(`\`resourceId\` = '${resourceId}'`);
|
|
278
|
-
const records = await query.toArray();
|
|
279
|
-
return processResultWithTypeConversion(
|
|
280
|
-
records,
|
|
281
|
-
await getTableSchema({ tableName: storage.TABLE_THREADS, client: this.client })
|
|
282
|
-
);
|
|
283
|
-
} catch (error$1) {
|
|
284
|
-
throw new error.MastraError(
|
|
285
|
-
{
|
|
286
|
-
id: "LANCE_STORE_GET_THREADS_BY_RESOURCE_ID_FAILED",
|
|
287
|
-
domain: error.ErrorDomain.STORAGE,
|
|
288
|
-
category: error.ErrorCategory.THIRD_PARTY
|
|
289
|
-
},
|
|
290
|
-
error$1
|
|
291
|
-
);
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
162
|
/**
|
|
295
163
|
* Saves a thread to the database. This function doesn't overwrite existing threads.
|
|
296
164
|
* @param thread - The thread to save
|
|
@@ -395,100 +263,174 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
395
263
|
})() : message.content
|
|
396
264
|
};
|
|
397
265
|
}
|
|
398
|
-
async
|
|
399
|
-
|
|
400
|
-
resourceId,
|
|
401
|
-
selectBy,
|
|
402
|
-
format,
|
|
403
|
-
threadConfig
|
|
404
|
-
}) {
|
|
266
|
+
async listMessagesById({ messageIds }) {
|
|
267
|
+
if (messageIds.length === 0) return { messages: [] };
|
|
405
268
|
try {
|
|
406
|
-
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
407
|
-
if (threadConfig) {
|
|
408
|
-
throw new Error("ThreadConfig is not supported by LanceDB storage");
|
|
409
|
-
}
|
|
410
|
-
const limit = storage.resolveMessageLimit({ last: selectBy?.last, defaultLimit: Number.MAX_SAFE_INTEGER });
|
|
411
269
|
const table = await this.client.openTable(storage.TABLE_MESSAGES);
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
const threadIds = [...new Set(selectBy.include.map((item) => item.threadId))];
|
|
415
|
-
for (const threadId2 of threadIds) {
|
|
416
|
-
const threadQuery = table.query().where(`thread_id = '${threadId2}'`);
|
|
417
|
-
let threadRecords = await threadQuery.toArray();
|
|
418
|
-
allRecords.push(...threadRecords);
|
|
419
|
-
}
|
|
420
|
-
} else {
|
|
421
|
-
let query = table.query().where(`\`thread_id\` = '${threadId}'`);
|
|
422
|
-
allRecords = await query.toArray();
|
|
423
|
-
}
|
|
424
|
-
allRecords.sort((a, b) => {
|
|
425
|
-
const dateA = new Date(a.createdAt).getTime();
|
|
426
|
-
const dateB = new Date(b.createdAt).getTime();
|
|
427
|
-
return dateA - dateB;
|
|
428
|
-
});
|
|
429
|
-
if (selectBy?.include && selectBy.include.length > 0) {
|
|
430
|
-
allRecords = this.processMessagesWithContext(allRecords, selectBy.include);
|
|
431
|
-
}
|
|
432
|
-
if (limit !== Number.MAX_SAFE_INTEGER) {
|
|
433
|
-
allRecords = allRecords.slice(-limit);
|
|
434
|
-
}
|
|
270
|
+
const quotedIds = messageIds.map((id) => `'${id}'`).join(", ");
|
|
271
|
+
const allRecords = await table.query().where(`id IN (${quotedIds})`).toArray();
|
|
435
272
|
const messages = processResultWithTypeConversion(
|
|
436
273
|
allRecords,
|
|
437
274
|
await getTableSchema({ tableName: storage.TABLE_MESSAGES, client: this.client })
|
|
438
275
|
);
|
|
439
|
-
const list = new agent.MessageList(
|
|
440
|
-
|
|
441
|
-
|
|
276
|
+
const list = new agent.MessageList().add(
|
|
277
|
+
messages.map(this.normalizeMessage),
|
|
278
|
+
"memory"
|
|
279
|
+
);
|
|
280
|
+
return { messages: list.get.all.db() };
|
|
442
281
|
} catch (error$1) {
|
|
443
282
|
throw new error.MastraError(
|
|
444
283
|
{
|
|
445
|
-
id: "
|
|
284
|
+
id: "LANCE_STORE_LIST_MESSAGES_BY_ID_FAILED",
|
|
446
285
|
domain: error.ErrorDomain.STORAGE,
|
|
447
286
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
448
287
|
details: {
|
|
449
|
-
|
|
450
|
-
resourceId: resourceId ?? ""
|
|
288
|
+
messageIds: JSON.stringify(messageIds)
|
|
451
289
|
}
|
|
452
290
|
},
|
|
453
291
|
error$1
|
|
454
292
|
);
|
|
455
293
|
}
|
|
456
294
|
}
|
|
457
|
-
async
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
295
|
+
async listMessages(args) {
|
|
296
|
+
const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
|
|
297
|
+
if (!threadId.trim()) {
|
|
298
|
+
throw new error.MastraError(
|
|
299
|
+
{
|
|
300
|
+
id: "STORAGE_LANCE_LIST_MESSAGES_INVALID_THREAD_ID",
|
|
301
|
+
domain: error.ErrorDomain.STORAGE,
|
|
302
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
303
|
+
details: { threadId }
|
|
304
|
+
},
|
|
305
|
+
new Error("threadId must be a non-empty string")
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
const perPage = storage.normalizePerPage(perPageInput, 40);
|
|
309
|
+
const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
462
310
|
try {
|
|
311
|
+
if (page < 0) {
|
|
312
|
+
throw new error.MastraError(
|
|
313
|
+
{
|
|
314
|
+
id: "STORAGE_LANCE_LIST_MESSAGES_INVALID_PAGE",
|
|
315
|
+
domain: error.ErrorDomain.STORAGE,
|
|
316
|
+
category: error.ErrorCategory.USER,
|
|
317
|
+
details: { page }
|
|
318
|
+
},
|
|
319
|
+
new Error("page must be >= 0")
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
const { field, direction } = this.parseOrderBy(orderBy, "ASC");
|
|
463
323
|
const table = await this.client.openTable(storage.TABLE_MESSAGES);
|
|
464
|
-
const
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
324
|
+
const conditions = [`thread_id = '${this.escapeSql(threadId)}'`];
|
|
325
|
+
if (resourceId) {
|
|
326
|
+
conditions.push(`\`resourceId\` = '${this.escapeSql(resourceId)}'`);
|
|
327
|
+
}
|
|
328
|
+
if (filter?.dateRange?.start) {
|
|
329
|
+
const startTime = filter.dateRange.start instanceof Date ? filter.dateRange.start.getTime() : new Date(filter.dateRange.start).getTime();
|
|
330
|
+
conditions.push(`\`createdAt\` >= ${startTime}`);
|
|
331
|
+
}
|
|
332
|
+
if (filter?.dateRange?.end) {
|
|
333
|
+
const endTime = filter.dateRange.end instanceof Date ? filter.dateRange.end.getTime() : new Date(filter.dateRange.end).getTime();
|
|
334
|
+
conditions.push(`\`createdAt\` <= ${endTime}`);
|
|
335
|
+
}
|
|
336
|
+
const whereClause = conditions.join(" AND ");
|
|
337
|
+
const total = await table.countRows(whereClause);
|
|
338
|
+
const query = table.query().where(whereClause);
|
|
339
|
+
let allRecords = await query.toArray();
|
|
340
|
+
allRecords.sort((a, b) => {
|
|
341
|
+
const aValue = field === "createdAt" ? a.createdAt : a[field];
|
|
342
|
+
const bValue = field === "createdAt" ? b.createdAt : b[field];
|
|
343
|
+
if (aValue == null && bValue == null) return 0;
|
|
344
|
+
if (aValue == null) return direction === "ASC" ? -1 : 1;
|
|
345
|
+
if (bValue == null) return direction === "ASC" ? 1 : -1;
|
|
346
|
+
if (typeof aValue === "string" && typeof bValue === "string") {
|
|
347
|
+
return direction === "ASC" ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue);
|
|
348
|
+
}
|
|
349
|
+
return direction === "ASC" ? aValue - bValue : bValue - aValue;
|
|
350
|
+
});
|
|
351
|
+
const paginatedRecords = allRecords.slice(offset, offset + perPage);
|
|
352
|
+
const messages = paginatedRecords.map((row) => this.normalizeMessage(row));
|
|
353
|
+
if (total === 0 && messages.length === 0 && (!include || include.length === 0)) {
|
|
354
|
+
return {
|
|
355
|
+
messages: [],
|
|
356
|
+
total: 0,
|
|
357
|
+
page,
|
|
358
|
+
perPage: perPageForResponse,
|
|
359
|
+
hasMore: false
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
const messageIds = new Set(messages.map((m) => m.id));
|
|
363
|
+
if (include && include.length > 0) {
|
|
364
|
+
const threadIds = [...new Set(include.map((item) => item.threadId || threadId))];
|
|
365
|
+
const allThreadMessages = [];
|
|
366
|
+
for (const tid of threadIds) {
|
|
367
|
+
const threadQuery = table.query().where(`thread_id = '${tid}'`);
|
|
368
|
+
let threadRecords = await threadQuery.toArray();
|
|
369
|
+
allThreadMessages.push(...threadRecords);
|
|
370
|
+
}
|
|
371
|
+
allThreadMessages.sort((a, b) => a.createdAt - b.createdAt);
|
|
372
|
+
const contextMessages = this.processMessagesWithContext(allThreadMessages, include);
|
|
373
|
+
const includedMessages = contextMessages.map((row) => this.normalizeMessage(row));
|
|
374
|
+
for (const includeMsg of includedMessages) {
|
|
375
|
+
if (!messageIds.has(includeMsg.id)) {
|
|
376
|
+
messages.push(includeMsg);
|
|
377
|
+
messageIds.add(includeMsg.id);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
const list = new agent.MessageList().add(messages, "memory");
|
|
382
|
+
let finalMessages = list.get.all.db();
|
|
383
|
+
finalMessages = finalMessages.sort((a, b) => {
|
|
384
|
+
const aValue = field === "createdAt" ? new Date(a.createdAt).getTime() : a[field];
|
|
385
|
+
const bValue = field === "createdAt" ? new Date(b.createdAt).getTime() : b[field];
|
|
386
|
+
if (aValue == null && bValue == null) return 0;
|
|
387
|
+
if (aValue == null) return direction === "ASC" ? -1 : 1;
|
|
388
|
+
if (bValue == null) return direction === "ASC" ? 1 : -1;
|
|
389
|
+
if (typeof aValue === "string" && typeof bValue === "string") {
|
|
390
|
+
return direction === "ASC" ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue);
|
|
391
|
+
}
|
|
392
|
+
return direction === "ASC" ? aValue - bValue : bValue - aValue;
|
|
393
|
+
});
|
|
394
|
+
const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
|
|
395
|
+
const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
|
|
396
|
+
const fetchedAll = perPageInput === false || allThreadMessagesReturned;
|
|
397
|
+
const hasMore = !fetchedAll && offset + perPage < total;
|
|
398
|
+
return {
|
|
399
|
+
messages: finalMessages,
|
|
400
|
+
total,
|
|
401
|
+
page,
|
|
402
|
+
perPage: perPageForResponse,
|
|
403
|
+
hasMore
|
|
404
|
+
};
|
|
473
405
|
} catch (error$1) {
|
|
474
|
-
|
|
406
|
+
const mastraError = new error.MastraError(
|
|
475
407
|
{
|
|
476
|
-
id: "
|
|
408
|
+
id: "LANCE_STORE_LIST_MESSAGES_FAILED",
|
|
477
409
|
domain: error.ErrorDomain.STORAGE,
|
|
478
410
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
479
411
|
details: {
|
|
480
|
-
|
|
412
|
+
threadId,
|
|
413
|
+
resourceId: resourceId ?? ""
|
|
481
414
|
}
|
|
482
415
|
},
|
|
483
416
|
error$1
|
|
484
417
|
);
|
|
418
|
+
this.logger?.error?.(mastraError.toString());
|
|
419
|
+
this.logger?.trackException?.(mastraError);
|
|
420
|
+
return {
|
|
421
|
+
messages: [],
|
|
422
|
+
total: 0,
|
|
423
|
+
page,
|
|
424
|
+
perPage: perPageForResponse,
|
|
425
|
+
hasMore: false
|
|
426
|
+
};
|
|
485
427
|
}
|
|
486
428
|
}
|
|
487
429
|
async saveMessages(args) {
|
|
488
430
|
try {
|
|
489
|
-
const { messages
|
|
431
|
+
const { messages } = args;
|
|
490
432
|
if (messages.length === 0) {
|
|
491
|
-
return [];
|
|
433
|
+
return { messages: [] };
|
|
492
434
|
}
|
|
493
435
|
const threadId = messages[0]?.threadId;
|
|
494
436
|
if (!threadId) {
|
|
@@ -524,8 +466,7 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
524
466
|
const updateRecord = { id: threadId, updatedAt: currentTime };
|
|
525
467
|
await threadsTable.mergeInsert("id").whenMatchedUpdateAll().whenNotMatchedInsertAll().execute([updateRecord]);
|
|
526
468
|
const list = new agent.MessageList().add(messages, "memory");
|
|
527
|
-
|
|
528
|
-
return list.get.all.v1();
|
|
469
|
+
return { messages: list.get.all.db() };
|
|
529
470
|
} catch (error$1) {
|
|
530
471
|
throw new error.MastraError(
|
|
531
472
|
{
|
|
@@ -537,32 +478,54 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
537
478
|
);
|
|
538
479
|
}
|
|
539
480
|
}
|
|
540
|
-
async
|
|
481
|
+
async listThreadsByResourceId(args) {
|
|
541
482
|
try {
|
|
542
|
-
const { resourceId, page = 0, perPage
|
|
543
|
-
const
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
483
|
+
const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
|
|
484
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
485
|
+
if (page < 0) {
|
|
486
|
+
throw new error.MastraError(
|
|
487
|
+
{
|
|
488
|
+
id: "STORAGE_LANCE_LIST_THREADS_BY_RESOURCE_ID_INVALID_PAGE",
|
|
489
|
+
domain: error.ErrorDomain.STORAGE,
|
|
490
|
+
category: error.ErrorCategory.USER,
|
|
491
|
+
details: { page }
|
|
492
|
+
},
|
|
493
|
+
new Error("page must be >= 0")
|
|
494
|
+
);
|
|
550
495
|
}
|
|
496
|
+
const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
497
|
+
const { field, direction } = this.parseOrderBy(orderBy);
|
|
498
|
+
const table = await this.client.openTable(storage.TABLE_THREADS);
|
|
499
|
+
const total = await table.countRows(`\`resourceId\` = '${this.escapeSql(resourceId)}'`);
|
|
500
|
+
const query = table.query().where(`\`resourceId\` = '${this.escapeSql(resourceId)}'`);
|
|
551
501
|
const records = await query.toArray();
|
|
552
|
-
records.sort((a, b) =>
|
|
502
|
+
records.sort((a, b) => {
|
|
503
|
+
const aValue = ["createdAt", "updatedAt"].includes(field) ? new Date(a[field]).getTime() : a[field];
|
|
504
|
+
const bValue = ["createdAt", "updatedAt"].includes(field) ? new Date(b[field]).getTime() : b[field];
|
|
505
|
+
if (aValue == null && bValue == null) return 0;
|
|
506
|
+
if (aValue == null) return direction === "ASC" ? -1 : 1;
|
|
507
|
+
if (bValue == null) return direction === "ASC" ? 1 : -1;
|
|
508
|
+
if (typeof aValue === "string" && typeof bValue === "string") {
|
|
509
|
+
return direction === "ASC" ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue);
|
|
510
|
+
}
|
|
511
|
+
return direction === "ASC" ? aValue - bValue : bValue - aValue;
|
|
512
|
+
});
|
|
513
|
+
const paginatedRecords = records.slice(offset, offset + perPage);
|
|
553
514
|
const schema = await getTableSchema({ tableName: storage.TABLE_THREADS, client: this.client });
|
|
554
|
-
const threads =
|
|
515
|
+
const threads = paginatedRecords.map(
|
|
516
|
+
(record) => processResultWithTypeConversion(record, schema)
|
|
517
|
+
);
|
|
555
518
|
return {
|
|
556
519
|
threads,
|
|
557
520
|
total,
|
|
558
521
|
page,
|
|
559
|
-
perPage,
|
|
560
|
-
hasMore:
|
|
522
|
+
perPage: perPageForResponse,
|
|
523
|
+
hasMore: offset + perPage < total
|
|
561
524
|
};
|
|
562
525
|
} catch (error$1) {
|
|
563
526
|
throw new error.MastraError(
|
|
564
527
|
{
|
|
565
|
-
id: "
|
|
528
|
+
id: "LANCE_STORE_LIST_THREADS_BY_RESOURCE_ID_FAILED",
|
|
566
529
|
domain: error.ErrorDomain.STORAGE,
|
|
567
530
|
category: error.ErrorCategory.THIRD_PARTY
|
|
568
531
|
},
|
|
@@ -618,132 +581,8 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
|
|
|
618
581
|
});
|
|
619
582
|
return Array.from(allIndices).sort((a, b) => a - b).map((index) => records[index]);
|
|
620
583
|
}
|
|
621
|
-
async getMessagesPaginated(args) {
|
|
622
|
-
const { threadId, resourceId, selectBy, format = "v1" } = args;
|
|
623
|
-
const page = selectBy?.pagination?.page ?? 0;
|
|
624
|
-
const perPage = selectBy?.pagination?.perPage ?? 10;
|
|
625
|
-
try {
|
|
626
|
-
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
627
|
-
const dateRange = selectBy?.pagination?.dateRange;
|
|
628
|
-
const fromDate = dateRange?.start;
|
|
629
|
-
const toDate = dateRange?.end;
|
|
630
|
-
const table = await this.client.openTable(storage.TABLE_MESSAGES);
|
|
631
|
-
const messages = [];
|
|
632
|
-
if (selectBy?.include && Array.isArray(selectBy.include)) {
|
|
633
|
-
const threadIds = [...new Set(selectBy.include.map((item) => item.threadId))];
|
|
634
|
-
const allThreadMessages = [];
|
|
635
|
-
for (const threadId2 of threadIds) {
|
|
636
|
-
const threadQuery = table.query().where(`thread_id = '${threadId2}'`);
|
|
637
|
-
let threadRecords = await threadQuery.toArray();
|
|
638
|
-
if (fromDate) threadRecords = threadRecords.filter((m) => m.createdAt >= fromDate.getTime());
|
|
639
|
-
if (toDate) threadRecords = threadRecords.filter((m) => m.createdAt <= toDate.getTime());
|
|
640
|
-
allThreadMessages.push(...threadRecords);
|
|
641
|
-
}
|
|
642
|
-
allThreadMessages.sort((a, b) => a.createdAt - b.createdAt);
|
|
643
|
-
const contextMessages = this.processMessagesWithContext(allThreadMessages, selectBy.include);
|
|
644
|
-
messages.push(...contextMessages);
|
|
645
|
-
}
|
|
646
|
-
const conditions = [`thread_id = '${threadId}'`];
|
|
647
|
-
if (resourceId) {
|
|
648
|
-
conditions.push(`\`resourceId\` = '${resourceId}'`);
|
|
649
|
-
}
|
|
650
|
-
if (fromDate) {
|
|
651
|
-
conditions.push(`\`createdAt\` >= ${fromDate.getTime()}`);
|
|
652
|
-
}
|
|
653
|
-
if (toDate) {
|
|
654
|
-
conditions.push(`\`createdAt\` <= ${toDate.getTime()}`);
|
|
655
|
-
}
|
|
656
|
-
let total = 0;
|
|
657
|
-
if (conditions.length > 0) {
|
|
658
|
-
total = await table.countRows(conditions.join(" AND "));
|
|
659
|
-
} else {
|
|
660
|
-
total = await table.countRows();
|
|
661
|
-
}
|
|
662
|
-
if (total === 0 && messages.length === 0) {
|
|
663
|
-
return {
|
|
664
|
-
messages: [],
|
|
665
|
-
total: 0,
|
|
666
|
-
page,
|
|
667
|
-
perPage,
|
|
668
|
-
hasMore: false
|
|
669
|
-
};
|
|
670
|
-
}
|
|
671
|
-
const excludeIds = messages.map((m) => m.id);
|
|
672
|
-
let selectedMessages = [];
|
|
673
|
-
if (selectBy?.last && selectBy.last > 0) {
|
|
674
|
-
const query = table.query();
|
|
675
|
-
if (conditions.length > 0) {
|
|
676
|
-
query.where(conditions.join(" AND "));
|
|
677
|
-
}
|
|
678
|
-
let records = await query.toArray();
|
|
679
|
-
records = records.sort((a, b) => a.createdAt - b.createdAt);
|
|
680
|
-
if (excludeIds.length > 0) {
|
|
681
|
-
records = records.filter((m) => !excludeIds.includes(m.id));
|
|
682
|
-
}
|
|
683
|
-
selectedMessages = records.slice(-selectBy.last);
|
|
684
|
-
} else {
|
|
685
|
-
const query = table.query();
|
|
686
|
-
if (conditions.length > 0) {
|
|
687
|
-
query.where(conditions.join(" AND "));
|
|
688
|
-
}
|
|
689
|
-
let records = await query.toArray();
|
|
690
|
-
records = records.sort((a, b) => a.createdAt - b.createdAt);
|
|
691
|
-
if (excludeIds.length > 0) {
|
|
692
|
-
records = records.filter((m) => !excludeIds.includes(m.id));
|
|
693
|
-
}
|
|
694
|
-
selectedMessages = records.slice(page * perPage, (page + 1) * perPage);
|
|
695
|
-
}
|
|
696
|
-
const allMessages = [...messages, ...selectedMessages];
|
|
697
|
-
const seen = /* @__PURE__ */ new Set();
|
|
698
|
-
const dedupedMessages = allMessages.filter((m) => {
|
|
699
|
-
const key = `${m.id}:${m.thread_id}`;
|
|
700
|
-
if (seen.has(key)) return false;
|
|
701
|
-
seen.add(key);
|
|
702
|
-
return true;
|
|
703
|
-
});
|
|
704
|
-
const formattedMessages = dedupedMessages.map((msg) => {
|
|
705
|
-
const { thread_id, ...rest } = msg;
|
|
706
|
-
return {
|
|
707
|
-
...rest,
|
|
708
|
-
threadId: thread_id,
|
|
709
|
-
content: typeof msg.content === "string" ? (() => {
|
|
710
|
-
try {
|
|
711
|
-
return JSON.parse(msg.content);
|
|
712
|
-
} catch {
|
|
713
|
-
return msg.content;
|
|
714
|
-
}
|
|
715
|
-
})() : msg.content
|
|
716
|
-
};
|
|
717
|
-
});
|
|
718
|
-
const list = new agent.MessageList().add(formattedMessages, "memory");
|
|
719
|
-
return {
|
|
720
|
-
messages: format === "v2" ? list.get.all.v2() : list.get.all.v1(),
|
|
721
|
-
total,
|
|
722
|
-
// Total should be the count of messages matching the filters
|
|
723
|
-
page,
|
|
724
|
-
perPage,
|
|
725
|
-
hasMore: total > (page + 1) * perPage
|
|
726
|
-
};
|
|
727
|
-
} catch (error$1) {
|
|
728
|
-
const mastraError = new error.MastraError(
|
|
729
|
-
{
|
|
730
|
-
id: "LANCE_STORE_GET_MESSAGES_PAGINATED_FAILED",
|
|
731
|
-
domain: error.ErrorDomain.STORAGE,
|
|
732
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
733
|
-
details: {
|
|
734
|
-
threadId,
|
|
735
|
-
resourceId: resourceId ?? ""
|
|
736
|
-
}
|
|
737
|
-
},
|
|
738
|
-
error$1
|
|
739
|
-
);
|
|
740
|
-
this.logger?.trackException?.(mastraError);
|
|
741
|
-
this.logger?.error?.(mastraError.toString());
|
|
742
|
-
return { messages: [], total: 0, page, perPage, hasMore: false };
|
|
743
|
-
}
|
|
744
|
-
}
|
|
745
584
|
/**
|
|
746
|
-
* Parse message data from LanceDB record format to
|
|
585
|
+
* Parse message data from LanceDB record format to MastraDBMessage format
|
|
747
586
|
*/
|
|
748
587
|
parseMessageData(data) {
|
|
749
588
|
const { thread_id, ...rest } = data;
|
|
@@ -1401,7 +1240,7 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1401
1240
|
async saveScore(score) {
|
|
1402
1241
|
let validatedScore;
|
|
1403
1242
|
try {
|
|
1404
|
-
validatedScore =
|
|
1243
|
+
validatedScore = evals.saveScorePayloadSchema.parse(score);
|
|
1405
1244
|
} catch (error$1) {
|
|
1406
1245
|
throw new error.MastraError(
|
|
1407
1246
|
{
|
|
@@ -1466,7 +1305,7 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1466
1305
|
);
|
|
1467
1306
|
}
|
|
1468
1307
|
}
|
|
1469
|
-
async
|
|
1308
|
+
async listScoresByScorerId({
|
|
1470
1309
|
scorerId,
|
|
1471
1310
|
pagination,
|
|
1472
1311
|
entityId,
|
|
@@ -1474,9 +1313,10 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1474
1313
|
source
|
|
1475
1314
|
}) {
|
|
1476
1315
|
try {
|
|
1316
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1317
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
1318
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1477
1319
|
const table = await this.client.openTable(storage.TABLE_SCORERS);
|
|
1478
|
-
const { page = 0, perPage = 10 } = pagination || {};
|
|
1479
|
-
const offset = page * perPage;
|
|
1480
1320
|
let query = table.query().where(`\`scorerId\` = '${scorerId}'`);
|
|
1481
1321
|
if (source) {
|
|
1482
1322
|
query = query.where(`\`source\` = '${source}'`);
|
|
@@ -1487,23 +1327,32 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1487
1327
|
if (entityType) {
|
|
1488
1328
|
query = query.where(`\`entityType\` = '${entityType}'`);
|
|
1489
1329
|
}
|
|
1490
|
-
query = query.limit(perPage);
|
|
1491
|
-
if (offset > 0) query.offset(offset);
|
|
1492
|
-
const records = await query.toArray();
|
|
1493
|
-
const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
|
|
1494
|
-
const scores = processResultWithTypeConversion(records, schema);
|
|
1495
1330
|
let totalQuery = table.query().where(`\`scorerId\` = '${scorerId}'`);
|
|
1496
1331
|
if (source) {
|
|
1497
1332
|
totalQuery = totalQuery.where(`\`source\` = '${source}'`);
|
|
1498
1333
|
}
|
|
1334
|
+
if (entityId) {
|
|
1335
|
+
totalQuery = totalQuery.where(`\`entityId\` = '${entityId}'`);
|
|
1336
|
+
}
|
|
1337
|
+
if (entityType) {
|
|
1338
|
+
totalQuery = totalQuery.where(`\`entityType\` = '${entityType}'`);
|
|
1339
|
+
}
|
|
1499
1340
|
const allRecords = await totalQuery.toArray();
|
|
1500
1341
|
const total = allRecords.length;
|
|
1342
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1343
|
+
if (perPageInput !== false) {
|
|
1344
|
+
query = query.limit(perPage);
|
|
1345
|
+
if (start > 0) query = query.offset(start);
|
|
1346
|
+
}
|
|
1347
|
+
const records = await query.toArray();
|
|
1348
|
+
const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
|
|
1349
|
+
const scores = processResultWithTypeConversion(records, schema);
|
|
1501
1350
|
return {
|
|
1502
1351
|
pagination: {
|
|
1503
1352
|
page,
|
|
1504
|
-
perPage,
|
|
1353
|
+
perPage: perPageForResponse,
|
|
1505
1354
|
total,
|
|
1506
|
-
hasMore:
|
|
1355
|
+
hasMore: end < total
|
|
1507
1356
|
},
|
|
1508
1357
|
scores
|
|
1509
1358
|
};
|
|
@@ -1520,27 +1369,32 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1520
1369
|
);
|
|
1521
1370
|
}
|
|
1522
1371
|
}
|
|
1523
|
-
async
|
|
1372
|
+
async listScoresByRunId({
|
|
1524
1373
|
runId,
|
|
1525
1374
|
pagination
|
|
1526
1375
|
}) {
|
|
1527
1376
|
try {
|
|
1377
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1378
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
1379
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1528
1380
|
const table = await this.client.openTable(storage.TABLE_SCORERS);
|
|
1529
|
-
const
|
|
1530
|
-
const
|
|
1531
|
-
const
|
|
1532
|
-
|
|
1381
|
+
const allRecords = await table.query().where(`\`runId\` = '${runId}'`).toArray();
|
|
1382
|
+
const total = allRecords.length;
|
|
1383
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1384
|
+
let query = table.query().where(`\`runId\` = '${runId}'`);
|
|
1385
|
+
if (perPageInput !== false) {
|
|
1386
|
+
query = query.limit(perPage);
|
|
1387
|
+
if (start > 0) query = query.offset(start);
|
|
1388
|
+
}
|
|
1533
1389
|
const records = await query.toArray();
|
|
1534
1390
|
const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
|
|
1535
1391
|
const scores = processResultWithTypeConversion(records, schema);
|
|
1536
|
-
const allRecords = await table.query().where(`\`runId\` = '${runId}'`).toArray();
|
|
1537
|
-
const total = allRecords.length;
|
|
1538
1392
|
return {
|
|
1539
1393
|
pagination: {
|
|
1540
1394
|
page,
|
|
1541
|
-
perPage,
|
|
1395
|
+
perPage: perPageForResponse,
|
|
1542
1396
|
total,
|
|
1543
|
-
hasMore:
|
|
1397
|
+
hasMore: end < total
|
|
1544
1398
|
},
|
|
1545
1399
|
scores
|
|
1546
1400
|
};
|
|
@@ -1557,28 +1411,33 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1557
1411
|
);
|
|
1558
1412
|
}
|
|
1559
1413
|
}
|
|
1560
|
-
async
|
|
1414
|
+
async listScoresByEntityId({
|
|
1561
1415
|
entityId,
|
|
1562
1416
|
entityType,
|
|
1563
1417
|
pagination
|
|
1564
1418
|
}) {
|
|
1565
1419
|
try {
|
|
1420
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1421
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
1422
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1566
1423
|
const table = await this.client.openTable(storage.TABLE_SCORERS);
|
|
1567
|
-
const
|
|
1568
|
-
const
|
|
1569
|
-
const
|
|
1570
|
-
|
|
1424
|
+
const allRecords = await table.query().where(`\`entityId\` = '${entityId}' AND \`entityType\` = '${entityType}'`).toArray();
|
|
1425
|
+
const total = allRecords.length;
|
|
1426
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1427
|
+
let query = table.query().where(`\`entityId\` = '${entityId}' AND \`entityType\` = '${entityType}'`);
|
|
1428
|
+
if (perPageInput !== false) {
|
|
1429
|
+
query = query.limit(perPage);
|
|
1430
|
+
if (start > 0) query = query.offset(start);
|
|
1431
|
+
}
|
|
1571
1432
|
const records = await query.toArray();
|
|
1572
1433
|
const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
|
|
1573
1434
|
const scores = processResultWithTypeConversion(records, schema);
|
|
1574
|
-
const allRecords = await table.query().where(`\`entityId\` = '${entityId}' AND \`entityType\` = '${entityType}'`).toArray();
|
|
1575
|
-
const total = allRecords.length;
|
|
1576
1435
|
return {
|
|
1577
1436
|
pagination: {
|
|
1578
1437
|
page,
|
|
1579
|
-
perPage,
|
|
1438
|
+
perPage: perPageForResponse,
|
|
1580
1439
|
total,
|
|
1581
|
-
hasMore:
|
|
1440
|
+
hasMore: end < total
|
|
1582
1441
|
},
|
|
1583
1442
|
scores
|
|
1584
1443
|
};
|
|
@@ -1595,28 +1454,33 @@ var StoreScoresLance = class extends storage.ScoresStorage {
|
|
|
1595
1454
|
);
|
|
1596
1455
|
}
|
|
1597
1456
|
}
|
|
1598
|
-
async
|
|
1457
|
+
async listScoresBySpan({
|
|
1599
1458
|
traceId,
|
|
1600
1459
|
spanId,
|
|
1601
1460
|
pagination
|
|
1602
1461
|
}) {
|
|
1603
1462
|
try {
|
|
1463
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1464
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
1465
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1604
1466
|
const table = await this.client.openTable(storage.TABLE_SCORERS);
|
|
1605
|
-
const
|
|
1606
|
-
const
|
|
1607
|
-
const
|
|
1608
|
-
|
|
1467
|
+
const allRecords = await table.query().where(`\`traceId\` = '${traceId}' AND \`spanId\` = '${spanId}'`).toArray();
|
|
1468
|
+
const total = allRecords.length;
|
|
1469
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1470
|
+
let query = table.query().where(`\`traceId\` = '${traceId}' AND \`spanId\` = '${spanId}'`);
|
|
1471
|
+
if (perPageInput !== false) {
|
|
1472
|
+
query = query.limit(perPage);
|
|
1473
|
+
if (start > 0) query = query.offset(start);
|
|
1474
|
+
}
|
|
1609
1475
|
const records = await query.toArray();
|
|
1610
1476
|
const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
|
|
1611
1477
|
const scores = processResultWithTypeConversion(records, schema);
|
|
1612
|
-
const allRecords = await table.query().where(`\`traceId\` = '${traceId}' AND \`spanId\` = '${spanId}'`).toArray();
|
|
1613
|
-
const total = allRecords.length;
|
|
1614
1478
|
return {
|
|
1615
1479
|
pagination: {
|
|
1616
1480
|
page,
|
|
1617
|
-
perPage,
|
|
1481
|
+
perPage: perPageForResponse,
|
|
1618
1482
|
total,
|
|
1619
|
-
hasMore:
|
|
1483
|
+
hasMore: end < total
|
|
1620
1484
|
},
|
|
1621
1485
|
scores
|
|
1622
1486
|
};
|
|
@@ -1663,7 +1527,7 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
|
|
|
1663
1527
|
// runId,
|
|
1664
1528
|
// stepId,
|
|
1665
1529
|
// result,
|
|
1666
|
-
//
|
|
1530
|
+
// requestContext,
|
|
1667
1531
|
}) {
|
|
1668
1532
|
throw new Error("Method not implemented.");
|
|
1669
1533
|
}
|
|
@@ -1691,11 +1555,13 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
|
|
|
1691
1555
|
} else {
|
|
1692
1556
|
createdAt = now;
|
|
1693
1557
|
}
|
|
1558
|
+
const { status, value, ...rest } = snapshot;
|
|
1694
1559
|
const record = {
|
|
1695
1560
|
workflow_name: workflowName,
|
|
1696
1561
|
run_id: runId,
|
|
1697
1562
|
resourceId,
|
|
1698
|
-
snapshot: JSON.stringify(
|
|
1563
|
+
snapshot: JSON.stringify({ status, value, ...rest }),
|
|
1564
|
+
// this is to ensure status is always just before value, for when querying the db by status
|
|
1699
1565
|
createdAt,
|
|
1700
1566
|
updatedAt: now
|
|
1701
1567
|
};
|
|
@@ -1757,7 +1623,7 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
|
|
|
1757
1623
|
);
|
|
1758
1624
|
}
|
|
1759
1625
|
}
|
|
1760
|
-
async
|
|
1626
|
+
async listWorkflowRuns(args) {
|
|
1761
1627
|
try {
|
|
1762
1628
|
const table = await this.client.openTable(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
1763
1629
|
let query = table.query();
|
|
@@ -1765,6 +1631,10 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
|
|
|
1765
1631
|
if (args?.workflowName) {
|
|
1766
1632
|
conditions.push(`workflow_name = '${args.workflowName.replace(/'/g, "''")}'`);
|
|
1767
1633
|
}
|
|
1634
|
+
if (args?.status) {
|
|
1635
|
+
const escapedStatus = args.status.replace(/\\/g, "\\\\").replace(/'/g, "''").replace(/%/g, "\\%").replace(/_/g, "\\_");
|
|
1636
|
+
conditions.push(`\`snapshot\` LIKE '%"status":"${escapedStatus}","value"%'`);
|
|
1637
|
+
}
|
|
1768
1638
|
if (args?.resourceId) {
|
|
1769
1639
|
conditions.push(`\`resourceId\` = '${args.resourceId}'`);
|
|
1770
1640
|
}
|
|
@@ -1781,11 +1651,22 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
|
|
|
1781
1651
|
} else {
|
|
1782
1652
|
total = await table.countRows();
|
|
1783
1653
|
}
|
|
1784
|
-
if (args?.
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1654
|
+
if (args?.perPage !== void 0 && args?.page !== void 0) {
|
|
1655
|
+
const normalizedPerPage = storage.normalizePerPage(args.perPage, Number.MAX_SAFE_INTEGER);
|
|
1656
|
+
if (args.page < 0 || !Number.isInteger(args.page)) {
|
|
1657
|
+
throw new error.MastraError(
|
|
1658
|
+
{
|
|
1659
|
+
id: "LANCE_STORE_INVALID_PAGINATION_PARAMS",
|
|
1660
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1661
|
+
category: error.ErrorCategory.USER,
|
|
1662
|
+
details: { page: args.page, perPage: args.perPage }
|
|
1663
|
+
},
|
|
1664
|
+
new Error(`Invalid pagination parameters: page=${args.page}, perPage=${args.perPage}`)
|
|
1665
|
+
);
|
|
1666
|
+
}
|
|
1667
|
+
const offset = args.page * normalizedPerPage;
|
|
1668
|
+
query.limit(normalizedPerPage);
|
|
1669
|
+
query.offset(offset);
|
|
1789
1670
|
}
|
|
1790
1671
|
const records = await query.toArray();
|
|
1791
1672
|
return {
|
|
@@ -1795,10 +1676,10 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
|
|
|
1795
1676
|
} catch (error$1) {
|
|
1796
1677
|
throw new error.MastraError(
|
|
1797
1678
|
{
|
|
1798
|
-
id: "
|
|
1679
|
+
id: "LANCE_STORE_LIST_WORKFLOW_RUNS_FAILED",
|
|
1799
1680
|
domain: error.ErrorDomain.STORAGE,
|
|
1800
1681
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
1801
|
-
details: {
|
|
1682
|
+
details: { resourceId: args?.resourceId ?? "", workflowName: args?.workflowName ?? "" }
|
|
1802
1683
|
},
|
|
1803
1684
|
error$1
|
|
1804
1685
|
);
|
|
@@ -1812,6 +1693,8 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
1812
1693
|
lanceClient;
|
|
1813
1694
|
/**
|
|
1814
1695
|
* Creates a new instance of LanceStorage
|
|
1696
|
+
* @param id The unique identifier for this storage instance
|
|
1697
|
+
* @param name The name for this storage instance
|
|
1815
1698
|
* @param uri The URI to connect to LanceDB
|
|
1816
1699
|
* @param options connection options
|
|
1817
1700
|
*
|
|
@@ -1819,21 +1702,21 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
1819
1702
|
*
|
|
1820
1703
|
* Connect to a local database
|
|
1821
1704
|
* ```ts
|
|
1822
|
-
* const store = await LanceStorage.create('/path/to/db');
|
|
1705
|
+
* const store = await LanceStorage.create('my-storage-id', 'MyStorage', '/path/to/db');
|
|
1823
1706
|
* ```
|
|
1824
1707
|
*
|
|
1825
1708
|
* Connect to a LanceDB cloud database
|
|
1826
1709
|
* ```ts
|
|
1827
|
-
* const store = await LanceStorage.create('db://host:port');
|
|
1710
|
+
* const store = await LanceStorage.create('my-storage-id', 'MyStorage', 'db://host:port');
|
|
1828
1711
|
* ```
|
|
1829
1712
|
*
|
|
1830
1713
|
* Connect to a cloud database
|
|
1831
1714
|
* ```ts
|
|
1832
|
-
* const store = await LanceStorage.create('s3://bucket/db', { storageOptions: { timeout: '60s' } });
|
|
1715
|
+
* const store = await LanceStorage.create('my-storage-id', 'MyStorage', 's3://bucket/db', { storageOptions: { timeout: '60s' } });
|
|
1833
1716
|
* ```
|
|
1834
1717
|
*/
|
|
1835
|
-
static async create(name, uri, options) {
|
|
1836
|
-
const instance = new _LanceStorage(name);
|
|
1718
|
+
static async create(id, name, uri, options) {
|
|
1719
|
+
const instance = new _LanceStorage(id, name);
|
|
1837
1720
|
try {
|
|
1838
1721
|
instance.lanceClient = await lancedb.connect(uri, options);
|
|
1839
1722
|
const operations = new StoreOperationsLance({ client: instance.lanceClient });
|
|
@@ -1841,8 +1724,7 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
1841
1724
|
operations: new StoreOperationsLance({ client: instance.lanceClient }),
|
|
1842
1725
|
workflows: new StoreWorkflowsLance({ client: instance.lanceClient }),
|
|
1843
1726
|
scores: new StoreScoresLance({ client: instance.lanceClient }),
|
|
1844
|
-
memory: new StoreMemoryLance({ client: instance.lanceClient, operations })
|
|
1845
|
-
legacyEvals: new StoreLegacyEvalsLance({ client: instance.lanceClient })
|
|
1727
|
+
memory: new StoreMemoryLance({ client: instance.lanceClient, operations })
|
|
1846
1728
|
};
|
|
1847
1729
|
return instance;
|
|
1848
1730
|
} catch (e) {
|
|
@@ -1862,14 +1744,13 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
1862
1744
|
* @internal
|
|
1863
1745
|
* Private constructor to enforce using the create factory method
|
|
1864
1746
|
*/
|
|
1865
|
-
constructor(name) {
|
|
1866
|
-
super({ name });
|
|
1747
|
+
constructor(id, name) {
|
|
1748
|
+
super({ id, name });
|
|
1867
1749
|
const operations = new StoreOperationsLance({ client: this.lanceClient });
|
|
1868
1750
|
this.stores = {
|
|
1869
1751
|
operations: new StoreOperationsLance({ client: this.lanceClient }),
|
|
1870
1752
|
workflows: new StoreWorkflowsLance({ client: this.lanceClient }),
|
|
1871
1753
|
scores: new StoreScoresLance({ client: this.lanceClient }),
|
|
1872
|
-
legacyEvals: new StoreLegacyEvalsLance({ client: this.lanceClient }),
|
|
1873
1754
|
memory: new StoreMemoryLance({ client: this.lanceClient, operations })
|
|
1874
1755
|
};
|
|
1875
1756
|
}
|
|
@@ -1904,9 +1785,6 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
1904
1785
|
async getThreadById({ threadId }) {
|
|
1905
1786
|
return this.stores.memory.getThreadById({ threadId });
|
|
1906
1787
|
}
|
|
1907
|
-
async getThreadsByResourceId({ resourceId }) {
|
|
1908
|
-
return this.stores.memory.getThreadsByResourceId({ resourceId });
|
|
1909
|
-
}
|
|
1910
1788
|
/**
|
|
1911
1789
|
* Saves a thread to the database. This function doesn't overwrite existing threads.
|
|
1912
1790
|
* @param thread - The thread to save
|
|
@@ -1932,7 +1810,7 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
1932
1810
|
hasColumn: true,
|
|
1933
1811
|
createTable: true,
|
|
1934
1812
|
deleteMessages: false,
|
|
1935
|
-
|
|
1813
|
+
listScoresBySpan: true
|
|
1936
1814
|
};
|
|
1937
1815
|
}
|
|
1938
1816
|
async getResourceById({ resourceId }) {
|
|
@@ -1996,41 +1874,17 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
1996
1874
|
});
|
|
1997
1875
|
return Array.from(allIndices).sort((a, b) => a - b).map((index) => records[index]);
|
|
1998
1876
|
}
|
|
1999
|
-
async
|
|
2000
|
-
|
|
2001
|
-
resourceId,
|
|
2002
|
-
selectBy,
|
|
2003
|
-
format,
|
|
2004
|
-
threadConfig
|
|
2005
|
-
}) {
|
|
2006
|
-
return this.stores.memory.getMessages({ threadId, resourceId, selectBy, format, threadConfig });
|
|
2007
|
-
}
|
|
2008
|
-
async getMessagesById({
|
|
2009
|
-
messageIds,
|
|
2010
|
-
format
|
|
2011
|
-
}) {
|
|
2012
|
-
return this.stores.memory.getMessagesById({ messageIds, format });
|
|
1877
|
+
async listMessagesById({ messageIds }) {
|
|
1878
|
+
return this.stores.memory.listMessagesById({ messageIds });
|
|
2013
1879
|
}
|
|
2014
1880
|
async saveMessages(args) {
|
|
2015
1881
|
return this.stores.memory.saveMessages(args);
|
|
2016
1882
|
}
|
|
2017
|
-
async getThreadsByResourceIdPaginated(args) {
|
|
2018
|
-
return this.stores.memory.getThreadsByResourceIdPaginated(args);
|
|
2019
|
-
}
|
|
2020
|
-
async getMessagesPaginated(args) {
|
|
2021
|
-
return this.stores.memory.getMessagesPaginated(args);
|
|
2022
|
-
}
|
|
2023
1883
|
async updateMessages(_args) {
|
|
2024
1884
|
return this.stores.memory.updateMessages(_args);
|
|
2025
1885
|
}
|
|
2026
|
-
async
|
|
2027
|
-
return this.stores.
|
|
2028
|
-
}
|
|
2029
|
-
async getEvals(options) {
|
|
2030
|
-
return this.stores.legacyEvals.getEvals(options);
|
|
2031
|
-
}
|
|
2032
|
-
async getWorkflowRuns(args) {
|
|
2033
|
-
return this.stores.workflows.getWorkflowRuns(args);
|
|
1886
|
+
async listWorkflowRuns(args) {
|
|
1887
|
+
return this.stores.workflows.listWorkflowRuns(args);
|
|
2034
1888
|
}
|
|
2035
1889
|
async getWorkflowRunById(args) {
|
|
2036
1890
|
return this.stores.workflows.getWorkflowRunById(args);
|
|
@@ -2040,9 +1894,9 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
2040
1894
|
runId,
|
|
2041
1895
|
stepId,
|
|
2042
1896
|
result,
|
|
2043
|
-
|
|
1897
|
+
requestContext
|
|
2044
1898
|
}) {
|
|
2045
|
-
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result,
|
|
1899
|
+
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, requestContext });
|
|
2046
1900
|
}
|
|
2047
1901
|
async updateWorkflowState({
|
|
2048
1902
|
workflowName,
|
|
@@ -2068,37 +1922,37 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
|
|
|
2068
1922
|
async getScoreById({ id: _id }) {
|
|
2069
1923
|
return this.stores.scores.getScoreById({ id: _id });
|
|
2070
1924
|
}
|
|
2071
|
-
async
|
|
1925
|
+
async listScoresByScorerId({
|
|
2072
1926
|
scorerId,
|
|
2073
1927
|
source,
|
|
2074
1928
|
entityId,
|
|
2075
1929
|
entityType,
|
|
2076
1930
|
pagination
|
|
2077
1931
|
}) {
|
|
2078
|
-
return this.stores.scores.
|
|
1932
|
+
return this.stores.scores.listScoresByScorerId({ scorerId, source, pagination, entityId, entityType });
|
|
2079
1933
|
}
|
|
2080
1934
|
async saveScore(_score) {
|
|
2081
1935
|
return this.stores.scores.saveScore(_score);
|
|
2082
1936
|
}
|
|
2083
|
-
async
|
|
1937
|
+
async listScoresByRunId({
|
|
2084
1938
|
runId,
|
|
2085
1939
|
pagination
|
|
2086
1940
|
}) {
|
|
2087
|
-
return this.stores.scores.
|
|
1941
|
+
return this.stores.scores.listScoresByRunId({ runId, pagination });
|
|
2088
1942
|
}
|
|
2089
|
-
async
|
|
1943
|
+
async listScoresByEntityId({
|
|
2090
1944
|
entityId,
|
|
2091
1945
|
entityType,
|
|
2092
1946
|
pagination
|
|
2093
1947
|
}) {
|
|
2094
|
-
return this.stores.scores.
|
|
1948
|
+
return this.stores.scores.listScoresByEntityId({ entityId, entityType, pagination });
|
|
2095
1949
|
}
|
|
2096
|
-
async
|
|
1950
|
+
async listScoresBySpan({
|
|
2097
1951
|
traceId,
|
|
2098
1952
|
spanId,
|
|
2099
1953
|
pagination
|
|
2100
1954
|
}) {
|
|
2101
|
-
return this.stores.scores.
|
|
1955
|
+
return this.stores.scores.listScoresBySpan({ traceId, spanId, pagination });
|
|
2102
1956
|
}
|
|
2103
1957
|
};
|
|
2104
1958
|
var LanceFilterTranslator = class extends filter.BaseFilterTranslator {
|
|
@@ -2447,7 +2301,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2447
2301
|
* ```
|
|
2448
2302
|
*/
|
|
2449
2303
|
static async create(uri, options) {
|
|
2450
|
-
const instance = new _LanceVectorStore();
|
|
2304
|
+
const instance = new _LanceVectorStore(options?.id || crypto.randomUUID());
|
|
2451
2305
|
try {
|
|
2452
2306
|
instance.lanceClient = await lancedb.connect(uri, options);
|
|
2453
2307
|
return instance;
|
|
@@ -2467,8 +2321,8 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
|
|
|
2467
2321
|
* @internal
|
|
2468
2322
|
* Private constructor to enforce using the create factory method
|
|
2469
2323
|
*/
|
|
2470
|
-
constructor() {
|
|
2471
|
-
super();
|
|
2324
|
+
constructor(id) {
|
|
2325
|
+
super({ id });
|
|
2472
2326
|
}
|
|
2473
2327
|
close() {
|
|
2474
2328
|
if (this.lanceClient) {
|