@mastra/libsql 0.0.1-alpha.1

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.
@@ -0,0 +1,344 @@
1
+ import { createClient } from '@libsql/client';
2
+ import type { Client as TursoClient, InValue } from '@libsql/client';
3
+ import { MastraVector } from '@mastra/core/vector';
4
+ import type {
5
+ IndexStats,
6
+ QueryResult,
7
+ QueryVectorParams,
8
+ CreateIndexParams,
9
+ UpsertVectorParams,
10
+ ParamsToArgs,
11
+ QueryVectorArgs,
12
+ } from '@mastra/core/vector';
13
+ import type { VectorFilter } from '@mastra/core/vector/filter';
14
+ import { LibSQLFilterTranslator } from './filter';
15
+ import { buildFilterQuery } from './sql-builder';
16
+
17
+ interface LibSQLQueryParams extends QueryVectorParams {
18
+ minScore?: number;
19
+ }
20
+
21
+ type LibSQLQueryArgs = [...QueryVectorArgs, number?];
22
+
23
+ export class LibSQLVector extends MastraVector {
24
+ private turso: TursoClient;
25
+
26
+ constructor({
27
+ connectionUrl,
28
+ authToken,
29
+ syncUrl,
30
+ syncInterval,
31
+ }: {
32
+ connectionUrl: string;
33
+ authToken?: string;
34
+ syncUrl?: string;
35
+ syncInterval?: number;
36
+ }) {
37
+ super();
38
+
39
+ this.turso = createClient({
40
+ url: connectionUrl,
41
+ syncUrl: syncUrl,
42
+ authToken,
43
+ syncInterval,
44
+ });
45
+
46
+ if (connectionUrl.includes(`file:`) || connectionUrl.includes(`:memory:`)) {
47
+ void this.turso.execute({
48
+ sql: 'PRAGMA journal_mode=WAL;',
49
+ args: {},
50
+ });
51
+ }
52
+ }
53
+
54
+ transformFilter(filter?: VectorFilter) {
55
+ const translator = new LibSQLFilterTranslator();
56
+ return translator.translate(filter);
57
+ }
58
+
59
+ async query(...args: ParamsToArgs<LibSQLQueryParams> | LibSQLQueryArgs): Promise<QueryResult[]> {
60
+ const params = this.normalizeArgs<LibSQLQueryParams, LibSQLQueryArgs>('query', args, ['minScore']);
61
+
62
+ try {
63
+ const { indexName, queryVector, topK = 10, filter, includeVector = false, minScore = 0 } = params;
64
+
65
+ const vectorStr = `[${queryVector.join(',')}]`;
66
+
67
+ const translatedFilter = this.transformFilter(filter);
68
+ const { sql: filterQuery, values: filterValues } = buildFilterQuery(translatedFilter);
69
+ filterValues.push(minScore);
70
+
71
+ const query = `
72
+ WITH vector_scores AS (
73
+ SELECT
74
+ vector_id as id,
75
+ (1-vector_distance_cos(embedding, '${vectorStr}')) as score,
76
+ metadata
77
+ ${includeVector ? ', vector_extract(embedding) as embedding' : ''}
78
+ FROM ${indexName}
79
+ ${filterQuery}
80
+ )
81
+ SELECT *
82
+ FROM vector_scores
83
+ WHERE score > ?
84
+ ORDER BY score DESC
85
+ LIMIT ${topK}`;
86
+
87
+ const result = await this.turso.execute({
88
+ sql: query,
89
+ args: filterValues,
90
+ });
91
+
92
+ return result.rows.map(({ id, score, metadata, embedding }) => ({
93
+ id: id as string,
94
+ score: score as number,
95
+ metadata: JSON.parse((metadata as string) ?? '{}'),
96
+ ...(includeVector && embedding && { vector: JSON.parse(embedding as string) }),
97
+ }));
98
+ } finally {
99
+ // client.release()
100
+ }
101
+ }
102
+
103
+ async upsert(...args: ParamsToArgs<UpsertVectorParams>): Promise<string[]> {
104
+ const params = this.normalizeArgs<UpsertVectorParams>('upsert', args);
105
+
106
+ const { indexName, vectors, metadata, ids } = params;
107
+ const tx = await this.turso.transaction('write');
108
+
109
+ try {
110
+ const vectorIds = ids || vectors.map(() => crypto.randomUUID());
111
+
112
+ for (let i = 0; i < vectors.length; i++) {
113
+ const query = `
114
+ INSERT INTO ${indexName} (vector_id, embedding, metadata)
115
+ VALUES (?, vector32(?), ?)
116
+ ON CONFLICT(vector_id) DO UPDATE SET
117
+ embedding = vector32(?),
118
+ metadata = ?
119
+ `;
120
+
121
+ // console.log('INSERTQ', query, [
122
+ // vectorIds[i] as InValue,
123
+ // JSON.stringify(vectors[i]),
124
+ // JSON.stringify(metadata?.[i] || {}),
125
+ // JSON.stringify(vectors[i]),
126
+ // JSON.stringify(metadata?.[i] || {}),
127
+ // ]);
128
+ await tx.execute({
129
+ sql: query,
130
+ // @ts-ignore
131
+ args: [
132
+ vectorIds[i] as InValue,
133
+ JSON.stringify(vectors[i]),
134
+ JSON.stringify(metadata?.[i] || {}),
135
+ JSON.stringify(vectors[i]),
136
+ JSON.stringify(metadata?.[i] || {}),
137
+ ],
138
+ });
139
+ }
140
+
141
+ await tx.commit();
142
+ return vectorIds;
143
+ } catch (error) {
144
+ await tx.rollback();
145
+ if (error instanceof Error && error.message?.includes('dimensions are different')) {
146
+ const match = error.message.match(/dimensions are different: (\d+) != (\d+)/);
147
+ if (match) {
148
+ const [, actual, expected] = match;
149
+ throw new Error(
150
+ `Vector dimension mismatch: Index "${indexName}" expects ${expected} dimensions but got ${actual} dimensions. ` +
151
+ `Either use a matching embedding model or delete and recreate the index with the new dimension.`,
152
+ );
153
+ }
154
+ }
155
+ throw error;
156
+ }
157
+ }
158
+
159
+ async createIndex(...args: ParamsToArgs<CreateIndexParams>): Promise<void> {
160
+ const params = this.normalizeArgs<CreateIndexParams>('createIndex', args);
161
+
162
+ const { indexName, dimension } = params;
163
+ try {
164
+ // Validate inputs
165
+ if (!indexName.match(/^[a-zA-Z_][a-zA-Z0-9_]*$/)) {
166
+ throw new Error('Invalid index name format');
167
+ }
168
+ if (!Number.isInteger(dimension) || dimension <= 0) {
169
+ throw new Error('Dimension must be a positive integer');
170
+ }
171
+
172
+ // Create the table with explicit schema
173
+ await this.turso.execute({
174
+ sql: `
175
+ CREATE TABLE IF NOT EXISTS ${indexName} (
176
+ id SERIAL PRIMARY KEY,
177
+ vector_id TEXT UNIQUE NOT NULL,
178
+ embedding F32_BLOB(${dimension}),
179
+ metadata TEXT DEFAULT '{}'
180
+ );
181
+ `,
182
+ args: [],
183
+ });
184
+
185
+ await this.turso.execute({
186
+ sql: `
187
+ CREATE INDEX IF NOT EXISTS ${indexName}_vector_idx
188
+ ON ${indexName} (libsql_vector_idx(embedding))
189
+ `,
190
+ args: [],
191
+ });
192
+ } catch (error: any) {
193
+ console.error('Failed to create vector table:', error);
194
+ throw error;
195
+ } finally {
196
+ // client.release()
197
+ }
198
+ }
199
+
200
+ async deleteIndex(indexName: string): Promise<void> {
201
+ try {
202
+ // Drop the table
203
+ await this.turso.execute({
204
+ sql: `DROP TABLE IF EXISTS ${indexName}`,
205
+ args: [],
206
+ });
207
+ } catch (error: any) {
208
+ console.error('Failed to delete vector table:', error);
209
+ throw new Error(`Failed to delete vector table: ${error.message}`);
210
+ } finally {
211
+ // client.release()
212
+ }
213
+ }
214
+
215
+ async listIndexes(): Promise<string[]> {
216
+ try {
217
+ const vectorTablesQuery = `
218
+ SELECT name FROM sqlite_master
219
+ WHERE type='table'
220
+ AND sql LIKE '%F32_BLOB%';
221
+ `;
222
+ const result = await this.turso.execute({
223
+ sql: vectorTablesQuery,
224
+ args: [],
225
+ });
226
+ return result.rows.map(row => row.name as string);
227
+ } catch (error: any) {
228
+ throw new Error(`Failed to list vector tables: ${error.message}`);
229
+ }
230
+ }
231
+
232
+ async describeIndex(indexName: string): Promise<IndexStats> {
233
+ try {
234
+ // Get table info including column info
235
+ const tableInfoQuery = `
236
+ SELECT sql
237
+ FROM sqlite_master
238
+ WHERE type='table'
239
+ AND name = ?;
240
+ `;
241
+ const tableInfo = await this.turso.execute({
242
+ sql: tableInfoQuery,
243
+ args: [indexName],
244
+ });
245
+
246
+ if (!tableInfo.rows[0]?.sql) {
247
+ throw new Error(`Table ${indexName} not found`);
248
+ }
249
+
250
+ // Extract dimension from F32_BLOB definition
251
+ const dimension = parseInt((tableInfo.rows[0].sql as string).match(/F32_BLOB\((\d+)\)/)?.[1] || '0');
252
+
253
+ // Get row count
254
+ const countQuery = `
255
+ SELECT COUNT(*) as count
256
+ FROM ${indexName};
257
+ `;
258
+ const countResult = await this.turso.execute({
259
+ sql: countQuery,
260
+ args: [],
261
+ });
262
+
263
+ // LibSQL only supports cosine similarity currently
264
+ const metric: 'cosine' | 'euclidean' | 'dotproduct' = 'cosine';
265
+
266
+ return {
267
+ dimension,
268
+ count: (countResult?.rows?.[0]?.count as number) ?? 0,
269
+ metric,
270
+ };
271
+ } catch (e: any) {
272
+ throw new Error(`Failed to describe vector table: ${e.message}`);
273
+ }
274
+ }
275
+
276
+ /**
277
+ * Updates an index entry by its ID with the provided vector and/or metadata.
278
+ *
279
+ * @param indexName - The name of the index to update.
280
+ * @param id - The ID of the index entry to update.
281
+ * @param update - An object containing the vector and/or metadata to update.
282
+ * @param update.vector - An optional array of numbers representing the new vector.
283
+ * @param update.metadata - An optional record containing the new metadata.
284
+ * @returns A promise that resolves when the update is complete.
285
+ * @throws Will throw an error if no updates are provided or if the update operation fails.
286
+ */
287
+ async updateIndexById(
288
+ indexName: string,
289
+ id: string,
290
+ update: { vector?: number[]; metadata?: Record<string, any> },
291
+ ): Promise<void> {
292
+ try {
293
+ const updates = [];
294
+ const args: InValue[] = [];
295
+
296
+ if (update.vector) {
297
+ updates.push('embedding = vector32(?)');
298
+ args.push(JSON.stringify(update.vector));
299
+ }
300
+
301
+ if (update.metadata) {
302
+ updates.push('metadata = ?');
303
+ args.push(JSON.stringify(update.metadata));
304
+ }
305
+
306
+ if (updates.length === 0) {
307
+ throw new Error('No updates provided');
308
+ }
309
+
310
+ args.push(id);
311
+
312
+ const query = `
313
+ UPDATE ${indexName}
314
+ SET ${updates.join(', ')}
315
+ WHERE vector_id = ?;
316
+ `;
317
+
318
+ await this.turso.execute({
319
+ sql: query,
320
+ args,
321
+ });
322
+ } catch (error: any) {
323
+ throw new Error(`Failed to update index by id: ${id} for index: ${indexName}: ${error.message}`);
324
+ }
325
+ }
326
+
327
+ async deleteIndexById(indexName: string, id: string): Promise<void> {
328
+ try {
329
+ await this.turso.execute({
330
+ sql: `DELETE FROM ${indexName} WHERE vector_id = ?`,
331
+ args: [id],
332
+ });
333
+ } catch (error: any) {
334
+ throw new Error(`Failed to delete index by id: ${id} for index: ${indexName}: ${error.message}`);
335
+ }
336
+ }
337
+
338
+ async truncateIndex(indexName: string) {
339
+ await this.turso.execute({
340
+ sql: `DELETE FROM ${indexName}`,
341
+ args: [],
342
+ });
343
+ }
344
+ }