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.presence (preload + ws bridge)

enesa9ab8871e82b
diff --git a/src/preload/index.ts b/src/preload/index.ts
index af7791bb1..0f9706d0d 100644
--- a/src/preload/index.ts
+++ b/src/preload/index.ts
@@ -10,6 +10,7 @@ import type {
UpdateProgress,
Workspace
} from '../shared/types'
+import type { PeerDiff, PeerIdentity, PeerState } from '../shared/presence'
// Fan a single ipcRenderer listener per channel out to many renderer subscribers. Without
// this, every node that subscribes (e.g. Cmd+M markdown toggle on each terminal/editor) adds
@@ -372,6 +373,26 @@ const api: NodeTerminalApi = {
listDevices: () => ipcRenderer.invoke(IPC.pairingListDevices),
revokeDevice: (id) => ipcRenderer.invoke(IPC.pairingRevokeDevice, id)
},
+ // Team presence. `hello` is the only request (its response is how this client learns its OWN
+ // ClientId, without which it would draw its own cursor as a peer's); the publishers are
+ // fire-and-forget sends, and the two event channels are subscriptions. Nothing is persisted.
+ presence: {
+ hello: (identity: PeerIdentity) => ipcRenderer.invoke(IPC.presenceHello, identity),
+ cursor: (cursor) => ipcRenderer.send(IPC.presenceCursor, cursor),
+ focus: (nodeId) => ipcRenderer.send(IPC.presenceFocus, nodeId),
+ chat: (text) => ipcRenderer.send(IPC.presenceChat, text),
+ project: (projectId) => ipcRenderer.send(IPC.presenceProject, projectId),
+ onSync: (listener) => {
+ const handler = (_e: unknown, peers: PeerState[]) => listener(peers)
+ ipcRenderer.on(IPC.presenceSync, handler)
+ return () => ipcRenderer.removeListener(IPC.presenceSync, handler)
+ },
+ onPeer: (listener) => {
+ const handler = (_e: unknown, diff: PeerDiff) => listener(diff)
+ ipcRenderer.on(IPC.presencePeer, handler)
+ return () => ipcRenderer.removeListener(IPC.presencePeer, handler)
+ }
+ },
contextLink: {
setLinks: (map) => ipcRenderer.invoke(IPC.contextLinkSetLinks, map),
info: () => ipcRenderer.invoke(IPC.contextLinkInfo)
diff --git a/src/renderer/bridge/stubs.ts b/src/renderer/bridge/stubs.ts
index c8b2086d8..151fe4fde 100644
--- a/src/renderer/bridge/stubs.ts
+++ b/src/renderer/bridge/stubs.ts
@@ -49,6 +49,7 @@ export function buildStubApi(): Omit<
| 'dialog'
| 'onAgentStatus'
| 'onSubagentActivity'
+ | 'presence'
> {
const api = {
ssh: {
@@ -225,6 +226,7 @@ export function buildStubApi(): Omit<
| 'dialog'
| 'onAgentStatus'
| 'onSubagentActivity'
+ | 'presence'
>
return api
diff --git a/src/renderer/bridge/ws-bridge.presence.test.ts b/src/renderer/bridge/ws-bridge.presence.test.ts
new file mode 100644
index 000000000..92520354b
--- /dev/null
+++ b/src/renderer/bridge/ws-bridge.presence.test.ts
@@ -0,0 +1,58 @@
+import { describe, it, expect } from 'vitest'
+import { buildPresenceApi } from './ws-bridge'
+import { IPC } from '../../shared/ipc'
+import { PRESENCE_COLORS } from '../../shared/presence'
+
+function fakeClient() {
+ const subs: Array<{ channel: string }> = []
+ const casts: Array<{ method: string; args: unknown[] }> = []
+ const requests: Array<{ method: string; args: unknown[] }> = []
+ return {
+ subs,
+ casts,
+ requests,
+ subscribe: (channel: string, _fn: (...a: unknown[]) => void) => {
+ subs.push({ channel })
+ return () => {}
+ },
+ cast: (method: string, ...args: unknown[]) => casts.push({ method, args }),
+ request: (method: string, ...args: unknown[]) => {
+ requests.push({ method, args })
+ return Promise.resolve({ clientId: 3, peers: [] })
+ }
+ }
+}
+
+describe('buildPresenceApi', () => {
+ it('hello is a request; cursor/focus/chat/project are casts; onSync/onPeer subscribe', async () => {
+ const c = fakeClient()
+ const { presence } = buildPresenceApi(c as never)
+
+ await expect(presence.hello({ name: 'Ada', color: PRESENCE_COLORS[0] })).resolves.toEqual({
+ clientId: 3,
+ peers: []
+ })
+ expect(c.requests).toEqual([
+ { method: IPC.presenceHello, args: [{ name: 'Ada', color: PRESENCE_COLORS[0] }] }
+ ])
+
+ presence.cursor({ x: 1, y: 2 })
+ presence.cursor(null)
+ presence.focus('node-a')
+ presence.chat('hey')
+ presence.project('web')
+ expect(c.casts).toEqual([
+ { method: IPC.presenceCursor, args: [{ x: 1, y: 2 }] },
+ { method: IPC.presenceCursor, args: [null] },
+ { method: IPC.presenceFocus, args: ['node-a'] },
+ { method: IPC.presenceChat, args: ['hey'] },
+ { method: IPC.presenceProject, args: ['web'] }
+ ])
+
+ const un1 = presence.onSync(() => {})
+ const un2 = presence.onPeer(() => {})
+ expect(c.subs).toEqual([{ channel: IPC.presenceSync }, { channel: IPC.presencePeer }])
+ expect(typeof un1).toBe('function')
+ expect(typeof un2).toBe('function')
+ })
+})
diff --git a/src/renderer/bridge/ws-bridge.ts b/src/renderer/bridge/ws-bridge.ts
index 152c4e4e3..4b949baca 100644
--- a/src/renderer/bridge/ws-bridge.ts
+++ b/src/renderer/bridge/ws-bridge.ts
@@ -14,6 +14,7 @@ import type {
FsApi,
GitApi,
NodeTerminalApi,
+ PresenceApi,
PtyApi,
PtyCreateOptions,
SettingsApi,
@@ -21,6 +22,7 @@ import type {
Workspace,
WorkspaceApi
} from '../../shared/types'
+import type { PeerIdentity } from '../../shared/presence'
import { buildStubApi } from './stubs'
import { mountPickerRoot, openDirectoryPicker } from './dialog-picker'
@@ -329,6 +331,27 @@ export function buildAgentApi(
}
}
+/**
+ * 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
+ * is how a client learns its OWN clientId), cursor/focus/chat/project are casts, and the two event
+ * channels are subscriptions. Declared against its `NodeTerminalApi` slice so `satisfies` keeps
+ * the compiler as the completeness gate.
+ */
+export function buildPresenceApi(client: RpcClient): Pick<NodeTerminalApi, 'presence'> {
+ const presence: PresenceApi = {
+ hello: (identity: PeerIdentity) =>
+ client.request(IPC.presenceHello, identity) as ReturnType<PresenceApi['hello']>,
+ cursor: (cursor) => client.cast(IPC.presenceCursor, cursor),
+ focus: (nodeId) => client.cast(IPC.presenceFocus, nodeId),
+ chat: (text) => client.cast(IPC.presenceChat, text),
+ project: (projectId) => client.cast(IPC.presenceProject, projectId),
+ onSync: (listener) => client.subscribe(IPC.presenceSync, listener as Listener),
+ onPeer: (listener) => client.subscribe(IPC.presencePeer, listener as Listener)
+ }
+ return { presence }
+}
+
/** WS URL for the current page: same host, `/ws`, ws→http / wss→https. */
function wsUrl(): string {
const scheme = location.protocol === 'https:' ? 'wss' : 'ws'
@@ -438,6 +461,7 @@ export async function installWsBridge(): Promise<boolean> {
...buildRealApi(client),
...buildFilesApi(client),
...buildAgentApi(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).
dialog: (() => {
diff --git a/src/shared/types.ts b/src/shared/types.ts
index c80d80ddb..79a3eb7bc 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -4,6 +4,7 @@ import type { CloneProgress } from './clone-url'
import type { NormalizedAgentEvent } from './agents/normalize'
import type { AgentId, PromptInjectionMode } from './agents/config'
import type { GroupWorktree } from './worktree'
+import type { ClientId, PeerDiff, PeerIdentity, PeerState } from './presence'
export interface PtyCreateOptions {
shell?: string
@@ -1015,6 +1016,26 @@ export interface PairingApi {
revokeDevice(id: string): Promise<void>
}
+/** Team presence (docs/team-presence.md). All of it is transient — nothing here is persisted. */
+export interface PresenceApi {
+ /** Announce {name, color}. Resolves with THIS client's own id (so it never draws its own
+ * cursor) plus the current peer table. */
+ hello(identity: PeerIdentity): Promise<{ clientId: ClientId; peers: PeerState[] }>
+ /** Publish the local cursor in FLOW coordinates (null when it leaves the canvas). */
+ cursor(cursor: { x: number; y: number } | null): void
+ /** Publish the node the local user is working in (null = none). */
+ focus(nodeId: string | null): void
+ /** Publish live cursor-chat text (null closes the bubble). */
+ chat(text: string | null): void
+ /** Publish the project (canvas) we are looking at — peers on other projects are never drawn
+ * on our canvas, and we are never drawn on theirs (null = no project open). */
+ project(projectId: string | null): void
+ /** Full peer-table snapshot (on join). Returns unsubscribe. */
+ onSync(listener: (peers: PeerState[]) => void): () => void
+ /** Single-peer diff (join / update / leave). Returns unsubscribe. */
+ onPeer(listener: (diff: PeerDiff) => void): () => void
+}
+
export interface NodeTerminalApi {
pty: PtyApi
workspace: WorkspaceApi
@@ -1044,6 +1065,7 @@ export interface NodeTerminalApi {
remoteClient: RemoteClientApi
handoff: HandoffApi
pairing: PairingApi
+ presence: PresenceApi
/** Fires when the user presses Cmd/Ctrl+M (toggle markdown view). Returns unsubscribe. */
onMarkdownToggle(listener: () => void): () => void
/** Fires when the user presses Cmd/Ctrl+W (close selected node). Returns unsubscribe. */