Skip to content
Merged
7 changes: 7 additions & 0 deletions src/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ export function setSessionId(id: string): void {
_channel = `brain/${id}`;
}

export function setSessionId(id: string): void {
if (id === _sessionId) return;
_sessionId = id;
_channel = `brain/${id}`;
}

export function initStatus(client: PluginInput["client"], directory: string): void {
_client = client;
startStatusServer(directory);
Expand All @@ -53,6 +59,7 @@ function getBus(): Promise<BusClient> {
if (!_busPromise) {
_busPromise = BusClient.connect().catch((err) => {
console.warn("[brain] BusClient connect failed:", (err as Error).message);
_busPromise = null; // allow retry on next call
throw err;
});
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
}
Expand Down
18 changes: 13 additions & 5 deletions src/tui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@ function BrainStatusBar(props: { centered?: boolean; api: TuiPluginApi; sessionI
let bus: BusTui | null = null;
let unsub: (() => void) | null = null;
let timer: ReturnType<typeof setInterval> | null = null;
let unmounted = false;
let busConnected = false;

onCleanup(() => {
unmounted = true;
unsub?.();
bus?.close();
if (timer) clearInterval(timer);
Expand All @@ -79,14 +82,19 @@ function BrainStatusBar(props: { centered?: boolean; api: TuiPluginApi; sessionI
// Real-time WebSocket subscription via plugin bus
BusTui.connect()
.then((b) => {
if (unmounted) { b.close(); return; }
bus = b;
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
const channel = `brain/${props.sessionId || "unknown"}`;
busConnected = true;
if (timer) { clearInterval(timer); timer = null; }
const channel = props.sessionId ? `brain/${props.sessionId}` : "brain/status";
unsub = b.subscribe(channel, (envelope) => {
handleStatus(envelope.payload as BrainStatusEvent);
});
})
.catch((err) => {
console.warn("[brain TUI] BusTui connect failed:", (err as Error).message);
busConnected = false;
if (!timer) timer = setInterval(poll, 200);
});

// HTTP fallback for when bus is unavailable (cross-process)
Expand Down Expand Up @@ -127,10 +135,10 @@ function BrainStatusBar(props: { centered?: boolean; api: TuiPluginApi; sessionI
<text fg={theme().textMuted}>🧠 {version()} </text>
{busy() ? <Spinner fg={fg()} /> : <text fg={connecting() ? theme().error : fg()}>{indicator()}</text>}
<text fg={connecting() ? theme().error : theme().textMuted}> {connecting() ? "connecting…" : status()}</text>
{current() > 0 && total() > 0 && (
{busy() && current() > 0 && total() > 0 && (
<text> </text>
)}
{current() > 0 && total() > 0 && (
{busy() && current() > 0 && total() > 0 && (
<ProgressBar current={current()} total={total()} showLabel={true} fillBg="#aaa" fillFg="#000" />
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
)}
</box>
Expand All @@ -156,10 +164,10 @@ function BrainStatusBar(props: { centered?: boolean; api: TuiPluginApi; sessionI
<box flexDirection="row">
{busy() ? <Spinner fg={fg()} /> : <text fg={connecting() ? theme().error : fg()}>{indicator()}</text>}
<text fg={connecting() ? theme().error : theme().textMuted}> {connecting() ? "connecting…" : status()}</text>
{current() > 0 && total() > 0 && (
{busy() && current() > 0 && total() > 0 && (
<text> </text>
)}
{current() > 0 && total() > 0 && (
{busy() && current() > 0 && total() > 0 && (
<ProgressBar current={current()} total={total()} showLabel={true} fillBg="#aaa" fillFg="#000" />
)}
</box>
Expand Down
Loading