|
1 | 1 | /** @jsxImportSource @opentui/solid */ |
2 | 2 |
|
| 3 | +import { createSignal, Show } from "solid-js"; |
3 | 4 | import type { TuiPlugin, TuiPluginApi } from "@opencode-ai/plugin/tui"; |
4 | | -import { useSlotStatusBridge } from "@four-bytes/opencode-plugin-lib/tui"; |
5 | | -import { StatusLine, StatusPanel } from "@four-bytes/opencode-plugin-lib/tui-components"; |
6 | | - |
7 | | -// Slot-safe status surface (#260, plugin-lib v0.12.2). |
8 | | -// useSlotStatusBridge must run inside a component the slot renderer mounts — |
9 | | -// never in the slot renderer function itself and never in a detached createRoot. |
10 | | -// It keeps the retained status signal in the renderer-owned root and asks the |
11 | | -// host for the next frame after every update. |
12 | | - |
13 | | -function HomeStatus(props: { api: TuiPluginApi }) { |
14 | | - const bridge = useSlotStatusBridge(props.api.renderer); |
15 | | - return <StatusPanel statuses={bridge.statuses} />; |
| 5 | +import type { RGBA } from "@opentui/core"; |
| 6 | +import { useServiceBus } from "@four-bytes/opencode-plugin-lib/tui"; |
| 7 | +import { ProgressBar, Spinner } from "@four-bytes/opencode-plugin-lib/tui-components"; |
| 8 | +import type { PluginStatus } from "@four-bytes/opencode-plugin-lib/tui"; |
| 9 | +import { VERSION } from "./shared"; |
| 10 | + |
| 11 | +/** Pure mapping from the shared plugin status payload to the row's view state. */ |
| 12 | +export interface StatusView { |
| 13 | + statusText: string; |
| 14 | + current: number; |
| 15 | + total: number; |
| 16 | + tone: "success" | "warning" | "error"; |
| 17 | + busy: boolean; |
| 18 | + hasError: boolean; |
| 19 | +} |
| 20 | + |
| 21 | +/** True for the plugin-lib clear payload (`{ plugin, cleared: true, ts }`). */ |
| 22 | +export function isStatusCleared(raw: unknown): boolean { |
| 23 | + return !!raw && typeof raw === "object" && (raw as { cleared?: unknown }).cleared === true; |
16 | 24 | } |
17 | 25 |
|
18 | | -function SidebarStatus(props: { api: TuiPluginApi }) { |
19 | | - const bridge = useSlotStatusBridge(props.api.renderer); |
20 | | - return <StatusLine statuses={bridge.statuses} />; |
| 26 | +export function statusView(data: PluginStatus): StatusView { |
| 27 | + const progress = data.progress; |
| 28 | + const current = |
| 29 | + typeof progress?.current === "number" && Number.isFinite(progress.current) ? progress.current : 0; |
| 30 | + const total = |
| 31 | + typeof progress?.total === "number" && Number.isFinite(progress.total) && progress.total > 0 |
| 32 | + ? progress.total |
| 33 | + : 0; |
| 34 | + if (data.state === "error") { |
| 35 | + return { statusText: data.summary || "error occurred", current: 0, total: 0, tone: "error", busy: false, hasError: true }; |
| 36 | + } |
| 37 | + if (data.state === "busy") { |
| 38 | + return { statusText: data.summary || "working…", current, total, tone: "warning", busy: true, hasError: false }; |
| 39 | + } |
| 40 | + if (data.state === "warn") { |
| 41 | + return { statusText: data.summary || "warning", current: 0, total: 0, tone: "warning", busy: false, hasError: false }; |
| 42 | + } |
| 43 | + return { statusText: data.summary || "ready", current: 0, total: 0, tone: "success", busy: false, hasError: false }; |
21 | 44 | } |
22 | 45 |
|
| 46 | +function BrainStatusBar(props: { variant: "sidebar" | "home"; api: TuiPluginApi }) { |
| 47 | + const [statusText, setStatusText] = createSignal("connecting…"); |
| 48 | + const [current, setCurrent] = createSignal(0); |
| 49 | + const [total, setTotal] = createSignal(0); |
| 50 | + const [fg, setFg] = createSignal<string | RGBA>(""); |
| 51 | + const [busy, setBusy] = createSignal(false); |
| 52 | + const [hasError, setHasError] = createSignal(false); |
| 53 | + const [hasStatus, setHasStatus] = createSignal(false); |
| 54 | + |
| 55 | + const theme = () => props.api.theme.current; |
| 56 | + // Push-only bus: "connecting" means no status has arrived yet. The old HTTP |
| 57 | + // poll fallback is gone, so wall-clock staleness would wrongly mask a final |
| 58 | + // ok/warn/error summary. The version is a build-time constant, so the row |
| 59 | + // shows it before the first publish and when no publish ever arrives. |
| 60 | + const connecting = () => !hasStatus(); |
| 61 | + const showProgress = () => busy() && current() > 0 && total() > 0; |
| 62 | + |
| 63 | + const handleStatus = (data: unknown) => { |
| 64 | + if (isStatusCleared(data)) { |
| 65 | + setHasStatus(false); |
| 66 | + setStatusText("connecting…"); |
| 67 | + setCurrent(0); |
| 68 | + setTotal(0); |
| 69 | + setBusy(false); |
| 70 | + setHasError(false); |
| 71 | + setFg(""); |
| 72 | + return; |
| 73 | + } |
| 74 | + try { |
| 75 | + const view = statusView(data as PluginStatus); |
| 76 | + setHasStatus(true); |
| 77 | + setHasError(view.hasError); |
| 78 | + setBusy(view.busy); |
| 79 | + setStatusText(view.statusText); |
| 80 | + setCurrent(view.current); |
| 81 | + setTotal(view.total); |
| 82 | + setFg(view.tone === "error" ? theme().error : view.tone === "warning" ? theme().warning : theme().success); |
| 83 | + } catch { |
| 84 | + setBusy(false); |
| 85 | + setStatusText("error occurred"); |
| 86 | + setFg(theme().error); |
| 87 | + setHasError(true); |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + // Brain publishes on the shared plugin-lib status channel `status/plugin/brain`. |
| 92 | + // The callback writes Solid signals owned by this mounted component, so the host |
| 93 | + // renderer repaints the row on every update — the pre-PR behaviour. |
| 94 | + useServiceBus("status", () => "plugin", "brain", (payload) => { |
| 95 | + handleStatus(payload); |
| 96 | + }); |
| 97 | + |
| 98 | + const indicatorColor = () => (connecting() ? theme().error : hasError() ? theme().error : fg()); |
| 99 | + const textColor = () => (connecting() ? theme().error : theme().textMuted); |
| 100 | + |
| 101 | + // Single ProgressBar render site shared by both variants. Keeping the bar in |
| 102 | + // one declaration makes a second (orphan) mount in either variant impossible. |
| 103 | + const StatusProgress = (p: { barWidth: number }) => ( |
| 104 | + <ProgressBar |
| 105 | + current={current()} |
| 106 | + total={total()} |
| 107 | + showLabel={false} |
| 108 | + width="auto" |
| 109 | + barWidth={p.barWidth} |
| 110 | + fillBg="#aaa" |
| 111 | + fillFg="#000" |
| 112 | + /> |
| 113 | + ); |
| 114 | + |
| 115 | + return ( |
| 116 | + <box width="100%"> |
| 117 | + <Show when={props.variant === "sidebar"}> |
| 118 | + <box width="100%" flexDirection="column"> |
| 119 | + <box flexDirection="row"> |
| 120 | + <text fg={theme().text}><b>Brain</b></text> |
| 121 | + <text fg={theme().textMuted}> 🧠 {VERSION}</text> |
| 122 | + </box> |
| 123 | + <box width="100%" flexDirection="row" justifyContent="space-between"> |
| 124 | + <box flexDirection="row" flexShrink={1}> |
| 125 | + {busy() ? <Spinner fg={fg()} /> : <text fg={indicatorColor()}>•</text>} |
| 126 | + <text fg={textColor()}> {connecting() ? "connecting…" : statusText()}</text> |
| 127 | + <Show when={showProgress()}> |
| 128 | + <text fg={theme().textMuted}> {current()}/{total()}</text> |
| 129 | + </Show> |
| 130 | + </box> |
| 131 | + <Show when={showProgress()}> |
| 132 | + <box flexShrink={0}> |
| 133 | + <StatusProgress barWidth={12} /> |
| 134 | + </box> |
| 135 | + </Show> |
| 136 | + </box> |
| 137 | + </box> |
| 138 | + </Show> |
| 139 | + <Show when={props.variant === "home"}> |
| 140 | + <box width="100%" flexDirection="row" justifyContent="center"> |
| 141 | + <text fg={theme().textMuted}>🧠 {VERSION} </text> |
| 142 | + {busy() ? <Spinner fg={fg()} /> : <text fg={indicatorColor()}>•</text>} |
| 143 | + <text fg={textColor()}> {connecting() ? "connecting…" : statusText()}</text> |
| 144 | + <Show when={showProgress()}> |
| 145 | + <text fg={theme().textMuted}> </text> |
| 146 | + <StatusProgress barWidth={10} /> |
| 147 | + </Show> |
| 148 | + </box> |
| 149 | + </Show> |
| 150 | + </box> |
| 151 | + ); |
| 152 | +} |
| 153 | + |
| 154 | +export { BrainStatusBar }; |
| 155 | + |
23 | 156 | const tui: TuiPlugin = (api) => { |
24 | 157 | api.slots.register({ |
25 | 158 | order: 60, |
26 | 159 | slots: { |
27 | | - sidebar_content: () => <SidebarStatus api={api} />, |
28 | | - home_bottom: () => <HomeStatus api={api} />, |
| 160 | + sidebar_content: () => <BrainStatusBar api={api} variant="sidebar" />, |
| 161 | + home_bottom: () => <BrainStatusBar api={api} variant="home" />, |
29 | 162 | }, |
30 | 163 | }); |
31 | 164 | return Promise.resolve(); |
32 | 165 | }; |
33 | 166 |
|
34 | | -export { HomeStatus, SidebarStatus }; |
35 | 167 | export default { |
36 | 168 | id: "four-opencode-brain", |
37 | 169 | tui, |
|
0 commit comments