Skip to content

Commit 0a90f57

Browse files
committed
Add fallback to use stored message model info when session.idle event lacks providerID/modelID
Adds getMessageDir() helper function and fallback logic in the session.idle event handler to retrieve stored model information (providerID/modelID) when the API response lacks these fields. This mirrors the approach used in todo-continuation-enforcer hook to ensure preemptive compaction can proceed even when model info is missing from the initial response. 🤖 GENERATED WITH ASSISTANCE OF [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
1 parent 73c0db7 commit 0a90f57

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/hooks/preemptive-compaction/index.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { existsSync, readdirSync } from "node:fs"
2+
import { join } from "node:path"
13
import type { PluginInput } from "@opencode-ai/plugin"
24
import type { ExperimentalConfig } from "../../config"
35
import type { PreemptiveCompactionState, TokenInfo } from "./types"
@@ -6,6 +8,10 @@ import {
68
MIN_TOKENS_FOR_COMPACTION,
79
COMPACTION_COOLDOWN_MS,
810
} from "./constants"
11+
import {
12+
findNearestMessageWithFields,
13+
MESSAGE_STORAGE,
14+
} from "../../features/hook-message-injector"
915
import { log } from "../../shared/logger"
1016

1117
export interface SummarizeContext {
@@ -48,6 +54,20 @@ function isSupportedModel(modelID: string): boolean {
4854
return CLAUDE_MODEL_PATTERN.test(modelID)
4955
}
5056

57+
function getMessageDir(sessionID: string): string | null {
58+
if (!existsSync(MESSAGE_STORAGE)) return null
59+
60+
const directPath = join(MESSAGE_STORAGE, sessionID)
61+
if (existsSync(directPath)) return directPath
62+
63+
for (const dir of readdirSync(MESSAGE_STORAGE)) {
64+
const sessionPath = join(MESSAGE_STORAGE, dir, sessionID)
65+
if (existsSync(sessionPath)) return sessionPath
66+
}
67+
68+
return null
69+
}
70+
5171
function createState(): PreemptiveCompactionState {
5272
return {
5373
lastCompactionTime: new Map(),
@@ -222,6 +242,21 @@ export function createPreemptiveCompactionHook(
222242
if (assistants.length === 0) return
223243

224244
const lastAssistant = assistants[assistants.length - 1]
245+
246+
if (!lastAssistant.providerID || !lastAssistant.modelID) {
247+
const messageDir = getMessageDir(sessionID)
248+
const storedMessage = messageDir ? findNearestMessageWithFields(messageDir) : null
249+
if (storedMessage?.model?.providerID && storedMessage?.model?.modelID) {
250+
lastAssistant.providerID = storedMessage.model.providerID
251+
lastAssistant.modelID = storedMessage.model.modelID
252+
log("[preemptive-compaction] using stored message model info", {
253+
sessionID,
254+
providerID: lastAssistant.providerID,
255+
modelID: lastAssistant.modelID,
256+
})
257+
}
258+
}
259+
225260
await checkAndTriggerCompaction(sessionID, lastAssistant)
226261
} catch {}
227262
}

0 commit comments

Comments
 (0)