2026-02-19 04:41:00 +02:00
|
|
|
import type { FallbackEntry } from "../../shared/model-requirements"
|
|
|
|
|
import type { ChatMessageInput, ChatMessageHandlerOutput } from "../../plugin/chat-message"
|
2026-04-03 21:37:16 +09:00
|
|
|
import { applyFallbackToChatMessage } from "./chat-message-fallback-handler"
|
2026-04-18 01:53:03 +09:00
|
|
|
import {
|
|
|
|
|
createModelFallbackStateController,
|
|
|
|
|
type ModelFallbackStateController,
|
|
|
|
|
} from "./fallback-state-controller"
|
2026-02-19 04:41:00 +02:00
|
|
|
|
|
|
|
|
type FallbackToast = (input: {
|
|
|
|
|
title: string
|
|
|
|
|
message: string
|
|
|
|
|
variant?: "info" | "success" | "warning" | "error"
|
|
|
|
|
duration?: number
|
|
|
|
|
}) => void | Promise<void>
|
|
|
|
|
|
|
|
|
|
type FallbackCallback = (input: {
|
|
|
|
|
sessionID: string
|
|
|
|
|
providerID: string
|
|
|
|
|
modelID: string
|
|
|
|
|
variant?: string
|
|
|
|
|
}) => void | Promise<void>
|
|
|
|
|
|
|
|
|
|
export type ModelFallbackState = {
|
|
|
|
|
providerID: string
|
|
|
|
|
modelID: string
|
|
|
|
|
fallbackChain: FallbackEntry[]
|
|
|
|
|
attemptCount: number
|
|
|
|
|
pending: boolean
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-18 01:53:03 +09:00
|
|
|
const modelFallbackControllerRef: { current?: ModelFallbackStateController } = {}
|
2026-02-20 00:02:17 +02:00
|
|
|
|
2026-04-18 01:53:03 +09:00
|
|
|
function getOrCreateModelFallbackController(): ModelFallbackStateController {
|
|
|
|
|
if (!modelFallbackControllerRef.current) {
|
|
|
|
|
createModelFallbackHook()
|
2026-04-07 11:20:42 +09:00
|
|
|
}
|
2026-04-18 01:53:03 +09:00
|
|
|
|
|
|
|
|
const controller = modelFallbackControllerRef.current
|
|
|
|
|
if (!controller) {
|
|
|
|
|
throw new Error("Model fallback controller should be initialized")
|
2026-02-20 00:02:17 +02:00
|
|
|
}
|
2026-04-18 01:53:03 +09:00
|
|
|
return controller
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function setSessionFallbackChain(sessionID: string, fallbackChain: FallbackEntry[] | undefined): void {
|
|
|
|
|
getOrCreateModelFallbackController().setSessionFallbackChain(sessionID, fallbackChain)
|
2026-02-20 00:02:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function clearSessionFallbackChain(sessionID: string): void {
|
2026-04-18 01:53:03 +09:00
|
|
|
getOrCreateModelFallbackController().clearSessionFallbackChain(sessionID)
|
2026-02-20 00:02:17 +02:00
|
|
|
}
|
2026-02-19 04:41:00 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets a pending model fallback for a session.
|
|
|
|
|
* Called when a model error is detected in session.error handler.
|
|
|
|
|
*/
|
|
|
|
|
export function setPendingModelFallback(
|
|
|
|
|
sessionID: string,
|
|
|
|
|
agentName: string,
|
|
|
|
|
currentProviderID: string,
|
|
|
|
|
currentModelID: string,
|
|
|
|
|
): boolean {
|
2026-04-18 01:53:03 +09:00
|
|
|
return getOrCreateModelFallbackController().setPendingModelFallback(
|
|
|
|
|
sessionID,
|
|
|
|
|
agentName,
|
|
|
|
|
currentProviderID,
|
|
|
|
|
currentModelID,
|
|
|
|
|
)
|
2026-02-19 04:41:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the next fallback model for a session.
|
|
|
|
|
* Increments attemptCount each time called.
|
|
|
|
|
*/
|
|
|
|
|
export function getNextFallback(
|
|
|
|
|
sessionID: string,
|
|
|
|
|
): { providerID: string; modelID: string; variant?: string } | null {
|
2026-04-18 01:53:03 +09:00
|
|
|
return getOrCreateModelFallbackController().getNextFallback(sessionID)
|
2026-02-19 04:41:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clears the pending fallback for a session.
|
|
|
|
|
* Called after fallback is successfully applied.
|
|
|
|
|
*/
|
|
|
|
|
export function clearPendingModelFallback(sessionID: string): void {
|
2026-04-18 01:53:03 +09:00
|
|
|
getOrCreateModelFallbackController().clearPendingModelFallback(sessionID)
|
2026-02-19 04:41:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks if there's a pending fallback for a session.
|
|
|
|
|
*/
|
|
|
|
|
export function hasPendingModelFallback(sessionID: string): boolean {
|
2026-04-18 01:53:03 +09:00
|
|
|
return getOrCreateModelFallbackController().hasPendingModelFallback(sessionID)
|
2026-02-19 04:41:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the current fallback state for a session (for debugging).
|
|
|
|
|
*/
|
|
|
|
|
export function getFallbackState(sessionID: string): ModelFallbackState | undefined {
|
2026-04-18 01:53:03 +09:00
|
|
|
return getOrCreateModelFallbackController().getFallbackState(sessionID)
|
2026-02-19 04:41:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a chat.message hook that applies model fallbacks when pending.
|
|
|
|
|
*/
|
|
|
|
|
export function createModelFallbackHook(args?: { toast?: FallbackToast; onApplied?: FallbackCallback }) {
|
2026-04-18 01:53:03 +09:00
|
|
|
if (!modelFallbackControllerRef.current) {
|
|
|
|
|
const pendingModelFallbacks = new Map<string, ModelFallbackState>()
|
|
|
|
|
const lastToastKey = new Map<string, string>()
|
|
|
|
|
const sessionFallbackChains = new Map<string, FallbackEntry[]>()
|
|
|
|
|
|
|
|
|
|
modelFallbackControllerRef.current = createModelFallbackStateController({
|
|
|
|
|
pendingModelFallbacks,
|
|
|
|
|
lastToastKey,
|
|
|
|
|
sessionFallbackChains,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const controller = getOrCreateModelFallbackController()
|
2026-02-19 04:41:00 +02:00
|
|
|
const toast = args?.toast
|
|
|
|
|
const onApplied = args?.onApplied
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
"chat.message": async (
|
|
|
|
|
input: ChatMessageInput,
|
|
|
|
|
output: ChatMessageHandlerOutput,
|
|
|
|
|
): Promise<void> => {
|
|
|
|
|
const { sessionID } = input
|
|
|
|
|
if (!sessionID) return
|
|
|
|
|
|
|
|
|
|
const fallback = getNextFallback(sessionID)
|
|
|
|
|
if (!fallback) return
|
|
|
|
|
|
2026-04-03 21:37:16 +09:00
|
|
|
await applyFallbackToChatMessage({
|
|
|
|
|
input,
|
|
|
|
|
output,
|
|
|
|
|
fallback,
|
|
|
|
|
toast,
|
|
|
|
|
onApplied,
|
2026-04-18 01:53:03 +09:00
|
|
|
lastToastKey: controller.lastToastKey,
|
2026-04-03 21:37:16 +09:00
|
|
|
})
|
2026-02-19 04:41:00 +02:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-26 09:30:34 +09:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resets all module-global state for testing.
|
|
|
|
|
* Clears pending fallbacks, toast keys, and session chains.
|
|
|
|
|
*/
|
|
|
|
|
export function _resetForTesting(): void {
|
2026-04-18 01:53:03 +09:00
|
|
|
getOrCreateModelFallbackController().reset()
|
2026-03-26 09:30:34 +09:00
|
|
|
}
|