GeezerVison is showing github.com — refactor(web): isolate remaining composer fork wiring (#509) · adampeterhiggins/t3code@fcaed53
File treeExpand file treeCollapse file tree Open diff view settings apps/web/src/components/chat
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/chat/ChatComposer.tsx +79-473Lines changed: 79 additions & 473 deletions Large diffs are not rendered by default. Collapse file Copy file name to clipboard +39Lines changed: 39 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { EllipsisIcon } from "lucide-react"; | | 2 | +import type { ChatComposerProps } from "./ChatComposer"; | | 3 | +import { shortcutLabelForCommand } from "../../keybindings"; | | 4 | +import { Button } from "../ui/button"; | | 5 | +import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; | | 6 | + | | 7 | +export function ComposerStartFromButton({ | | 8 | + onStartFrom, | | 9 | + keybindings, | | 10 | + terminalOpen, | | 11 | +}: Pick<ChatComposerProps, "onStartFrom" | "keybindings" | "terminalOpen">) { | | 12 | + const startFromShortcutLabel = onStartFrom | | 13 | + ? shortcutLabelForCommand(keybindings, "chat.startFrom", { | | 14 | + context: { terminalFocus: false, terminalOpen, modelPickerOpen: false }, | | 15 | + }) | | 16 | + : null; | | 17 | + return ( | | 18 | + <Tooltip> | | 19 | + <TooltipTrigger | | 20 | + render={ | | 21 | + <Button | | 22 | + type="button" | | 23 | + variant="ghost" | | 24 | + size="icon-sm" | | 25 | + className="absolute end-2 top-2 z-10 sm:end-2.5 sm:top-2.5" | | 26 | + aria-label="Start from a pull request, branch, issue, or automation" | | 27 | + onClick={onStartFrom} | | 28 | + /> | | 29 | + } | | 30 | + > | | 31 | + <EllipsisIcon /> | | 32 | + </TooltipTrigger> | | 33 | + <TooltipPopup> | | 34 | + Start from a PR, branch, issue, or automation | | 35 | + {startFromShortcutLabel ? ` (${startFromShortcutLabel})` : null} | | 36 | + </TooltipPopup> | | 37 | + </Tooltip> | | 38 | + ); | | 39 | +} |
Collapse file Copy file name to clipboard +87Lines changed: 87 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { EnvironmentId, ProviderInstanceId, ThreadId } from "@t3tools/contracts"; | | 2 | +import { describe, expect, it } from "vite-plus/test"; | | 3 | +import { composerPathChatItems, resolveComposerPathTab } from "./composerPathMenu"; | | 4 | + | | 5 | +const defaults = { | | 6 | + rangeStart: 2, | | 7 | + choice: null, | | 8 | + agentsAvailable: true, | | 9 | + prefersAgents: false, | | 10 | + prefersModels: false, | | 11 | + query: "chat", | | 12 | + chatCount: 1, | | 13 | + fileCount: 0, | | 14 | + filesLoading: false, | | 15 | +}; | | 16 | + | | 17 | +describe("resolveComposerPathTab", () => { | | 18 | + it("keeps a manual tab for the current token, then returns to query selection", () => { | | 19 | + const choice = { rangeStart: 2, tab: "files" as const }; | | 20 | + expect(resolveComposerPathTab({ ...defaults, choice, prefersModels: true })).toBe("files"); | | 21 | + expect( | | 22 | + resolveComposerPathTab({ ...defaults, choice, rangeStart: 7, prefersModels: true }), | | 23 | + ).toBe("models"); | | 24 | + expect( | | 25 | + resolveComposerPathTab({ ...defaults, choice, rangeStart: null, prefersModels: true }), | | 26 | + ).toBe("models"); | | 27 | + }); | | 28 | + | | 29 | + it("ignores a manual Agents choice once agents are unavailable", () => { | | 30 | + expect( | | 31 | + resolveComposerPathTab({ | | 32 | + ...defaults, | | 33 | + choice: { rangeStart: 2, tab: "agents" }, | | 34 | + agentsAvailable: false, | | 35 | + }), | | 36 | + ).toBe("chats"); | | 37 | + }); | | 38 | + | | 39 | + it("prefers agent handles to model names, and model names to chat matches", () => { | | 40 | + expect(resolveComposerPathTab({ ...defaults, prefersAgents: true, prefersModels: true })).toBe( | | 41 | + "agents", | | 42 | + ); | | 43 | + expect(resolveComposerPathTab({ ...defaults, prefersModels: true })).toBe("models"); | | 44 | + expect(resolveComposerPathTab(defaults)).toBe("chats"); | | 45 | + }); | | 46 | + | | 47 | + it("waits for files before automatically choosing Chats and keeps blank queries on Files", () => { | | 48 | + expect(resolveComposerPathTab({ ...defaults, filesLoading: true })).toBe("files"); | | 49 | + expect(resolveComposerPathTab({ ...defaults, fileCount: 1 })).toBe("files"); | | 50 | + expect(resolveComposerPathTab({ ...defaults, query: " " })).toBe("files"); | | 51 | + expect(resolveComposerPathTab({ ...defaults, chatCount: 0 })).toBe("files"); | | 52 | + }); | | 53 | +}); | | 54 | + | | 55 | +it("lists matching sibling tabs first and excludes their duplicate thread results", () => { | | 56 | + const environmentId = EnvironmentId.make("env"); | | 57 | + const activeThreadId = ThreadId.make("current"); | | 58 | + const sibling = ThreadId.make("sibling"); | | 59 | + const other = ThreadId.make("other"); | | 60 | + const modelSelection = { instanceId: ProviderInstanceId.make("codex"), model: "gpt-6" }; | | 61 | + const items = composerPathChatItems({ | | 62 | + query: " FIX ", | | 63 | + activeThreadId, | | 64 | + threadTabs: [ | | 65 | + { threadId: activeThreadId, title: "Fix current", modelSelection }, | | 66 | + { threadId: sibling, title: "Fix sibling", modelSelection }, | | 67 | + { threadId: ThreadId.make("unmatched"), title: "Other tab", modelSelection }, | | 68 | + ], | | 69 | + items: [ | | 70 | + { | | 71 | + id: "sibling-result", | | 72 | + type: "thread", | | 73 | + thread: { environmentId, threadId: sibling }, | | 74 | + label: "Fix sibling", | | 75 | + description: "", | | 76 | + }, | | 77 | + { | | 78 | + id: "other-result", | | 79 | + type: "thread", | | 80 | + thread: { environmentId, threadId: other }, | | 81 | + label: "Fix other", | | 82 | + description: "", | | 83 | + }, | | 84 | + ], | | 85 | + }); | | 86 | + expect(items.map((item) => item.id)).toEqual([`thread-tab:${sibling}`, "other-result"]); | | 87 | +}); |
Collapse file Copy file name to clipboard +61Lines changed: 61 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import type { ThreadId, ThreadTabGroup } from "@t3tools/contracts"; | | 2 | +import type { ComposerCommandItem } from "./ComposerCommandMenu"; | | 3 | + | | 4 | +export type ComposerPathTab = "files" | "chats" | "agents" | "models"; | | 5 | + | | 6 | +export function composerPathChatItems({ | | 7 | + query, | | 8 | + threadTabs, | | 9 | + activeThreadId, | | 10 | + items, | | 11 | +}: { | | 12 | + query: string; | | 13 | + threadTabs: ThreadTabGroup["tabs"]; | | 14 | + activeThreadId: ThreadId | null; | | 15 | + items: ReadonlyArray<ComposerCommandItem>; | | 16 | +}): ComposerCommandItem[] { | | 17 | + const tabQuery = query.trim().toLowerCase(); | | 18 | + const tabs = threadTabs; | | 19 | + const tabThreadIds = new Set(tabs.map((tab) => tab.threadId)); | | 20 | + return [ | | 21 | + ...tabs | | 22 | + .filter( | | 23 | + (tab) => tab.threadId !== activeThreadId && tab.title.toLowerCase().includes(tabQuery), | | 24 | + ) | | 25 | + .map((tab) => ({ | | 26 | + id: `thread-tab:${tab.threadId}`, | | 27 | + type: "thread-tab" as const, | | 28 | + threadId: tab.threadId, | | 29 | + label: tab.title, | | 30 | + description: "Chat tab", | | 31 | + })), | | 32 | + ...items.filter((item) => item.type === "thread" && !tabThreadIds.has(item.thread.threadId)), | | 33 | + ]; | | 34 | +} | | 35 | + | | 36 | +export function resolveComposerPathTab(input: { | | 37 | + rangeStart: number | null; | | 38 | + choice: { rangeStart: number; tab: ComposerPathTab } | null; | | 39 | + agentsAvailable: boolean; | | 40 | + prefersAgents: boolean; | | 41 | + prefersModels: boolean; | | 42 | + query: string; | | 43 | + chatCount: number; | | 44 | + fileCount: number; | | 45 | + filesLoading: boolean; | | 46 | +}): ComposerPathTab { | | 47 | + if ( | | 48 | + input.rangeStart !== null && | | 49 | + input.choice?.rangeStart === input.rangeStart && | | 50 | + (input.choice.tab !== "agents" || input.agentsAvailable) | | 51 | + ) | | 52 | + return input.choice.tab; | | 53 | + if (input.prefersAgents) return "agents"; | | 54 | + if (input.prefersModels) return "models"; | | 55 | + return input.query.trim().length > 0 && | | 56 | + input.chatCount > 0 && | | 57 | + !input.filesLoading && | | 58 | + input.fileCount === 0 | | 59 | + ? "chats" | | 60 | + : "files"; | | 61 | +} |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/chat/messageDraftComposer.test.ts +100-2Lines changed: 100 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -import type { ChatFileAttachment, ChatImageAttachment } from "@t3tools/contracts"; | | 1 | +import { | | 2 | + MessageDraftId, | | 3 | + ThreadId, | | 4 | + type MessageDraft, | | 5 | + type ChatFileAttachment, | | 6 | + type ChatImageAttachment, | | 7 | +} from "@t3tools/contracts"; | 2 | 8 | import { describe, expect, it } from "vite-plus/test"; | 3 | 9 |
| 4 | 10 | import { attachmentContextRecord } from "../../lib/composerContextRecords"; | 5 | | -import { messageDraftComposerRecords } from "./messageDraftComposer"; | | 11 | +import { messageDraftComposerRecords, openMessageDraftInComposer } from "./messageDraftComposer"; | 6 | 12 |
| 7 | 13 | const image: ChatImageAttachment = { | 8 | 14 | type: "image", | @@ -40,3 +46,95 @@ describe("messageDraftComposerRecords", () => { | 40 | 46 | ]); | 41 | 47 | }); | 42 | 48 | }); | | 49 | + | | 50 | +function deferred<T>() { | | 51 | + let resolve!: (value: T) => void; | | 52 | + const promise = new Promise<T>((complete) => { | | 53 | + resolve = complete; | | 54 | + }); | | 55 | + return { promise, resolve }; | | 56 | +} | | 57 | + | | 58 | +function draftOpening() { | | 59 | + const draft = { | | 60 | + id: MessageDraftId.make("draft"), | | 61 | + threadId: ThreadId.make("thread"), | | 62 | + text: "saved message", | | 63 | + attachments: [], | | 64 | + origin: "web" as const, | | 65 | + createdAt: "2026-10-27T12:00:00.000Z", | | 66 | + }; | | 67 | + const composer = { targetKey: "thread", hasContent: false, hasPrompt: false }; | | 68 | + const loaded: MessageDraft[] = []; | | 69 | + const returned: MessageDraft[] = []; | | 70 | + const input = { | | 71 | + targetKey: "thread", | | 72 | + take: async () => draft, | | 73 | + putBack: async (value: MessageDraft) => { | | 74 | + returned.push(value); | | 75 | + }, | | 76 | + readComposer: () => composer, | | 77 | + saveCurrent: async () => true, | | 78 | + waitForRender: async () => {}, | | 79 | + load: (value: MessageDraft) => { | | 80 | + loaded.push(value); | | 81 | + }, | | 82 | + }; | | 83 | + return { draft, composer, loaded, returned, input }; | | 84 | +} | | 85 | + | | 86 | +describe("openMessageDraftInComposer", () => { | | 87 | + it("returns the taken draft when the user switches composers during the request", async () => { | | 88 | + const state = draftOpening(); | | 89 | + const take = deferred<MessageDraft | null>(); | | 90 | + const opening = openMessageDraftInComposer({ ...state.input, take: () => take.promise }); | | 91 | + state.composer.targetKey = "another-thread"; | | 92 | + take.resolve(state.draft); | | 93 | + await opening; | | 94 | + expect(state.returned).toEqual([state.draft]); | | 95 | + expect(state.loaded).toEqual([]); | | 96 | + }); | | 97 | + | | 98 | + it.each(["save failed", "prompt remains"])("keeps existing content when %s", async (reason) => { | | 99 | + const state = draftOpening(); | | 100 | + state.composer.hasContent = true; | | 101 | + state.composer.hasPrompt = reason === "prompt remains"; | | 102 | + await openMessageDraftInComposer({ | | 103 | + ...state.input, | | 104 | + saveCurrent: async () => reason !== "save failed", | | 105 | + }); | | 106 | + expect(state.returned).toEqual([state.draft]); | | 107 | + expect(state.loaded).toEqual([]); | | 108 | + }); | | 109 | + | | 110 | + it("waits for the saved composer's render before loading the taken draft", async () => { | | 111 | + const state = draftOpening(); | | 112 | + state.composer.hasContent = true; | | 113 | + const rendered = deferred<void>(); | | 114 | + const waiting = deferred<void>(); | | 115 | + const opening = openMessageDraftInComposer({ | | 116 | + ...state.input, | | 117 | + saveCurrent: async () => { | | 118 | + state.composer.hasContent = false; | | 119 | + return true; | | 120 | + }, | | 121 | + waitForRender: () => { | | 122 | + waiting.resolve(); | | 123 | + return rendered.promise; | | 124 | + }, | | 125 | + }); | | 126 | + await waiting.promise; | | 127 | + expect(state.loaded).toEqual([]); | | 128 | + rendered.resolve(); | | 129 | + await opening; | | 130 | + expect(state.loaded).toEqual([state.draft]); | | 131 | + expect(state.returned).toEqual([]); | | 132 | + }); | | 133 | + | | 134 | + it("leaves the composer alone when another device already took the draft", async () => { | | 135 | + const state = draftOpening(); | | 136 | + await openMessageDraftInComposer({ ...state.input, take: async () => null }); | | 137 | + expect(state.loaded).toEqual([]); | | 138 | + expect(state.returned).toEqual([]); | | 139 | + }); | | 140 | +}); |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/chat/messageDraftComposer.ts +25Lines changed: 25 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -39,3 +39,28 @@ export function messageDraftComposerRecords( | 39 | 39 | ); | 40 | 40 | return [...records, ...unbound]; | 41 | 41 | } | | 42 | + | | 43 | +export async function openMessageDraftInComposer(input: { | | 44 | + targetKey: string; | | 45 | + take: () => Promise<MessageDraft | null>; | | 46 | + putBack: (draft: MessageDraft) => Promise<unknown>; | | 47 | + readComposer: () => { targetKey: string; hasContent: boolean; hasPrompt: boolean }; | | 48 | + saveCurrent: () => Promise<boolean>; | | 49 | + waitForRender: () => Promise<unknown>; | | 50 | + load: (draft: MessageDraft) => void; | | 51 | +}) { | | 52 | + const draft = await input.take(); | | 53 | + if (draft === null) return; | | 54 | + if (input.readComposer().targetKey !== input.targetKey) { | | 55 | + await input.putBack(draft); | | 56 | + return; | | 57 | + } | | 58 | + if (input.readComposer().hasContent) { | | 59 | + if (!(await input.saveCurrent()) || input.readComposer().hasPrompt) { | | 60 | + await input.putBack(draft); | | 61 | + return; | | 62 | + } | | 63 | + await input.waitForRender(); | | 64 | + } | | 65 | + input.load(draft); | | 66 | +} |
|