| 1 | export const DEFAULT_SHORT_MODEL_TIMEOUT_MS = 5 * 60_000 |
| 2 | export const DEFAULT_MODEL_TIMEOUT_MS = 10 * 60_000 |
| 3 | export const MIN_MODEL_TIMEOUT_MS = 60_000 |
| 4 | export const MAX_MODEL_TIMEOUT_MS = 60 * 60_000 |
| 5 | |
| 6 | export type ModelTimeoutProfile = 'verify' | 'planning' | 'design' | 'agent' | 'document' |
| 7 | export type ConfigurableModelTimeoutProfile = Exclude<ModelTimeoutProfile, 'verify'> |
| 8 | export const MODEL_TIMEOUT_PROFILES: readonly ModelTimeoutProfile[] = [ |
| 9 | 'verify', |
| 10 | 'planning', |
| 11 | 'design', |
| 12 | 'agent', |
| 13 | 'document' |
| 14 | ] |
| 15 | export const CONFIGURABLE_MODEL_TIMEOUT_PROFILES: readonly ConfigurableModelTimeoutProfile[] = [ |
| 16 | 'planning', |
| 17 | 'design', |
| 18 | 'agent', |
| 19 | 'document' |
| 20 | ] |
| 21 | |
| 22 | const PROFILE_DEFAULT_TIMEOUT_MS: Record<ModelTimeoutProfile, number> = { |
| 23 | verify: 60_000, |
| 24 | planning: DEFAULT_SHORT_MODEL_TIMEOUT_MS, |
| 25 | design: DEFAULT_SHORT_MODEL_TIMEOUT_MS, |
| 26 | agent: DEFAULT_MODEL_TIMEOUT_MS, |
| 27 | document: DEFAULT_MODEL_TIMEOUT_MS |
| 28 | } |
| 29 | |
| 30 | const PROFILE_MIN_TIMEOUT_MS: Record<ModelTimeoutProfile, number> = { |
| 31 | verify: 30_000, |
| 32 | planning: 2 * 60_000, |
| 33 | design: 2 * 60_000, |
| 34 | agent: 5 * 60_000, |
| 35 | document: 5 * 60_000 |
| 36 | } |
| 37 | |
| 38 | const PROFILE_MAX_TIMEOUT_MS: Record<ModelTimeoutProfile, number> = { |
| 39 | verify: 2 * 60_000, |
| 40 | planning: MAX_MODEL_TIMEOUT_MS, |
| 41 | design: MAX_MODEL_TIMEOUT_MS, |
| 42 | agent: MAX_MODEL_TIMEOUT_MS, |
| 43 | document: MAX_MODEL_TIMEOUT_MS |
| 44 | } |
| 45 | |
| 46 | export function normalizeModelTimeoutMs(value: unknown): number { |
| 47 | const numeric = |
| 48 | typeof value === 'number' |
| 49 | ? value |
| 50 | : typeof value === 'string' && value.trim().length > 0 |
| 51 | ? Number(value) |
| 52 | : Number.NaN |
| 53 | if (!Number.isFinite(numeric)) return DEFAULT_MODEL_TIMEOUT_MS |
| 54 | const integer = Math.round(numeric) |
| 55 | return Math.max(MIN_MODEL_TIMEOUT_MS, Math.min(MAX_MODEL_TIMEOUT_MS, integer)) |
| 56 | } |
| 57 | |
| 58 | export function resolveModelTimeoutMs( |
| 59 | value: unknown, |
| 60 | profile: ModelTimeoutProfile = 'agent' |
| 61 | ): number { |
| 62 | const numeric = |
| 63 | typeof value === 'number' |
| 64 | ? value |
| 65 | : typeof value === 'string' && value.trim().length > 0 |
| 66 | ? Number(value) |
| 67 | : Number.NaN |
| 68 | const fallback = PROFILE_DEFAULT_TIMEOUT_MS[profile] |
| 69 | const integer = Number.isFinite(numeric) ? Math.round(numeric) : fallback |
| 70 | return Math.max( |
| 71 | PROFILE_MIN_TIMEOUT_MS[profile], |
| 72 | Math.min(PROFILE_MAX_TIMEOUT_MS[profile], integer) |
| 73 | ) |
| 74 | } |
| 75 | |
| 76 | export function defaultModelTimeoutMs(profile: ModelTimeoutProfile): number { |
| 77 | return PROFILE_DEFAULT_TIMEOUT_MS[profile] |
| 78 | } |
| 79 | |
| 80 | export function normalizeModelTimeoutSeconds(value: unknown): number { |
| 81 | return Math.round(normalizeModelTimeoutMs(Number(value) * 1000) / 1000) |
| 82 | } |
| 83 | |
| 84 | export function modelTimeoutMsToSeconds( |
| 85 | value: unknown, |
| 86 | profile: ModelTimeoutProfile = 'agent' |
| 87 | ): number { |
| 88 | return Math.round(resolveModelTimeoutMs(value, profile) / 1000) |
| 89 | } |
| 90 |