diff --git a/README.md b/README.md index 8cde4bc..edc3199 100644 --- a/README.md +++ b/README.md @@ -219,7 +219,10 @@ Create `~/.config/opencode/supermemory.jsonc`: "containerTagPrefix": "opencode", // Extra keyword patterns for memory detection (regex) - "keywordPatterns": ["log\\s+this", "write\\s+down"] + "keywordPatterns": ["log\\s+this", "write\\s+down"], + + // Context usage ratio that triggers compaction (0-1) + "compactionThreshold": 0.80 } ``` diff --git a/src/config.ts b/src/config.ts index 2a4351e..e9c98bb 100644 --- a/src/config.ts +++ b/src/config.ts @@ -19,6 +19,7 @@ interface SupermemoryConfig { containerTagPrefix?: string; filterPrompt?: string; keywordPatterns?: string[]; + compactionThreshold?: number; } const DEFAULT_KEYWORD_PATTERNS = [ @@ -49,6 +50,7 @@ const DEFAULTS: Required> = { containerTagPrefix: "opencode", 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.", keywordPatterns: [], + compactionThreshold: 0.80, }; function isValidRegex(pattern: string): boolean { @@ -60,6 +62,14 @@ function isValidRegex(pattern: string): boolean { } } +function validateCompactionThreshold(value: number | undefined): number { + if (value === undefined || typeof value !== 'number' || isNaN(value)) { + return DEFAULTS.compactionThreshold; + } + if (value <= 0 || value > 1) return DEFAULTS.compactionThreshold; + return value; +} + function loadConfig(): SupermemoryConfig { for (const path of CONFIG_FILES) { if (existsSync(path)) { @@ -91,6 +101,7 @@ export const CONFIG = { ...DEFAULT_KEYWORD_PATTERNS, ...(fileConfig.keywordPatterns ?? []).filter(isValidRegex), ], + compactionThreshold: validateCompactionThreshold(fileConfig.compactionThreshold), }; export function isConfigured(): boolean { diff --git a/src/index.ts b/src/index.ts index fc844f7..b6ff342 100644 --- a/src/index.ts +++ b/src/index.ts @@ -47,7 +47,9 @@ export const SupermemoryPlugin: Plugin = async (ctx: PluginInput) => { } const compactionHook = isConfigured() && ctx.client - ? createCompactionHook(ctx as CompactionContext, tags) + ? createCompactionHook(ctx as CompactionContext, tags, { + threshold: CONFIG.compactionThreshold, + }) : null; return {