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

appSettings.ts

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