/**
 * Notification command handler.
 *
 * When the onboarding service sends a `notify` command via heartbeat,
 * this handler enqueues a synthetic system message into the message queue
 * and wakes the main loop so the notification is injected into the agent's
 * context on the next wake cycle.
 */
import { logger } from "../logger.js";
/**
 * Creates a `notify` command handler that enqueues a system message
 * and wakes the main loop.
 *
 * @param messageQueue  The persistent message queue.
 * @param wakeMainLoop  Resolves the sleep promise to wake the agent.
 */
export function createNotifyHandler(messageQueue, wakeMainLoop) {
    return (commandId, payload) => {
        const p = (payload ?? {});
        const message = p.message;
        if (!message || typeof message !== "string" || message.trim().length === 0) {
            return Promise.resolve({
                status: "failed",
                result: JSON.stringify({ error: "Missing or empty 'message' in payload" }),
            });
        }
        const source = typeof p.source === "string" && p.source.trim() ? p.source.trim() : "server";
        logger.system(`[notify] Received notification (id=${commandId}, source=${source}): ${message.slice(0, 120)}${message.length > 120 ? "..." : ""}`);
        messageQueue.enqueue({
            id: `notify_${commandId}_${Date.now()}`,
            channel: "system",
            sender: source,
            text: message,
            attachments: [],
            timestamp: new Date().toISOString(),
        });
        // Wake the main loop so the notification is processed promptly.
        wakeMainLoop();
        return Promise.resolve({
            status: "completed",
            result: JSON.stringify({ message: "Notification enqueued and agent woken" }),
        });
    };
}
//# sourceMappingURL=notification-handler.js.map