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

settings.ts

155 lines6.6 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​
96// ── Unified type ─────────────────────────────────────────────────────
97​
98export type UnifiedSettings = ServerSettings & ClientSettings;
99export const DEFAULT_UNIFIED_SETTINGS: UnifiedSettings = {
100 ...DEFAULT_SERVER_SETTINGS,
101 ...DEFAULT_CLIENT_SETTINGS,
102};
103​
104// ── Server Settings Patch (replace with a Schema.deepPartial if available) ──────────────────────────────────────────
105​
106const CodexModelOptionsPatch = Schema.Struct({
107 reasoningEffort: Schema.optionalKey(CodexModelOptions.fields.reasoningEffort),
108 fastMode: Schema.optionalKey(CodexModelOptions.fields.fastMode),
109});
110​
111const ClaudeModelOptionsPatch = Schema.Struct({
112 thinking: Schema.optionalKey(ClaudeModelOptions.fields.thinking),
113 effort: Schema.optionalKey(ClaudeModelOptions.fields.effort),
114 fastMode: Schema.optionalKey(ClaudeModelOptions.fields.fastMode),
115 contextWindow: Schema.optionalKey(ClaudeModelOptions.fields.contextWindow),
116});
117​
118const ModelSelectionPatch = Schema.Union([
119 Schema.Struct({
120 provider: Schema.optionalKey(Schema.Literal("codex")),
121 model: Schema.optionalKey(TrimmedNonEmptyString),
122 options: Schema.optionalKey(CodexModelOptionsPatch),
123 }),
124 Schema.Struct({
125 provider: Schema.optionalKey(Schema.Literal("claudeAgent")),
126 model: Schema.optionalKey(TrimmedNonEmptyString),
127 options: Schema.optionalKey(ClaudeModelOptionsPatch),
128 }),
129]);
130​
131const CodexSettingsPatch = Schema.Struct({
132 enabled: Schema.optionalKey(Schema.Boolean),
133 binaryPath: Schema.optionalKey(Schema.String),
134 homePath: Schema.optionalKey(Schema.String),
135 customModels: Schema.optionalKey(Schema.Array(Schema.String)),
136});
137​
138const ClaudeSettingsPatch = Schema.Struct({
139 enabled: Schema.optionalKey(Schema.Boolean),
140 binaryPath: Schema.optionalKey(Schema.String),
141 customModels: Schema.optionalKey(Schema.Array(Schema.String)),
142});
143​
144export const ServerSettingsPatch = Schema.Struct({
145 enableAssistantStreaming: Schema.optionalKey(Schema.Boolean),
146 defaultThreadEnvMode: Schema.optionalKey(ThreadEnvMode),
147 textGenerationModelSelection: Schema.optionalKey(ModelSelectionPatch),
148 providers: Schema.optionalKey(
149 Schema.Struct({
150 codex: Schema.optionalKey(CodexSettingsPatch),
151 claudeAgent: Schema.optionalKey(ClaudeSettingsPatch),
152 }),
153 ),
154});
155export type ServerSettingsPatch = typeof ServerSettingsPatch.Type;