sigma-memory 0.2.1 → 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/src/index.ts CHANGED
@@ -1,184 +1,184 @@
1
- import { join } from 'path';
2
- import { homedir } from 'os';
3
- import { existsSync, mkdirSync } from 'fs';
4
- import { NotesManager } from './notes.js';
5
- import { OntologyManager } from './ontology.js';
6
- import { VectorStore } from './vector-store.js';
7
- import type { MemoryConfig, UnifiedSearchResult, MemoryStatus } from './types.js';
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
- 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
- }
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 './notes.js';
179
- export { OntologyManager } from './ontology.js';
180
- export { VectorStore } from './vector-store.js';
181
- export * from './types.js';
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;