From 4ef243875f388009023be2d0f66bd712df33cc8e Mon Sep 17 00:00:00 2001 From: Callum Alpass Date: Tue, 3 Nov 2026 02:53:53 +1100 Subject: [PATCH] Run terminals and $EDITOR in a pane beside the conversation ctrl+j opens the thread's T3 terminal in an embedded terminal that takes the details column's place (or the conversation's on narrow terminals); ctrl+g writes the message there and o on a changed file edits it. While the pane has focus every key but ctrl+\ goes to its program. Full-screen attach stays in the T picker and the palette. --- README.md | 4 +- docs/reference.md | 17 ++-- src/ui/App.tsx | 145 ++++++++++++++++++++++++------- src/ui/DetailsPanel.tsx | 21 +++-- src/ui/TerminalPane.tsx | 186 ++++++++++++++++++++++++++++++++++++++++ src/ui/editor.ts | 27 ++++-- src/ui/useDetails.ts | 10 ++- src/ui/useWorkspace.ts | 30 +++++-- test/app/harness.tsx | 10 ++- test/app/pane.test.tsx | 111 ++++++++++++++++++++++++ 10 files changed, 500 insertions(+), 61 deletions(-) create mode 100644 src/ui/TerminalPane.tsx create mode 100644 test/app/pane.test.tsx diff --git a/README.md b/README.md index 9330884..8af7e17 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,9 @@ The keys you'll use most: | `c` | new thread | | `m` | model, reasoning and access mode | | `.` | thread actions: pin, snooze, rename, archive… | -| `ctrl+j` | attach to the thread's terminal on the server | +| `ctrl+j` | the thread's terminal on the server, in a pane beside the conversation | +| `ctrl+\` | move the keys between the pane and the conversation | +| `ctrl+g` | write the message in `$EDITOR`, in the pane | | `ctrl+c` | quit | In the message box, `/`, `$`, `@` and `#` suggest commands, skills, files and threads, and pull requests. Most things also work with the mouse. diff --git a/docs/reference.md b/docs/reference.md index dff5025..cecc099 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -27,7 +27,8 @@ Any mode: | Key | Action | | --- | --- | | `ctrl+k` | command palette: every action below, plus thread search | -| `ctrl+j` | attach to the thread's terminal (see [Terminals](#terminals)) | +| `ctrl+j` | open the thread's terminal in the pane (see [Terminals](#terminals)) | +| `ctrl+\` | with the pane open: move the keys between it and the conversation | | `ctrl+n` / `ctrl+p` (or `alt+↓`/`alt+↑`) | next / previous thread | | `pgup`/`pgdn`, `ctrl+u`/`ctrl+d` | scroll the conversation (`ctrl+u`/`ctrl+d` only in navigation; while typing they edit the text) | | `ctrl+c` | quit | @@ -41,7 +42,7 @@ Navigation: | `a` | attach a file to the next message (see [Attachments](#attachments)) | | `.` | thread actions (see [Thread actions](#thread-actions)); a right-click on a thread opens them too | | `m` | model, options (reasoning, fast mode…) and access mode | -| `T` | terminals: attach, new, close or clear | +| `T` | terminals: open in the pane, attach full screen, new, close or clear | | `b` | branches and worktrees: switch, create, pull or remove | | `E` | open the workspace (or diff file) in the server's editor | | `y` / `Y` / `n` | approve / always allow this session / decline the pending approval | @@ -60,7 +61,7 @@ Navigation: | `d` | show or hide thread details (kept for the next run) | | `B` | hide or show the thread list (kept for the next run) | | `P` | show the thread's linked pull request | -| `l` / `→` | move into the details panel: `j`/`k` choose, `enter` opens, `t` turns an automation on or off, `esc` comes back | +| `l` / `→` | move into the details panel: `j`/`k` choose, `enter` opens, `o` edits the selected changed file in the pane, `t` turns an automation on or off, `esc` comes back | | `u` | go to the parent thread (from a fork or subagent) | | `:` | command palette | | `z` | show the Settled and Snoozed sections; `Show more settled threads` in the palette loads older rows | @@ -73,7 +74,7 @@ Navigation: `enter` sends. While the agent is running, `enter` steers it (the message reaches it mid-run) and `alt+enter` queues the message for after the run; the `followUpBehavior` setting swaps the two. With an approval pending, a typed message declines it with your feedback. -`shift+enter` adds a newline (it needs a terminal with the kitty keyboard protocol); so does ending the line with `\` and pressing `enter`. The box grows up to 8 lines. `ctrl+g` opens the message in `$VISUAL` or `$EDITOR` (falling back to `vi`); save and quit to bring it back. +`shift+enter` adds a newline (it needs a terminal with the kitty keyboard protocol); so does ending the line with `\` and pressing `enter`. The box grows up to 8 lines. `ctrl+g` opens the message in `$VISUAL` or `$EDITOR` (falling back to `vi`) in the terminal pane; save and quit to bring it back. With the box empty after an interrupted run (or one a usage limit stopped), `enter` continues where it left off and releases messages held in the queue. @@ -150,9 +151,13 @@ Limits: GIF, JPEG, PNG and WebP images up to 10 MB each and 80 MB in all, other ## Terminals -`ctrl+j` attaches to the thread's terminal on the T3 server, the same one the app's terminal drawer shows. If the thread has none, it starts one in the thread's worktree or project folder; with several, you pick one or start another. t3tui hands the whole screen to the terminal; `ctrl+j` or `ctrl+]` comes back, and the terminal keeps running. Its size is shared, so attaching resizes it for anyone else watching. +`ctrl+j` opens the thread's terminal on the T3 server, the same one the app's terminal drawer shows, in the pane on the right. If the thread has none, it starts one in the thread's worktree or project folder; with several, you pick one or start another. The pane takes the details column's place and width (drag its edge to resize it); on a terminal too narrow for both, it takes the conversation's place. -`T` opens the terminal picker even with only one terminal. Choose `New terminal`, `Close terminal…` or `Clear terminal…`; typing `close` or `clear` finds each terminal's action directly. Closing ends the shell and deletes its history; clearing clears its scrollback. These are in the command palette too. +While the pane has focus, every key goes to its program, `esc` included, apart from `ctrl+\`, which gives the keys back to the conversation. `ctrl+\` again, or a click on the pane, returns them; the pane's left edge shows the accent while it has them. The pane follows its size, and so does the terminal on the server: its size is shared, so it resizes for anyone else watching. When the shell exits, the pane closes; `Close the terminal pane` in the palette detaches without stopping it. + +The same pane runs `$VISUAL` or `$EDITOR` (falling back to `vi`) on this machine: `ctrl+g` writes the message there, and `o` on a file under Changes in the details panel (or its `▸ Edit`) opens that file, when the workspace is on this machine. An open editor isn't replaced by anything else; quit it first. + +`T` opens the terminal picker even with only one terminal. Choose `New terminal`, `Attach full screen` (the whole screen goes to the terminal until `ctrl+j` or `ctrl+]`), `Close terminal…` or `Clear terminal…`; typing `close` or `clear` finds each terminal's action directly. Closing ends the shell and deletes its history; clearing clears its scrollback. These are in the command palette too. ## Workspace diff --git a/src/ui/App.tsx b/src/ui/App.tsx index 67d4fde..1791948 100644 --- a/src/ui/App.tsx +++ b/src/ui/App.tsx @@ -3,7 +3,9 @@ import { useKeyboard, useRenderer, useTerminalDimensions } from "@opentui/react" import { resolveComposerDispatchMode } from "@t3tools/client-runtime/state/composer-dispatch"; import { ATTACHMENT_ONLY_BOOTSTRAP_PROMPT } from "@t3tools/shared/composerContextReferences"; import type { OrchestrationV2ThreadShell, ProviderApprovalDecision, ThreadId, TurnItemId } from "@t3tools/contracts"; +import { existsSync } from "node:fs"; import { homedir } from "node:os"; +import { resolvePathLinkTarget } from "@t3tools/shared/fileLinks"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import type { ChatAttachment, OrchestrationMessageContext } from "@t3tools/contracts"; @@ -31,7 +33,8 @@ import { useDraft } from "./useDraft.ts"; import { useModelPicker } from "./useModelPicker.ts"; import { useQuestion } from "./useQuestion.ts"; import { useThreadMenu } from "./useThreadMenu.ts"; -import { editInEditor } from "./editor.ts"; +import { editorCommand, scratchFile } from "./editor.ts"; +import { TerminalPane, type PaneProgram } from "./TerminalPane.tsx"; import { Palette } from "./Palette.tsx"; import { ApprovalPanel } from "./ApprovalPanel.tsx"; import { LiveStatus } from "./LiveStatus.tsx"; @@ -68,9 +71,10 @@ import { useUsageSummary } from "./useUsage.ts"; /** * "input": typing goes to the composer. "nav": single keys move, scroll, approve. * "details": j/k move through the details panel. "settings": j/k move through settings. + * "pane": every key but ctrl+\ goes to the program in the terminal pane. * The rest are overlays with their own input. */ -type Mode = "input" | "nav" | "filter" | "details" | "palette" | "picker" | "confirm" | "settings" | "find" | "pull-request"; +type Mode = "input" | "nav" | "filter" | "details" | "palette" | "picker" | "confirm" | "settings" | "find" | "pull-request" | "pane"; /** Holding j/k skims past threads; only subscribe to one you stop on. */ const SUBSCRIBE_DELAY_MS = 120; @@ -134,6 +138,10 @@ export function App(props: { readonly session: T3Session; readonly config: T3tui /** The edge being dragged. */ const [resizing, setResizing] = useState<"list" | "details" | null>(null); const [diff, setDiff] = useState(null); + /** The program in the right-hand pane: one of T3's terminals, or $EDITOR. */ + const [pane, setPane] = useState(null); + /** The conversation area's height, for the pane when it takes that place. */ + const [contentHeight, setContentHeight] = useState(0); const [config, setConfig] = useState(props.config); const [settingsIndex, setSettingsIndex] = useState(null); const [serverConfig, setServerConfig] = useState(null); @@ -459,6 +467,7 @@ export function App(props: { readonly session: T3Session; readonly config: T3tui say, progress: (text) => setFlash({ text }), openThread, + editFile: (path, cwd) => editFile(path, cwd), focus: (focused) => setMode(focused ? "details" : "nav"), }); @@ -513,17 +522,58 @@ export function App(props: { readonly session: T3Session; readonly config: T3tui }, }); - const workspace = useWorkspace({ session, renderer, live, thread: threadShell, project, serverConfig: serverConfig?.config ?? null, draft: draft !== null, diff, openPicker, confirm, run, say }); + /** Puts a program in the pane and gives it the keys. An editor there may hold unsaved work, so it isn't replaced. */ + const showPane = (program: PaneProgram) => { + if (pane?.kind === "local" && pane.id !== program.id) return say("The editor in the pane is still open; ctrl+\\ there and quit it first.", true); + setPane(program); + setMode("pane"); + }; + const closePane = (message?: string) => { + setPane(null); + setMode((current) => (current === "pane" ? "nav" : current)); + if (message) say(message); + }; + const workspace = useWorkspace({ + session, + renderer, + live, + thread: threadShell, + project, + serverConfig: serverConfig?.config ?? null, + draft: draft !== null, + diff, + openPicker, + openPane: (target) => showPane({ kind: "server", id: `server:${target.threadId}:${target.terminalId}`, title: `Terminal · ${target.label}`, target }), + confirm, + run, + say, + }); - /** The composer's text in $EDITOR. */ - const openEditor = async () => { + /** The composer's text in $EDITOR, in the pane; what's saved comes back when it exits. */ + const openEditor = () => { const composer = composerRef.current; if (!composer) return; - const edited = await editInEditor(renderer, composer.plainText); - if (edited === null) return say("The editor exited with an error; the message is unchanged.", true); - composer.setText(edited); - composer.gotoBufferEnd(); - setMode("input"); + const key = composerKey; + const file = scratchFile(composer.plainText); + showPane({ + kind: "local", + id: `message:${file.path}`, + title: "Message · save and quit to bring it back", + command: editorCommand(file.path), + onExit: (code) => { + const edited = code === 0 ? file.read() : null; + file.remove(); + if (edited === null) return say("The editor exited with an error; the message is unchanged.", true); + putMessage(key, edited); + setMode((current) => (current === "pane" ? "input" : current)); + }, + }); + }; + /** A workspace file in $EDITOR, in the pane, when the workspace is on this machine. */ + const editFile = (path: string, cwd: string | null) => { + const file = cwd ? resolvePathLinkTarget(path, cwd) : path; + if (!existsSync(file)) return say(`${path} isn't on this machine.`, true); + showPane({ kind: "local", id: `edit:${file}`, title: `Editing ${path}`, command: editorCommand(file), cwd: cwd ?? undefined, onExit: () => {} }); }; const settingsRows = settingRows(config); @@ -545,6 +595,16 @@ export function App(props: { readonly session: T3Session; readonly config: T3tui setMode("nav"); }; + /** Replaces a composer's text, the open one's or a stored draft's. */ + const putMessage = (key: string, text: string) => { + const composer = composerRef.current; + if (key !== activeComposerKey.current || !composer) return void drafts.current.set(key, text); + composer.setText(text); + composer.gotoBufferEnd(); + setComposerLines(composerLineCount(composer)); + setComposerEmpty(text.trim() === ""); + }; + /** * A message that didn't go comes back to its composer, as the web's does: its text, its * attachments and its mentions, unless something new was typed there meanwhile. @@ -724,12 +784,14 @@ export function App(props: { readonly session: T3Session; readonly config: T3tui const quitKey: Binding = { keys: ["ctrl+c"], run: quit }; const paletteKey: Binding = { keys: ["ctrl+k"], run: openPalette }; const terminalKey: Binding = { keys: ["ctrl+j"], run: workspace.openTerminal }; + const paneKey: Binding | null = pane && { keys: ["ctrl+\\"], run: () => setMode(mode === "pane" ? "nav" : "pane"), hint: mode === "pane" ? "leave pane" : "pane", command: { id: "pane-focus", title: mode === "pane" ? "Leave the terminal pane" : "Focus the terminal pane", keywords: ["terminal", "editor"] } }; const editorKey: Binding | null = hasComposer ? { keys: ["ctrl+g"], run: openEditor, command: { id: "editor", title: "Write the message in $EDITOR", keywords: ["vim", "editor", "compose"] } } : null; const nextThreadKey: Binding = { keys: ["ctrl+n", "alt+↓"], label: "ctrl+n/p", run: () => move(1) }; const previousThreadKey: Binding = { keys: ["ctrl+p", "alt+↑"], run: () => move(-1) }; // Anywhere with a text field, ctrl+u and ctrl+d edit (delete to line start, delete forward); page up/down still scroll. const lineInput = mode === "filter" || mode === "input" || mode === "find" || mode === "palette" || mode === "picker" || mode === "confirm"; const globalKeys = keys( + paneKey, quitKey, paletteKey, terminalKey, @@ -795,15 +857,19 @@ export function App(props: { readonly session: T3Session; readonly config: T3tui cancelDraftKey, ); // A diff and a draft name their own few keys; everything else names the lot. - const navHints = diff - ? [scrollDiffDown, closeDiff, detailsKey] - : draft - ? keys(hinted(typeKey, "write the first message"), projectKey, modelKey && hinted(modelKey, "model & mode"), worktreeKey, cancelDraftKey) - : navKeys; + const navHints = keys( + paneKey, + diff + ? [scrollDiffDown, closeDiff, detailsKey] + : draft + ? keys(hinted(typeKey, "write the first message"), projectKey, modelKey && hinted(modelKey, "model & mode"), worktreeKey, cancelDraftKey) + : navKeys, + ); // Commands only the palette runs. const paletteOnly = keys( editorKey, + pane?.kind === "server" && { keys: [], run: () => closePane(), command: { id: "pane-close", title: "Close the terminal pane", keywords: ["detach", "hide"] } }, queue.keys.input, queue.keys.palette, (projection?.subagents ?? []).filter((agent) => threadId && canStopSubagent(agent)).map((agent): Binding => ({ keys: [], run: () => run("Stop requested", () => session.stopSubagent(agent)), command: { id: `stop-subagent:${agent.id}`, title: `Stop subagent: ${agent.title ?? "subagent"}`, keywords: ["interrupt", "cancel", "kill"] } })), @@ -814,7 +880,7 @@ export function App(props: { readonly session: T3Session; readonly config: T3tui pullRequests.commands.map((command): Binding => ({ ...fromCommand(command), keys: [], ...(command.shortcut ? { label: command.shortcut } : {}) })), { keys: [], run: openSettings, command: { id: "settings", title: "Settings", keywords: ["preferences", "config", "options", "notifications", "alerts"] } }, ); - const commands = commandsFor([...navKeys, ...paletteOnly]); + const commands = commandsFor(keys(paneKey, navKeys, paletteOnly)); // The composer. Enter and newlines are the textarea's own; their hints only explain. const sendKey = config.sendShortcut === "enter" ? "enter" : "ctrl+enter"; @@ -888,6 +954,7 @@ export function App(props: { readonly session: T3Session; readonly config: T3tui return { layers: [confirmKeys], hints: confirmKeys }; } } + if (mode === "pane" && paneKey) return { layers: [[paneKey]], hints: [paneKey] }; // The secret field takes every key while it's focused. if (secret.focused) return { layers: [secret.bindings, [quitKey]], hints: secret.bindings }; switch (mode) { @@ -921,7 +988,7 @@ export function App(props: { readonly session: T3Session; readonly config: T3tui return key.preventDefault(); } } - if (secret.focused && mode !== "find" && mode !== "picker" && mode !== "palette" && mode !== "confirm") { + if (secret.focused && mode !== "find" && mode !== "picker" && mode !== "palette" && mode !== "confirm" && mode !== "pane") { secret.type(key); key.preventDefault(); } @@ -985,7 +1052,7 @@ export function App(props: { readonly session: T3Session; readonly config: T3tui }); /** A click in the conversation, a diff or the PR view takes the keys there, out of the composer or details. */ const focusConversation = (event: MouseEvent) => { - if (event.button !== 0 || (mode !== "input" && mode !== "details")) return; + if (event.button !== 0 || (mode !== "input" && mode !== "details" && mode !== "pane")) return; panel.leave(); }; @@ -999,8 +1066,11 @@ export function App(props: { readonly session: T3Session; readonly config: T3tui // Room for the details column and its edge beside the conversation; a dragged width gives way to the conversation first. const detailsRoom = width - listWidth - 1 - MIN_CONVERSATION_WIDTH; const detailsWidth = Math.min(detailsWidthPref ?? DETAILS_WIDTH, Math.max(DETAILS_WIDTH, detailsRoom)); - const detailsColumn = panel.show && detailsWidth <= detailsRoom; - const detailsInPlace = panel.show && !detailsColumn; + // The pane takes the details column's place, or the conversation's when there's no room beside it. + const paneColumn = pane !== null && detailsWidth <= detailsRoom; + const detailsColumn = !pane && panel.show && detailsWidth <= detailsRoom; + const detailsInPlace = !pane && panel.show && !detailsColumn; + const rightColumn = paneColumn || detailsColumn; // The block's height: a row of padding on top, the text, then the model line inside it. // Padding row, text, a blank row, then the model line. const composerHeight = Math.min(MAX_COMPOSER_LINES, composerLines) + 3; @@ -1044,6 +1114,7 @@ export function App(props: { readonly session: T3Session; readonly config: T3tui inPlace={inPlace} selected={mode === "details" || (mode === "confirm" && returnMode.current === "details") ? panel.selected : null} onActivate={panel.activateById} + onEdit={panel.edit} /> ); @@ -1118,8 +1189,8 @@ export function App(props: { readonly session: T3Session; readonly config: T3tui saveLayoutNow({ listWidth: null }); }} />} - - {settingsIndex !== null ? ( + + {pane && !paneColumn ? null : settingsIndex !== null ? ( {workspace.note} : null} {find.open ? setMode("nav")} /> : null} {mode !== "confirm" && secret.request ? : null} - {queue.queued.length > 0 ? : null} + {queue.queued.length > 0 ? : null} {mode !== "confirm" && questions.request && questions.draft ? ) : null} - {menu ? : null} + {menu ? : null} {attach.pending.length > 0 ? ( Attached: @@ -1275,14 +1346,24 @@ export function App(props: { readonly session: T3Session; readonly config: T3tui ) : null} - {detailsColumn ? ( - <> - setResizing("details")} onReset={() => { - setDetailsWidthPref(null); - saveLayoutNow({ detailsWidth: null }); - }} /> - {detailsPanel(false)} - + {rightColumn ? ( + setResizing("details")} onReset={() => { + setDetailsWidthPref(null); + saveLayoutNow({ detailsWidth: null }); + }} /> + ) : null} + {detailsColumn ? detailsPanel(false) : null} + {/* One place in the tree in both layouts, so a narrower window doesn't restart its program. */} + {pane ? ( + setMode("pane")} + onClose={closePane} + place={paneColumn ? { width: detailsWidth } : { left: showList ? listWidth + 1 : 0, height: contentHeight }} + /> ) : null} diff --git a/src/ui/DetailsPanel.tsx b/src/ui/DetailsPanel.tsx index befabe5..489fc3f 100644 --- a/src/ui/DetailsPanel.tsx +++ b/src/ui/DetailsPanel.tsx @@ -1,7 +1,7 @@ import type { ReactNode } from "react"; import { MAX_DETAIL_FILES, type ChangedFile, type DetailsAction, type DetailsFold, type LineageLine, type ThreadDetails } from "../model/details.ts"; -import { ClickRow } from "./Button.tsx"; +import { Button, ClickRow } from "./Button.tsx"; import { truncate } from "./text.ts"; import { colors, statusColor } from "./theme.ts"; @@ -25,6 +25,8 @@ export function DetailsPanel(props: { readonly selected: string | null; readonly unfolded: ReadonlySet; readonly onActivate: (id: string) => void; + /** Opens a changed file in $EDITOR, from its ▸ Edit. */ + readonly onEdit: (path: string) => void; /** Shown when there are no details, which defaults to no thread being selected. */ readonly empty?: string; }) { @@ -48,7 +50,7 @@ export function DetailsPanel(props: { {!details ? ( {props.empty ?? "No thread selected."} ) : ( - + )} @@ -60,7 +62,7 @@ interface LineProps { readonly onActivate: (id: string) => void; } -function Sections({ details, inner, loading, unfolded, selected, onActivate }: { readonly details: ThreadDetails; readonly inner: number; readonly loading: boolean; readonly unfolded: ReadonlySet } & LineProps) { +function Sections({ details, inner, loading, unfolded, selected, onActivate, onEdit }: { readonly details: ThreadDetails; readonly inner: number; readonly loading: boolean; readonly unfolded: ReadonlySet; readonly onEdit: (path: string) => void } & LineProps) { const { branch, changes, git, tasks } = details; const files = changes?.files ?? []; const line = { selected, onActivate }; @@ -102,9 +104,16 @@ function Sections({ details, inner, loading, unfolded, selected, onActivate }: { {changes ? (
}> {files.slice(0, MAX_DETAIL_FILES).map((file) => ( - - - + + + + + {selected === `diff:${file.path}` && !file.path.endsWith("/") ? ( + +