@mastra/libsql 0.11.0 → 0.11.1-alpha.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/libsql",
3
- "version": "0.11.0",
3
+ "version": "0.11.1-alpha.0",
4
4
  "description": "Libsql provider for Mastra - includes both vector and db storage capabilities",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -25,13 +25,13 @@
25
25
  "devDependencies": {
26
26
  "@microsoft/api-extractor": "^7.52.8",
27
27
  "@types/node": "^20.19.0",
28
- "eslint": "^9.29.0",
28
+ "eslint": "^9.30.1",
29
29
  "tsup": "^8.5.0",
30
30
  "typescript": "^5.8.3",
31
- "vitest": "^3.2.3",
32
- "@internal/lint": "0.0.14",
33
- "@internal/storage-test-utils": "0.0.10",
34
- "@mastra/core": "0.10.7"
31
+ "vitest": "^3.2.4",
32
+ "@internal/lint": "0.0.20",
33
+ "@mastra/core": "0.11.0-alpha.2",
34
+ "@internal/storage-test-utils": "0.0.16"
35
35
  },
36
36
  "peerDependencies": {
37
37
  "@mastra/core": ">=0.10.7-0 <0.11.0-0"
@@ -0,0 +1,149 @@
1
+ import type { Client, InValue } from '@libsql/client';
2
+ import { ErrorCategory, ErrorDomain, MastraError } from '@mastra/core/error';
3
+ import type { MetricResult, TestInfo } from '@mastra/core/eval';
4
+ import { LegacyEvalsStorage, TABLE_EVALS } from '@mastra/core/storage';
5
+ import type { PaginationArgs, PaginationInfo, EvalRow } from '@mastra/core/storage';
6
+
7
+ function transformEvalRow(row: Record<string, any>): EvalRow {
8
+ const resultValue = JSON.parse(row.result as string);
9
+ const testInfoValue = row.test_info ? JSON.parse(row.test_info as string) : undefined;
10
+
11
+ if (!resultValue || typeof resultValue !== 'object' || !('score' in resultValue)) {
12
+ throw new Error(`Invalid MetricResult format: ${JSON.stringify(resultValue)}`);
13
+ }
14
+
15
+ return {
16
+ input: row.input as string,
17
+ output: row.output as string,
18
+ result: resultValue as MetricResult,
19
+ agentName: row.agent_name as string,
20
+ metricName: row.metric_name as string,
21
+ instructions: row.instructions as string,
22
+ testInfo: testInfoValue as TestInfo,
23
+ globalRunId: row.global_run_id as string,
24
+ runId: row.run_id as string,
25
+ createdAt: row.created_at as string,
26
+ };
27
+ }
28
+
29
+ export class LegacyEvalsLibSQL extends LegacyEvalsStorage {
30
+ private client: Client;
31
+ constructor({ client }: { client: Client }) {
32
+ super();
33
+ this.client = client;
34
+ }
35
+
36
+ /** @deprecated use getEvals instead */
37
+ async getEvalsByAgentName(agentName: string, type?: 'test' | 'live'): Promise<EvalRow[]> {
38
+ try {
39
+ const baseQuery = `SELECT * FROM ${TABLE_EVALS} WHERE agent_name = ?`;
40
+ const typeCondition =
41
+ type === 'test'
42
+ ? " AND test_info IS NOT NULL AND test_info->>'testPath' IS NOT NULL"
43
+ : type === 'live'
44
+ ? " AND (test_info IS NULL OR test_info->>'testPath' IS NULL)"
45
+ : '';
46
+
47
+ const result = await this.client.execute({
48
+ sql: `${baseQuery}${typeCondition} ORDER BY created_at DESC`,
49
+ args: [agentName],
50
+ });
51
+
52
+ return result.rows?.map(row => transformEvalRow(row)) ?? [];
53
+ } catch (error) {
54
+ // Handle case where table doesn't exist yet
55
+ if (error instanceof Error && error.message.includes('no such table')) {
56
+ return [];
57
+ }
58
+ throw new MastraError(
59
+ {
60
+ id: 'LIBSQL_STORE_GET_EVALS_BY_AGENT_NAME_FAILED',
61
+ domain: ErrorDomain.STORAGE,
62
+ category: ErrorCategory.THIRD_PARTY,
63
+ details: { agentName },
64
+ },
65
+ error,
66
+ );
67
+ }
68
+ }
69
+
70
+ async getEvals(
71
+ options: {
72
+ agentName?: string;
73
+ type?: 'test' | 'live';
74
+ } & PaginationArgs = {},
75
+ ): Promise<PaginationInfo & { evals: EvalRow[] }> {
76
+ const { agentName, type, page = 0, perPage = 100, dateRange } = options;
77
+ const fromDate = dateRange?.start;
78
+ const toDate = dateRange?.end;
79
+
80
+ const conditions: string[] = [];
81
+ const queryParams: InValue[] = [];
82
+
83
+ if (agentName) {
84
+ conditions.push(`agent_name = ?`);
85
+ queryParams.push(agentName);
86
+ }
87
+
88
+ if (type === 'test') {
89
+ conditions.push(`(test_info IS NOT NULL AND json_extract(test_info, '$.testPath') IS NOT NULL)`);
90
+ } else if (type === 'live') {
91
+ conditions.push(`(test_info IS NULL OR json_extract(test_info, '$.testPath') IS NULL)`);
92
+ }
93
+
94
+ if (fromDate) {
95
+ conditions.push(`created_at >= ?`);
96
+ queryParams.push(fromDate.toISOString());
97
+ }
98
+
99
+ if (toDate) {
100
+ conditions.push(`created_at <= ?`);
101
+ queryParams.push(toDate.toISOString());
102
+ }
103
+
104
+ const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';
105
+
106
+ try {
107
+ const countResult = await this.client.execute({
108
+ sql: `SELECT COUNT(*) as count FROM ${TABLE_EVALS} ${whereClause}`,
109
+ args: queryParams,
110
+ });
111
+ const total = Number(countResult.rows?.[0]?.count ?? 0);
112
+
113
+ const currentOffset = page * perPage;
114
+ const hasMore = currentOffset + perPage < total;
115
+
116
+ if (total === 0) {
117
+ return {
118
+ evals: [],
119
+ total: 0,
120
+ page,
121
+ perPage,
122
+ hasMore: false,
123
+ };
124
+ }
125
+
126
+ const dataResult = await this.client.execute({
127
+ sql: `SELECT * FROM ${TABLE_EVALS} ${whereClause} ORDER BY created_at DESC LIMIT ? OFFSET ?`,
128
+ args: [...queryParams, perPage, currentOffset],
129
+ });
130
+
131
+ return {
132
+ evals: dataResult.rows?.map(row => transformEvalRow(row)) ?? [],
133
+ total,
134
+ page,
135
+ perPage,
136
+ hasMore,
137
+ };
138
+ } catch (error) {
139
+ throw new MastraError(
140
+ {
141
+ id: 'LIBSQL_STORE_GET_EVALS_FAILED',
142
+ domain: ErrorDomain.STORAGE,
143
+ category: ErrorCategory.THIRD_PARTY,
144
+ },
145
+ error,
146
+ );
147
+ }
148
+ }
149
+ }