Compare commits

...
Author SHA1 Message Date
Theo Browne 7efaceae8a better fake version 2026-02-28 14:41:46 -08:00
Theo Browne a84fbbc20e custom models 2026-02-28 04:24:20 -08:00
6 changed files with 311 additions and 44 deletions

No files matched your search

+42
View File
@@ -0,0 +1,42 @@
import { describe, expect, it } from "vitest";
import { getAppModelOptions, normalizeCustomModelSlugs } from "./appSettings";
describe("normalizeCustomModelSlugs", () => {
it("normalizes aliases, removes built-ins, and deduplicates values", () => {
expect(
normalizeCustomModelSlugs([
" custom/internal-model ",
"gpt-5.3-codex",
"5.3",
"custom/internal-model",
"",
null,
]),
).toEqual(["custom/internal-model"]);
});
});
describe("getAppModelOptions", () => {
it("appends saved custom models after the built-in options", () => {
const options = getAppModelOptions(["custom/internal-model"]);
expect(options.map((option) => option.slug)).toEqual([
"gpt-5.3-codex",
"gpt-5.3-codex-spark",
"gpt-5.2-codex",
"gpt-5.2",
"custom/internal-model",
]);
});
it("keeps the currently selected custom model available even if it is no longer saved", () => {
const options = getAppModelOptions([], "custom/selected-model");
expect(options.at(-1)).toEqual({
slug: "custom/selected-model",
name: "custom/selected-model",
isCustom: true,
});
});
});
+89 -5
View File
@@ -1,7 +1,11 @@
import { useCallback, useSyncExternalStore } from "react";
import { Option, Schema } from "effect";
import { MODEL_OPTIONS, normalizeModelSlug } from "@t3tools/contracts";
const APP_SETTINGS_STORAGE_KEY = "t3code:app-settings:v1";
const MAX_CUSTOM_MODEL_COUNT = 32;
export const MAX_CUSTOM_MODEL_LENGTH = 256;
const BUILT_IN_MODEL_SLUGS = new Set<string>(MODEL_OPTIONS.map((option) => option.slug));
const AppSettingsSchema = Schema.Struct({
codexBinaryPath: Schema.String.check(Schema.isMaxLength(4096)).pipe(
@@ -14,8 +18,16 @@ const AppSettingsSchema = Schema.Struct({
enableAssistantStreaming: Schema.Boolean.pipe(
Schema.withConstructorDefault(() => Option.some(false)),
),
customCodexModels: 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({});
@@ -23,6 +35,76 @@ let listeners: Array<() => void> = [];
let cachedRawSettings: string | null | undefined;
let cachedSnapshot: AppSettings = DEFAULT_APP_SETTINGS;
export function normalizeCustomModelSlugs(
models: Iterable<string | null | undefined>,
): AppSettings["customCodexModels"] {
const normalizedModels: string[] = [];
const seen = new Set<string>();
for (const candidate of models) {
const normalized = normalizeModelSlug(candidate);
if (
!normalized ||
normalized.length > MAX_CUSTOM_MODEL_LENGTH ||
BUILT_IN_MODEL_SLUGS.has(normalized) ||
seen.has(normalized)
) {
continue;
}
seen.add(normalized);
normalizedModels.push(normalized);
if (normalizedModels.length >= MAX_CUSTOM_MODEL_COUNT) {
break;
}
}
return normalizedModels;
}
function normalizeAppSettings(settings: AppSettings): AppSettings {
return {
...settings,
customCodexModels: normalizeCustomModelSlugs(settings.customCodexModels),
};
}
export function getAppModelOptions(
customModels: readonly string[],
selectedModel?: string | null,
): AppModelOption[] {
const options: AppModelOption[] = MODEL_OPTIONS.map(({ slug, name }) => ({
slug,
name,
isCustom: false,
}));
const seen = new Set(options.map((option) => option.slug));
for (const slug of normalizeCustomModelSlugs(customModels)) {
if (seen.has(slug)) {
continue;
}
seen.add(slug);
options.push({
slug,
name: slug,
isCustom: true,
});
}
const normalizedSelectedModel = normalizeModelSlug(selectedModel);
if (normalizedSelectedModel && !seen.has(normalizedSelectedModel)) {
options.push({
slug: normalizedSelectedModel,
name: normalizedSelectedModel,
isCustom: true,
});
}
return options;
}
function emitChange(): void {
for (const listener of listeners) {
listener();
@@ -35,7 +117,7 @@ function parsePersistedSettings(value: string | null): AppSettings {
}
try {
return Schema.decodeSync(Schema.fromJsonString(AppSettingsSchema))(value);
return normalizeAppSettings(Schema.decodeSync(Schema.fromJsonString(AppSettingsSchema))(value));
} catch {
return DEFAULT_APP_SETTINGS;
}
@@ -96,10 +178,12 @@ export function useAppSettings() {
);
const updateSettings = useCallback((patch: Partial<AppSettings>) => {
const next = Schema.decodeSync(AppSettingsSchema)({
...getAppSettingsSnapshot(),
...patch,
});
const next = normalizeAppSettings(
Schema.decodeSync(AppSettingsSchema)({
...getAppSettingsSnapshot(),
...patch,
}),
);
persistSettings(next);
emitChange();
}, []);
+37 -28
View File
@@ -6,11 +6,9 @@ import {
type EditorId,
type KeybindingCommand,
type MessageId,
MODEL_OPTIONS,
type ProjectId,
type ProjectEntry,
type ProjectScript,
ModelSlug,
PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
PROVIDER_SEND_TURN_MAX_IMAGE_BYTES,
REASONING_OPTIONS,
@@ -112,7 +110,7 @@ import { Toggle } from "./ui/toggle";
import { SidebarTrigger } from "./ui/sidebar";
import { newCommandId, newMessageId } from "~/lib/utils";
import { readNativeApi } from "~/nativeApi";
import { useAppSettings } from "../appSettings";
import { getAppModelOptions, useAppSettings } from "../appSettings";
import { clamp } from "effect/Number";
function formatMessageMeta(createdAt: string, duration: string | null): string {
@@ -129,12 +127,6 @@ const IMAGE_ONLY_BOOTSTRAP_PROMPT =
const EMPTY_KEYBINDINGS: ResolvedKeybindingsConfig = [];
const EMPTY_PROJECT_ENTRIES: ProjectEntry[] = [];
const COMPOSER_PATH_QUERY_DEBOUNCE_MS = 120;
const SEARCHABLE_MODEL_OPTIONS = MODEL_OPTIONS.map(({ slug, name }) => ({
slug,
name,
searchSlug: slug.toLowerCase(),
searchName: name.toLowerCase(),
}));
const SCRIPT_TERMINAL_COLS = 120;
const SCRIPT_TERMINAL_ROWS = 30;
@@ -216,7 +208,7 @@ type ComposerCommandItem =
| {
id: string;
type: "model";
model: ModelSlug;
model: string;
label: string;
description: string;
};
@@ -438,6 +430,10 @@ export default function ChatView({ threadId }: ChatViewProps) {
const selectedModel = resolveModelSlug(
activeThread?.model ?? activeProject?.model ?? DEFAULT_MODEL,
);
const modelOptions = useMemo(
() => getAppModelOptions(settings.customCodexModels, selectedModel),
[selectedModel, settings.customCodexModels],
);
const phase = derivePhase(activeThread?.session ?? null);
const isSendBusy = sendPhase !== "idle";
const isPreparingWorktree = sendPhase === "preparing-worktree";
@@ -614,18 +610,26 @@ export default function ChatView({ threadId }: ChatViewProps) {
];
}
return SEARCHABLE_MODEL_OPTIONS.filter(({ searchSlug, searchName }) => {
const query = composerTrigger.query.trim().toLowerCase();
if (!query) return true;
return searchSlug.includes(query) || searchName.includes(query);
}).map(({ slug, name }) => ({
id: `model:${slug}`,
type: "model",
model: slug,
label: name,
description: slug,
}));
}, [composerTrigger, workspaceEntries]);
return modelOptions
.map(({ slug, name }) => ({
slug,
name,
searchSlug: slug.toLowerCase(),
searchName: name.toLowerCase(),
}))
.filter(({ searchSlug, searchName }) => {
const query = composerTrigger.query.trim().toLowerCase();
if (!query) return true;
return searchSlug.includes(query) || searchName.includes(query);
})
.map(({ slug, name }) => ({
id: `model:${slug}`,
type: "model" as const,
model: slug,
label: name,
description: slug,
}));
}, [composerTrigger, modelOptions, workspaceEntries]);
const composerMenuOpen = Boolean(composerTrigger);
const activeComposerMenuItem = useMemo(
() =>
@@ -1660,7 +1664,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
);
const onModelSelect = useCallback(
(model: ModelSlug) => {
(model: string) => {
const api = readNativeApi();
if (!activeThread) return;
if (api) {
@@ -2012,7 +2016,11 @@ export default function ChatView({ threadId }: ChatViewProps) {
<div className="flex flex-wrap items-center justify-between gap-2 px-2.5 pb-2.5 sm:flex-nowrap sm:gap-0 sm:px-3 sm:pb-3">
<div className="flex min-w-0 flex-1 items-center gap-1 overflow-x-auto [scrollbar-width:none] [&::-webkit-scrollbar]:hidden sm:min-w-max sm:overflow-visible">
{/* Model picker */}
<ModelPicker model={selectedModel} onModelChange={onModelSelect} />
<ModelPicker
model={selectedModel}
options={modelOptions}
onModelChange={onModelSelect}
/>
{/* Divider */}
<Separator orientation="vertical" className="mx-0.5 hidden h-4 sm:block" />
@@ -2912,12 +2920,13 @@ const MessagesTimeline = memo(function MessagesTimeline({
});
const ModelPicker = memo(function ModelPicker(props: {
model: ModelSlug;
onModelChange: (model: ModelSlug) => void;
model: string;
options: ReadonlyArray<{ slug: string; name: string }>;
onModelChange: (model: string) => void;
}) {
return (
<Select
items={MODEL_OPTIONS.map((option) => ({ label: option.name, value: option.slug }))}
items={props.options.map((option) => ({ label: option.name, value: option.slug }))}
value={props.model}
onValueChange={(value) => (value ? props.onModelChange(value) : undefined)}
>
@@ -2925,7 +2934,7 @@ const ModelPicker = memo(function ModelPicker(props: {
<SelectValue />
</SelectTrigger>
<SelectPopup alignItemWithTrigger={false}>
{MODEL_OPTIONS.map(({ slug, name }) => (
{props.options.map(({ slug, name }) => (
<SelectItem key={slug} value={slug}>
{name}
</SelectItem>
+131 -1
View File
@@ -1,8 +1,9 @@
import { createFileRoute } from "@tanstack/react-router";
import { useQuery } from "@tanstack/react-query";
import { useCallback, useState } from "react";
import { MODEL_OPTIONS, normalizeModelSlug } from "@t3tools/contracts";
import { useAppSettings } from "../appSettings";
import { MAX_CUSTOM_MODEL_LENGTH, useAppSettings } from "../appSettings";
import { isElectron } from "../env";
import { useTheme } from "../hooks/useTheme";
import { serverConfigQueryOptions } from "../lib/serverReactQuery";
@@ -52,9 +53,12 @@ function SettingsRouteView() {
const serverConfigQuery = useQuery(serverConfigQueryOptions());
const [isOpeningKeybindings, setIsOpeningKeybindings] = useState(false);
const [openKeybindingsError, setOpenKeybindingsError] = useState<string | null>(null);
const [customModelInput, setCustomModelInput] = useState("");
const [customModelError, setCustomModelError] = useState<string | null>(null);
const codexBinaryPath = settings.codexBinaryPath;
const codexHomePath = settings.codexHomePath;
const customCodexModels = settings.customCodexModels;
const keybindingsConfigPath = serverConfigQuery.data?.keybindingsConfigPath ?? null;
const openKeybindingsFile = useCallback(() => {
@@ -74,6 +78,42 @@ function SettingsRouteView() {
});
}, [keybindingsConfigPath]);
const addCustomModel = useCallback(() => {
const normalized = normalizeModelSlug(customModelInput);
if (!normalized) {
setCustomModelError("Enter a model slug.");
return;
}
if (MODEL_OPTIONS.some((option) => option.slug === normalized)) {
setCustomModelError("That model is already built in.");
return;
}
if (normalized.length > MAX_CUSTOM_MODEL_LENGTH) {
setCustomModelError(`Model slugs must be ${MAX_CUSTOM_MODEL_LENGTH} characters or less.`);
return;
}
if (customCodexModels.includes(normalized)) {
setCustomModelError("That custom model is already saved.");
return;
}
updateSettings({
customCodexModels: [...customCodexModels, normalized],
});
setCustomModelInput("");
setCustomModelError(null);
}, [customCodexModels, customModelInput, updateSettings]);
const removeCustomModel = useCallback(
(slug: string) => {
updateSettings({
customCodexModels: customCodexModels.filter((model) => model !== slug),
});
setCustomModelError(null);
},
[customCodexModels, updateSettings],
);
return (
<SidebarInset className="h-dvh min-h-0 overflow-hidden overscroll-y-none bg-background text-foreground isolate">
<div className="flex min-h-0 min-w-0 flex-1 flex-col bg-background text-foreground">
@@ -193,6 +233,96 @@ function SettingsRouteView() {
</div>
</section>
<section className="rounded-2xl border border-border bg-card p-5">
<div className="mb-4">
<h2 className="text-sm font-medium text-foreground">Models</h2>
<p className="mt-1 text-xs text-muted-foreground">
Save additional Codex model slugs so they appear in the chat model picker.
</p>
</div>
<div className="space-y-4">
<form
className="flex flex-col gap-3 sm:flex-row sm:items-start"
onSubmit={(event) => {
event.preventDefault();
addCustomModel();
}}
>
<label className="block flex-1 space-y-1">
<span className="text-xs font-medium text-foreground">Custom model slug</span>
<Input
value={customModelInput}
onChange={(event) => {
setCustomModelInput(event.target.value);
if (customModelError) {
setCustomModelError(null);
}
}}
placeholder="your-model-slug"
spellCheck={false}
/>
<span className="text-xs text-muted-foreground">
Example: <code>gpt-6.7-codex-ultra-preview</code>
</span>
</label>
<Button className="sm:mt-6" type="submit">
Add model
</Button>
</form>
{customModelError ? (
<p className="text-xs text-destructive">{customModelError}</p>
) : null}
<div className="space-y-2">
<div className="flex items-center justify-between gap-2 text-xs text-muted-foreground">
<p>Saved custom models: {customCodexModels.length}</p>
{customCodexModels.length > 0 ? (
<Button
size="xs"
variant="outline"
onClick={() =>
updateSettings({
customCodexModels: defaults.customCodexModels,
})
}
>
Reset custom models
</Button>
) : null}
</div>
{customCodexModels.length > 0 ? (
<div className="space-y-2">
{customCodexModels.map((slug) => (
<div
key={slug}
className="flex items-center justify-between gap-3 rounded-lg border border-border bg-background px-3 py-2"
>
<code className="min-w-0 flex-1 truncate text-xs text-foreground">
{slug}
</code>
<Button
size="xs"
variant="ghost"
onClick={() => removeCustomModel(slug)}
>
Remove
</Button>
</div>
))}
</div>
) : (
<div className="rounded-lg border border-dashed border-border bg-background px-3 py-4 text-xs text-muted-foreground">
No custom models saved yet.
</div>
)}
</div>
</div>
</section>
<section className="rounded-2xl border border-border bg-card p-5">
<div className="mb-4">
<h2 className="text-sm font-medium text-foreground">Runtime Mode</h2>
+7 -2
View File
@@ -22,9 +22,14 @@ describe("normalizeModelSlug", () => {
});
describe("resolveModelSlug", () => {
it("returns default for missing and unknown models", () => {
it("returns default only when the model is missing", () => {
expect(resolveModelSlug(undefined)).toBe(DEFAULT_MODEL);
expect(resolveModelSlug("gpt-4.1")).toBe(DEFAULT_MODEL);
expect(resolveModelSlug(null)).toBe(DEFAULT_MODEL);
});
it("preserves unknown custom models", () => {
expect(resolveModelSlug("gpt-4.1")).toBe("gpt-4.1");
expect(resolveModelSlug("custom/internal-model")).toBe("custom/internal-model");
});
it("resolves only supported model options", () => {
+5 -8
View File
@@ -5,11 +5,12 @@ export const MODEL_OPTIONS = [
{ slug: "gpt-5.2", name: "GPT-5.2" },
] as const;
export type ModelSlug = (typeof MODEL_OPTIONS)[number]["slug"];
export type BuiltInModelSlug = (typeof MODEL_OPTIONS)[number]["slug"];
export type ModelSlug = string;
export const DEFAULT_MODEL = "gpt-5.3-codex";
export const MODEL_SLUG_ALIASES: Record<string, ModelSlug> = {
export const MODEL_SLUG_ALIASES: Record<string, BuiltInModelSlug> = {
"5.3": "gpt-5.3-codex",
"gpt-5.3": "gpt-5.3-codex",
"5.3-spark": "gpt-5.3-codex-spark",
@@ -26,16 +27,12 @@ export function normalizeModelSlug(model: string | null | undefined): ModelSlug
return null;
}
return MODEL_SLUG_ALIASES[trimmed] ?? (trimmed as ModelSlug);
return MODEL_SLUG_ALIASES[trimmed] ?? trimmed;
}
export function resolveModelSlug(model: string | null | undefined): ModelSlug {
const normalized = normalizeModelSlug(model);
if (!normalized) {
return DEFAULT_MODEL;
}
return MODEL_OPTIONS.some((option) => option.slug === normalized) ? normalized : DEFAULT_MODEL;
return normalized ?? DEFAULT_MODEL;
}
export const REASONING_OPTIONS = ["xhigh", "high", "medium", "low"] as const;