diff --git a/packages/tui/src/component/dialog-config.tsx b/packages/tui/src/component/dialog-config.tsx index 6c9df2b505d..a78f77cfcef 100644 --- a/packages/tui/src/component/dialog-config.tsx +++ b/packages/tui/src/component/dialog-config.tsx @@ -75,6 +75,16 @@ export const settings: Setting[] = [ labels: ["off", "on"], keywords: ["scroll bar"], }, + { + title: "Scroll on send", + category: "Session", + path: ["session", "submit_scroll"], + description: "Whether sending a prompt jumps to the latest message", + default: "bottom", + values: ["bottom", "keep"], + labels: ["jump to latest", "keep position"], + keywords: ["submit", "scroll position", "jump to latest", "follow"], + }, { title: "Thinking", category: "Session", diff --git a/packages/tui/src/config/index.tsx b/packages/tui/src/config/index.tsx index 8af86e01faa..3ca1e0db087 100644 --- a/packages/tui/src/config/index.tsx +++ b/packages/tui/src/config/index.tsx @@ -155,6 +155,9 @@ export const Info = Schema.Struct({ description: "Session sidebar visibility; 'auto' shows it when space permits", }), scrollbar: Schema.optional(Schema.Boolean).annotate({ description: "Show the session transcript scrollbar" }), + submit_scroll: Schema.optional(Schema.Literals(["bottom", "keep"])).annotate({ + description: "Jump to the latest message when sending a prompt, or keep the current transcript position", + }), thinking: Schema.optional(Schema.Literals(["show", "hide"])).annotate({ description: "Show or hide model reasoning by default", }), diff --git a/packages/tui/src/routes/session/index.tsx b/packages/tui/src/routes/session/index.tsx index 0e685134a47..e470eae06cc 100644 --- a/packages/tui/src/routes/session/index.tsx +++ b/packages/tui/src/routes/session/index.tsx @@ -1648,7 +1648,7 @@ export function Session(props: { ref={bind} muted={props.promptMuted} onSubmit={() => { - toBottom() + if (config.session?.submit_scroll !== "keep") toBottom() }} onEmptySubmit={async () => { const next = queuedPrompts()[0] diff --git a/packages/tui/test/session-submit-scroll.test.tsx b/packages/tui/test/session-submit-scroll.test.tsx new file mode 100644 index 00000000000..355143f1076 --- /dev/null +++ b/packages/tui/test/session-submit-scroll.test.tsx @@ -0,0 +1,85 @@ +import { expect, test } from "bun:test" +import { type Renderable, ScrollBoxRenderable } from "@opentui/core" +import { createAppFixture } from "./fixture/app" +import { directory, json } from "./fixture/tui-client" + +test.each([ + { setting: undefined, kept: false }, + { setting: "keep" as const, kept: true }, +])("sending a prompt while scrolled up with session.submit_scroll $setting", async ({ setting, kept }) => { + const sessionID = "ses_submit_scroll" + const location = { directory, project: { id: "project", directory, canonical: directory } } + const messages = Array.from({ length: 40 }, (_, index) => ({ + id: `message-${index}`, + type: "user", + text: `History message ${String(index).padStart(4, "0")}`, + time: { created: index }, + })) + const prompted = Promise.withResolvers() + await using app = await createAppFixture({ + width: 100, + height: 30, + args: { sessionID }, + config: { animations: false, tabs: { mode: "off" }, session: { submit_scroll: setting } }, + fetch: async (url, request) => { + if (url.pathname === `/api/session/${sessionID}`) + return json({ + data: { + id: sessionID, + projectID: "project", + title: "Submit scroll fixture", + agent: "build", + model: { providerID: "demo", id: "model" }, + location: { directory }, + cost: 0, + tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, + time: { created: 0, updated: 0 }, + }, + }) + if (url.pathname === `/api/session/${sessionID}/message`) return json({ data: messages.toReversed(), cursor: {} }) + if (url.pathname === `/api/session/${sessionID}/inbox`) return json({ data: [] }) + if (url.pathname === "/api/agent") + return json({ location, data: [{ id: "build", mode: "primary", hidden: false, permissions: [] }] }) + if (url.pathname === "/api/provider") return json({ location, data: [{ id: "demo", name: "Demo" }] }) + if (url.pathname === "/api/model") + return json({ location, data: [{ id: "model", providerID: "demo", name: "Demo Model", variants: [] }] }) + if (url.pathname === `/api/session/${sessionID}/prompt`) { + const body = await request.json() + prompted.resolve(body.id) + return json({ + data: { id: body.id, sessionID, type: "user", time: { created: 100 }, payload: { text: body.text } }, + }) + } + return undefined + }, + }) + await app.ready + await app.waitForFrame((frame) => frame.includes("History message 0039") && frame.includes("Demo Model")) + const find = (root: Renderable): ScrollBoxRenderable | undefined => + root instanceof ScrollBoxRenderable && root.getRenderable("message-39") + ? root + : root.getChildren().map(find).find(Boolean) + const scroll = find(app.renderer.root) + if (!scroll) throw new Error("Session scrollbox not found") + for (let tick = 0; tick < 3; tick++) await app.mockMouse.scroll(scroll.viewport.x + 2, scroll.viewport.y + 2, "up") + await app.waitForFrame((frame) => frame.includes("Jump to latest")) + await app.waitForVisualIdle({ quietFrames: 3 }) + const before = scroll.scrollTop + expect(before).toBeLessThan(scroll.scrollHeight - scroll.viewport.height) + + await app.mockInput.typeText("Follow-up prompt") + app.mockInput.pressEnter() + const id = await prompted.promise + await app.waitFor(() => !!scroll.getRenderable(id)) + // Outlasts the delayed jump to the bottom, which starts when the prompt is sent. + await app.waitForVisualIdle({ quietFrames: 3 }) + + if (kept) { + expect(scroll.scrollTop).toBe(before) + expect(app.captureCharFrame()).toContain("Jump to latest") + return + } + expect(scroll.scrollTop).toBe(Math.max(0, scroll.scrollHeight - scroll.viewport.height)) + expect(app.captureCharFrame()).toContain("Follow-up prompt") + expect(app.captureCharFrame()).not.toContain("Jump to latest") +}) diff --git a/services/www/src/docs/content/cli/config.mdx b/services/www/src/docs/content/cli/config.mdx index c4812d778f0..a9a943724a8 100644 --- a/services/www/src/docs/content/cli/config.mdx +++ b/services/www/src/docs/content/cli/config.mdx @@ -119,6 +119,7 @@ Configure how full-screen TUI sessions are presented and where new sessions star "session": { "sidebar": "auto", "scrollbar": false, + "submit_scroll": "bottom", "thinking": "hide", "grouping": "auto", "image_preview": false, @@ -136,6 +137,7 @@ Configure how full-screen TUI sessions are presented and where new sessions star | ----------------------- | --------------------------- | ---------------------------------------------------------------------------- | | `session.sidebar` | `auto` or `hide` | Shows the sidebar when space permits or always hides it. | | `session.scrollbar` | boolean | Shows the transcript scrollbar. | +| `session.submit_scroll` | `bottom` or `keep` | Jumps to the latest message when you send a prompt, or keeps your position. | | `session.thinking` | `show` or `hide` | Shows or hides model reasoning by default. | | `session.grouping` | `auto` or `none` | Groups related transcript items or renders each item separately. | | `session.image_preview` | boolean | Shows user attachment and tool-result images in the transcript. |