-
Notifications
You must be signed in to change notification settings - Fork 530
Expand file tree
/
Copy pathrouter.ts
More file actions
416 lines (385 loc) · 21.8 KB
/
Copy pathrouter.ts
File metadata and controls
416 lines (385 loc) · 21.8 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import type { CodexAccountMode, OcxConfig, OcxProviderConfig } from "./types";
import { preservesPhysicalComboProvider, tryPickComboModel, type ComboPick } from "./combos";
import { hasOwnProvider, resolveEnvValue } from "./config";
import { assertProviderDestinationAllowed } from "./lib/destination-policy";
import { redactSecretString, redactUrlForLog } from "./lib/redact";
import { PROVIDER_REGISTRY, providerCodexAccountMode, providerMatchesRegistryTransport } from "./providers/registry";
import { LEGACY_CHATGPT_PROVIDER_ID, LEGACY_OPENAI_MULTI_PROVIDER_ID, OPENAI_API_PROVIDER_ID, OPENAI_CODEX_PROVIDER_ID } from "./providers/openai-tiers";
import { decodeRoutedModelId, encodeRoutedModelId } from "./providers/slug-codec";
import { getStaleCached } from "./codex/model-cache";
export interface RouteResult {
providerName: string;
provider: OcxProviderConfig;
modelId: string;
codexAccountMode?: CodexAccountMode;
combo?: ComboPick;
}
const MODEL_PROVIDER_PATTERNS: Array<{ providerNames: string[]; prefixes: string[] }> = [
{
providerNames: ["anthropic"],
prefixes: [
"claude-", "claude-sonnet-", "claude-opus-", "claude-haiku-",
],
},
{
providerNames: ["groq"],
prefixes: [
"llama-", "mixtral-", "gemma-",
],
},
];
/**
* Known native model ids for a provider — the decode source for the Codex slug codec
* (src/providers/slug-codec.ts). Union of static config ids, registry seeds, and the
* last-known-good live /models cache (may be empty on a cold start; decode then passes
* unknown ids through unchanged for an honest upstream error).
*/
export function knownModelIdsForProvider(provName: string, prov: OcxProviderConfig): string[] {
const ids = new Set<string>();
for (const id of prov.models ?? []) ids.add(id);
const registry = providerMatchesRegistryTransport(provName, prov)
? PROVIDER_REGISTRY.find(entry => entry.id === provName)
: undefined;
for (const id of registry?.models ?? []) ids.add(id);
// Registry model-keyed hint maps double as known native ids (e.g. NVIDIA carries no
// static models list but names `moonshotai/kimi-k2.6` in its effort/window maps).
for (const map of [
registry?.modelContextWindows,
registry?.modelInputModalities,
registry?.modelReasoningEfforts,
registry?.modelDefaultReasoningEfforts,
registry?.modelReasoningEffortMap,
registry?.modelMaxOutputTokens,
]) {
for (const id of Object.keys(map ?? {})) ids.add(id);
}
for (const cached of getStaleCached(provName) ?? []) ids.add(cached.id);
return [...ids];
}
// Merge registry-default effort maps under user values so built-in provider configs can
// carry real upstream aliases without a disk migration. User overrides win per-key.
function mergeRecord(
seed: Record<string, string> | undefined,
user: Record<string, string> | undefined,
): Record<string, string> | undefined {
if (!seed && !user) return undefined;
return { ...(seed ?? {}), ...(user ?? {}) };
}
function mergeNestedRecord(
seed: Record<string, Record<string, string>> | undefined,
user: Record<string, Record<string, string>> | undefined,
): Record<string, Record<string, string>> | undefined {
if (!seed && !user) return undefined;
const out: Record<string, Record<string, string>> = {};
for (const [key, value] of Object.entries(seed ?? {})) out[key] = { ...value };
for (const [key, value] of Object.entries(user ?? {})) out[key] = { ...(out[key] ?? {}), ...value };
return out;
}
function mergeStringArray(
seed: string[] | undefined,
user: string[] | undefined,
): string[] | undefined {
if (!seed && !user) return undefined;
return [...new Set([...(seed ?? []), ...(user ?? [])])];
}
function mergeRecordFill<T>(
seed: Record<string, T> | undefined,
user: Record<string, T> | undefined,
): Record<string, T> | undefined {
if (!seed && !user) return undefined;
return { ...(seed ?? {}), ...(user ?? {}) };
}
function mergePositiveNumberCaps(
seed: Record<string, number> | undefined,
user: Record<string, number> | undefined,
): Record<string, number> | undefined {
if (!seed && !user) return undefined;
const out = { ...(seed ?? {}) };
for (const [key, value] of Object.entries(user ?? {})) {
out[key] = typeof out[key] === "number" ? Math.min(out[key]!, value) : value;
}
return out;
}
function mergeStringArrayRecord(
seed: Record<string, string[]> | undefined,
user: Record<string, string[]> | undefined,
): Record<string, string[]> | undefined {
if (!seed && !user) return undefined;
const out: Record<string, string[]> = {};
for (const [key, value] of Object.entries(seed ?? {})) out[key] = [...value];
for (const [key, value] of Object.entries(user ?? {})) out[key] = [...value];
return out;
}
/** Same endpoint modulo surrounding space and trailing slashes — matches `matchBaseUrlChoice`. */
function isSameEndpoint(a: string, b: string): boolean {
return a.trim().replace(/\/+$/, "") === b.trim().replace(/\/+$/, "");
}
/**
* Origin of a user-configured URL, with the path withheld.
*
* A configured `baseUrl` is user-controlled and its path may itself be the credential — an
* account-scoped route token such as `https://proxy.example/v1/8fK2mP7qR4nV6x` is opaque and
* high-entropy, so it matches none of the prefix patterns in `redactSecretString`. Pattern
* redaction cannot be trusted for this value, so no path segment is logged at all. `URL.origin`
* also excludes userinfo, query and fragment.
*
* `…/…` marks that a path was present without revealing it, so a reader can tell an origin-only
* config apart from one whose path was dropped.
*/
function configuredOriginForLog(url: string): string {
try {
const parsed = new URL(url.trim());
// "null" is what URL.origin yields for non-special schemes; treat it as unusable.
if (!parsed.origin || parsed.origin === "null") return "(unloggable URL)";
const hasPath = parsed.pathname !== "" && parsed.pathname !== "/";
return hasPath ? `${parsed.origin}/…` : parsed.origin;
} catch {
return "(unparseable URL)";
}
}
// `routedProviderConfig` runs per request, so warn once per (provider, discarded, effective) triple.
// Keyed by the URLs too: editing config.json to a different wrong value warns again.
const discardedBaseUrlWarnings = new Set<string>();
/**
* A pinned registry entry — non-template `baseUrl`, no `allowBaseUrlOverride` — outranks a saved
* `baseUrl`. Dropping it silently is a footgun: requests go to an endpoint the user never
* configured, and a wrong-region or wrong-account URL then surfaces only as a 401 with nothing
* pointing back at the discarded setting.
*
* Warns rather than throws. The effective route is exactly what it was before, so a hard error
* here would break configs that route fine today (a stale `baseUrl` left over from an earlier
* provider is harmless whenever it names the same endpoint the registry pins).
*/
function warnIfBaseUrlDiscarded(providerName: string, userBaseUrl: string, effectiveBaseUrl: string): void {
if (isSameEndpoint(userBaseUrl, effectiveBaseUrl)) return;
// Asymmetric on purpose. Past the guard above, `effectiveBaseUrl` is necessarily
// `registryEntry.baseUrl`: the caller passes the resolved URL, and whenever that resolution
// kept the user's value the two are equal and we have already returned. So the effective side
// is a constant from this repo's registry and safe to print in full — it is also the useful
// half, naming the endpoint requests will actually use. The configured side is untrusted.
const discarded = configuredOriginForLog(userBaseUrl);
const effective = redactSecretString(redactUrlForLog(effectiveBaseUrl));
// Key off the logged forms: no raw credential is retained for the process lifetime, and
// rotating a key embedded in the URL no longer re-warns about the same endpoint mismatch.
// Coarser than the raw URLs — two bad paths on one host warn once, which is the right grain.
const key = `${providerName} | ${discarded} | ${effective}`;
if (discardedBaseUrlWarnings.has(key)) return;
discardedBaseUrlWarnings.add(key);
console.warn(
// Routing is what this warning speaks for: an adapter may adjust the endpoint again
// downstream (kiro re-derives the region), so do not promise where the request lands.
`⚠️ config.json provider "${providerName}": configured baseUrl ${discarded} is ignored`
+ ` because this provider's endpoint is fixed at ${effective}. A URL saved for a different`
+ ` account or region is a common cause of 401s here — drop it, or use the provider whose endpoint matches.`,
);
}
function usableResolvedApiKey(apiKey: string | undefined): string | undefined {
const resolved = resolveEnvValue(apiKey);
return typeof resolved === "string" && resolved.trim().length > 0 ? resolved : undefined;
}
function routedProviderConfig(providerName: string, provider: OcxProviderConfig): OcxProviderConfig {
const registryEntry = PROVIDER_REGISTRY.find(entry => entry.id === providerName);
if (!registryEntry || !providerMatchesRegistryTransport(providerName, provider)) {
assertProviderDestinationAllowed(providerName, provider);
return { ...provider, apiKey: usableResolvedApiKey(provider.apiKey) };
}
const resolvedApiKey = usableResolvedApiKey(provider.apiKey);
const explicitKeyOverride = registryEntry.authKind === "oauth"
&& registryEntry.allowKeyAuthOverride === true
&& provider.authMode === "key"
&& resolvedApiKey !== undefined;
const canonicalAuthMode = explicitKeyOverride
? "key"
: registryEntry.authKind === "forward" || registryEntry.authKind === "oauth"
? registryEntry.authKind
: provider.authMode === "forward" ? undefined : provider.authMode;
const reasoningEffortMap = mergeRecord(registryEntry.reasoningEffortMap, provider.reasoningEffortMap);
const modelReasoningEffortMap = mergeNestedRecord(registryEntry.modelReasoningEffortMap, provider.modelReasoningEffortMap);
const modelReasoningEfforts = mergeStringArrayRecord(registryEntry.modelReasoningEfforts, provider.modelReasoningEfforts);
const modelDefaultReasoningEfforts = mergeRecordFill(registryEntry.modelDefaultReasoningEfforts, provider.modelDefaultReasoningEfforts);
const modelContextWindows = providerName === OPENAI_API_PROVIDER_ID
? mergePositiveNumberCaps(registryEntry.modelContextWindows, provider.modelContextWindows)
: mergeRecordFill(registryEntry.modelContextWindows, provider.modelContextWindows);
const modelInputModalities = mergeRecordFill(registryEntry.modelInputModalities, provider.modelInputModalities);
const modelMaxInputTokens = providerName === OPENAI_API_PROVIDER_ID
? mergePositiveNumberCaps(registryEntry.modelMaxInputTokens, provider.modelMaxInputTokens)
: mergeRecordFill(registryEntry.modelMaxInputTokens, provider.modelMaxInputTokens);
const modelMaxOutputTokens = mergeRecordFill(registryEntry.modelMaxOutputTokens, provider.modelMaxOutputTokens);
const noVisionModels = mergeStringArray(registryEntry.noVisionModels, provider.noVisionModels);
const noReasoningModels = mergeStringArray(registryEntry.noReasoningModels, provider.noReasoningModels);
const noTemperatureModels = mergeStringArray(registryEntry.noTemperatureModels, provider.noTemperatureModels);
const noTopPModels = mergeStringArray(registryEntry.noTopPModels, provider.noTopPModels);
const noPenaltyModels = mergeStringArray(registryEntry.noPenaltyModels, provider.noPenaltyModels);
const autoToolChoiceOnlyModels = mergeStringArray(registryEntry.autoToolChoiceOnlyModels, provider.autoToolChoiceOnlyModels);
const preserveReasoningContentModels = mergeStringArray(registryEntry.preserveReasoningContentModels, provider.preserveReasoningContentModels);
const reasoningSplitModels = mergeStringArray(registryEntry.reasoningSplitModels, provider.reasoningSplitModels);
const thinkingToggleModels = mergeStringArray(registryEntry.thinkingToggleModels, provider.thinkingToggleModels);
const thinkingBudgetModels = mergeStringArray(registryEntry.thinkingBudgetModels, provider.thinkingBudgetModels);
const registryBaseUrlIsTemplate = /\{[^}]*\}/.test(registryEntry.baseUrl);
const userBaseUrl = typeof provider.baseUrl === "string" ? provider.baseUrl.trim() : "";
const userBaseUrlIsResolved = userBaseUrl.length > 0 && !/\{[^}]*\}/.test(userBaseUrl);
if (registryEntry.allowBaseUrlOverride && !userBaseUrlIsResolved) {
throw new Error(`Invalid baseUrl for provider "${providerName}": expected a nonblank URL without unresolved placeholders`);
}
// Registry template URLs are presets; local/self-hosted entries opt in explicitly.
const baseUrl = (registryBaseUrlIsTemplate || registryEntry.allowBaseUrlOverride) && userBaseUrlIsResolved
? userBaseUrl
: registryEntry.baseUrl;
if (userBaseUrlIsResolved) warnIfBaseUrlDiscarded(providerName, userBaseUrl, baseUrl);
assertProviderDestinationAllowed(providerName, { baseUrl, allowPrivateNetwork: provider.allowPrivateNetwork });
return {
...provider,
adapter: registryEntry.adapter,
baseUrl,
...(provider.responsesPath === undefined && registryEntry.responsesPath !== undefined
? { responsesPath: registryEntry.responsesPath }
: {}),
authMode: canonicalAuthMode,
apiKey: resolvedApiKey,
// Backfill the Google wire mode + Vertex project/location from the registry when the user
// config omits them, so a minimal `google-vertex`/`google-antigravity` entry still routes
// through the correct branch (CCA/Vertex) instead of falling back to AI Studio.
...(provider.googleMode === undefined && registryEntry.googleMode !== undefined ? { googleMode: registryEntry.googleMode } : {}),
...(provider.project === undefined && registryEntry.project !== undefined ? { project: registryEntry.project } : {}),
...(provider.location === undefined && registryEntry.location !== undefined ? { location: registryEntry.location } : {}),
...(provider.contextWindow === undefined && registryEntry.contextWindow !== undefined ? { contextWindow: registryEntry.contextWindow } : {}),
...(provider.reasoningEfforts === undefined && registryEntry.reasoningEfforts !== undefined ? { reasoningEfforts: registryEntry.reasoningEfforts } : {}),
...(provider.escapeBuiltinToolNames === undefined && registryEntry.escapeBuiltinToolNames !== undefined ? { escapeBuiltinToolNames: registryEntry.escapeBuiltinToolNames } : {}),
...(provider.keyOptional === undefined && registryEntry.keyOptional !== undefined ? { keyOptional: registryEntry.keyOptional } : {}),
...(provider.modelSuffixBracketStrip === undefined && registryEntry.modelSuffixBracketStrip !== undefined ? { modelSuffixBracketStrip: registryEntry.modelSuffixBracketStrip } : {}),
// Scalar backfill: a persisted config created before the flag shipped inherits the registry
// opt-in, while an explicit user `false` keeps overriding registry `true`.
...(provider.parallelToolCalls === undefined && registryEntry.parallelToolCalls !== undefined ? { parallelToolCalls: registryEntry.parallelToolCalls } : {}),
...(provider.promptCacheKey === undefined && registryEntry.promptCacheKey !== undefined ? { promptCacheKey: registryEntry.promptCacheKey } : {}),
...(provider.defaultMaxOutputTokens === undefined && registryEntry.defaultMaxOutputTokens !== undefined
? { defaultMaxOutputTokens: registryEntry.defaultMaxOutputTokens }
: {}),
...(modelContextWindows ? { modelContextWindows } : {}),
...(modelInputModalities ? { modelInputModalities } : {}),
...(modelMaxInputTokens ? { modelMaxInputTokens } : {}),
...(modelMaxOutputTokens ? { modelMaxOutputTokens } : {}),
...(modelReasoningEfforts ? { modelReasoningEfforts } : {}),
...(modelDefaultReasoningEfforts ? { modelDefaultReasoningEfforts } : {}),
...(reasoningEffortMap ? { reasoningEffortMap } : {}),
...(modelReasoningEffortMap ? { modelReasoningEffortMap } : {}),
...(noVisionModels ? { noVisionModels } : {}),
...(noReasoningModels ? { noReasoningModels } : {}),
...(noTemperatureModels ? { noTemperatureModels } : {}),
...(noTopPModels ? { noTopPModels } : {}),
...(noPenaltyModels ? { noPenaltyModels } : {}),
...(autoToolChoiceOnlyModels ? { autoToolChoiceOnlyModels } : {}),
...(preserveReasoningContentModels ? { preserveReasoningContentModels } : {}),
...(reasoningSplitModels ? { reasoningSplitModels } : {}),
...(thinkingToggleModels ? { thinkingToggleModels } : {}),
...(thinkingBudgetModels ? { thinkingBudgetModels } : {}),
};
}
function activeProviderEntries(config: OcxConfig): [string, OcxProviderConfig][] {
return Object.entries(config.providers)
.filter(([name, provider]) => name !== LEGACY_CHATGPT_PROVIDER_ID && provider.disabled !== true);
}
export class NoEnabledOpenAiProviderError extends Error {
constructor(modelId: string) {
super(
`Model ${modelId} requires the canonical openai provider. `
+ `Run: ocx provider add openai && ocx sync && ocx restart`,
);
this.name = "NoEnabledOpenAiProviderError";
}
}
// Codex uses a small number of control-plane model ids that are not part of the public GPT/o
// naming families. Keep this exact: a broad `codex-*` rule could capture a third-party model.
const CODEX_INTERNAL_OPENAI_MODELS = new Set(["codex-auto-review"]);
function isBareOpenAiFamilyModel(modelId: string): boolean {
return !modelId.includes("/")
&& (/^(?:gpt-|o1-|o3-|o4-)/.test(modelId) || CODEX_INTERNAL_OPENAI_MODELS.has(modelId));
}
function routeResult(providerName: string, provider: OcxProviderConfig, modelId: string): RouteResult {
const codexAccountMode = providerCodexAccountMode(providerName, provider);
return {
providerName,
provider: routedProviderConfig(providerName, provider),
modelId,
...(codexAccountMode ? { codexAccountMode } : {}),
};
}
function routeModelInternal(config: OcxConfig, modelId: string, bypassCombos: boolean): RouteResult {
if (!bypassCombos && !preservesPhysicalComboProvider(config)) {
const combo = tryPickComboModel(config, modelId);
if (combo) {
const concrete = `${combo.target.provider}/${combo.target.model}`;
// The selected target is already a concrete provider/model reference. Resolve it without
// consulting combo aliases again, otherwise an alias that shadows the target can recurse.
const routed = routeModelInternal(config, concrete, true);
return { ...routed, combo };
}
}
// 0. Explicit "<provider>/<model>" namespace (e.g. "opencode-go/deepseek-v4-pro").
// Only triggers when the prefix matches a CONFIGURED provider, so genuine
// slash-containing model ids (e.g. "anthropic/claude-...") fall through when
// no such provider exists.
const slash = modelId.indexOf("/");
if (slash > 0) {
const provName = modelId.slice(0, slash);
if (provName === LEGACY_CHATGPT_PROVIDER_ID || provName === LEGACY_OPENAI_MULTI_PROVIDER_ID) {
throw new Error(`No provider configured for model: ${modelId}`);
}
if (hasOwnProvider(config.providers, provName)) {
const prov = config.providers[provName];
if (prov.disabled === true) throw new Error(`Provider is disabled: ${provName}`);
const known = knownModelIdsForProvider(provName, prov);
// Self-namespaced native id — the vendor segment equals the provider id, so the FULL ref is
// itself a known model (e.g. orcarouter/auto). Route it whole instead of stripping to the
// remainder, which would send a bare `auto` the upstream cannot resolve.
if (known.includes(modelId)) return routeResult(provName, prov, modelId);
// Codex-facing alias ids (`provider/vendor-model`) decode back to the native
// slash id via an exact known-id lookup; raw full-slash selectors keep working.
return routeResult(provName, prov, decodeRoutedModelId(modelId.slice(slash + 1), known));
}
}
if (isBareOpenAiFamilyModel(modelId)) {
const provider = config.providers[OPENAI_CODEX_PROVIDER_ID];
if (provider && provider.disabled !== true) return routeResult(OPENAI_CODEX_PROVIDER_ID, provider, modelId);
throw new NoEnabledOpenAiProviderError(modelId);
}
for (const [provName, prov] of activeProviderEntries(config)) {
if (prov.defaultModel === modelId
|| (typeof prov.defaultModel === "string" && encodeRoutedModelId(prov.defaultModel) === modelId)) {
return routeResult(provName, prov, prov.defaultModel as string);
}
}
const patternRoute = routeByKnownModelPattern(config, modelId);
if (patternRoute) return patternRoute;
for (const [provName, prov] of activeProviderEntries(config)) {
if (prov.models && Array.isArray(prov.models)) {
const hit = (prov.models as string[]).find(id => id === modelId || encodeRoutedModelId(id) === modelId);
if (hit !== undefined) return routeResult(provName, prov, hit);
}
}
if (config.defaultProvider === LEGACY_CHATGPT_PROVIDER_ID) {
throw new Error(`No provider configured for model: ${modelId}`);
}
if (hasOwnProvider(config.providers, config.defaultProvider)) {
const defaultProv = config.providers[config.defaultProvider];
if (defaultProv.disabled === true) throw new Error(`Default provider is disabled: ${config.defaultProvider}`);
return routeResult(config.defaultProvider, defaultProv, modelId);
}
throw new Error(`No provider configured for model: ${modelId}`);
}
export function routeModel(config: OcxConfig, modelId: string): RouteResult {
return routeModelInternal(config, modelId, false);
}
function routeByKnownModelPattern(config: OcxConfig, modelId: string): RouteResult | undefined {
for (const { providerNames, prefixes } of MODEL_PROVIDER_PATTERNS) {
if (prefixes.some(prefix => modelId.startsWith(prefix))) {
const matchingProvider = Object.entries(config.providers).find(
([name]) => providerNames.some(providerName => name === providerName || name.startsWith(`${providerName}-`))
);
if (matchingProvider) {
const [provName, prov] = matchingProvider;
return routeResult(provName, prov, modelId);
}
}
}
return undefined;
}