MCPcopy
hub / github.com/coder/mux / normalizeToCanonical

Function normalizeToCanonical

src/common/utils/ai/models.ts:36–63  ·  view source on GitHub ↗
(modelString: string)

Source from the content-addressed store, hash-verified

34 * For gateway-only vendor/model IDs, keep the original gateway-scoped identity.
35 */
36export function normalizeToCanonical(modelString: string): string {
37 const colonIndex = modelString.indexOf(":");
38 if (colonIndex === -1) {
39 return modelString;
40 }
41
42 const providerName = modelString.slice(0, colonIndex) as ProviderName;
43 const gatewayModelId = modelString.slice(colonIndex + 1);
44
45 const def = PROVIDER_DEFINITIONS[providerName];
46 if (def?.kind !== "gateway" || !("fromGatewayModelId" in def) || !def.fromGatewayModelId) {
47 return modelString; // direct/local provider or unknown — already canonical
48 }
49
50 const parsed = def.fromGatewayModelId(gatewayModelId);
51 if (!parsed) {
52 return modelString; // couldn't parse
53 }
54
55 // Only normalize if the origin is a known direct provider.
56 // Gateway-only models like "meta-llama/llama-3.1-405b" stay gateway-scoped.
57 const originDef = PROVIDER_DEFINITIONS[parsed.origin as ProviderName];
58 if (originDef?.kind !== "direct") {
59 return modelString; // origin is not a known direct provider
60 }
61
62 return `${parsed.origin}:${parsed.modelId}`;
63}
64
65/**
66 * Return the explicitly requested gateway provider prefix from a raw model string.

Callers 15

AppInnerFunction · 0.90
ModelSelector.tsxFile · 0.90
TitleBarFunction · 0.90
processSlashCommandFunction · 0.90
normalizeExplicitModelFunction · 0.90
checkContextSwitchFunction · 0.90
WorkspaceStoreClass · 0.90
ModelDisplayFunction · 0.90
ModelFallbacksEditorFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected