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(agents): wire HTTP hook server into main + pty spawn, rename IPC to agent:*

enes63f380af40f6
diff --git a/src/main/agents/hook-server.ts b/src/main/agents/hook-server.ts
index 198d43875..1adf97e20 100644
--- a/src/main/agents/hook-server.ts
+++ b/src/main/agents/hook-server.ts
@@ -37,6 +37,7 @@ class HookServer {
private port = 0
private token = ''
private listener: ((e: NormalizedAgentEvent) => void) | null = null
+ private rawListener: ((agentId: string, nodeId: string, payload: Record<string, unknown>) => void) | null = null
private endpointPath = ''
endpointFilePath(): string {
@@ -48,6 +49,12 @@ class HookServer {
this.listener = cb
}
+ // Raw payload listener: receives the parsed (un-normalized) hook JSON. Drives the
+ // contextTail/subagentTail features, which need transcript_path (not in NormalizedAgentEvent).
+ setRawListener(cb: (agentId: string, nodeId: string, payload: Record<string, unknown>) => void): void {
+ this.rawListener = cb
+ }
+
async start(): Promise<void> {
if (this.server) return
this.token = randomUUID()
@@ -75,6 +82,9 @@ class HookServer {
} catch {
payload = {}
}
+ // Raw listener first: it drives the transcript-tailing features (which need
+ // transcript_path). Inside the try so a throwing raw listener still ends 204.
+ this.rawListener?.(agentId, nodeId, payload)
const normalized = normalizeFor(agentId, { nodeId, agentId, payload })
if (normalized && this.listener) this.listener(normalized)
}
diff --git a/src/main/claude-hooks.ts b/src/main/claude-hooks.ts
index d4dbdaf47..f5d88a744 100644
--- a/src/main/claude-hooks.ts
+++ b/src/main/claude-hooks.ts
@@ -180,7 +180,7 @@ export function initClaudeHooks(win: BrowserWindow): void {
?.filter((c) => c.type === 'text' && c.text)
.map((c) => c.text)
.join('\n')
- win.webContents.send(IPC.claudeStatus, {
+ win.webContents.send(IPC.agentStatus, {
nodeId,
event: p.hook_event_name,
sessionId: p.session_id,
diff --git a/src/main/index.ts b/src/main/index.ts
index d54079dfe..d547d171f 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -11,7 +11,10 @@ import { GitService } from './git-service'
import { generateCommitMessage, generateTerminalName } from './commit-message'
import { initUpdater } from './updater'
import { fetchAnnouncements } from './announcements'
-import { initClaudeHooks } from './claude-hooks'
+import { hookServer } from './agents/hook-server'
+import { installManagedAgentHooks } from './agents/hooks'
+import { createSubagentTail } from './subagent-tail'
+import { createContextTail } from './context-tail'
import { initBridge } from './bridge'
import { initClaudeUsage } from './claude-usage'
@@ -93,7 +96,7 @@ function createWindow(): BrowserWindow {
return win
}
-app.whenReady().then(() => {
+app.whenReady().then(async () => {
if (!gotSingleInstanceLock) return // losing second instance — quitting; don't touch tmux
settingsStore.init()
settingsStore.registerIpc()
@@ -231,7 +234,38 @@ app.whenReady().then(() => {
const win = createWindow()
mainWin = win
initUpdater(win)
- initClaudeHooks(win)
+
+ // Agent hooks: install the managed hook script into each agent's config, then start the
+ // local HTTP server that receives hook posts and forwards normalized events to the renderer.
+ // A raw listener drives the transcript-tailing features (context meter + subagent transcript),
+ // which need the raw transcript_path the NormalizedAgentEvent intentionally drops.
+ const subagentTail = createSubagentTail(win)
+ const contextTail = createContextTail(win)
+ installManagedAgentHooks()
+ hookServer.setListener((e) => {
+ if (!win.isDestroyed()) win.webContents.send(IPC.agentStatus, e)
+ })
+ hookServer.setRawListener((agentId, _nodeId, payload) => {
+ if (agentId !== 'claude') return
+ const p = payload as {
+ hook_event_name?: string
+ session_id?: string
+ transcript_path?: string
+ tool_name?: string
+ tool_use_id?: string
+ }
+ // Context-window meter: tail the session transcript (any event carrying both fields).
+ if (p.session_id && p.transcript_path) contextTail.track(p.session_id, p.transcript_path)
+ if (p.hook_event_name === 'SessionEnd' && p.session_id) contextTail.untrack(p.session_id)
+ // Subagent live transcript: track on PreToolUse / finish on PostToolUse for subagent tools.
+ const SUBAGENT_TOOLS = new Set(['Agent', 'Task'])
+ if (p.tool_use_id && p.tool_name && SUBAGENT_TOOLS.has(p.tool_name)) {
+ if (p.hook_event_name === 'PreToolUse') subagentTail.track(p.tool_use_id, p.transcript_path)
+ else if (p.hook_event_name === 'PostToolUse') subagentTail.finish(p.tool_use_id)
+ }
+ })
+ await hookServer.start()
+
initBridge(win, ptyManager)
initClaudeUsage(win)
diff --git a/src/main/pty-manager.ts b/src/main/pty-manager.ts
index 191160851..a6bd69e39 100644
--- a/src/main/pty-manager.ts
+++ b/src/main/pty-manager.ts
@@ -7,7 +7,7 @@ import { app, ipcMain, webContents } from 'electron'
import * as pty from 'node-pty'
import { IPC } from '../shared/ipc'
import { DEFAULT_SETTINGS, type PtyCreateOptions, type Settings } from '../shared/types'
-import { claudeHookDir } from './claude-hooks'
+import { hookServer } from './agents/hook-server'
// A dedicated tmux socket isolates our sessions from the user's own tmux server.
const TMUX_SOCKET = 'node-terminal'
@@ -132,12 +132,13 @@ export class PtyManager {
delete env.TMUX
delete env.TMUX_PANE
- // Claude Code hooks: each session carries its node id + the signal dir. Our managed
- // hook (installed globally in ~/.claude/settings.json, but a no-op without these vars)
- // then reports state back to us for any `claude` run in this session.
- const hookDir = claudeHookDir()
- env.NODETERM_HOOK_DIR = hookDir
- if (options.persistKey) env.NODETERM_NODE_ID = options.persistKey
+ // Agent hooks: each session carries the hook-server coordinates + its node/agent id.
+ // Our managed hook (installed globally in each agent's config, but a no-op without these
+ // vars) then posts state back to us for any agent run in this session.
+ const hookEnv = options.persistKey
+ ? hookServer.buildPtyEnv(options.persistKey, options.agentId ?? 'claude')
+ : {}
+ for (const [k, v] of Object.entries(hookEnv)) env[k] = v
const settings = this.getSettings()
let file: string
@@ -148,6 +149,9 @@ export class PtyManager {
// `-e` sets the session environment explicitly (the tmux server is shared, so relying
// on the client's inherited env would leak the first session's values into later ones).
file = this.tmuxPath
+ // The hook-server env (port/token/node id/agent id) is passed explicitly via `-e`
+ // (one `-e KEY=VALUE` per key) since the shared tmux server can't rely on inherited env.
+ const hookEnvArgs = Object.entries(hookEnv).flatMap(([k, v]) => ['-e', `${k}=${v}`])
args = [
'-L',
TMUX_SOCKET,
@@ -156,10 +160,7 @@ export class PtyManager {
'new-session',
'-A',
'-D',
- '-e',
- `NODETERM_NODE_ID=${options.persistKey}`,
- '-e',
- `NODETERM_HOOK_DIR=${hookDir}`,
+ ...hookEnvArgs,
'-c',
cwd,
'-s',
diff --git a/src/main/subagent-tail.ts b/src/main/subagent-tail.ts
index dffbad275..a9db492e5 100644
--- a/src/main/subagent-tail.ts
+++ b/src/main/subagent-tail.ts
@@ -102,7 +102,7 @@ export function createSubagentTail(win: BrowserWindow): SubagentTail {
let timer: ReturnType<typeof setInterval> | null = null
const send = (toolUseId: string, chunk: string) => {
- if (chunk && !win.isDestroyed()) win.webContents.send(IPC.claudeSubagentActivity, { toolUseId, chunk })
+ if (chunk && !win.isDestroyed()) win.webContents.send(IPC.agentSubagentActivity, { toolUseId, chunk })
}
const readOne = (toolUseId: string, e: Tracked) => {
diff --git a/src/preload/index.ts b/src/preload/index.ts
index 19e891a83..c7e99a506 100644
--- a/src/preload/index.ts
+++ b/src/preload/index.ts
@@ -156,15 +156,15 @@ const api: NodeTerminalApi = {
ipcRenderer.on(IPC.appFocusNode, handler)
return () => ipcRenderer.removeListener(IPC.appFocusNode, handler)
},
- onClaudeStatus: (listener) => {
+ onAgentStatus: (listener) => {
const handler = (_e: unknown, payload: Parameters<typeof listener>[0]) => listener(payload)
- ipcRenderer.on(IPC.claudeStatus, handler)
- return () => ipcRenderer.removeListener(IPC.claudeStatus, handler)
+ ipcRenderer.on(IPC.agentStatus, handler)
+ return () => ipcRenderer.removeListener(IPC.agentStatus, handler)
},
onSubagentActivity: (listener) => {
const handler = (_e: unknown, payload: Parameters<typeof listener>[0]) => listener(payload)
- ipcRenderer.on(IPC.claudeSubagentActivity, handler)
- return () => ipcRenderer.removeListener(IPC.claudeSubagentActivity, handler)
+ ipcRenderer.on(IPC.agentSubagentActivity, handler)
+ return () => ipcRenderer.removeListener(IPC.agentSubagentActivity, handler)
}
}
diff --git a/src/shared/ipc.ts b/src/shared/ipc.ts
index dd1bec976..097453fdc 100644
--- a/src/shared/ipc.ts
+++ b/src/shared/ipc.ts
@@ -16,8 +16,8 @@ export const IPC = {
appNotify: 'app:notify',
appFocusNode: 'app:focus-node',
appSetBadge: 'app:set-badge',
- claudeStatus: 'claude:status',
- claudeSubagentActivity: 'claude:subagent-activity',
+ agentStatus: 'agent:status',
+ agentSubagentActivity: 'agent:subagent-activity',
bridgeConfigPath: 'bridge:config-path',
bridgeSetTopology: 'bridge:set-topology',
bridgeMessage: 'bridge:message',
diff --git a/src/shared/types.ts b/src/shared/types.ts
index b5a947214..e1c80b4d0 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -1,5 +1,7 @@
// Types shared across the main, preload, and renderer processes.
+import type { NormalizedAgentEvent } from './agents/normalize'
+
export interface PtyCreateOptions {
shell?: string
cwd?: string
@@ -10,6 +12,12 @@ export interface PtyCreateOptions {
* terminal reattaches to the same session across remounts and app restarts.
*/
persistKey?: string
+ /**
+ * Which agent runs in this session (claude/codex/gemini/custom). Drives the hook env
+ * injected at spawn. Defaults to 'claude' for backward compat; the renderer passes a
+ * real value in a later phase.
+ */
+ agentId?: string
}
// 'subagent' and 'loop' are render-only (ephemeral hook-driven viz) and never persisted.
@@ -481,8 +489,8 @@ export interface NodeTerminalApi {
notify(payload: NotifyPayload): Promise<boolean>
/** Fires when a notification is clicked, asking the renderer to focus a node. Returns unsubscribe. */
onFocusNode(listener: (nodeId: string) => void): () => void
- /** Fires on each Claude Code hook event (start/working/idle/attention). Returns unsubscribe. */
- onClaudeStatus(listener: (e: ClaudeStatusEvent) => void): () => void
+ /** Fires on each normalized agent hook event (working/done/waiting/subagent/…). Returns unsubscribe. */
+ onAgentStatus(listener: (e: NormalizedAgentEvent) => void): () => void
/** Fires with live subagent transcript chunks while a subagent runs. Returns unsubscribe. */
onSubagentActivity(listener: (e: SubagentActivity) => void): () => void
}