sigma-memory 0.2.0 → 0.2.2
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.d.ts +8 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +22 -22
- package/dist/index.js.map +1 -1
- package/dist/notes.d.ts +1 -1
- package/dist/notes.d.ts.map +1 -1
- package/dist/notes.js +24 -24
- package/dist/notes.js.map +1 -1
- package/dist/ontology.d.ts +5 -4
- package/dist/ontology.d.ts.map +1 -1
- package/dist/ontology.js +32 -24
- package/dist/ontology.js.map +1 -1
- package/dist/types.d.ts +7 -7
- package/dist/types.d.ts.map +1 -1
- package/dist/vector-store.d.ts +1 -1
- package/dist/vector-store.d.ts.map +1 -1
- package/dist/vector-store.js +39 -33
- package/dist/vector-store.js.map +1 -1
- package/package.json +3 -2
- package/src/index.ts +176 -176
- package/src/notes.ts +161 -161
- package/src/ontology.ts +359 -346
- package/src/types.ts +63 -63
- package/src/vector-store.ts +356 -357
- package/src/vendor.d.ts +21 -21
package/src/index.ts
CHANGED
|
@@ -1,184 +1,184 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { homedir } from
|
|
3
|
-
import {
|
|
4
|
-
import { NotesManager } from
|
|
5
|
-
import { OntologyManager } from
|
|
6
|
-
import {
|
|
7
|
-
import
|
|
1
|
+
import { existsSync, mkdirSync } from "fs";
|
|
2
|
+
import { homedir } from "os";
|
|
3
|
+
import { join } from "path";
|
|
4
|
+
import { NotesManager } from "./notes.js";
|
|
5
|
+
import { OntologyManager } from "./ontology.js";
|
|
6
|
+
import type { MemoryConfig, MemoryStatus, UnifiedSearchResult } from "./types.js";
|
|
7
|
+
import { VectorStore } from "./vector-store.js";
|
|
8
8
|
|
|
9
9
|
export class SigmaMemory {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
10
|
+
public readonly notes: NotesManager;
|
|
11
|
+
public readonly ontology: OntologyManager;
|
|
12
|
+
public readonly vectors: VectorStore;
|
|
13
|
+
private readonly config: MemoryConfig;
|
|
14
|
+
|
|
15
|
+
constructor(config?: Partial<MemoryConfig>) {
|
|
16
|
+
// Default configuration
|
|
17
|
+
const defaultConfig: MemoryConfig = {
|
|
18
|
+
memoryDir: join(homedir(), ".phi", "memory"),
|
|
19
|
+
projectMemoryDir: join(process.cwd(), ".phi", "memory"),
|
|
20
|
+
ontologyPath: join(homedir(), ".phi", "memory", "ontology", "graph.jsonl"),
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
this.config = { ...defaultConfig, ...config };
|
|
24
|
+
|
|
25
|
+
// Initialize managers
|
|
26
|
+
this.notes = new NotesManager(this.config);
|
|
27
|
+
this.ontology = new OntologyManager(this.config);
|
|
28
|
+
this.vectors = new VectorStore(join(this.config.memoryDir, "vectors.db"));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Unified search: searches notes + ontology + vectors, combines results
|
|
33
|
+
*/
|
|
34
|
+
async search(query: string): Promise<UnifiedSearchResult[]> {
|
|
35
|
+
const results: UnifiedSearchResult[] = [];
|
|
36
|
+
|
|
37
|
+
// Search in notes (full-text grep)
|
|
38
|
+
try {
|
|
39
|
+
const notesResults = this.notes.search(query);
|
|
40
|
+
for (const result of notesResults) {
|
|
41
|
+
results.push({
|
|
42
|
+
source: "notes",
|
|
43
|
+
type: "note",
|
|
44
|
+
score: 0.8, // Default score for text-match notes
|
|
45
|
+
data: result,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
} catch (error) {
|
|
49
|
+
// Notes search failed silently
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Search in ontology
|
|
53
|
+
try {
|
|
54
|
+
const entityResults = this.ontology.findEntity({ name: query });
|
|
55
|
+
for (const entity of entityResults) {
|
|
56
|
+
results.push({
|
|
57
|
+
source: "ontology",
|
|
58
|
+
type: "entity",
|
|
59
|
+
score: 0.9,
|
|
60
|
+
data: entity,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// Include relations for this entity
|
|
64
|
+
const relations = this.ontology.findRelations(entity.id);
|
|
65
|
+
for (const relation of relations) {
|
|
66
|
+
results.push({
|
|
67
|
+
source: "ontology",
|
|
68
|
+
type: "relation",
|
|
69
|
+
score: 0.7,
|
|
70
|
+
data: relation,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
} catch (error) {
|
|
75
|
+
// Ontology search failed silently
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Vector similarity search
|
|
79
|
+
try {
|
|
80
|
+
const vectorResults = await this.vectors.search(query, 5);
|
|
81
|
+
for (const result of vectorResults) {
|
|
82
|
+
results.push({
|
|
83
|
+
source: "vectors",
|
|
84
|
+
type: "file",
|
|
85
|
+
score: result.score,
|
|
86
|
+
data: result,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
} catch (error) {
|
|
90
|
+
// Vector search failed silently
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Sort by score descending
|
|
94
|
+
results.sort((a, b) => b.score - a.score);
|
|
95
|
+
|
|
96
|
+
return results;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Initialize all required directories and the vector store.
|
|
101
|
+
* On first run, this will download the embedding model and index notes.
|
|
102
|
+
*/
|
|
103
|
+
async init(): Promise<void> {
|
|
104
|
+
// Create base directories
|
|
105
|
+
if (!existsSync(this.config.memoryDir)) {
|
|
106
|
+
mkdirSync(this.config.memoryDir, { recursive: true });
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (!existsSync(this.config.projectMemoryDir)) {
|
|
110
|
+
mkdirSync(this.config.projectMemoryDir, { recursive: true });
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Initialize vector store (DB setup only — fast)
|
|
114
|
+
await this.vectors.init();
|
|
115
|
+
|
|
116
|
+
// Auto-index existing notes into the vector store
|
|
117
|
+
await this.indexNotes();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Index all markdown notes into the vector store.
|
|
122
|
+
* Reads every .md file from the notes directory and adds it.
|
|
123
|
+
*/
|
|
124
|
+
async indexNotes(): Promise<void> {
|
|
125
|
+
const notesList = this.notes.list();
|
|
126
|
+
|
|
127
|
+
for (const note of notesList) {
|
|
128
|
+
try {
|
|
129
|
+
const content = this.notes.read(note.name);
|
|
130
|
+
await this.vectors.addDocument(note.name, content);
|
|
131
|
+
} catch {
|
|
132
|
+
// Skip files that can't be read
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Status of all subsystems
|
|
139
|
+
*/
|
|
140
|
+
async status(): Promise<MemoryStatus> {
|
|
141
|
+
// Notes status
|
|
142
|
+
const notesList = this.notes.list();
|
|
143
|
+
const notesStatus = {
|
|
144
|
+
count: notesList.length,
|
|
145
|
+
totalSize: notesList.reduce((sum, note) => sum + note.size, 0),
|
|
146
|
+
lastModified: notesList.length > 0 ? notesList[0].date : null,
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
// Ontology status
|
|
150
|
+
const ontologyStats = this.ontology.stats();
|
|
151
|
+
const ontologyGraph = this.ontology.getGraph();
|
|
152
|
+
const ontologyStatus = {
|
|
153
|
+
entities: ontologyGraph.entities.length,
|
|
154
|
+
relations: ontologyGraph.relations.length,
|
|
155
|
+
entitiesByType: ontologyStats.entitiesByType,
|
|
156
|
+
relationsByType: ontologyStats.relationsByType,
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
// Vector store status
|
|
160
|
+
const vectorStats = this.vectors.getStats();
|
|
161
|
+
|
|
162
|
+
return {
|
|
163
|
+
notes: notesStatus,
|
|
164
|
+
ontology: ontologyStatus,
|
|
165
|
+
vectors: vectorStats,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Current configuration
|
|
171
|
+
*/
|
|
172
|
+
getConfig(): MemoryConfig {
|
|
173
|
+
return { ...this.config };
|
|
174
|
+
}
|
|
175
175
|
}
|
|
176
176
|
|
|
177
177
|
// Convenient exports
|
|
178
|
-
export { NotesManager } from
|
|
179
|
-
export { OntologyManager } from
|
|
180
|
-
export
|
|
181
|
-
export
|
|
178
|
+
export { NotesManager } from "./notes.js";
|
|
179
|
+
export { OntologyManager } from "./ontology.js";
|
|
180
|
+
export * from "./types.js";
|
|
181
|
+
export { VectorStore } from "./vector-store.js";
|
|
182
182
|
|
|
183
183
|
// Default export
|
|
184
184
|
export default SigmaMemory;
|