Skip to content

Commit 5b9d105

Browse files
Sanyue0v0Sanyue
andauthored
feat(config): add configurable compaction threshold (#18)
* feat(config): add configurable compaction threshold Add compactionThreshold config option (default 0.80) to control when context compaction triggers. Previously hardcoded at 80%, now users can adjust via supermemory.jsonc. - Add compactionThreshold to SupermemoryConfig interface - Add default value 0.80 in DEFAULTS - Export compactionThreshold in CONFIG - Pass threshold to createCompactionHook - Update README with config documentation * fix(config): validate compactionThreshold to ensure 0-1 range Prevents silent failures when users enter invalid values like 80 instead of 0.80, which would disable compaction and risk context overflow. --------- Co-authored-by: Sanyue <sanyue@ciallo.cafe>
1 parent ea0ac61 commit 5b9d105

3 files changed

Lines changed: 18 additions & 2 deletions

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,10 @@ Create `~/.config/opencode/supermemory.jsonc`:
219219
"containerTagPrefix": "opencode",
220220

221221
// Extra keyword patterns for memory detection (regex)
222-
"keywordPatterns": ["log\\s+this", "write\\s+down"]
222+
"keywordPatterns": ["log\\s+this", "write\\s+down"],
223+
224+
// Context usage ratio that triggers compaction (0-1)
225+
"compactionThreshold": 0.80
223226
}
224227
```
225228

src/config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ interface SupermemoryConfig {
1919
containerTagPrefix?: string;
2020
filterPrompt?: string;
2121
keywordPatterns?: string[];
22+
compactionThreshold?: number;
2223
}
2324

2425
const DEFAULT_KEYWORD_PATTERNS = [
@@ -49,6 +50,7 @@ const DEFAULTS: Required<Omit<SupermemoryConfig, "apiKey">> = {
4950
containerTagPrefix: "opencode",
5051
filterPrompt: "You are a stateful coding agent. Remember all the information, including but not limited to user's coding preferences, tech stack, behaviours, workflows, and any other relevant details.",
5152
keywordPatterns: [],
53+
compactionThreshold: 0.80,
5254
};
5355

5456
function isValidRegex(pattern: string): boolean {
@@ -60,6 +62,14 @@ function isValidRegex(pattern: string): boolean {
6062
}
6163
}
6264

65+
function validateCompactionThreshold(value: number | undefined): number {
66+
if (value === undefined || typeof value !== 'number' || isNaN(value)) {
67+
return DEFAULTS.compactionThreshold;
68+
}
69+
if (value <= 0 || value > 1) return DEFAULTS.compactionThreshold;
70+
return value;
71+
}
72+
6373
function loadConfig(): SupermemoryConfig {
6474
for (const path of CONFIG_FILES) {
6575
if (existsSync(path)) {
@@ -91,6 +101,7 @@ export const CONFIG = {
91101
...DEFAULT_KEYWORD_PATTERNS,
92102
...(fileConfig.keywordPatterns ?? []).filter(isValidRegex),
93103
],
104+
compactionThreshold: validateCompactionThreshold(fileConfig.compactionThreshold),
94105
};
95106

96107
export function isConfigured(): boolean {

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ export const SupermemoryPlugin: Plugin = async (ctx: PluginInput) => {
4747
}
4848

4949
const compactionHook = isConfigured() && ctx.client
50-
? createCompactionHook(ctx as CompactionContext, tags)
50+
? createCompactionHook(ctx as CompactionContext, tags, {
51+
threshold: CONFIG.compactionThreshold,
52+
})
5153
: null;
5254

5355
return {

0 commit comments

Comments
 (0)