-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemoryStore.js
More file actions
168 lines (148 loc) · 4.96 KB
/
memoryStore.js
File metadata and controls
168 lines (148 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
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
/**
* guIDE — Memory Store
*
* Persistent cross-session memory: conversations, project facts, code patterns,
* and error history. Debounced save (5 s) to <projectRoot>/.ide-memory/memory.json.
*/
'use strict';
const fs = require('fs');
const path = require('path');
const log = require('./logger');
class MemoryStore {
constructor() {
this._basePath = null;
this._filePath = null;
this._saveTimer = null;
this.conversations = [];
this.projectFacts = new Map();
this.codePatterns = new Map();
this.errorHistory = [];
}
/* ── Lifecycle ─────────────────────────────────────────────────── */
initialize(projectPath) {
if (!projectPath) return;
this._basePath = path.join(projectPath, '.ide-memory');
this._filePath = path.join(this._basePath, 'memory.json');
try {
fs.mkdirSync(this._basePath, { recursive: true });
if (fs.existsSync(this._filePath)) {
const raw = JSON.parse(fs.readFileSync(this._filePath, 'utf8'));
this.conversations = raw.conversations || [];
this.projectFacts = new Map(Object.entries(raw.projectFacts || {}));
this.codePatterns = new Map(Object.entries(raw.codePatterns || {}));
this.errorHistory = raw.errorHistory || [];
log.info('Memory', `Loaded ${this.conversations.length} conversations, ${this.projectFacts.size} facts`);
}
} catch (e) {
log.warn('Memory', 'Failed to load memory store:', e.message);
}
}
/* ── Learning ──────────────────────────────────────────────────── */
addConversation(entry) {
this.conversations.push({
timestamp: Date.now(),
...entry,
});
// Keep last 200 conversations
if (this.conversations.length > 200) {
this.conversations = this.conversations.slice(-200);
}
this._scheduleSave();
}
learnFact(key, value) {
this.projectFacts.set(key, { value, learnedAt: Date.now() });
this._scheduleSave();
}
learnPattern(key, pattern) {
this.codePatterns.set(key, { pattern, learnedAt: Date.now() });
this._scheduleSave();
}
recordError(error) {
this.errorHistory.push({
timestamp: Date.now(),
message: typeof error === 'string' ? error : error.message,
stack: error?.stack,
});
// Keep last 100 errors
if (this.errorHistory.length > 100) {
this.errorHistory = this.errorHistory.slice(-100);
}
this._scheduleSave();
}
/* ── Querying ──────────────────────────────────────────────────── */
findSimilarErrors(errorMsg) {
if (!errorMsg) return [];
const lower = errorMsg.toLowerCase();
return this.errorHistory.filter(e =>
e.message && e.message.toLowerCase().includes(lower)
).slice(-5);
}
getContextPrompt() {
const parts = [];
if (this.projectFacts.size) {
parts.push('Known project facts:');
for (const [k, v] of this.projectFacts) {
parts.push(` - ${k}: ${v.value}`);
}
}
if (this.codePatterns.size) {
parts.push('Known code patterns:');
for (const [k, v] of this.codePatterns) {
parts.push(` - ${k}: ${v.pattern}`);
}
}
return parts.length ? parts.join('\n') : '';
}
getStats() {
return {
conversations: this.conversations.length,
facts: this.projectFacts.size,
patterns: this.codePatterns.size,
errors: this.errorHistory.length,
};
}
clear() {
this.conversations = [];
this.projectFacts.clear();
this.codePatterns.clear();
this.errorHistory = [];
this._scheduleSave();
}
clearConversations() {
this.conversations = [];
this._scheduleSave();
}
dispose() {
if (this._saveTimer) {
clearTimeout(this._saveTimer);
this._saveTimer = null;
}
this._save(); // final flush
}
/* ── Persistence ───────────────────────────────────────────────── */
_scheduleSave() {
if (this._saveTimer) return;
this._saveTimer = setTimeout(() => {
this._saveTimer = null;
this._save();
}, 5000);
}
_save() {
if (!this._filePath) return;
const data = {
conversations: this.conversations,
projectFacts: Object.fromEntries(this.projectFacts),
codePatterns: Object.fromEntries(this.codePatterns),
errorHistory: this.errorHistory,
};
try {
fs.mkdirSync(this._basePath, { recursive: true });
const tmp = this._filePath + '.tmp';
fs.writeFileSync(tmp, JSON.stringify(data, null, 2));
fs.renameSync(tmp, this._filePath);
} catch (e) {
log.warn('Memory', 'Failed to save memory store:', e.message);
}
}
}
module.exports = { MemoryStore };