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

settings.ts

486 lines19.0 KB

1import { Effect } from "effect";
2import * as Schema from "effect/Schema";
3import * as SchemaTransformation from "effect/SchemaTransformation";
4import { TrimmedNonEmptyString, TrimmedString } from "./baseSchemas.ts";
5import { DEFAULT_GIT_TEXT_GENERATION_MODEL, ProviderOptionSelections } from "./model.ts";
6import { ModelSelection } from "./orchestration.ts";
7import { ProviderInstanceConfig, ProviderInstanceId } from "./providerInstance.ts";
8​
9// ── Client Settings (local-only) ───────────────────────────────
10​
11export const TimestampFormat = Schema.Literals(["locale", "12-hour", "24-hour"]);
12export type TimestampFormat = typeof TimestampFormat.Type;
13export const DEFAULT_TIMESTAMP_FORMAT: TimestampFormat = "locale";
14​
15export const SidebarProjectSortOrder = Schema.Literals(["updated_at", "created_at", "manual"]);
16export type SidebarProjectSortOrder = typeof SidebarProjectSortOrder.Type;
17export const DEFAULT_SIDEBAR_PROJECT_SORT_ORDER: SidebarProjectSortOrder = "updated_at";
18​
19export const SidebarThreadSortOrder = Schema.Literals(["updated_at", "created_at"]);
20export type SidebarThreadSortOrder = typeof SidebarThreadSortOrder.Type;
21export const DEFAULT_SIDEBAR_THREAD_SORT_ORDER: SidebarThreadSortOrder = "updated_at";
22​
23export const SidebarProjectGroupingMode = Schema.Literals([
24 "repository",
25 "repository_path",
26 "separate",
27]);
28export type SidebarProjectGroupingMode = typeof SidebarProjectGroupingMode.Type;
29export const DEFAULT_SIDEBAR_PROJECT_GROUPING_MODE: SidebarProjectGroupingMode = "repository";
30​
31export const ClientSettingsSchema = Schema.Struct({
32 autoOpenPlanSidebar: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(true))),
33 confirmThreadArchive: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))),
34 confirmThreadDelete: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(true))),
35 diffIgnoreWhitespace: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(true))),
36 diffWordWrap: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))),
37 // Model favorites. Historically keyed by provider kind, now
38 // widened to `ProviderInstanceId` so users can favorite a specific model
39 // on a custom provider instance (e.g. "Codex Personal · gpt-5") without
40 // the UI collapsing it into the same bucket as the default Codex. The
41 // widening is backward-compatible by construction: prior provider-kind
42 // strings satisfy the `ProviderInstanceId` slug schema, so previously
43 // persisted favorites decode unchanged and continue to point at the
44 // default instance for their kind (because `defaultInstanceIdForDriver(kind)`
45 // uses the same slug). The field name is kept as `provider` for storage
46 // stability; new call sites should treat the value as an instance id.
47 favorites: Schema.Array(
48 Schema.Struct({
49 provider: ProviderInstanceId,
50 model: TrimmedNonEmptyString,
51 }),
52 ).pipe(Schema.withDecodingDefault(Effect.succeed([]))),
53 providerModelPreferences: Schema.Record(
54 ProviderInstanceId,
55 Schema.Struct({
56 hiddenModels: Schema.Array(Schema.String).pipe(
57 Schema.withDecodingDefault(Effect.succeed([])),
58 ),
59 modelOrder: Schema.Array(Schema.String).pipe(Schema.withDecodingDefault(Effect.succeed([]))),
60 }),
61 ).pipe(Schema.withDecodingDefault(Effect.succeed({}))),
62 sidebarProjectGroupingMode: SidebarProjectGroupingMode.pipe(
63 Schema.withDecodingDefault(Effect.succeed(DEFAULT_SIDEBAR_PROJECT_GROUPING_MODE)),
64 ),
65 sidebarProjectGroupingOverrides: Schema.Record(
66 TrimmedNonEmptyString,
67 SidebarProjectGroupingMode,
68 ).pipe(Schema.withDecodingDefault(Effect.succeed({}))),
69 sidebarProjectSortOrder: SidebarProjectSortOrder.pipe(
70 Schema.withDecodingDefault(Effect.succeed(DEFAULT_SIDEBAR_PROJECT_SORT_ORDER)),
71 ),
72 sidebarThreadSortOrder: SidebarThreadSortOrder.pipe(
73 Schema.withDecodingDefault(Effect.succeed(DEFAULT_SIDEBAR_THREAD_SORT_ORDER)),
74 ),
75 timestampFormat: TimestampFormat.pipe(
76 Schema.withDecodingDefault(Effect.succeed(DEFAULT_TIMESTAMP_FORMAT)),
77 ),
78});
79export type ClientSettings = typeof ClientSettingsSchema.Type;
80​
81export const DEFAULT_CLIENT_SETTINGS: ClientSettings = Schema.decodeSync(ClientSettingsSchema)({});
82​
83// ── Server Settings (server-authoritative) ────────────────────
84​
85export const ThreadEnvMode = Schema.Literals(["local", "worktree"]);
86export type ThreadEnvMode = typeof ThreadEnvMode.Type;
87​
88const makeBinaryPathSetting = (fallback: string) =>
89 TrimmedString.pipe(
90 Schema.decodeTo(
91 Schema.String,
92 SchemaTransformation.transformOrFail({
93 decode: (value) => Effect.succeed(value || fallback),
94 encode: (value) => Effect.succeed(value),
95 }),
96 ),
97 Schema.withDecodingDefault(Effect.succeed(fallback)),
98 );
99​
100export type ProviderSettingsFormControl = "text" | "password" | "textarea" | "switch";
101​
102export interface ProviderSettingsFormAnnotation {
103 readonly control?: ProviderSettingsFormControl | undefined;
104 readonly placeholder?: string | undefined;
105 readonly hidden?: boolean | undefined;
106 readonly clearWhenEmpty?: "omit" | "persist" | undefined;
108​
109export interface ProviderSettingsFormSchemaAnnotation {
110 readonly order?: readonly string[] | undefined;
112​
113declare module "effect/Schema" {
114 namespace Annotations {
115 interface Annotations {
116 readonly providerSettingsForm?: ProviderSettingsFormAnnotation | undefined;
117 readonly providerSettingsFormSchema?: ProviderSettingsFormSchemaAnnotation | undefined;
118 }
119 }
121​
122export type ProviderSettingsOrder<Fields extends Schema.Struct.Fields> = readonly Extract<
123 keyof Fields,
124 string
125>[];
126​
127export function makeProviderSettingsSchema<const Fields extends Schema.Struct.Fields>(
128 fields: Fields,
129 options?: {
130 readonly order?: ProviderSettingsOrder<Fields> | undefined;
131 },
132): Schema.Struct<Fields> {
133 return Schema.Struct(fields).pipe(
134 Schema.annotate({
135 providerSettingsFormSchema:
136 options?.order === undefined ? undefined : { order: options.order },
137 }),
138 );
140​
141export const CodexSettings = makeProviderSettingsSchema(
142 {
143 enabled: Schema.Boolean.pipe(
144 Schema.withDecodingDefault(Effect.succeed(true)),
145 Schema.annotateKey({ providerSettingsForm: { hidden: true } }),
146 ),
147 binaryPath: makeBinaryPathSetting("codex").pipe(
148 Schema.annotateKey({
149 title: "Binary path",
150 description: "Path to the Codex binary used by this instance.",
151 providerSettingsForm: { placeholder: "codex", clearWhenEmpty: "omit" },
152 }),
153 ),
154 homePath: TrimmedString.pipe(
155 Schema.withDecodingDefault(Effect.succeed("")),
156 Schema.annotateKey({
157 title: "CODEX_HOME path",
158 description: "Custom Codex home and config directory.",
159 providerSettingsForm: {
160 placeholder: "~/.codex",
161 clearWhenEmpty: "omit",
162 },
163 }),
164 ),
165 shadowHomePath: TrimmedString.pipe(
166 Schema.withDecodingDefault(Effect.succeed("")),
167 Schema.annotateKey({
168 title: "Shadow home path",
169 description:
170 "Account-specific Codex home. Keeps auth.json separate while sharing state from CODEX_HOME.",
171 providerSettingsForm: {
172 placeholder: "~/.codex-t3/personal",
173 clearWhenEmpty: "omit",
174 },
175 }),
176 ),
177 customModels: Schema.Array(Schema.String).pipe(
178 Schema.withDecodingDefault(Effect.succeed([])),
179 Schema.annotateKey({ providerSettingsForm: { hidden: true } }),
180 ),
181 },
182 {
183 order: ["binaryPath", "homePath", "shadowHomePath"],
184 },
185);
186export type CodexSettings = typeof CodexSettings.Type;
187​
188export const ClaudeSettings = makeProviderSettingsSchema(
189 {
190 enabled: Schema.Boolean.pipe(
191 Schema.withDecodingDefault(Effect.succeed(true)),
192 Schema.annotateKey({ providerSettingsForm: { hidden: true } }),
193 ),
194 binaryPath: makeBinaryPathSetting("claude").pipe(
195 Schema.annotateKey({
196 title: "Binary path",
197 description: "Path to the Claude binary used by this instance.",
198 providerSettingsForm: { placeholder: "claude", clearWhenEmpty: "omit" },
199 }),
200 ),
201 homePath: TrimmedString.pipe(
202 Schema.withDecodingDefault(Effect.succeed("")),
203 Schema.annotateKey({
204 title: "Claude HOME path",
205 description:
206 "Custom HOME used when running this Claude instance. Keeps .claude.json and .claude separate.",
207 providerSettingsForm: { placeholder: "~", clearWhenEmpty: "omit" },
208 }),
209 ),
210 customModels: Schema.Array(Schema.String).pipe(
211 Schema.withDecodingDefault(Effect.succeed([])),
212 Schema.annotateKey({ providerSettingsForm: { hidden: true } }),
213 ),
214 launchArgs: Schema.String.pipe(
215 Schema.withDecodingDefault(Effect.succeed("")),
216 Schema.annotateKey({
217 title: "Launch arguments",
218 description: "Additional CLI arguments passed on session start.",
219 providerSettingsForm: {
220 placeholder: "e.g. --chrome",
221 clearWhenEmpty: "omit",
222 },
223 }),
224 ),
225 },
226 {
227 order: ["binaryPath", "homePath", "launchArgs"],
228 },
229);
230export type ClaudeSettings = typeof ClaudeSettings.Type;
231​
232export const CursorSettings = makeProviderSettingsSchema(
233 {
234 enabled: Schema.Boolean.pipe(
235 Schema.withDecodingDefault(Effect.succeed(false)),
236 Schema.annotateKey({ providerSettingsForm: { hidden: true } }),
237 ),
238 binaryPath: makeBinaryPathSetting("agent").pipe(
239 Schema.annotateKey({
240 title: "Binary path",
241 description: "Path to the Cursor agent binary.",
242 providerSettingsForm: { placeholder: "agent", clearWhenEmpty: "omit" },
243 }),
244 ),
245 apiEndpoint: TrimmedString.pipe(
246 Schema.withDecodingDefault(Effect.succeed("")),
247 Schema.annotateKey({
248 title: "API endpoint",
249 description: "Override the Cursor API endpoint for this instance.",
250 providerSettingsForm: {
251 placeholder: "https://...",
252 clearWhenEmpty: "omit",
253 },
254 }),
255 ),
256 customModels: Schema.Array(Schema.String).pipe(
257 Schema.withDecodingDefault(Effect.succeed([])),
258 Schema.annotateKey({ providerSettingsForm: { hidden: true } }),
259 ),
260 },
261 {
262 order: ["binaryPath", "apiEndpoint"],
263 },
264);
265export type CursorSettings = typeof CursorSettings.Type;
266export const OpenCodeSettings = makeProviderSettingsSchema(
267 {
268 enabled: Schema.Boolean.pipe(
269 Schema.withDecodingDefault(Effect.succeed(true)),
270 Schema.annotateKey({ providerSettingsForm: { hidden: true } }),
271 ),
272 binaryPath: makeBinaryPathSetting("opencode").pipe(
273 Schema.annotateKey({
274 title: "Binary path",
275 description: "Path to the OpenCode binary.",
276 providerSettingsForm: {
277 placeholder: "opencode",
278 clearWhenEmpty: "omit",
279 },
280 }),
281 ),
282 serverUrl: TrimmedString.pipe(
283 Schema.withDecodingDefault(Effect.succeed("")),
284 Schema.annotateKey({
285 title: "Server URL",
286 description: "Leave blank to let T3 Code spawn the server when needed.",
287 providerSettingsForm: {
288 placeholder: "http://127.0.0.1:4096",
289 clearWhenEmpty: "omit",
290 },
291 }),
292 ),
293 serverPassword: TrimmedString.pipe(
294 Schema.withDecodingDefault(Effect.succeed("")),
295 Schema.annotateKey({
296 title: "Server password",
297 description: "Stored in plain text on disk.",
298 providerSettingsForm: {
299 control: "password",
300 placeholder: "Optional",
301 clearWhenEmpty: "omit",
302 },
303 }),
304 ),
305 customModels: Schema.Array(Schema.String).pipe(
306 Schema.withDecodingDefault(Effect.succeed([])),
307 Schema.annotateKey({ providerSettingsForm: { hidden: true } }),
308 ),
309 },
310 {
311 order: ["binaryPath", "serverUrl", "serverPassword"],
312 },
313);
314export type OpenCodeSettings = typeof OpenCodeSettings.Type;
315​
316export const ObservabilitySettings = Schema.Struct({
317 otlpTracesUrl: TrimmedString.pipe(Schema.withDecodingDefault(Effect.succeed(""))),
318 otlpMetricsUrl: TrimmedString.pipe(Schema.withDecodingDefault(Effect.succeed(""))),
319});
320export type ObservabilitySettings = typeof ObservabilitySettings.Type;
321​
322export const ServerSettings = Schema.Struct({
323 enableAssistantStreaming: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))),
324 defaultThreadEnvMode: ThreadEnvMode.pipe(
325 Schema.withDecodingDefault(Effect.succeed("local" as const satisfies ThreadEnvMode)),
326 ),
327 addProjectBaseDirectory: TrimmedString.pipe(Schema.withDecodingDefault(Effect.succeed(""))),
328 textGenerationModelSelection: ModelSelection.pipe(
329 Schema.withDecodingDefault(
330 Effect.succeed({
331 instanceId: ProviderInstanceId.make("codex"),
332 model: DEFAULT_GIT_TEXT_GENERATION_MODEL,
333 }),
334 ),
335 ),
336​
337 // Legacy single-instance-per-driver settings. Continues to be the source
338 // of truth until `providerInstances` (below) lands per-driver migration
339 // shims and the server starts hydrating instances from it. Driver-specific
340 // schemas live here for the duration of the migration; once each driver
341 // owns its config in its own package, this struct shrinks to nothing and
342 // is removed entirely.
343 providers: Schema.Struct({
344 codex: CodexSettings.pipe(Schema.withDecodingDefault(Effect.succeed({}))),
345 claudeAgent: ClaudeSettings.pipe(Schema.withDecodingDefault(Effect.succeed({}))),
346 cursor: CursorSettings.pipe(Schema.withDecodingDefault(Effect.succeed({}))),
347 opencode: OpenCodeSettings.pipe(Schema.withDecodingDefault(Effect.succeed({}))),
348 }).pipe(Schema.withDecodingDefault(Effect.succeed({}))),
349 // New driver-agnostic instance map. Keyed by `ProviderInstanceId`; values
350 // are `ProviderInstanceConfig` envelopes. The driver-specific config blob
351 // is `Schema.Unknown` at this layer so envelopes with unknown drivers
352 // (forks, downgrades, in-flight PR branches) round-trip without loss.
353 // See providerInstance.ts for the forward/backward compatibility invariant.
354 providerInstances: Schema.Record(ProviderInstanceId, ProviderInstanceConfig).pipe(
355 Schema.withDecodingDefault(Effect.succeed({})),
356 ),
357 observability: ObservabilitySettings.pipe(Schema.withDecodingDefault(Effect.succeed({}))),
358});
359export type ServerSettings = typeof ServerSettings.Type;
360​
361export const DEFAULT_SERVER_SETTINGS: ServerSettings = Schema.decodeSync(ServerSettings)({});
362​
363export class ServerSettingsError extends Schema.TaggedErrorClass<ServerSettingsError>()(
364 "ServerSettingsError",
365 {
366 settingsPath: Schema.String,
367 detail: Schema.String,
368 cause: Schema.optional(Schema.Defect),
369 },
370) {
371 override get message(): string {
372 return `Server settings error at ${this.settingsPath}: ${this.detail}`;
373 }
375​
376// ── Unified type ─────────────────────────────────────────────────────
377​
378export type UnifiedSettings = ServerSettings & ClientSettings;
379export const DEFAULT_UNIFIED_SETTINGS: UnifiedSettings = {
380 ...DEFAULT_SERVER_SETTINGS,
381 ...DEFAULT_CLIENT_SETTINGS,
382};
383​
384// ── Server Settings Patch (replace with a Schema.deepPartial if available) ──────────────────────────────────────────
385​
386const ModelSelectionPatch = Schema.Struct({
387 instanceId: Schema.optionalKey(ProviderInstanceId),
388 model: Schema.optionalKey(TrimmedNonEmptyString),
389 options: Schema.optionalKey(ProviderOptionSelections),
390});
391​
392const CodexSettingsPatch = Schema.Struct({
393 enabled: Schema.optionalKey(Schema.Boolean),
394 binaryPath: Schema.optionalKey(Schema.String),
395 homePath: Schema.optionalKey(Schema.String),
396 shadowHomePath: Schema.optionalKey(Schema.String),
397 customModels: Schema.optionalKey(Schema.Array(Schema.String)),
398});
399​
400const ClaudeSettingsPatch = Schema.Struct({
401 enabled: Schema.optionalKey(Schema.Boolean),
402 binaryPath: Schema.optionalKey(Schema.String),
403 homePath: Schema.optionalKey(Schema.String),
404 customModels: Schema.optionalKey(Schema.Array(Schema.String)),
405 launchArgs: Schema.optionalKey(Schema.String),
406});
407​
408const CursorSettingsPatch = Schema.Struct({
409 enabled: Schema.optionalKey(Schema.Boolean),
410 binaryPath: Schema.optionalKey(Schema.String),
411 apiEndpoint: Schema.optionalKey(Schema.String),
412 customModels: Schema.optionalKey(Schema.Array(Schema.String)),
413});
414​
415const OpenCodeSettingsPatch = Schema.Struct({
416 enabled: Schema.optionalKey(Schema.Boolean),
417 binaryPath: Schema.optionalKey(Schema.String),
418 serverUrl: Schema.optionalKey(Schema.String),
419 serverPassword: Schema.optionalKey(Schema.String),
420 customModels: Schema.optionalKey(Schema.Array(Schema.String)),
421});
422​
423export const ServerSettingsPatch = Schema.Struct({
424 // Server settings
425 enableAssistantStreaming: Schema.optionalKey(Schema.Boolean),
426 defaultThreadEnvMode: Schema.optionalKey(ThreadEnvMode),
427 addProjectBaseDirectory: Schema.optionalKey(Schema.String),
428 textGenerationModelSelection: Schema.optionalKey(ModelSelectionPatch),
429 observability: Schema.optionalKey(
430 Schema.Struct({
431 otlpTracesUrl: Schema.optionalKey(Schema.String),
432 otlpMetricsUrl: Schema.optionalKey(Schema.String),
433 }),
434 ),
435 providers: Schema.optionalKey(
436 Schema.Struct({
437 codex: Schema.optionalKey(CodexSettingsPatch),
438 claudeAgent: Schema.optionalKey(ClaudeSettingsPatch),
439 cursor: Schema.optionalKey(CursorSettingsPatch),
440 opencode: Schema.optionalKey(OpenCodeSettingsPatch),
441 }),
442 ),
443 // Whole-map replacement for the new instance config. Patching individual
444 // entries is intentionally out of scope: the map is small, and partial
445 // patches risk leaving driver-specific config in a half-merged state.
446 // The web UI sends a fully-formed map every time it edits this field.
447 providerInstances: Schema.optionalKey(Schema.Record(ProviderInstanceId, ProviderInstanceConfig)),
448});
449export type ServerSettingsPatch = typeof ServerSettingsPatch.Type;
450​
451export const ClientSettingsPatch = Schema.Struct({
452 autoOpenPlanSidebar: Schema.optionalKey(Schema.Boolean),
453 confirmThreadArchive: Schema.optionalKey(Schema.Boolean),
454 confirmThreadDelete: Schema.optionalKey(Schema.Boolean),
455 diffIgnoreWhitespace: Schema.optionalKey(Schema.Boolean),
456 diffWordWrap: Schema.optionalKey(Schema.Boolean),
457 favorites: Schema.optionalKey(
458 Schema.Array(
459 Schema.Struct({
460 provider: ProviderInstanceId,
461 model: TrimmedNonEmptyString,
462 }),
463 ),
464 ),
465 providerModelPreferences: Schema.optionalKey(
466 Schema.Record(
467 ProviderInstanceId,
468 Schema.Struct({
469 hiddenModels: Schema.Array(Schema.String).pipe(
470 Schema.withDecodingDefault(Effect.succeed([])),
471 ),
472 modelOrder: Schema.Array(Schema.String).pipe(
473 Schema.withDecodingDefault(Effect.succeed([])),
474 ),
475 }),
476 ),
477 ),
478 sidebarProjectGroupingMode: Schema.optionalKey(SidebarProjectGroupingMode),
479 sidebarProjectGroupingOverrides: Schema.optionalKey(
480 Schema.Record(TrimmedNonEmptyString, SidebarProjectGroupingMode),
481 ),
482 sidebarProjectSortOrder: Schema.optionalKey(SidebarProjectSortOrder),
483 sidebarThreadSortOrder: Schema.optionalKey(SidebarThreadSortOrder),
484 timestampFormat: Schema.optionalKey(TimestampFormat),
485});
486export type ClientSettingsPatch = typeof ClientSettingsPatch.Type;