Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```

Expand Down
11 changes: 11 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface SupermemoryConfig {
containerTagPrefix?: string;
filterPrompt?: string;
keywordPatterns?: string[];
compactionThreshold?: number;
}

const DEFAULT_KEYWORD_PATTERNS = [
Expand Down Expand Up @@ -49,6 +50,7 @@ const DEFAULTS: Required<Omit<SupermemoryConfig, "apiKey">> = {
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 {
Expand All @@ -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)) {
Expand Down Expand Up @@ -91,6 +101,7 @@ export const CONFIG = {
...DEFAULT_KEYWORD_PATTERNS,
...(fileConfig.keywordPatterns ?? []).filter(isValidRegex),
],
compactionThreshold: validateCompactionThreshold(fileConfig.compactionThreshold),
};

export function isConfigured(): boolean {
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down