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