Skip to content

Commit 9d29e80

Browse files
committed
feat(tui): remember, hide and reorder sidebar sections
Collapsed state of the MCP, LSP, Todo and Modified files sections is stored in KV, so it applies to every session and survives entering and leaving a subagent and restarting. A new tui.json option, sidebar_sections, takes `order` (sections shown first, in that order; the rest keep their current order) and `hidden` (sections removed). Built-in names are context, mcp, lsp, todo and files; plugin sections use their plugin ID. Only a plugin's sidebar_content slot is reordered or hidden; its other slots keep their place. Closes anomalyco#51535
1 parent c8b61f5 commit 9d29e80

11 files changed

Lines changed: 366 additions & 13 deletions

File tree

‎packages/plugin/src/tui.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ type TuiConfigView = Pick<PluginConfig, "$schema" | "theme" | "plugin"> &
484484
leader_timeout: number
485485
attention: TuiAttentionConfigView
486486
plugin_enabled?: Record<string, boolean>
487+
sidebar_sections?: { order?: string[]; hidden?: string[] }
487488
keybinds: TuiBindingLookupView
488489
}
489490

‎packages/tui/src/config/index.tsx‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ export const Prompt = Schema.Struct({
6060
}),
6161
}).annotate({ description: "Prompt size settings" })
6262

