/** Source-aware model identity shared by Desktop, TUI and the local Agent. */ export type ModelRef = | { source: 'local'; modelId: string } | { source: 'gateway'; serverId: string; modelId: string }; /** Compatibility is kept at the CLI/Web protocol edge only. */ export function modelRefFromLegacy(value: string, serverId = 'gateway'): ModelRef { return value.startsWith('online:') ? { source: 'gateway', serverId, modelId: value.slice('online:'.length) } : { source: 'local', modelId: value }; } export function legacyModelId(ref: ModelRef): string { return ref.source === 'gateway' ? `online:${ref.modelId}` : ref.modelId; } export type ModelFallbackReason = | 'offline' | 'logged_out' | 'grant_revoked' | 'quota_exhausted' | 'model_disabled' | 'credential_expired' | null; export interface SessionModelPreference { preferred: ModelRef | null; effective: ModelRef | null; fallbackReason: ModelFallbackReason; restoreWhenAvailable: boolean; }