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

model.ts

232 lines9.1 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.transformEffect({
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​
130/**
131 * A user-authored custom model. `name` and `capabilities` are optional so a
132 * bare slug keeps its driver-default presentation; when `capabilities` is
133 * set, its descriptors replace the driver default in the model picker.
134 */
135export const CustomModelEntry = Schema.Struct({
136 slug: TrimmedNonEmptyString,
137 name: Schema.optional(TrimmedNonEmptyString),
138 capabilities: Schema.optional(ModelCapabilities),
139});
140export type CustomModelEntry = typeof CustomModelEntry.Type;
141​
142/** On-disk custom model setting: the legacy bare slug, or a full entry. */
143export const CustomModelSetting = Schema.Union([Schema.String, CustomModelEntry]);
144export type CustomModelSetting = typeof CustomModelSetting.Type;
145​
146const CODEX_DRIVER_KIND = ProviderDriverKind.make("codex");
147const CLAUDE_DRIVER_KIND = ProviderDriverKind.make("claudeAgent");
148const CURSOR_DRIVER_KIND = ProviderDriverKind.make("cursor");
149const GROK_DRIVER_KIND = ProviderDriverKind.make("grok");
150const PI_DRIVER_KIND = ProviderDriverKind.make("pi");
151const ACP_REGISTRY_DRIVER_KIND = ProviderDriverKind.make("acpRegistry");
152const OPENCODE_DRIVER_KIND = ProviderDriverKind.make("opencode");
153​
154export const DEFAULT_MODEL = "gpt-6-astra";
155​
156/**
157 * Codex default-model preference, most preferred first. The provider snapshot
158 * marks the first of these present in the live `model/list` response as
159 * default; when none are available, Codex's own `isDefault` flag wins.
160 */
161export const PREFERRED_DEFAULT_CODEX_MODELS: ReadonlyArray<string> = [
162 DEFAULT_MODEL,
163 "gpt-5.6-sol",
164 "gpt-5.6-terra",
165];
166export const DEFAULT_TEXT_GENERATION_MODEL = "gpt-6-luna";
167/** Keep the official Antigravity session's current model. Never send this ID to ACP. */
168export const ANTIGRAVITY_DEFAULT_MODEL = "antigravity-default";
169export const DEFAULT_TEXT_GENERATION_REASONING_EFFORT = "low";
170​
171export const DEFAULT_MODEL_BY_PROVIDER: Partial<Record<ProviderDriverKind, string>> = {
172 [CODEX_DRIVER_KIND]: DEFAULT_MODEL,
173 [CLAUDE_DRIVER_KIND]: "claude-fable-5-1",
174 [CURSOR_DRIVER_KIND]: "auto",
175 // Product slug, not an ACP model id. The Grok adapter treats it as "the session's current model".
176 [GROK_DRIVER_KIND]: "grok-build",
177 [ACP_REGISTRY_DRIVER_KIND]: "default",
178 // "default" defers to the user's own Pi settings.json model selection.
179 [PI_DRIVER_KIND]: "default",
180 [OPENCODE_DRIVER_KIND]: "openai/gpt-5",
181 [ProviderDriverKind.make("antigravity")]: ANTIGRAVITY_DEFAULT_MODEL,
182};
183​
184/** Per-provider text generation model defaults. */
185export const DEFAULT_TEXT_GENERATION_MODEL_BY_PROVIDER: Partial<
186 Record<ProviderDriverKind, string>
187> = {
188 [CODEX_DRIVER_KIND]: DEFAULT_TEXT_GENERATION_MODEL,
189 [ProviderDriverKind.make("antigravity")]: ANTIGRAVITY_DEFAULT_MODEL,
190 [CLAUDE_DRIVER_KIND]: "claude-haiku-4-5",
191 [CURSOR_DRIVER_KIND]: "composer-2",
192 [OPENCODE_DRIVER_KIND]: "openai/gpt-5",
193};
194​
195export const MODEL_SLUG_ALIASES_BY_PROVIDER: Partial<
196 Record<ProviderDriverKind, Record<string, string>>
197> = {
198 [CODEX_DRIVER_KIND]: {
199 "gpt-5-codex": "gpt-5.4",
200 "5.4": "gpt-5.4",
201 "5.3": "gpt-5.3-codex",
202 "gpt-5.3": "gpt-5.3-codex",
203 "5.3-spark": "gpt-5.3-codex-spark",
204 "gpt-5.3-spark": "gpt-5.3-codex-spark",
205 },
206 [CLAUDE_DRIVER_KIND]: {},
207 [CURSOR_DRIVER_KIND]: {
208 composer: "composer-2",
209 "composer-1.5": "composer-1.5",
210 "composer-1": "composer-1.5",
211 "opus-4.6-thinking": "claude-opus-4-6",
212 "opus-4.6": "claude-opus-4-6",
213 "sonnet-4.6-thinking": "claude-sonnet-4-6",
214 "sonnet-4.6": "claude-sonnet-4-6",
215 "opus-4.5-thinking": "claude-opus-4-5",
216 "opus-4.5": "claude-opus-4-5",
217 },
218 [OPENCODE_DRIVER_KIND]: {},
219};
220​
221// ── Provider display names ────────────────────────────────────────────
222​
223export const PROVIDER_DISPLAY_NAMES: Partial<Record<ProviderDriverKind, string>> = {
224 [ProviderDriverKind.make("antigravity")]: "Antigravity",
225 [CODEX_DRIVER_KIND]: "Codex",
226 [CLAUDE_DRIVER_KIND]: "Claude",
227 [CURSOR_DRIVER_KIND]: "Cursor",
228 [GROK_DRIVER_KIND]: "Grok",
229 [ACP_REGISTRY_DRIVER_KIND]: "ACP Registry",
230 [PI_DRIVER_KIND]: "Pi",
231 [OPENCODE_DRIVER_KIND]: "OpenCode",
232};