File size: 7,154 Bytes
064bfd6 fae9128 064bfd6 eb16a76 064bfd6 eb16a76 | 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | import {
getModelStrings as getModelStringsState,
setModelStrings as setModelStringsState,
} from 'src/bootstrap/state.js'
import { logError } from '../log.js'
import { sequential } from '../sequential.js'
import { getInitialSettings } from '../settings/settings.js'
import { findFirstMatch, getBedrockInferenceProfiles } from './bedrock.js'
import {
ALL_MODEL_CONFIGS,
CANONICAL_ID_TO_KEY,
type CanonicalModelId,
type ModelKey,
} from './configs.js'
import { type APIProvider, getAPIProvider } from './providers.js'
/**
* Maps each model version to its provider-specific model ID string.
* Derived from ALL_MODEL_CONFIGS — adding a model there extends this type.
*/
export type ModelStrings = Record<ModelKey, string>
const MODEL_KEYS = Object.keys(ALL_MODEL_CONFIGS) as ModelKey[]
function getBuiltinModelStrings(provider: APIProvider): ModelStrings {
if (provider === 'openai') {
const out = getBuiltinModelStrings('firstParty') as Record<string, string>
out.haiku45 = process.env.OPENAI_HAIKU_MODEL || 'gpt-5.4-mini'
out.sonnet46 = process.env.OPENAI_SONNET_MODEL || 'gpt-5.4'
out.opus46 = process.env.OPENAI_OPUS_MODEL || 'gpt-5.4'
return out as ModelStrings
}
if (provider === 'opencode') {
const out = getBuiltinModelStrings('firstParty') as Record<string, string>
out.haiku45 = process.env.OPENCODE_HAIKU_MODEL || 'gpt-5-nano'
out.sonnet46 = process.env.OPENCODE_SONNET_MODEL || 'big-pickle'
out.opus46 = process.env.OPENCODE_OPUS_MODEL || 'big-pickle'
return out as ModelStrings
}
if (provider === 'openrouter') {
const out = getBuiltinModelStrings('firstParty') as Record<string, string>
out.sonnet46 =
process.env.OPENROUTER_SONNET_MODEL || 'anthropic/claude-sonnet-4.6'
out.opus46 =
process.env.OPENROUTER_OPUS_MODEL || 'anthropic/claude-opus-4.6'
out.haiku45 =
process.env.OPENROUTER_HAIKU_MODEL || 'anthropic/claude-haiku-4.5'
return out as ModelStrings
}
if (provider === 'nvidia') {
const out = getBuiltinModelStrings('firstParty') as Record<string, string>
out.haiku45 = process.env.NVIDIA_HAIKU_MODEL || 'nvidia/llama-3.1-nemotron-70b-instruct'
out.sonnet45 = process.env.NVIDIA_SONNET_MODEL || 'nvidia/llama-3.1-nemotron-70b-instruct'
out.sonnet46 = process.env.NVIDIA_SONNET_MODEL || 'nvidia/llama-3.1-nemotron-70b-instruct'
out.opus46 = process.env.NVIDIA_OPUS_MODEL || 'nvidia/llama-3.1-nemotron-70b-instruct'
return out as ModelStrings
}
const out = {} as ModelStrings
for (const key of MODEL_KEYS) {
out[key] = ALL_MODEL_CONFIGS[key][provider]
}
return out
}
async function getBedrockModelStrings(): Promise<ModelStrings> {
const fallback = getBuiltinModelStrings('bedrock')
let profiles: string[] | undefined
try {
profiles = await getBedrockInferenceProfiles()
} catch (error) {
logError(error as Error)
return fallback
}
if (!profiles?.length) {
return fallback
}
// Each config's firstParty ID is the canonical substring we search for in the
// user's inference profile list (e.g. "claude-opus-4-6" matches
// "eu.anthropic.claude-opus-4-6-v1"). Fall back to the hardcoded bedrock ID
// when no matching profile is found.
const out = {} as ModelStrings
for (const key of MODEL_KEYS) {
const needle = ALL_MODEL_CONFIGS[key].firstParty
out[key] = findFirstMatch(profiles, needle) || fallback[key]
}
return out
}
/**
* Layer user-configured modelOverrides (from settings.json) on top of the
* provider-derived model strings. Overrides are keyed by canonical first-party
* model ID (e.g. "claude-opus-4-6") and map to arbitrary provider-specific
* strings — typically Bedrock inference profile ARNs.
*/
function applyModelOverrides(ms: ModelStrings): ModelStrings {
const overrides = getInitialSettings().modelOverrides
if (!overrides) {
return ms
}
const out = { ...ms }
for (const [canonicalId, override] of Object.entries(overrides)) {
const key = CANONICAL_ID_TO_KEY[canonicalId as CanonicalModelId]
if (key && override) {
out[key] = override
}
}
return out
}
/**
* Resolve an overridden model ID (e.g. a Bedrock ARN) back to its canonical
* first-party model ID. If the input doesn't match any current override value,
* it is returned unchanged. Safe to call during module init (no-ops if settings
* aren't loaded yet).
*/
export function resolveOverriddenModel(modelId: string): string {
let overrides: Record<string, string> | undefined
try {
overrides = getInitialSettings().modelOverrides
} catch {
return modelId
}
if (!overrides) {
return modelId
}
for (const [canonicalId, override] of Object.entries(overrides)) {
if (override === modelId) {
return canonicalId
}
}
return modelId
}
const updateBedrockModelStrings = sequential(async () => {
if (getModelStringsState() !== null) {
// Already initialized. Doing the check here, combined with
// `sequential`, allows the test suite to reset the state
// between tests while still preventing multiple API calls
// in production.
return
}
try {
const ms = await getBedrockModelStrings()
setModelStringsState(ms)
} catch (error) {
logError(error as Error)
}
})
function initModelStrings(): void {
const ms = getModelStringsState()
if (ms !== null) {
// Already initialized
return
}
// Initial with default values for non-Bedrock providers
if (getAPIProvider() !== 'bedrock') {
setModelStringsState(getBuiltinModelStrings(getAPIProvider()))
return
}
// On Bedrock, update model strings in the background without blocking.
// Don't set the state in this case so that we can use `sequential` on
// `updateBedrockModelStrings` and check for existing state on multiple
// calls.
void updateBedrockModelStrings()
}
export function getModelStrings(): ModelStrings {
const ms = getModelStringsState()
if (ms === null) {
initModelStrings()
// Bedrock path falls through here while the profile fetch runs in the
// background — still honor overrides on the interim defaults.
return applyModelOverrides(getBuiltinModelStrings(getAPIProvider()))
}
return applyModelOverrides(ms)
}
/**
* Ensure model strings are fully initialized.
* For Bedrock users, this waits for the profile fetch to complete.
* Call this before generating model options to ensure correct region strings.
*/
export async function ensureModelStringsInitialized(): Promise<void> {
const ms = getModelStringsState()
if (ms !== null) {
return
}
// For non-Bedrock, initialize synchronously
if (getAPIProvider() !== 'bedrock') {
setModelStringsState(getBuiltinModelStrings(getAPIProvider()))
return
}
// For Bedrock, wait for the profile fetch
await updateBedrockModelStrings()
}
/**
* Clear cached model strings so the next call to getModelStrings()
* re-initializes from the current provider. Call this after changing
* the auth provider (e.g., after /login).
*/
export function clearModelStrings(): void {
setModelStringsState(null as unknown as ModelStrings)
}
|