@mastra/libsql 0.10.4-alpha.0 → 0.10.4-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.
@@ -1,6 +1,7 @@
1
1
  import { createClient } from '@libsql/client';
2
2
  import type { Client as TursoClient, InValue } from '@libsql/client';
3
3
 
4
+ import { ErrorCategory, ErrorDomain, MastraError } from '@mastra/core/error';
4
5
  import { parseSqlIdentifier } from '@mastra/core/utils';
5
6
  import { MastraVector } from '@mastra/core/vector';
6
7
  import type {
@@ -128,7 +129,18 @@ export class LibSQLVector extends MastraVector {
128
129
  if (!Array.isArray(queryVector) || !queryVector.every(x => typeof x === 'number' && Number.isFinite(x))) {
129
130
  throw new Error('queryVector must be an array of finite numbers');
130
131
  }
132
+ } catch (error) {
133
+ throw new MastraError(
134
+ {
135
+ id: 'LIBSQL_VECTOR_QUERY_INVALID_ARGS',
136
+ domain: ErrorDomain.STORAGE,
137
+ category: ErrorCategory.USER,
138
+ },
139
+ error,
140
+ );
141
+ }
131
142
 
143
+ try {
132
144
  const parsedIndexName = parseSqlIdentifier(indexName, 'index name');
133
145
 
134
146
  const vectorStr = `[${queryVector.join(',')}]`;
@@ -165,13 +177,31 @@ export class LibSQLVector extends MastraVector {
165
177
  metadata: JSON.parse((metadata as string) ?? '{}'),
166
178
  ...(includeVector && embedding && { vector: JSON.parse(embedding as string) }),
167
179
  }));
168
- } finally {
169
- // client.release()
180
+ } catch (error) {
181
+ throw new MastraError(
182
+ {
183
+ id: 'LIBSQL_VECTOR_QUERY_FAILED',
184
+ domain: ErrorDomain.STORAGE,
185
+ category: ErrorCategory.THIRD_PARTY,
186
+ },
187
+ error,
188
+ );
170
189
  }
171
190
  }
172
191
 
173
192
  public upsert(args: UpsertVectorParams): Promise<string[]> {
174
- return this.executeWriteOperationWithRetry(() => this.doUpsert(args), true);
193
+ try {
194
+ return this.executeWriteOperationWithRetry(() => this.doUpsert(args), true);
195
+ } catch (error) {
196
+ throw new MastraError(
197
+ {
198
+ id: 'LIBSQL_VECTOR_UPSERT_FAILED',
199
+ domain: ErrorDomain.STORAGE,
200
+ category: ErrorCategory.THIRD_PARTY,
201
+ },
202
+ error,
203
+ );
204
+ }
175
205
  }
176
206
 
177
207
  private async doUpsert({ indexName, vectors, metadata, ids }: UpsertVectorParams): Promise<string[]> {
@@ -218,7 +248,19 @@ export class LibSQLVector extends MastraVector {
218
248
  }
219
249
 
220
250
  public createIndex(args: CreateIndexParams): Promise<void> {
221
- return this.executeWriteOperationWithRetry(() => this.doCreateIndex(args));
251
+ try {
252
+ return this.executeWriteOperationWithRetry(() => this.doCreateIndex(args));
253
+ } catch (error) {
254
+ throw new MastraError(
255
+ {
256
+ id: 'LIBSQL_VECTOR_CREATE_INDEX_FAILED',
257
+ domain: ErrorDomain.STORAGE,
258
+ category: ErrorCategory.THIRD_PARTY,
259
+ details: { indexName: args.indexName, dimension: args.dimension },
260
+ },
261
+ error,
262
+ );
263
+ }
222
264
  }
223
265
 
224
266
  private async doCreateIndex({ indexName, dimension }: CreateIndexParams): Promise<void> {
@@ -247,7 +289,19 @@ export class LibSQLVector extends MastraVector {
247
289
  }
248
290
 
249
291
  public deleteIndex(args: DeleteIndexParams): Promise<void> {
250
- return this.executeWriteOperationWithRetry(() => this.doDeleteIndex(args));
292
+ try {
293
+ return this.executeWriteOperationWithRetry(() => this.doDeleteIndex(args));
294
+ } catch (error) {
295
+ throw new MastraError(
296
+ {
297
+ id: 'LIBSQL_VECTOR_DELETE_INDEX_FAILED',
298
+ domain: ErrorDomain.STORAGE,
299
+ category: ErrorCategory.THIRD_PARTY,
300
+ details: { indexName: args.indexName },
301
+ },
302
+ error,
303
+ );
304
+ }
251
305
  }
252
306
 
253
307
  private async doDeleteIndex({ indexName }: DeleteIndexParams): Promise<void> {
@@ -271,7 +325,14 @@ export class LibSQLVector extends MastraVector {
271
325
  });
272
326
  return result.rows.map(row => row.name as string);
273
327
  } catch (error: any) {
274
- throw new Error(`Failed to list vector tables: ${error.message}`);
328
+ throw new MastraError(
329
+ {
330
+ id: 'LIBSQL_VECTOR_LIST_INDEXES_FAILED',
331
+ domain: ErrorDomain.STORAGE,
332
+ category: ErrorCategory.THIRD_PARTY,
333
+ },
334
+ error,
335
+ );
275
336
  }
276
337
  }
