Skip to content

Commit 8ddfc93

Browse files
authored
make compaction work for every model (#19)
1 parent 5b9d105 commit 8ddfc93

2 files changed

Lines changed: 30 additions & 12 deletions

File tree

src/index.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,37 @@ export const SupermemoryPlugin: Plugin = async (ctx: PluginInput) => {
4646
log("Plugin disabled - SUPERMEMORY_API_KEY not set");
4747
}
4848

49+
// Fetch model limits once at plugin init
50+
const modelLimits = new Map<string, number>();
51+
52+
(async () => {
53+
try {
54+
const response = await ctx.client.provider.list();
55+
if (response.data?.all) {
56+
for (const provider of response.data.all) {
57+
if (provider.models) {
58+
for (const [modelId, model] of Object.entries(provider.models)) {
59+
if (model.limit?.context) {
60+
modelLimits.set(`${provider.id}/${modelId}`, model.limit.context);
61+
}
62+
}
63+
}
64+
}
65+
}
66+
log("Model limits loaded", { count: modelLimits.size });
67+
} catch (error) {
68+
log("Failed to fetch model limits", { error: String(error) });
69+
}
70+
})();
71+
72+
const getModelLimit = (providerID: string, modelID: string): number | undefined => {
73+
return modelLimits.get(`${providerID}/${modelID}`);
74+
};
75+
4976
const compactionHook = isConfigured() && ctx.client
5077
? createCompactionHook(ctx as CompactionContext, tags, {
5178
threshold: CONFIG.compactionThreshold,
79+
getModelLimit,
5280
})
5381
: null;
5482

src/services/compaction.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ const PART_STORAGE = join(homedir(), ".opencode", "parts");
1111
const DEFAULT_THRESHOLD = 0.80;
1212
const MIN_TOKENS_FOR_COMPACTION = 50_000;
1313
const COMPACTION_COOLDOWN_MS = 30_000;
14-
const CLAUDE_DEFAULT_CONTEXT_LIMIT = 200_000;
15-
const CLAUDE_MODEL_PATTERN = /claude-(opus|sonnet|haiku)/i;
14+
const DEFAULT_CONTEXT_LIMIT = 200_000;
1615

1716
interface CompactionState {
1817
lastCompactionTime: Map<string, number>;
@@ -98,10 +97,6 @@ This context is critical for maintaining continuity after compaction.
9897
`;
9998
}
10099

101-
function isSupportedModel(modelID: string): boolean {
102-
return CLAUDE_MODEL_PATTERN.test(modelID);
103-
}
104-
105100
function getMessageDir(sessionID: string): string | null {
106101
if (!existsSync(MESSAGE_STORAGE)) return null;
107102

@@ -344,13 +339,8 @@ export function createCompactionHook(
344339
}
345340
agent = storedMessage?.agent;
346341

347-
if (!isSupportedModel(modelID)) {
348-
log("[compaction] skipping unsupported model", { modelID });
349-
return;
350-
}
351-
352342
const configLimit = getModelLimit?.(providerID, modelID);
353-
const contextLimit = configLimit ?? CLAUDE_DEFAULT_CONTEXT_LIMIT;
343+
const contextLimit = configLimit ?? DEFAULT_CONTEXT_LIMIT;
354344
const totalUsed = tokens.input + tokens.cache.read + tokens.output;
355345

356346
if (totalUsed < MIN_TOKENS_FOR_COMPACTION) return;

0 commit comments

Comments
 (0)