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

settings.ts

503 lines19.8 KB

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