@o-lang/semantic-doc-search 1.1.1 → 1.1.3
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 +1 -1
- package/src/adapters/inMemoryAdapter.js +54 -5
- package/src/resolver.js +12 -8
package/package.json
CHANGED
|
@@ -1,11 +1,50 @@
|
|
|
1
1
|
const VectorAdapter = require("./VectorAdapter");
|
|
2
2
|
const capabilities = require("./vectorCapabilities");
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
|
|
6
|
+
// ✅ SINGLETON store — persists across all requests in the same process
|
|
7
|
+
// This ensures vectors ingested in one request are available in the next
|
|
8
|
+
const GLOBAL_STORE = [];
|
|
9
|
+
|
|
10
|
+
// ✅ Persist store to disk so vectors survive server restarts
|
|
11
|
+
const STORE_PATH = path.join(process.cwd(), "vector-store.json");
|
|
12
|
+
|
|
13
|
+
function loadStore() {
|
|
14
|
+
try {
|
|
15
|
+
if (fs.existsSync(STORE_PATH)) {
|
|
16
|
+
const data = JSON.parse(fs.readFileSync(STORE_PATH, "utf8"));
|
|
17
|
+
if (Array.isArray(data) && data.length > 0) {
|
|
18
|
+
GLOBAL_STORE.push(...data);
|
|
19
|
+
console.log(`[InMemoryAdapter] ✅ Loaded ${data.length} vectors from disk`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
} catch (e) {
|
|
23
|
+
console.warn("[InMemoryAdapter] Could not load vector store from disk:", e.message);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function saveStore() {
|
|
28
|
+
try {
|
|
29
|
+
fs.writeFileSync(STORE_PATH, JSON.stringify(GLOBAL_STORE, null, 2));
|
|
30
|
+
} catch (e) {
|
|
31
|
+
console.warn("[InMemoryAdapter] Could not save vector store to disk:", e.message);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Load persisted vectors once when the module is first required
|
|
36
|
+
let _loaded = false;
|
|
37
|
+
if (!_loaded) {
|
|
38
|
+
loadStore();
|
|
39
|
+
_loaded = true;
|
|
40
|
+
}
|
|
3
41
|
|
|
4
42
|
class InMemoryAdapter extends VectorAdapter {
|
|
5
43
|
constructor(config = {}) {
|
|
6
44
|
super({ ...config, backend: "memory" });
|
|
7
45
|
this.dimension = config.dimension || 384;
|
|
8
|
-
|
|
46
|
+
// ✅ Use singleton store instead of new empty array
|
|
47
|
+
this.store = GLOBAL_STORE;
|
|
9
48
|
}
|
|
10
49
|
|
|
11
50
|
static capabilities() {
|
|
@@ -14,13 +53,23 @@ class InMemoryAdapter extends VectorAdapter {
|
|
|
14
53
|
|
|
15
54
|
async upsert({ id, vector, content, source, metadata = {} }) {
|
|
16
55
|
this.validateVector(vector);
|
|
17
|
-
|
|
18
|
-
|
|
56
|
+
|
|
57
|
+
// ✅ Deduplicate by id — replace if exists
|
|
58
|
+
const existingIndex = this.store.findIndex(item => item.id === id);
|
|
59
|
+
if (existingIndex >= 0) {
|
|
60
|
+
this.store[existingIndex] = { id, vector, content, source, metadata };
|
|
61
|
+
} else {
|
|
62
|
+
this.store.push({ id, vector, content, source, metadata });
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// ✅ Persist to disk after every upsert
|
|
66
|
+
saveStore();
|
|
67
|
+
console.log("💾 Stored vector for:", id, "| store size:", this.store.length);
|
|
19
68
|
}
|
|
20
69
|
|
|
21
70
|
async query(vector, { topK = 5 } = {}) {
|
|
22
71
|
this.validateVector(vector);
|
|
23
|
-
|
|
72
|
+
console.log("🔍 Querying store with", this.store.length, "vectors");
|
|
24
73
|
return this.store
|
|
25
74
|
.map(doc => ({
|
|
26
75
|
...doc,
|
|
@@ -41,4 +90,4 @@ function cosineSimilarity(a, b) {
|
|
|
41
90
|
return dot / (Math.sqrt(na) * Math.sqrt(nb));
|
|
42
91
|
}
|
|
43
92
|
|
|
44
|
-
module.exports = InMemoryAdapter;
|
|
93
|
+
module.exports = InMemoryAdapter;
|
package/src/resolver.js
CHANGED
|
@@ -46,11 +46,13 @@ async function resolver(action, context = {}) {
|
|
|
46
46
|
|
|
47
47
|
// Extract ALL quoted args from action string
|
|
48
48
|
const args = [...action.matchAll(/"([^"]*)"/g)].map(m => m[1]);
|
|
49
|
-
|
|
49
|
+
|
|
50
50
|
const vectorStore = VectorRouter.create(context);
|
|
51
51
|
const getEmbedFn = await embedder({ dimension: 384 });
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
|
|
53
|
+
// ✅ Always use cache for local persistence — no Postgres/Redis required
|
|
54
|
+
const useCache = true;
|
|
55
|
+
const cache = loadCache();
|
|
54
56
|
|
|
55
57
|
// =====================================================
|
|
56
58
|
// ✅ INGEST: 1 arg = doc_root
|
|
@@ -71,7 +73,7 @@ async function resolver(action, context = {}) {
|
|
|
71
73
|
const text = sanitizeTextForEmbedding(chunks[i]);
|
|
72
74
|
if (!text) continue;
|
|
73
75
|
const hash = hashText(text);
|
|
74
|
-
if (
|
|
76
|
+
if (cache[hash]) continue; // skip already-ingested chunks
|
|
75
77
|
const rawVector = await getEmbedFn(text);
|
|
76
78
|
await vectorStore.upsert({
|
|
77
79
|
id: `${file}:${i}`,
|
|
@@ -79,13 +81,15 @@ async function resolver(action, context = {}) {
|
|
|
79
81
|
content: text,
|
|
80
82
|
source: `file:${file}`,
|
|
81
83
|
});
|
|
82
|
-
|
|
84
|
+
cache[hash] = true;
|
|
83
85
|
inserted++;
|
|
84
86
|
}
|
|
85
87
|
}
|
|
86
88
|
}
|
|
87
|
-
|
|
89
|
+
|
|
90
|
+
saveCache(cache);
|
|
88
91
|
if (vectorStore.close) await vectorStore.close();
|
|
92
|
+
console.log(`[vector.search] ✅ Ingested ${inserted} chunks from ${ingestRoot}`);
|
|
89
93
|
return { inserted, doc_root: ingestRoot };
|
|
90
94
|
}
|
|
91
95
|
|
|
@@ -110,6 +114,6 @@ async function resolver(action, context = {}) {
|
|
|
110
114
|
|
|
111
115
|
// ✅ Must match workflow's "Allow resolvers: - vector.search"
|
|
112
116
|
resolver.resolverName = "vector.search";
|
|
113
|
-
resolver.version = "1.0.
|
|
117
|
+
resolver.version = "1.0.43";
|
|
114
118
|
|
|
115
|
-
module.exports = resolver;
|
|
119
|
+
module.exports = resolver;
|