@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.
- package/.turbo/turbo-build.log +7 -7
- package/CHANGELOG.md +14 -0
- package/dist/index.cjs +469 -149
- package/dist/index.js +458 -138
- package/package.json +3 -3
- package/src/storage/index.test.ts +1 -30
- package/src/storage/index.ts +345 -135
- package/src/vector/index.ts +126 -14
package/src/storage/index.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { createClient } from '@libsql/client';
|
|
|
2
2
|
import type { Client, InValue } from '@libsql/client';
|
|
3
3
|
import { MessageList } from '@mastra/core/agent';
|
|
4
4
|
import type { MastraMessageContentV2, MastraMessageV2 } from '@mastra/core/agent';
|
|
5
|
+
import { ErrorCategory, ErrorDomain, MastraError } from '@mastra/core/error';
|
|
5
6
|
import type { MetricResult, TestInfo } from '@mastra/core/eval';
|
|
6
7
|
import type { MastraMessageV1, StorageThreadType } from '@mastra/core/memory';
|
|
7
8
|
import {
|
|
@@ -128,8 +129,19 @@ export class LibSQLStore extends MastraStorage {
|
|
|
128
129
|
const sql = this.getCreateTableSQL(tableName, schema);
|
|
129
130
|
await this.client.execute(sql);
|
|
130
131
|
} catch (error) {
|
|
131
|
-
this.logger.error(`Error creating table ${tableName}: ${error}`);
|
|
132
|
-
throw error;
|
|
132
|
+
// this.logger.error(`Error creating table ${tableName}: ${error}`);
|
|
133
|
+
// throw error;
|
|
134
|
+
throw new MastraError(
|
|
135
|
+
{
|
|
136
|
+
id: 'LIBSQL_STORE_CREATE_TABLE_FAILED',
|
|
137
|
+
domain: ErrorDomain.STORAGE,
|
|
138
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
139
|
+
details: {
|
|
140
|
+
tableName,
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
error,
|
|
144
|
+
);
|
|
133
145
|
}
|
|
134
146
|
}
|
|
135
147
|
|
|
@@ -183,10 +195,17 @@ export class LibSQLStore extends MastraStorage {
|
|
|
183
195
|
}
|
|
184
196
|
}
|
|
185
197
|
} catch (error) {
|
|
186
|
-
|
|
187
|
-
|
|
198
|
+
throw new MastraError(
|
|
199
|
+
{
|
|
200
|
+
id: 'LIBSQL_STORE_ALTER_TABLE_FAILED',
|
|
201
|
+
domain: ErrorDomain.STORAGE,
|
|
202
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
203
|
+
details: {
|
|
204
|
+
tableName,
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
error,
|
|
188
208
|
);
|
|
189
|
-
throw new Error(`Failed to alter table ${tableName}: ${error}`);
|
|
190
209
|
}
|
|
191
210
|
}
|
|
192
211
|
|
|
@@ -195,9 +214,19 @@ export class LibSQLStore extends MastraStorage {
|
|
|
195
214
|
try {
|
|
196
215
|
await this.client.execute(`DELETE FROM ${parsedTableName}`);
|
|
197
216
|
} catch (e) {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
217
|
+
const mastraError = new MastraError(
|
|
218
|
+
{
|
|
219
|
+
id: 'LIBSQL_STORE_CLEAR_TABLE_FAILED',
|
|
220
|
+
domain: ErrorDomain.STORAGE,
|
|
221
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
222
|
+
details: {
|
|
223
|
+
tableName,
|
|
224
|
+
},
|
|
225
|
+
},
|
|
226
|
+
e,
|
|
227
|
+
);
|
|
228
|
+
this.logger?.trackException?.(mastraError);
|
|
229
|
+
this.logger?.error?.(mastraError.toString());
|
|
201
230
|
}
|
|
202
231
|
}
|
|
203
232
|
|
|
@@ -277,7 +306,19 @@ export class LibSQLStore extends MastraStorage {
|
|
|
277
306
|
return this.executeWriteOperationWithRetry(
|
|
278
307
|
() => this.doBatchInsert(args),
|
|
279
308
|
`batch insert into table ${args.tableName}`,
|
|
280
|
-
)
|
|
309
|
+
).catch(error => {
|
|
310
|
+
throw new MastraError(
|
|
311
|
+
{
|
|
312
|
+
id: 'LIBSQL_STORE_BATCH_INSERT_FAILED',
|
|
313
|
+
domain: ErrorDomain.STORAGE,
|
|
314
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
315
|
+
details: {
|
|
316
|
+
tableName: args.tableName,
|
|
317
|
+
},
|
|
318
|
+
},
|
|
319
|
+
error,
|
|
320
|
+
);
|
|
321
|
+
});
|
|
281
322
|
}
|
|
282
323
|
|
|
283
324
|
private async doBatchInsert({
|
|
@@ -327,19 +368,31 @@ export class LibSQLStore extends MastraStorage {
|
|
|
327
368
|
}
|
|
328
369
|
|
|
329
370
|
async getThreadById({ threadId }: { threadId: string }): Promise<StorageThreadType | null> {
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
371
|
+
try {
|
|
372
|
+
const result = await this.load<StorageThreadType>({
|
|
373
|
+
tableName: TABLE_THREADS,
|
|
374
|
+
keys: { id: threadId },
|
|
375
|
+
});
|
|
334
376
|
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
377
|
+
if (!result) {
|
|
378
|
+
return null;
|
|
379
|
+
}
|
|
338
380
|
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
381
|
+
return {
|
|
382
|
+
...result,
|
|
383
|
+
metadata: typeof result.metadata === 'string' ? JSON.parse(result.metadata) : result.metadata,
|
|
384
|
+
};
|
|
385
|
+
} catch (error) {
|
|
386
|
+
throw new MastraError(
|
|
387
|
+
{
|
|
388
|
+
id: 'LIBSQL_STORE_GET_THREAD_BY_ID_FAILED',
|
|
389
|
+
domain: ErrorDomain.STORAGE,
|
|
390
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
391
|
+
details: { threadId },
|
|
392
|
+
},
|
|
393
|
+
error,
|
|
394
|
+
);
|
|
395
|
+
}
|
|
343
396
|
}
|
|
344
397
|
|
|
345
398
|
/**
|
|
@@ -372,7 +425,17 @@ export class LibSQLStore extends MastraStorage {
|
|
|
372
425
|
}
|
|
373
426
|
return result.rows.map(mapRowToStorageThreadType);
|
|
374
427
|
} catch (error) {
|
|
375
|
-
|
|
428
|
+
const mastraError = new MastraError(
|
|
429
|
+
{
|
|
430
|
+
id: 'LIBSQL_STORE_GET_THREADS_BY_RESOURCE_ID_FAILED',
|
|
431
|
+
domain: ErrorDomain.STORAGE,
|
|
432
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
433
|
+
details: { resourceId },
|
|
434
|
+
},
|
|
435
|
+
error,
|
|
436
|
+
);
|
|
437
|
+
this.logger?.trackException?.(mastraError);
|
|
438
|
+
this.logger?.error?.(mastraError.toString());
|
|
376
439
|
return [];
|
|
377
440
|
}
|
|
378
441
|
}
|
|
@@ -430,21 +493,46 @@ export class LibSQLStore extends MastraStorage {
|
|
|
430
493
|
hasMore: currentOffset + threads.length < total,
|
|
431
494
|
};
|
|
432
495
|
} catch (error) {
|
|
433
|
-
|
|
496
|
+
const mastraError = new MastraError(
|
|
497
|
+
{
|
|
498
|
+
id: 'LIBSQL_STORE_GET_THREADS_BY_RESOURCE_ID_FAILED',
|
|
499
|
+
domain: ErrorDomain.STORAGE,
|
|
500
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
501
|
+
details: { resourceId },
|
|
502
|
+
},
|
|
503
|
+
error,
|
|
504
|
+
);
|
|
505
|
+
this.logger?.trackException?.(mastraError);
|
|
506
|
+
this.logger?.error?.(mastraError.toString());
|
|
434
507
|
return { threads: [], total: 0, page, perPage, hasMore: false };
|
|
435
508
|
}
|
|
436
509
|
}
|
|
437
510
|
|
|
438
511
|
async saveThread({ thread }: { thread: StorageThreadType }): Promise<StorageThreadType> {
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
512
|
+
try {
|
|
513
|
+
await this.insert({
|
|
514
|
+
tableName: TABLE_THREADS,
|
|
515
|
+
record: {
|
|
516
|
+
...thread,
|
|
517
|
+
metadata: JSON.stringify(thread.metadata),
|
|
518
|
+
},
|
|
519
|
+
});
|
|
446
520
|
|
|
447
|
-
|
|
521
|
+
return thread;
|
|
522
|
+
} catch (error) {
|
|
523
|
+
const mastraError = new MastraError(
|
|
524
|
+
{
|
|
525
|
+
id: 'LIBSQL_STORE_SAVE_THREAD_FAILED',
|
|
526
|
+
domain: ErrorDomain.STORAGE,
|
|
527
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
528
|
+
details: { threadId: thread.id },
|
|
529
|
+
},
|
|
530
|
+
error,
|
|
531
|
+
);
|
|
532
|
+
this.logger?.trackException?.(mastraError);
|
|
533
|
+
this.logger?.error?.(mastraError.toString());
|
|
534
|
+
throw mastraError;
|
|
535
|
+
}
|
|
448
536
|
}
|
|
449
537
|
|
|
450
538
|
async updateThread({
|
|
@@ -458,7 +546,13 @@ export class LibSQLStore extends MastraStorage {
|
|
|
458
546
|
}): Promise<StorageThreadType> {
|
|
459
547
|
const thread = await this.getThreadById({ threadId: id });
|
|
460
548
|
if (!thread) {
|
|
461
|
-
throw new
|
|
549
|
+
throw new MastraError({
|
|
550
|
+
id: 'LIBSQL_STORE_UPDATE_THREAD_FAILED_THREAD_NOT_FOUND',
|
|
551
|
+
domain: ErrorDomain.STORAGE,
|
|
552
|
+
category: ErrorCategory.USER,
|
|
553
|
+
text: `Thread ${id} not found`,
|
|
554
|
+
details: { threadId: id },
|
|
555
|
+
});
|
|
462
556
|
}
|
|
463
557
|
|
|
464
558
|
const updatedThread = {
|
|
@@ -470,24 +564,49 @@ export class LibSQLStore extends MastraStorage {
|
|
|
470
564
|
},
|
|
471
565
|
};
|
|
472
566
|
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
567
|
+
try {
|
|
568
|
+
await this.client.execute({
|
|
569
|
+
sql: `UPDATE ${TABLE_THREADS} SET title = ?, metadata = ? WHERE id = ?`,
|
|
570
|
+
args: [title, JSON.stringify(updatedThread.metadata), id],
|
|
571
|
+
});
|
|
477
572
|
|
|
478
|
-
|
|
573
|
+
return updatedThread;
|
|
574
|
+
} catch (error) {
|
|
575
|
+
throw new MastraError(
|
|
576
|
+
{
|
|
577
|
+
id: 'LIBSQL_STORE_UPDATE_THREAD_FAILED',
|
|
578
|
+
domain: ErrorDomain.STORAGE,
|
|
579
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
580
|
+
text: `Failed to update thread ${id}`,
|
|
581
|
+
details: { threadId: id },
|
|
582
|
+
},
|
|
583
|
+
error,
|
|
584
|
+
);
|
|
585
|
+
}
|
|
479
586
|
}
|
|
480
587
|
|
|
481
588
|
async deleteThread({ threadId }: { threadId: string }): Promise<void> {
|
|
482
589
|
// Delete messages for this thread (manual step)
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
590
|
+
try {
|
|
591
|
+
await this.client.execute({
|
|
592
|
+
sql: `DELETE FROM ${TABLE_MESSAGES} WHERE thread_id = ?`,
|
|
593
|
+
args: [threadId],
|
|
594
|
+
});
|
|
595
|
+
await this.client.execute({
|
|
596
|
+
sql: `DELETE FROM ${TABLE_THREADS} WHERE id = ?`,
|
|
597
|
+
args: [threadId],
|
|
598
|
+
});
|
|
599
|
+
} catch (error) {
|
|
600
|
+
throw new MastraError(
|
|
601
|
+
{
|
|
602
|
+
id: 'LIBSQL_STORE_DELETE_THREAD_FAILED',
|
|
603
|
+
domain: ErrorDomain.STORAGE,
|
|
604
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
605
|
+
details: { threadId },
|
|
606
|
+
},
|
|
607
|
+
error,
|
|
608
|
+
);
|
|
609
|
+
}
|
|
491
610
|
// TODO: Need to check if CASCADE is enabled so that messages will be automatically deleted due to CASCADE constraint
|
|
492
611
|
}
|
|
493
612
|
|
|
@@ -577,8 +696,7 @@ export class LibSQLStore extends MastraStorage {
|
|
|
577
696
|
}): Promise<MastraMessageV1[] | MastraMessageV2[]> {
|
|
578
697
|
try {
|
|
579
698
|
const messages: MastraMessageV2[] = [];
|
|
580
|
-
const limit =
|
|
581
|
-
|
|
699
|
+
const limit = this.resolveMessageLimit({ last: selectBy?.last, defaultLimit: 40 });
|
|
582
700
|
if (selectBy?.include?.length) {
|
|
583
701
|
const includeMessages = await this._getIncludedMessages({ threadId, selectBy });
|
|
584
702
|
if (includeMessages) {
|
|
@@ -612,8 +730,15 @@ export class LibSQLStore extends MastraStorage {
|
|
|
612
730
|
if (format === `v2`) return list.get.all.v2();
|
|
613
731
|
return list.get.all.v1();
|
|
614
732
|
} catch (error) {
|
|
615
|
-
|
|
616
|
-
|
|
733
|
+
throw new MastraError(
|
|
734
|
+
{
|
|
735
|
+
id: 'LIBSQL_STORE_GET_MESSAGES_FAILED',
|
|
736
|
+
domain: ErrorDomain.STORAGE,
|
|
737
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
738
|
+
details: { threadId },
|
|
739
|
+
},
|
|
740
|
+
error,
|
|
741
|
+
);
|
|
617
742
|
}
|
|
618
743
|
}
|
|
619
744
|
|
|
@@ -630,9 +755,21 @@ export class LibSQLStore extends MastraStorage {
|
|
|
630
755
|
const messages: MastraMessageV2[] = [];
|
|
631
756
|
|
|
632
757
|
if (selectBy?.include?.length) {
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
758
|
+
try {
|
|
759
|
+
const includeMessages = await this._getIncludedMessages({ threadId, selectBy });
|
|
760
|
+
if (includeMessages) {
|
|
761
|
+
messages.push(...includeMessages);
|
|
762
|
+
}
|
|
763
|
+
} catch (error) {
|
|
764
|
+
throw new MastraError(
|
|
765
|
+
{
|
|
766
|
+
id: 'LIBSQL_STORE_GET_MESSAGES_PAGINATED_GET_INCLUDE_MESSAGES_FAILED',
|
|
767
|
+
domain: ErrorDomain.STORAGE,
|
|
768
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
769
|
+
details: { threadId },
|
|
770
|
+
},
|
|
771
|
+
error,
|
|
772
|
+
);
|
|
636
773
|
}
|
|
637
774
|
}
|
|
638
775
|
|
|
@@ -688,7 +825,17 @@ export class LibSQLStore extends MastraStorage {
|
|
|
688
825
|
hasMore: currentOffset + messages.length < total,
|
|
689
826
|
};
|
|
690
827
|
} catch (error) {
|
|
691
|
-
|
|
828
|
+
const mastraError = new MastraError(
|
|
829
|
+
{
|
|
830
|
+
id: 'LIBSQL_STORE_GET_MESSAGES_PAGINATED_FAILED',
|
|
831
|
+
domain: ErrorDomain.STORAGE,
|
|
832
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
833
|
+
details: { threadId },
|
|
834
|
+
},
|
|
835
|
+
error,
|
|
836
|
+
);
|
|
837
|
+
this.logger?.trackException?.(mastraError);
|
|
838
|
+
this.logger?.error?.(mastraError.toString());
|
|
692
839
|
return { messages: [], total: 0, page, perPage, hasMore: false };
|
|
693
840
|
}
|
|
694
841
|
}
|
|
@@ -750,8 +897,14 @@ export class LibSQLStore extends MastraStorage {
|
|
|
750
897
|
if (format === `v2`) return list.get.all.v2();
|
|
751
898
|
return list.get.all.v1();
|
|
752
899
|
} catch (error) {
|
|
753
|
-
|
|
754
|
-
|
|
900
|
+
throw new MastraError(
|
|
901
|
+
{
|
|
902
|
+
id: 'LIBSQL_STORE_SAVE_MESSAGES_FAILED',
|
|
903
|
+
domain: ErrorDomain.STORAGE,
|
|
904
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
905
|
+
},
|
|
906
|
+
error,
|
|
907
|
+
);
|
|
755
908
|
}
|
|
756
909
|
}
|
|
757
910
|
|
|
@@ -905,8 +1058,15 @@ export class LibSQLStore extends MastraStorage {
|
|
|
905
1058
|
if (error instanceof Error && error.message.includes('no such table')) {
|
|
906
1059
|
return [];
|
|
907
1060
|
}
|
|
908
|
-
|
|
909
|
-
|
|
1061
|
+
throw new MastraError(
|
|
1062
|
+
{
|
|
1063
|
+
id: 'LIBSQL_STORE_GET_EVALS_BY_AGENT_NAME_FAILED',
|
|
1064
|
+
domain: ErrorDomain.STORAGE,
|
|
1065
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1066
|
+
details: { agentName },
|
|
1067
|
+
},
|
|
1068
|
+
error,
|
|
1069
|
+
);
|
|
910
1070
|
}
|
|
911
1071
|
}
|
|
912
1072
|
|
|
@@ -946,37 +1106,48 @@ export class LibSQLStore extends MastraStorage {
|
|
|
946
1106
|
|
|
947
1107
|
const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';
|
|
948
1108
|
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
1109
|
+
try {
|
|
1110
|
+
const countResult = await this.client.execute({
|
|
1111
|
+
sql: `SELECT COUNT(*) as count FROM ${TABLE_EVALS} ${whereClause}`,
|
|
1112
|
+
args: queryParams,
|
|
1113
|
+
});
|
|
1114
|
+
const total = Number(countResult.rows?.[0]?.count ?? 0);
|
|
954
1115
|
|
|
955
|
-
|
|
956
|
-
|
|
1116
|
+
const currentOffset = page * perPage;
|
|
1117
|
+
const hasMore = currentOffset + perPage < total;
|
|
1118
|
+
|
|
1119
|
+
if (total === 0) {
|
|
1120
|
+
return {
|
|
1121
|
+
evals: [],
|
|
1122
|
+
total: 0,
|
|
1123
|
+
page,
|
|
1124
|
+
perPage,
|
|
1125
|
+
hasMore: false,
|
|
1126
|
+
};
|
|
1127
|
+
}
|
|
1128
|
+
|
|
1129
|
+
const dataResult = await this.client.execute({
|
|
1130
|
+
sql: `SELECT * FROM ${TABLE_EVALS} ${whereClause} ORDER BY created_at DESC LIMIT ? OFFSET ?`,
|
|
1131
|
+
args: [...queryParams, perPage, currentOffset],
|
|
1132
|
+
});
|
|
957
1133
|
|
|
958
|
-
if (total === 0) {
|
|
959
1134
|
return {
|
|
960
|
-
evals: [],
|
|
961
|
-
total
|
|
1135
|
+
evals: dataResult.rows?.map(row => this.transformEvalRow(row)) ?? [],
|
|
1136
|
+
total,
|
|
962
1137
|
page,
|
|
963
1138
|
perPage,
|
|
964
|
-
hasMore
|
|
1139
|
+
hasMore,
|
|
965
1140
|
};
|
|
1141
|
+
} catch (error) {
|
|
1142
|
+
throw new MastraError(
|
|
1143
|
+
{
|
|
1144
|
+
id: 'LIBSQL_STORE_GET_EVALS_FAILED',
|
|
1145
|
+
domain: ErrorDomain.STORAGE,
|
|
1146
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1147
|
+
},
|
|
1148
|
+
error,
|
|
1149
|
+
);
|
|
966
1150
|
}
|
|
967
|
-
|
|
968
|
-
const dataResult = await this.client.execute({
|
|
969
|
-
sql: `SELECT * FROM ${TABLE_EVALS} ${whereClause} ORDER BY created_at DESC LIMIT ? OFFSET ?`,
|
|
970
|
-
args: [...queryParams, perPage, currentOffset],
|
|
971
|
-
});
|
|
972
|
-
|
|
973
|
-
return {
|
|
974
|
-
evals: dataResult.rows?.map(row => this.transformEvalRow(row)) ?? [],
|
|
975
|
-
total,
|
|
976
|
-
page,
|
|
977
|
-
perPage,
|
|
978
|
-
hasMore,
|
|
979
|
-
};
|
|
980
1151
|
}
|
|
981
1152
|
|
|
982
1153
|
/**
|
|
@@ -998,8 +1169,19 @@ export class LibSQLStore extends MastraStorage {
|
|
|
998
1169
|
end: args.toDate,
|
|
999
1170
|
};
|
|
1000
1171
|
}
|
|
1001
|
-
|
|
1002
|
-
|
|
1172
|
+
try {
|
|
1173
|
+
const result = await this.getTracesPaginated(args);
|
|
1174
|
+
return result.traces;
|
|
1175
|
+
} catch (error) {
|
|
1176
|
+
throw new MastraError(
|
|
1177
|
+
{
|
|
1178
|
+
id: 'LIBSQL_STORE_GET_TRACES_FAILED',
|
|
1179
|
+
domain: ErrorDomain.STORAGE,
|
|
1180
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1181
|
+
},
|
|
1182
|
+
error,
|
|
1183
|
+
);
|
|
1184
|
+
}
|
|
1003
1185
|
}
|
|
1004
1186
|
|
|
1005
1187
|
public async getTracesPaginated(
|
|
@@ -1049,55 +1231,66 @@ export class LibSQLStore extends MastraStorage {
|
|
|
1049
1231
|
|
|
1050
1232
|
const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';
|
|
1051
1233
|
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1234
|
+
try {
|
|
1235
|
+
const countResult = await this.client.execute({
|
|
1236
|
+
sql: `SELECT COUNT(*) as count FROM ${TABLE_TRACES} ${whereClause}`,
|
|
1237
|
+
args: queryArgs,
|
|
1238
|
+
});
|
|
1239
|
+
const total = Number(countResult.rows?.[0]?.count ?? 0);
|
|
1240
|
+
|
|
1241
|
+
if (total === 0) {
|
|
1242
|
+
return {
|
|
1243
|
+
traces: [],
|
|
1244
|
+
total: 0,
|
|
1245
|
+
page,
|
|
1246
|
+
perPage,
|
|
1247
|
+
hasMore: false,
|
|
1248
|
+
};
|
|
1249
|
+
}
|
|
1250
|
+
|
|
1251
|
+
const dataResult = await this.client.execute({
|
|
1252
|
+
sql: `SELECT * FROM ${TABLE_TRACES} ${whereClause} ORDER BY "startTime" DESC LIMIT ? OFFSET ?`,
|
|
1253
|
+
args: [...queryArgs, perPage, currentOffset],
|
|
1254
|
+
});
|
|
1255
|
+
|
|
1256
|
+
const traces =
|
|
1257
|
+
dataResult.rows?.map(
|
|
1258
|
+
row =>
|
|
1259
|
+
({
|
|
1260
|
+
id: row.id,
|
|
1261
|
+
parentSpanId: row.parentSpanId,
|
|
1262
|
+
traceId: row.traceId,
|
|
1263
|
+
name: row.name,
|
|
1264
|
+
scope: row.scope,
|
|
1265
|
+
kind: row.kind,
|
|
1266
|
+
status: safelyParseJSON(row.status as string),
|
|
1267
|
+
events: safelyParseJSON(row.events as string),
|
|
1268
|
+
links: safelyParseJSON(row.links as string),
|
|
1269
|
+
attributes: safelyParseJSON(row.attributes as string),
|
|
1270
|
+
startTime: row.startTime,
|
|
1271
|
+
endTime: row.endTime,
|
|
1272
|
+
other: safelyParseJSON(row.other as string),
|
|
1273
|
+
createdAt: row.createdAt,
|
|
1274
|
+
}) as Trace,
|
|
1275
|
+
) ?? [];
|
|
1057
1276
|
|
|
1058
|
-
if (total === 0) {
|
|
1059
1277
|
return {
|
|
1060
|
-
traces
|
|
1061
|
-
total
|
|
1278
|
+
traces,
|
|
1279
|
+
total,
|
|
1062
1280
|
page,
|
|
1063
1281
|
perPage,
|
|
1064
|
-
hasMore:
|
|
1282
|
+
hasMore: currentOffset + traces.length < total,
|
|
1065
1283
|
};
|
|
1284
|
+
} catch (error) {
|
|
1285
|
+
throw new MastraError(
|
|
1286
|
+
{
|
|
1287
|
+
id: 'LIBSQL_STORE_GET_TRACES_PAGINATED_FAILED',
|
|
1288
|
+
domain: ErrorDomain.STORAGE,
|
|
1289
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1290
|
+
},
|
|
1291
|
+
error,
|
|
1292
|
+
);
|
|
1066
1293
|
}
|
|
1067
|
-
|
|
1068
|
-
const dataResult = await this.client.execute({
|
|
1069
|
-
sql: `SELECT * FROM ${TABLE_TRACES} ${whereClause} ORDER BY "startTime" DESC LIMIT ? OFFSET ?`,
|
|
1070
|
-
args: [...queryArgs, perPage, currentOffset],
|
|
1071
|
-
});
|
|
1072
|
-
|
|
1073
|
-
const traces =
|
|
1074
|
-
dataResult.rows?.map(
|
|
1075
|
-
row =>
|
|
1076
|
-
({
|
|
1077
|
-
id: row.id,
|
|
1078
|
-
parentSpanId: row.parentSpanId,
|
|
1079
|
-
traceId: row.traceId,
|
|
1080
|
-
name: row.name,
|
|
1081
|
-
scope: row.scope,
|
|
1082
|
-
kind: row.kind,
|
|
1083
|
-
status: safelyParseJSON(row.status as string),
|
|
1084
|
-
events: safelyParseJSON(row.events as string),
|
|
1085
|
-
links: safelyParseJSON(row.links as string),
|
|
1086
|
-
attributes: safelyParseJSON(row.attributes as string),
|
|
1087
|
-
startTime: row.startTime,
|
|
1088
|
-
endTime: row.endTime,
|
|
1089
|
-
other: safelyParseJSON(row.other as string),
|
|
1090
|
-
createdAt: row.createdAt,
|
|
1091
|
-
}) as Trace,
|
|
1092
|
-
) ?? [];
|
|
1093
|
-
|
|
1094
|
-
return {
|
|
1095
|
-
traces,
|
|
1096
|
-
total,
|
|
1097
|
-
page,
|
|
1098
|
-
perPage,
|
|
1099
|
-
hasMore: currentOffset + traces.length < total,
|
|
1100
|
-
};
|
|
1101
1294
|
}
|
|
1102
1295
|
|
|
1103
1296
|
async getWorkflowRuns({
|
|
@@ -1167,8 +1360,14 @@ export class LibSQLStore extends MastraStorage {
|
|
|
1167
1360
|
// Use runs.length as total when not paginating
|
|
1168
1361
|
return { runs, total: total || runs.length };
|
|
1169
1362
|
} catch (error) {
|
|
1170
|
-
|
|
1171
|
-
|
|
1363
|
+
throw new MastraError(
|
|
1364
|
+
{
|
|
1365
|
+
id: 'LIBSQL_STORE_GET_WORKFLOW_RUNS_FAILED',
|
|
1366
|
+
domain: ErrorDomain.STORAGE,
|
|
1367
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1368
|
+
},
|
|
1369
|
+
error,
|
|
1370
|
+
);
|
|
1172
1371
|
}
|
|
1173
1372
|
}
|
|
1174
1373
|
|
|
@@ -1194,16 +1393,27 @@ export class LibSQLStore extends MastraStorage {
|
|
|
1194
1393
|
|
|
1195
1394
|
const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';
|
|
1196
1395
|
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1396
|
+
try {
|
|
1397
|
+
const result = await this.client.execute({
|
|
1398
|
+
sql: `SELECT * FROM ${TABLE_WORKFLOW_SNAPSHOT} ${whereClause}`,
|
|
1399
|
+
args,
|
|
1400
|
+
});
|
|
1201
1401
|
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1402
|
+
if (!result.rows?.[0]) {
|
|
1403
|
+
return null;
|
|
1404
|
+
}
|
|
1205
1405
|
|
|
1206
|
-
|
|
1406
|
+
return this.parseWorkflowRun(result.rows[0]);
|
|
1407
|
+
} catch (error) {
|
|
1408
|
+
throw new MastraError(
|
|
1409
|
+
{
|
|
1410
|
+
id: 'LIBSQL_STORE_GET_WORKFLOW_RUN_BY_ID_FAILED',
|
|
1411
|
+
domain: ErrorDomain.STORAGE,
|
|
1412
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1413
|
+
},
|
|
1414
|
+
error,
|
|
1415
|
+
);
|
|
1416
|
+
}
|
|
1207
1417
|
}
|
|
1208
1418
|
|
|
1209
1419
|
private async hasColumn(table: string, column: string): Promise<boolean> {
|