@mastra/lance 0.0.0-update-stores-peerDeps-20250723031338 → 0.0.0-usechat-duplicate-20251016110554

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.
Files changed (52) hide show
  1. package/CHANGELOG.md +443 -2
  2. package/README.md +3 -3
  3. package/dist/index.cjs +199 -35
  4. package/dist/index.cjs.map +1 -0
  5. package/dist/index.d.ts +3 -2
  6. package/dist/index.d.ts.map +1 -0
  7. package/dist/index.js +199 -35
  8. package/dist/index.js.map +1 -0
  9. package/dist/storage/domains/legacy-evals/index.d.ts +25 -0
  10. package/dist/storage/domains/legacy-evals/index.d.ts.map +1 -0
  11. package/dist/storage/domains/memory/index.d.ts +103 -0
  12. package/dist/storage/domains/memory/index.d.ts.map +1 -0
  13. package/dist/storage/domains/operations/index.d.ts +40 -0
  14. package/dist/storage/domains/operations/index.d.ts.map +1 -0
  15. package/dist/storage/domains/scores/index.d.ts +50 -0
  16. package/dist/storage/domains/scores/index.d.ts.map +1 -0
  17. package/dist/storage/domains/traces/index.d.ts +34 -0
  18. package/dist/storage/domains/traces/index.d.ts.map +1 -0
  19. package/dist/storage/domains/utils.d.ts +10 -0
  20. package/dist/storage/domains/utils.d.ts.map +1 -0
  21. package/dist/storage/domains/workflows/index.d.ts +57 -0
  22. package/dist/storage/domains/workflows/index.d.ts.map +1 -0
  23. package/dist/storage/index.d.ts +272 -0
  24. package/dist/storage/index.d.ts.map +1 -0
  25. package/dist/vector/filter.d.ts +41 -0
  26. package/dist/vector/filter.d.ts.map +1 -0
  27. package/dist/vector/index.d.ts +85 -0
  28. package/dist/vector/index.d.ts.map +1 -0
  29. package/dist/vector/types.d.ts +15 -0
  30. package/dist/vector/types.d.ts.map +1 -0
  31. package/package.json +24 -10
  32. package/dist/_tsup-dts-rollup.d.cts +0 -680
  33. package/dist/_tsup-dts-rollup.d.ts +0 -680
  34. package/dist/index.d.cts +0 -2
  35. package/eslint.config.js +0 -6
  36. package/src/index.ts +0 -2
  37. package/src/storage/domains/legacy-evals/index.ts +0 -156
  38. package/src/storage/domains/memory/index.ts +0 -947
  39. package/src/storage/domains/operations/index.ts +0 -489
  40. package/src/storage/domains/scores/index.ts +0 -221
  41. package/src/storage/domains/traces/index.ts +0 -212
  42. package/src/storage/domains/utils.ts +0 -158
  43. package/src/storage/domains/workflows/index.ts +0 -207
  44. package/src/storage/index.test.ts +0 -10
  45. package/src/storage/index.ts +0 -441
  46. package/src/vector/filter.test.ts +0 -295
  47. package/src/vector/filter.ts +0 -443
  48. package/src/vector/index.test.ts +0 -1493
  49. package/src/vector/index.ts +0 -941
  50. package/src/vector/types.ts +0 -16
  51. package/tsconfig.json +0 -5
  52. package/vitest.config.ts +0 -11
