|
| 1 | +/** |
| 2 | + * Candidate capability evidence for policy routing (RI-05). |
| 3 | + * |
| 4 | + * Evidence comes from canonical local sources only - provider config maps, |
| 5 | + * the provider registry, the cached Codex catalog file, and the native-model |
| 6 | + * metadata helpers. No live network fetch happens at routing time. |
| 7 | + * |
| 8 | + * "Unknown is not zero": any dimension without canonical evidence stays |
| 9 | + * `undefined` (unknown) and the profile's `unknownEvidence` policy decides |
| 10 | + * how that affects eligibility. |
| 11 | + */ |
| 12 | + |
| 13 | +import type { OcxConfig } from "../types"; |
| 14 | +import { isCanonicalOpenAiForwardProvider } from "../providers/openai-tiers"; |
| 15 | +import { PROVIDER_REGISTRY } from "../providers/registry"; |
| 16 | +import { |
| 17 | + nativeInputModalities, |
| 18 | + nativeOpenAiContextWindow, |
| 19 | + nativeParallelToolCalls, |
| 20 | + nativeReasoningEfforts, |
| 21 | +} from "../codex/catalog/metadata"; |
| 22 | +import { readCatalog, readCodexCatalogPath } from "../codex/catalog/parsing"; |
| 23 | +import type { RouteCapabilityEvidence } from "./trace"; |
| 24 | + |
| 25 | +function cachedCatalogModels(): Array<{ provider: string; id: string; contextWindow?: number; inputModalities?: string[]; reasoningEfforts?: string[]; capabilities?: string[] }> { |
| 26 | + try { |
| 27 | + const catalog = readCatalog(readCodexCatalogPath()); |
| 28 | + const models = catalog?.models; |
| 29 | + if (!Array.isArray(models)) return []; |
| 30 | + return models |
| 31 | + .filter((model): model is Record<string, unknown> & { id: string; provider: string } => |
| 32 | + typeof model === "object" && model !== null && typeof model.id === "string" && typeof model.provider === "string") |
| 33 | + .map(model => ({ |
| 34 | + provider: model.provider, |
| 35 | + id: model.id, |
| 36 | + ...(typeof model.contextWindow === "number" ? { contextWindow: model.contextWindow } : {}), |
| 37 | + ...(Array.isArray(model.inputModalities) |
| 38 | + ? { inputModalities: model.inputModalities.filter((value): value is string => typeof value === "string") } |
| 39 | + : {}), |
| 40 | + ...(Array.isArray(model.reasoningEfforts) |
| 41 | + ? { reasoningEfforts: model.reasoningEfforts.filter((value): value is string => typeof value === "string") } |
| 42 | + : {}), |
| 43 | + ...(Array.isArray(model.capabilities) |
| 44 | + ? { capabilities: model.capabilities.filter((value): value is string => typeof value === "string") } |
| 45 | + : {}), |
| 46 | + })); |
| 47 | + } catch { |
| 48 | + return []; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +function isLocalHostname(hostname: string): boolean { |
| 53 | + const normalized = hostname.trim().toLowerCase().replace(/\.$/, ""); |
| 54 | + return normalized === "localhost" || normalized === "127.0.0.1" |
| 55 | + || normalized === "::1" || normalized === "[::1]" |
| 56 | + || normalized.endsWith(".localhost"); |
| 57 | +} |
| 58 | + |
| 59 | +function isPrivateHostname(hostname: string): boolean { |
| 60 | + return hostname.startsWith("10.") || hostname.startsWith("192.168.") |
| 61 | + || /^172\.(1[6-9]|2\d|3[01])\./.test(hostname); |
| 62 | +} |
| 63 | + |
| 64 | +function localRemoteEvidence(baseUrl: string | undefined): Pick<RouteCapabilityEvidence, "localOnly" | "remoteAllowed"> { |
| 65 | + if (typeof baseUrl !== "string" || baseUrl.length === 0) return {}; |
| 66 | + try { |
| 67 | + const hostname = new URL(baseUrl).hostname; |
| 68 | + if (!hostname) return {}; |
| 69 | + if (isLocalHostname(hostname) || isPrivateHostname(hostname)) return { localOnly: true }; |
| 70 | + return { remoteAllowed: true }; |
| 71 | + } catch { |
| 72 | + return {}; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Assemble canonical capability evidence for one `provider/model` candidate. |
| 78 | + * Sources (in priority order): provider config maps, provider registry hints, |
| 79 | + * cached Codex catalog row, native-model metadata. |
| 80 | + */ |
| 81 | +export function candidateCapabilityEvidence( |
| 82 | + config: OcxConfig, |
| 83 | + providerName: string, |
| 84 | + modelId: string, |
| 85 | +): RouteCapabilityEvidence { |
| 86 | + const provider = config.providers[providerName]; |
| 87 | + const registryEntry = PROVIDER_REGISTRY.find(entry => entry.id === providerName); |
| 88 | + const catalogRow = cachedCatalogModels().find(model => model.provider === providerName && model.id === modelId); |
| 89 | + const isNative = providerName === "openai" && !modelId.includes("/"); |
| 90 | + |
| 91 | + const contextWindow = provider?.modelContextWindows?.[modelId] |
| 92 | + ?? provider?.contextWindow |
| 93 | + ?? registryEntry?.modelContextWindows?.[modelId] |
| 94 | + ?? catalogRow?.contextWindow |
| 95 | + ?? (isNative ? nativeOpenAiContextWindow(modelId) : undefined); |
| 96 | + |
| 97 | + const modalities = provider?.modelInputModalities?.[modelId] |
| 98 | + ?? registryEntry?.modelInputModalities?.[modelId] |
| 99 | + ?? catalogRow?.inputModalities |
| 100 | + ?? (isNative ? nativeInputModalities(modelId) : undefined); |
| 101 | + const image = Array.isArray(modalities) |
| 102 | + ? modalities.includes("image") |
| 103 | + : undefined; |
| 104 | + |
| 105 | + const capabilities = catalogRow?.capabilities ?? []; |
| 106 | + const tools = capabilities.includes("tools") |
| 107 | + || (isNative ? true : provider?.parallelToolCalls === true) |
| 108 | + || undefined; |
| 109 | + |
| 110 | + const reasoningEfforts = provider?.modelReasoningEfforts?.[modelId] |
| 111 | + ?? registryEntry?.modelReasoningEfforts?.[modelId] |
| 112 | + ?? catalogRow?.reasoningEfforts |
| 113 | + ?? (isNative ? nativeReasoningEfforts(modelId) : undefined); |
| 114 | + |
| 115 | + const tierSupport = provider?.supportsServiceTier |
| 116 | + ?? registryEntry?.supportsServiceTier; |
| 117 | + const serviceTier = tierSupport === true |
| 118 | + ? "supported" |
| 119 | + : tierSupport === false ? "unsupported" : "unknown"; |
| 120 | + |
| 121 | + const localRemote = localRemoteEvidence(provider?.baseUrl); |
| 122 | + const encryptedCodexTasks = isCanonicalOpenAiForwardProvider( |
| 123 | + provider ?? { adapter: "", authMode: undefined, baseUrl: undefined }, |
| 124 | + ); |
| 125 | + |
| 126 | + return { |
| 127 | + ...(typeof contextWindow === "number" ? { contextWindow } : {}), |
| 128 | + ...(typeof image === "boolean" ? { image } : {}), |
| 129 | + ...(typeof tools === "boolean" ? { tools } : {}), |
| 130 | + ...(reasoningEfforts !== undefined && reasoningEfforts.length > 0 ? { reasoningEfforts } : {}), |
| 131 | + ...(serviceTier !== "unknown" ? { serviceTier } : {}), |
| 132 | + ...localRemote, |
| 133 | + encryptedCodexTasks, |
| 134 | + }; |
| 135 | +} |
0 commit comments