Skip to content

Commit 17f6957

Browse files
committed
feat(tui): start host transcript rows at a plugin-set layout gutter
Every row the host draws (tool rows, notices, groups, prose, errors, footers) now starts at the layout's gutter, default 3, so a plugin that draws its own rows at another column can keep the host's in line.
1 parent f188cc0 commit 17f6957

7 files changed

Lines changed: 83 additions & 22 deletions

File tree

‎packages/plugin/src/tui/context.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,13 @@ export interface Layout {
566566
}) => number
567567
/** Horizontal padding of the transcript and the dock, in cells. The host default is 2. */
568568
readonly inset?: number
569+
/**
570+
* Cells from the inset to where a transcript row starts: the glyph of a tool
571+
* call, a group or a notice, and the first letter of prose. Every row the
572+
* host draws starts there, so a plugin that draws its own rows at another
573+
* column sets this to keep the host's rows in line. The host default is 3.
574+
*/
575+
readonly gutter?: number
569576
/**
570577
* The composer's chrome. "bar" is the host default: a left accent bar and a
571578
* metadata row. "rail" is a bare

‎packages/tui/src/plugin/context.tsx‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,12 @@ function isPlugin(value: unknown): value is Plugin.Definition {
805805
)
806806
}
807807

808+
/** The plugins' merged layout; none outside a PluginProvider, as for a row a test renders on its own. */
809+
export function useLayout(): () => Layout {
810+
const value = useContext(PluginContext)
811+
return value ? value.layout : () => ({})
812+
}
813+
808814
export function usePlugin() {
809815
const value = useContext(PluginContext)
810816
if (!value) throw new Error("PluginProvider is missing")

‎packages/tui/src/routes/session/group-view.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { EntryAnchor, GroupAnchor, visitEntries } from "./anchor-view"
1010
import { groupID } from "./anchors"
1111
import { instructionPaths, type PartRef, type SessionEntry, type SessionGroup, type SessionNode } from "./grouping/session"
1212
import { summarizeActivity } from "./activity-summary"
13-
import { InlineToolRow, reasoningContent, toolDisplay } from "./message-parts"
13+
import { InlineToolRow, reasoningContent, toolDisplay, useRowGutter } from "./message-parts"
1414
import { use } from "./render-context"
1515
import { resolvePart } from "./rows"
1616
import { generateThinkingSyntax } from "./thinking-syntax"
@@ -64,6 +64,7 @@ function GroupContent(props: GroupProps) {
6464
const ctx = use()
6565
const theme = useTheme()
6666
const disclosure = useDisclosure(props)
67+
const gutter = useRowGutter()
6768
const id = disclosure.id
6869
const expanded = disclosure.expanded
6970
const [hover, setHover] = createSignal(false)
@@ -188,7 +189,7 @@ function GroupContent(props: GroupProps) {
188189
<Show when={props.completed && duration()}> · {Locale.duration(duration())}</Show>
189190
</InlineToolRow>
190191
<Show when={expanded()}>
191-
<box paddingLeft={3}>{children("thought")}</box>
192+
<box paddingLeft={gutter()}>{children("thought")}</box>
192193
</Show>
193194
</Show>
194195
</Show>

‎packages/tui/src/routes/session/index.tsx‎

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ import { isRecord } from "../../util/record"
133133
import { parseTodos } from "../../util/todo"
134134
import { createHistoryPrepend } from "./history"
135135
import { context, use, type PendingAction } from "./render-context"
136-
import { INLINE_TOOL_ICON_WIDTH, InlineToolRow, ReasoningPart, TextPart, toolDisplay } from "./message-parts"
136+
import { INLINE_TOOL_ICON_WIDTH, InlineToolRow, ReasoningPart, TextPart, toolDisplay, useRowGutter } from "./message-parts"
137137
import { defaultVerbosity, type GroupKind, type SessionEntry } from "./grouping/session"
138138
import { SessionGroupView } from "./group-view"
139139
import { useEntryAnchor } from "./anchor-view"
@@ -1870,6 +1870,7 @@ function TurnTokenUsage(props: {
18701870
previousCache?: CacheUsage
18711871
message: (messageID: string) => SessionMessageInfo | undefined
18721872
}) {
1873+
const gutter = useRowGutter()
18731874
const config = useConfig()
18741875
const theme = useTheme()
18751876
const renderer = useRenderer()
@@ -1925,7 +1926,7 @@ function TurnTokenUsage(props: {
19251926
})
19261927
return (
19271928
<Show when={Boolean(config.data.debug?.turn_tokens) && steps().length > 0}>
1928-
<box paddingLeft={3} flexDirection="column">
1929+
<box paddingLeft={gutter()} flexDirection="column">
19291930
<box
19301931
flexDirection="row"
19311932
onMouseOver={() => setHover(true)}
@@ -2026,6 +2027,7 @@ function turnTokenToolSummary(tool: SessionMessageAssistantTool) {
20262027
}
20272028

20282029
function BackgroundToolHint(props: { messages: SessionMessageInfo[] }) {
2030+
const gutter = useRowGutter()
20292031
const theme = useTheme()
20302032
const shortcut = Keymap.useShortcut("session.background")
20312033
const running = createMemo(() => {
@@ -2049,7 +2051,7 @@ function BackgroundToolHint(props: { messages: SessionMessageInfo[] }) {
20492051
return (
20502052
<Show when={visible() && shortcut()}>
20512053
{(value) => (
2052-
<box marginTop={1} paddingLeft={3} flexShrink={0}>
2054+
<box marginTop={1} paddingLeft={gutter()} flexShrink={0}>
20532055
<text fg={theme.text.muted}>
20542056
Press <span style={{ fg: theme.text.base }}>{value()}</span> to move running work to the background
20552057
</text>
@@ -2100,9 +2102,10 @@ function SessionMessageView(props: { message: SessionMessageInfo }) {
21002102
}
21012103

21022104
function ExecutionError(props: { error: NonNullable<Extract<SessionMessageInfo, { type: "idle" }>["error"]> }) {
2105+
const gutter = useRowGutter()
21032106
const theme = useTheme()
21042107
return (
2105-
<box paddingLeft={3}>
2108+
<box paddingLeft={gutter()}>
21062109
<text fg={theme.text.feedback.error.base}>Error: {errorMessage(props.error)}</text>
21072110
</box>
21082111
)
@@ -2159,6 +2162,7 @@ function SessionPartView(props: {
21592162
}
21602163

21612164
function AssistantFooter(props: { message: SessionMessageAssistant }) {
2165+
const gutter = useRowGutter()
21622166
const ctx = use()
21632167
const config = useConfig()
21642168
const data = useData()
@@ -2184,7 +2188,7 @@ function AssistantFooter(props: { message: SessionMessageAssistant }) {
21842188
<Show when={props.message.error && !interrupted() && !props.message.retry}>
21852189
<Switch
21862190
fallback={
2187-
<box paddingLeft={3}>
2191+
<box paddingLeft={gutter()}>
21882192
<text fg={theme.text.feedback.error.base}>Error: {errorMessage(props.message.error)}</text>
21892193
</box>
21902194
}
@@ -2196,7 +2200,7 @@ function AssistantFooter(props: { message: SessionMessageAssistant }) {
21962200
</Switch>
21972201
</Show>
21982202
<AssistantRetry retry={props.message.retry} />
2199-
<box paddingLeft={3} marginTop={props.message.retry || (props.message.error && !interrupted()) ? 1 : 0}>
2203+
<box paddingLeft={gutter()} marginTop={props.message.retry || (props.message.error && !interrupted()) ? 1 : 0}>
22002204
<text>
22012205
<span style={{ fg: props.message.error ? theme.text.muted : local.agent.color(props.message.agent) }}>
22022206
{Locale.titlecase(props.message.agent)}
@@ -2223,11 +2227,12 @@ function AssistantFooter(props: { message: SessionMessageAssistant }) {
22232227
}
22242228

22252229
function SessionSwitchMessageV2(props: { message: SessionMessageInfo }) {
2230+
const gutter = useRowGutter()
22262231
const ctx = use()
22272232
const theme = useTheme()
22282233
if (props.message.type === "location-switched")
22292234
return (
2230-
<box paddingLeft={3}>
2235+
<box paddingLeft={gutter()}>
22312236
<text>
22322237
<span style={{ fg: theme.text.muted }}>↳ Moved to </span>
22332238
<span style={{ fg: theme.text.feedback.info.base }}>{props.message.location.directory}</span>
@@ -2246,13 +2251,14 @@ function SessionSwitchMessageV2(props: { message: SessionMessageInfo }) {
22462251
return ""
22472252
}
22482253
return (
2249-
<box paddingLeft={3}>
2254+
<box paddingLeft={gutter()}>
22502255
<text fg={theme.text.muted}>{text()}</text>
22512256
</box>
22522257
)
22532258
}
22542259

22552260
function SessionNoticeMessageV2(props: { message: SessionMessageInfo }) {
2261+
const gutter = useRowGutter()
22562262
const ctx = use()
22572263
const theme = useTheme()
22582264
const renderer = useRenderer()
@@ -2276,7 +2282,7 @@ function SessionNoticeMessageV2(props: { message: SessionMessageInfo }) {
22762282
return state() ?? "finished"
22772283
}
22782284
const heading = () => `${state() === "completed" ? "↳" : "!"} ${actor()} ${status()}`
2279-
const suffix = () => Locale.truncateWidth(` · ${description()}`, Math.max(0, ctx.width - 3 - stringWidth(heading())))
2285+
const suffix = () => Locale.truncateWidth(` · ${description()}`, Math.max(0, ctx.width - gutter() - stringWidth(heading())))
22802286
const color = () => {
22812287
if (state() === "error") return theme.text.feedback.error.base
22822288
if (state() === "cancelled") return theme.text.feedback.warning.base
@@ -2294,7 +2300,7 @@ function SessionNoticeMessageV2(props: { message: SessionMessageInfo }) {
22942300
>
22952301
<Slot path="session.message.completion" input={{ sessionID: ctx.sessionID, messageID: props.message.id }}>
22962302
<box
2297-
marginLeft={3}
2303+
marginLeft={gutter()}
22982304
onMouseOver={() => childID() && setHover(true)}
22992305
onMouseOut={() => setHover(false)}
23002306
onMouseUp={() => {
@@ -2323,6 +2329,7 @@ function SessionSkillMessage(props: { message: Extract<SessionMessageInfo, { typ
23232329
}
23242330

23252331
function CompactionMessage(props: { message: Extract<SessionMessageInfo, { type: "compaction" }> }) {
2332+
const gutter = useRowGutter()
23262333
const ctx = use()
23272334
const theme = useTheme()
23282335
const { currentMarkdownSyntax: syntax } = useThemes()
@@ -2374,7 +2381,7 @@ function CompactionMessage(props: { message: Extract<SessionMessageInfo, { type:
23742381
<box border={["top"]} borderColor={color()} flexGrow={1} />
23752382
</box>
23762383
<Show when={content()}>
2377-
<box paddingTop={1} paddingLeft={3}>
2384+
<box paddingTop={1} paddingLeft={gutter()}>
23782385
<markdown
23792386
ref={useSessionLinks()}
23802387
syntaxStyle={syntax()}
@@ -2682,12 +2689,13 @@ function freeLimitReset(delay: number | undefined, base: number) {
26822689
}
26832690

26842691
function FreeLimitNotice(props: { message: Pick<SessionMessageAssistant, "error" | "time"> }) {
2692+
const gutter = useRowGutter()
26852693
const theme = useTheme()
26862694
const reset = createMemo(() =>
26872695
freeLimitReset(props.message.error?.retryAfterMs, props.message.time.completed ?? props.message.time.created),
26882696
)
26892697
return (
2690-
<box paddingLeft={3} flexDirection="column">
2698+
<box paddingLeft={gutter()} flexDirection="column">
26912699
<text fg={theme.text.muted}>You've used today's free messages.{reset() ? ` They reset at ${reset()}.` : ""}</text>
26922700
<text fg={theme.text.muted}>
26932701
Upgrade to OpenCode Go → <span style={{ fg: theme.text.base }}>/connect</span> · Switch model →{" "}
@@ -2698,6 +2706,7 @@ function FreeLimitNotice(props: { message: Pick<SessionMessageAssistant, "error"
26982706
}
26992707

27002708
function AssistantRetry(props: { retry: SessionMessageAssistant["retry"] }) {
2709+
const gutter = useRowGutter()
27012710
const theme = useTheme()
27022711
const [seconds, setSeconds] = createSignal(0)
27032712
createEffect(() => {
@@ -2713,7 +2722,7 @@ function AssistantRetry(props: { retry: SessionMessageAssistant["retry"] }) {
27132722
return (
27142723
<Show when={props.retry}>
27152724
{(retry) => (
2716-
<box paddingLeft={3}>
2725+
<box paddingLeft={gutter()}>
27172726
<text fg={theme.text.feedback.warning.base}>
27182727
⚠ {seconds() > 0 ? `Retrying in ${seconds()}s` : "Retry due"} · attempt {retry().attempt} ·{" "}
27192728
{retry().error.message}
@@ -3384,6 +3393,7 @@ function Glob(props: ToolProps) {
33843393
}
33853394

33863395
function Read(props: ToolProps) {
3396+
const gutter = useRowGutter()
33873397
const theme = useTheme()
33883398
const pathFormatter = usePathFormatter()
33893399
const isRunning = createMemo(() => props.part.state.status === "running")
@@ -3410,7 +3420,7 @@ function Read(props: ToolProps) {
34103420
</InlineTool>
34113421
<For each={loaded()}>
34123422
{(filepath) => (
3413-
<box paddingLeft={3}>
3423+
<box paddingLeft={gutter()}>
34143424
<text paddingLeft={3} fg={theme.text.muted}>
34153425
↳ Loaded {pathFormatter.format(filepath)}
34163426
</text>
@@ -3531,6 +3541,7 @@ export function isBackgroundSubagent(
35313541
export { executeCallSummary }
35323542

35333543
function ExecuteCallView(props: { call: Accessor<ExecuteCall> }) {
3544+
const gutter = useRowGutter()
35343545
const theme = useTheme()
35353546
const renderer = useRenderer()
35363547
const [expanded, setExpanded] = createSignal(false)
@@ -3546,7 +3557,7 @@ function ExecuteCallView(props: { call: Accessor<ExecuteCall> }) {
35463557

35473558
return (
35483559
<box
3549-
paddingLeft={3}
3560+
paddingLeft={gutter()}
35503561
onMouseOver={() => expandable() && setHover(true)}
35513562
onMouseOut={() => setHover(false)}
35523563
onMouseUp={() => {
@@ -3584,6 +3595,7 @@ function ExecuteCallView(props: { call: Accessor<ExecuteCall> }) {
35843595

35853596
// The `execute` tool streams child tool calls through metadata, not a child session like Task.
35863597
function Execute(props: ToolProps) {
3598+
const gutter = useRowGutter()
35873599
const ctx = use()
35883600
const theme = useTheme()
35893601
const dialog = useDialog()
@@ -3615,7 +3627,7 @@ function Execute(props: ToolProps) {
36153627
</InlineTool>
36163628
<Index each={calls()}>{(call) => <ExecuteCallView call={call} />}</Index>
36173629
<Show when={showOutput()}>
3618-
<box paddingLeft={3}>
3630+
<box paddingLeft={gutter()}>
36193631
<For each={outputPreview().split("\n")}>
36203632
{(line, index) => (
36213633
<text paddingLeft={3} fg={theme.text.feedback.error.base}>

‎packages/tui/src/routes/session/message-parts.tsx‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type {
99
import { Spinner } from "../../component/spinner"
1010
import { createSyntaxStyleMemo, useTheme, useThemes } from "../../context/theme"
1111
import { reasoningSummary } from "../../context/thinking"
12-
import { usePlugin } from "../../plugin/context"
12+
import { useLayout, usePlugin } from "../../plugin/context"
1313
import { SplitBorder } from "../../ui/border"
1414
import { Locale } from "../../util/locale"
1515
import { use } from "./render-context"
@@ -19,6 +19,15 @@ import { useSessionLinks } from "../../ui/session-link"
1919

2020
export const INLINE_TOOL_ICON_WIDTH = 2
2121

22+
/** The host's cells from the transcript's inset to where each row starts (its glyph or first letter). */
23+
export const ROW_GUTTER = 3
24+
25+
/** Where each transcript row starts: a plugin's layout `gutter`, else the host's. Every row-level indent reads it, so all rows share one column. */
26+
export function useRowGutter() {
27+
const layout = useLayout()
28+
return () => layout().gutter ?? ROW_GUTTER
29+
}
30+
2231
const toolDisplays = new Set([
2332
"shell",
2433
"glob",
@@ -53,6 +62,7 @@ export function ReasoningPart(props: {
5362
generateThinkingSyntax(syntax(), theme.text.muted, renderer.nativeScene),
5463
)
5564
const ctx = use()
65+
const gutter = useRowGutter()
5666
// Collapsed by default in hide mode: a single line throughout, so the
5767
// layout never shifts. Click to open the full markdown block, click to close.
5868
const [expanded, setExpanded] = createSignal(false)
@@ -75,7 +85,7 @@ export function ReasoningPart(props: {
7585

7686
return (
7787
<Show when={content()}>
78-
<box paddingLeft={3} flexDirection="column" flexShrink={0}>
88+
<box paddingLeft={gutter()} flexDirection="column" flexShrink={0}>
7989
<box
8090
border={!inMinimal() || expanded() ? ["left"] : undefined}
8191
customBorderChars={SplitBorder.customBorderChars}
@@ -180,9 +190,10 @@ export function TextPart(props: {
180190
const theme = useTheme()
181191
const { currentMarkdownSyntax: syntax } = useThemes()
182192
const plugins = usePlugin()
193+
const gutter = useRowGutter()
183194
return (
184195
<Show when={props.part.text.trim()}>
185-
<box paddingLeft={3} flexShrink={0}>
196+
<box paddingLeft={gutter()} flexShrink={0}>
186197
{/* Configure custom nodes before parsing; apply content before streaming so completion keeps the final tokens. */}
187198
<markdown
188199
ref={useSessionLinks()}
@@ -220,8 +231,9 @@ export function InlineToolRow(props: {
220231
onMouseOut?: () => void
221232
onMouseUp?: (event: MouseEvent) => void
222233
}) {
234+
const gutter = useRowGutter()
223235
return (
224-
<box paddingLeft={3} onMouseOver={props.onMouseOver} onMouseOut={props.onMouseOut} onMouseUp={props.onMouseUp}>
236+
<box paddingLeft={gutter()} onMouseOver={props.onMouseOver} onMouseOut={props.onMouseOut} onMouseUp={props.onMouseUp}>
225237
<Switch>
226238
<Match when={props.spinner}>
227239
<Show when={props.status} fallback={<Spinner color={props.color} children={props.children} />}>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Plugin } from "@opencode/plugin/tui"
2+
3+
// Starts every transcript row one cell in from a one-cell inset.
4+
export default Plugin.define({
5+
id: "fixture.row-gutter",
6+
setup(context) {
7+
context.ui.layout({ inset: 1, gutter: 1 })
8+
},
9+
})

‎packages/tui/test/plugin-session-hooks.test.tsx‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,20 @@ test("a plugin claims completion notices, and layout rules see notices as their
253253
expect(frame).not.toContain("Worker finished")
254254
})
255255

256+
test("a layout gutter starts every host row, a notice's glyph and prose alike, in one column", async () => {
257+
const goal = { type: "synthetic", id: "msg_goal", text: "Notice", description: "Goal updated", metadata: { source: "goal" }, time: { created: 6 } }
258+
await using app = await openSession("row-gutter", [goal, ...messages])
259+
await app.ready
260+
const frame = await waitForHighlightedFrame(app, (frame) => frame.includes("Goal updated") && frame.includes("is fixed"))
261+
const start = (needle: string) => {
262+
const line = frame.split("\n").find((item) => item.includes(needle)) ?? ""
263+
return line.length - line.trimStart().length
264+
}
265+
// inset 1 and gutter 1: each row starts two cells in, where the host's own gutter (3) would put it five in.
266+
expect(start("◎ Goal updated")).toBe(2)
267+
expect(start("is fixed")).toBe(2)
268+
})
269+
256270
test("a plugin follows whether the session's sidebar is visible", async () => {
257271
const fixture = await copyPlugin("sidebar-visible")
258272
try {

0 commit comments

Comments
 (0)