|
| 1 | +/** |
| 2 | + * The one place that decides which models the management API offers as the |
| 3 | + * web-search sidecar, and which it refuses to persist (#2188). |
| 4 | + * |
| 5 | + * Two routes write `webSearchSidecar.model` — `PUT /api/sidecar-settings` |
| 6 | + * and the `webSearchSidecar` override in `PUT /api/claude-code`. They share |
| 7 | + * this module for the same reason the vision routes share |
| 8 | + * vision-sidecar-options.ts: a gate on one route and a stale copy on the |
| 9 | + * other is the same as no gate at all. `ocx config set` raw JSON writes |
| 10 | + * bypass management gates by design (operator escape hatch, same as vision). |
| 11 | + * |
| 12 | + * Unlike vision's provably-blind gate (reject only on positive proof), the |
| 13 | + * web-search gate is MEMBERSHIP: the executor set is closed and known, so an |
| 14 | + * id outside (candidates ∪ auth slots) can never run and is refused. |
| 15 | + */ |
| 16 | +import type { OcxConfig } from "../../types"; |
| 17 | +import { resolveSidecarAuth } from "../../sidecar/auth"; |
| 18 | +import { pickerVisibleSidecarCandidates, type SidecarCandidate } from "../../sidecar/candidates"; |
| 19 | +import { isWebSearchAuthSlotModel, webSearchSidecarCandidates } from "../../web-search/backends"; |
| 20 | + |
| 21 | +export interface WebSearchModelOption { |
| 22 | + value: string; |
| 23 | + label: string; |
| 24 | + /** True when the row is an auth-slot entitlement rather than a picker row. */ |
| 25 | + authSlot?: boolean; |
| 26 | +} |
| 27 | + |
| 28 | +/** The candidate rows the web-search executors can actually run right now. */ |
| 29 | +export async function webSearchCandidateRows(config: OcxConfig): Promise<SidecarCandidate[]> { |
| 30 | + const auth = resolveSidecarAuth(config); |
| 31 | + const all = await pickerVisibleSidecarCandidates(config, auth); |
| 32 | + return webSearchSidecarCandidates(config, auth, all); |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * The GUI/CLI option list. The persisted model is display-grandfathered so an |
| 37 | + * operator can SEE a now-illegal setting in the picker (parity with the vision |
| 38 | + * GET); new writes of such an id are still rejected by the gate below. |
| 39 | + */ |
| 40 | +export function webSearchModelOptionsFrom( |
| 41 | + config: Pick<OcxConfig, "webSearchSidecar">, |
| 42 | + candidates: readonly SidecarCandidate[], |
| 43 | +): WebSearchModelOption[] { |
| 44 | + const byValue = new Map<string, WebSearchModelOption>(); |
| 45 | + for (const candidate of candidates) { |
| 46 | + if (byValue.has(candidate.id)) continue; |
| 47 | + byValue.set(candidate.id, { |
| 48 | + value: candidate.id, |
| 49 | + label: candidate.id, |
| 50 | + ...(candidate.authSlot ? { authSlot: true } : {}), |
| 51 | + }); |
| 52 | + } |
| 53 | + const persisted = config.webSearchSidecar?.model; |
| 54 | + if (persisted && !byValue.has(persisted)) { |
| 55 | + byValue.set(persisted, { value: persisted, label: persisted }); |
| 56 | + } |
| 57 | + return [...byValue.values()].sort((a, b) => a.value.localeCompare(b.value)); |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Membership gate: reject when the id is neither a runnable candidate nor an |
| 62 | + * auth-slot model. Auth slots pass even when the matching login is currently |
| 63 | + * absent — the slot is a legal setting whose executor simply is not live yet, |
| 64 | + * and refusing it would make settings order-dependent on login state. |
| 65 | + */ |
| 66 | +export function webSearchModelIsRejected( |
| 67 | + requested: string, |
| 68 | + candidates: readonly SidecarCandidate[], |
| 69 | +): boolean { |
| 70 | + if (isWebSearchAuthSlotModel(requested)) return false; |
| 71 | + return !candidates.some(candidate => candidate.id === requested); |
| 72 | +} |
| 73 | + |
| 74 | +/** Uniform 400 payload naming the filter, mirroring visionDescriberRejection's shape. */ |
| 75 | +export function webSearchModelRejection( |
| 76 | + field: string, |
| 77 | + requested: string, |
| 78 | + candidates: readonly SidecarCandidate[], |
| 79 | +): { error: string; allowedModels: string[] } { |
| 80 | + return { |
| 81 | + error: `${field}: "${requested}" is not a web-search sidecar candidate — the model must be ` + |
| 82 | + "picker-visible and runnable by an active executor-backed backend (or an auth-slot model)", |
| 83 | + allowedModels: candidates.map(candidate => candidate.id).sort(), |
| 84 | + }; |
| 85 | +} |
0 commit comments