- Snapshot sessions Map with Array.from() in stopAll and finalizer to prevent entries being skipped when stopSessionInternal deletes during iteration (ClaudeCodeAdapter.ts) - Stop resolving aliases in normalizeCustomModelSlugs so dated model versions like claude-sonnet-4-6-20251117 are not silently dropped as built-in duplicates (appSettings.ts) - Remove incorrect model: gpt-5-codex assertion from claudeCode provider test since the test validates provider selection, not model resolution (ProviderCommandReactor.test.ts) Co-authored-by: Julius Marminge <[email protected]>
182 lines
5.4 KiB
TypeScript
182 lines
5.4 KiB
TypeScript
import { useCallback } from "react";
|
|
import { Option, Schema } from "effect";
|
|
import { type ProviderKind } from "@t3tools/contracts";
|
|
import { getDefaultModel, getModelOptions, normalizeModelSlug } from "@t3tools/shared/model";
|
|
import { useLocalStorage } from "./hooks/useLocalStorage";
|
|
|
|
const APP_SETTINGS_STORAGE_KEY = "t3code:app-settings:v1";
|
|
const MAX_CUSTOM_MODEL_COUNT = 32;
|
|
export const MAX_CUSTOM_MODEL_LENGTH = 256;
|
|
export const TIMESTAMP_FORMAT_OPTIONS = ["locale", "12-hour", "24-hour"] as const;
|
|
export type TimestampFormat = (typeof TIMESTAMP_FORMAT_OPTIONS)[number];
|
|
export const DEFAULT_TIMESTAMP_FORMAT: TimestampFormat = "locale";
|
|
const BUILT_IN_MODEL_SLUGS_BY_PROVIDER: Record<ProviderKind, ReadonlySet<string>> = {
|
|
codex: new Set(getModelOptions("codex").map((option) => option.slug)),
|
|
claudeCode: new Set(getModelOptions("claudeCode").map((option) => option.slug)),
|
|
};
|
|
|
|
const AppSettingsSchema = Schema.Struct({
|
|
codexBinaryPath: Schema.String.check(Schema.isMaxLength(4096)).pipe(
|
|
Schema.withConstructorDefault(() => Option.some("")),
|
|
),
|
|
codexHomePath: Schema.String.check(Schema.isMaxLength(4096)).pipe(
|
|
Schema.withConstructorDefault(() => Option.some("")),
|
|
),
|
|
defaultThreadEnvMode: Schema.Literals(["local", "worktree"]).pipe(
|
|
Schema.withConstructorDefault(() => Option.some("local")),
|
|
),
|
|
confirmThreadDelete: Schema.Boolean.pipe(Schema.withConstructorDefault(() => Option.some(true))),
|
|
enableAssistantStreaming: Schema.Boolean.pipe(
|
|
Schema.withConstructorDefault(() => Option.some(false)),
|
|
),
|
|
timestampFormat: Schema.Literals(["locale", "12-hour", "24-hour"]).pipe(
|
|
Schema.withConstructorDefault(() => Option.some(DEFAULT_TIMESTAMP_FORMAT)),
|
|
),
|
|
customCodexModels: Schema.Array(Schema.String).pipe(
|
|
Schema.withConstructorDefault(() => Option.some([])),
|
|
),
|
|
customClaudeModels: Schema.Array(Schema.String).pipe(
|
|
Schema.withConstructorDefault(() => Option.some([])),
|
|
),
|
|
});
|
|
export type AppSettings = typeof AppSettingsSchema.Type;
|
|
export interface AppModelOption {
|
|
slug: string;
|
|
name: string;
|
|
isCustom: boolean;
|
|
}
|
|
|
|
const DEFAULT_APP_SETTINGS = AppSettingsSchema.makeUnsafe({});
|
|
|
|
export function normalizeCustomModelSlugs(
|
|
models: Iterable<string | null | undefined>,
|
|
provider: ProviderKind = "codex",
|
|
): string[] {
|
|
const normalizedModels: string[] = [];
|
|
const seen = new Set<string>();
|
|
const builtInModelSlugs = BUILT_IN_MODEL_SLUGS_BY_PROVIDER[provider];
|
|
|
|
for (const candidate of models) {
|
|
if (typeof candidate !== "string") continue;
|
|
const trimmed = candidate.trim();
|
|
if (
|
|
!trimmed ||
|
|
trimmed.length > MAX_CUSTOM_MODEL_LENGTH ||
|
|
builtInModelSlugs.has(trimmed) ||
|
|
seen.has(trimmed)
|
|
) {
|
|
continue;
|
|
}
|
|
|
|
seen.add(trimmed);
|
|
normalizedModels.push(trimmed);
|
|
if (normalizedModels.length >= MAX_CUSTOM_MODEL_COUNT) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
return normalizedModels;
|
|
}
|
|
|
|
function normalizeAppSettings(settings: AppSettings): AppSettings {
|
|
return {
|
|
...settings,
|
|
customCodexModels: normalizeCustomModelSlugs(settings.customCodexModels, "codex"),
|
|
customClaudeModels: normalizeCustomModelSlugs(settings.customClaudeModels, "claudeCode"),
|
|
};
|
|
}
|
|
export function getAppModelOptions(
|
|
provider: ProviderKind,
|
|
customModels: readonly string[],
|
|
selectedModel?: string | null,
|
|
): AppModelOption[] {
|
|
const options: AppModelOption[] = getModelOptions(provider).map(({ slug, name }) => ({
|
|
slug,
|
|
name,
|
|
isCustom: false,
|
|
}));
|
|
const seen = new Set(options.map((option) => option.slug));
|
|
|
|
for (const slug of normalizeCustomModelSlugs(customModels, provider)) {
|
|
if (seen.has(slug)) {
|
|
continue;
|
|
}
|
|
|
|
seen.add(slug);
|
|
options.push({
|
|
slug,
|
|
name: slug,
|
|
isCustom: true,
|
|
});
|
|
}
|
|
|
|
const normalizedSelectedModel = normalizeModelSlug(selectedModel, provider);
|
|
if (normalizedSelectedModel && !seen.has(normalizedSelectedModel)) {
|
|
options.push({
|
|
slug: normalizedSelectedModel,
|
|
name: normalizedSelectedModel,
|
|
isCustom: true,
|
|
});
|
|
}
|
|
|
|
return options;
|
|
}
|
|
|
|
export function resolveAppModelSelection(
|
|
provider: ProviderKind,
|
|
customModels: readonly string[],
|
|
selectedModel: string | null | undefined,
|
|
): string {
|
|
const options = getAppModelOptions(provider, customModels, selectedModel);
|
|
const trimmedSelectedModel = selectedModel?.trim();
|
|
if (trimmedSelectedModel) {
|
|
const direct = options.find((option) => option.slug === trimmedSelectedModel);
|
|
if (direct) {
|
|
return direct.slug;
|
|
}
|
|
|
|
const byName = options.find(
|
|
(option) => option.name.toLowerCase() === trimmedSelectedModel.toLowerCase(),
|
|
);
|
|
if (byName) {
|
|
return byName.slug;
|
|
}
|
|
}
|
|
|
|
const normalizedSelectedModel = normalizeModelSlug(selectedModel, provider);
|
|
if (!normalizedSelectedModel) {
|
|
return getDefaultModel(provider);
|
|
}
|
|
|
|
return (
|
|
options.find((option) => option.slug === normalizedSelectedModel)?.slug ??
|
|
getDefaultModel(provider)
|
|
);
|
|
}
|
|
|
|
export function useAppSettings() {
|
|
const [settings, setSettings] = useLocalStorage(
|
|
APP_SETTINGS_STORAGE_KEY,
|
|
DEFAULT_APP_SETTINGS,
|
|
AppSettingsSchema,
|
|
);
|
|
|
|
const updateSettings = useCallback(
|
|
(patch: Partial<AppSettings>) => {
|
|
setSettings((prev) => normalizeAppSettings({ ...prev, ...patch }));
|
|
},
|
|
[setSettings],
|
|
);
|
|
|
|
const resetSettings = useCallback(() => {
|
|
setSettings(DEFAULT_APP_SETTINGS);
|
|
}, [setSettings]);
|
|
|
|
return {
|
|
settings,
|
|
updateSettings,
|
|
resetSettings,
|
|
defaults: DEFAULT_APP_SETTINGS,
|
|
} as const;
|
|
}
|