Open History to see the last commit
1import { Effect, Schema, SchemaTransformation } from "effect";
2import { TrimmedNonEmptyString } from "./baseSchemas.ts";
3import { ProviderDriverKind } from "./providerInstance.ts";
4
5export const ProviderOptionDescriptorType = Schema.Literals(["select", "boolean"]);
6export type ProviderOptionDescriptorType = typeof ProviderOptionDescriptorType.Type;
7
8export const ProviderOptionChoice = Schema.Struct({
9 id: TrimmedNonEmptyString,
10 label: TrimmedNonEmptyString,
11 description: Schema.optional(TrimmedNonEmptyString),
12 isDefault: Schema.optional(Schema.Boolean),
13});
14export type ProviderOptionChoice = typeof ProviderOptionChoice.Type;
15
16const ProviderOptionDescriptorBase = {
17 id: TrimmedNonEmptyString,
18 label: TrimmedNonEmptyString,
19 description: Schema.optional(TrimmedNonEmptyString),
20} as const;
21
22export const SelectProviderOptionDescriptor = Schema.Struct({
23 ...ProviderOptionDescriptorBase,
24 type: Schema.Literal("select"),
25 options: Schema.Array(ProviderOptionChoice),
26 currentValue: Schema.optional(TrimmedNonEmptyString),
27 promptInjectedValues: Schema.optional(Schema.Array(TrimmedNonEmptyString)),
28});
29export type SelectProviderOptionDescriptor = typeof SelectProviderOptionDescriptor.Type;
30
31export const BooleanProviderOptionDescriptor = Schema.Struct({
32 ...ProviderOptionDescriptorBase,
33 type: Schema.Literal("boolean"),
34 currentValue: Schema.optional(Schema.Boolean),
35});
36export type BooleanProviderOptionDescriptor = typeof BooleanProviderOptionDescriptor.Type;
37
38export const ProviderOptionDescriptor = Schema.Union([
39 SelectProviderOptionDescriptor,
40 BooleanProviderOptionDescriptor,
41]);
42export type ProviderOptionDescriptor = typeof ProviderOptionDescriptor.Type;
43
44export const ProviderOptionSelectionValue = Schema.Union([TrimmedNonEmptyString, Schema.Boolean]);
45export type ProviderOptionSelectionValue = typeof ProviderOptionSelectionValue.Type;
46
47export const ProviderOptionSelection = Schema.Struct({
48 id: TrimmedNonEmptyString,
49 value: ProviderOptionSelectionValue,
50});
51export type ProviderOptionSelection = typeof ProviderOptionSelection.Type;
52
53/**
54 * Legacy on-disk shape for provider option selections, kept readable by the
55 * decoder so we can tolerate stored data written before the v3 array shape.
56 *
57 * Persisted historically as `{ effort: "max", fastMode: true, ... }` inside
58 * `modelSelection.options`. Migration 026 rewrites stored rows to the
59 * canonical array shape, but we still see the legacy form in:
60 * - `settings.json` files from older client builds,
61 * - SQLite databases that have not yet run migration 026,
62 * - any future regression that re-introduces the legacy shape.
63 */
64const LegacyProviderOptionSelectionsObject = Schema.Record(Schema.String, Schema.Unknown);
65
66const ProviderOptionSelectionsFromLegacyObject = LegacyProviderOptionSelectionsObject.pipe(
67 Schema.decodeTo(
68 Schema.Array(ProviderOptionSelection),
69 SchemaTransformation.transformOrFail({
70 decode: (record) => Effect.succeed(coerceLegacyOptionsObjectToArray(record)),
71 encode: (selections) => Effect.succeed(canonicalSelectionsToLegacyObject(selections)),
72 }),
73 ),
74);
75
76/**
77 * Schema for the `options` field of every `ModelSelection` variant.
78 *
79 * Accepts both:
80 * - the canonical array shape `Array<{ id, value }>` (preferred), and
81 * - the legacy object shape `Record<string, string | boolean | …>` from
82 * pre-migration data.
83 *
84 * Always normalizes to the canonical array on decode and re-encodes as the
85 * canonical array, so any legacy storage gets cleaned up the next time the
86 * containing record is written back.
87 */
88export const ProviderOptionSelections = Schema.Union([
89 Schema.Array(ProviderOptionSelection),
90 ProviderOptionSelectionsFromLegacyObject,
91]);
92export type ProviderOptionSelections = typeof ProviderOptionSelections.Type;
93
94function coerceLegacyOptionsObjectToArray(
95 record: Record<string, unknown>,
96): ReadonlyArray<ProviderOptionSelection> {
97 const entries: Array<ProviderOptionSelection> = [];
98 for (const [rawKey, rawValue] of Object.entries(record)) {
99 const id = typeof rawKey === "string" ? rawKey.trim() : "";
100 if (!id) continue;
101 if (typeof rawValue === "string") {
102 const trimmed = rawValue.trim();
103 if (trimmed) entries.push({ id, value: trimmed });
104 } else if (typeof rawValue === "boolean") {
105 entries.push({ id, value: rawValue });
106 }
107 // Drop anything else (numbers, null, nested objects/arrays) to match the
108 // permissive normalization performed by migration 026.
109 }
110 return entries;
111}
112
113function canonicalSelectionsToLegacyObject(
114 selections: ReadonlyArray<ProviderOptionSelection>,
115): Record<string, string | boolean> {
116 const out: Record<string, string | boolean> = {};
117 for (const { id, value } of selections) {
118 out[id] = value;
119 }
120 return out;
121}
122
123export const ModelCapabilities = Schema.Struct({
124 optionDescriptors: Schema.optional(Schema.Array(ProviderOptionDescriptor)),
125});
126export type ModelCapabilities = typeof ModelCapabilities.Type;
127
128const CODEX_DRIVER_KIND = ProviderDriverKind.make("codex");
129const CLAUDE_DRIVER_KIND = ProviderDriverKind.make("claudeAgent");
130const CURSOR_DRIVER_KIND = ProviderDriverKind.make("cursor");
131const OPENCODE_DRIVER_KIND = ProviderDriverKind.make("opencode");
132
133export const DEFAULT_MODEL = "gpt-5.4";
134export const DEFAULT_GIT_TEXT_GENERATION_MODEL = "gpt-5.4-mini";
135
136export const DEFAULT_MODEL_BY_PROVIDER: Partial<Record<ProviderDriverKind, string>> = {
137 [CODEX_DRIVER_KIND]: DEFAULT_MODEL,
138 [CLAUDE_DRIVER_KIND]: "claude-sonnet-4-6",
139 [CURSOR_DRIVER_KIND]: "auto",
140 [OPENCODE_DRIVER_KIND]: "openai/gpt-5",
141};
142
143/** Per-provider text generation model defaults. */
144export const DEFAULT_GIT_TEXT_GENERATION_MODEL_BY_PROVIDER: Partial<
145 Record<ProviderDriverKind, string>
146> = {
147 [CODEX_DRIVER_KIND]: DEFAULT_GIT_TEXT_GENERATION_MODEL,
148 [CLAUDE_DRIVER_KIND]: "claude-haiku-4-5",
149 [CURSOR_DRIVER_KIND]: "composer-2",
150 [OPENCODE_DRIVER_KIND]: "openai/gpt-5",
151};
152
153export const MODEL_SLUG_ALIASES_BY_PROVIDER: Partial<
154 Record<ProviderDriverKind, Record<string, string>>
155> = {
156 [CODEX_DRIVER_KIND]: {
157 "gpt-5-codex": "gpt-5.4",
158 "5.4": "gpt-5.4",
159 "5.3": "gpt-5.3-codex",
160 "gpt-5.3": "gpt-5.3-codex",
161 "5.3-spark": "gpt-5.3-codex-spark",
162 "gpt-5.3-spark": "gpt-5.3-codex-spark",
163 },
164 [CLAUDE_DRIVER_KIND]: {
165 opus: "claude-opus-4-7",
166 "opus-4.7": "claude-opus-4-7",
167 "claude-opus-4.7": "claude-opus-4-7",
168 "opus-4.6": "claude-opus-4-6",
169 "claude-opus-4.6": "claude-opus-4-6",
170 "claude-opus-4-6-20251117": "claude-opus-4-6",
171 sonnet: "claude-sonnet-4-6",
172 "sonnet-4.6": "claude-sonnet-4-6",
173 "claude-sonnet-4.6": "claude-sonnet-4-6",
174 "claude-sonnet-4-6-20251117": "claude-sonnet-4-6",
175 haiku: "claude-haiku-4-5",
176 "haiku-4.5": "claude-haiku-4-5",
177 "claude-haiku-4.5": "claude-haiku-4-5",
178 "claude-haiku-4-5-20251001": "claude-haiku-4-5",
179 },
180 [CURSOR_DRIVER_KIND]: {
181 composer: "composer-2",
182 "composer-1.5": "composer-1.5",
183 "composer-1": "composer-1.5",
184 "opus-4.6-thinking": "claude-opus-4-6",
185 "opus-4.6": "claude-opus-4-6",
186 "sonnet-4.6-thinking": "claude-sonnet-4-6",
187 "sonnet-4.6": "claude-sonnet-4-6",
188 "opus-4.5-thinking": "claude-opus-4-5",
189 "opus-4.5": "claude-opus-4-5",
190 },
191 [OPENCODE_DRIVER_KIND]: {},
192};
193
194// ── Provider display names ────────────────────────────────────────────
195
196export const PROVIDER_DISPLAY_NAMES: Partial<Record<ProviderDriverKind, string>> = {
197 [CODEX_DRIVER_KIND]: "Codex",
198 [CLAUDE_DRIVER_KIND]: "Claude",
199 [CURSOR_DRIVER_KIND]: "Cursor",
200 [OPENCODE_DRIVER_KIND]: "OpenCode",
201};