/**
 * Message Router Service
 *
 * Provides transparent cross-device message routing for agent communication.
 * When a target agent session is not found locally, the router queries
 * CloudSyncService for same-account devices that host the agent and forwards
 * the message through the Cloud message queue.
 *
 * On the receiving end, listens for incoming `agent_message` events from
 * CloudSyncService and delivers them to the local agent session via the
 * standard terminal delivery path.
 *
 * This makes cross-device communication fully transparent to callers —
 * orchestrator skills (send-message, delegate-task) require zero changes.
 *
 * @module services/messaging/message-router.service
 */
import type { SyncDevice, IncomingMessage } from '../cloud/cloud-sync.types.js';
/** Constants for the MessageRouterService. */
export declare const MESSAGE_ROUTER_CONSTANTS: {
    /** Maximum time to wait for remote delivery confirmation (ms) */
    readonly REMOTE_DELIVERY_TIMEOUT_MS: 15000;
    /** Log component name */
    readonly COMPONENT_NAME: "MessageRouterService";
};
/** Result of a remote routing attempt. */
export interface RouteResult {
    /** Whether the message was successfully routed to a remote device */
    routed: boolean;
    /** Target device ID (if routed) */
    deviceId?: string;
    /** Target device name (if routed) */
    deviceName?: string;
    /** Error message (if routing failed) */
    error?: string;
}
/**
 * Handler function for delivering a message to a local agent session.
 * Injected via setLocalDeliveryHandler() to avoid circular dependencies
 * with terminal.controller / AgentRegistrationService.
 */
export type LocalDeliveryHandler = (sessionName: string, message: string) => Promise<{
    success: boolean;
    error?: string;
}>;
/**
 * MessageRouterService singleton.
 *
 * Transparently routes agent messages across devices under the same
 * Cloud account. Local sessions are delivered directly; remote sessions
 * are forwarded via the Cloud message queue.
 *
 * @example
 * ```typescript
 * const router = MessageRouterService.getInstance();
 * router.setLocalDeliveryHandler(myDeliveryFn);
 * router.startListening();
 *
 * // Route a message — automatically determines local vs remote
 * const result = await router.routeRemote('crewly-product-leo', 'Hello Leo');
 * if (result.routed) {
 *   console.log(`Routed to device ${result.deviceName}`);
 * }
 * ```
 */
export declare class MessageRouterService {
    private static instance;
    private readonly logger;
    /** Whether we are listening for incoming agent_message events */
    private listening;
    /** Bound handler reference for cleanup */
    private boundMessageHandler;
    /** Handler for delivering messages to local agent sessions */
    private localDeliveryHandler;
    /** Handler for enqueuing messages into the message queue */
    private messageEnqueueHandler;
    private constructor();
    /**
     * Get the singleton instance.
     *
     * @returns MessageRouterService instance
     */
    static getInstance(): MessageRouterService;
    /**
     * Reset the singleton (for testing).
     */
    static resetInstance(): void;
    /**
     * Set the handler used to deliver messages to local agent sessions.
     * Must be called before startListening() to enable incoming message delivery.
     *
     * @param handler - Function that delivers a message to a local session
     */
    setLocalDeliveryHandler(handler: LocalDeliveryHandler): void;
    /**
     * Set the handler for enqueuing messages into the message queue.
     * Used by cross-machine message handling to route messages through
     * the standard queue pipeline (with source metadata for reply routing).
     */
    setMessageEnqueueHandler(handler: (input: {
        content: string;
        conversationId: string;
        source: string;
        sourceMetadata?: Record<string, unknown>;
    }) => void): void;
    /**
     * Find which remote device hosts the given agent session.
     *
     * Searches the cached device list from CloudSyncService, inspecting
     * each device's team summaries for matching agent session names.
     *
     * @param sessionName - Target agent session name
     * @returns The SyncDevice hosting the session, or null if not found
     */
    findDeviceForSession(sessionName: string): SyncDevice | null;
    /**
     * Route a message to a remote device via the Cloud message queue.
     *
     * Called when a target agent session is not found locally. Queries
     * CloudSyncService for the device hosting that session and sends
     * an `agent_message` through the Cloud queue.
     *
     * @param targetSession - Target agent session name
     * @param message - Message content to deliver
     * @param priority - Delivery priority (default: 'normal')
     * @returns RouteResult indicating success/failure
     *
     * @example
     * ```typescript
     * const result = await router.routeRemote('crewly-product-leo', 'Build the feature');
     * if (!result.routed) {
     *   console.error('Remote routing failed:', result.error);
     * }
     * ```
     */
    routeRemote(targetSession: string, message: string, priority?: 'high' | 'normal'): Promise<RouteResult>;
    /**
     * Start listening for incoming `agent_message` events from CloudSyncService.
     *
     * When an `agent_message` is received, it extracts the target session name
     * and delivers the message to the local agent via the configured
     * localDeliveryHandler.
     */
    startListening(): void;
    /**
     * Stop listening for incoming messages.
     */
    stopListening(): void;
    /**
     * Handle an incoming message from CloudSyncService.
     * Only processes messages of type `agent_message`.
     *
     * @param msg - The incoming message from another device
     */
    handleIncomingMessage(msg: IncomingMessage): Promise<void>;
    /**
     * Check if the router is currently listening for incoming messages.
     *
     * @returns True if listening
     */
    isListening(): boolean;
    /**
     * Handle an incoming cross-machine message from CloudSync.
     *
     * Cross-machine messages wrap a CrossMachineMessage object in the payload.
     * The message content is delivered to the orchestrator via the message queue,
     * formatted similarly to a Slack/chat message so it appears in the
     * orchestrator's conversation flow.
     *
     * @param msg - The incoming CloudSync message with type 'cross-machine'
     */
    private handleCrossMachineMessage;
    /**
     * Get local device identity info from CloudSyncService config.
     * Falls back to defaults if sync is not started.
     *
     * @returns Object with deviceId and deviceName
     */
    private getLocalDeviceInfo;
}
//# sourceMappingURL=message-router.service.d.ts.map