opencode-mem 1.0.0
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/README.md +588 -0
- package/dist/config.d.ts +33 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +258 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +618 -0
- package/dist/plugin.d.ts +5 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/plugin.js +15 -0
- package/dist/services/api-handlers.d.ts +102 -0
- package/dist/services/api-handlers.d.ts.map +1 -0
- package/dist/services/api-handlers.js +494 -0
- package/dist/services/auto-capture.d.ts +32 -0
- package/dist/services/auto-capture.d.ts.map +1 -0
- package/dist/services/auto-capture.js +451 -0
- package/dist/services/cleanup-service.d.ts +20 -0
- package/dist/services/cleanup-service.d.ts.map +1 -0
- package/dist/services/cleanup-service.js +88 -0
- package/dist/services/client.d.ts +104 -0
- package/dist/services/client.d.ts.map +1 -0
- package/dist/services/client.js +251 -0
- package/dist/services/compaction.d.ts +92 -0
- package/dist/services/compaction.d.ts.map +1 -0
- package/dist/services/compaction.js +421 -0
- package/dist/services/context.d.ts +17 -0
- package/dist/services/context.d.ts.map +1 -0
- package/dist/services/context.js +41 -0
- package/dist/services/deduplication-service.d.ts +30 -0
- package/dist/services/deduplication-service.d.ts.map +1 -0
- package/dist/services/deduplication-service.js +131 -0
- package/dist/services/embedding.d.ts +10 -0
- package/dist/services/embedding.d.ts.map +1 -0
- package/dist/services/embedding.js +77 -0
- package/dist/services/jsonc.d.ts +7 -0
- package/dist/services/jsonc.d.ts.map +1 -0
- package/dist/services/jsonc.js +76 -0
- package/dist/services/logger.d.ts +2 -0
- package/dist/services/logger.d.ts.map +1 -0
- package/dist/services/logger.js +16 -0
- package/dist/services/migration-service.d.ts +42 -0
- package/dist/services/migration-service.d.ts.map +1 -0
- package/dist/services/migration-service.js +258 -0
- package/dist/services/privacy.d.ts +4 -0
- package/dist/services/privacy.d.ts.map +1 -0
- package/dist/services/privacy.js +10 -0
- package/dist/services/sqlite/connection-manager.d.ts +10 -0
- package/dist/services/sqlite/connection-manager.d.ts.map +1 -0
- package/dist/services/sqlite/connection-manager.js +45 -0
- package/dist/services/sqlite/shard-manager.d.ts +20 -0
- package/dist/services/sqlite/shard-manager.d.ts.map +1 -0
- package/dist/services/sqlite/shard-manager.js +221 -0
- package/dist/services/sqlite/types.d.ts +39 -0
- package/dist/services/sqlite/types.d.ts.map +1 -0
- package/dist/services/sqlite/types.js +1 -0
- package/dist/services/sqlite/vector-search.d.ts +18 -0
- package/dist/services/sqlite/vector-search.d.ts.map +1 -0
- package/dist/services/sqlite/vector-search.js +129 -0
- package/dist/services/sqlite-client.d.ts +116 -0
- package/dist/services/sqlite-client.d.ts.map +1 -0
- package/dist/services/sqlite-client.js +284 -0
- package/dist/services/tags.d.ts +20 -0
- package/dist/services/tags.d.ts.map +1 -0
- package/dist/services/tags.js +76 -0
- package/dist/services/web-server-lock.d.ts +12 -0
- package/dist/services/web-server-lock.d.ts.map +1 -0
- package/dist/services/web-server-lock.js +157 -0
- package/dist/services/web-server-worker.d.ts +2 -0
- package/dist/services/web-server-worker.d.ts.map +1 -0
- package/dist/services/web-server-worker.js +221 -0
- package/dist/services/web-server.d.ts +22 -0
- package/dist/services/web-server.d.ts.map +1 -0
- package/dist/services/web-server.js +134 -0
- package/dist/types/index.d.ts +48 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +1 -0
- package/dist/web/app.d.ts +2 -0
- package/dist/web/app.d.ts.map +1 -0
- package/dist/web/app.js +691 -0
- package/dist/web/favicon.ico +0 -0
- package/dist/web/favicon.svg +14 -0
- package/dist/web/index.html +202 -0
- package/dist/web/styles.css +851 -0
- package/package.json +52 -0
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { memoryClient } from "./client.js";
|
|
5
|
+
import { log } from "./logger.js";
|
|
6
|
+
import { CONFIG } from "../config.js";
|
|
7
|
+
const MESSAGE_STORAGE = join(homedir(), ".opencode", "messages");
|
|
8
|
+
const PART_STORAGE = join(homedir(), ".opencode", "parts");
|
|
9
|
+
const DEFAULT_THRESHOLD = 0.80;
|
|
10
|
+
const MIN_TOKENS_FOR_COMPACTION = 50_000;
|
|
11
|
+
const COMPACTION_COOLDOWN_MS = 30_000;
|
|
12
|
+
const CLAUDE_DEFAULT_CONTEXT_LIMIT = 200_000;
|
|
13
|
+
const CLAUDE_MODEL_PATTERN = /claude-(opus|sonnet|haiku)/i;
|
|
14
|
+
function createCompactionPrompt(projectMemories) {
|
|
15
|
+
const memoriesSection = projectMemories.length > 0
|
|
16
|
+
? `
|
|
17
|
+
## Project Knowledge (from Memory System)
|
|
18
|
+
The following project-specific knowledge should be preserved and referenced in the summary:
|
|
19
|
+
${projectMemories.map(m => `- ${m}`).join('\n')}
|
|
20
|
+
`
|
|
21
|
+
: '';
|
|
22
|
+
return `[COMPACTION CONTEXT INJECTION]
|
|
23
|
+
|
|
24
|
+
When summarizing this session, you MUST include the following sections in your summary:
|
|
25
|
+
|
|
26
|
+
## 1. User Requests (As-Is)
|
|
27
|
+
- List all original user requests exactly as they were stated
|
|
28
|
+
- Preserve the user's exact wording and intent
|
|
29
|
+
|
|
30
|
+
## 2. Final Goal
|
|
31
|
+
- What the user ultimately wanted to achieve
|
|
32
|
+
- The end result or deliverable expected
|
|
33
|
+
|
|
34
|
+
## 3. Work Completed
|
|
35
|
+
- What has been done so far
|
|
36
|
+
- Files created/modified
|
|
37
|
+
- Features implemented
|
|
38
|
+
- Problems solved
|
|
39
|
+
|
|
40
|
+
## 4. Remaining Tasks
|
|
41
|
+
- What still needs to be done
|
|
42
|
+
- Pending items from the original request
|
|
43
|
+
- Follow-up tasks identified during the work
|
|
44
|
+
|
|
45
|
+
## 5. MUST NOT Do (Critical Constraints)
|
|
46
|
+
- Things that were explicitly forbidden
|
|
47
|
+
- Approaches that failed and should not be retried
|
|
48
|
+
- User's explicit restrictions or preferences
|
|
49
|
+
- Anti-patterns identified during the session
|
|
50
|
+
${memoriesSection}
|
|
51
|
+
This context is critical for maintaining continuity after compaction.
|
|
52
|
+
`;
|
|
53
|
+
}
|
|
54
|
+
function isSupportedModel(modelID) {
|
|
55
|
+
return CLAUDE_MODEL_PATTERN.test(modelID);
|
|
56
|
+
}
|
|
57
|
+
function getMessageDir(sessionID) {
|
|
58
|
+
if (!existsSync(MESSAGE_STORAGE))
|
|
59
|
+
return null;
|
|
60
|
+
const directPath = join(MESSAGE_STORAGE, sessionID);
|
|
61
|
+
if (existsSync(directPath))
|
|
62
|
+
return directPath;
|
|
63
|
+
for (const dir of readdirSync(MESSAGE_STORAGE)) {
|
|
64
|
+
const sessionPath = join(MESSAGE_STORAGE, dir, sessionID);
|
|
65
|
+
if (existsSync(sessionPath))
|
|
66
|
+
return sessionPath;
|
|
67
|
+
}
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
function getOrCreateMessageDir(sessionID) {
|
|
71
|
+
if (!existsSync(MESSAGE_STORAGE)) {
|
|
72
|
+
mkdirSync(MESSAGE_STORAGE, { recursive: true });
|
|
73
|
+
}
|
|
74
|
+
const directPath = join(MESSAGE_STORAGE, sessionID);
|
|
75
|
+
if (existsSync(directPath))
|
|
76
|
+
return directPath;
|
|
77
|
+
for (const dir of readdirSync(MESSAGE_STORAGE)) {
|
|
78
|
+
const sessionPath = join(MESSAGE_STORAGE, dir, sessionID);
|
|
79
|
+
if (existsSync(sessionPath))
|
|
80
|
+
return sessionPath;
|
|
81
|
+
}
|
|
82
|
+
mkdirSync(directPath, { recursive: true });
|
|
83
|
+
return directPath;
|
|
84
|
+
}
|
|
85
|
+
function findNearestMessageWithFields(messageDir) {
|
|
86
|
+
try {
|
|
87
|
+
const files = readdirSync(messageDir)
|
|
88
|
+
.filter((f) => f.endsWith(".json"))
|
|
89
|
+
.sort()
|
|
90
|
+
.reverse();
|
|
91
|
+
for (const file of files) {
|
|
92
|
+
try {
|
|
93
|
+
const content = readFileSync(join(messageDir, file), "utf-8");
|
|
94
|
+
const msg = JSON.parse(content);
|
|
95
|
+
if (msg.agent && msg.model?.providerID && msg.model?.modelID) {
|
|
96
|
+
return msg;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
function generateMessageId() {
|
|
110
|
+
const timestamp = Date.now().toString(16);
|
|
111
|
+
const random = Math.random().toString(36).substring(2, 14);
|
|
112
|
+
return `msg_${timestamp}${random}`;
|
|
113
|
+
}
|
|
114
|
+
function generatePartId() {
|
|
115
|
+
const timestamp = Date.now().toString(16);
|
|
116
|
+
const random = Math.random().toString(36).substring(2, 10);
|
|
117
|
+
return `prt_${timestamp}${random}`;
|
|
118
|
+
}
|
|
119
|
+
function injectHookMessage(sessionID, hookContent, originalMessage) {
|
|
120
|
+
if (!hookContent || hookContent.trim().length === 0) {
|
|
121
|
+
log("[compaction] attempted to inject empty content, skipping");
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
const messageDir = getOrCreateMessageDir(sessionID);
|
|
125
|
+
const fallback = findNearestMessageWithFields(messageDir);
|
|
126
|
+
const now = Date.now();
|
|
127
|
+
const messageID = generateMessageId();
|
|
128
|
+
const partID = generatePartId();
|
|
129
|
+
const resolvedAgent = originalMessage.agent ?? fallback?.agent ?? "general";
|
|
130
|
+
const resolvedModel = originalMessage.model?.providerID && originalMessage.model?.modelID
|
|
131
|
+
? { providerID: originalMessage.model.providerID, modelID: originalMessage.model.modelID }
|
|
132
|
+
: fallback?.model?.providerID && fallback?.model?.modelID
|
|
133
|
+
? { providerID: fallback.model.providerID, modelID: fallback.model.modelID }
|
|
134
|
+
: undefined;
|
|
135
|
+
const messageMeta = {
|
|
136
|
+
id: messageID,
|
|
137
|
+
sessionID,
|
|
138
|
+
role: "user",
|
|
139
|
+
time: { created: now },
|
|
140
|
+
agent: resolvedAgent,
|
|
141
|
+
model: resolvedModel,
|
|
142
|
+
path: originalMessage.path?.cwd
|
|
143
|
+
? { cwd: originalMessage.path.cwd, root: originalMessage.path.root ?? "/" }
|
|
144
|
+
: undefined,
|
|
145
|
+
};
|
|
146
|
+
const textPart = {
|
|
147
|
+
id: partID,
|
|
148
|
+
type: "text",
|
|
149
|
+
text: hookContent,
|
|
150
|
+
synthetic: true,
|
|
151
|
+
time: { start: now, end: now },
|
|
152
|
+
messageID,
|
|
153
|
+
sessionID,
|
|
154
|
+
};
|
|
155
|
+
try {
|
|
156
|
+
writeFileSync(join(messageDir, `${messageID}.json`), JSON.stringify(messageMeta, null, 2));
|
|
157
|
+
const partDir = join(PART_STORAGE, messageID);
|
|
158
|
+
if (!existsSync(partDir)) {
|
|
159
|
+
mkdirSync(partDir, { recursive: true });
|
|
160
|
+
}
|
|
161
|
+
writeFileSync(join(partDir, `${partID}.json`), JSON.stringify(textPart, null, 2));
|
|
162
|
+
log("[compaction] hook message injected", { sessionID, messageID });
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
catch (err) {
|
|
166
|
+
log("[compaction] failed to inject hook message", { error: String(err) });
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
export function createCompactionHook(ctx, tags, options) {
|
|
171
|
+
const state = {
|
|
172
|
+
lastCompactionTime: new Map(),
|
|
173
|
+
compactionInProgress: new Set(),
|
|
174
|
+
summarizedSessions: new Set(),
|
|
175
|
+
};
|
|
176
|
+
const threshold = options?.threshold ?? DEFAULT_THRESHOLD;
|
|
177
|
+
const getModelLimit = options?.getModelLimit;
|
|
178
|
+
async function fetchProjectMemoriesForCompaction() {
|
|
179
|
+
try {
|
|
180
|
+
const result = await memoryClient.listMemories(tags.project, CONFIG.maxProjectMemories);
|
|
181
|
+
const memories = result.memories || [];
|
|
182
|
+
return memories.map((m) => m.summary || m.content || "").filter(Boolean);
|
|
183
|
+
}
|
|
184
|
+
catch (err) {
|
|
185
|
+
log("[compaction] failed to fetch project memories", { error: String(err) });
|
|
186
|
+
return [];
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
async function injectCompactionContext(summarizeCtx) {
|
|
190
|
+
log("[compaction] injecting context", { sessionID: summarizeCtx.sessionID });
|
|
191
|
+
const projectMemories = await fetchProjectMemoriesForCompaction();
|
|
192
|
+
const prompt = createCompactionPrompt(projectMemories);
|
|
193
|
+
const success = injectHookMessage(summarizeCtx.sessionID, prompt, {
|
|
194
|
+
agent: summarizeCtx.agent,
|
|
195
|
+
model: { providerID: summarizeCtx.providerID, modelID: summarizeCtx.modelID },
|
|
196
|
+
path: { cwd: summarizeCtx.directory },
|
|
197
|
+
});
|
|
198
|
+
if (success) {
|
|
199
|
+
log("[compaction] context injected with project memories", {
|
|
200
|
+
sessionID: summarizeCtx.sessionID,
|
|
201
|
+
memoriesCount: projectMemories.length
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
async function saveSummaryAsMemory(sessionID, summaryContent) {
|
|
206
|
+
if (!summaryContent || summaryContent.length < 100) {
|
|
207
|
+
log("[compaction] summary too short to save", { sessionID, length: summaryContent.length });
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
try {
|
|
211
|
+
const result = await memoryClient.addMemory(`[Session Summary]\n${summaryContent}`, tags.project, { type: "conversation" });
|
|
212
|
+
if (result.success) {
|
|
213
|
+
log("[compaction] summary saved as memory", { sessionID, memoryId: result.id });
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
log("[compaction] failed to save summary", { error: result.error });
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
catch (err) {
|
|
220
|
+
log("[compaction] failed to save summary", { error: String(err) });
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
async function checkAndTriggerCompaction(sessionID, lastAssistant) {
|
|
224
|
+
if (state.compactionInProgress.has(sessionID))
|
|
225
|
+
return;
|
|
226
|
+
const lastCompaction = state.lastCompactionTime.get(sessionID) ?? 0;
|
|
227
|
+
if (Date.now() - lastCompaction < COMPACTION_COOLDOWN_MS)
|
|
228
|
+
return;
|
|
229
|
+
if (lastAssistant.summary === true)
|
|
230
|
+
return;
|
|
231
|
+
const tokens = lastAssistant.tokens;
|
|
232
|
+
if (!tokens)
|
|
233
|
+
return;
|
|
234
|
+
let modelID = lastAssistant.modelID ?? "";
|
|
235
|
+
let providerID = lastAssistant.providerID ?? "";
|
|
236
|
+
let agent;
|
|
237
|
+
// Fallback: find model/agent from stored messages if not available
|
|
238
|
+
const messageDir = getMessageDir(sessionID);
|
|
239
|
+
const storedMessage = messageDir ? findNearestMessageWithFields(messageDir) : null;
|
|
240
|
+
if (!providerID || !modelID) {
|
|
241
|
+
if (storedMessage?.model?.providerID)
|
|
242
|
+
providerID = storedMessage.model.providerID;
|
|
243
|
+
if (storedMessage?.model?.modelID)
|
|
244
|
+
modelID = storedMessage.model.modelID;
|
|
245
|
+
}
|
|
246
|
+
agent = storedMessage?.agent;
|
|
247
|
+
if (!isSupportedModel(modelID)) {
|
|
248
|
+
log("[compaction] skipping unsupported model", { modelID });
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
const configLimit = getModelLimit?.(providerID, modelID);
|
|
252
|
+
const contextLimit = configLimit ?? CLAUDE_DEFAULT_CONTEXT_LIMIT;
|
|
253
|
+
const totalUsed = tokens.input + tokens.cache.read + tokens.output;
|
|
254
|
+
if (totalUsed < MIN_TOKENS_FOR_COMPACTION)
|
|
255
|
+
return;
|
|
256
|
+
const usageRatio = totalUsed / contextLimit;
|
|
257
|
+
log("[compaction] checking", {
|
|
258
|
+
sessionID,
|
|
259
|
+
totalUsed,
|
|
260
|
+
contextLimit,
|
|
261
|
+
usageRatio: usageRatio.toFixed(2),
|
|
262
|
+
threshold,
|
|
263
|
+
});
|
|
264
|
+
if (usageRatio < threshold)
|
|
265
|
+
return;
|
|
266
|
+
state.compactionInProgress.add(sessionID);
|
|
267
|
+
state.lastCompactionTime.set(sessionID, Date.now());
|
|
268
|
+
if (!providerID || !modelID) {
|
|
269
|
+
state.compactionInProgress.delete(sessionID);
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
await ctx.client.tui.showToast({
|
|
273
|
+
body: {
|
|
274
|
+
title: "Preemptive Compaction",
|
|
275
|
+
message: `Context at ${(usageRatio * 100).toFixed(0)}% - compacting with memory context...`,
|
|
276
|
+
variant: "warning",
|
|
277
|
+
duration: 3000,
|
|
278
|
+
},
|
|
279
|
+
}).catch(() => { });
|
|
280
|
+
log("[compaction] triggering compaction", { sessionID, usageRatio });
|
|
281
|
+
try {
|
|
282
|
+
await injectCompactionContext({
|
|
283
|
+
sessionID,
|
|
284
|
+
providerID,
|
|
285
|
+
modelID,
|
|
286
|
+
usageRatio,
|
|
287
|
+
directory: ctx.directory,
|
|
288
|
+
agent,
|
|
289
|
+
});
|
|
290
|
+
state.summarizedSessions.add(sessionID);
|
|
291
|
+
await ctx.client.session.summarize({
|
|
292
|
+
path: { id: sessionID },
|
|
293
|
+
body: { providerID, modelID },
|
|
294
|
+
query: { directory: ctx.directory },
|
|
295
|
+
});
|
|
296
|
+
await ctx.client.tui.showToast({
|
|
297
|
+
body: {
|
|
298
|
+
title: "Compaction Complete",
|
|
299
|
+
message: "Session compacted with memory context. Resuming...",
|
|
300
|
+
variant: "success",
|
|
301
|
+
duration: 2000,
|
|
302
|
+
},
|
|
303
|
+
}).catch(() => { });
|
|
304
|
+
state.compactionInProgress.delete(sessionID);
|
|
305
|
+
setTimeout(async () => {
|
|
306
|
+
try {
|
|
307
|
+
const messageDir = getMessageDir(sessionID);
|
|
308
|
+
const storedMessage = messageDir ? findNearestMessageWithFields(messageDir) : null;
|
|
309
|
+
await ctx.client.session.promptAsync({
|
|
310
|
+
path: { id: sessionID },
|
|
311
|
+
body: {
|
|
312
|
+
agent: storedMessage?.agent,
|
|
313
|
+
parts: [{ type: "text", text: "Continue" }],
|
|
314
|
+
},
|
|
315
|
+
query: { directory: ctx.directory },
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
catch { }
|
|
319
|
+
}, 500);
|
|
320
|
+
}
|
|
321
|
+
catch (err) {
|
|
322
|
+
log("[compaction] compaction failed", { sessionID, error: String(err) });
|
|
323
|
+
state.compactionInProgress.delete(sessionID);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
async function handleSummaryMessage(sessionID, _messageInfo) {
|
|
327
|
+
log("[compaction] handleSummaryMessage called", { sessionID, inSet: state.summarizedSessions.has(sessionID) });
|
|
328
|
+
if (!state.summarizedSessions.has(sessionID))
|
|
329
|
+
return;
|
|
330
|
+
state.summarizedSessions.delete(sessionID);
|
|
331
|
+
log("[compaction] capturing summary for memory", { sessionID });
|
|
332
|
+
try {
|
|
333
|
+
const resp = await ctx.client.session.messages({
|
|
334
|
+
path: { id: sessionID },
|
|
335
|
+
query: { directory: ctx.directory },
|
|
336
|
+
});
|
|
337
|
+
const messages = (resp.data ?? resp);
|
|
338
|
+
const summaryMessage = messages.find(m => m.info.role === "assistant" &&
|
|
339
|
+
m.info.summary === true);
|
|
340
|
+
log("[compaction] looking for summary message", {
|
|
341
|
+
sessionID,
|
|
342
|
+
found: !!summaryMessage,
|
|
343
|
+
hasParts: !!summaryMessage?.parts
|
|
344
|
+
});
|
|
345
|
+
if (summaryMessage?.parts) {
|
|
346
|
+
const textParts = summaryMessage.parts.filter(p => p.type === "text" && p.text);
|
|
347
|
+
const summaryContent = textParts.map(p => p.text).join("\n");
|
|
348
|
+
log("[compaction] summary content", {
|
|
349
|
+
sessionID,
|
|
350
|
+
textPartsCount: textParts.length,
|
|
351
|
+
contentLength: summaryContent.length
|
|
352
|
+
});
|
|
353
|
+
if (summaryContent) {
|
|
354
|
+
await saveSummaryAsMemory(sessionID, summaryContent);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
catch (err) {
|
|
359
|
+
log("[compaction] failed to capture summary", { error: String(err) });
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
return {
|
|
363
|
+
compactionTracker: state.lastCompactionTime,
|
|
364
|
+
async event({ event }) {
|
|
365
|
+
const props = event.properties;
|
|
366
|
+
if (event.type === "session.deleted") {
|
|
367
|
+
const sessionInfo = props?.info;
|
|
368
|
+
if (sessionInfo?.id) {
|
|
369
|
+
state.lastCompactionTime.delete(sessionInfo.id);
|
|
370
|
+
state.compactionInProgress.delete(sessionInfo.id);
|
|
371
|
+
state.summarizedSessions.delete(sessionInfo.id);
|
|
372
|
+
}
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
if (event.type === "message.updated") {
|
|
376
|
+
const info = props?.info;
|
|
377
|
+
if (!info)
|
|
378
|
+
return;
|
|
379
|
+
const sessionID = info.sessionID;
|
|
380
|
+
if (!sessionID)
|
|
381
|
+
return;
|
|
382
|
+
if (info.role === "assistant" && info.summary === true && info.finish) {
|
|
383
|
+
await handleSummaryMessage(sessionID, info);
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
if (info.role !== "assistant" || !info.finish)
|
|
387
|
+
return;
|
|
388
|
+
await checkAndTriggerCompaction(sessionID, info);
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
if (event.type === "session.idle") {
|
|
392
|
+
const sessionID = props?.sessionID;
|
|
393
|
+
if (!sessionID)
|
|
394
|
+
return;
|
|
395
|
+
try {
|
|
396
|
+
const resp = await ctx.client.session.messages({
|
|
397
|
+
path: { id: sessionID },
|
|
398
|
+
query: { directory: ctx.directory },
|
|
399
|
+
});
|
|
400
|
+
const messages = (resp.data ?? resp);
|
|
401
|
+
const assistants = messages
|
|
402
|
+
.filter((m) => m.info.role === "assistant")
|
|
403
|
+
.map((m) => m.info);
|
|
404
|
+
if (assistants.length === 0)
|
|
405
|
+
return;
|
|
406
|
+
const lastAssistant = assistants[assistants.length - 1];
|
|
407
|
+
if (!lastAssistant.providerID || !lastAssistant.modelID) {
|
|
408
|
+
const messageDir = getMessageDir(sessionID);
|
|
409
|
+
const storedMessage = messageDir ? findNearestMessageWithFields(messageDir) : null;
|
|
410
|
+
if (storedMessage?.model?.providerID && storedMessage?.model?.modelID) {
|
|
411
|
+
lastAssistant.providerID = storedMessage.model.providerID;
|
|
412
|
+
lastAssistant.modelID = storedMessage.model.modelID;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
await checkAndTriggerCompaction(sessionID, lastAssistant);
|
|
416
|
+
}
|
|
417
|
+
catch { }
|
|
418
|
+
}
|
|
419
|
+
},
|
|
420
|
+
};
|
|
421
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
interface MemoryResultMinimal {
|
|
2
|
+
similarity: number;
|
|
3
|
+
memory?: string;
|
|
4
|
+
chunk?: string;
|
|
5
|
+
}
|
|
6
|
+
interface MemoriesResponseMinimal {
|
|
7
|
+
results?: MemoryResultMinimal[];
|
|
8
|
+
}
|
|
9
|
+
interface ProfileResponse {
|
|
10
|
+
profile?: {
|
|
11
|
+
static: string[];
|
|
12
|
+
dynamic: string[];
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export declare function formatContextForPrompt(profile: ProfileResponse | null, userMemories: MemoriesResponseMinimal, projectMemories: MemoriesResponseMinimal): string;
|
|
16
|
+
export {};
|
|
17
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/services/context.ts"],"names":[],"mappings":"AAEA,UAAU,mBAAmB;IAC3B,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,UAAU,uBAAuB;IAC/B,OAAO,CAAC,EAAE,mBAAmB,EAAE,CAAC;CACjC;AAED,UAAU,eAAe;IACvB,OAAO,CAAC,EAAE;QACR,MAAM,EAAE,MAAM,EAAE,CAAC;QACjB,OAAO,EAAE,MAAM,EAAE,CAAC;KACnB,CAAC;CACH;AAED,wBAAgB,sBAAsB,CACpC,OAAO,EAAE,eAAe,GAAG,IAAI,EAC/B,YAAY,EAAE,uBAAuB,EACrC,eAAe,EAAE,uBAAuB,GACvC,MAAM,CA8CR"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { CONFIG } from "../config.js";
|
|
2
|
+
export function formatContextForPrompt(profile, userMemories, projectMemories) {
|
|
3
|
+
const parts = ["[MEMORY]"];
|
|
4
|
+
if (CONFIG.injectProfile && profile?.profile) {
|
|
5
|
+
const { static: staticFacts, dynamic: dynamicFacts } = profile.profile;
|
|
6
|
+
if (staticFacts.length > 0) {
|
|
7
|
+
parts.push("\nUser Profile:");
|
|
8
|
+
staticFacts.slice(0, CONFIG.maxProfileItems).forEach((fact) => {
|
|
9
|
+
parts.push(`- ${fact}`);
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
if (dynamicFacts.length > 0) {
|
|
13
|
+
parts.push("\nRecent Context:");
|
|
14
|
+
dynamicFacts.slice(0, CONFIG.maxProfileItems).forEach((fact) => {
|
|
15
|
+
parts.push(`- ${fact}`);
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const projectResults = projectMemories.results || [];
|
|
20
|
+
if (projectResults.length > 0) {
|
|
21
|
+
parts.push("\nProject Knowledge:");
|
|
22
|
+
projectResults.forEach((mem) => {
|
|
23
|
+
const similarity = Math.round(mem.similarity * 100);
|
|
24
|
+
const content = mem.memory || mem.chunk || "";
|
|
25
|
+
parts.push(`- [${similarity}%] ${content}`);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
const userResults = userMemories.results || [];
|
|
29
|
+
if (userResults.length > 0) {
|
|
30
|
+
parts.push("\nRelevant Memories:");
|
|
31
|
+
userResults.forEach((mem) => {
|
|
32
|
+
const similarity = Math.round(mem.similarity * 100);
|
|
33
|
+
const content = mem.memory || mem.chunk || "";
|
|
34
|
+
parts.push(`- [${similarity}%] ${content}`);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
if (parts.length === 1) {
|
|
38
|
+
return "";
|
|
39
|
+
}
|
|
40
|
+
return parts.join("\n");
|
|
41
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
interface DuplicateGroup {
|
|
2
|
+
representative: {
|
|
3
|
+
id: string;
|
|
4
|
+
content: string;
|
|
5
|
+
containerTag: string;
|
|
6
|
+
createdAt: number;
|
|
7
|
+
};
|
|
8
|
+
duplicates: Array<{
|
|
9
|
+
id: string;
|
|
10
|
+
content: string;
|
|
11
|
+
similarity: number;
|
|
12
|
+
}>;
|
|
13
|
+
}
|
|
14
|
+
interface DeduplicationResult {
|
|
15
|
+
exactDuplicatesDeleted: number;
|
|
16
|
+
nearDuplicateGroups: DuplicateGroup[];
|
|
17
|
+
}
|
|
18
|
+
export declare class DeduplicationService {
|
|
19
|
+
private isRunning;
|
|
20
|
+
detectAndRemoveDuplicates(): Promise<DeduplicationResult>;
|
|
21
|
+
private cosineSimilarity;
|
|
22
|
+
getStatus(): {
|
|
23
|
+
enabled: boolean;
|
|
24
|
+
threshold: number;
|
|
25
|
+
isRunning: boolean;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
export declare const deduplicationService: DeduplicationService;
|
|
29
|
+
export {};
|
|
30
|
+
//# sourceMappingURL=deduplication-service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deduplication-service.d.ts","sourceRoot":"","sources":["../../src/services/deduplication-service.ts"],"names":[],"mappings":"AAOA,UAAU,cAAc;IACtB,cAAc,EAAE;QACd,EAAE,EAAE,MAAM,CAAC;QACX,OAAO,EAAE,MAAM,CAAC;QAChB,YAAY,EAAE,MAAM,CAAC;QACrB,SAAS,EAAE,MAAM,CAAC;KACnB,CAAC;IACF,UAAU,EAAE,KAAK,CAAC;QAChB,EAAE,EAAE,MAAM,CAAC;QACX,OAAO,EAAE,MAAM,CAAC;QAChB,UAAU,EAAE,MAAM,CAAC;KACpB,CAAC,CAAC;CACJ;AAED,UAAU,mBAAmB;IAC3B,sBAAsB,EAAE,MAAM,CAAC;IAC/B,mBAAmB,EAAE,cAAc,EAAE,CAAC;CACvC;AAED,qBAAa,oBAAoB;IAC/B,OAAO,CAAC,SAAS,CAAkB;IAE7B,yBAAyB,IAAI,OAAO,CAAC,mBAAmB,CAAC;IAgH/D,OAAO,CAAC,gBAAgB;IAoBxB,SAAS;;;;;CAOV;AAED,eAAO,MAAM,oBAAoB,sBAA6B,CAAC"}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { embeddingService } from "./embedding.js";
|
|
2
|
+
import { shardManager } from "./sqlite/shard-manager.js";
|
|
3
|
+
import { vectorSearch } from "./sqlite/vector-search.js";
|
|
4
|
+
import { connectionManager } from "./sqlite/connection-manager.js";
|
|
5
|
+
import { CONFIG } from "../config.js";
|
|
6
|
+
import { log } from "./logger.js";
|
|
7
|
+
export class DeduplicationService {
|
|
8
|
+
isRunning = false;
|
|
9
|
+
async detectAndRemoveDuplicates() {
|
|
10
|
+
if (this.isRunning) {
|
|
11
|
+
throw new Error("Deduplication already running");
|
|
12
|
+
}
|
|
13
|
+
if (!CONFIG.deduplicationEnabled) {
|
|
14
|
+
throw new Error("Deduplication is disabled in config");
|
|
15
|
+
}
|
|
16
|
+
this.isRunning = true;
|
|
17
|
+
try {
|
|
18
|
+
log("Deduplication: starting", {
|
|
19
|
+
threshold: CONFIG.deduplicationSimilarityThreshold,
|
|
20
|
+
});
|
|
21
|
+
const userShards = shardManager.getAllShards("user", "");
|
|
22
|
+
const projectShards = shardManager.getAllShards("project", "");
|
|
23
|
+
const allShards = [...userShards, ...projectShards];
|
|
24
|
+
let exactDeleted = 0;
|
|
25
|
+
const nearDuplicateGroups = [];
|
|
26
|
+
for (const shard of allShards) {
|
|
27
|
+
const db = connectionManager.getConnection(shard.dbPath);
|
|
28
|
+
const memories = vectorSearch.getAllMemories(db);
|
|
29
|
+
const contentMap = new Map();
|
|
30
|
+
for (const memory of memories) {
|
|
31
|
+
const key = `${memory.container_tag}:${memory.content}`;
|
|
32
|
+
if (!contentMap.has(key)) {
|
|
33
|
+
contentMap.set(key, []);
|
|
34
|
+
}
|
|
35
|
+
contentMap.get(key).push(memory);
|
|
36
|
+
}
|
|
37
|
+
for (const [key, duplicates] of contentMap) {
|
|
38
|
+
if (duplicates.length > 1) {
|
|
39
|
+
duplicates.sort((a, b) => Number(b.created_at) - Number(a.created_at));
|
|
40
|
+
const keep = duplicates[0];
|
|
41
|
+
const toDelete = duplicates.slice(1);
|
|
42
|
+
for (const dup of toDelete) {
|
|
43
|
+
try {
|
|
44
|
+
vectorSearch.deleteVector(db, dup.id);
|
|
45
|
+
shardManager.decrementVectorCount(shard.id);
|
|
46
|
+
exactDeleted++;
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
log("Deduplication: delete error", {
|
|
50
|
+
memoryId: dup.id,
|
|
51
|
+
error: String(error),
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const uniqueMemories = Array.from(contentMap.values()).map((arr) => arr[0]);
|
|
58
|
+
for (let i = 0; i < uniqueMemories.length; i++) {
|
|
59
|
+
const mem1 = uniqueMemories[i];
|
|
60
|
+
if (!mem1.vector)
|
|
61
|
+
continue;
|
|
62
|
+
const vector1 = new Float32Array(new Uint8Array(mem1.vector).buffer);
|
|
63
|
+
const similarGroup = {
|
|
64
|
+
representative: {
|
|
65
|
+
id: mem1.id,
|
|
66
|
+
content: mem1.content,
|
|
67
|
+
containerTag: mem1.container_tag,
|
|
68
|
+
createdAt: mem1.created_at,
|
|
69
|
+
},
|
|
70
|
+
duplicates: [],
|
|
71
|
+
};
|
|
72
|
+
for (let j = i + 1; j < uniqueMemories.length; j++) {
|
|
73
|
+
const mem2 = uniqueMemories[j];
|
|
74
|
+
if (!mem2.vector)
|
|
75
|
+
continue;
|
|
76
|
+
if (mem1.container_tag !== mem2.container_tag)
|
|
77
|
+
continue;
|
|
78
|
+
const vector2 = new Float32Array(new Uint8Array(mem2.vector).buffer);
|
|
79
|
+
const similarity = this.cosineSimilarity(vector1, vector2);
|
|
80
|
+
if (similarity >= CONFIG.deduplicationSimilarityThreshold && similarity < 1.0) {
|
|
81
|
+
similarGroup.duplicates.push({
|
|
82
|
+
id: mem2.id,
|
|
83
|
+
content: mem2.content,
|
|
84
|
+
similarity,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (similarGroup.duplicates.length > 0) {
|
|
89
|
+
nearDuplicateGroups.push(similarGroup);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
log("Deduplication: completed", {
|
|
94
|
+
exactDeleted,
|
|
95
|
+
nearDuplicateGroupsFound: nearDuplicateGroups.length,
|
|
96
|
+
});
|
|
97
|
+
return {
|
|
98
|
+
exactDuplicatesDeleted: exactDeleted,
|
|
99
|
+
nearDuplicateGroups,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
finally {
|
|
103
|
+
this.isRunning = false;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
cosineSimilarity(a, b) {
|
|
107
|
+
if (a.length !== b.length)
|
|
108
|
+
return 0;
|
|
109
|
+
let dotProduct = 0;
|
|
110
|
+
let normA = 0;
|
|
111
|
+
let normB = 0;
|
|
112
|
+
for (let i = 0; i < a.length; i++) {
|
|
113
|
+
const aVal = a[i] || 0;
|
|
114
|
+
const bVal = b[i] || 0;
|
|
115
|
+
dotProduct += aVal * bVal;
|
|
116
|
+
normA += aVal * aVal;
|
|
117
|
+
normB += bVal * bVal;
|
|
118
|
+
}
|
|
119
|
+
if (normA === 0 || normB === 0)
|
|
120
|
+
return 0;
|
|
121
|
+
return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
|
|
122
|
+
}
|
|
123
|
+
getStatus() {
|
|
124
|
+
return {
|
|
125
|
+
enabled: CONFIG.deduplicationEnabled,
|
|
126
|
+
threshold: CONFIG.deduplicationSimilarityThreshold,
|
|
127
|
+
isRunning: this.isRunning,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
export const deduplicationService = new DeduplicationService();
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare class EmbeddingService {
|
|
2
|
+
private pipe;
|
|
3
|
+
private initPromise;
|
|
4
|
+
isWarmedUp: boolean;
|
|
5
|
+
warmup(progressCallback?: (progress: any) => void): Promise<void>;
|
|
6
|
+
embed(text: string): Promise<Float32Array>;
|
|
7
|
+
embedWithTimeout(text: string): Promise<Float32Array>;
|
|
8
|
+
}
|
|
9
|
+
export declare const embeddingService: EmbeddingService;
|
|
10
|
+
//# sourceMappingURL=embedding.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"embedding.d.ts","sourceRoot":"","sources":["../../src/services/embedding.ts"],"names":[],"mappings":"AAiBA,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,IAAI,CAAa;IACzB,OAAO,CAAC,WAAW,CAA8B;IAC1C,UAAU,EAAE,OAAO,CAAS;IAE7B,MAAM,CAAC,gBAAgB,CAAC,EAAE,CAAC,QAAQ,EAAE,GAAG,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IA8BjE,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;IAkC1C,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;CAG5D;AAED,eAAO,MAAM,gBAAgB,kBAAyB,CAAC"}
|