Skip to content

Commit 5cee10c

Browse files
fix(script): resolve overlapping model prefixes deterministically (longest match wins)
1 parent 02f0632 commit 5cee10c

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

packages/db/scripts/migrate-block-api-keys-to-byok.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,21 @@ function parseRequireKeyPrefixArgs(): Record<string, string> {
168168

169169
const REQUIRED_KEY_PREFIX = parseRequireKeyPrefixArgs()
170170

171+
// Longest prefix first so the most specific mapping wins when prefixes overlap
172+
// (e.g. grok-4=other beats grok=xai for "grok-4-fast"), making resolution deterministic
173+
// regardless of --model-map argument order.
174+
const MODEL_PREFIX_ENTRIES = Object.entries(MODEL_PREFIX_TO_PROVIDER).sort(
175+
(a, b) => b[0].length - a[0].length
176+
)
177+
171178
/**
172179
* Resolves an LLM-family block's selected model to a BYOK provider id via the configured
173180
* model-prefix map. Mirrors how the app matches models to providers (e.g. grok -> xai).
174181
*/
175182
function resolveModelProvider(model: string): string | null {
176183
const normalized = model.trim().toLowerCase()
177184
if (!normalized) return null
178-
for (const [prefix, providerId] of Object.entries(MODEL_PREFIX_TO_PROVIDER)) {
185+
for (const [prefix, providerId] of MODEL_PREFIX_ENTRIES) {
179186
if (normalized.startsWith(prefix)) return providerId
180187
}
181188
return null

0 commit comments

Comments
 (0)