Fix model fallback across main/background/sync agents
This commit is contained in:
@@ -2,6 +2,7 @@ import type { OhMyOpenCodeConfig } from "../config"
|
||||
import type { PluginContext } from "./types"
|
||||
|
||||
import { hasConnectedProvidersCache } from "../shared"
|
||||
import { setSessionModel } from "../shared/session-model-state"
|
||||
import { setSessionAgent } from "../features/claude-code-session-state"
|
||||
import { applyUltraworkModelOverrideOnMessage } from "./ultrawork-model-override"
|
||||
|
||||
@@ -13,7 +14,12 @@ type FirstMessageVariantGate = {
|
||||
}
|
||||
|
||||
type ChatMessagePart = { type: string; text?: string; [key: string]: unknown }
|
||||
type ChatMessageHandlerOutput = { message: Record<string, unknown>; parts: ChatMessagePart[] }
|
||||
export type ChatMessageHandlerOutput = { message: Record<string, unknown>; parts: ChatMessagePart[] }
|
||||
export type ChatMessageInput = {
|
||||
sessionID: string
|
||||
agent?: string
|
||||
model?: { providerID: string; modelID: string }
|
||||
}
|
||||
type StartWorkHookOutput = { parts: Array<{ type: string; text?: string }> }
|
||||
|
||||
function isStartWorkHookOutput(value: unknown): value is StartWorkHookOutput {
|
||||
@@ -34,13 +40,13 @@ export function createChatMessageHandler(args: {
|
||||
firstMessageVariantGate: FirstMessageVariantGate
|
||||
hooks: CreatedHooks
|
||||
}): (
|
||||
input: { sessionID: string; agent?: string; model?: { providerID: string; modelID: string } },
|
||||
input: ChatMessageInput,
|
||||
output: ChatMessageHandlerOutput
|
||||
) => Promise<void> {
|
||||
const { ctx, pluginConfig, firstMessageVariantGate, hooks } = args
|
||||
|
||||
return async (
|
||||
input: { sessionID: string; agent?: string; model?: { providerID: string; modelID: string } },
|
||||
input: ChatMessageInput,
|
||||
output: ChatMessageHandlerOutput
|
||||
): Promise<void> => {
|
||||
if (input.agent) {
|
||||
@@ -53,6 +59,22 @@ export function createChatMessageHandler(args: {
|
||||
firstMessageVariantGate.markApplied(input.sessionID)
|
||||
}
|
||||
|
||||
await hooks.modelFallback?.["chat.message"]?.(input, output)
|
||||
const modelOverride = output.message["model"]
|
||||
if (
|
||||
modelOverride &&
|
||||
typeof modelOverride === "object" &&
|
||||
"providerID" in modelOverride &&
|
||||
"modelID" in modelOverride
|
||||
) {
|
||||
const providerID = (modelOverride as { providerID?: string }).providerID
|
||||
const modelID = (modelOverride as { modelID?: string }).modelID
|
||||
if (typeof providerID === "string" && typeof modelID === "string") {
|
||||
setSessionModel(input.sessionID, { providerID, modelID })
|
||||
}
|
||||
} else if (input.model) {
|
||||
setSessionModel(input.sessionID, input.model)
|
||||
}
|
||||
await hooks.stopContinuationGuard?.["chat.message"]?.(input)
|
||||
await hooks.keywordDetector?.["chat.message"]?.(input, output)
|
||||
await hooks.claudeCodeHooks?.["chat.message"]?.(input, output)
|
||||
|
||||
Reference in New Issue
Block a user