| 97 | | null; |
| 98 | |
| 99 | async function resolveCanonical(endpointName: string): Promise<Resolution> { |
| 100 | const bare = endpointName.replace(/^databricks-/, ""); |
| 101 | |
| 102 | // Models in provider subdirectories may not have provider-agnostic metadata |
| 103 | // yet, so inline when no model-only entry exists. |
| 104 | if (bare.startsWith("gpt-oss-")) { |
| 105 | const p = path.join(PROVIDERS_DIR, "openrouter", "models", "openai", `${bare}.toml`); |
| 106 | if (existsSync(p)) { |
| 107 | return { type: "inline", content: await readFile(p, "utf8") }; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // Meta Llama: "meta-llama-3-3-70b-instruct" → "llama-3.3-70b-instruct" |
| 112 | if (bare.startsWith("meta-llama-") || bare.startsWith("llama-")) { |
| 113 | const llamaId = bare |
| 114 | .replace(/^meta-llama-/, "llama-") |
| 115 | .replace(/^(llama-\d+)-(\d+)-/, "$1.$2-"); |
| 116 | const p = path.join(PROVIDERS_DIR, "llama", "models", `${llamaId}.toml`); |
| 117 | const metadata = path.join(MODEL_METADATA_DIR, "meta", `${llamaId}.toml`); |
| 118 | if (existsSync(p) && existsSync(metadata)) { |
| 119 | return { type: "base_model", from: `meta/${llamaId}` }; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | for (const [prefix, provider] of PREFIX_TO_PROVIDER) { |
| 124 | if (!bare.startsWith(prefix)) continue; |
| 125 | |
| 126 | const exact = path.join(PROVIDERS_DIR, provider, "models", `${bare}.toml`); |
| 127 | if (existsSync(exact)) return { type: "base_model", from: `${provider}/${bare}` }; |
| 128 | |
| 129 | // Try with hyphens-as-dots in version (e.g. gpt-5-4 → gpt-5.4) |
| 130 | const dotted = bare.replace(/^((?:[a-z]+-)+\d+)-(\d)/, "$1.$2"); |
| 131 | if (dotted !== bare) { |
| 132 | const dottedExact = path.join(PROVIDERS_DIR, provider, "models", `${dotted}.toml`); |
| 133 | if (existsSync(dottedExact)) return { type: "base_model", from: `${provider}/${dotted}` }; |
| 134 | } |
| 135 | |
| 136 | // Fuzzy: longest filename that shares a prefix with bare or its dotted form |
| 137 | const candidates = [bare, ...(dotted !== bare ? [dotted] : [])]; |
| 138 | const files: string[] = []; |
| 139 | try { |
| 140 | for await (const f of new Bun.Glob("*.toml").scan({ |
| 141 | cwd: path.join(PROVIDERS_DIR, provider, "models"), |
| 142 | })) { |
| 143 | files.push(f); |
| 144 | } |
| 145 | } catch { |
| 146 | // provider directory may not exist |
| 147 | } |
| 148 | const match = files |
| 149 | .map((f) => f.replace(/\.toml$/, "")) |
| 150 | .filter((id) => candidates.some((c) => id.startsWith(c) || c.startsWith(id))) |
| 151 | .sort((a, b) => b.length - a.length)[0]; |
| 152 | if (match) return { type: "base_model", from: `${provider}/${match}` }; |
| 153 | } |
| 154 | |
| 155 | return null; |
| 156 | } |