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(bridge): window.nodeTerminal.canvas (mutate + onMutation) on preload and ws-bridge

enes07ad1891d137
diff --git a/src/preload/index.ts b/src/preload/index.ts
index 8b35a7b92..1b0abe8b2 100644
--- a/src/preload/index.ts
+++ b/src/preload/index.ts
@@ -294,6 +294,20 @@ const api: NodeTerminalApi = {
ensure: (sessionId, cwd, accountId) =>
ipcRenderer.send(IPC.contextEnsure, sessionId, cwd, accountId)
},
+ // Canvas sync: one channel in both directions. The cast goes to the reflector (src/core/canvas-sync),
+ // which fans it to every OTHER attached client — so what arrives here is always a peer's mutation.
+ canvas: {
+ mutate: (projectId, mutation) => ipcRenderer.send(IPC.canvasMut, projectId, mutation),
+ onMutation: (listener) => {
+ const handler = (
+ _e: unknown,
+ projectId: string,
+ mutation: Parameters<typeof listener>[1]
+ ): void => listener(projectId, mutation)
+ ipcRenderer.on(IPC.canvasMut, handler)
+ return () => ipcRenderer.removeListener(IPC.canvasMut, handler)
+ }
+ },
claude: {
readTranscript: (sessionId, cwd, accountId) =>
ipcRenderer.invoke(IPC.claudeReadTranscript, sessionId, cwd, accountId)
diff --git a/src/renderer/bridge/stubs.ts b/src/renderer/bridge/stubs.ts
index 151fe4fde..7648a9348 100644
--- a/src/renderer/bridge/stubs.ts
+++ b/src/renderer/bridge/stubs.ts
@@ -46,6 +46,7 @@ export function buildStubApi(): Omit<
| 'git'
| 'files'
| 'context'
+ | 'canvas'
| 'dialog'
| 'onAgentStatus'
| 'onSubagentActivity'
@@ -223,6 +224,7 @@ export function buildStubApi(): Omit<
| 'git'
| 'files'
| 'context'
+ | 'canvas'
| 'dialog'
| 'onAgentStatus'
| 'onSubagentActivity'
diff --git a/src/renderer/bridge/ws-bridge.canvas.test.ts b/src/renderer/bridge/ws-bridge.canvas.test.ts
new file mode 100644
index 000000000..71087142e
--- /dev/null
+++ b/src/renderer/bridge/ws-bridge.canvas.test.ts
@@ -0,0 +1,44 @@
+import { describe, it, expect } from 'vitest'
+import { buildCanvasApi } from './ws-bridge'
+import { IPC } from '../../shared/ipc'
+import type { CanvasMutation } from '../../shared/types'
+
+function fakeClient() {
+ const casts: Array<{ method: string; args: unknown[] }> = []
+ const subs = new Map<string, Set<(...a: unknown[]) => void>>()
+ return {
+ casts,
+ subs,
+ /** Deliver a server `ev` frame on a channel, as RpcClient's onMessage would. */
+ emit: (channel: string, ...args: unknown[]) => subs.get(channel)?.forEach((fn) => fn(...args)),
+ cast: (method: string, ...args: unknown[]) => casts.push({ method, args }),
+ subscribe: (channel: string, fn: (...a: unknown[]) => void) => {
+ const set = subs.get(channel) ?? new Set()
+ set.add(fn)
+ subs.set(channel, set)
+ return () => set.delete(fn)
+ }
+ }
+}
+
+describe('buildCanvasApi', () => {
+ it('mutate casts canvas:mut and onMutation subscribes to it', () => {
+ const c = fakeClient()
+ const { canvas } = buildCanvasApi(c as never)
+ const seen: Array<[string, CanvasMutation]> = []
+ const off = canvas.onMutation((projectId, m) => seen.push([projectId, m]))
+
+ canvas.mutate('p1', { op: 'remove', id: 'n1' })
+ expect(c.casts).toEqual([
+ { method: IPC.canvasMut, args: ['p1', { op: 'remove', id: 'n1' }] }
+ ])
+
+ // A PEER's mutation arrives on the same channel (the reflector never echoes our own back).
+ c.emit(IPC.canvasMut, 'p1', { op: 'remove', id: 'n2' })
+ expect(seen).toEqual([['p1', { op: 'remove', id: 'n2' }]])
+
+ off()
+ c.emit(IPC.canvasMut, 'p1', { op: 'remove', id: 'n3' })
+ expect(seen).toHaveLength(1)
+ })
+})
diff --git a/src/renderer/bridge/ws-bridge.ts b/src/renderer/bridge/ws-bridge.ts
index 1175cfdd4..0f34846a9 100644
--- a/src/renderer/bridge/ws-bridge.ts
+++ b/src/renderer/bridge/ws-bridge.ts
@@ -341,6 +341,21 @@ export function buildAgentApi(
}
}
+/**
+ * Build the `canvas` namespace over an RpcClient: a cast out (`canvas:mut`) and a subscription in on
+ * the same channel. The server reflects each mutation to every OTHER client, so the `ev` frames we
+ * receive here are always a peer's — never our own echo. This is a REAL implementation, not a stub:
+ * the Server Edition (two browsers on one workspace) is the surface that needs canvas sync most.
+ */
+export function buildCanvasApi(client: RpcClient): Pick<NodeTerminalApi, 'canvas'> {
+ return {
+ canvas: {
+ mutate: (projectId, mutation) => client.cast(IPC.canvasMut, projectId, mutation),
+ onMutation: (listener) => client.subscribe(IPC.canvasMut, listener as Listener)
+ }
+ }
+}
+
/**
* Build the `presence` namespace over an RpcClient, mirroring the preload's invoke(→request) /
* send(→cast) / on(→subscribe) split member-for-member: `hello` is the only request (its response
@@ -471,6 +486,7 @@ export async function installWsBridge(): Promise<boolean> {
...buildRealApi(client),
...buildFilesApi(client),
...buildAgentApi(client),
+ ...buildCanvasApi(client),
...buildPresenceApi(client),
// Web replacement for the Electron native dialog: an in-app server-directory browser over
// fs.list (the stub's E_UNSUPPORTED reject is dropped in favor of this real picker).
diff --git a/src/shared/types.ts b/src/shared/types.ts
index 62333d501..f8117d444 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -800,6 +800,22 @@ export interface ContextApi {
ensure(sessionId: string, cwd?: string, accountId?: string): void
}
+/**
+ * Canvas sync: node mutations travel between the attached clients (an Electron renderer, a
+ * Server-Edition browser tab) so they converge on one node set — instead of each holding its own
+ * canvas until someone's whole-file `workspace.save` overwrites the other's edits.
+ */
+export interface CanvasApi {
+ /**
+ * Publish one local node mutation for `projectId` (a project IS a canvas — a mutation is only
+ * ever applied to the canvas it was made on). Fire-and-forget; the reflector fans it out to every
+ * OTHER attached client and never echoes it back to the sender.
+ */
+ mutate(projectId: string, mutation: CanvasMutation): void
+ /** Fires with each PEER's mutation (project id + mutation). Returns unsubscribe. */
+ onMutation(listener: (projectId: string, mutation: CanvasMutation) => void): () => void
+}
+
/** One searchable line extracted from a Claude session transcript. */
export interface TranscriptLine {
role: 'user' | 'assistant' | 'tool'
@@ -1125,6 +1141,7 @@ export interface NodeTerminalApi {
contextLink: ContextLinkApi
usage: UsageApi
context: ContextApi
+ canvas: CanvasApi
claude: ClaudeApi
chat: ChatApi
claudeAccounts: ClaudeAccountsApi