@@ -1,441 +0,0 @@
1
- import { connect } from '@lancedb/lancedb';
2
- import type { Connection, ConnectionOptions } from '@lancedb/lancedb';
3
- import type { MastraMessageContentV2 } from '@mastra/core/agent';
4
- import { ErrorCategory, ErrorDomain, MastraError } from '@mastra/core/error';
5
- import type { MastraMessageV1, MastraMessageV2, StorageThreadType, TraceType } from '@mastra/core/memory';
6
- import type { ScoreRowData } from '@mastra/core/scores';
7
- import { MastraStorage } from '@mastra/core/storage';
8
- import type {
9
- TABLE_NAMES,
10
- PaginationInfo,
11
- StorageGetMessagesArg,
12
- StorageColumn,
13
- EvalRow,
14
- WorkflowRuns,
15
- StoragePagination,
16
- StorageDomains,
17
- StorageGetTracesPaginatedArg,
18
- StorageResourceType,
19
- } from '@mastra/core/storage';
20
- import type { Trace } from '@mastra/core/telemetry';
21
- import type { WorkflowRunState } from '@mastra/core/workflows';
22
- import { StoreLegacyEvalsLance } from './domains/legacy-evals';
23
- import { StoreMemoryLance } from './domains/memory';
24
- import { StoreOperationsLance } from './domains/operations';
25
- import { StoreScoresLance } from './domains/scores';
26
- import { StoreTracesLance } from './domains/traces';
27
- import { StoreWorkflowsLance } from './domains/workflows';
28
-
29
- export class LanceStorage extends MastraStorage {
30
- stores: StorageDomains;
31
- private lanceClient!: Connection;
32
- /**
33
- * Creates a new instance of LanceStorage
34
- * @param uri The URI to connect to LanceDB
35
- * @param options connection options
36
- *
37
- * Usage:
38
- *
39
- * Connect to a local database
40
- * ```ts
41
- * const store = await LanceStorage.create('/path/to/db');
42
- * ```
43
- *
44
- * Connect to a LanceDB cloud database
45
- * ```ts
46
- * const store = await LanceStorage.create('db://host:port');
47
- * ```
48
- *
49
- * Connect to a cloud database
50
- * ```ts
51
- * const store = await LanceStorage.create('s3://bucket/db', { storageOptions: { timeout: '60s' } });
52
- * ```
53
- */
54
- public static async create(name: string, uri: string, options?: ConnectionOptions): Promise<LanceStorage> {
55
- const instance = new LanceStorage(name);
56
- try {
57
- instance.lanceClient = await connect(uri, options);
58
- const operations = new StoreOperationsLance({ client: instance.lanceClient });
59
- instance.stores = {
60
- operations: new StoreOperationsLance({ client: instance.lanceClient }),
61
- workflows: new StoreWorkflowsLance({ client: instance.lanceClient }),
62
- traces: new StoreTracesLance({ client: instance.lanceClient, operations }),
63
- scores: new StoreScoresLance({ client: instance.lanceClient }),
64
- memory: new StoreMemoryLance({ client: instance.lanceClient, operations }),
65
- legacyEvals: new StoreLegacyEvalsLance({ client: instance.lanceClient }),
66
- };
67
- return instance;
68
- } catch (e: any) {
69
- throw new MastraError(
70
- {
71
- id: 'STORAGE_LANCE_STORAGE_CONNECT_FAILED',
72
- domain: ErrorDomain.STORAGE,
73
- category: ErrorCategory.THIRD_PARTY,
74
- text: `Failed to connect to LanceDB: ${e.message || e}`,
75
- details: { uri, optionsProvided: !!options },
76
- },
77
- e,
78
- );
79
- }
80
- }
81
-
82
- /**
83
- * @internal
84
- * Private constructor to enforce using the create factory method
85
- */
86
- private constructor(name: string) {
87
- super({ name });
88
- const operations = new StoreOperationsLance({ client: this.lanceClient });
89
-
90
- this.stores = {
91
- operations: new StoreOperationsLance({ client: this.lanceClient }),
92
- workflows: new StoreWorkflowsLance({ client: this.lanceClient }),
93
- traces: new StoreTracesLance({ client: this.lanceClient, operations }),
94
- scores: new StoreScoresLance({ client: this.lanceClient }),
95
- legacyEvals: new StoreLegacyEvalsLance({ client: this.lanceClient }),
96
- memory: new StoreMemoryLance({ client: this.lanceClient, operations }),
97
- };
98
- }
99
-
100
- async createTable({
101
- tableName,
102
- schema,
103
- }: {
104
- tableName: TABLE_NAMES;
105
- schema: Record<string, StorageColumn>;
106
- }): Promise<void> {
107
- return this.stores.operations.createTable({ tableName, schema });
108
- }
109
-
110
- async dropTable({ tableName }: { tableName: TABLE_NAMES }): Promise<void> {
111
- return this.stores.operations.dropTable({ tableName });
112
- }
113
-
114
- async alterTable({
115
- tableName,
116
- schema,
117
- ifNotExists,
118
- }: {
119
- tableName: TABLE_NAMES;
120
- schema: Record<string, StorageColumn>;
121
- ifNotExists: string[];
122
- }): Promise<void> {
123
- return this.stores.operations.alterTable({ tableName, schema, ifNotExists });
124
- }
125
-
126
- async clearTable({ tableName }: { tableName: TABLE_NAMES }): Promise<void> {
127
- return this.stores.operations.clearTable({ tableName });
128
- }
129
-
130
- async insert({ tableName, record }: { tableName: TABLE_NAMES; record: Record<string, any> }): Promise<void> {
131
- return this.stores.operations.insert({ tableName, record });
132
- }
133
-
134
- async batchInsert({ tableName, records }: { tableName: TABLE_NAMES; records: Record<string, any>[] }): Promise<void> {
135
- return this.stores.operations.batchInsert({ tableName, records });
136
- }
137
-
138
- async load({ tableName, keys }: { tableName: TABLE_NAMES; keys: Record<string, any> }): Promise<any> {
139
- return this.stores.operations.load({ tableName, keys });
140
- }
141
-
142
- async getThreadById({ threadId }: { threadId: string }): Promise<StorageThreadType | null> {
143
- return this.stores.memory.getThreadById({ threadId });
144
- }
145
-
146
- async getThreadsByResourceId({ resourceId }: { resourceId: string }): Promise<StorageThreadType[]> {
147
- return this.stores.memory.getThreadsByResourceId({ resourceId });
148
- }
149
-
150
- /**
151
- * Saves a thread to the database. This function doesn't overwrite existing threads.
152
- * @param thread - The thread to save
153
- * @returns The saved thread
154
- */
155
- async saveThread({ thread }: { thread: StorageThreadType }): Promise<StorageThreadType> {
156
- return this.stores.memory.saveThread({ thread });
157
- }
158
-
159
- async updateThread({
160
- id,
161
- title,
162
- metadata,
163
- }: {
164
- id: string;
165
- title: string;
166
- metadata: Record<string, unknown>;
167
- }): Promise<StorageThreadType> {
168
- return this.stores.memory.updateThread({ id, title, metadata });
169
- }
170
-
171
- async deleteThread({ threadId }: { threadId: string }): Promise<void> {
172
- return this.stores.memory.deleteThread({ threadId });
173
- }
174
-
175
- public get supports() {
176
- return {
177
- selectByIncludeResourceScope: true,
178
- resourceWorkingMemory: true,
179
- hasColumn: true,
180
- createTable: true,
181
- };
182
- }
183
-
184
- async getResourceById({ resourceId }: { resourceId: string }): Promise<StorageResourceType | null> {
185
- return this.stores.memory.getResourceById({ resourceId });
186
- }
187
-
188
- async saveResource({ resource }: { resource: StorageResourceType }): Promise<StorageResourceType> {
189
- return this.stores.memory.saveResource({ resource });
190
- }
191
-
192
- async updateResource({
193
- resourceId,
194
- workingMemory,
195
- metadata,
196
- }: {
197
- resourceId: string;
198
- workingMemory?: string;
199
- metadata?: Record<string, unknown>;
200
- }): Promise<StorageResourceType> {
201
- return this.stores.memory.updateResource({ resourceId, workingMemory, metadata });
202
- }
203
-
204
- /**
205
- * Processes messages to include context messages based on withPreviousMessages and withNextMessages
206
- * @param records - The sorted array of records to process
207
- * @param include - The array of include specifications with context parameters
208
- * @returns The processed array with context messages included
209
- */
210
- private processMessagesWithContext(
211
- records: any[],
212
- include: { id: string; withPreviousMessages?: number; withNextMessages?: number }[],
213
- ): any[] {
214
- const messagesWithContext = include.filter(item => item.withPreviousMessages || item.withNextMessages);
215
-
216
- if (messagesWithContext.length === 0) {
217
- return records;
218
- }
219
-
220
- // Create a map of message id to index in the sorted array for quick lookup
221
- const messageIndexMap = new Map<string, number>();
222
- records.forEach((message, index) => {
223
- messageIndexMap.set(message.id, index);
224
- });
225
-
226
- // Keep track of additional indices to include
227
- const additionalIndices = new Set<number>();
228
-
229
- for (const item of messagesWithContext) {
230
- const messageIndex = messageIndexMap.get(item.id);
231
-
232
- if (messageIndex !== undefined) {
233
- // Add previous messages if requested
234
- if (item.withPreviousMessages) {
235
- const startIdx = Math.max(0, messageIndex - item.withPreviousMessages);
236
- for (let i = startIdx; i < messageIndex; i++) {
237
- additionalIndices.add(i);
238
- }
239
- }
240
-
241
- // Add next messages if requested
242
- if (item.withNextMessages) {
243
- const endIdx = Math.min(records.length - 1, messageIndex + item.withNextMessages);
244
- for (let i = messageIndex + 1; i <= endIdx; i++) {
245
- additionalIndices.add(i);
246
- }
247
- }
248
- }
249
- }
250
-
251
- // If we need to include additional messages, create a new set of records
252
- if (additionalIndices.size === 0) {
253
- return records;
254
- }
255
-
256
- // Get IDs of the records that matched the original query
257
- const originalMatchIds = new Set(include.map(item => item.id));
258
-
259
- // Create a set of all indices we need to include
260
- const allIndices = new Set<number>();
261
-
262
- // Add indices of originally matched messages
263
- records.forEach((record, index) => {
264
- if (originalMatchIds.has(record.id)) {
265
- allIndices.add(index);
266
- }
267
- });
268
-
269
- // Add the additional context message indices
270
- additionalIndices.forEach(index => {
271
- allIndices.add(index);
272
- });
273
-
274
- // Create a new filtered array with only the required messages
275
- // while maintaining chronological order
276
- return Array.from(allIndices)
277
- .sort((a, b) => a - b)
278
- .map(index => records[index]);
279
- }
280
-
281
- public async getMessages(args: StorageGetMessagesArg & { format?: 'v1' }): Promise<MastraMessageV1[]>;
282
- public async getMessages(args: StorageGetMessagesArg & { format: 'v2' }): Promise<MastraMessageV2[]>;
283
- public async getMessages({
284
- threadId,
285
- resourceId,
286
- selectBy,
287
- format,
288
- threadConfig,
289
- }: StorageGetMessagesArg & { format?: 'v1' | 'v2' }): Promise<MastraMessageV1[] | MastraMessageV2[]> {
290
- return this.stores.memory.getMessages({ threadId, resourceId, selectBy, format, threadConfig });
291
- }
292
-
293
- async saveMessages(args: { messages: MastraMessageV1[]; format?: undefined | 'v1' }): Promise<MastraMessageV1[]>;
294
- async saveMessages(args: { messages: MastraMessageV2[]; format: 'v2' }): Promise<MastraMessageV2[]>;
295
- async saveMessages(
296
- args: { messages: MastraMessageV1[]; format?: undefined | 'v1' } | { messages: MastraMessageV2[]; format: 'v2' },
297
- ): Promise<MastraMessageV2[] | MastraMessageV1[]> {
298
- return this.stores.memory.saveMessages(args);
299
- }
300
-
301
- async getThreadsByResourceIdPaginated(args: {
302
- resourceId: string;
303
- page: number;
304
- perPage: number;
305
- }): Promise<PaginationInfo & { threads: StorageThreadType[] }> {
306
- return this.stores.memory.getThreadsByResourceIdPaginated(args);
307
- }
308
-
309
- async getMessagesPaginated(
310
- args: StorageGetMessagesArg & { format?: 'v1' | 'v2' },
311
- ): Promise<PaginationInfo & { messages: MastraMessageV1[] | MastraMessageV2[] }> {
312
- return this.stores.memory.getMessagesPaginated(args);
313
- }
314
-
315
- async updateMessages(_args: {
316
- messages: Partial<Omit<MastraMessageV2, 'createdAt'>> &
317
- {
318
- id: string;
319
- content?: { metadata?: MastraMessageContentV2['metadata']; content?: MastraMessageContentV2['content'] };
320
- }[];
321
- }): Promise<MastraMessageV2[]> {
322
- return this.stores.memory.updateMessages(_args);
323
- }
324
-
325
- async getTraceById(args: { traceId: string }): Promise<TraceType> {
326
- return (this.stores as any).traces.getTraceById(args);
327
- }
328
-
329
- async getTraces(args: {
330
- name?: string;
331
- scope?: string;
332
- page: number;
333
- perPage: number;
334
- attributes?: Record<string, string>;
335
- }): Promise<Trace[]> {
336
- return (this.stores as any).traces.getTraces(args);
337
- }
338
-
339
- async getTracesPaginated(args: StorageGetTracesPaginatedArg): Promise<PaginationInfo & { traces: Trace[] }> {
340
- return (this.stores as any).traces.getTracesPaginated(args);
341
- }
342
-
343
- async getEvalsByAgentName(agentName: string, type?: 'test' | 'live'): Promise<EvalRow[]> {
344
- return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
345
- }
346
-
347
- async getEvals(options: {
348
- agentName?: string;
349
- type?: 'test' | 'live';
350
- page?: number;
351
- perPage?: number;
352
- fromDate?: Date;
353
- toDate?: Date;
354
- dateRange?: { start?: Date; end?: Date };
355
- }): Promise<PaginationInfo & { evals: EvalRow[] }> {
356
- return this.stores.legacyEvals.getEvals(options);
357
- }
358
-
359
- async getWorkflowRuns(args?: {
360
- namespace?: string;
361
- workflowName?: string;
362
- fromDate?: Date;
363
- toDate?: Date;
364
- limit?: number;
365
- offset?: number;
366
- }): Promise<WorkflowRuns> {
367
- return this.stores.workflows.getWorkflowRuns(args);
368
- }
369
-
370
- async getWorkflowRunById(args: { runId: string; workflowName?: string }): Promise<{
371
- workflowName: string;
372
- runId: string;
373
- snapshot: any;
374
- createdAt: Date;
375
- updatedAt: Date;
376
- } | null> {
377
- return this.stores.workflows.getWorkflowRunById(args);
378
- }
379
-
380
- async persistWorkflowSnapshot({
381
- workflowName,
382
- runId,
383
- snapshot,
384
- }: {
385
- workflowName: string;
386
- runId: string;
387
- snapshot: WorkflowRunState;
388
- }): Promise<void> {
389
- return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, snapshot });
390
- }
391
-
392
- async loadWorkflowSnapshot({
393
- workflowName,
394
- runId,
395
- }: {
396
- workflowName: string;
397
- runId: string;
398
- }): Promise<WorkflowRunState | null> {
399
- return this.stores.workflows.loadWorkflowSnapshot({ workflowName, runId });
400
- }
401
-
402
- async getScoreById({ id: _id }: { id: string }): Promise<ScoreRowData | null> {
403
- return this.stores.scores.getScoreById({ id: _id });
404
- }
405
-
406
- async getScoresByScorerId({
407
- scorerId,
408
- pagination,
409
- }: {
410
- scorerId: string;
411
- pagination: StoragePagination;
412
- }): Promise<{ pagination: PaginationInfo; scores: ScoreRowData[] }> {
413
- return this.stores.scores.getScoresByScorerId({ scorerId, pagination });
414
- }
415
-
416
- async saveScore(_score: ScoreRowData): Promise<{ score: ScoreRowData }> {
417
- return this.stores.scores.saveScore(_score);
418
- }
419
-
420
- async getScoresByRunId({
421
- runId,
422
- pagination,
423
- }: {
424
- runId: string;
425
- pagination: StoragePagination;
426
- }): Promise<{ pagination: PaginationInfo; scores: ScoreRowData[] }> {
427
- return this.stores.scores.getScoresByRunId({ runId, pagination });
428
- }
429
-
430
- async getScoresByEntityId({
431
- entityId,
432
- entityType,
433
- pagination,
434
- }: {
435
- pagination: StoragePagination;
436
- entityId: string;
437
- entityType: string;
438
- }): Promise<{ pagination: PaginationInfo; scores: ScoreRowData[] }> {
439
- return this.stores.scores.getScoresByEntityId({ entityId, entityType, pagination });
440
- }
441
- }