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

model.ts

211 lines8.0 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.4";
137export const DEFAULT_GIT_TEXT_GENERATION_MODEL = "gpt-5.4-mini";
138​
139export const DEFAULT_MODEL_BY_PROVIDER: Partial<Record<ProviderDriverKind, string>> = {
140 [CODEX_DRIVER_KIND]: DEFAULT_MODEL,
141 [CLAUDE_DRIVER_KIND]: "claude-sonnet-5",
142 [CURSOR_DRIVER_KIND]: "auto",
143 [GROK_DRIVER_KIND]: "grok-build",
144 [OPENCODE_DRIVER_KIND]: "openai/gpt-5",
145};
146​
147/** Per-provider text generation model defaults. */
148export const DEFAULT_GIT_TEXT_GENERATION_MODEL_BY_PROVIDER: Partial<
149 Record<ProviderDriverKind, string>
150> = {
151 [CODEX_DRIVER_KIND]: DEFAULT_GIT_TEXT_GENERATION_MODEL,
152 [CLAUDE_DRIVER_KIND]: "claude-haiku-4-5",
153 [CURSOR_DRIVER_KIND]: "composer-2",
154 [OPENCODE_DRIVER_KIND]: "openai/gpt-5",
155};
156​
157export const MODEL_SLUG_ALIASES_BY_PROVIDER: Partial<
158 Record<ProviderDriverKind, Record<string, string>>
159> = {
160 [CODEX_DRIVER_KIND]: {
161 "gpt-5-codex": "gpt-5.4",
162 "5.4": "gpt-5.4",
163 "5.3": "gpt-5.3-codex",
164 "gpt-5.3": "gpt-5.3-codex",
165 "5.3-spark": "gpt-5.3-codex-spark",
166 "gpt-5.3-spark": "gpt-5.3-codex-spark",
167 },
168 [CLAUDE_DRIVER_KIND]: {
169 opus: "claude-opus-4-8",
170 "opus-4.8": "claude-opus-4-8",
171 "claude-opus-4.8": "claude-opus-4-8",
172 "opus-4.7": "claude-opus-4-7",
173 "claude-opus-4.7": "claude-opus-4-7",
174 "opus-4.6": "claude-opus-4-6",
175 "claude-opus-4.6": "claude-opus-4-6",
176 "claude-opus-4-6-20251117": "claude-opus-4-6",
177 sonnet: "claude-sonnet-5",
178 "sonnet-5": "claude-sonnet-5",
179 "claude-sonnet-5.0": "claude-sonnet-5",
180 "claude-sonnet-5-0": "claude-sonnet-5",
181 "sonnet-4.6": "claude-sonnet-4-6",
182 "claude-sonnet-4.6": "claude-sonnet-4-6",
183 "claude-sonnet-4-6-20251117": "claude-sonnet-4-6",
184 haiku: "claude-haiku-4-5",
185 "haiku-4.5": "claude-haiku-4-5",
186 "claude-haiku-4.5": "claude-haiku-4-5",
187 "claude-haiku-4-5-20251001": "claude-haiku-4-5",
188 },
189 [CURSOR_DRIVER_KIND]: {
190 composer: "composer-2",
191 "composer-1.5": "composer-1.5",
192 "composer-1": "composer-1.5",
193 "opus-4.6-thinking": "claude-opus-4-6",
194 "opus-4.6": "claude-opus-4-6",
195 "sonnet-4.6-thinking": "claude-sonnet-4-6",
196 "sonnet-4.6": "claude-sonnet-4-6",
197 "opus-4.5-thinking": "claude-opus-4-5",
198 "opus-4.5": "claude-opus-4-5",
199 },
200 [OPENCODE_DRIVER_KIND]: {},
201};
202​
203// ── Provider display names ────────────────────────────────────────────
204​
205export const PROVIDER_DISPLAY_NAMES: Partial<Record<ProviderDriverKind, string>> = {
206 [CODEX_DRIVER_KIND]: "Codex",
207 [CLAUDE_DRIVER_KIND]: "Claude",
208 [CURSOR_DRIVER_KIND]: "Cursor",
209 [GROK_DRIVER_KIND]: "Grok",
210 [OPENCODE_DRIVER_KIND]: "OpenCode",
211};