/** * The app side of the platform channel: rooms an app can join, and the * messages it exchanges through them. * * The app never holds a socket. The platform shell, outside the iframe, owns * the connection and relays over `postMessage`; this module is the typed * layer on top of that contract, reached as {@link PlatformClient.channel}. */ /** What a command looks like: the payload it carries and the reply it expects. */ export interface ChannelCommandShape { payload?: unknown; reply?: unknown; } /** * The default command map, when none is given: every command name is allowed * and nothing is typed. Give `external()` your own map to get * payloads and replies checked at compile time. */ export type AnyChannelCommands = Record; /** The default event map for {@link CollabRoom}: any name, any payload. */ export type AnyChannelEvents = Record; type PayloadOf = 'payload' extends keyof T ? T['payload'] : undefined; type ReplyOf = 'reply' extends keyof T ? T['reply'] : void; /** Someone else on the other end of a room. `kind` is set for external channels. */ export interface ChannelPeer { from: string; kind?: string; } /** Who sent a message, and when. */ export interface ChannelMessageMeta { /** The sender's connection id, stamped by the platform. */ from: string; at: number; /** Present when the sender expects an answer. */ requestId?: string; /** For an external sender that declared one: `'mcp'`, `'cli'`, `'revit'`… */ kind?: string; } /** How the client talks to the shell. Replaceable, so it can be tested. */ export interface ChannelTransport { post(message: Record): void; listen(handler: (data: unknown) => void): () => void; } /** * Talks to the platform shell through `window.parent`. Only messages whose * `source` is the parent window are accepted — the same check the shell makes * in the other direction — so another frame on the page cannot inject messages * into the app. */ export declare function windowTransport(): ChannelTransport; /** A message as the platform delivers it to the app. */ export interface ChannelIncomingMessage { type: string; requestId?: string; payload?: unknown; at: number; from: string; scope?: 'collab' | 'external'; kind?: string; } /** What a room did with a message. */ export type ChannelOutcome = { handled: false; } | { handled: true; value?: unknown; error?: string; }; /** What every room shares: membership, peers and presence. */ export declare abstract class ChannelRoom { #private; private readonly transport; private readonly controls; constructor(transport: ChannelTransport, controls: { join: Record; leave: Record; }); /** Whether the platform has confirmed this tab is in the room. */ get joined(): boolean; /** Who else is in the room right now, as the platform last reported. */ get peers(): readonly ChannelPeer[]; /** * Enters the room. Resolves when the platform confirms; rejects if it * refuses or does not answer in time. Joining twice is harmless. */ join(): Promise; /** Leaves the room. Handlers stay registered, so `join()` resumes them. */ leave(): Promise; /** Fires when `joined` or `peers` changes. Returns a function that stops it. */ onChange(listener: () => void): () => void; onPeerJoined(listener: (peer: ChannelPeer) => void): () => void; onPeerLeft(listener: (peer: ChannelPeer) => void): () => void; /** @internal Does a message arriving with this scope/kind belong to this room? */ abstract accepts(scope: string | undefined, kind: string | undefined): boolean; /** @internal */ abstract handle(message: ChannelIncomingMessage): Promise; /** @internal */ applyJoined(): void; /** @internal */ applyLeft(): void; /** @internal */ applyPeers(peers: ChannelPeer[]): void; /** @internal */ applyPeerJoined(peer: ChannelPeer): void; /** @internal */ applyPeerLeft(from: string): void; /** @internal */ failPending(error: Error): boolean; protected post(message: Record): void; } /** * The collaboration room: every user's tab of this app that joined it, across * accounts. Messages here are events — nobody replies. * * `E` maps each event name to its payload, so `send` and `on` are checked. */ export declare class CollabRoom extends ChannelRoom { #private; /** @internal Use `client.channel.collab()`. */ constructor(transport: ChannelTransport); /** * Listens for an event other tabs send. Any number of listeners per event. * @returns a function that removes the listener. */ on(type: K, listener: (payload: E[K], meta: ChannelMessageMeta) => void): () => void; /** * Sends an event to the other tabs in the room. Does nothing while this tab * is not in it, matching what the platform does with a message from outside. */ send(type: K, payload: E[K]): void; accepts(scope: string | undefined): boolean; handle(message: ChannelIncomingMessage): Promise; } /** * Commands from this account's external tools — a CLI, an MCP server, a Revit * plugin — of one `kind`, or of every kind when none is given. * * `C` maps each command name to `{ payload, reply }`, so a handler's argument * and return value are checked. A command without `reply` is a notification. */ export declare class ExternalRoom extends ChannelRoom { #private; readonly kind?: string | undefined; /** @internal Use `client.channel.external(kind)`. */ constructor(transport: ChannelTransport, kind?: string | undefined); /** * Handles a command. When the sender asked for an answer, what you return * is sent back to it; if you throw, it gets an error instead of waiting for * a timeout. When it did not ask (a notification), the return value is * ignored. * * One handler per command: a second one for the same name throws, since two * would both try to answer. * @returns a function that removes the handler. */ on(type: K, handler: (payload: PayloadOf, meta: ChannelMessageMeta) => ReplyOf | Promise>): () => void; accepts(scope: string | undefined, kind: string | undefined): boolean; /** @internal Whether this room has any handler at all. */ get hasHandlers(): boolean; handle(message: ChannelIncomingMessage): Promise; } /** * Typed access to the platform channel from inside an app: a collaboration * room shared with other users, and rooms for commands from external tools. * Reach it as `client.channel`. * * @example * ```ts * type McpCommands = { * 'select-by-category': { payload: { category: string }; reply: { count: number } }; * }; * * const mcp = client.channel.external('mcp'); * mcp.on('select-by-category', async ({ category }) => ({ * count: await selectByCategory(category), * })); * await mcp.join(); * ``` */ export declare class ChannelClient { #private; constructor(transport: ChannelTransport); /** The collaboration room for this app. Always the same instance. */ collab(): CollabRoom; /** * The room for commands from this account's external tools of one `kind` * (`'mcp'`, `'cli'`, `'revit'`…), or from any of them when `kind` is omitted. * Always the same instance for the same kind. */ external(kind?: string): ExternalRoom; /** Stops listening. For tests and teardown; an app normally never calls it. */ dispose(): void; } export {};