@mastra/libsql 0.0.0-agui-20250501182100

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
+ }
@@ -0,0 +1,101 @@
1
+ /**
2
+ * Vector store specific prompt that details supported operators and examples.
3
+ * This prompt helps users construct valid filters for LibSQL Vector.
4
+ */
5
+ export const LIBSQL_PROMPT = `When querying LibSQL Vector, you can ONLY use the operators listed below. Any other operators will be rejected.
6
+ Important: Don't explain how to construct the filter - use the specified operators and fields to search the content and return relevant results.
7
+ If a user tries to give an explicit operator that is not supported, reject the filter entirely and let them know that the operator is not supported.
8
+
9
+ Basic Comparison Operators:
10
+ - $eq: Exact match (default when using field: value)
11
+ Example: { "category": "electronics" }
12
+ - $ne: Not equal
13
+ Example: { "category": { "$ne": "electronics" } }
14
+ - $gt: Greater than
15
+ Example: { "price": { "$gt": 100 } }
16
+ - $gte: Greater than or equal
17
+ Example: { "price": { "$gte": 100 } }
18
+ - $lt: Less than
19
+ Example: { "price": { "$lt": 100 } }
20
+ - $lte: Less than or equal
21
+ Example: { "price": { "$lte": 100 } }
22
+
23
+ Array Operators:
24
+ - $in: Match any value in array
25
+ Example: { "category": { "$in": ["electronics", "books"] } }
26
+ - $nin: Does not match any value in array
27
+ Example: { "category": { "$nin": ["electronics", "books"] } }
28
+ - $all: Match all values in array
29
+ Example: { "tags": { "$all": ["premium", "sale"] } }
30
+ - $elemMatch: Match array elements that meet all specified conditions
31
+ Example: { "items": { "$elemMatch": { "price": { "$gt": 100 } } } }
32
+ - $contains: Check if array contains value
33
+ Example: { "tags": { "$contains": "premium" } }
34
+
35
+ Logical Operators:
36
+ - $and: Logical AND (implicit when using multiple conditions)
37
+ Example: { "$and": [{ "price": { "$gt": 100 } }, { "category": "electronics" }] }
38
+ - $or: Logical OR
39
+ Example: { "$or": [{ "price": { "$lt": 50 } }, { "category": "books" }] }
40
+ - $not: Logical NOT
41
+ Example: { "$not": { "category": "electronics" } }
42
+ - $nor: Logical NOR
43
+ Example: { "$nor": [{ "price": { "$lt": 50 } }, { "category": "books" }] }
44
+
45
+ Element Operators:
46
+ - $exists: Check if field exists
47
+ Example: { "rating": { "$exists": true } }
48
+
49
+ Special Operators:
50
+ - $size: Array length check
51
+ Example: { "tags": { "$size": 2 } }
52
+
53
+ Restrictions:
54
+ - Regex patterns are not supported
55
+ - Direct RegExp patterns will throw an error
56
+ - Nested fields are supported using dot notation
57
+ - Multiple conditions on the same field are supported with both implicit and explicit $and
58
+ - Array operations work on array fields only
59
+ - Basic operators handle array values as JSON strings
60
+ - Empty arrays in conditions are handled gracefully
61
+ - Only logical operators ($and, $or, $not, $nor) can be used at the top level
62
+ - All other operators must be used within a field condition
63
+ Valid: { "field": { "$gt": 100 } }
64
+ Valid: { "$and": [...] }
65
+ Invalid: { "$gt": 100 }
66
+ Invalid: { "$contains": "value" }
67
+ - Logical operators must contain field conditions, not direct operators
68
+ Valid: { "$and": [{ "field": { "$gt": 100 } }] }
69
+ Invalid: { "$and": [{ "$gt": 100 }] }
70
+ - $not operator:
71
+ - Must be an object
72
+ - Cannot be empty
73
+ - Can be used at field level or top level
74
+ - Valid: { "$not": { "field": "value" } }
75
+ - Valid: { "field": { "$not": { "$eq": "value" } } }
76
+ - Other logical operators ($and, $or, $nor):
77
+ - Can only be used at top level or nested within other logical operators
78
+ - Can not be used on a field level, or be nested inside a field
79
+ - Can not be used inside an operator
80
+ - Valid: { "$and": [{ "field": { "$gt": 100 } }] }
81
+ - Valid: { "$or": [{ "$and": [{ "field": { "$gt": 100 } }] }] }
82
+ - Invalid: { "field": { "$and": [{ "$gt": 100 }] } }
83
+ - Invalid: { "field": { "$or": [{ "$gt": 100 }] } }
84
+ - Invalid: { "field": { "$gt": { "$and": [{...}] } } }
85
+ - $elemMatch requires an object with conditions
86
+ Valid: { "array": { "$elemMatch": { "field": "value" } } }
87
+ Invalid: { "array": { "$elemMatch": "value" } }
88
+
89
+ Example Complex Query:
90
+ {
91
+ "$and": [
92
+ { "category": { "$in": ["electronics", "computers"] } },
93
+ { "price": { "$gte": 100, "$lte": 1000 } },
94
+ { "tags": { "$all": ["premium", "sale"] } },
95
+ { "items": { "$elemMatch": { "price": { "$gt": 50 }, "inStock": true } } },
96
+ { "$or": [
97
+ { "stock": { "$gt": 0 } },
98
+ { "preorder": true }
99
+ ]}
100
+ ]
101
+ }`;