-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(providers): add model-specific provider routing for Vercel AI Gateway #2364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lidge-jun
merged 5 commits into
lidge-jun:dev
from
chilung-cgu:fix/issue-1406-vercel-gateway-routing
Aug 29, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fc64a1f
feat(providers): add model-specific provider routing for Vercel AI Ga…
chilung-cgu 16a45bf
feat(providers): add model-specific provider routing for Vercel AI Ga…
chilung-cgu fdf41e1
feat(providers): wire vercelGatewayRouting management validation, saf…
chilung-cgu 76f76c5
test(vercel): cover routed model preference selectors
chilung-cgu ce9e8ca
docs(vercel): clarify dynamic routing fallback and sort semantics
chilung-cgu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import type { OcxProviderConfig, VercelGatewayRouting } from "../types"; | ||
| import { sanitizeLogMetadataString } from "../lib/redact"; | ||
|
|
||
| const ROUTING_KEYS = new Set(["order", "only", "sort"]); | ||
| const SORT_VALUES = new Set(["cost", "ttft", "tps"]); | ||
| const MAX_PROVIDER_SLUGS = 64; | ||
|
|
||
| function isPlainRecord(value: unknown): value is Record<string, unknown> { | ||
| if (!value || typeof value !== "object" || Array.isArray(value)) return false; | ||
| const prototype = Object.getPrototypeOf(value); | ||
| return prototype === Object.prototype || prototype === null; | ||
| } | ||
|
|
||
| export function isCanonicalVercelGatewayTarget(baseUrl: string): boolean { | ||
| try { | ||
| const url = new URL(baseUrl); | ||
| return url.origin === "https://ai-gateway.vercel.sh" | ||
| && !url.username | ||
| && !url.password | ||
| && !url.search | ||
| && !url.hash | ||
| && url.pathname.replace(/\/+$/, "") === "/v1"; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| function routingPreferenceError(value: unknown, field: string): string | null { | ||
| if (!isPlainRecord(value)) return `${field} must be a plain object`; | ||
| const unknown = Object.keys(value).find(key => !ROUTING_KEYS.has(key)); | ||
| if (unknown) { | ||
| const sanitized = sanitizeLogMetadataString(unknown); | ||
| return `${field} contains unknown field "${sanitized ?? "unknown"}"`; | ||
| } | ||
|
|
||
| for (const listField of ["order", "only"] as const) { | ||
| const list = value[listField]; | ||
| if (list === undefined) continue; | ||
| if (!Array.isArray(list) || list.length === 0 || list.length > MAX_PROVIDER_SLUGS) { | ||
| return `${field}.${listField} must contain 1-${MAX_PROVIDER_SLUGS} provider slugs`; | ||
| } | ||
| const seen = new Set<string>(); | ||
| for (const slug of list) { | ||
| if (typeof slug !== "string" || !slug.trim() || slug !== slug.trim() || slug.length > 128) { | ||
| return `${field}.${listField} must contain nonblank trimmed provider slugs up to 128 characters`; | ||
| } | ||
| if (seen.has(slug)) return `${field}.${listField} must not contain duplicate provider slugs`; | ||
| seen.add(slug); | ||
| } | ||
| } | ||
| if (value.sort !== undefined && (typeof value.sort !== "string" || !SORT_VALUES.has(value.sort))) { | ||
| return `${field}.sort must be "cost", "ttft", or "tps"`; | ||
| } | ||
| if (value.order === undefined && value.only === undefined && value.sort === undefined) { | ||
| return `${field} must define order, only, or sort`; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| export function vercelGatewayRoutingConfigError(provider: OcxProviderConfig): string | null { | ||
| const hasDefault = provider.vercelGatewayRouting !== undefined; | ||
| const hasModels = provider.modelVercelGatewayRouting !== undefined; | ||
| if (!hasDefault && !hasModels) return null; | ||
| if (provider.adapter !== "openai-chat") { | ||
| return "Vercel AI Gateway routing preferences require the openai-chat adapter"; | ||
| } | ||
| if (!isCanonicalVercelGatewayTarget(provider.baseUrl)) { | ||
| return "Vercel AI Gateway routing preferences require the canonical https://ai-gateway.vercel.sh/v1 baseUrl"; | ||
| } | ||
| if (hasDefault) { | ||
| const error = routingPreferenceError(provider.vercelGatewayRouting, "vercelGatewayRouting"); | ||
| if (error) return error; | ||
| } | ||
| if (hasModels) { | ||
| const routes = provider.modelVercelGatewayRouting; | ||
| if (!isPlainRecord(routes)) return "modelVercelGatewayRouting must be a plain object"; | ||
| for (const [modelId, preference] of Object.entries(routes)) { | ||
| if (!modelId.trim() || modelId !== modelId.trim()) { | ||
| return "modelVercelGatewayRouting keys must be nonblank trimmed model ids"; | ||
| } | ||
| const sanitizedModel = sanitizeLogMetadataString(modelId) ?? "model"; | ||
| const error = routingPreferenceError(preference, `modelVercelGatewayRouting.${sanitizedModel}`); | ||
| if (error) return error; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| export function resolveVercelGatewayRouting( | ||
| provider: OcxProviderConfig, | ||
| modelId: string, | ||
| ): VercelGatewayRouting | undefined { | ||
| if (!isCanonicalVercelGatewayTarget(provider.baseUrl)) return undefined; | ||
| const modelRoutes = provider.modelVercelGatewayRouting; | ||
| return modelRoutes && Object.hasOwn(modelRoutes, modelId) | ||
| ? modelRoutes[modelId] | ||
| : provider.vercelGatewayRouting; | ||
| } | ||
|
|
||
| export function vercelGatewayProviderPayload( | ||
| preference: VercelGatewayRouting, | ||
| ): Record<string, unknown> { | ||
| return { | ||
| ...(preference.order ? { order: [...preference.order] } : {}), | ||
| ...(preference.only ? { only: [...preference.only] } : {}), | ||
| ...(preference.sort ? { sort: preference.sort } : {}), | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When Compatibility Lab is enabled and an operator changes
only,order, orsort, this adapter sends the request through a different Vercel upstream route, butresolveProductionBehaviorValues()and the closed key list insrc/lab/subject/behavior-fingerprint.tsstill include only the analogous OpenRouter settings. Consequently, configurations such asonly: ["novita"]andonly: ["deepinfra"]produce the same route subject, allowing evidence collected for one inference provider to be reused by compatibility policy for the other. Add the effective model/default Vercel routing values to the behavior resolver and fingerprint schema, including the appropriate resolver-version update.Useful? React with 👍 / 👎.