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(core): CorePlatform.onWithSender — casts carry the sender id

enes40253e14bb00
diff --git a/src/core/platform-fake.ts b/src/core/platform-fake.ts
index a1b279ebf..55bc46b3f 100644
--- a/src/core/platform-fake.ts
+++ b/src/core/platform-fake.ts
@@ -3,6 +3,7 @@ import type { CorePlatform } from './platform'
export interface FakePlatform extends CorePlatform {
handlers: Record<string, (...args: any[]) => unknown>
listeners: Record<string, (...args: any[]) => void>
+ senderListeners: Record<string, (senderId: number, ...args: any[]) => void>
sent: Array<{ to: number | 'broadcast'; channel: string; args: any[] }>
opened: string[]
}
@@ -15,6 +16,7 @@ export function fakePlatform(overrides: Partial<CorePlatform> = {}): FakePlatfor
isPackaged: false,
handlers: {},
listeners: {},
+ senderListeners: {},
sent: [],
opened: [],
handle(ch, fn) {
@@ -26,6 +28,9 @@ export function fakePlatform(overrides: Partial<CorePlatform> = {}): FakePlatfor
handleWithSender(ch, fn) {
f.handlers[ch] = fn as (...args: any[]) => unknown
},
+ onWithSender(ch, fn) {
+ f.senderListeners[ch] = fn
+ },
sendTo(to, channel, ...args) {
f.sent.push({ to, channel, args })
},
diff --git a/src/core/platform.test.ts b/src/core/platform.test.ts
index ed656083f..db1cc5214 100644
--- a/src/core/platform.test.ts
+++ b/src/core/platform.test.ts
@@ -28,4 +28,12 @@ describe('core platform accessor', () => {
])
expect(fake.opened).toEqual(['https://example.com'])
})
+
+ it('fake records onWithSender listeners and passes the sender id through', () => {
+ const fake = fakePlatform()
+ const seen: Array<[number, string]> = []
+ fake.onWithSender('a:cast', (senderId: number, text: string) => seen.push([senderId, text]))
+ fake.senderListeners['a:cast'](7, 'hi')
+ expect(seen).toEqual([[7, 'hi']])
+ })
})
diff --git a/src/core/platform.ts b/src/core/platform.ts
index 56cb64f25..4f46715b8 100644
--- a/src/core/platform.ts
+++ b/src/core/platform.ts
@@ -14,6 +14,9 @@ export interface CorePlatform {
on(channel: string, fn: (...args: any[]) => void): void
/** Like handle, but fn receives the calling UI's numeric id first (Electron: event.sender.id). */
handleWithSender(channel: string, fn: (senderId: number, ...args: any[]) => unknown): void
+ /** Like on, but fn receives the calling UI's numeric id first (Electron: event.sender.id).
+ * The seam presence (and, later, typing attribution) needs: a cast must say WHO sent it. */
+ onWithSender(channel: string, fn: (senderId: number, ...args: any[]) => void): void
/** Send to one attached UI by id (Electron: webContents.fromId). Silently drops if gone. */
sendTo(uiId: number, channel: string, ...args: any[]): void
/** Send to every attached UI (Electron: the main window). */
diff --git a/src/main/platform-electron.ts b/src/main/platform-electron.ts
index 1f0bbed26..547ba6b03 100644
--- a/src/main/platform-electron.ts
+++ b/src/main/platform-electron.ts
@@ -17,6 +17,7 @@ export function electronPlatform(): CorePlatform {
handle: (ch, fn) => ipcMain.handle(ch, (_e, ...args) => fn(...args)),
on: (ch, fn) => ipcMain.on(ch, (_e, ...args) => fn(...args)),
handleWithSender: (ch, fn) => ipcMain.handle(ch, (e, ...args) => fn(e.sender.id, ...args)),
+ onWithSender: (ch, fn) => ipcMain.on(ch, (e, ...args) => fn(e.sender.id, ...args)),
sendTo: (id, ch, ...args) => {
const wc = webContents.fromId(id)
if (wc && !wc.isDestroyed()) wc.send(ch, ...args)
diff --git a/src/server/platform-server.test.ts b/src/server/platform-server.test.ts
index 790aa5bfe..fce261c57 100644
--- a/src/server/platform-server.test.ts
+++ b/src/server/platform-server.test.ts
@@ -45,6 +45,22 @@ describe('ServerPlatform', () => {
expect(got).toEqual(['hello'])
})
+ it('cast delivers the sending uiId to onWithSender listeners (and still runs plain on)', () => {
+ const p = new ServerPlatform({ userDataDir: '/tmp/x', appVersion: '1.0.0' })
+ const withSender: Array<[number, unknown]> = []
+ const plain: unknown[] = []
+ p.onWithSender('presence:cursor', (senderId: number, cursor: unknown) =>
+ withSender.push([senderId, cursor])
+ )
+ p.on('presence:cursor', (cursor: unknown) => plain.push(cursor))
+ const ui = p.attach(fakeSink().sink)
+ p.cast(ui, 'presence:cursor', [{ x: 1, y: 2 }])
+ expect(withSender).toEqual([[ui, { x: 1, y: 2 }]])
+ expect(plain).toEqual([{ x: 1, y: 2 }])
+ // Unknown method: silent no-op, never throws.
+ expect(() => p.cast(ui, 'nope', [])).not.toThrow()
+ })
+
it('on() supports multiple listeners per channel; cast fires all of them', () => {
const p = new ServerPlatform({ userDataDir: '/tmp', appVersion: '0' })
const hits: string[] = []
diff --git a/src/server/platform-server.ts b/src/server/platform-server.ts
index 76d0f4489..da6f44203 100644
--- a/src/server/platform-server.ts
+++ b/src/server/platform-server.ts
@@ -31,6 +31,7 @@ export class ServerPlatform implements CorePlatform {
private handlers = new Map<string, Handler>()
private listeners = new Map<string, Set<(...args: any[]) => void>>()
+ private senderListeners = new Map<string, Set<(senderId: number, ...args: any[]) => void>>()
private sinks = new Map<number, UiSink>()
private nextUiId = 1
@@ -65,6 +66,15 @@ export class ServerPlatform implements CorePlatform {
set.add(fn)
}
+ onWithSender(channel: string, fn: (senderId: number, ...args: any[]) => void): void {
+ let set = this.senderListeners.get(channel)
+ if (!set) {
+ set = new Set()
+ this.senderListeners.set(channel, set)
+ }
+ set.add(fn)
+ }
+
sendTo(uiId: number, channel: string, ...args: any[]): void {
const sink = this.sinks.get(uiId)
if (!sink) return
@@ -134,7 +144,8 @@ export class ServerPlatform implements CorePlatform {
}
cast(uiId: number, method: string, args: unknown[]): void {
- void uiId
+ const withSender = this.senderListeners.get(method)
+ if (withSender) for (const fn of withSender) fn(uiId, ...args)
const set = this.listeners.get(method)
if (!set) return
for (const fn of set) fn(...args)