/** * Inter-agent communication broker for `spectral serve`. * * Provides project-scoped, channel-based messaging between active agent * sessions running inside a single `spectral serve` process. Messages are * durably backed by the same-machine SQLite `sessions.db` so they survive * reconnects and can be polled by sessions that were not attached when the * message was sent. * * The broker is intentionally machine-local and process-local: * - Live subscriptions are in-memory within the current `spectral serve` * daemon only. * - Durable delivery relies solely on the shared same-machine SQLite backing * store. There is no relay/cloud fan-out and no cross-process live push. * - A session in a different process (or on a different machine) is not * notified automatically; it must poll the same local `sessions.db` via its * own broker instance. */ import type { SessionStore } from "./storage.js"; export interface InterAgentMessage { /** Unique message id (UUID). */ id: string; /** Owning project. */ projectId: string; /** Logical channel within the project (e.g. "memory", "task-handoff"). */ channel: string; /** Session that sent the message. */ senderSessionId: string; /** Optional human-readable sender name. */ senderName?: string; /** When set, the message is a private direct message; otherwise broadcast. */ recipientSessionId?: string; /** Message kind used to route interpretation (e.g. "observation"). */ kind: string; /** Opaque JSON payload. */ payload: string; /** Timestamp (ms since epoch) when the message was created. */ createdAt: number; /** Timestamp (ms) when the recipient first polled it, if ever. */ deliveredAt?: number; /** Timestamp (ms) after which the message may be garbage collected. */ expiresAt?: number; } export interface InterAgentSendInput { projectId: string; channel: string; senderSessionId: string; senderName?: string; /** Omit for broadcast. */ recipientSessionId?: string; kind: string; payload: string; /** Default: 24 hours. Set to 0 to disable expiry. */ ttlSeconds?: number; } export interface InterAgentPollInput { projectId: string; /** Filter by channel. Omit to poll all channels in the project. */ channel?: string; /** Poll only messages addressed to this session (or broadcast). Omit to poll all. */ recipientSessionId?: string; /** Only return messages created after this timestamp (exclusive). */ since?: number; /** Maximum messages to return. Default 100. */ limit?: number; /** Mark returned messages as delivered. Default true. */ markDelivered?: boolean; } export interface InterAgentSubscribeInput { sessionId: string; projectId: string; channel?: string; handler: (msg: InterAgentMessage) => void; } export interface InterAgentBrokerOptions { store: SessionStore; /** Default TTL in seconds for messages that don't specify one. Default 86400. */ defaultTtlSeconds?: number; } /** * In-process broker for inter-session messages. * * All public methods are synchronous because the underlying `SessionStore` * uses better-sqlite3. Live subscribers are called synchronously from `send`. */ export declare class InterAgentBroker { private readonly store; private readonly defaultTtlSeconds; /** * In-process subscriptions keyed by `projectId:channel`. Each active session * in the current process registers a handler here to receive live pushes. */ private readonly subscriptions; constructor(opts: InterAgentBrokerOptions); /** * Send a message. Persists it to SQLite and synchronously pushes it to any * in-process subscribers whose session/channel filters match. */ send(input: InterAgentSendInput): InterAgentMessage; /** * Poll for messages matching the filter. By default marks returned messages * as delivered so they won't be returned again on subsequent polls. */ poll(input: InterAgentPollInput): InterAgentMessage[]; /** * Subscribe a session to live pushes for a project/channel. Returns an * unsubscribe function. Subscriptions are in-process only. */ subscribe(input: InterAgentSubscribeInput): () => void; /** * Delete expired messages from the backing store. Call periodically. */ cleanup(maxAgeSeconds?: number): void; private pushToSubscribers; } //# sourceMappingURL=inter-agent-broker.d.ts.map