@mastra/libsql 0.13.0-alpha.1 → 0.13.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/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +34 -0
- package/dist/index.cjs +46 -33
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +46 -33
- package/dist/index.js.map +1 -1
- package/dist/vector/index.d.ts.map +1 -1
- package/dist/vector/sql-builder.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/vector/index.test.ts +22 -1
- package/src/vector/index.ts +1 -1
- package/src/vector/sql-builder.ts +51 -33
package/.turbo/turbo-build.log
CHANGED
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,39 @@
|
|
|
1
1
|
# @mastra/libsql
|
|
2
2
|
|
|
3
|
+
## 0.13.1-alpha.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 8888b57: Fix LibSQL vector metadata filtering for Memory system - corrected JSON path syntax for simple fields and changed default minScore to -1 to include all similarity results
|
|
8
|
+
- Updated dependencies [cd0042e]
|
|
9
|
+
- @mastra/core@0.13.1-alpha.0
|
|
10
|
+
|
|
11
|
+
## 0.13.0
|
|
12
|
+
|
|
13
|
+
### Minor Changes
|
|
14
|
+
|
|
15
|
+
- ea0c5f2: Add store support to new score api
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- 2871020: update safelyParseJSON to check for value of param when handling parse
|
|
20
|
+
- 4a406ec: fixes TypeScript declaration file imports to ensure proper ESM compatibility
|
|
21
|
+
- Updated dependencies [cb36de0]
|
|
22
|
+
- Updated dependencies [d0496e6]
|
|
23
|
+
- Updated dependencies [a82b851]
|
|
24
|
+
- Updated dependencies [ea0c5f2]
|
|
25
|
+
- Updated dependencies [41a0a0e]
|
|
26
|
+
- Updated dependencies [2871020]
|
|
27
|
+
- Updated dependencies [94f4812]
|
|
28
|
+
- Updated dependencies [e202b82]
|
|
29
|
+
- Updated dependencies [e00f6a0]
|
|
30
|
+
- Updated dependencies [4a406ec]
|
|
31
|
+
- Updated dependencies [b0e43c1]
|
|
32
|
+
- Updated dependencies [5d377e5]
|
|
33
|
+
- Updated dependencies [1fb812e]
|
|
34
|
+
- Updated dependencies [35c5798]
|
|
35
|
+
- @mastra/core@0.13.0
|
|
36
|
+
|
|
3
37
|
## 0.13.0-alpha.1
|
|
4
38
|
|
|
5
39
|
### Patch Changes
|
package/dist/index.cjs
CHANGED
|
@@ -78,11 +78,11 @@ var LibSQLFilterTranslator = class extends filter.BaseFilterTranslator {
|
|
|
78
78
|
};
|
|
79
79
|
var createBasicOperator = (symbol) => {
|
|
80
80
|
return (key, value) => {
|
|
81
|
-
const
|
|
81
|
+
const jsonPath = getJsonPath(key);
|
|
82
82
|
return {
|
|
83
83
|
sql: `CASE
|
|
84
|
-
WHEN ? IS NULL THEN json_extract(metadata,
|
|
85
|
-
ELSE json_extract(metadata,
|
|
84
|
+
WHEN ? IS NULL THEN json_extract(metadata, ${jsonPath}) IS ${symbol === "=" ? "" : "NOT"} NULL
|
|
85
|
+
ELSE json_extract(metadata, ${jsonPath}) ${symbol} ?
|
|
86
86
|
END`,
|
|
87
87
|
needsValue: true,
|
|
88
88
|
transformValue: () => {
|
|
@@ -93,16 +93,19 @@ var createBasicOperator = (symbol) => {
|
|
|
93
93
|
};
|
|
94
94
|
var createNumericOperator = (symbol) => {
|
|
95
95
|
return (key) => {
|
|
96
|
-
const
|
|
96
|
+
const jsonPath = getJsonPath(key);
|
|
97
97
|
return {
|
|
98
|
-
sql: `CAST(json_extract(metadata,
|
|
98
|
+
sql: `CAST(json_extract(metadata, ${jsonPath}) AS NUMERIC) ${symbol} ?`,
|
|
99
99
|
needsValue: true
|
|
100
100
|
};
|
|
101
101
|
};
|
|
102
102
|
};
|
|
103
|
-
var validateJsonArray = (key) =>
|
|
104
|
-
|
|
105
|
-
|
|
103
|
+
var validateJsonArray = (key) => {
|
|
104
|
+
const jsonPath = getJsonPath(key);
|
|
105
|
+
return `json_valid(json_extract(metadata, ${jsonPath}))
|
|
106
|
+
AND json_type(json_extract(metadata, ${jsonPath})) = 'array'`;
|
|
107
|
+
};
|
|
108
|
+
var pattern = /json_extract\(metadata, '\$\.(?:"[^"]*"(?:\."[^"]*")*|[^']+)'\)/g;
|
|
106
109
|
function buildElemMatchConditions(value) {
|
|
107
110
|
const conditions = Object.entries(value).map(([field, fieldValue]) => {
|
|
108
111
|
if (field.startsWith("$")) {
|
|
@@ -111,12 +114,13 @@ function buildElemMatchConditions(value) {
|
|
|
111
114
|
return { sql: elemSql, values };
|
|
112
115
|
} else if (typeof fieldValue === "object" && !Array.isArray(fieldValue)) {
|
|
113
116
|
const { sql, values } = buildCondition(field, fieldValue);
|
|
114
|
-
const
|
|
117
|
+
const jsonPath = parseJsonPathKey(field);
|
|
118
|
+
const elemSql = sql.replace(pattern, `json_extract(elem.value, '$.${jsonPath}')`);
|
|
115
119
|
return { sql: elemSql, values };
|
|
116
120
|
} else {
|
|
117
|
-
const
|
|
121
|
+
const jsonPath = parseJsonPathKey(field);
|
|
118
122
|
return {
|
|
119
|
-
sql: `json_extract(elem.value, '
|
|
123
|
+
sql: `json_extract(elem.value, '$.${jsonPath}') = ?`,
|
|
120
124
|
values: [fieldValue]
|
|
121
125
|
};
|
|
122
126
|
}
|
|
@@ -132,7 +136,7 @@ var FILTER_OPERATORS = {
|
|
|
132
136
|
$lte: createNumericOperator("<="),
|
|
133
137
|
// Array Operators
|
|
134
138
|
$in: (key, value) => {
|
|
135
|
-
const
|
|
139
|
+
const jsonPath = getJsonPath(key);
|
|
136
140
|
const arr = Array.isArray(value) ? value : [value];
|
|
137
141
|
if (arr.length === 0) {
|
|
138
142
|
return { sql: "1 = 0", needsValue: true, transformValue: () => [] };
|
|
@@ -141,12 +145,12 @@ var FILTER_OPERATORS = {
|
|
|
141
145
|
return {
|
|
142
146
|
sql: `(
|
|
143
147
|
CASE
|
|
144
|
-
WHEN ${validateJsonArray(
|
|
148
|
+
WHEN ${validateJsonArray(key)} THEN
|
|
145
149
|
EXISTS (
|
|
146
|
-
SELECT 1 FROM json_each(json_extract(metadata,
|
|
150
|
+
SELECT 1 FROM json_each(json_extract(metadata, ${jsonPath})) as elem
|
|
147
151
|
WHERE elem.value IN (SELECT value FROM json_each(?))
|
|
148
152
|
)
|
|
149
|
-
ELSE json_extract(metadata,
|
|
153
|
+
ELSE json_extract(metadata, ${jsonPath}) IN (${paramPlaceholders})
|
|
150
154
|
END
|
|
151
155
|
)`,
|
|
152
156
|
needsValue: true,
|
|
@@ -154,7 +158,7 @@ var FILTER_OPERATORS = {
|
|
|
154
158
|
};
|
|
155
159
|
},
|
|
156
160
|
$nin: (key, value) => {
|
|
157
|
-
const
|
|
161
|
+
const jsonPath = getJsonPath(key);
|
|
158
162
|
const arr = Array.isArray(value) ? value : [value];
|
|
159
163
|
if (arr.length === 0) {
|
|
160
164
|
return { sql: "1 = 1", needsValue: true, transformValue: () => [] };
|
|
@@ -163,12 +167,12 @@ var FILTER_OPERATORS = {
|
|
|
163
167
|
return {
|
|
164
168
|
sql: `(
|
|
165
169
|
CASE
|
|
166
|
-
WHEN ${validateJsonArray(
|
|
170
|
+
WHEN ${validateJsonArray(key)} THEN
|
|
167
171
|
NOT EXISTS (
|
|
168
|
-
SELECT 1 FROM json_each(json_extract(metadata,
|
|
172
|
+
SELECT 1 FROM json_each(json_extract(metadata, ${jsonPath})) as elem
|
|
169
173
|
WHERE elem.value IN (SELECT value FROM json_each(?))
|
|
170
174
|
)
|
|
171
|
-
ELSE json_extract(metadata,
|
|
175
|
+
ELSE json_extract(metadata, ${jsonPath}) NOT IN (${paramPlaceholders})
|
|
172
176
|
END
|
|
173
177
|
)`,
|
|
174
178
|
needsValue: true,
|
|
@@ -176,7 +180,7 @@ var FILTER_OPERATORS = {
|
|
|
176
180
|
};
|
|
177
181
|
},
|
|
178
182
|
$all: (key, value) => {
|
|
179
|
-
const
|
|
183
|
+
const jsonPath = getJsonPath(key);
|
|
180
184
|
let sql;
|
|
181
185
|
const arrayValue = Array.isArray(value) ? value : [value];
|
|
182
186
|
if (arrayValue.length === 0) {
|
|
@@ -184,13 +188,13 @@ var FILTER_OPERATORS = {
|
|
|
184
188
|
} else {
|
|
185
189
|
sql = `(
|
|
186
190
|
CASE
|
|
187
|
-
WHEN ${validateJsonArray(
|
|
191
|
+
WHEN ${validateJsonArray(key)} THEN
|
|
188
192
|
NOT EXISTS (
|
|
189
193
|
SELECT value
|
|
190
194
|
FROM json_each(?)
|
|
191
195
|
WHERE value NOT IN (
|
|
192
196
|
SELECT value
|
|
193
|
-
FROM json_each(json_extract(metadata,
|
|
197
|
+
FROM json_each(json_extract(metadata, ${jsonPath}))
|
|
194
198
|
)
|
|
195
199
|
)
|
|
196
200
|
ELSE FALSE
|
|
@@ -209,7 +213,7 @@ var FILTER_OPERATORS = {
|
|
|
209
213
|
};
|
|
210
214
|
},
|
|
211
215
|
$elemMatch: (key, value) => {
|
|
212
|
-
const
|
|
216
|
+
const jsonPath = getJsonPath(key);
|
|
213
217
|
if (typeof value !== "object" || Array.isArray(value)) {
|
|
214
218
|
throw new Error("$elemMatch requires an object with conditions");
|
|
215
219
|
}
|
|
@@ -217,10 +221,10 @@ var FILTER_OPERATORS = {
|
|
|
217
221
|
return {
|
|
218
222
|
sql: `(
|
|
219
223
|
CASE
|
|
220
|
-
WHEN ${validateJsonArray(
|
|
224
|
+
WHEN ${validateJsonArray(key)} THEN
|
|
221
225
|
EXISTS (
|
|
222
226
|
SELECT 1
|
|
223
|
-
FROM json_each(json_extract(metadata,
|
|
227
|
+
FROM json_each(json_extract(metadata, ${jsonPath})) as elem
|
|
224
228
|
WHERE ${conditions.map((c) => c.sql).join(" AND ")}
|
|
225
229
|
)
|
|
226
230
|
ELSE FALSE
|
|
@@ -232,9 +236,9 @@ var FILTER_OPERATORS = {
|
|
|
232
236
|
},
|
|
233
237
|
// Element Operators
|
|
234
238
|
$exists: (key) => {
|
|
235
|
-
const
|
|
239
|
+
const jsonPath = getJsonPath(key);
|
|
236
240
|
return {
|
|
237
|
-
sql: `json_extract(metadata,
|
|
241
|
+
sql: `json_extract(metadata, ${jsonPath}) IS NOT NULL`,
|
|
238
242
|
needsValue: false
|
|
239
243
|
};
|
|
240
244
|
},
|
|
@@ -253,12 +257,12 @@ var FILTER_OPERATORS = {
|
|
|
253
257
|
needsValue: false
|
|
254
258
|
}),
|
|
255
259
|
$size: (key, paramIndex) => {
|
|
256
|
-
const
|
|
260
|
+
const jsonPath = getJsonPath(key);
|
|
257
261
|
return {
|
|
258
262
|
sql: `(
|
|
259
263
|
CASE
|
|
260
|
-
WHEN json_type(json_extract(metadata,
|
|
261
|
-
json_array_length(json_extract(metadata,
|
|
264
|
+
WHEN json_type(json_extract(metadata, ${jsonPath})) = 'array' THEN
|
|
265
|
+
json_array_length(json_extract(metadata, ${jsonPath})) = $${paramIndex}
|
|
262
266
|
ELSE FALSE
|
|
263
267
|
END
|
|
264
268
|
)`,
|
|
@@ -377,7 +381,14 @@ function isFilterResult(obj) {
|
|
|
377
381
|
}
|
|
378
382
|
var parseJsonPathKey = (key) => {
|
|
379
383
|
const parsedKey = utils.parseFieldKey(key);
|
|
380
|
-
|
|
384
|
+
if (parsedKey.includes(".")) {
|
|
385
|
+
return parsedKey.split(".").map((segment) => `"${segment}"`).join(".");
|
|
386
|
+
}
|
|
387
|
+
return parsedKey;
|
|
388
|
+
};
|
|
389
|
+
var getJsonPath = (key) => {
|
|
390
|
+
const jsonPathKey = parseJsonPathKey(key);
|
|
391
|
+
return `'$.${jsonPathKey}'`;
|
|
381
392
|
};
|
|
382
393
|
function escapeLikePattern(str) {
|
|
383
394
|
return str.replace(/([%_\\])/g, "\\$1");
|
|
@@ -402,8 +413,9 @@ function buildCondition(key, value, parentPath) {
|
|
|
402
413
|
return handleLogicalOperator(key, value);
|
|
403
414
|
}
|
|
404
415
|
if (!value || typeof value !== "object") {
|
|
416
|
+
const jsonPath = getJsonPath(key);
|
|
405
417
|
return {
|
|
406
|
-
sql: `json_extract(metadata,
|
|
418
|
+
sql: `json_extract(metadata, ${jsonPath}) = ?`,
|
|
407
419
|
values: [value]
|
|
408
420
|
};
|
|
409
421
|
}
|
|
@@ -549,7 +561,8 @@ var LibSQLVector = class extends vector.MastraVector {
|
|
|
549
561
|
topK = 10,
|
|
550
562
|
filter,
|
|
551
563
|
includeVector = false,
|
|
552
|
-
minScore =
|
|
564
|
+
minScore = -1
|
|
565
|
+
// Default to -1 to include all results (cosine similarity ranges from -1 to 1)
|
|
553
566
|
}) {
|
|
554
567
|
try {
|
|
555
568
|
if (!Number.isInteger(topK) || topK <= 0) {
|