File size: 1,738 Bytes
7a1ad33 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import type {
ModelAvailabilityService,
ModelHealthStatus,
ModelId,
} from './modelAvailabilityService.js';
/**
* Whether to prompt the user or fallback silently on a model API failure.
*/
export type FallbackAction = 'silent' | 'prompt';
/**
* Type of possible errors from model API failures.
*/
export type FailureKind = 'terminal' | 'transient' | 'not_found' | 'unknown';
/**
* Map from model API failure reason to user interaction.
*/
export type ModelPolicyActionMap = Partial<Record<FailureKind, FallbackAction>>;
/**
* What state (e.g. Terminal, Sticky Retry) to set a model after failed API call.
*/
export type ModelPolicyStateMap = Partial<
Record<FailureKind, ModelHealthStatus>
>;
/**
* Defines the policy for a single model in the availability chain.
*
* This includes:
* - Which model this policy applies to.
* - What actions to take (prompt vs silent fallback) for different failure kinds.
* - How the model's health status should transition upon failure.
* - Whether this model is considered a "last resort" (i.e. use if all models are unavailable).
*/
export interface ModelPolicy {
model: ModelId;
actions: ModelPolicyActionMap;
stateTransitions: ModelPolicyStateMap;
isLastResort?: boolean;
maxAttempts?: number;
}
/**
* A chain of model policies defining the priority and fallback behavior.
* The first model in the chain is the primary model.
*/
export type ModelPolicyChain = ModelPolicy[];
/**
* Context required by retry logic to apply availability policies on failure.
*/
export interface RetryAvailabilityContext {
service: ModelAvailabilityService;
policy: ModelPolicy;
}
|