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