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

settings.ts

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