63+
export const SidebarSections = Schema.Struct({
64+
order: Schema.optional(Schema.Array(Schema.String)),
65+
hidden: Schema.optional(Schema.Array(Schema.String)),
66+
}).annotate({ description: "Session sidebar section order and visibility" })
67+
6368
export const Info = Schema.Struct({
6469
$schema: Schema.optional(Schema.String),
6570
theme: Schema.optional(Schema.String),
@@ -69,6 +74,7 @@ export const Info = Schema.Struct({
6974
leader_timeout: Schema.optional(LeaderTimeout),
7075
attention: Schema.optional(Attention),
7176
prompt: Schema.optional(Prompt),
77+
sidebar_sections: Schema.optional(SidebarSections),
7278
scroll_speed: Schema.optional(ScrollSpeed).annotate({ description: "TUI scroll speed" }),
7379
scroll_acceleration: Schema.optional(ScrollAcceleration),
7480
diff_style: Schema.optional(DiffStyle),

‎packages/tui/src/feature-plugins/sidebar/files.tsx‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { TuiPlugin, TuiPluginApi } from "@opencode-ai/plugin/tui"
22
import type { BuiltinTuiPlugin } from "../builtins"
3-
import { createMemo, For, Show, createSignal } from "solid-js"
3+
import { createMemo, For, Show } from "solid-js"
44
import { Locale } from "../../util/locale"
55

66
const id = "internal:sidebar-files"
@@ -12,14 +12,18 @@ function changeCountWidth(item: { additions: number; deletions: number }) {
1212
}
1313

1414
function View(props: { api: TuiPluginApi; session_id: string }) {
15-
const [open, setOpen] = createSignal(true)
15+
const open = () => props.api.kv.get("sidebar:files:open", true)
1616
const theme = () => props.api.theme.current
1717
const list = createMemo(() => props.api.state.session.diff(props.session_id))
1818

1919
return (
2020
<Show when={list().length > 0}>
2121
<box>
22-
<box flexDirection="row" gap={1} onMouseDown={() => list().length > 2 && setOpen((x) => !x)}>
22+
<box
23+
flexDirection="row"
24+
gap={1}
25+
onMouseDown={() => list().length > 2 && props.api.kv.set("sidebar:files:open", !open())}
26+
>
2327
<Show when={list().length > 2}>
2428
<text fg={theme().text}>{open() ? "▼" : "▶"}</text>
2529
</Show>

‎packages/tui/src/feature-plugins/sidebar/lsp.tsx‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
import type { TuiPlugin, TuiPluginApi } from "@opencode-ai/plugin/tui"
22
import type { BuiltinTuiPlugin } from "../builtins"
3-
import { createMemo, For, Show, createSignal } from "solid-js"
3+
import { createMemo, For, Show } from "solid-js"
44

55
const id = "internal:sidebar-lsp"
66

77
function View(props: { api: TuiPluginApi }) {
8-
const [open, setOpen] = createSignal(true)
8+
const open = () => props.api.kv.get("sidebar:lsp:open", true)
99
const theme = () => props.api.theme.current
1010
const list = createMemo(() => props.api.state.lsp())
1111
const off = createMemo(() => !props.api.state.config.lsp)
1212

1313
return (
1414
<box>
15-
<box flexDirection="row" gap={1} onMouseDown={() => list().length > 2 && setOpen((x) => !x)}>
15+
<box
16+
flexDirection="row"
17+
gap={1}
18+
onMouseDown={() => list().length > 2 && props.api.kv.set("sidebar:lsp:open", !open())}
19+
>
1620
<Show when={list().length > 2}>
1721
<text fg={theme().text}>{open() ? "▼" : "▶"}</text>
1822
</Show>

‎packages/tui/src/feature-plugins/sidebar/mcp.tsx‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { TuiPlugin, TuiPluginApi } from "@opencode-ai/plugin/tui"
22
import type { BuiltinTuiPlugin } from "../builtins"
3-
import { createMemo, For, Match, Show, Switch, createSignal } from "solid-js"
3+
import { createMemo, For, Match, Show, Switch } from "solid-js"
44

55
const id = "internal:sidebar-mcp"
66

77
function View(props: { api: TuiPluginApi }) {
8-
const [open, setOpen] = createSignal(true)
8+
const open = () => props.api.kv.get("sidebar:mcp:open", true)
99
const theme = () => props.api.theme.current
1010
const list = createMemo(() => props.api.state.mcp())
1111
const on = createMemo(() => list().filter((item) => item.status === "connected").length)
@@ -29,7 +29,11 @@ function View(props: { api: TuiPluginApi }) {
2929
return (
3030
<Show when={list().length > 0}>
3131
<box>
32-
<box flexDirection="row" gap={1} onMouseDown={() => list().length > 2 && setOpen((x) => !x)}>
32+
<box
33+
flexDirection="row"
34+
gap={1}
35+
onMouseDown={() => list().length > 2 && props.api.kv.set("sidebar:mcp:open", !open())}
36+
>
3337
<Show when={list().length > 2}>
3438
<text fg={theme().text}>{open() ? "▼" : "▶"}</text>
3539
</Show>

‎packages/tui/src/feature-plugins/sidebar/todo.tsx‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
import type { TuiPlugin, TuiPluginApi } from "@opencode-ai/plugin/tui"
22
import type { BuiltinTuiPlugin } from "../builtins"
3-
import { createMemo, For, Show, createSignal } from "solid-js"
3+
import { createMemo, For, Show } from "solid-js"
44
import { TodoItem } from "../../component/todo-item"
55

66
const id = "internal:sidebar-todo"
77

88
function View(props: { api: TuiPluginApi; session_id: string }) {
9-
const [open, setOpen] = createSignal(true)
9+
const open = () => props.api.kv.get("sidebar:todo:open", true)
1010
const theme = () => props.api.theme.current
1111
const list = createMemo(() => props.api.state.session.todo(props.session_id))
1212
const show = createMemo(() => list().length > 0 && list().some((item) => item.status !== "completed"))
1313

1414
return (
1515
<Show when={show()}>
1616
<box>
17-
<box flexDirection="row" gap={1} onMouseDown={() => list().length > 2 && setOpen((x) => !x)}>
17+
<box
18+
flexDirection="row"
19+
gap={1}
20+
onMouseDown={() => list().length > 2 && props.api.kv.set("sidebar:todo:open", !open())}
21+
>
1822
<Show when={list().length > 2}>
1923
<text fg={theme().text}>{open() ? "▼" : "▶"}</text>
2024
</Show>

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,40 @@ export function createSlots() {
5151
return {
5252
register(plugin: HostSlotPlugin) {
5353
if (!isHostSlotPlugin(plugin)) return () => {}
54-
return registry.register(plugin)
54+
if (!plugin.slots.sidebar_content) return registry.register(plugin)
55+
const sections = api.tuiConfig.sidebar_sections
56+
if (!sections) return registry.register(plugin)
57+
const name = plugin.id.startsWith("internal:sidebar-")
58+
? plugin.id.slice("internal:sidebar-".length)
59+
: plugin.id
60+
const index = sections.order?.indexOf(name) ?? -1
61+
const hidden = sections.hidden?.includes(name)
62+
if (!hidden && index < 0) return registry.register(plugin)
63+
if (!hidden && Object.keys(plugin.slots).length === 1) {
64+
return registry.register({ ...plugin, order: Number.MIN_SAFE_INTEGER + index })
65+
}
66+
67+
const other: HostSlotPlugin<RuntimeSlotMap>["slots"] = {}
68+
Object.assign(other, plugin.slots)
69+
delete other.sidebar_content
70+
if (hidden) return registry.register({ ...plugin, slots: other })
71+
72+
const sectionSlots: HostSlotPlugin<RuntimeSlotMap>["slots"] = {}
73+
sectionSlots.sidebar_content = plugin.slots.sidebar_content
74+
const section = {
75+
id: plugin.id,
76+
order: Number.MIN_SAFE_INTEGER + index,
77+
slots: sectionSlots,
78+
}
79+
return registry.batch(() => {
80+
const disposeOther = registry.register({ ...plugin, id: `${plugin.id}:sidebar-other`, slots: other })
81+
const disposeSection = registry.register(section)
82+
return () =>
83+
registry.batch(() => {
84+
disposeSection()
85+
disposeOther()
86+
})
87+
})
5588
},
5689
dispose() {
5790
setView(() => empty)

‎packages/tui/test/config.test.tsx‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ test("decodes the optional sidebar state vocabulary", () => {
5656
expect(decodeInfo({}).sidebar).toBeUndefined()
5757
})
5858

59+
test("accepts sidebar section names while rejecting non-string entries", () => {
60+
expect(decodeInfo({ sidebar_sections: { order: ["todo", "external-plugin"], hidden: ["lsp"] } })).toEqual({
61+
sidebar_sections: { order: ["todo", "external-plugin"], hidden: ["lsp"] },
62+
})
63+
expect(() => decodeInfo({ sidebar_sections: { order: [1] } })).toThrow()
64+
})
65+
5966
test("resolves host-neutral defaults", () => {
6067
const config = resolve({}, { terminalSuspend: true })
6168

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/** @jsxImportSource @opentui/solid */
2+
import { expect, test } from "bun:test"
3+
import type { TuiSlotPlugin } from "@opencode-ai/plugin/tui"
4+
import { testRender } from "@opentui/solid"
5+
import { createSignal } from "solid-js"
6+
import Mcp from "../../src/feature-plugins/sidebar/mcp"
7+
import Lsp from "../../src/feature-plugins/sidebar/lsp"
8+
import { createTuiPluginApi } from "../fixture/tui-plugin"
9+
10+
test.each([
11+
{ plugin: Mcp, label: "MCP", row: "server-one" },
12+
{ plugin: Lsp, label: "LSP", row: "server-one" },
13+
])("$label collapse survives remounts and is shared across sessions", async ({ plugin, label, row }) => {
14+
const api = createTuiPluginApi()
15+
Object.assign(api.state, { config: { lsp: true } })
16+
api.state.mcp = () => ["server-one", "server-two", "server-three"].map((name) => ({ name, status: "connected" }))
17+
api.state.lsp = () =>
18+
["server-one", "server-two", "server-three"].map((id) => ({ id, root: "/tmp", status: "connected" }))
19+
let slot: TuiSlotPlugin["slots"]["sidebar_content"]
20+
const calls: string[] = []
21+
api.slots = {
22+
register(value: TuiSlotPlugin) {
23+
const render = value.slots.sidebar_content
24+
slot = (context, props) => {
25+
calls.push(props.session_id)
26+
return render?.(context, props)
27+
}
28+
return "sidebar-test"
29+
},
30+
}
31+
const writes: [string, unknown][] = []
32+
const [values, setValues] = createSignal<Record<string, unknown>>({})
33+
api.kv.get = <Value,>(key: string, fallback?: Value) => (values()[key] ?? fallback) as Value
34+
api.kv.set = (key, value) => {
35+
writes.push([key, value])
36+
setValues((previous) => ({ ...previous, [key]: value }))
37+
}
38+
await plugin.tui(api, undefined, {} as Parameters<typeof plugin.tui>[2])
39+
40+
const first = await testRender(() => slot?.({ theme: api.theme }, { session_id: "one" }), {
41+
width: 70,
42+
height: 20,
43+
})
44+
try {
45+
await first.renderOnce()
46+
expect(first.captureCharFrame()).toContain(row)
47+
await first.mockMouse.click(2, 0)
48+
await first.renderOnce()
49+
expect(first.captureCharFrame()).toContain(label)
50+
expect(first.captureCharFrame()).not.toContain(row)
51+
} finally {
52+
first.renderer.destroy()
53+
}
54+
55+
const second = await testRender(() => slot?.({ theme: api.theme }, { session_id: "two" }), {
56+
width: 70,
57+
height: 20,
58+
})
59+
try {
60+
await second.renderOnce()
61+
expect(calls).toEqual(["one", "two"])
62+
expect(second.captureCharFrame()).toContain(label)
63+
expect(second.captureCharFrame()).not.toContain(row)
64+
expect(writes).toEqual([[`sidebar:${label.toLowerCase()}:open`, false]])
65+
expect(api.kv.get(`sidebar:${label.toLowerCase()}:open`, true)).toBe(false)
66+
} finally {
67+
second.renderer.destroy()
68+
}
69+
})

0 commit comments

Comments
 (0)