Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 77 additions & 1 deletion apps/web/src/appSettings.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,55 @@
import { describe, expect, it } from "vitest";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

import {
getAppSettingsSnapshot,
getAppModelOptions,
getSlashModelOptions,
normalizeCustomModelSlugs,
resolveAppModelSelection,
} from "./appSettings";

function createStorage() {
const values = new Map<string, string>();
return {
getItem: (key: string) => values.get(key) ?? null,
setItem: (key: string, value: string) => {
values.set(key, value);
},
removeItem: (key: string) => {
values.delete(key);
},
clear: () => {
values.clear();
},
};
}

function writeSettings(partial: Record<string, unknown>) {
localStorage.setItem(
"t3code:app-settings:v1",
JSON.stringify({
codexBinaryPath: "",
codexHomePath: "",
confirmThreadDelete: true,
enableAssistantStreaming: false,
customCodexModels: [],
...partial,
}),
);
}

beforeEach(() => {
const storage = createStorage();
vi.stubGlobal("localStorage", storage);
vi.stubGlobal("window", {
localStorage: storage,
});
});

afterEach(() => {
vi.unstubAllGlobals();
});

describe("normalizeCustomModelSlugs", () => {
it("normalizes aliases, removes built-ins, and deduplicates values", () => {
expect(
Expand Down Expand Up @@ -72,3 +115,36 @@ describe("getSlashModelOptions", () => {
expect(options.map((option) => option.slug)).toEqual(["openai/gpt-oss-120b"]);
});
});

describe("getAppSettingsSnapshot", () => {
it("defaults the thread environment mode to local for older persisted settings", () => {
localStorage.setItem(
"t3code:app-settings:v1",
JSON.stringify({
codexBinaryPath: "/usr/local/bin/codex",
codexHomePath: "",
confirmThreadDelete: true,
enableAssistantStreaming: false,
customCodexModels: [],
}),
);

expect(getAppSettingsSnapshot().defaultThreadEnvMode).toBe("local");
});

it("falls back to local when the persisted thread environment mode is invalid", () => {
writeSettings({
defaultThreadEnvMode: "invalid",
});

expect(getAppSettingsSnapshot().defaultThreadEnvMode).toBe("local");
});

it("reads a persisted worktree default for new threads", () => {
writeSettings({
defaultThreadEnvMode: "worktree",
});

expect(getAppSettingsSnapshot().defaultThreadEnvMode).toBe("worktree");
});
});
3 changes: 3 additions & 0 deletions apps/web/src/appSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const AppSettingsSchema = Schema.Struct({
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)),
Expand Down
20 changes: 20 additions & 0 deletions apps/web/src/components/Sidebar.logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";

import {
hasUnseenCompletion,
resolveSidebarNewThreadEnvMode,
resolveThreadStatusPill,
shouldClearThreadSelectionOnMouseDown,
} from "./Sidebar.logic";
Expand Down Expand Up @@ -62,6 +63,25 @@ describe("shouldClearThreadSelectionOnMouseDown", () => {
});
});

describe("resolveSidebarNewThreadEnvMode", () => {
it("uses the app default when the caller does not request a specific mode", () => {
expect(
resolveSidebarNewThreadEnvMode({
defaultEnvMode: "worktree",
}),
).toBe("worktree");
});

it("preserves an explicit requested mode over the app default", () => {
expect(
resolveSidebarNewThreadEnvMode({
requestedEnvMode: "local",
defaultEnvMode: "worktree",
}),
).toBe("local");
});
});

describe("resolveThreadStatusPill", () => {
const baseThread = {
interactionMode: "plan" as const,
Expand Down
8 changes: 8 additions & 0 deletions apps/web/src/components/Sidebar.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Thread } from "../types";
import { findLatestProposedPlan, isLatestTurnSettled } from "../session-logic";

export const THREAD_SELECTION_SAFE_SELECTOR = "[data-thread-item], [data-thread-selection-safe]";
export type SidebarNewThreadEnvMode = "local" | "worktree";

export interface ThreadStatusPill {
label:
Expand Down Expand Up @@ -37,6 +38,13 @@ export function shouldClearThreadSelectionOnMouseDown(target: HTMLElement | null
return !target.closest(THREAD_SELECTION_SAFE_SELECTOR);
}

export function resolveSidebarNewThreadEnvMode(input: {
requestedEnvMode?: SidebarNewThreadEnvMode;
defaultEnvMode: SidebarNewThreadEnvMode;
}): SidebarNewThreadEnvMode {
return input.requestedEnvMode ?? input.defaultEnvMode;
}

export function resolveThreadStatusPill(input: {
thread: ThreadStatusInput;
hasPendingApprovals: boolean;
Expand Down
25 changes: 20 additions & 5 deletions apps/web/src/components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ import {
import { useThreadSelectionStore } from "../threadSelectionStore";
import { formatWorktreePathForDisplay, getOrphanedWorktreePathForThread } from "../worktreeCleanup";
import { isNonEmpty as isNonEmptyString } from "effect/String";
import { resolveThreadStatusPill, shouldClearThreadSelectionOnMouseDown } from "./Sidebar.logic";
import {
resolveSidebarNewThreadEnvMode,
resolveThreadStatusPill,
shouldClearThreadSelectionOnMouseDown,
} from "./Sidebar.logic";

const EMPTY_KEYBINDINGS: ResolvedKeybindingsConfig = [];
const THREAD_PREVIEW_LIMIT = 6;
Expand Down Expand Up @@ -310,6 +314,7 @@ export default function Sidebar() {
const setSelectionAnchor = useThreadSelectionStore((s) => s.setAnchor);
const shouldBrowseForProjectImmediately = isElectron;
const shouldShowProjectPathEntry = addingProject && !shouldBrowseForProjectImmediately;
const defaultNewThreadEnvMode = appSettings.defaultThreadEnvMode;
const pendingApprovalByThreadId = useMemo(() => {
const map = new Map<ThreadId, boolean>();
for (const thread of threads) {
Expand Down Expand Up @@ -451,7 +456,7 @@ export default function Sidebar() {
createdAt,
branch: options?.branch ?? null,
worktreePath: options?.worktreePath ?? null,
envMode: options?.envMode ?? "local",
envMode: options?.envMode ?? appSettings.defaultThreadEnvMode,
runtimeMode: DEFAULT_RUNTIME_MODE,
});

Expand All @@ -469,6 +474,7 @@ export default function Sidebar() {
routeThreadId,
setDraftThreadContext,
setProjectDraftThreadId,
appSettings.defaultThreadEnvMode,
],
);

Expand Down Expand Up @@ -526,7 +532,9 @@ export default function Sidebar() {
defaultModel: DEFAULT_MODEL_BY_PROVIDER.codex,
createdAt,
});
await handleNewThread(projectId).catch(() => undefined);
await handleNewThread(projectId, {
envMode: defaultNewThreadEnvMode,
}).catch(() => undefined);
} catch (error) {
const description =
error instanceof Error ? error.message : "An error occurred while adding the project.";
Expand All @@ -545,6 +553,7 @@ export default function Sidebar() {
finishAddingProject();
},
[
defaultNewThreadEnvMode,
focusMostRecentThreadForProject,
handleNewThread,
isAddingProject,
Expand Down Expand Up @@ -1053,7 +1062,9 @@ export default function Sidebar() {
activeThread?.projectId ?? activeDraftThread?.projectId ?? projects[0]?.id;
if (!projectId) return;
event.preventDefault();
void handleNewThread(projectId);
void handleNewThread(projectId, {
envMode: "local",
});
return;
}

Expand Down Expand Up @@ -1485,7 +1496,11 @@ export default function Sidebar() {
onClick={(event) => {
event.preventDefault();
event.stopPropagation();
void handleNewThread(project.id);
void handleNewThread(project.id, {
envMode: resolveSidebarNewThreadEnvMode({
defaultEnvMode: defaultNewThreadEnvMode,
}),
});
}}
>
<SquarePenIcon className="size-3.5" />
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/ui/switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function Switch({ className, ...props }: SwitchPrimitive.Root.Props) {
return (
<SwitchPrimitive.Root
className={cn(
"inline-flex h-[calc(var(--thumb-size)+2px)] w-[calc(var(--thumb-size)*2-2px)] shrink-0 items-center rounded-full p-px outline-none transition-[background-color,box-shadow] duration-200 [--thumb-size:--spacing(5)] focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background data-checked:bg-primary data-unchecked:bg-input data-disabled:opacity-64 sm:[--thumb-size:--spacing(4)]",
"inline-flex h-[calc(var(--thumb-size)+2px)] w-[calc(var(--thumb-size)*2-2px)] shrink-0 cursor-pointer items-center rounded-full p-px outline-none transition-[background-color,box-shadow] duration-200 [--thumb-size:--spacing(5)] focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background data-checked:bg-primary data-unchecked:bg-input data-disabled:cursor-not-allowed data-disabled:opacity-64 sm:[--thumb-size:--spacing(4)]",
className,
)}
data-slot="switch"
Expand Down
43 changes: 43 additions & 0 deletions apps/web/src/routes/_chat.settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,49 @@ 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">Threads</h2>
<p className="mt-1 text-xs text-muted-foreground">
Choose the default workspace mode for newly created draft threads.
</p>
</div>

<div className="flex items-center justify-between rounded-lg border border-border bg-background px-3 py-2">
<div>
<p className="text-sm font-medium text-foreground">Default to New worktree</p>
<p className="text-xs text-muted-foreground">
New threads start in New worktree mode instead of Local.
</p>
</div>
<Switch
checked={settings.defaultThreadEnvMode === "worktree"}
onCheckedChange={(checked) =>
updateSettings({
defaultThreadEnvMode: checked ? "worktree" : "local",
})
}
aria-label="Default new threads to New worktree mode"
/>
</div>

{settings.defaultThreadEnvMode !== defaults.defaultThreadEnvMode ? (
<div className="mt-3 flex justify-end">
<Button
size="xs"
variant="outline"
onClick={() =>
updateSettings({
defaultThreadEnvMode: defaults.defaultThreadEnvMode,
})
}
>
Restore default
</Button>
</div>
) : null}
</section>

<section className="rounded-2xl border border-border bg-card p-5">
<div className="mb-4">
<h2 className="text-sm font-medium text-foreground">Responses</h2>
Expand Down
Loading