import { logger } from '@pellux/goodvibes-sdk/platform/utils/logger';
import { summarizeError } from '@pellux/goodvibes-sdk/platform/utils/error-display';
import { buildConversationFollowUpPrompt, normalizeConversationFollowUpItems, } from '@pellux/goodvibes-sdk/platform/core/conversation-follow-ups';
import { normalizeUsage } from './orchestrator-runtime.js';
const FOLLOW_UP_DEDUP_TTL_MS = 60_000;
const FOLLOW_UP_MAX_OUTPUT_TOKENS = 160;
const FOLLOW_UP_RETRY_DELAY_MS = 250;
export class OrchestratorFollowUpRuntime {
    options;
    queue = [];
    flushScheduled = false;
    isRunning = false;
    recentKeys = new Map();
    constructor(options) {
        this.options = options;
    }
    enqueue(item) {
        const summary = item.summary.trim();
        if (summary.length === 0)
            return;
        const now = Date.now();
        this.pruneRecent(now);
        const lastSeenAt = this.recentKeys.get(item.key);
        if (lastSeenAt !== undefined && now - lastSeenAt < FOLLOW_UP_DEDUP_TTL_MS)
            return;
        this.recentKeys.set(item.key, now);
        this.queue.push({ key: item.key, summary });
        this.scheduleFlush();
    }
    scheduleFlush() {
        if (this.flushScheduled || this.queue.length === 0)
            return;
        this.flushScheduled = true;
        queueMicrotask(() => {
            this.flushScheduled = false;
            void this.flush();
        });
    }
    pruneRecent(now) {
        for (const [key, seenAt] of this.recentKeys.entries()) {
            if (now - seenAt >= FOLLOW_UP_DEDUP_TTL_MS) {
                this.recentKeys.delete(key);
            }
        }
    }
    takeBatch() {
        const batch = normalizeConversationFollowUpItems(this.queue);
        if (batch.length === 0) {
            this.queue = [];
            return [];
        }
        const batchKeys = new Set(batch.map((item) => item.key));
        this.queue = this.queue.filter((item) => !batchKeys.has(item.key));
        return batch;
    }
    async flush() {
        if (this.queue.length === 0)
            return;
        const state = this.options.getThinkingState();
        if (state.isThinking || state.isCompacting || this.isRunning || this.options.getQueuedUserMessageCount() > 0) {
            setTimeout(() => this.scheduleFlush(), FOLLOW_UP_RETRY_DELAY_MS);
            return;
        }
        const providerRegistry = this.options.getProviderRegistry();
        const model = this.options.getCurrentModel();
        const provider = providerRegistry.get(model.provider);
        const tokenLimit = Math.min(FOLLOW_UP_MAX_OUTPUT_TOKENS, providerRegistry.getTokenLimitsForModel(model).maxOutputTokens);
        const batch = this.takeBatch();
        if (batch.length === 0)
            return;
        this.isRunning = true;
        try {
            const response = await provider.chat({
                model: model.id,
                messages: [
                    ...this.options.conversation.getMessagesForLLM(),
                    { role: 'user', content: buildConversationFollowUpPrompt(batch) },
                ],
                systemPrompt: this.options.getSystemPrompt(),
                maxTokens: tokenLimit,
                reasoningEffort: model.capabilities.reasoning ? 'low' : undefined,
            });
            const content = response.content.trim();
            if (content.length > 0) {
                this.options.conversation.addAssistantMessage(content, {
                    usage: response.usage,
                    model: model.displayName,
                    provider: model.provider,
                });
                const normalizedUsage = normalizeUsage(response.usage);
                this.options.applyUsage(normalizedUsage);
                this.options.scrollToEnd(this.options.getViewportHeight());
                this.options.requestRender();
            }
        }
        catch (error) {
            logger.debug('Orchestrator follow-up acknowledgement failed', {
                error: summarizeError(error),
                updates: batch.map((item) => item.summary),
            });
            this.options.routeLowPriorityMessage(`[Follow-up] Acknowledgement update failed: ${summarizeError(error)}`);
        }
        finally {
            this.isRunning = false;
            if (this.queue.length > 0) {
                this.scheduleFlush();
            }
        }
    }
}
