diff --git a/packages/tui/src/routes/session/index.tsx b/packages/tui/src/routes/session/index.tsx index 8c9ceef5774..4bcd0c1820e 100644 --- a/packages/tui/src/routes/session/index.tsx +++ b/packages/tui/src/routes/session/index.tsx @@ -86,12 +86,15 @@ import { useLocation } from "../../context/location" import { Slot } from "../../plugin/render" import { usePlugin } from "../../plugin/context" import { + backgroundToolRowIndex, cacheReuseDrop, createSessionRows, messageBoundaryIDs, resolvePart, + sessionRowID, turnDuration, turnTokensPerSecond, + type BackgroundToolTarget, type CacheUsage, type PartRef, type SessionRow, @@ -140,6 +143,7 @@ const context = createContext<{ config: ReturnType["data"] mutatePending: (action: PendingAction, inboxID: string) => Promise pendingDelivery: (inboxID: string) => SessionInbox.Delivery | undefined + jumpToBackgroundTool: (target: BackgroundToolTarget, beforeMessageID: string) => void }>() function use() { @@ -654,6 +658,24 @@ export function Session(props: { alignMessage(messageID, Math.max(0, y - (message?.type === "assistant" ? 1 : 0))) }) + const jumpToBackgroundTool = (target: BackgroundToolTarget, beforeMessageID: string) => { + const jump = () => { + const index = backgroundToolRowIndex(rows, messages(), target, beforeMessageID) + if (index === -1) { + if (data.session.message.more(route.sessionID)) prependHistory(0, jump) + return + } + const id = sessionRowID(rows[index]!, boundaries()[index]) + if (!id) return + ensureAllRows(() => { + const child = scroll.getRenderable(id) + if (!child) return + alignMessage(id, Math.max(0, scroll.scrollTop + child.y - scroll.viewport.y - 1)) + }) + } + jump() + } + function toBottom() { clearMessageNavigation() ensureAllRowsPending = undefined @@ -1267,6 +1289,7 @@ export function Session(props: { config, mutatePending, pendingDelivery: (inboxID) => pendingDeliveries().get(inboxID), + jumpToBackgroundTool, }} > @@ -1429,7 +1452,7 @@ type SessionRowViewProps = { function SessionRowView(props: SessionRowViewProps) { return ( - + {(row) => ( @@ -2008,8 +2031,20 @@ function SessionSwitchMessageV2(props: { message: SessionMessageInfo }) { function SessionNoticeMessageV2(props: { message: SessionMessageInfo }) { const ctx = use() const theme = useTheme() + const renderer = useRenderer() + const [hover, setHover] = createSignal(false) const metadata = () => (props.message.type === "synthetic" ? props.message.metadata : undefined) const source = () => stringValue(metadata()?.source) + const target = createMemo(() => { + if (source() === "shell") { + const id = stringValue(metadata()?.jobID) + return id ? { source: "shell", id } : undefined + } + if (source() === "subagent") { + const id = stringValue(metadata()?.childID) + return id ? { source: "subagent", id } : undefined + } + }) const completion = () => source() === "subagent" || source() === "shell" const state = () => stringValue(metadata()?.state) const actor = () => (source() === "shell" ? "Shell" : Locale.titlecase(stringValue(metadata()?.agent) ?? "Subagent")) @@ -2027,6 +2062,7 @@ function SessionNoticeMessageV2(props: { message: SessionMessageInfo }) { const heading = () => `${state() === "completed" ? "↳" : "!"} ${actor()} ${status()}` const suffix = () => Locale.truncateWidth(` · ${description()}`, Math.max(0, ctx.width - 3 - stringWidth(heading()))) const color = () => { + if (hover()) return theme.text.action.secondary.hovered if (state() === "error") return theme.text.feedback.error.default if (state() === "cancelled") return theme.text.feedback.warning.default return theme.text.feedback.info.default @@ -2040,7 +2076,19 @@ function SessionNoticeMessageV2(props: { message: SessionMessageInfo }) { } > - + { + if (target()) setHover(true) + }} + onMouseOut={() => setHover(false)} + onMouseUp={() => { + const item = target() + if (!item || renderer.getSelection()?.getSelectedText()) return + ctx.jumpToBackgroundTool(item, props.message.id) + }} + > {heading()} {suffix()} diff --git a/packages/tui/src/routes/session/rows.ts b/packages/tui/src/routes/session/rows.ts index 2271f174dd5..f54b9c76f12 100644 --- a/packages/tui/src/routes/session/rows.ts +++ b/packages/tui/src/routes/session/rows.ts @@ -35,6 +35,8 @@ export type SessionRow = | { type: "assistant-footer"; messageID: string } | { type: "turn-usage"; messageIDs: string[]; previousCache?: CacheUsage } +export type BackgroundToolTarget = { source: "shell" | "subagent"; id: string } + export function createSessionRows(sessionID: Accessor, onSynced?: (sessionID: string) => void) { const data = useData() const client = useClient() @@ -393,6 +395,35 @@ export function messageBoundaryIDs(rows: SessionRow[], messages: SessionMessageI }) } +export function sessionRowID(row: SessionRow, boundaryID?: string) { + if (boundaryID) return boundaryID + if (row.type === "part") return `session-part:${row.ref.messageID}:${row.ref.partID}` +} + +export function backgroundToolRowIndex( + rows: SessionRow[], + messages: SessionMessageInfo[], + target: BackgroundToolTarget, + beforeMessageID: string, +) { + const byID = new Map(messages.map((message) => [message.id, message])) + const end = rows.findIndex((row) => row.type === "message" && row.messageID === beforeMessageID) + return rows.slice(0, end === -1 ? rows.length : end).findLastIndex((row) => { + if (row.type !== "part") return false + if (target.source === "shell") return row.ref.partID === target.id + const message = byID.get(row.ref.messageID) + if (message?.type !== "assistant") return false + const part = resolvePart(message, row.ref.partID) + return ( + part?.type === "tool" && + part.name.toLowerCase() === "subagent" && + part.state.status === "completed" && + part.state.metadata?.status === "running" && + part.state.metadata.sessionID === target.id + ) + }) +} + function rowBoundaryMessageID(row: SessionRow, messages: Map) { if (row.type === "message") { const message = messages.get(row.messageID) diff --git a/packages/tui/test/cli/tui/session-rows.test.ts b/packages/tui/test/cli/tui/session-rows.test.ts index 05b10323d09..a7513940d7f 100644 --- a/packages/tui/test/cli/tui/session-rows.test.ts +++ b/packages/tui/test/cli/tui/session-rows.test.ts @@ -1,9 +1,11 @@ import { expect, test } from "bun:test" -import type { SessionMessageAssistant, SessionMessageInfo } from "@opencode-ai/client" +import type { SessionMessageAssistant, SessionMessageAssistantTool, SessionMessageInfo } from "@opencode-ai/client" import { + backgroundToolRowIndex, cacheReuseDrop, messageBoundaryIDs, reduceSessionRows, + sessionRowID, turnDuration, turnTokensPerSecond, } from "../../../src/routes/session/rows" @@ -144,6 +146,78 @@ test("assigns assistant boundaries to the first rendered row instead of the firs expect(messageBoundaryIDs(rows, messages)).toEqual(["user-1", "assistant-1", undefined, undefined]) }) +test("assigns stable IDs to tool rows for direct navigation", () => { + const messages: SessionMessageInfo[] = [ + assistant("assistant-1", [ + { type: "text", text: "Starting a shell" }, + { type: "tool", id: "shell-1", name: "shell", state: pending(), time: { created: 2 } }, + ]), + assistant("assistant-2", [ + { type: "tool", id: "shell-2", name: "shell", state: pending(), time: { created: 3 } }, + ]), + ] + const rows = reduceSessionRows(messages) + const boundaries = messageBoundaryIDs(rows, messages) + + expect(rows.map((row, index) => sessionRowID(row, boundaries[index]))).toEqual([ + "assistant-1", + "session-part:assistant-1:shell-1", + "assistant-2", + ]) +}) + +test("finds background tool launch rows for completion navigation", () => { + const messages: SessionMessageInfo[] = [ + assistant("assistant-1", [ + { type: "tool", id: "shell-1", name: "shell", state: pending(), time: { created: 1 } }, + ]), + assistant("assistant-2", [ + { + type: "tool", + id: "subagent-1", + name: "subagent", + state: completed({ sessionID: "child-1", status: "running" }), + time: { created: 2 }, + }, + ]), + { + type: "synthetic", + id: "completion-1", + text: "First background run completed", + description: "First run", + time: { created: 3 }, + }, + assistant("assistant-3", [ + { + type: "tool", + id: "subagent-2", + name: "subagent", + state: completed({ sessionID: "child-1", status: "running" }), + time: { created: 4 }, + }, + { + type: "tool", + id: "subagent-foreground", + name: "subagent", + state: completed({ sessionID: "child-1", status: "completed" }), + time: { created: 5 }, + }, + ]), + { + type: "synthetic", + id: "completion-2", + text: "Second background run completed", + description: "Second run", + time: { created: 6 }, + }, + ] + const rows = reduceSessionRows(messages) + + expect(backgroundToolRowIndex(rows, messages, { source: "shell", id: "shell-1" }, "completion-2")).toBe(0) + expect(backgroundToolRowIndex(rows, messages, { source: "subagent", id: "child-1" }, "completion-1")).toBe(1) + expect(backgroundToolRowIndex(rows, messages, { source: "subagent", id: "child-1" }, "completion-2")).toBe(3) +}) + test("groups exploration parts across assistant messages until a delimiter", () => { const messages: SessionMessageInfo[] = [ { type: "user", id: "user-1", text: "Explore", time: { created: 0 } }, @@ -436,3 +510,14 @@ function assistant(id: string, content: SessionMessageAssistant["content"]): Ses function pending() { return { status: "streaming" as const, input: "" } } + +function completed( + metadata: Record, +): Extract { + return { + status: "completed", + input: {}, + content: [{ type: "text", text: "Background" }], + metadata, + } +}