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

appSettings.ts

185 lines5.6 KB

1import { useCallback } from "react";
2import { Option, Schema } from "effect";
3import { type ProviderKind } from "@t3tools/contracts";
4import { getDefaultModel, getModelOptions, normalizeModelSlug } from "@t3tools/shared/model";
5import { useLocalStorage } from "./hooks/useLocalStorage";
6​
7const APP_SETTINGS_STORAGE_KEY = "t3code:app-settings:v1";
8const MAX_CUSTOM_MODEL_COUNT = 32;
9export const MAX_CUSTOM_MODEL_LENGTH = 256;
10export const TIMESTAMP_FORMAT_OPTIONS = ["locale", "12-hour", "24-hour"] as const;
11export type TimestampFormat = (typeof TIMESTAMP_FORMAT_OPTIONS)[number];
12export const DEFAULT_TIMESTAMP_FORMAT: TimestampFormat = "locale";
13const BUILT_IN_MODEL_SLUGS_BY_PROVIDER: Record<ProviderKind, ReadonlySet<string>> = {
14 codex: new Set(getModelOptions("codex").map((option) => option.slug)),
15 claudeCode: new Set(getModelOptions("claudeCode").map((option) => option.slug)),
16};
17​
18const AppSettingsSchema = Schema.Struct({
19 codexBinaryPath: Schema.String.check(Schema.isMaxLength(4096)).pipe(
20 Schema.withConstructorDefault(() => Option.some("")),
21 ),
22 codexHomePath: Schema.String.check(Schema.isMaxLength(4096)).pipe(
23 Schema.withConstructorDefault(() => Option.some("")),
24 ),
25 defaultThreadEnvMode: Schema.Literals(["local", "worktree"]).pipe(
26 Schema.withConstructorDefault(() => Option.some("local")),
27 ),
28 confirmThreadDelete: Schema.Boolean.pipe(Schema.withConstructorDefault(() => Option.some(true))),
29 enableAssistantStreaming: Schema.Boolean.pipe(
30 Schema.withConstructorDefault(() => Option.some(false)),
31 ),
32 timestampFormat: Schema.Literals(["locale", "12-hour", "24-hour"]).pipe(
33 Schema.withConstructorDefault(() => Option.some(DEFAULT_TIMESTAMP_FORMAT)),
34 ),
35 customCodexModels: Schema.Array(Schema.String).pipe(
36 Schema.withConstructorDefault(() => Option.some([])),
37 ),
38 customClaudeModels: Schema.Array(Schema.String).pipe(
39 Schema.withConstructorDefault(() => Option.some([])),
40 ),
41 claudeExperimentalAgentTeams: Schema.Boolean.pipe(
42 Schema.withConstructorDefault(() => Option.some(false)),
43 ),
44 claudeAgentProgressSummaries: Schema.Boolean.pipe(
45 Schema.withConstructorDefault(() => Option.some(false)),
46 ),
47 claudeTeammateMode: Schema.Literals(["in-process", "tmux", "auto"]).pipe(
48 Schema.withConstructorDefault(() => Option.some("auto" as const)),
49 ),
50});
51export type AppSettings = typeof AppSettingsSchema.Type;
52export interface AppModelOption {
53 slug: string;
54 name: string;
55 isCustom: boolean;
57​
58export const DEFAULT_APP_SETTINGS = AppSettingsSchema.makeUnsafe({});
59​
60export function normalizeCustomModelSlugs(
61 models: Iterable<string | null | undefined>,
62 provider: ProviderKind = "codex",
63): string[] {
64 const normalizedModels: string[] = [];
65 const seen = new Set<string>();
66 const builtInModelSlugs = BUILT_IN_MODEL_SLUGS_BY_PROVIDER[provider];
67​
68 for (const candidate of models) {
69 const normalized = normalizeModelSlug(candidate, provider);
70 if (
71 !normalized ||
72 normalized.length > MAX_CUSTOM_MODEL_LENGTH ||
73 builtInModelSlugs.has(normalized) ||
74 seen.has(normalized)
75 ) {
76 continue;
77 }
78​
79 seen.add(normalized);
80 normalizedModels.push(normalized);
81 if (normalizedModels.length >= MAX_CUSTOM_MODEL_COUNT) {
82 break;
83 }
84 }
85​
86 return normalizedModels;
88​
89export function getAppModelOptions(
90 provider: ProviderKind,
91 customModels: readonly string[],
92 selectedModel?: string | null,
93): AppModelOption[] {
94 const options: AppModelOption[] = getModelOptions(provider).map(({ slug, name }) => ({
95 slug,
96 name,
97 isCustom: false,
98 }));
99 const seen = new Set(options.map((option) => option.slug));
100​
101 for (const slug of normalizeCustomModelSlugs(customModels, provider)) {
102 if (seen.has(slug)) {
103 continue;
104 }
105​
106 seen.add(slug);
107 options.push({
108 slug,
109 name: slug,
110 isCustom: true,
111 });
112 }
113​
114 const normalizedSelectedModel = normalizeModelSlug(selectedModel, provider);
115 if (normalizedSelectedModel && !seen.has(normalizedSelectedModel)) {
116 options.push({
117 slug: normalizedSelectedModel,
118 name: normalizedSelectedModel,
119 isCustom: true,
120 });
121 }
122​
123 return options;
125​
126export function resolveAppModelSelection(
127 provider: ProviderKind,
128 customModels: readonly string[],
129 selectedModel: string | null | undefined,
130): string {
131 const options = getAppModelOptions(provider, customModels, selectedModel);
132 const trimmedSelectedModel = selectedModel?.trim();
133 if (trimmedSelectedModel) {
134 const direct = options.find((option) => option.slug === trimmedSelectedModel);
135 if (direct) {
136 return direct.slug;
137 }
138​
139 const byName = options.find(
140 (option) => option.name.toLowerCase() === trimmedSelectedModel.toLowerCase(),
141 );
142 if (byName) {
143 return byName.slug;
144 }
145 }
146​
147 const normalizedSelectedModel = normalizeModelSlug(selectedModel, provider);
148 if (!normalizedSelectedModel) {
149 return getDefaultModel(provider);
150 }
151​
152 return (
153 options.find((option) => option.slug === normalizedSelectedModel)?.slug ??
154 getDefaultModel(provider)
155 );
157​
158export function useAppSettings() {
159 const [settings, setSettings] = useLocalStorage(
160 APP_SETTINGS_STORAGE_KEY,
161 DEFAULT_APP_SETTINGS,
162 AppSettingsSchema,
163 );
164​
165 const updateSettings = useCallback(
166 (patch: Partial<AppSettings>) => {
167 setSettings((prev) => ({
168 ...prev,
169 ...patch,
170 }));
171 },
172 [setSettings],
173 );
174​
175 const resetSettings = useCallback(() => {
176 setSettings(DEFAULT_APP_SETTINGS);
177 }, [setSettings]);
178​
179 return {
180 settings,
181 updateSettings,
182 resetSettings,
183 defaults: DEFAULT_APP_SETTINGS,
184 } as const;