import { AgentMetrics } from '../telemetry/meter.js';
/**
 * Result returned by the agent loop.
 */
export class AgentResult {
    type = 'agentResult';
    /**
     * The stop reason from the final model response.
     */
    stopReason;
    /**
     * The last message added to the messages array.
     */
    lastMessage;
    /**
     * Local execution traces collected during the agent invocation.
     * Contains timing and hierarchy of operations within the agent loop.
     */
    traces;
    /**
     * The validated structured output from the LLM, if a schema was provided.
     * Type represents any validated Zod schema output.
     */
    structuredOutput;
    /**
     * Aggregated metrics for the agent's loop execution.
     * Tracks cycle counts, token usage, tool execution stats, and model latency.
     */
    metrics;
    /**
     * Per-invocation state passed into the agent, threaded through hooks and
     * tools, and surfaced here at the end of the invocation. See
     * {@link InvocationState} for details. Always defined — defaults to `{}` when
     * no `invocationState` was provided in {@link InvokeOptions}.
     */
    invocationState;
    /**
     * Interrupts that caused the agent to stop, when `stopReason` is `'interrupt'`.
     * Contains the unanswered interrupts that require human input to resume.
     */
    interrupts;
    constructor(data) {
        this.stopReason = data.stopReason;
        this.lastMessage = data.lastMessage;
        this.invocationState = data.invocationState;
        if (data.traces !== undefined) {
            this.traces = data.traces;
        }
        if (data.metrics !== undefined) {
            this.metrics = data.metrics;
        }
        if (data.structuredOutput !== undefined) {
            this.structuredOutput = data.structuredOutput;
        }
        if (data.interrupts !== undefined) {
            this.interrupts = data.interrupts;
        }
    }
    /**
     * The most recent input token count from the last model invocation.
     * Convenience accessor that delegates to `metrics.latestContextSize`.
     * Returns `undefined` when no metrics or invocations are available.
     */
    get contextSize() {
        return this.metrics?.latestContextSize;
    }
    /**
     * Projected context size for the next model call (inputTokens + outputTokens from the last call).
     * Convenience accessor that delegates to `metrics.projectedContextSize`.
     * Returns `undefined` when no metrics or invocations are available.
     */
    get projectedContextSize() {
        return this.metrics?.projectedContextSize;
    }
    /**
     * Custom JSON serialization that excludes traces, metrics, and invocationState.
     * Traces and metrics are excluded to avoid sending large payloads over the wire
     * in API responses; `invocationState` is excluded because its values are
     * caller-owned and may not be serializable (see {@link InvocationState}).
     *
     * All three remain accessible via their properties for debugging.
     *
     * @returns Object representation for safe serialization
     */
    toJSON() {
        return {
            type: this.type,
            stopReason: this.stopReason,
            lastMessage: this.lastMessage,
            ...(this.structuredOutput !== undefined && { structuredOutput: this.structuredOutput }),
        };
    }
    /**
     * Extracts a string representation of the result.
     *
     * Priority order:
     * 1. `interrupts` serialized as JSON, if any are present
     * 2. `structuredOutput` serialized as JSON
     * 3. Text from `textBlock`, `reasoningBlock`, and `citationsBlock` content blocks
     *
     * @returns String representation of the result: JSON for interrupts/structuredOutput, or text content joined by newlines.
     */
    toString() {
        if (this.interrupts && this.interrupts.length > 0) {
            return JSON.stringify(this.interrupts);
        }
        if (this.structuredOutput !== undefined) {
            return JSON.stringify(this.structuredOutput);
        }
        const textParts = [];
        for (const block of this.lastMessage.content) {
            switch (block.type) {
                case 'textBlock':
                    textParts.push(block.text);
                    break;
                case 'reasoningBlock':
                    if (block.text) {
                        // Add indentation to reasoning content
                        const indentedText = block.text.replace(/\n/g, '\n   ');
                        textParts.push(`💭 Reasoning:\n   ${indentedText}`);
                    }
                    break;
                case 'citationsBlock':
                    for (const c of block.content) {
                        if ('text' in c) {
                            textParts.push(c.text);
                        }
                    }
                    break;
                default:
                    console.debug(`Skipping content block type: ${block.type}`);
                    break;
            }
        }
        return textParts.join('\n');
    }
}
//# sourceMappingURL=agent.js.map