/**
 * Message Queue with Steer/Followup/Collect Modes
 *
 * OpenClaw-inspired message handling during agent execution.
 *
 * Modes:
 * - steer: New messages interrupt the current execution (injected as user message)
 * - followup: Messages are queued and processed after the current turn completes
 * - collect: Messages are batched into a single string for the next turn
 */
import { EventEmitter } from 'events';
// ============================================================================
// Message Queue
// ============================================================================
export class MessageQueue extends EventEmitter {
    mode = 'followup';
    queue = [];
    processing = false;
    /**
     * Set the queue mode
     */
    setMode(mode) {
        this.mode = mode;
        this.emit('mode-changed', mode);
    }
    /**
     * Get current mode
     */
    getMode() {
        return this.mode;
    }
    /**
     * Enqueue a message
     */
    enqueue(msg) {
        this.queue.push(msg);
        this.emit('message-enqueued', msg);
        if (this.mode === 'steer' && this.processing) {
            this.emit('steering-available');
        }
    }
    /**
     * Check if a steering message is available (steer mode)
     */
    hasSteeringMessage() {
        return this.mode === 'steer' && this.queue.length > 0;
    }
    /**
     * Consume the first steering message (steer mode)
     */
    consumeSteeringMessage() {
        if (this.mode !== 'steer' || this.queue.length === 0) {
            return null;
        }
        return this.queue.shift() || null;
    }
    /**
     * Drain all queued messages (followup mode)
     */
    drain() {
        const messages = [...this.queue];
        this.queue = [];
        return messages;
    }
    /**
     * Collect all queued messages into a single string (collect mode)
     */
    collect() {
        if (this.queue.length === 0)
            return '';
        const combined = this.queue
            .map(m => `[${m.source}] ${m.content}`)
            .join('\n');
        this.queue = [];
        return combined;
    }
    /**
     * Signal that agent is starting processing
     */
    startProcessing() {
        this.processing = true;
        this.emit('processing-started');
    }
    /**
     * Signal that agent has finished processing
     */
    endProcessing() {
        this.processing = false;
        this.emit('processing-ended');
    }
    /**
     * Check if there are pending messages
     */
    hasPendingMessages() {
        return this.queue.length > 0;
    }
    /**
     * Get the number of pending messages
     */
    get length() {
        return this.queue.length;
    }
    /**
     * Check if the agent is currently processing
     */
    isProcessing() {
        return this.processing;
    }
    /**
     * Clear the queue
     */
    clear() {
        this.queue = [];
    }
}
//# sourceMappingURL=message-queue.js.map