Skip to content

Commit 30e33a3

Browse files
feat(web): continue conversations in new threads
1 parent e2d4d12 commit 30e33a3

9 files changed

Lines changed: 528 additions & 1 deletion

‎apps/web/src/components/Sidebar.tsx‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ import { useProjects, useThreadShells } from "../state/entities";
111111
import { environmentServerConfigsAtom, primaryServerKeybindingsAtom } from "../state/server";
112112
import { vcsEnvironment } from "../state/vcs";
113113
import { threadEnvironment } from "../state/threads";
114+
import { continueThreadInNewDraft } from "../threadContinuation";
114115
import { useEnvironmentQuery } from "../state/query";
115116
import { useAtomCommand } from "../state/use-atom-command";
116117
import {
@@ -3078,6 +3079,7 @@ export default function Sidebar() {
30783079
api.contextMenu.show(
30793080
buildThreadActionMenuItems({
30803081
branch: thread.branch ?? null,
3082+
hasConversation: thread.latestUserMessageAt !== null,
30813083
isPinned,
30823084
isSettled,
30833085
isSnoozed,
@@ -3105,6 +3107,38 @@ export default function Sidebar() {
31053107
return;
31063108
}
31073109
switch (clicked.value) {
3110+
case "continue-in-new-thread": {
3111+
const result = await continueThreadInNewDraft({
3112+
threadRef,
3113+
createDraft: () =>
3114+
handleNewThreadRef.current(
3115+
scopeProjectRef(thread.environmentId, thread.projectId),
3116+
{
3117+
branch: thread.branch,
3118+
worktreePath: thread.worktreePath,
3119+
envMode: thread.worktreePath ? "worktree" : "local",
3120+
startFromOrigin: false,
3121+
},
3122+
),
3123+
});
3124+
if (!result.ok) {
3125+
toastManager.add(
3126+
stackedThreadToast({
3127+
type: "error",
3128+
title: "Could not continue thread",
3129+
description:
3130+
result.error instanceof Error ? result.error.message : "An error occurred.",
3131+
}),
3132+
);
3133+
return;
3134+
}
3135+
toastManager.add({
3136+
type: "success",
3137+
title: "Conversation context added",
3138+
description: "Choose any provider, edit the handoff if needed, then send.",
3139+
});
3140+
return;
3141+
}
31083142
case "new-thread-on-branch": {
31093143
// Explicit branch carry-over: reuse the thread's worktree when it
31103144
// has one, otherwise its branch on the local checkout.

‎apps/web/src/components/threadActionMenu.logic.test.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { buildThreadActionMenuItems, type ThreadActionMenuState } from "./thread
44

55
const baseState: ThreadActionMenuState = {
66
branch: null,
7+
hasConversation: true,
78
isPinned: false,
89
isSettled: false,
910
isSnoozed: false,
@@ -33,7 +34,7 @@ describe("buildThreadActionMenuItems", () => {
3334
...baseState,
3435
supports: { settlement: false, snooze: false, pinning: false, titleRegeneration: false },
3536
}),
36-
).toEqual(["rename", "mark-unread", "copy", "archive", "delete"]);
37+
).toEqual(["continue-in-new-thread", "rename", "mark-unread", "copy", "archive", "delete"]);
3738
});
3839

3940
it("includes branch items only for threads with a branch", () => {
@@ -66,6 +67,20 @@ describe("buildThreadActionMenuItems", () => {
6667
expect(item).toMatchObject({ label: "Regenerating…", disabled: true });
6768
});
6869

70+
it("disables continuation without settled conversation context", () => {
71+
const withoutMessages = buildThreadActionMenuItems({
72+
...baseState,
73+
hasConversation: false,
74+
}).find((candidate) => candidate.id === "continue-in-new-thread");
75+
const whileRunning = buildThreadActionMenuItems({
76+
...baseState,
77+
isRunning: true,
78+
}).find((candidate) => candidate.id === "continue-in-new-thread");
79+
80+
expect(withoutMessages?.disabled).toBe(true);
81+
expect(whileRunning?.disabled).toBe(true);
82+
});
83+
6984
it("marks delete as destructive and keeps it last", () => {
7085
const items = buildThreadActionMenuItems({ ...baseState, branch: "main" });
7186
expect(items.at(-1)).toMatchObject({ id: "delete", destructive: true });

‎apps/web/src/components/threadActionMenu.logic.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { SnoozePreset } from "@t3tools/client-runtime/state/thread-settled"
77
* remains data-driven.
88
*/
99
export type ThreadActionMenuId =
10+
| "continue-in-new-thread"
1011
| "new-thread-on-branch"
1112
| "pin"
1213
| "unpin"
@@ -27,6 +28,7 @@ export type ThreadActionMenuId =
2728

2829
export interface ThreadActionMenuState {
2930
readonly branch: string | null;
31+
readonly hasConversation: boolean;
3032
readonly isPinned: boolean;
3133
readonly isSettled: boolean;
3234
readonly isSnoozed: boolean;
@@ -52,6 +54,12 @@ export function buildThreadActionMenuItems(
5254
state: ThreadActionMenuState,
5355
): ReadonlyArray<ContextMenuItem<ThreadActionMenuId>> {
5456
return [
57+
{
58+
id: "continue-in-new-thread",
59+
label: "Continue in new thread",
60+
icon: "message-square-plus",
61+
disabled: !state.hasConversation || state.isRunning,
62+
},
5563
...(state.branch
5664
? [
5765
{

‎apps/web/src/hooks/useThreadActionMenu.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
readThreadShell,
3131
} from "../state/entities";
3232
import { readLocalApi } from "../localApi";
33+
import { continueThreadInNewDraft } from "../threadContinuation";
3334
import { useUiStateStore } from "../uiStateStore";
3435
import { useCopyToClipboard } from "./useCopyToClipboard";
3536
import { useNewThreadHandler } from "./useHandleNewThread";
@@ -126,6 +127,7 @@ export function useThreadActionMenu(input: {
126127
const snoozePresets = resolveSnoozePresets(now, timestampFormat);
127128
const items = buildThreadActionMenuItems({
128129
branch: thread.branch ?? null,
130+
hasConversation: thread.latestUserMessageAt !== null,
129131
isPinned: thread.pinnedAt != null,
130132
isSettled:
131133
supports.settlement &&
@@ -187,6 +189,28 @@ export function useThreadActionMenu(input: {
187189
}
188190
};
189191
switch (action) {
192+
case "continue-in-new-thread": {
193+
const result = await continueThreadInNewDraft({
194+
threadRef,
195+
createDraft: () =>
196+
handleNewThread(scopeProjectRef(threadRef.environmentId, thread.projectId), {
197+
branch: thread.branch,
198+
worktreePath: thread.worktreePath,
199+
envMode: thread.worktreePath ? "worktree" : "local",
200+
startFromOrigin: false,
201+
}),
202+
});
203+
if (!result.ok) {
204+
failureToast("Could not continue thread", result.error);
205+
return;
206+
}
207+
toastManager.add({
208+
type: "success",
209+
title: "Conversation context added",
210+
description: "Choose any provider, edit the handoff if needed, then send.",
211+
});
212+
return;
213+
}
190214
case "new-thread-on-branch": {
191215
// Explicit branch carry-over: reuse the thread's worktree when it
192216
// has one, otherwise its branch on the local checkout.
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import {
2+
EnvironmentId,
3+
MessageId,
4+
ProviderInstanceId,
5+
ThreadId,
6+
TurnId,
7+
type OrchestrationMessage,
8+
} from "@t3tools/contracts";
9+
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
10+
11+
const drafts = vi.hoisted(() => ({
12+
setModelSelection: vi.fn(),
13+
setRuntimeMode: vi.fn(),
14+
setInteractionMode: vi.fn(),
15+
setPrompt: vi.fn(),
16+
}));
17+
18+
vi.mock("./composerDraftStore", () => ({
19+
useComposerDraftStore: { getState: () => drafts },
20+
}));
21+
vi.mock("./state/entities", () => ({ readThreadDetail: vi.fn() }));
22+
vi.mock("./state/threads", () => ({
23+
environmentThreadDetails: { detailAtom: vi.fn(() => Symbol("detail")) },
24+
}));
25+
vi.mock("./rpc/atomRegistry", () => ({
26+
appAtomRegistry: { subscribe: vi.fn() },
27+
}));
28+
29+
import type { DraftId } from "./composerDraftStore";
30+
import { appAtomRegistry } from "./rpc/atomRegistry";
31+
import { readThreadDetail } from "./state/entities";
32+
import { continueThreadInNewDraft } from "./threadContinuation";
33+
34+
const threadRef = {
35+
environmentId: EnvironmentId.make("environment-1"),
36+
threadId: ThreadId.make("thread-1"),
37+
};
38+
const draftId = "draft-1" as DraftId;
39+
40+
function message(role: OrchestrationMessage["role"], text: string): OrchestrationMessage {
41+
return {
42+
id: MessageId.make(`message-${role}`),
43+
role,
44+
text,
45+
turnId: TurnId.make("turn-1"),
46+
streaming: false,
47+
createdAt: "2026-08-28T00:00:00.000Z",
48+
updatedAt: "2026-08-28T00:00:00.000Z",
49+
};
50+
}
51+
52+
function sourceThread(messages: ReadonlyArray<OrchestrationMessage>) {
53+
return {
54+
title: "Source thread",
55+
modelSelection: {
56+
instanceId: ProviderInstanceId.make("claude"),
57+
model: "claude-opus-5",
58+
options: [{ id: "effort", value: "high" }],
59+
},
60+
runtimeMode: "full-access" as const,
61+
interactionMode: "plan" as const,
62+
messages,
63+
} as unknown as NonNullable<ReturnType<typeof readThreadDetail>>;
64+
}
65+
66+
describe("continueThreadInNewDraft", () => {
67+
beforeEach(() => {
68+
vi.clearAllMocks();
69+
});
70+
71+
it("creates a draft with source context and source provider state", async () => {
72+
vi.mocked(readThreadDetail).mockReturnValue(
73+
sourceThread([message("user", "Diagnose it"), message("assistant", "Found the cause")]),
74+
);
75+
const createDraft = vi.fn(async () => ({ draftId }));
76+
77+
const result = await continueThreadInNewDraft({ threadRef, createDraft });
78+
79+
expect(result).toEqual({ ok: true });
80+
expect(createDraft).toHaveBeenCalledOnce();
81+
expect(drafts.setModelSelection).toHaveBeenCalledWith(
82+
draftId,
83+
expect.objectContaining({ instanceId: "claude", model: "claude-opus-5" }),
84+
{ replaceOptions: true },
85+
);
86+
expect(drafts.setRuntimeMode).toHaveBeenCalledWith(draftId, "full-access");
87+
expect(drafts.setInteractionMode).toHaveBeenCalledWith(draftId, "plan");
88+
expect(drafts.setPrompt).toHaveBeenCalledWith(
89+
draftId,
90+
expect.stringContaining("### Assistant\n\nFound the cause"),
91+
);
92+
});
93+
94+
it("loads uncached thread detail through the subscribed atom", async () => {
95+
const source = sourceThread([message("user", "Load this context")]);
96+
vi.mocked(readThreadDetail).mockReturnValue(null);
97+
const dispose = vi.fn();
98+
vi.mocked(appAtomRegistry.subscribe).mockImplementation((_atom, listener) => {
99+
listener(source);
100+
return dispose;
101+
});
102+
103+
const result = await continueThreadInNewDraft({
104+
threadRef,
105+
createDraft: async () => ({ draftId }),
106+
});
107+
108+
expect(result).toEqual({ ok: true });
109+
expect(dispose).toHaveBeenCalledOnce();
110+
expect(drafts.setPrompt).toHaveBeenCalledWith(
111+
draftId,
112+
expect.stringContaining("Load this context"),
113+
);
114+
});
115+
116+
it("does not create a draft when no completed portable context exists", async () => {
117+
vi.mocked(readThreadDetail).mockReturnValue(sourceThread([message("system", "hidden")]));
118+
const createDraft = vi.fn(async () => ({ draftId }));
119+
120+
const result = await continueThreadInNewDraft({ threadRef, createDraft });
121+
122+
expect(result).toMatchObject({ ok: false });
123+
expect(createDraft).not.toHaveBeenCalled();
124+
expect(drafts.setPrompt).not.toHaveBeenCalled();
125+
});
126+
127+
it("reports a missing destination without mutating composer state", async () => {
128+
vi.mocked(readThreadDetail).mockReturnValue(sourceThread([message("user", "Continue")]));
129+
130+
const result = await continueThreadInNewDraft({
131+
threadRef,
132+
createDraft: async () => null,
133+
});
134+
135+
expect(result).toMatchObject({ ok: false });
136+
expect(drafts.setPrompt).not.toHaveBeenCalled();
137+
});
138+
});

0 commit comments

Comments
 (0)