277
338
 
@@ -322,7 +383,15 @@ export class LibSQLVector extends MastraVector {
322
383
  metric,
323
384
  };
324
385
  } catch (e: any) {
325
- throw new Error(`Failed to describe vector table: ${e.message}`);
386
+ throw new MastraError(
387
+ {
388
+ id: 'LIBSQL_VECTOR_DESCRIBE_INDEX_FAILED',
389
+ domain: ErrorDomain.STORAGE,
390
+ category: ErrorCategory.THIRD_PARTY,
391
+ details: { indexName },
392
+ },
393
+ e,
394
+ );
326
395
  }
327
396
  }
328
397
 
@@ -357,7 +426,13 @@ export class LibSQLVector extends MastraVector {
357
426
  }
358
427
 
359
428
  if (updates.length === 0) {
360
- throw new Error('No updates provided');
429
+ throw new MastraError({
430
+ id: 'LIBSQL_VECTOR_UPDATE_VECTOR_INVALID_ARGS',
431
+ domain: ErrorDomain.STORAGE,
432
+ category: ErrorCategory.USER,
433
+ details: { indexName, id },
434
+ text: 'No updates provided',
435
+ });
361
436
  }
362
437
  args.push(id);
363
438
  const query = `
@@ -365,10 +440,23 @@ export class LibSQLVector extends MastraVector {
365
440
  SET ${updates.join(', ')}
366
441
  WHERE vector_id = ?;
367
442
  `;
368
- await this.turso.execute({
369
- sql: query,
370
- args,
371
- });
443
+
444
+ try {
445
+ await this.turso.execute({
446
+ sql: query,
447
+ args,
448
+ });
449
+ } catch (error) {
450
+ throw new MastraError(
451
+ {
452
+ id: 'LIBSQL_VECTOR_UPDATE_VECTOR_FAILED',
453
+ domain: ErrorDomain.STORAGE,
454
+ category: ErrorCategory.THIRD_PARTY,
455
+ details: { indexName, id },
456
+ },
457
+ error,
458
+ );
459
+ }
372
460
  }
373
461
 
374
462
  /**
@@ -379,7 +467,19 @@ export class LibSQLVector extends MastraVector {
379
467
  * @throws Will throw an error if the deletion operation fails.
380
468
  */
381
469
  public deleteVector(args: DeleteVectorParams): Promise<void> {
382
- return this.executeWriteOperationWithRetry(() => this.doDeleteVector(args));
470
+ try {
471
+ return this.executeWriteOperationWithRetry(() => this.doDeleteVector(args));
472
+ } catch (error) {
473
+ throw new MastraError(
474
+ {
475
+ id: 'LIBSQL_VECTOR_DELETE_VECTOR_FAILED',
476
+ domain: ErrorDomain.STORAGE,
477
+ category: ErrorCategory.THIRD_PARTY,
478
+ details: { indexName: args.indexName, id: args.id },
479
+ },
480
+ error,
481
+ );
482
+ }
383
483
  }
384
484
 
385
485
  private async doDeleteVector({ indexName, id }: DeleteVectorParams): Promise<void> {
@@ -391,7 +491,19 @@ export class LibSQLVector extends MastraVector {
391
491
  }
392
492
 
393
493
  public truncateIndex(args: DeleteIndexParams): Promise<void> {
394
- return this.executeWriteOperationWithRetry(() => this._doTruncateIndex(args));
494
+ try {
495
+ return this.executeWriteOperationWithRetry(() => this._doTruncateIndex(args));
496
+ } catch (error) {
497
+ throw new MastraError(
498
+ {
499
+ id: 'LIBSQL_VECTOR_TRUNCATE_INDEX_FAILED',
500
+ domain: ErrorDomain.STORAGE,
501
+ category: ErrorCategory.THIRD_PARTY,
502
+ details: { indexName: args.indexName },
503
+ },
504
+ error,
505
+ );
506
+ }
395
507
  }
396
508
 
397
509
  private async _doTruncateIndex({ indexName }: DeleteIndexParams): Promise<void> {