Skip to content
Merged
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
9 changes: 6 additions & 3 deletions apps/web/src/components/ChatView.browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1776,11 +1776,12 @@ describe("ChatView timeline estimator parity (full app)", () => {

await newThreadButton.click();

await waitForURL(
const secondThreadPath = await waitForURL(
mounted.router,
(path) => path === threadPath,
"New-thread should reuse the existing project draft thread.",
(path) => UUID_ROUTE_RE.test(path) && path !== threadPath,
"New-thread should create a fresh draft thread UUID.",
);
const secondThreadId = secondThreadPath.slice(1) as ThreadId;
expect(useComposerDraftStore.getState().draftsByThreadId[threadId]).toMatchObject({
model: "gpt-5.4",
modelOptions: {
Expand All @@ -1790,6 +1791,8 @@ describe("ChatView timeline estimator parity (full app)", () => {
},
},
});
expect(useComposerDraftStore.getState().draftThreadsByThreadId[threadId]).toBeDefined();
expect(useComposerDraftStore.getState().draftThreadsByThreadId[secondThreadId]).toBeDefined();
} finally {
await mounted.cleanup();
}
Expand Down
38 changes: 4 additions & 34 deletions apps/web/src/components/ChatView.logic.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,21 @@
import { type MessageId, ProjectId, type ThreadId } from "@okcode/contracts";
import { type ChatMessage, type Thread } from "../types";
import { type MessageId, ProjectId } from "@okcode/contracts";
import { type ChatMessage } from "../types";
import { randomUUID } from "~/lib/utils";
import { type ComposerAttachment, type DraftThreadState } from "../composerDraftStore";
import { type ComposerAttachment } from "../composerDraftStore";
import { Schema } from "effect";
import {
filterTerminalContextsWithText,
stripInlineTerminalContextPlaceholders,
type TerminalContextDraft,
} from "../lib/terminalContext";
import { type PromptEnhancementId } from "../promptEnhancement";
import { normalizeThreadTitle } from "../threadTitle";
export { buildLocalDraftThread } from "../draftThreads";

export const LAST_INVOKED_SCRIPT_BY_PROJECT_KEY = "okcode:last-invoked-script-by-project";
const WORKTREE_BRANCH_PREFIX = "okcode";

export const LastInvokedScriptByProjectSchema = Schema.Record(ProjectId, Schema.String);

export function buildLocalDraftThread(
threadId: ThreadId,
draftThread: DraftThreadState,
fallbackModel: string,
error: string | null,
): Thread {
return {
id: threadId,
codexThreadId: null,
projectId: draftThread.projectId,
title: normalizeThreadTitle(draftThread.title),
model: fallbackModel,
runtimeMode: draftThread.runtimeMode,
interactionMode: draftThread.interactionMode,
session: null,
messages: [],
error,
createdAt: draftThread.createdAt,
latestTurn: null,
lastVisitedAt: draftThread.createdAt,
branch: draftThread.branch,
worktreePath: draftThread.worktreePath,
worktreeBaseBranch: null,
...(draftThread.githubRef ? { githubRef: draftThread.githubRef } : {}),
turnDiffSummaries: [],
activities: [],
proposedPlans: [],
};
}

export function revokeBlobPreviewUrl(previewUrl: string | undefined): void {
if (!previewUrl || typeof URL === "undefined" || !previewUrl.startsWith("blob:")) {
return;
Expand Down
58 changes: 58 additions & 0 deletions apps/web/src/components/Sidebar.logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getVisibleThreadsForProject,
getProjectSortTimestamp,
hasUnseenCompletion,
mergeDraftThreadsIntoSidebarThreads,
resolveProjectStatusIndicator,
resolveProjectNameTone,
resolveSidebarNewThreadEnvMode,
Expand All @@ -16,6 +17,7 @@ import {
sortThreadsForSidebar,
} from "./Sidebar.logic";
import { ProjectId, ThreadId } from "@okcode/contracts";
import type { DraftThreadState } from "../composerDraftStore";
import {
DEFAULT_INTERACTION_MODE,
DEFAULT_RUNTIME_MODE,
Expand Down Expand Up @@ -99,6 +101,62 @@ describe("resolveSidebarNewThreadEnvMode", () => {
});
});

describe("mergeDraftThreadsIntoSidebarThreads", () => {
it("includes preserved local drafts alongside server threads for the same project", () => {
const projectId = ProjectId.makeUnsafe("project-1");
const merged = mergeDraftThreadsIntoSidebarThreads({
serverThreads: [makeThread({ id: ThreadId.makeUnsafe("thread-server"), projectId })],
draftThreadsByThreadId: {
[ThreadId.makeUnsafe("thread-draft")]: {
projectId,
createdAt: "2026-03-09T11:00:00.000Z",
title: "Draft thread",
runtimeMode: DEFAULT_RUNTIME_MODE,
interactionMode: DEFAULT_INTERACTION_MODE,
branch: null,
worktreePath: null,
envMode: "local",
} satisfies DraftThreadState,
},
projectModelByProjectId: new Map([[projectId, "gpt-5.4"]]),
});

expect(merged.map((thread) => thread.id)).toEqual([
ThreadId.makeUnsafe("thread-server"),
ThreadId.makeUnsafe("thread-draft"),
]);
expect(merged[1]).toMatchObject({
projectId,
title: "Draft thread",
model: "gpt-5.4",
});
});

it("skips draft entries once a server thread with the same id exists", () => {
const projectId = ProjectId.makeUnsafe("project-1");
const threadId = ThreadId.makeUnsafe("thread-shared");
const merged = mergeDraftThreadsIntoSidebarThreads({
serverThreads: [makeThread({ id: threadId, projectId })],
draftThreadsByThreadId: {
[threadId]: {
projectId,
createdAt: "2026-03-09T11:00:00.000Z",
title: "Promoted draft",
runtimeMode: DEFAULT_RUNTIME_MODE,
interactionMode: DEFAULT_INTERACTION_MODE,
branch: null,
worktreePath: null,
envMode: "local",
} satisfies DraftThreadState,
},
projectModelByProjectId: new Map([[projectId, "gpt-5.4"]]),
});

expect(merged).toHaveLength(1);
expect(merged[0]?.id).toBe(threadId);
});
});

describe("resolveThreadStatusPill", () => {
const baseThread = {
interactionMode: "plan" as const,
Expand Down
25 changes: 25 additions & 0 deletions apps/web/src/components/Sidebar.logic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { DEFAULT_MODEL_BY_PROVIDER } from "@okcode/contracts";
import type { SidebarProjectSortOrder, SidebarThreadSortOrder } from "../appSettings";
import type { DraftThreadState } from "../composerDraftStore";
import { buildLocalDraftThread } from "../draftThreads";
import type { Thread } from "../types";
import { cn } from "../lib/utils";
import {
Expand Down Expand Up @@ -283,6 +286,28 @@ export function groupThreadsByProjectId<TThread extends SidebarProjectThread>(
return threadsByProjectId;
}

export function mergeDraftThreadsIntoSidebarThreads(input: {
serverThreads: readonly Thread[];
draftThreadsByThreadId: Readonly<Record<string, DraftThreadState>>;
projectModelByProjectId: ReadonlyMap<Thread["projectId"], string>;
}): Thread[] {
const serverThreadIds = new Set(input.serverThreads.map((thread) => thread.id));
const mergedThreads = [...input.serverThreads];

for (const [threadId, draftThread] of Object.entries(input.draftThreadsByThreadId)) {
if (serverThreadIds.has(threadId as Thread["id"])) {
continue;
}
const fallbackModel =
input.projectModelByProjectId.get(draftThread.projectId) ?? DEFAULT_MODEL_BY_PROVIDER.codex;
mergedThreads.push(
buildLocalDraftThread(threadId as Thread["id"], draftThread, fallbackModel, null),
);
}

return mergedThreads;
}

function toSortableTimestamp(iso: string | undefined): number | null {
if (!iso) return null;
const ms = Date.parse(iso);
Expand Down
Loading
Loading