@o-lang/semantic-doc-search 1.1.2 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@o-lang/semantic-doc-search",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "description": "O-Lang semantic document search resolver with vector embeddings",
5
5
  "main": "src/index.js",
6
6
  "exports": {
@@ -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
- this.store = [];
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
- this.store.push({ id, vector, content, source, metadata });
18
- console.log("💾 Stored vector for:", id, "store size:", this.store.length);
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
- console.log("🔍 Querying store with", this.store.length, "vectors");
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;