Open History to see the last commit
1import { Effect } from "effect";
2import * as Schema from "effect/Schema";
3import * as SchemaTransformation from "effect/SchemaTransformation";
4import { TrimmedNonEmptyString, TrimmedString } from "./baseSchemas";
5import {
6 ClaudeModelOptions,
7 CodexModelOptions,
8 DEFAULT_GIT_TEXT_GENERATION_MODEL_BY_PROVIDER,
9} from "./model";
10import { ModelSelection } from "./orchestration";
11
12// ── Client Settings (local-only) ───────────────────────────────
13
14export const TimestampFormat = Schema.Literals(["locale", "12-hour", "24-hour"]);
15export type TimestampFormat = typeof TimestampFormat.Type;
16export const DEFAULT_TIMESTAMP_FORMAT: TimestampFormat = "locale";
17
18export const SidebarProjectSortOrder = Schema.Literals(["updated_at", "created_at", "manual"]);
19export type SidebarProjectSortOrder = typeof SidebarProjectSortOrder.Type;
20export const DEFAULT_SIDEBAR_PROJECT_SORT_ORDER: SidebarProjectSortOrder = "updated_at";
21
22export const SidebarThreadSortOrder = Schema.Literals(["updated_at", "created_at"]);
23export type SidebarThreadSortOrder = typeof SidebarThreadSortOrder.Type;
24export const DEFAULT_SIDEBAR_THREAD_SORT_ORDER: SidebarThreadSortOrder = "updated_at";
25
26export const ClientSettingsSchema = Schema.Struct({
27 confirmThreadArchive: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))),
28 confirmThreadDelete: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(true))),
29 diffWordWrap: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))),
30 sidebarProjectSortOrder: SidebarProjectSortOrder.pipe(
31 Schema.withDecodingDefault(Effect.succeed(DEFAULT_SIDEBAR_PROJECT_SORT_ORDER)),
32 ),
33 sidebarThreadSortOrder: SidebarThreadSortOrder.pipe(
34 Schema.withDecodingDefault(Effect.succeed(DEFAULT_SIDEBAR_THREAD_SORT_ORDER)),
35 ),
36 timestampFormat: TimestampFormat.pipe(
37 Schema.withDecodingDefault(Effect.succeed(DEFAULT_TIMESTAMP_FORMAT)),
38 ),
39});
40export type ClientSettings = typeof ClientSettingsSchema.Type;
41
42export const DEFAULT_CLIENT_SETTINGS: ClientSettings = Schema.decodeSync(ClientSettingsSchema)({});
43
44// ── Server Settings (server-authoritative) ────────────────────
45
46export const ThreadEnvMode = Schema.Literals(["local", "worktree"]);
47export type ThreadEnvMode = typeof ThreadEnvMode.Type;
48
49const makeBinaryPathSetting = (fallback: string) =>
50 TrimmedString.pipe(
51 Schema.decodeTo(
52 Schema.String,
53 SchemaTransformation.transformOrFail({
54 decode: (value) => Effect.succeed(value || fallback),
55 encode: (value) => Effect.succeed(value),
56 }),
57 ),
58 Schema.withDecodingDefault(Effect.succeed(fallback)),
59 );
60
61export const CodexSettings = Schema.Struct({
62 enabled: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(true))),
63 binaryPath: makeBinaryPathSetting("codex"),
64 homePath: TrimmedString.pipe(Schema.withDecodingDefault(Effect.succeed(""))),
65 customModels: Schema.Array(Schema.String).pipe(Schema.withDecodingDefault(Effect.succeed([]))),
66});
67export type CodexSettings = typeof CodexSettings.Type;
68
69export const ClaudeSettings = Schema.Struct({
70 enabled: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(true))),
71 binaryPath: makeBinaryPathSetting("claude"),
72 customModels: Schema.Array(Schema.String).pipe(Schema.withDecodingDefault(Effect.succeed([]))),
73});
74export type ClaudeSettings = typeof ClaudeSettings.Type;
75
76export const ObservabilitySettings = Schema.Struct({
77 otlpTracesUrl: TrimmedString.pipe(Schema.withDecodingDefault(Effect.succeed(""))),
78 otlpMetricsUrl: TrimmedString.pipe(Schema.withDecodingDefault(Effect.succeed(""))),
79});
80export type ObservabilitySettings = typeof ObservabilitySettings.Type;
81
82export const ServerSettings = Schema.Struct({
83 enableAssistantStreaming: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))),
84 defaultThreadEnvMode: ThreadEnvMode.pipe(
85 Schema.withDecodingDefault(Effect.succeed("local" as const satisfies ThreadEnvMode)),
86 ),
87 textGenerationModelSelection: ModelSelection.pipe(
88 Schema.withDecodingDefault(
89 Effect.succeed({
90 provider: "codex" as const,
91 model: DEFAULT_GIT_TEXT_GENERATION_MODEL_BY_PROVIDER.codex,
92 }),
93 ),
94 ),
95
96 // Provider specific settings
97 providers: Schema.Struct({
98 codex: CodexSettings.pipe(Schema.withDecodingDefault(Effect.succeed({}))),
99 claudeAgent: ClaudeSettings.pipe(Schema.withDecodingDefault(Effect.succeed({}))),
100 }).pipe(Schema.withDecodingDefault(Effect.succeed({}))),
101 observability: ObservabilitySettings.pipe(Schema.withDecodingDefault(Effect.succeed({}))),
102});
103export type ServerSettings = typeof ServerSettings.Type;
104
105export const DEFAULT_SERVER_SETTINGS: ServerSettings = Schema.decodeSync(ServerSettings)({});
106
107export class ServerSettingsError extends Schema.TaggedErrorClass<ServerSettingsError>()(
108 "ServerSettingsError",
109 {
110 settingsPath: Schema.String,
111 detail: Schema.String,
112 cause: Schema.optional(Schema.Defect),
113 },
114) {
115 override get message(): string {
116 return `Server settings error at ${this.settingsPath}: ${this.detail}`;
117 }
118}
119
120// ── Unified type ─────────────────────────────────────────────────────
121
122export type UnifiedSettings = ServerSettings & ClientSettings;
123export const DEFAULT_UNIFIED_SETTINGS: UnifiedSettings = {
124 ...DEFAULT_SERVER_SETTINGS,
125 ...DEFAULT_CLIENT_SETTINGS,
126};
127
128// ── Server Settings Patch (replace with a Schema.deepPartial if available) ──────────────────────────────────────────
129
130const CodexModelOptionsPatch = Schema.Struct({
131 reasoningEffort: Schema.optionalKey(CodexModelOptions.fields.reasoningEffort),
132 fastMode: Schema.optionalKey(CodexModelOptions.fields.fastMode),
133});
134
135const ClaudeModelOptionsPatch = Schema.Struct({
136 thinking: Schema.optionalKey(ClaudeModelOptions.fields.thinking),
137 effort: Schema.optionalKey(ClaudeModelOptions.fields.effort),
138 fastMode: Schema.optionalKey(ClaudeModelOptions.fields.fastMode),
139 contextWindow: Schema.optionalKey(ClaudeModelOptions.fields.contextWindow),
140});
141
142const ModelSelectionPatch = Schema.Union([
143 Schema.Struct({
144 provider: Schema.optionalKey(Schema.Literal("codex")),
145 model: Schema.optionalKey(TrimmedNonEmptyString),
146 options: Schema.optionalKey(CodexModelOptionsPatch),
147 }),
148 Schema.Struct({
149 provider: Schema.optionalKey(Schema.Literal("claudeAgent")),
150 model: Schema.optionalKey(TrimmedNonEmptyString),
151 options: Schema.optionalKey(ClaudeModelOptionsPatch),
152 }),
153]);
154
155const CodexSettingsPatch = Schema.Struct({
156 enabled: Schema.optionalKey(Schema.Boolean),
157 binaryPath: Schema.optionalKey(Schema.String),
158 homePath: Schema.optionalKey(Schema.String),
159 customModels: Schema.optionalKey(Schema.Array(Schema.String)),
160});
161
162const ClaudeSettingsPatch = Schema.Struct({
163 enabled: Schema.optionalKey(Schema.Boolean),
164 binaryPath: Schema.optionalKey(Schema.String),
165 customModels: Schema.optionalKey(Schema.Array(Schema.String)),
166});
167
168export const ServerSettingsPatch = Schema.Struct({
169 enableAssistantStreaming: Schema.optionalKey(Schema.Boolean),
170 defaultThreadEnvMode: Schema.optionalKey(ThreadEnvMode),
171 textGenerationModelSelection: Schema.optionalKey(ModelSelectionPatch),
172 observability: Schema.optionalKey(
173 Schema.Struct({
174 otlpTracesUrl: Schema.optionalKey(Schema.String),
175 otlpMetricsUrl: Schema.optionalKey(Schema.String),
176 }),
177 ),
178 providers: Schema.optionalKey(
179 Schema.Struct({
180 codex: Schema.optionalKey(CodexSettingsPatch),
181 claudeAgent: Schema.optionalKey(ClaudeSettingsPatch),
182 }),
183 ),
184});
185export type ServerSettingsPatch = typeof ServerSettingsPatch.Type;