/**
 * PersistenceHandler — writes AgentController events to SessionManager.
 *
 * Owns timing state (turn start, tool start times). Computes duration deltas
 * and writes entries to SessionManager. Tracks the last assistant message for
 * downstream retry/compaction checks.
 *
 * Does NOT own turnIndex — that's a shared pipeline counter passed in by the
 * orchestrator so both this handler and the extension emitter see the same value.
 *
 * Part of the AgentSession event pipeline (step 4). Does not reference
 * AgentSession or any other pipeline handler.
 */
// ============================================================================
// Handler
// ============================================================================
export class PersistenceHandler {
    // Timing state
    _turnStartTime = 0;
    _toolStartTimes = new Map();
    // Last assistant message — read by the pipeline for retry/compaction decisions
    _lastAssistantMessage = undefined;
    /** Last assistant message seen. Read by the pipeline on agent_end. */
    get lastAssistantMessage() {
        return this._lastAssistantMessage;
    }
    /** Clear lastAssistantMessage after the pipeline has consumed it. */
    clearLastAssistant() {
        this._lastAssistantMessage = undefined;
    }
    /**
     * Reset state and snapshot system prompt + tools at the start of an agent invocation.
     * Called directly by the pipeline on agent_start (not through handle()).
     */
    startAgent(sessionManager, systemPrompt, tools) {
        this._turnStartTime = 0;
        this._toolStartTimes.clear();
        this._lastAssistantMessage = undefined;
        sessionManager.appendSystemPrompt(systemPrompt);
        sessionManager.appendToolDefinitions(tools);
    }
    /**
     * Handle a non-agent_start event: write timing + message data to SessionManager.
     *
     * agent_start is handled by startAgent() — the pipeline calls that directly
     * because it needs to pass the system prompt and tool snapshot.
     */
    handle(event, sessionManager, turnIndex) {
        switch (event.type) {
            case 'turn_start':
                this._turnStartTime = Date.now();
                sessionManager.appendTurnStart(turnIndex);
                break;
            case 'tool_execution_start':
                this._toolStartTimes.set(event.toolCallId, Date.now());
                break;
            // tool_execution_end: no action — duration computed on message_end(toolResult)
            case 'message_end':
                this._onMessageEnd(event, sessionManager);
                break;
            case 'turn_end': {
                const durationMs = this._turnStartTime > 0 ? Date.now() - this._turnStartTime : 0;
                sessionManager.appendTurnEnd(turnIndex, durationMs);
                break;
            }
        }
    }
    // --------------------------------------------------------------------------
    // Internal
    // --------------------------------------------------------------------------
    _onMessageEnd(event, sessionManager) {
        const { message } = event;
        if (message.role === 'custom') {
            const custom = message;
            sessionManager.appendCustomMessageEntry(custom.customType, custom.content, custom.display, custom.details);
            return;
        }
        if (message.role === 'assistant') {
            const durationMs = this._turnStartTime > 0 ? Date.now() - this._turnStartTime : undefined;
            sessionManager.appendMessage(message, durationMs);
            this._lastAssistantMessage = message;
            return;
        }
        if (message.role === 'toolResult') {
            const toolCallId = message.toolCallId;
            let durationMs;
            if (toolCallId) {
                const startTime = this._toolStartTimes.get(toolCallId);
                if (startTime) {
                    durationMs = Date.now() - startTime;
                    this._toolStartTimes.delete(toolCallId);
                }
            }
            sessionManager.appendMessage(message, durationMs);
            return;
        }
        if (message.role === 'user') {
            sessionManager.appendMessage(message);
        }
        // Other message types are persisted elsewhere — not through the event pipeline.
    }
}
//# sourceMappingURL=persistence-handler.js.map