Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

model.ts

225 lines8.4 KB

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