|
| 1 | +import assert from "node:assert/strict" |
| 2 | + |
| 3 | +type Effort = "disable" | "none" | "minimal" | "low" | "medium" | "high" | "xhigh" | "max" |
| 4 | +type Supported = true | readonly Effort[] |
| 5 | + |
| 6 | +interface ModelState { |
| 7 | + supported: Supported |
| 8 | + required: boolean |
| 9 | + modelDefault?: Effort |
| 10 | + stored?: Effort |
| 11 | + enabled?: boolean |
| 12 | +} |
| 13 | + |
| 14 | +const canonicalEfforts = ["low", "medium", "high", "xhigh", "max"] as const |
| 15 | +const defaultEfforts = ["low", "medium", "high"] as const |
| 16 | +const supportedSets: Supported[] = [ |
| 17 | + true, |
| 18 | + ["disable", "low", "high"], |
| 19 | + ["low", "high"], |
| 20 | + ["none", "low", "high"], |
| 21 | + ["low", "medium", "high", "xhigh", "max"], |
| 22 | +] |
| 23 | +const values = [undefined, "disable", "none", "low", "high", "max"] as const |
| 24 | +const enabledValues = [undefined, false, true] as const |
| 25 | + |
| 26 | +function availableOptions(state: ModelState): readonly Effort[] { |
| 27 | + return state.supported === true |
| 28 | + ? state.required |
| 29 | + ? defaultEfforts |
| 30 | + : ["disable", ...defaultEfforts] |
| 31 | + : state.supported |
| 32 | +} |
| 33 | + |
| 34 | +function supportsEffort(supported: Supported, effort: Effort): boolean { |
| 35 | + return supported === true || supported.includes(effort) |
| 36 | +} |
| 37 | + |
| 38 | +function resolveSelection(state: ModelState): Effort { |
| 39 | + const available = availableOptions(state) |
| 40 | + const defaultEffort = state.modelDefault ?? (state.required ? "medium" : "disable") |
| 41 | + const raw = state.stored ?? defaultEffort |
| 42 | + const fallback = available.includes(defaultEffort) ? defaultEffort : (available[0] ?? raw) |
| 43 | + return available.includes(raw) ? raw : fallback |
| 44 | +} |
| 45 | + |
| 46 | +function resolveRequest(state: ModelState): Effort | undefined { |
| 47 | + const disabled = state.stored === "disable" || state.stored === "none" || state.enabled === false |
| 48 | + const canDisable = supportsEffort(state.supported, "disable") |
| 49 | + if (disabled && canDisable) return undefined |
| 50 | + |
| 51 | + const candidates = [disabled ? undefined : state.stored, state.modelDefault] |
| 52 | + const supported = state.supported |
| 53 | + if (supported !== true && !supported.includes("disable")) { |
| 54 | + candidates.push(canonicalEfforts.find((effort) => supported.includes(effort))) |
| 55 | + } |
| 56 | + |
| 57 | + for (const effort of candidates) { |
| 58 | + if ( |
| 59 | + effort && |
| 60 | + effort !== "disable" && |
| 61 | + effort !== "none" && |
| 62 | + effort !== "minimal" && |
| 63 | + supportsEffort(state.supported, effort) |
| 64 | + ) { |
| 65 | + return effort |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return state.required && state.modelDefault && state.modelDefault !== "none" ? state.modelDefault : undefined |
| 70 | +} |
| 71 | + |
| 72 | +let checked = 0 |
| 73 | +for (const supported of supportedSets) { |
| 74 | + for (const required of [false, true]) { |
| 75 | + for (const modelDefault of values) { |
| 76 | + for (const stored of values) { |
| 77 | + for (const enabled of enabledValues) { |
| 78 | + const state: ModelState = { supported, required, modelDefault, stored, enabled } |
| 79 | + const selection = resolveSelection(state) |
| 80 | + const available = availableOptions(state) |
| 81 | + assert.ok(available.length === 0 || available.includes(selection), "selection must be supported") |
| 82 | + if (!available.includes("disable")) |
| 83 | + assert.notEqual(selection, "disable", "required reasoning cannot disable") |
| 84 | + |
| 85 | + const normalized: ModelState = { |
| 86 | + ...state, |
| 87 | + stored: selection, |
| 88 | + enabled: required || selection !== "disable", |
| 89 | + } |
| 90 | + const request = resolveRequest(normalized) |
| 91 | + if (selection === "disable") { |
| 92 | + assert.equal(request, undefined, "an explicit supported disable must omit reasoning") |
| 93 | + } else if (canonicalEfforts.includes(selection as (typeof canonicalEfforts)[number])) { |
| 94 | + assert.equal(request, selection, "a normalized effort must reach the request") |
| 95 | + } |
| 96 | + |
| 97 | + checked++ |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +console.log(`Reasoning defaults model check passed (${checked} states)`) |
0 commit comments