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

settings.ts

189 lines8.0 KB

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 launchArgs: Schema.String.pipe(Schema.withDecodingDefault(Effect.succeed(""))),
74});
75export type ClaudeSettings = typeof ClaudeSettings.Type;
76​
77export const ObservabilitySettings = Schema.Struct({
78 otlpTracesUrl: TrimmedString.pipe(Schema.withDecodingDefault(Effect.succeed(""))),
79 otlpMetricsUrl: TrimmedString.pipe(Schema.withDecodingDefault(Effect.succeed(""))),
80});
81export type ObservabilitySettings = typeof ObservabilitySettings.Type;
82​
83export const ServerSettings = Schema.Struct({
84 enableAssistantStreaming: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))),
85 defaultThreadEnvMode: ThreadEnvMode.pipe(
86 Schema.withDecodingDefault(Effect.succeed("local" as const satisfies ThreadEnvMode)),
87 ),
88 addProjectBaseDirectory: TrimmedString.pipe(Schema.withDecodingDefault(Effect.succeed(""))),
89 textGenerationModelSelection: ModelSelection.pipe(
90 Schema.withDecodingDefault(
91 Effect.succeed({
92 provider: "codex" as const,
93 model: DEFAULT_GIT_TEXT_GENERATION_MODEL_BY_PROVIDER.codex,
94 }),
95 ),
96 ),
97​
98 // Provider specific settings
99 providers: Schema.Struct({
100 codex: CodexSettings.pipe(Schema.withDecodingDefault(Effect.succeed({}))),
101 claudeAgent: ClaudeSettings.pipe(Schema.withDecodingDefault(Effect.succeed({}))),
102 }).pipe(Schema.withDecodingDefault(Effect.succeed({}))),
103 observability: ObservabilitySettings.pipe(Schema.withDecodingDefault(Effect.succeed({}))),
104});
105export type ServerSettings = typeof ServerSettings.Type;
106​
107export const DEFAULT_SERVER_SETTINGS: ServerSettings = Schema.decodeSync(ServerSettings)({});
108​
109export class ServerSettingsError extends Schema.TaggedErrorClass<ServerSettingsError>()(
110 "ServerSettingsError",
111 {
112 settingsPath: Schema.String,
113 detail: Schema.String,
114 cause: Schema.optional(Schema.Defect),
115 },
116) {
117 override get message(): string {
118 return `Server settings error at ${this.settingsPath}: ${this.detail}`;
119 }
121​
122// ── Unified type ─────────────────────────────────────────────────────
123​
124export type UnifiedSettings = ServerSettings & ClientSettings;
125export const DEFAULT_UNIFIED_SETTINGS: UnifiedSettings = {
126 ...DEFAULT_SERVER_SETTINGS,
127 ...DEFAULT_CLIENT_SETTINGS,
128};
129​
130// ── Server Settings Patch (replace with a Schema.deepPartial if available) ──────────────────────────────────────────
131​
132const CodexModelOptionsPatch = Schema.Struct({
133 reasoningEffort: Schema.optionalKey(CodexModelOptions.fields.reasoningEffort),
134 fastMode: Schema.optionalKey(CodexModelOptions.fields.fastMode),
135});
136​
137const ClaudeModelOptionsPatch = Schema.Struct({
138 thinking: Schema.optionalKey(ClaudeModelOptions.fields.thinking),
139 effort: Schema.optionalKey(ClaudeModelOptions.fields.effort),
140 fastMode: Schema.optionalKey(ClaudeModelOptions.fields.fastMode),
141 contextWindow: Schema.optionalKey(ClaudeModelOptions.fields.contextWindow),
142});
143​
144const ModelSelectionPatch = Schema.Union([
145 Schema.Struct({
146 provider: Schema.optionalKey(Schema.Literal("codex")),
147 model: Schema.optionalKey(TrimmedNonEmptyString),
148 options: Schema.optionalKey(CodexModelOptionsPatch),
149 }),
150 Schema.Struct({
151 provider: Schema.optionalKey(Schema.Literal("claudeAgent")),
152 model: Schema.optionalKey(TrimmedNonEmptyString),
153 options: Schema.optionalKey(ClaudeModelOptionsPatch),
154 }),
155]);
156​
157const CodexSettingsPatch = Schema.Struct({
158 enabled: Schema.optionalKey(Schema.Boolean),
159 binaryPath: Schema.optionalKey(Schema.String),
160 homePath: Schema.optionalKey(Schema.String),
161 customModels: Schema.optionalKey(Schema.Array(Schema.String)),
162});
163​
164const ClaudeSettingsPatch = Schema.Struct({
165 enabled: Schema.optionalKey(Schema.Boolean),
166 binaryPath: Schema.optionalKey(Schema.String),
167 customModels: Schema.optionalKey(Schema.Array(Schema.String)),
168 launchArgs: Schema.optionalKey(Schema.String),
169});
170​
171export const ServerSettingsPatch = Schema.Struct({
172 enableAssistantStreaming: Schema.optionalKey(Schema.Boolean),
173 defaultThreadEnvMode: Schema.optionalKey(ThreadEnvMode),
174 addProjectBaseDirectory: Schema.optionalKey(Schema.String),
175 textGenerationModelSelection: Schema.optionalKey(ModelSelectionPatch),
176 observability: Schema.optionalKey(
177 Schema.Struct({
178 otlpTracesUrl: Schema.optionalKey(Schema.String),
179 otlpMetricsUrl: Schema.optionalKey(Schema.String),
180 }),
181 ),
182 providers: Schema.optionalKey(
183 Schema.Struct({
184 codex: Schema.optionalKey(CodexSettingsPatch),
185 claudeAgent: Schema.optionalKey(ClaudeSettingsPatch),
186 }),
187 ),
188});
189export type ServerSettingsPatch = typeof ServerSettingsPatch.Type;