Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

feat(chat): ChatNode UI — streaming bubbles, permission card, queue, stop

enes93451e8f5dec
diff --git a/src/renderer/nodes/ChatNode.tsx b/src/renderer/nodes/ChatNode.tsx
new file mode 100644
index 000000000..d72c5620a
--- /dev/null
+++ b/src/renderer/nodes/ChatNode.tsx
@@ -0,0 +1,156 @@
+// SDK-driven chat node: token-streaming conversation with Claude, permission cards,
+// send-while-working queue. Continuity is resume-based via data.chatSessionId — the
+// SDK process dies with the app; history reloads from the on-disk transcript.
+import { useCallback, useEffect, useRef, useState, type KeyboardEvent } from 'react'
+import { NodeResizer, useReactFlow, type NodeProps } from '@xyflow/react'
+import { MarkdownText } from './ChatPanel'
+import { useChatSessions } from '../state/chatSessions'
+import { useAgentStatus } from '../state/agentStatus'
+import type { CanvasNode } from '../state/workspace'
+import type { ChatImageAttachment } from '@shared/types'
+
+export default function ChatNode({ id, data, selected }: NodeProps<CanvasNode>) {
+ const chat = useChatSessions((s) => s.byId[id])
+ const apply = useChatSessions((s) => s.apply)
+ const seed = useChatSessions((s) => s.seed)
+ const addLocalUser = useChatSessions((s) => s.addLocalUser)
+ const status = useAgentStatus((s) => s.byId[id])
+ const { setNodes } = useReactFlow()
+ const [input, setInput] = useState('')
+ const [images, setImages] = useState<ChatImageAttachment[]>([])
+ const msgsRef = useRef<HTMLDivElement>(null)
+
+ // Boot: subscribe to driver events, seed history from disk, start/reattach the driver.
+ useEffect(() => {
+ const off = window.nodeTerminal.chat.onEvent(id, (e) => {
+ apply(id, e)
+ if (e.kind === 'session') {
+ // Persist the session id on the node so a relaunch resumes it.
+ setNodes((ns) => ns.map((n) => (n.id === id ? { ...n, data: { ...n.data, chatSessionId: e.sessionId } } : n)))
+ }
+ })
+ const own = data.chatSessionId as string | undefined
+ const forkFrom = data.forkFrom as string | undefined
+ // Fork path (Task 10): until our own session id arrives, resume the SOURCE session with
+ // fork:true; once the driver's `session` event persists chatSessionId, forkFrom is ignored.
+ const sessionId = own ?? forkFrom
+ if (sessionId) void window.nodeTerminal.chat.readTranscript(sessionId, data.cwd as string | undefined).then((m) => seed(id, m))
+ void window.nodeTerminal.chat.ensure(id, {
+ cwd: data.cwd as string | undefined,
+ sessionId,
+ fork: !own && !!forkFrom ? true : undefined
+ })
+ return off
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [id])
+
+ // Fork bootstrap: forkFrom carries the source session id until our own id arrives.
+ // (ensure() above passes fork:true with sessionId=data.forkFrom when set — see Task 10.)
+
+ useEffect(() => {
+ const el = msgsRef.current
+ if (el) el.scrollTop = el.scrollHeight
+ }, [chat?.messages, chat?.streamText, chat?.streamThinking])
+
+ const send = useCallback(() => {
+ const text = input.trim()
+ if (!text && images.length === 0) return
+ window.nodeTerminal.chat.send(id, text, images.length ? images : undefined)
+ if (!chat?.working) addLocalUser(id, text)
+ setInput('')
+ setImages([])
+ }, [input, images, id, chat?.working, addLocalUser])
+
+ const onKeyDown = (e: KeyboardEvent<HTMLTextAreaElement>) => {
+ if (e.key === 'Enter' && !e.shiftKey) {
+ e.preventDefault()
+ send()
+ }
+ }
+
+ const perm = chat?.permission
+ const working = !!chat?.working || status?.state === 'working'
+
+ return (
+ <div className={`chat-node${selected ? ' selected' : ''}`} style={{ borderTopColor: data.color }}>
+ <NodeResizer isVisible={selected} minWidth={360} minHeight={280} />
+ <div className="chat-node__header">
+ <span className="chat-node__title">{(data.title as string) || 'Chat'}</span>
+ {working && <span className="chat-node__badge chat-node__badge--working">RUNNING</span>}
+ {perm && <span className="chat-node__badge chat-node__badge--needs">NEEDS YOU</span>}
+ {chat && chat.costUsd > 0 && <span className="chat-node__cost">${chat.costUsd.toFixed(2)}</span>}
+ </div>
+ <div className="chat-node__msgs nodrag nowheel" ref={msgsRef}>
+ {chat?.messages.map((m, i) => (
+ <div key={i} className={`term-chat__msg term-chat__msg--${m.role}`}>
+ {m.parts.map((p, j) =>
+ p.kind === 'text' ? (
+ <MarkdownText key={j} text={p.text} />
+ ) : p.kind === 'thinking' ? (
+ <details key={j} className="chat-node__thinking"><summary>Thinking</summary><MarkdownText text={p.text} /></details>
+ ) : (
+ <details key={j} className="term-chat__tool">
+ <summary><span className="term-chat__tool-name">{p.name}</span><span className="term-chat__tool-arg">{p.arg}</span></summary>
+ {p.result && <pre className="term-chat__tool-result">{p.result}</pre>}
+ </details>
+ )
+ )}
+ </div>
+ ))}
+ {/* Live turn: streaming thinking + text + tool cards */}
+ {chat?.streamThinking && (
+ <details className="chat-node__thinking"><summary>Thinking…</summary><MarkdownText text={chat.streamThinking} /></details>
+ )}
+ {chat?.toolOrder.map((tid) => {
+ const t = chat.tools[tid]
+ return (
+ <details key={tid} className="term-chat__tool" open={!t.result}>
+ <summary>
+ <span className="term-chat__tool-name">{t.name}</span>
+ <span className="term-chat__tool-arg">
+ {t.summary ? `${t.summary.filePath} +${t.summary.added} −${t.summary.removed}` : t.arg}
+ </span>
+ </summary>
+ {t.result && <pre className="term-chat__tool-result">{t.result}</pre>}
+ </details>
+ )
+ })}
+ {chat?.streamText && <div className="term-chat__msg term-chat__msg--assistant"><MarkdownText text={chat.streamText} /></div>}
+ {chat?.error && <div className="chat-node__error">{chat.error.message}</div>}
+ {perm && (
+ <div className="chat-node__permission">
+ <div className="chat-node__permission-title">Allow {perm.toolName}?</div>
+ <pre className="chat-node__permission-input">{JSON.stringify(perm.input, null, 2).slice(0, 600)}</pre>
+ <div className="chat-node__permission-actions">
+ <button onClick={() => window.nodeTerminal.chat.permissionReply(id, perm.requestId, { behavior: 'allow' })}>Allow</button>
+ <button onClick={() => window.nodeTerminal.chat.permissionReply(id, perm.requestId, { behavior: 'allow', alwaysSession: true })}>Always (session)</button>
+ <button onClick={() => window.nodeTerminal.chat.permissionReply(id, perm.requestId, { behavior: 'deny' })}>Deny</button>
+ </div>
+ </div>
+ )}
+ </div>
+ {chat && chat.queue.length > 0 && (
+ <div className="chat-node__queue">
+ {chat.queue.map((q) => (
+ <span key={q.id} className="chat-node__queue-item">
+ {q.text.slice(0, 40)}
+ <button onClick={() => window.nodeTerminal.chat.removeQueued(id, q.id)}>×</button>
+ </span>
+ ))}
+ </div>
+ )}
+ <div className="chat-node__compose nodrag">
+ <textarea
+ className="term-chat__input"
+ value={input}
+ onChange={(e) => setInput(e.target.value)}
+ onKeyDown={onKeyDown}
+ placeholder={working ? 'Claude is working — message will queue…' : 'Message Claude… (Enter to send)'}
+ rows={2}
+ />
+ {/* Stop = interrupt: the driver's turn-done then auto-flushes the next queued item (Task 5). */}
+ {working && <button className="chat-node__stop" onClick={() => window.nodeTerminal.chat.interrupt(id)}>Stop</button>}
+ </div>
+ </div>
+ )
+}
diff --git a/src/renderer/nodes/ChatPanel.tsx b/src/renderer/nodes/ChatPanel.tsx
index 6634e68be..3d6fea5bd 100644
--- a/src/renderer/nodes/ChatPanel.tsx
+++ b/src/renderer/nodes/ChatPanel.tsx
@@ -5,7 +5,7 @@ import type { ChatMessage } from '@shared/types'
// Memoized bubble: marked+DOMPurify re-ran for EVERY message on each ChatPanel render (each
// turn-finish reload, each keystroke re-render). Text is stable per message, so cache per text.
-const MarkdownText = memo(function MarkdownText({ text }: { text: string }) {
+export const MarkdownText = memo(function MarkdownText({ text }: { text: string }) {
const html = useMemo(() => renderMarkdown(text), [text])
return <div className="term-chat__text" dangerouslySetInnerHTML={{ __html: html }} />
})
diff --git a/src/renderer/styles.css b/src/renderer/styles.css
index 99efb127c..76e513321 100644
--- a/src/renderer/styles.css
+++ b/src/renderer/styles.css
@@ -4551,3 +4551,186 @@ body,
font-size: 12px;
flex: none;
}
+
+/* Chat node — SDK-driven conversation node (reuses the .term-chat vocabulary). */
+.chat-node {
+ width: 100%;
+ height: 100%;
+ display: flex;
+ flex-direction: column;
+ background: var(--panel);
+ border: 1px solid var(--border);
+ border-top-width: 3px;
+ border-top-style: solid;
+ border-radius: 10px;
+ overflow: hidden;
+ box-shadow: 0 8px 28px rgba(0, 0, 0, 0.4);
+}
+.chat-node.selected {
+ border-color: var(--accent);
+}
+.chat-node__header {
+ flex: 0 0 auto;
+ display: flex;
+ align-items: center;
+ gap: 8px;
+ padding: 6px 10px;
+ border-bottom: 1px solid rgba(255, 255, 255, 0.08);
+}
+.chat-node__title {
+ font-size: 12px;
+ font-weight: 600;
+ color: var(--label, rgba(255, 255, 255, 0.92));
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+ flex: 1 1 auto;
+}
+.chat-node__badge {
+ flex: 0 0 auto;
+ font-size: 10px;
+ font-weight: 700;
+ letter-spacing: 0.06em;
+ padding: 1px 6px;
+ border-radius: 5px;
+}
+/* RUNNING matches the working spinner + node glow (clay #d97757). */
+.chat-node__badge--working {
+ color: #d97757;
+ background: rgba(217, 119, 87, 0.15);
+ animation: nt-pulse 1.1s ease-in-out infinite;
+}
+/* NEEDS YOU is red everywhere (same as the terminal attention badge). */
+.chat-node__badge--needs {
+ color: #ff453a;
+ background: rgba(255, 69, 58, 0.15);
+ animation: nt-pulse 1.1s ease-in-out infinite;
+}
+.chat-node__cost {
+ flex: 0 0 auto;
+ font-size: 11px;
+ font-variant-numeric: tabular-nums;
+ color: var(--label-secondary, rgba(255, 255, 255, 0.55));
+}
+.chat-node__msgs {
+ flex: 1 1 auto;
+ overflow-y: auto;
+ padding: 10px;
+ display: flex;
+ flex-direction: column;
+ gap: 10px;
+ /* Allow text selection / Cmd+C (React Flow nodes are non-selectable by default). */
+ user-select: text;
+ -webkit-user-select: text;
+ cursor: auto;
+}
+.chat-node__thinking {
+ font-size: 12px;
+ opacity: 0.6;
+}
+.chat-node__thinking summary {
+ cursor: pointer;
+ list-style: none;
+}
+.chat-node__error {
+ padding: 8px 10px;
+ border-radius: 8px;
+ font-size: 12px;
+ color: #ff453a;
+ background: rgba(255, 69, 58, 0.12);
+ border: 1px solid rgba(255, 69, 58, 0.35);
+}
+.chat-node__permission {
+ padding: 10px;
+ border-radius: 8px;
+ background: rgba(255, 255, 255, 0.04);
+ border: 1px solid var(--accent, #0a84ff);
+}
+.chat-node__permission-title {
+ font-size: 12px;
+ font-weight: 600;
+ color: var(--label, rgba(255, 255, 255, 0.92));
+ margin-bottom: 6px;
+}
+.chat-node__permission-input {
+ margin: 0 0 8px;
+ max-height: 140px;
+ overflow: auto;
+ white-space: pre-wrap;
+ font-family: var(--mono, ui-monospace, monospace);
+ font-size: 11px;
+ opacity: 0.85;
+}
+.chat-node__permission-actions {
+ display: flex;
+ gap: 6px;
+}
+.chat-node__permission-actions button {
+ flex: 0 0 auto;
+ padding: 4px 10px;
+ font-size: 12px;
+ border-radius: 6px;
+ border: 1px solid rgba(255, 255, 255, 0.12);
+ background: rgba(255, 255, 255, 0.06);
+ color: var(--label, #fff);
+ cursor: pointer;
+}
+.chat-node__permission-actions button:hover {
+ background: rgba(255, 255, 255, 0.12);
+}
+.chat-node__queue {
+ flex: 0 0 auto;
+ display: flex;
+ flex-wrap: wrap;
+ gap: 6px;
+ padding: 6px 10px;
+ border-top: 1px solid rgba(255, 255, 255, 0.08);
+}
+.chat-node__queue-item {
+ display: inline-flex;
+ align-items: center;
+ gap: 4px;
+ padding: 2px 6px;
+ font-size: 11px;
+ border-radius: 5px;
+ background: rgba(255, 255, 255, 0.08);
+ color: var(--label-secondary, rgba(255, 255, 255, 0.7));
+}
+.chat-node__queue-item button {
+ border: none;
+ background: transparent;
+ color: inherit;
+ cursor: pointer;
+ font-size: 12px;
+ line-height: 1;
+ padding: 0;
+ opacity: 0.7;
+}
+.chat-node__queue-item button:hover {
+ opacity: 1;
+}
+.chat-node__compose {
+ flex: 0 0 auto;
+ display: flex;
+ align-items: flex-end;
+ gap: 6px;
+ padding: 8px;
+ border-top: 1px solid rgba(255, 255, 255, 0.08);
+}
+.chat-node__compose .term-chat__input {
+ flex: 1 1 auto;
+}
+.chat-node__stop {
+ flex: 0 0 auto;
+ padding: 6px 12px;
+ font-size: 12px;
+ font-weight: 600;
+ border-radius: 6px;
+ border: 1px solid rgba(255, 69, 58, 0.5);
+ background: rgba(255, 69, 58, 0.15);
+ color: #ff453a;
+ cursor: pointer;
+}
+.chat-node__stop:hover {
+ background: rgba(255, 69, 58, 0.25);
+}