feat(tui): jump to background tool starts (#45554)

This commit is contained in:
James Long
2026-08-27 10:56:51 -04:00
committed by GitHub
Unverified
parent ef9b9fb6bf
commit b0b5bf6749
3 changed files with 167 additions and 3 deletions
+50 -2
View File
@@ -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<typeof useConfig>["data"]
mutatePending: (action: PendingAction, inboxID: string) => Promise<boolean>
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,
}}
>
<box flexDirection="row" flexGrow={1} minHeight={0}>
@@ -1429,7 +1452,7 @@ type SessionRowViewProps = {
function SessionRowView(props: SessionRowViewProps) {
return (
<box id={props.boundaryID} marginTop={1} flexShrink={0}>
<box id={sessionRowID(props.row, props.boundaryID)} marginTop={1} flexShrink={0}>
<Switch>
<Match when={props.row.type === "message" ? props.row : undefined}>
{(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<BackgroundToolTarget | undefined>(() => {
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 }) {
</InlineToolRow>
}
>
<box marginLeft={3}>
<box
id={target() ? `${target()!.source}-completion:${target()!.id}` : undefined}
marginLeft={3}
onMouseOver={() => {
if (target()) setHover(true)
}}
onMouseOut={() => setHover(false)}
onMouseUp={() => {
const item = target()
if (!item || renderer.getSelection()?.getSelectedText()) return
ctx.jumpToBackgroundTool(item, props.message.id)
}}
>
<text wrapMode="none">
<span style={{ fg: color() }}>{heading()}</span>
<span style={{ fg: theme.text.subdued }}>{suffix()}</span>
+31
View File
@@ -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<string>, 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<string, SessionMessageInfo>) {
if (row.type === "message") {
const message = messages.get(row.messageID)
+86 -1
View File
@@ -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<string, string>,
): Extract<SessionMessageAssistantTool["state"], { status: "completed" }> {
return {
status: "completed",
input: {},
content: [{ type: "text", text: "Background" }],
metadata,
}
}