@memrosetta/core 0.4.0 → 0.4.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/dist/index.js +51 -12
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -88,6 +88,26 @@ var SCHEMA_V5 = `
|
|
|
88
88
|
ALTER TABLE memories ADD COLUMN use_count INTEGER DEFAULT 0;
|
|
89
89
|
ALTER TABLE memories ADD COLUMN success_count INTEGER DEFAULT 0;
|
|
90
90
|
`;
|
|
91
|
+
var SCHEMA_V6 = `
|
|
92
|
+
CREATE TABLE IF NOT EXISTS migration_version (
|
|
93
|
+
name TEXT PRIMARY KEY,
|
|
94
|
+
applied_at TEXT NOT NULL
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
CREATE TABLE IF NOT EXISTS memory_legacy_scope (
|
|
98
|
+
memory_id TEXT PRIMARY KEY,
|
|
99
|
+
legacy_user_id TEXT NOT NULL,
|
|
100
|
+
legacy_namespace TEXT,
|
|
101
|
+
migrated_at TEXT NOT NULL,
|
|
102
|
+
FOREIGN KEY (memory_id) REFERENCES memories(memory_id)
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
CREATE INDEX IF NOT EXISTS idx_memory_legacy_scope_user
|
|
106
|
+
ON memory_legacy_scope(legacy_user_id);
|
|
107
|
+
|
|
108
|
+
CREATE INDEX IF NOT EXISTS idx_memory_legacy_scope_user_ns
|
|
109
|
+
ON memory_legacy_scope(legacy_user_id, legacy_namespace);
|
|
110
|
+
`;
|
|
91
111
|
function schemaV2(dim) {
|
|
92
112
|
return `CREATE VIRTUAL TABLE IF NOT EXISTS vec_memories USING vec0(embedding float[${dim}]);`;
|
|
93
113
|
}
|
|
@@ -122,7 +142,8 @@ function ensureSchema(db, options) {
|
|
|
122
142
|
db.exec(schemaV2(dim));
|
|
123
143
|
version = 2;
|
|
124
144
|
}
|
|
125
|
-
|
|
145
|
+
db.exec(SCHEMA_V6);
|
|
146
|
+
version = 6;
|
|
126
147
|
db.prepare("INSERT INTO schema_version (version, embedding_dimension) VALUES (?, ?)").run(version, dim);
|
|
127
148
|
return;
|
|
128
149
|
}
|
|
@@ -154,6 +175,10 @@ function ensureSchema(db, options) {
|
|
|
154
175
|
}
|
|
155
176
|
db.prepare("UPDATE schema_version SET version = ?").run(5);
|
|
156
177
|
}
|
|
178
|
+
if (currentVersion < 6) {
|
|
179
|
+
db.exec(SCHEMA_V6);
|
|
180
|
+
db.prepare("UPDATE schema_version SET version = ?").run(6);
|
|
181
|
+
}
|
|
157
182
|
if (options?.vectorEnabled) {
|
|
158
183
|
const hasVecTable = db.prepare(
|
|
159
184
|
"SELECT name FROM sqlite_master WHERE type='table' AND name='vec_memories'"
|
|
@@ -490,7 +515,7 @@ function getRelationsByMemory(stmts, memoryId) {
|
|
|
490
515
|
}
|
|
491
516
|
|
|
492
517
|
// src/search.ts
|
|
493
|
-
var FTS5_SPECIAL_CHARS = /["\*\(\):^{}\[\]?!.,;'
|
|
518
|
+
var FTS5_SPECIAL_CHARS = /["\*\(\):^{}\[\]?!.,;'\\/]/g;
|
|
494
519
|
var STOP_WORDS = /* @__PURE__ */ new Set([
|
|
495
520
|
"a",
|
|
496
521
|
"an",
|
|
@@ -603,16 +628,32 @@ var STOP_WORDS = /* @__PURE__ */ new Set([
|
|
|
603
628
|
"got",
|
|
604
629
|
"getting"
|
|
605
630
|
]);
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
631
|
+
var KOREAN_LOW_SIGNAL_TOKENS = /* @__PURE__ */ new Set([
|
|
632
|
+
"\uBB50\uC9C0",
|
|
633
|
+
"\uBB50\uC57C",
|
|
634
|
+
"\uBB54\uC9C0",
|
|
635
|
+
"\uC5B4\uB514",
|
|
636
|
+
"\uC65C",
|
|
637
|
+
"\uC5B4\uB5BB\uAC8C",
|
|
638
|
+
"\uC5B8\uC81C",
|
|
639
|
+
"\uB204\uAD6C",
|
|
640
|
+
"\uC54C\uB824\uC918"
|
|
641
|
+
]);
|
|
642
|
+
function preprocessQuery(rawQuery) {
|
|
643
|
+
const normalized = rawQuery.normalize("NFKC").toLowerCase().split(/\s+/).filter((t) => t.length > 0).map((t) => t.replace(FTS5_SPECIAL_CHARS, "")).filter((t) => t.length > 0);
|
|
644
|
+
if (normalized.length === 0) {
|
|
645
|
+
return [];
|
|
610
646
|
}
|
|
611
|
-
const meaningful =
|
|
612
|
-
|
|
647
|
+
const meaningful = normalized.filter(
|
|
648
|
+
(t) => !STOP_WORDS.has(t) && !KOREAN_LOW_SIGNAL_TOKENS.has(t)
|
|
649
|
+
);
|
|
650
|
+
return meaningful.length > 0 ? meaningful : normalized;
|
|
651
|
+
}
|
|
652
|
+
function buildFtsQuery(rawQuery) {
|
|
653
|
+
const tokens = preprocessQuery(rawQuery);
|
|
613
654
|
if (tokens.length === 0) return "";
|
|
614
655
|
if (tokens.length === 1) return `"${tokens[0]}"`;
|
|
615
|
-
if (tokens.length
|
|
656
|
+
if (tokens.length === 2) {
|
|
616
657
|
return tokens.map((t) => `"${t}"`).join(" AND ");
|
|
617
658
|
}
|
|
618
659
|
return tokens.map((t) => `"${t}"`).join(" OR ");
|
|
@@ -1064,9 +1105,7 @@ function applyKeywordBoost(results, queryTokens) {
|
|
|
1064
1105
|
}).sort((a, b) => b.score - a.score);
|
|
1065
1106
|
}
|
|
1066
1107
|
function extractQueryTokens(rawQuery) {
|
|
1067
|
-
|
|
1068
|
-
const meaningful = allTokens.filter((t) => !STOP_WORDS.has(t));
|
|
1069
|
-
return meaningful.length > 0 ? meaningful : allTokens;
|
|
1108
|
+
return preprocessQuery(rawQuery);
|
|
1070
1109
|
}
|
|
1071
1110
|
function updateAccessTracking(db, memoryIds) {
|
|
1072
1111
|
if (memoryIds.length === 0) return;
|