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

model.ts

191 lines6.9 KB

1import { Effect, Schema, SchemaTransformation } from "effect";
2import { TrimmedNonEmptyString } from "./baseSchemas.ts";
3import type { ProviderKind } from "./orchestration.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;
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;
122​
123export const ModelCapabilities = Schema.Struct({
124 optionDescriptors: Schema.optional(Schema.Array(ProviderOptionDescriptor)),
125});
126export type ModelCapabilities = typeof ModelCapabilities.Type;
127​
128export const DEFAULT_MODEL_BY_PROVIDER: Record<ProviderKind, string> = {
129 codex: "gpt-5.4",
130 claudeAgent: "claude-sonnet-4-6",
131 cursor: "auto",
132 opencode: "openai/gpt-5",
133};
134​
135export const DEFAULT_MODEL = DEFAULT_MODEL_BY_PROVIDER.codex;
136​
137/** Per-provider text generation model defaults. */
138export const DEFAULT_GIT_TEXT_GENERATION_MODEL_BY_PROVIDER: Record<ProviderKind, string> = {
139 codex: "gpt-5.4-mini",
140 claudeAgent: "claude-haiku-4-5",
141 cursor: "composer-2",
142 opencode: "openai/gpt-5",
143};
144​
145export const MODEL_SLUG_ALIASES_BY_PROVIDER: Record<ProviderKind, Record<string, string>> = {
146 codex: {
147 "gpt-5-codex": "gpt-5.4",
148 "5.4": "gpt-5.4",
149 "5.3": "gpt-5.3-codex",
150 "gpt-5.3": "gpt-5.3-codex",
151 "5.3-spark": "gpt-5.3-codex-spark",
152 "gpt-5.3-spark": "gpt-5.3-codex-spark",
153 },
154 claudeAgent: {
155 opus: "claude-opus-4-7",
156 "opus-4.7": "claude-opus-4-7",
157 "claude-opus-4.7": "claude-opus-4-7",
158 "opus-4.6": "claude-opus-4-6",
159 "claude-opus-4.6": "claude-opus-4-6",
160 "claude-opus-4-6-20251117": "claude-opus-4-6",
161 sonnet: "claude-sonnet-4-6",
162 "sonnet-4.6": "claude-sonnet-4-6",
163 "claude-sonnet-4.6": "claude-sonnet-4-6",
164 "claude-sonnet-4-6-20251117": "claude-sonnet-4-6",
165 haiku: "claude-haiku-4-5",
166 "haiku-4.5": "claude-haiku-4-5",
167 "claude-haiku-4.5": "claude-haiku-4-5",
168 "claude-haiku-4-5-20251001": "claude-haiku-4-5",
169 },
170 cursor: {
171 composer: "composer-2",
172 "composer-1.5": "composer-1.5",
173 "composer-1": "composer-1.5",
174 "opus-4.6-thinking": "claude-opus-4-6",
175 "opus-4.6": "claude-opus-4-6",
176 "sonnet-4.6-thinking": "claude-sonnet-4-6",
177 "sonnet-4.6": "claude-sonnet-4-6",
178 "opus-4.5-thinking": "claude-opus-4-5",
179 "opus-4.5": "claude-opus-4-5",
180 },
181 opencode: {},
182};
183​
184// ── Provider display names ────────────────────────────────────────────
185​
186export const PROVIDER_DISPLAY_NAMES: Record<ProviderKind, string> = {
187 codex: "Codex",
188 claudeAgent: "Claude",
189 cursor: "Cursor",
190 opencode: "OpenCode",
191};