Open History to see the last commit
1import * as Effect from "effect/Effect";
2import * as Duration from "effect/Duration";
3import * as Schema from "effect/Schema";
4import * as SchemaTransformation from "effect/SchemaTransformation";
5import { TrimmedNonEmptyString, TrimmedString } from "./baseSchemas.ts";
6import { DEFAULT_TEXT_GENERATION_MODEL, ProviderOptionSelections } from "./model.ts";
7import { ModelSelection } from "./orchestration.ts";
8import { ProviderInstanceConfig, ProviderInstanceId } from "./providerInstance.ts";
9
10// ── Client Settings (local-only) ───────────────────────────────
11
12export const TimestampFormat = Schema.Literals(["locale", "12-hour", "24-hour"]);
13export type TimestampFormat = typeof TimestampFormat.Type;
14export const DEFAULT_TIMESTAMP_FORMAT: TimestampFormat = "locale";
15
16export const SidebarProjectSortOrder = Schema.Literals(["updated_at", "created_at", "manual"]);
17export type SidebarProjectSortOrder = typeof SidebarProjectSortOrder.Type;
18export const DEFAULT_SIDEBAR_PROJECT_SORT_ORDER: SidebarProjectSortOrder = "updated_at";
19
20export const SidebarThreadSortOrder = Schema.Literals(["updated_at", "created_at"]);
21export type SidebarThreadSortOrder = typeof SidebarThreadSortOrder.Type;
22export const DEFAULT_SIDEBAR_THREAD_SORT_ORDER: SidebarThreadSortOrder = "updated_at";
23
24export const SidebarProjectGroupingMode = Schema.Literals([
25 "repository",
26 "repository_path",
27 "separate",
28]);
29export type SidebarProjectGroupingMode = typeof SidebarProjectGroupingMode.Type;
30export const DEFAULT_SIDEBAR_PROJECT_GROUPING_MODE: SidebarProjectGroupingMode = "repository";
31export const MIN_SIDEBAR_THREAD_PREVIEW_COUNT = 1;
32export const MAX_SIDEBAR_THREAD_PREVIEW_COUNT = 15;
33export const SidebarThreadPreviewCount = Schema.Int.check(
34 Schema.isBetween({
35 minimum: MIN_SIDEBAR_THREAD_PREVIEW_COUNT,
36 maximum: MAX_SIDEBAR_THREAD_PREVIEW_COUNT,
37 }),
38);
39export type SidebarThreadPreviewCount = typeof SidebarThreadPreviewCount.Type;
40export const DEFAULT_SIDEBAR_THREAD_PREVIEW_COUNT: SidebarThreadPreviewCount = 6;
41export const MIN_SIDEBAR_AUTO_SETTLE_AFTER_DAYS = 1;
42export const MAX_SIDEBAR_AUTO_SETTLE_AFTER_DAYS = 90;
43export const SidebarAutoSettleAfterDays = Schema.Number.check(
44 Schema.isBetween({
45 minimum: MIN_SIDEBAR_AUTO_SETTLE_AFTER_DAYS,
46 maximum: MAX_SIDEBAR_AUTO_SETTLE_AFTER_DAYS,
47 }),
48);
49export type SidebarAutoSettleAfterDays = typeof SidebarAutoSettleAfterDays.Type;
50export const DEFAULT_SIDEBAR_AUTO_SETTLE_AFTER_DAYS: SidebarAutoSettleAfterDays = 3;
51export const MIN_GLASS_OPACITY = 40;
52export const MAX_GLASS_OPACITY = 100;
53export const GlassOpacity = Schema.Int.check(
54 Schema.isBetween({
55 minimum: MIN_GLASS_OPACITY,
56 maximum: MAX_GLASS_OPACITY,
57 }),
58);
59export type GlassOpacity = typeof GlassOpacity.Type;
60export const DEFAULT_GLASS_OPACITY: GlassOpacity = 80;
61export const EnvironmentIdentificationMode = Schema.Literals(["artwork", "pill", "none"]);
62export type EnvironmentIdentificationMode = typeof EnvironmentIdentificationMode.Type;
63export const DEFAULT_ENVIRONMENT_IDENTIFICATION_MODE: EnvironmentIdentificationMode = "artwork";
64
65export const ClientSettingsSchema = Schema.Struct({
66 autoOpenPlanSidebar: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))),
67 confirmThreadArchive: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))),
68 confirmThreadDelete: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(true))),
69 dismissedProviderUpdateNotificationKeys: Schema.Array(TrimmedNonEmptyString).pipe(
70 Schema.withDecodingDefault(Effect.succeed([])),
71 ),
72 diffIgnoreWhitespace: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(true))),
73 environmentIdentificationMode: EnvironmentIdentificationMode.pipe(
74 Schema.withDecodingDefault(Effect.succeed(DEFAULT_ENVIRONMENT_IDENTIFICATION_MODE)),
75 ),
76 glassOpacity: GlassOpacity.pipe(
77 Schema.withDecodingDefault(Effect.succeed(DEFAULT_GLASS_OPACITY)),
78 ),
79 // Model favorites. Historically keyed by provider kind, now
80 // widened to `ProviderInstanceId` so users can favorite a specific model
81 // on a custom provider instance (e.g. "Codex Personal · gpt-5") without
82 // the UI collapsing it into the same bucket as the default Codex. The
83 // widening is backward-compatible by construction: prior provider-kind
84 // strings satisfy the `ProviderInstanceId` slug schema, so previously
85 // persisted favorites decode unchanged and continue to point at the
86 // default instance for their kind (because `defaultInstanceIdForDriver(kind)`
87 // uses the same slug). The field name is kept as `provider` for storage
88 // stability; new call sites should treat the value as an instance id.
89 favorites: Schema.Array(
90 Schema.Struct({
91 provider: ProviderInstanceId,
92 model: TrimmedNonEmptyString,
93 }),
94 ).pipe(Schema.withDecodingDefault(Effect.succeed([]))),
95 providerModelPreferences: Schema.Record(
96 ProviderInstanceId,
97 Schema.Struct({
98 hiddenModels: Schema.Array(Schema.String).pipe(
99 Schema.withDecodingDefault(Effect.succeed([])),
100 ),
101 modelOrder: Schema.Array(Schema.String).pipe(Schema.withDecodingDefault(Effect.succeed([]))),
102 }),
103 ).pipe(Schema.withDecodingDefault(Effect.succeed({}))),
104 sidebarAutoSettleAfterDays: Schema.NullOr(SidebarAutoSettleAfterDays).pipe(
105 Schema.withDecodingDefault(Effect.succeed(DEFAULT_SIDEBAR_AUTO_SETTLE_AFTER_DAYS)),
106 ),
107 sidebarProjectGroupingMode: SidebarProjectGroupingMode.pipe(
108 Schema.withDecodingDefault(Effect.succeed(DEFAULT_SIDEBAR_PROJECT_GROUPING_MODE)),
109 ),
110 sidebarProjectGroupingOverrides: Schema.Record(
111 TrimmedNonEmptyString,
112 SidebarProjectGroupingMode,
113 ).pipe(Schema.withDecodingDefault(Effect.succeed({}))),
114 sidebarProjectSortOrder: SidebarProjectSortOrder.pipe(
115 Schema.withDecodingDefault(Effect.succeed(DEFAULT_SIDEBAR_PROJECT_SORT_ORDER)),
116 ),
117 sidebarThreadSortOrder: SidebarThreadSortOrder.pipe(
118 Schema.withDecodingDefault(Effect.succeed(DEFAULT_SIDEBAR_THREAD_SORT_ORDER)),
119 ),
120 sidebarThreadPreviewCount: SidebarThreadPreviewCount.pipe(
121 Schema.withDecodingDefault(Effect.succeed(DEFAULT_SIDEBAR_THREAD_PREVIEW_COUNT)),
122 ),
123 sidebarV2Enabled: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))),
124 // Whether `sidebarV2Enabled` reflects an explicit choice in Settings → Beta.
125 // Client settings persist as a whole blob, so every user who has ever touched
126 // any setting already has `sidebarV2Enabled: false` stored — without this bit
127 // there is no way to tell that apart from "left alone", and a channel-derived
128 // default could never reach them. Mirrors `updateChannelConfiguredByUser`.
129 sidebarV2ConfiguredByUser: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))),
130 timestampFormat: TimestampFormat.pipe(
131 Schema.withDecodingDefault(Effect.succeed(DEFAULT_TIMESTAMP_FORMAT)),
132 ),
133 wordWrap: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(true))),
134});
135export type ClientSettings = typeof ClientSettingsSchema.Type;
136
137export const DEFAULT_CLIENT_SETTINGS: ClientSettings = Schema.decodeSync(ClientSettingsSchema)({});
138
139// ── Server Settings (server-authoritative) ────────────────────
140
141export const ThreadEnvMode = Schema.Literals(["local", "worktree"]);
142export type ThreadEnvMode = typeof ThreadEnvMode.Type;
143
144const makeBinaryPathSetting = (fallback: string) =>
145 TrimmedString.pipe(
146 Schema.decodeTo(
147 Schema.String,
148 SchemaTransformation.transformOrFail({
149 decode: (value) => Effect.succeed(value || fallback),
150 encode: (value) => Effect.succeed(value),
151 }),
152 ),
153 Schema.withDecodingDefault(Effect.succeed(fallback)),
154 );
155
156export type ProviderSettingsFormControl = "text" | "password" | "textarea" | "switch";
157
158export interface ProviderSettingsFormAnnotation {
159 readonly control?: ProviderSettingsFormControl | undefined;
160 readonly placeholder?: string | undefined;
161 readonly hidden?: boolean | undefined;
162 readonly clearWhenEmpty?: "omit" | "persist" | undefined;
163}
164
165export interface ProviderSettingsFormSchemaAnnotation {
166 readonly order?: readonly string[] | undefined;
167}
168
169declare module "effect/Schema" {
170 namespace Annotations {
171 interface Annotations {
172 readonly providerSettingsForm?: ProviderSettingsFormAnnotation | undefined;
173 readonly providerSettingsFormSchema?: ProviderSettingsFormSchemaAnnotation | undefined;
174 }
175 }
176}
177
178export type ProviderSettingsOrder<Fields extends Schema.Struct.Fields> = readonly Extract<
179 keyof Fields,
180 string
181>[];
182
183export function makeProviderSettingsSchema<const Fields extends Schema.Struct.Fields>(
184 fields: Fields,
185 options?: {
186 readonly order?: ProviderSettingsOrder<Fields> | undefined;
187 },
188): Schema.Struct<Fields> {
189 return Schema.Struct(fields).pipe(
190 Schema.annotate({
191 providerSettingsFormSchema:
192 options?.order === undefined ? undefined : { order: options.order },
193 }),
194 );
195}
196
197export const CodexSettings = makeProviderSettingsSchema(
198 {
199 enabled: Schema.Boolean.pipe(
200 Schema.withDecodingDefault(Effect.succeed(true)),
201 Schema.annotateKey({ providerSettingsForm: { hidden: true } }),
202 ),
203 binaryPath: makeBinaryPathSetting("codex").pipe(
204 Schema.annotateKey({
205 title: "Binary path",
206 description: "Path to the Codex binary used by this instance.",
207 providerSettingsForm: { placeholder: "codex", clearWhenEmpty: "omit" },
208 }),
209 ),
210 homePath: TrimmedString.pipe(
211 Schema.withDecodingDefault(Effect.succeed("")),
212 Schema.annotateKey({
213 title: "CODEX_HOME path",
214 description: "Custom Codex home and config directory.",
215 providerSettingsForm: {
216 placeholder: "~/.codex",
217 clearWhenEmpty: "omit",
218 },
219 }),
220 ),
221 shadowHomePath: TrimmedString.pipe(
222 Schema.withDecodingDefault(Effect.succeed("")),
223 Schema.annotateKey({
224 title: "Shadow home path",
225 description:
226 "Account-specific Codex home. Keeps auth.json separate while sharing state from CODEX_HOME.",
227 providerSettingsForm: {
228 placeholder: "~/.codex-t3/personal",
229 clearWhenEmpty: "omit",
230 },
231 }),
232 ),
233 launchArgs: TrimmedString.pipe(
234 Schema.withDecodingDefault(Effect.succeed("")),
235 Schema.annotateKey({
236 title: "Launch arguments",
237 description: "Additional CLI arguments passed to codex app-server on session start.",
238 }),
239 ),
240 customModels: Schema.Array(Schema.String).pipe(
241 Schema.withDecodingDefault(Effect.succeed([])),
242 Schema.annotateKey({ providerSettingsForm: { hidden: true } }),
243 ),
244 },
245 {
246 order: ["binaryPath", "homePath", "shadowHomePath", "launchArgs"],
247 },
248);
249export type CodexSettings = typeof CodexSettings.Type;
250
251export const ClaudeSettings = makeProviderSettingsSchema(
252 {
253 enabled: Schema.Boolean.pipe(
254 Schema.withDecodingDefault(Effect.succeed(true)),
255 Schema.annotateKey({ providerSettingsForm: { hidden: true } }),
256 ),
257 binaryPath: makeBinaryPathSetting("claude").pipe(
258 Schema.annotateKey({
259 title: "Binary path",
260 description: "Path to the Claude binary used by this instance.",
261 providerSettingsForm: { placeholder: "claude", clearWhenEmpty: "omit" },
262 }),
263 ),
264 homePath: TrimmedString.pipe(
265 Schema.withDecodingDefault(Effect.succeed("")),
266 Schema.annotateKey({
267 title: "CLAUDE_CONFIG_DIR path",
268 description:
269 "Custom Claude home and config directory. Keeps .claude.json and .claude separate.",
270 providerSettingsForm: { placeholder: "~/.claude", clearWhenEmpty: "omit" },
271 }),
272 ),
273 customModels: Schema.Array(Schema.String).pipe(
274 Schema.withDecodingDefault(Effect.succeed([])),
275 Schema.annotateKey({ providerSettingsForm: { hidden: true } }),
276 ),
277 launchArgs: Schema.String.pipe(
278 Schema.withDecodingDefault(Effect.succeed("")),
279 Schema.annotateKey({
280 title: "Launch arguments",
281 description: "Additional CLI arguments passed on session start.",
282 providerSettingsForm: {
283 placeholder: "e.g. --chrome",
284 clearWhenEmpty: "omit",
285 },
286 }),
287 ),
288 },
289 {
290 order: ["binaryPath", "homePath", "launchArgs"],
291 },
292);
293export type ClaudeSettings = typeof ClaudeSettings.Type;
294
295export const CursorSettings = makeProviderSettingsSchema(
296 {
297 enabled: Schema.Boolean.pipe(
298 Schema.withDecodingDefault(Effect.succeed(false)),
299 Schema.annotateKey({ providerSettingsForm: { hidden: true } }),
300 ),
301 binaryPath: makeBinaryPathSetting("cursor-agent").pipe(
302 Schema.annotateKey({
303 title: "Binary path",
304 description: "Path to the Cursor agent binary.",
305 providerSettingsForm: { placeholder: "cursor-agent", clearWhenEmpty: "omit" },
306 }),
307 ),
308 apiEndpoint: TrimmedString.pipe(
309 Schema.withDecodingDefault(Effect.succeed("")),
310 Schema.annotateKey({
311 title: "API endpoint",
312 description: "Override the Cursor API endpoint for this instance.",
313 providerSettingsForm: {
314 placeholder: "https://...",
315 clearWhenEmpty: "omit",
316 },
317 }),
318 ),
319 customModels: Schema.Array(Schema.String).pipe(
320 Schema.withDecodingDefault(Effect.succeed([])),
321 Schema.annotateKey({ providerSettingsForm: { hidden: true } }),
322 ),
323 },
324 {
325 order: ["binaryPath", "apiEndpoint"],
326 },
327);
328export type CursorSettings = typeof CursorSettings.Type;
329
330export const GrokSettings = makeProviderSettingsSchema(
331 {
332 enabled: Schema.Boolean.pipe(
333 Schema.withDecodingDefault(Effect.succeed(true)),
334 Schema.annotateKey({ providerSettingsForm: { hidden: true } }),
335 ),
336 binaryPath: makeBinaryPathSetting("grok").pipe(
337 Schema.annotateKey({
338 title: "Binary path",
339 description: "Path to the Grok CLI binary.",
340 providerSettingsForm: { placeholder: "grok", clearWhenEmpty: "omit" },
341 }),
342 ),
343 customModels: Schema.Array(Schema.String).pipe(
344 Schema.withDecodingDefault(Effect.succeed([])),
345 Schema.annotateKey({ providerSettingsForm: { hidden: true } }),
346 ),
347 },
348 {
349 order: ["binaryPath"],
350 },
351);
352export type GrokSettings = typeof GrokSettings.Type;
353
354export const OpenCodeSettings = makeProviderSettingsSchema(
355 {
356 enabled: Schema.Boolean.pipe(
357 Schema.withDecodingDefault(Effect.succeed(true)),
358 Schema.annotateKey({ providerSettingsForm: { hidden: true } }),
359 ),
360 binaryPath: makeBinaryPathSetting("opencode").pipe(
361 Schema.annotateKey({
362 title: "Binary path",
363 description: "Path to the OpenCode binary.",
364 providerSettingsForm: {
365 placeholder: "opencode",
366 clearWhenEmpty: "omit",
367 },
368 }),
369 ),
370 serverUrl: TrimmedString.pipe(
371 Schema.withDecodingDefault(Effect.succeed("")),
372 Schema.annotateKey({
373 title: "Server URL",
374 description: "Leave blank to let T3 Code spawn the server when needed.",
375 providerSettingsForm: {
376 placeholder: "http://127.0.0.1:4096",
377 clearWhenEmpty: "omit",
378 },
379 }),
380 ),
381 serverPassword: TrimmedString.pipe(
382 Schema.withDecodingDefault(Effect.succeed("")),
383 Schema.annotateKey({
384 title: "Server password",
385 description: "Stored in plain text on disk.",
386 providerSettingsForm: {
387 control: "password",
388 placeholder: "Optional",
389 clearWhenEmpty: "omit",
390 },
391 }),
392 ),
393 customModels: Schema.Array(Schema.String).pipe(
394 Schema.withDecodingDefault(Effect.succeed([])),
395 Schema.annotateKey({ providerSettingsForm: { hidden: true } }),
396 ),
397 },
398 {
399 order: ["binaryPath", "serverUrl", "serverPassword"],
400 },
401);
402export type OpenCodeSettings = typeof OpenCodeSettings.Type;
403
404export const ObservabilitySettings = Schema.Struct({
405 otlpTracesUrl: TrimmedString.pipe(Schema.withDecodingDefault(Effect.succeed(""))),
406 otlpMetricsUrl: TrimmedString.pipe(Schema.withDecodingDefault(Effect.succeed(""))),
407});
408export type ObservabilitySettings = typeof ObservabilitySettings.Type;
409
410export const SourceControlWritingStyleMode = Schema.Literals([
411 "repo_conventions",
412 "conventional_commits",
413 "custom",
414]);
415export type SourceControlWritingStyleMode = typeof SourceControlWritingStyleMode.Type;
416
417export const SourceControlWritingStyleSettings = Schema.Struct({
418 mode: SourceControlWritingStyleMode.pipe(
419 Schema.withDecodingDefault(Effect.succeed("repo_conventions" as const)),
420 ),
421 customInstructions: TrimmedString.pipe(Schema.withDecodingDefault(Effect.succeed(""))),
422 followChangeRequestTemplates: Schema.Boolean.pipe(
423 Schema.withDecodingDefault(Effect.succeed(true)),
424 ),
425});
426export type SourceControlWritingStyleSettings = typeof SourceControlWritingStyleSettings.Type;
427
428export const DEFAULT_AUTOMATIC_GIT_FETCH_INTERVAL = Duration.seconds(30);
429export const DEFAULT_PROVIDER_HEALTH_REFRESH_INTERVAL = Duration.minutes(5);
430
431export const BackgroundActivityProfile = Schema.Literals([
432 "balanced",
433 "performance",
434 "battery-saver",
435]);
436export type BackgroundActivityProfile = typeof BackgroundActivityProfile.Type;
437export const DEFAULT_BACKGROUND_ACTIVITY_PROFILE: BackgroundActivityProfile = "balanced";
438
439export const BackgroundActivityProfileSelection = Schema.Literals([
440 "balanced",
441 "performance",
442 "battery-saver",
443 "custom",
444]);
445export type BackgroundActivityProfileSelection = typeof BackgroundActivityProfileSelection.Type;
446
447export const BackgroundActivityOverrides = Schema.Struct({
448 automaticGitFetchInterval: Schema.optionalKey(Schema.DurationFromMillis),
449 providerHealthRefreshInterval: Schema.optionalKey(Schema.DurationFromMillis),
450 hostPowerMonitorActiveInterval: Schema.optionalKey(Schema.DurationFromMillis),
451 hostPowerMonitorIdleInterval: Schema.optionalKey(Schema.DurationFromMillis),
452 idleClientTtl: Schema.optionalKey(Schema.DurationFromMillis),
453 pauseWhenHostLocked: Schema.optionalKey(Schema.Boolean),
454 pauseWhenHostLowPower: Schema.optionalKey(Schema.Boolean),
455 pauseWhenClientLowPower: Schema.optionalKey(Schema.Boolean),
456 pauseWhenOnBattery: Schema.optionalKey(Schema.Boolean),
457});
458export type BackgroundActivityOverrides = typeof BackgroundActivityOverrides.Type;
459
460export const BackgroundActivitySettings = Schema.Struct({
461 schemaVersion: Schema.Literal(1).pipe(Schema.withDecodingDefault(Effect.succeed(1 as const))),
462 profile: BackgroundActivityProfileSelection.pipe(
463 Schema.withDecodingDefault(Effect.succeed(DEFAULT_BACKGROUND_ACTIVITY_PROFILE)),
464 ),
465 baseProfile: Schema.optionalKey(BackgroundActivityProfile),
466 overrides: BackgroundActivityOverrides.pipe(Schema.withDecodingDefault(Effect.succeed({}))),
467}).pipe(Schema.withDecodingDefault(Effect.succeed({})));
468export type BackgroundActivitySettings = typeof BackgroundActivitySettings.Type;
469
470export const ServerSettings = Schema.Struct({
471 enableAssistantStreaming: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))),
472 enableProviderUpdateChecks: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(true))),
473 backgroundActivity: BackgroundActivitySettings,
474 // Legacy flat fields retained for old settings files and old clients. New
475 // consumers should resolve `backgroundActivity` instead.
476 automaticGitFetchInterval: Schema.DurationFromMillis.pipe(
477 Schema.withDecodingDefault(
478 Effect.succeed(Duration.toMillis(DEFAULT_AUTOMATIC_GIT_FETCH_INTERVAL)),
479 ),
480 ),
481 providerHealthRefreshInterval: Schema.DurationFromMillis.pipe(
482 Schema.withDecodingDefault(
483 Effect.succeed(Duration.toMillis(DEFAULT_PROVIDER_HEALTH_REFRESH_INTERVAL)),
484 ),
485 ),
486 backgroundActivityProfile: BackgroundActivityProfile.pipe(
487 Schema.withDecodingDefault(Effect.succeed(DEFAULT_BACKGROUND_ACTIVITY_PROFILE)),
488 ),
489 defaultThreadEnvMode: ThreadEnvMode.pipe(
490 Schema.withDecodingDefault(Effect.succeed("local" as const satisfies ThreadEnvMode)),
491 ),
492 newWorktreesStartFromOrigin: Schema.Boolean.pipe(
493 Schema.withDecodingDefault(Effect.succeed(true)),
494 ),
495 addProjectBaseDirectory: TrimmedString.pipe(Schema.withDecodingDefault(Effect.succeed(""))),
496 textGenerationModelSelection: ModelSelection.pipe(
497 Schema.withDecodingDefault(
498 Effect.succeed({
499 instanceId: ProviderInstanceId.make("codex"),
500 model: DEFAULT_TEXT_GENERATION_MODEL,
501 }),
502 ),
503 ),
504 sourceControlWritingStyle: SourceControlWritingStyleSettings.pipe(
505 Schema.withDecodingDefault(Effect.succeed({})),
506 ),
507 sourceControlWriterModelSelection: Schema.NullOr(ModelSelection).pipe(
508 Schema.withDecodingDefault(Effect.succeed(null)),
509 ),
510
511 // Legacy single-instance-per-driver settings. Continues to be the source
512 // of truth until `providerInstances` (below) lands per-driver migration
513 // shims and the server starts hydrating instances from it. Driver-specific
514 // schemas live here for the duration of the migration; once each driver
515 // owns its config in its own package, this struct shrinks to nothing and
516 // is removed entirely.
517 providers: Schema.Struct({
518 codex: CodexSettings.pipe(Schema.withDecodingDefault(Effect.succeed({}))),
519 claudeAgent: ClaudeSettings.pipe(Schema.withDecodingDefault(Effect.succeed({}))),
520 cursor: CursorSettings.pipe(Schema.withDecodingDefault(Effect.succeed({}))),
521 grok: GrokSettings.pipe(Schema.withDecodingDefault(Effect.succeed({}))),
522 opencode: OpenCodeSettings.pipe(Schema.withDecodingDefault(Effect.succeed({}))),
523 }).pipe(Schema.withDecodingDefault(Effect.succeed({}))),
524 // New driver-agnostic instance map. Keyed by `ProviderInstanceId`; values
525 // are `ProviderInstanceConfig` envelopes. The driver-specific config blob
526 // is `Schema.Unknown` at this layer so envelopes with unknown drivers
527 // (forks, downgrades, in-flight PR branches) round-trip without loss.
528 // See providerInstance.ts for the forward/backward compatibility invariant.
529 providerInstances: Schema.Record(ProviderInstanceId, ProviderInstanceConfig).pipe(
530 Schema.withDecodingDefault(Effect.succeed({})),
531 ),
532 observability: ObservabilitySettings.pipe(Schema.withDecodingDefault(Effect.succeed({}))),
533});
534export type ServerSettings = typeof ServerSettings.Type;
535
536export const DEFAULT_SERVER_SETTINGS: ServerSettings = Schema.decodeSync(ServerSettings)({});
537
538export const ServerSettingsOperation = Schema.Literals([
539 "normalize",
540 "check-exists",
541 "read-file",
542 "read-secret",
543 "remove-secret",
544 "remove-stale-secret",
545 "write-secret",
546 "write-file",
547 "prepare-directory",
548]);
549export type ServerSettingsOperation = typeof ServerSettingsOperation.Type;
550
551export class ServerSettingsError extends Schema.TaggedErrorClass<ServerSettingsError>()(
552 "ServerSettingsError",
553 {
554 settingsPath: Schema.String,
555 operation: ServerSettingsOperation,
556 providerInstanceId: Schema.optional(Schema.String),
557 environmentVariable: Schema.optional(Schema.String),
558 cause: Schema.Defect(),
559 },
560) {
561 override get message(): string {
562 const provider =
563 this.providerInstanceId === undefined ? "" : ` for provider ${this.providerInstanceId}`;
564 const variable =
565 this.environmentVariable === undefined
566 ? ""
567 : ` and environment variable ${this.environmentVariable}`;
568 return `Server settings ${this.operation} failed${provider}${variable} at ${this.settingsPath}.`;
569 }
570}
571
572// ── Unified type ─────────────────────────────────────────────────────
573
574export type UnifiedSettings = ServerSettings & ClientSettings;
575export const DEFAULT_UNIFIED_SETTINGS: UnifiedSettings = {
576 ...DEFAULT_SERVER_SETTINGS,
577 ...DEFAULT_CLIENT_SETTINGS,
578};
579
580// ── Server Settings Patch (replace with a Schema.deepPartial if available) ──────────────────────────────────────────
581
582const ModelSelectionPatch = Schema.Struct({
583 instanceId: Schema.optionalKey(ProviderInstanceId),
584 model: Schema.optionalKey(TrimmedNonEmptyString),
585 options: Schema.optionalKey(ProviderOptionSelections),
586});
587
588const CodexSettingsPatch = Schema.Struct({
589 enabled: Schema.optionalKey(Schema.Boolean),
590 binaryPath: Schema.optionalKey(TrimmedString),
591 homePath: Schema.optionalKey(TrimmedString),
592 shadowHomePath: Schema.optionalKey(TrimmedString),
593 launchArgs: Schema.optionalKey(TrimmedString),
594 customModels: Schema.optionalKey(Schema.Array(Schema.String)),
595});
596
597const ClaudeSettingsPatch = Schema.Struct({
598 enabled: Schema.optionalKey(Schema.Boolean),
599 binaryPath: Schema.optionalKey(TrimmedString),
600 homePath: Schema.optionalKey(TrimmedString),
601 customModels: Schema.optionalKey(Schema.Array(Schema.String)),
602 launchArgs: Schema.optionalKey(TrimmedString),
603});
604
605const CursorSettingsPatch = Schema.Struct({
606 enabled: Schema.optionalKey(Schema.Boolean),
607 binaryPath: Schema.optionalKey(TrimmedString),
608 apiEndpoint: Schema.optionalKey(TrimmedString),
609 customModels: Schema.optionalKey(Schema.Array(Schema.String)),
610});
611
612const GrokSettingsPatch = Schema.Struct({
613 enabled: Schema.optionalKey(Schema.Boolean),
614 binaryPath: Schema.optionalKey(TrimmedString),
615 customModels: Schema.optionalKey(Schema.Array(Schema.String)),
616});
617
618const OpenCodeSettingsPatch = Schema.Struct({
619 enabled: Schema.optionalKey(Schema.Boolean),
620 binaryPath: Schema.optionalKey(TrimmedString),
621 serverUrl: Schema.optionalKey(TrimmedString),
622 serverPassword: Schema.optionalKey(TrimmedString),
623 customModels: Schema.optionalKey(Schema.Array(Schema.String)),
624});
625
626export const ServerSettingsPatch = Schema.Struct({
627 // Server settings
628 enableAssistantStreaming: Schema.optionalKey(Schema.Boolean),
629 enableProviderUpdateChecks: Schema.optionalKey(Schema.Boolean),
630 backgroundActivity: Schema.optionalKey(
631 Schema.Struct({
632 schemaVersion: Schema.optionalKey(Schema.Literal(1)),
633 profile: Schema.optionalKey(BackgroundActivityProfileSelection),
634 baseProfile: Schema.optionalKey(BackgroundActivityProfile),
635 overrides: Schema.optionalKey(BackgroundActivityOverrides),
636 }),
637 ),
638 automaticGitFetchInterval: Schema.optionalKey(Schema.DurationFromMillis),
639 providerHealthRefreshInterval: Schema.optionalKey(Schema.DurationFromMillis),
640 backgroundActivityProfile: Schema.optionalKey(BackgroundActivityProfile),
641 defaultThreadEnvMode: Schema.optionalKey(ThreadEnvMode),
642 newWorktreesStartFromOrigin: Schema.optionalKey(Schema.Boolean),
643 addProjectBaseDirectory: Schema.optionalKey(TrimmedString),
644 textGenerationModelSelection: Schema.optionalKey(ModelSelectionPatch),
645 sourceControlWritingStyle: Schema.optionalKey(
646 Schema.Struct({
647 mode: Schema.optionalKey(SourceControlWritingStyleMode),
648 customInstructions: Schema.optionalKey(TrimmedString),
649 followChangeRequestTemplates: Schema.optionalKey(Schema.Boolean),
650 }),
651 ),
652 sourceControlWriterModelSelection: Schema.optionalKey(Schema.NullOr(ModelSelection)),
653 observability: Schema.optionalKey(
654 Schema.Struct({
655 otlpTracesUrl: Schema.optionalKey(TrimmedString),
656 otlpMetricsUrl: Schema.optionalKey(TrimmedString),
657 }),
658 ),
659 providers: Schema.optionalKey(
660 Schema.Struct({
661 codex: Schema.optionalKey(CodexSettingsPatch),
662 claudeAgent: Schema.optionalKey(ClaudeSettingsPatch),
663 cursor: Schema.optionalKey(CursorSettingsPatch),
664 grok: Schema.optionalKey(GrokSettingsPatch),
665 opencode: Schema.optionalKey(OpenCodeSettingsPatch),
666 }),
667 ),
668 // Whole-map replacement for the new instance config. Patching individual
669 // entries is intentionally out of scope: the map is small, and partial
670 // patches risk leaving driver-specific config in a half-merged state.
671 // The web UI sends a fully-formed map every time it edits this field.
672 providerInstances: Schema.optionalKey(Schema.Record(ProviderInstanceId, ProviderInstanceConfig)),
673});
674export type ServerSettingsPatch = typeof ServerSettingsPatch.Type;
675
676export const ClientSettingsPatch = Schema.Struct({
677 autoOpenPlanSidebar: Schema.optionalKey(Schema.Boolean),
678 confirmThreadArchive: Schema.optionalKey(Schema.Boolean),
679 confirmThreadDelete: Schema.optionalKey(Schema.Boolean),
680 diffIgnoreWhitespace: Schema.optionalKey(Schema.Boolean),
681 environmentIdentificationMode: Schema.optionalKey(EnvironmentIdentificationMode),
682 glassOpacity: Schema.optionalKey(GlassOpacity),
683 favorites: Schema.optionalKey(
684 Schema.Array(
685 Schema.Struct({
686 provider: ProviderInstanceId,
687 model: TrimmedNonEmptyString,
688 }),
689 ),
690 ),
691 providerModelPreferences: Schema.optionalKey(
692 Schema.Record(
693 ProviderInstanceId,
694 Schema.Struct({
695 hiddenModels: Schema.Array(Schema.String).pipe(
696 Schema.withDecodingDefault(Effect.succeed([])),
697 ),
698 modelOrder: Schema.Array(Schema.String).pipe(
699 Schema.withDecodingDefault(Effect.succeed([])),
700 ),
701 }),
702 ),
703 ),
704 sidebarAutoSettleAfterDays: Schema.optionalKey(Schema.NullOr(SidebarAutoSettleAfterDays)),
705 sidebarProjectGroupingMode: Schema.optionalKey(SidebarProjectGroupingMode),
706 sidebarProjectGroupingOverrides: Schema.optionalKey(
707 Schema.Record(TrimmedNonEmptyString, SidebarProjectGroupingMode),
708 ),
709 sidebarProjectSortOrder: Schema.optionalKey(SidebarProjectSortOrder),
710 sidebarThreadSortOrder: Schema.optionalKey(SidebarThreadSortOrder),
711 sidebarThreadPreviewCount: Schema.optionalKey(SidebarThreadPreviewCount),
712 sidebarV2Enabled: Schema.optionalKey(Schema.Boolean),
713 sidebarV2ConfiguredByUser: Schema.optionalKey(Schema.Boolean),
714 timestampFormat: Schema.optionalKey(TimestampFormat),
715 wordWrap: Schema.optionalKey(Schema.Boolean),
716});
717export type ClientSettingsPatch = typeof ClientSettingsPatch.Type;