2026-02-08 13:57:26 +09:00
|
|
|
import { detectThinkKeyword, extractPromptText } from "./detector"
|
2026-03-08 09:12:01 -06:00
|
|
|
import { isAlreadyHighVariant } from "./switcher"
|
2026-02-26 12:00:45 +09:00
|
|
|
import type { ThinkModeState } from "./types"
|
2026-02-08 13:57:26 +09:00
|
|
|
import { log } from "../../shared"
|
|
|
|
|
|
|
|
|
|
const thinkModeState = new Map<string, ThinkModeState>()
|
|
|
|
|
|
|
|
|
|
export function clearThinkModeState(sessionID: string): void {
|
|
|
|
|
thinkModeState.delete(sessionID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function createThinkModeHook() {
|
|
|
|
|
return {
|
2026-02-26 12:00:45 +09:00
|
|
|
"chat.message": async (
|
|
|
|
|
input: {
|
|
|
|
|
sessionID: string
|
|
|
|
|
model?: { providerID: string; modelID: string }
|
|
|
|
|
},
|
|
|
|
|
output: {
|
|
|
|
|
message: Record<string, unknown>
|
|
|
|
|
parts: Array<{ type: string; text?: string; [key: string]: unknown }>
|
|
|
|
|
}
|
|
|
|
|
): Promise<void> => {
|
2026-02-08 13:57:26 +09:00
|
|
|
const promptText = extractPromptText(output.parts)
|
2026-02-26 12:00:45 +09:00
|
|
|
const sessionID = input.sessionID
|
2026-02-08 13:57:26 +09:00
|
|
|
|
|
|
|
|
const state: ThinkModeState = {
|
|
|
|
|
requested: false,
|
|
|
|
|
modelSwitched: false,
|
2026-02-26 12:00:45 +09:00
|
|
|
variantSet: false,
|
2026-02-08 13:57:26 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!detectThinkKeyword(promptText)) {
|
|
|
|
|
thinkModeState.set(sessionID, state)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state.requested = true
|
|
|
|
|
|
2026-02-26 12:00:45 +09:00
|
|
|
if (typeof output.message.variant === "string") {
|
|
|
|
|
thinkModeState.set(sessionID, state)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const currentModel = input.model
|
2026-02-08 13:57:26 +09:00
|
|
|
if (!currentModel) {
|
|
|
|
|
thinkModeState.set(sessionID, state)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state.providerID = currentModel.providerID
|
|
|
|
|
state.modelID = currentModel.modelID
|
|
|
|
|
|
|
|
|
|
if (isAlreadyHighVariant(currentModel.modelID)) {
|
|
|
|
|
thinkModeState.set(sessionID, state)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-08 09:12:01 -06:00
|
|
|
output.message.variant = "high"
|
|
|
|
|
state.modelSwitched = false
|
|
|
|
|
state.variantSet = true
|
|
|
|
|
log("Think mode: variant set to high", { sessionID })
|
2026-02-08 13:57:26 +09:00
|
|
|
|
|
|
|
|
thinkModeState.set(sessionID, state)
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
event: async ({ event }: { event: { type: string; properties?: unknown } }) => {
|
|
|
|
|
if (event.type === "session.deleted") {
|
|
|
|
|
const props = event.properties as { info?: { id?: string } } | undefined
|
|
|
|
|
if (props?.info?.id) {
|
|
|
|
|
thinkModeState.delete(props.info.id)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|