Skip to content

Commit 44cd984

Browse files
committed
feat(tui): refine plugin context slots
1 parent c445d98 commit 44cd984

10 files changed

Lines changed: 65 additions & 48 deletions

File tree

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,22 @@ export interface Page {
113113
readonly render: (input: { readonly data?: Record<string, any> }) => JSX.Element
114114
}
115115

116-
export type Slot = (props: Record<string, any>) => JSX.Element
116+
export interface SlotMap {
117+
readonly app: Readonly<Record<string, never>>
118+
readonly "home.footer": Readonly<Record<string, never>>
119+
readonly "sidebar.content": {
120+
readonly sessionID: string
121+
}
122+
readonly "sidebar.footer": Readonly<Record<string, never>>
123+
}
124+
125+
export type SlotName = keyof SlotMap
126+
export type Slot<Name extends SlotName = SlotName> = (props: SlotMap[Name]) => JSX.Element
127+
128+
export interface App {
129+
readonly version: string
130+
readonly channel: string
131+
}
117132

118133
export type ToastVariant = "info" | "success" | "warning" | "error"
119134

@@ -308,17 +323,21 @@ export interface Keymap {
308323
export interface UI {
309324
readonly dialog: Dialog
310325
readonly toast: Toast
326+
readonly format: {
327+
path(value: string): string
328+
}
311329
readonly router: {
312330
register(page: Page): () => void
313331
navigate(destination: Destination): void
314332
current(): Route
315333
}
316-
readonly slot: (name: string, render: Slot) => () => void
334+
readonly slot: <Name extends SlotName>(name: Name, render: Slot<Name>) => () => void
317335
}
318336

319337
export interface Context {
320338
readonly options: Readonly<Record<string, any>>
321339
readonly location: LocationRef | undefined
340+
readonly app: App
322341
readonly renderer: CliRenderer
323342
readonly client: OpenCodeClient
324343
readonly data: Data

‎packages/tui/src/app.tsx‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,10 +1127,7 @@ function App(props: { pair?: DialogPairCredentials }) {
11271127
</Match>
11281128
</Switch>
11291129
</box>
1130-
<box flexShrink={0}>
1131-
<PluginSlot name="app.bottom" />
1132-
</box>
1133-
<PluginSlot name="app" />
1130+
<PluginSlot name="app" input={{}} mode="all" />
11341131
</Show>
11351132
</box>
11361133
</box>

‎packages/tui/src/feature-plugins/home/footer.tsx‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
import { Plugin } from "@opencode-ai/plugin/tui"
22
import { createMemo, Match, Show, Switch } from "solid-js"
33
import { useTerminalDimensions } from "@opentui/solid"
4-
import { useTuiApp, useTuiPaths } from "../../context/runtime"
5-
import { abbreviateHome } from "../../runtime"
64
import { FilePath } from "../../ui/file-path"
75
import { stringWidth } from "../../util/string-width"
86

97
function Directory(props: { context: Plugin.Context; maxWidth: number }) {
10-
const paths = useTuiPaths()
118
const directory = createMemo(() =>
12-
props.context.location ? abbreviateHome(props.context.location.directory, paths.home) : undefined,
9+
props.context.location ? props.context.ui.format.path(props.context.location.directory) : undefined,
1310
)
1411

1512
return (
1613
<Show when={directory()}>
17-
{(value) => <FilePath value={value()} maxWidth={props.maxWidth} fg={props.context.theme.text.subdued} />}
14+
{(value) => <FilePath value={value()} maxWidth={props.maxWidth} fg={props.context.theme.themeV2.text.subdued} />}
1815
</Show>
1916
)
2017
}
@@ -27,16 +24,18 @@ function Mcp(props: { context: Plugin.Context }) {
2724
return (
2825
<Show when={list().length}>
2926
<box gap={1} flexDirection="row" flexShrink={0}>
30-
<text fg={props.context.theme.text.default}>
27+
<text fg={props.context.theme.themeV2.text.default}>
3128
<Switch>
3229
<Match when={failed()}>
33-
<span style={{ fg: props.context.theme.text.feedback.error.default }}>⊙ </span>
30+
<span style={{ fg: props.context.theme.themeV2.text.feedback.error.default }}>⊙ </span>
3431
</Match>
3532
<Match when={true}>
3633
<span
3734
style={{
3835
fg:
39-
count() > 0 ? props.context.theme.text.feedback.success.default : props.context.theme.text.subdued,
36+
count() > 0
37+
? props.context.theme.themeV2.text.feedback.success.default
38+
: props.context.theme.themeV2.text.subdued,
4039
}}
4140
>
4241
⊙{" "}
@@ -45,14 +44,13 @@ function Mcp(props: { context: Plugin.Context }) {
4544
</Switch>
4645
{count()} MCP
4746
</text>
48-
<text fg={props.context.theme.text.subdued}>/status</text>
47+
<text fg={props.context.theme.themeV2.text.subdued}>/status</text>
4948
</box>
5049
</Show>
5150
)
5251
}
5352

5453
function View(props: { context: Plugin.Context }) {
55-
const app = useTuiApp()
5654
const dimensions = useTerminalDimensions()
5755
const mcpWidth = createMemo(() => {
5856
const list = props.context.data.location.mcp.server.list(props.context.location) ?? []
@@ -74,12 +72,12 @@ function View(props: { context: Plugin.Context }) {
7472
>
7573
<Directory
7674
context={props.context}
77-
maxWidth={Math.max(2, dimensions().width - 8 - stringWidth(app.version) - mcpWidth())}
75+
maxWidth={Math.max(2, dimensions().width - 8 - stringWidth(props.context.app.version) - mcpWidth())}
7876
/>
7977
<Mcp context={props.context} />
8078
<box flexGrow={1} />
8179
<box flexShrink={0}>
82-
<text fg={props.context.theme.text.subdued}>{app.version}</text>
80+
<text fg={props.context.theme.themeV2.text.subdued}>{props.context.app.version}</text>
8381
</box>
8482
</box>
8583
)

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
import { Plugin } from "@opencode-ai/plugin/tui"
22
import { createMemo, Show } from "solid-js"
3-
import { useTuiPaths } from "../../context/runtime"
4-
import { useTheme } from "../../context/theme"
5-
import { abbreviateHome } from "../../runtime"
63
import { FilePath } from "../../ui/file-path"
74

85
function View(props: { context: Plugin.Context }) {
9-
const { themeV2 } = useTheme()
10-
const paths = useTuiPaths()
116
const directory = createMemo(() =>
12-
props.context.location ? abbreviateHome(props.context.location.directory, paths.home) : undefined,
7+
props.context.location ? props.context.ui.format.path(props.context.location.directory) : undefined,
138
)
149
return (
15-
<Show when={directory()}>{(value) => <FilePath value={value()} maxWidth={38} fg={themeV2.text.subdued} />}</Show>
10+
<Show when={directory()}>
11+
{(value) => <FilePath value={value()} maxWidth={38} fg={props.context.theme.themeV2.text.subdued} />}
12+
</Show>
1613
)
1714
}
1815

‎packages/tui/src/feature-plugins/system/diff-viewer.tsx‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import path from "path"
1616
import { createEffect, createMemo, createResource, createSignal, For, Match, onCleanup, Show, Switch } from "solid-js"
1717
import { DiffViewerFileTree } from "./diff-viewer-file-tree"
1818
import { Panel, PanelGroup, Separator } from "./diff-viewer-ui"
19-
import { useDialog } from "../../ui/dialog"
2019
import { DialogSelect } from "../../ui/dialog-select"
2120
import { getScrollAcceleration } from "../../util/scroll"
2221
import { useConfig } from "../../config"
@@ -1051,7 +1050,6 @@ function DiffViewerHelpDialog(props: { context: Plugin.Context }) {
10511050
}
10521051

10531052
function Commands(props: { context: Plugin.Context }) {
1054-
const dialog = useDialog()
10551053
props.context.keymap.layer(() => ({
10561054
mode: "global",
10571055
commands: [
@@ -1083,7 +1081,7 @@ function Commands(props: { context: Plugin.Context }) {
10831081
returnRoute,
10841082
},
10851083
})
1086-
dialog.clear()
1084+
props.context.ui.dialog.clear()
10871085
},
10881086
},
10891087
],

‎packages/tui/src/feature-plugins/system/scrap.tsx‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import { Plugin } from "@opencode-ai/plugin/tui"
22
import { useTerminalDimensions } from "@opentui/solid"
3-
import { Keymap } from "../../context/keymap"
43
import { useTheme } from "../../context/theme"
5-
import { useDialog } from "../../ui/dialog"
64

75
function Commands(props: { context: Plugin.Context }) {
8-
const dialog = useDialog()
9-
Keymap.createLayer(() => ({
6+
props.context.keymap.layer(() => ({
107
mode: "global",
118
commands: [
129
{
@@ -16,7 +13,7 @@ function Commands(props: { context: Plugin.Context }) {
1613
palette: true,
1714
run() {
1815
props.context.ui.router.navigate({ type: "plugin", name: "scrap" })
19-
dialog.clear()
16+
props.context.ui.dialog.clear()
2017
},
2118
},
2219
],
@@ -29,7 +26,7 @@ function Scrap(props: { context: Plugin.Context }) {
2926
const { themeV2 } = useTheme()
3027
const { themeV2: elevatedTheme } = useTheme().contextual("elevated")
3128

32-
Keymap.createLayer(() => ({
29+
props.context.keymap.layer(() => ({
3330
commands: [
3431
{
3532
bind: "escape",

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ import {
1313
import path from "path"
1414
import { stat } from "fs/promises"
1515
import { fileURLToPath, pathToFileURL } from "url"
16-
import type { Context, Dialog, Page, Slot, Toast } from "@opencode-ai/plugin/tui/context"
16+
import type { Context, Dialog, Page, Slot, SlotMap, SlotName, Toast } from "@opencode-ai/plugin/tui/context"
1717
import { createStore, produce, reconcile as reconcileStore } from "solid-js/store"
1818
import { useRenderer } from "@opentui/solid"
1919
import { useConfig } from "../config"
2020
import { useClient } from "../context/client"
2121
import { useData } from "../context/data"
2222
import { Keymap } from "../context/keymap"
2323
import { useRoute } from "../context/route"
24-
import { useTuiLifecycle } from "../context/runtime"
24+
import { useTuiApp, useTuiLifecycle, useTuiPaths } from "../context/runtime"
2525
import { useLocation } from "../context/location"
2626
import { useTheme } from "../context/theme"
2727
import { DialogAlert } from "../ui/dialog-alert"
@@ -31,6 +31,7 @@ import { DialogSelect } from "../ui/dialog-select"
3131
import { useDialog } from "../ui/dialog"
3232
import { useToast } from "../ui/toast"
3333
import { useAttention } from "../context/attention"
34+
import { abbreviateHome } from "../util/path-format"
3435
import { builtins } from "./builtins"
3536

3637
export interface PackageResolver {
@@ -47,7 +48,7 @@ type Value = {
4748
readonly ready: () => boolean
4849
readonly list: () => ReadonlyArray<State>
4950
readonly route: (id: string, name: string) => Page["render"] | undefined
50-
readonly slot: (name: string) => ReadonlyArray<Slot>
51+
readonly slot: <Name extends SlotName>(name: Name) => ReadonlyArray<Slot<Name>>
5152
readonly activate: (id: string) => Promise<boolean>
5253
readonly deactivate: (id: string) => Promise<boolean>
5354
}
@@ -75,6 +76,8 @@ export function PluginProvider(props: ParentProps<{ packages: PackageResolver }>
7576
const shortcuts = Keymap.useShortcuts()
7677
const keymapState = Keymap.useState()
7778
const lifecycle = useTuiLifecycle()
79+
const app = useTuiApp()
80+
const paths = useTuiPaths()
7881
const location = useLocation()
7982
const theme = useTheme()
8083
const dialog = useDialog()
@@ -120,7 +123,6 @@ export function PluginProvider(props: ParentProps<{ packages: PackageResolver }>
120123
dialog.setCentered(options.centered ?? false)
121124
},
122125
clear() {
123-
if (!activeDialog) return
124126
dialog.clear()
125127
},
126128
alert(options) {
@@ -218,11 +220,12 @@ export function PluginProvider(props: ParentProps<{ packages: PackageResolver }>
218220
get location() {
219221
return location.current
220222
},
223+
app: { version: app.version, channel: app.channel },
221224
renderer,
222225
client: client.api,
223226
data,
224227
attention,
225-
theme: theme.themeV2,
228+
theme,
226229
keymap: {
227230
layer: Keymap.createLayer,
228231
dispatch: keymap.dispatch,
@@ -235,6 +238,9 @@ export function PluginProvider(props: ParentProps<{ packages: PackageResolver }>
235238
ui: {
236239
dialog: dialogApi,
237240
toast: toastApi,
241+
format: {
242+
path: (value) => abbreviateHome(value, paths.home),
243+
},
238244
router: {
239245
register(page) {
240246
if (store.registrations[item.plugin.id]?.routes[page.name])
@@ -530,7 +536,16 @@ export function PluginRoute(props: { readonly fallback: (id: string, name: strin
530536
return <>{content()}</>
531537
}
532538

533-
export function PluginSlot(props: { readonly name: string; readonly input?: Record<string, any> }) {
539+
export function PluginSlot<Name extends SlotName>(props: {
540+
readonly name: Name
541+
readonly input: SlotMap[Name]
542+
readonly mode: "all" | "replace"
543+
}) {
534544
const plugins = usePlugin()
535-
return <For each={plugins.slot(props.name)}>{(render) => render(props.input ?? {})}</For>
545+
const renderers = createMemo(() => {
546+
const items = plugins.slot(props.name)
547+
if (props.mode === "replace") return items.slice(-1)
548+
return items
549+
})
550+
return <For each={renderers()}>{(render) => render(props.input)}</For>
536551
}

‎packages/tui/src/routes/home.tsx‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,10 @@ export function Home() {
8585
/>
8686
</pluginRuntime.Slot>
8787
</box>
88-
<PluginSlot name="home.bottom" />
8988
<box flexGrow={1} minHeight={0} />
9089
</box>
9190
<box width="100%" flexShrink={0}>
92-
<PluginSlot name="home.footer" />
91+
<PluginSlot name="home.footer" input={{}} mode="replace" />
9392
</box>
9493
<Show when={forms()[0]?.id} keyed>
9594
{(_) => {

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ import { nextThinkingMode, reasoningSummary, type ThinkingMode } from "../../con
7777
import { getScrollAcceleration } from "../../util/scroll"
7878
import { collapseToolOutput } from "../../util/collapse-tool-output"
7979
import { usePluginRuntime } from "../../plugin/runtime"
80-
import { PluginSlot } from "../../plugin/context"
8180
import { Keymap, type KeymapCommand } from "../../context/keymap"
8281
import { usePathFormatter } from "../../context/path-format"
8382
import { useLocation } from "../../context/location"
@@ -915,7 +914,6 @@ export function Session() {
915914
>
916915
<box flexDirection="row" flexGrow={1} minHeight={0}>
917916
<box flexGrow={1} minHeight={0} paddingBottom={1} paddingLeft={2} paddingRight={2} gap={1}>
918-
<PluginSlot name="session.header" input={{ sessionID: route.sessionID }} />
919917
<Show when={session()}>
920918
<scrollbox
921919
ref={(r) => (scroll = r)}
@@ -960,7 +958,6 @@ export function Session() {
960958
</Show>
961959
</scrollbox>
962960
<box flexShrink={0}>
963-
<PluginSlot name="session.composer.top" input={{ sessionID: route.sessionID }} />
964961
<Composer
965962
sessionID={route.sessionID}
966963
open={composer.open || (!!session()?.parentID && forms().length === 0)}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ export function Sidebar(props: { sessionID: string; overlay?: boolean }) {
5353
</Show>
5454
</box>
5555
</pluginRuntime.Slot>
56-
<PluginSlot name="sidebar.content" input={{ sessionID: props.sessionID }} />
56+
<PluginSlot name="sidebar.content" input={{ sessionID: props.sessionID }} mode="all" />
5757
</box>
5858
</scrollbox>
5959

6060
<box flexShrink={0} gap={1} paddingTop={1}>
61-
<PluginSlot name="sidebar.footer" />
61+
<PluginSlot name="sidebar.footer" input={{}} mode="replace" />
6262
</box>
6363
</box>
6464
</Show>

0 commit comments

Comments
 (0)