|
| 1 | +import { comboModelId, comboPublicModelId } from "../combos/types"; |
| 2 | +import { detectCursorInstalls } from "../integrations/cursor-detect"; |
| 3 | +import { |
| 4 | + loadCursorEffortTable, |
| 5 | + type CursorEffortTable, |
| 6 | +} from "../integrations/cursor-effort-table"; |
| 7 | +import { |
| 8 | + canonicalizeReasoningEfforts, |
| 9 | + isDeclaredReasoningEffort, |
| 10 | +} from "../reasoning-effort"; |
| 11 | +import { knownModelIdsForProvider } from "../router"; |
| 12 | +import { policyModelId, policyPublicModelId } from "../routing/profile"; |
| 13 | +import type { OcxConfig } from "../types"; |
| 14 | +import { routedSlug } from "../providers/slug-codec"; |
| 15 | +import { predictCursorEffort } from "./models-capabilities"; |
| 16 | + |
| 17 | +const EFFORT_ROW_SEPARATOR = "--"; |
| 18 | + |
| 19 | +export interface ParsedEffortRowId { |
| 20 | + baseId: string; |
| 21 | + effort: string; |
| 22 | +} |
| 23 | + |
| 24 | +export type EffortRowKnownIds = ReadonlySet<string> | ((id: string) => boolean); |
| 25 | + |
| 26 | +export interface EffortRowOptions { |
| 27 | + knownIds?: EffortRowKnownIds; |
| 28 | + table?: CursorEffortTable | null; |
| 29 | + supportsReasoning?: boolean; |
| 30 | +} |
| 31 | + |
| 32 | +function isKnownId(knownIds: EffortRowKnownIds | undefined, id: string): boolean { |
| 33 | + return typeof knownIds === "function" ? knownIds(id) : knownIds?.has(id) === true; |
| 34 | +} |
| 35 | + |
| 36 | +export function effortRowId(baseId: string, effort: string): string { |
| 37 | + return `${baseId}${EFFORT_ROW_SEPARATOR}${effort}`; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Exact configured/public ids that must beat the synthetic terminal-suffix grammar. |
| 42 | + * This is request-local because live-model cache contents can change while the server runs. |
| 43 | + */ |
| 44 | +export function knownEffortRowIds(config: OcxConfig): Set<string> { |
| 45 | + const ids = new Set<string>(); |
| 46 | + for (const [providerName, provider] of Object.entries(config.providers)) { |
| 47 | + const known = knownModelIdsForProvider(providerName, provider, config); |
| 48 | + const namespaces = [providerName, provider.alias].filter((value): value is string => ( |
| 49 | + typeof value === "string" && value.length > 0 |
| 50 | + )); |
| 51 | + for (const id of known) { |
| 52 | + ids.add(id); |
| 53 | + ids.add(routedSlug(providerName, id)); |
| 54 | + for (const namespace of namespaces) ids.add(`${namespace}/${id}`); |
| 55 | + } |
| 56 | + for (const alias of Object.values(provider.modelAliases ?? {})) { |
| 57 | + ids.add(alias); |
| 58 | + for (const namespace of namespaces) ids.add(`${namespace}/${alias}`); |
| 59 | + } |
| 60 | + } |
| 61 | + for (const [id, combo] of Object.entries(config.combos ?? {})) { |
| 62 | + ids.add(comboModelId(id)); |
| 63 | + ids.add(comboPublicModelId(id, combo)); |
| 64 | + } |
| 65 | + for (const [id, profile] of Object.entries(config.routingProfiles ?? {})) { |
| 66 | + ids.add(policyModelId(id)); |
| 67 | + ids.add(policyPublicModelId(id, profile)); |
| 68 | + } |
| 69 | + return ids; |
| 70 | +} |
| 71 | + |
| 72 | +/** Resolve the installed Private Inference effort table once for the current request. */ |
| 73 | +export function loadDetectedCursorEffortTable(): CursorEffortTable | null { |
| 74 | + const privateInference = detectCursorInstalls().find(install => install.build === "private-inference"); |
| 75 | + return loadCursorEffortTable(privateInference); |
| 76 | +} |
| 77 | + |
| 78 | +export function parseEffortRowId( |
| 79 | + id: string, |
| 80 | + config: Pick<OcxConfig, "cursorEffortRows">, |
| 81 | + options: EffortRowOptions = {}, |
| 82 | +): ParsedEffortRowId | null { |
| 83 | + if (config.cursorEffortRows !== true || isKnownId(options.knownIds, id)) return null; |
| 84 | + |
| 85 | + const separator = id.lastIndexOf(EFFORT_ROW_SEPARATOR); |
| 86 | + if (separator <= 0) return null; |
| 87 | + const baseId = id.slice(0, separator); |
| 88 | + const effort = id.slice(separator + EFFORT_ROW_SEPARATOR.length); |
| 89 | + // "none" is never published as a row (discovery filters it), so it is never accepted either. |
| 90 | + if (effort === "none" || !isDeclaredReasoningEffort(effort)) return null; |
| 91 | + if (predictCursorEffort(baseId, options.table ?? null, options.supportsReasoning).ladder !== null) { |
| 92 | + return null; |
| 93 | + } |
| 94 | + return { baseId, effort }; |
| 95 | +} |
| 96 | + |
| 97 | +/** Parse one ingress selector against the current config and installed Cursor table. */ |
| 98 | +export function parseRequestEffortRowId(id: string, config: OcxConfig): ParsedEffortRowId | null { |
| 99 | + if (config.cursorEffortRows !== true) return null; |
| 100 | + // Ordinary ids carry no separator; bail before the known-id scan and install detection so |
| 101 | + // the flag costs nothing on the request path for models that are not effort rows. |
| 102 | + if (id.lastIndexOf(EFFORT_ROW_SEPARATOR) <= 0) return null; |
| 103 | + return parseEffortRowId(id, config, { |
| 104 | + knownIds: knownEffortRowIds(config), |
| 105 | + table: loadDetectedCursorEffortTable(), |
| 106 | + }); |
| 107 | +} |
| 108 | + |
| 109 | +export function expandCursorEffortRow<T extends { id: string }>( |
| 110 | + row: T, |
| 111 | + efforts: readonly string[] | undefined, |
| 112 | + config: Pick<OcxConfig, "cursorEffortRows">, |
| 113 | + options: EffortRowOptions = {}, |
| 114 | +): T[] { |
| 115 | + if (config.cursorEffortRows !== true) return [row]; |
| 116 | + |
| 117 | + const supported = canonicalizeReasoningEfforts( |
| 118 | + (efforts ?? []).filter(effort => effort !== "none" && isDeclaredReasoningEffort(effort)), |
| 119 | + ); |
| 120 | + const supportsReasoning = options.supportsReasoning ?? supported.length > 0; |
| 121 | + if (predictCursorEffort(row.id, options.table ?? null, supportsReasoning).ladder !== null) { |
| 122 | + return [row]; |
| 123 | + } |
| 124 | + return [ |
| 125 | + row, |
| 126 | + ...supported |
| 127 | + .map(effort => effortRowId(row.id, effort)) |
| 128 | + .filter(id => !isKnownId(options.knownIds, id)) |
| 129 | + .map(id => ({ ...row, id })), |
| 130 | + ]; |
| 131 | +} |
0 commit comments