/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
/**
 * @plan PLAN-20251028-STATELESS6.P06
 * @requirement REQ-STAT6-001.1, REQ-STAT6-001.3, REQ-STAT6-002.2, REQ-STAT6-002.3
 * @pseudocode agent-runtime-context.md lines 64-81
 *
 * Factory for creating immutable agent runtime contexts.
 */
import { HistoryService } from '../services/history/HistoryService.js';
import { tokenLimit } from '../core/tokenLimits.js';
const EPHEMERAL_DEFAULTS = {
    compressionThreshold: 0.8,
    preserveThreshold: 0.2,
    /** @plan PLAN-20251202-THINKING.P03b @requirement REQ-THINK-006 */
    reasoning: {
        enabled: true, // REQ-THINK-006.1
        includeInContext: false, // REQ-THINK-006.2
        includeInResponse: true, // REQ-THINK-006.3
        format: 'field', // REQ-THINK-006.4
        stripFromContext: 'none', // REQ-THINK-006.5
    },
};
export function createAgentRuntimeContext(options) {
    if (!options.provider) {
        throw new Error('AgentRuntimeContext requires a provider adapter. Supply options.provider.');
    }
    if (!options.telemetry) {
        throw new Error('AgentRuntimeContext requires a telemetry adapter. Supply options.telemetry.');
    }
    if (!options.tools) {
        throw new Error('AgentRuntimeContext requires a tools view. Supply options.tools.');
    }
    if (!options.providerRuntime) {
        throw new Error('AgentRuntimeContext requires a provider runtime context. Supply options.providerRuntime.');
    }
    const history = options.history ?? new HistoryService();
    const ephemerals = {
        compressionThreshold: () => options.settings.compressionThreshold ??
            EPHEMERAL_DEFAULTS.compressionThreshold,
        contextLimit: () => {
            const liveOverride = typeof options.settings.contextLimit === 'number' &&
                Number.isFinite(options.settings.contextLimit) &&
                options.settings.contextLimit > 0
                ? options.settings.contextLimit
                : undefined;
            return tokenLimit(options.state.model, liveOverride);
        },
        preserveThreshold: () => options.settings.preserveThreshold ??
            EPHEMERAL_DEFAULTS.preserveThreshold,
        toolFormatOverride: () => options.settings.toolFormatOverride,
        /**
         * @plan PLAN-20251202-THINKING.P03b
         * @requirement REQ-THINK-006
         */
        reasoning: {
            enabled: () => options.settings['reasoning.enabled'] ??
                EPHEMERAL_DEFAULTS.reasoning.enabled,
            includeInContext: () => options.settings['reasoning.includeInContext'] ??
                EPHEMERAL_DEFAULTS.reasoning.includeInContext,
            includeInResponse: () => options.settings['reasoning.includeInResponse'] ??
                EPHEMERAL_DEFAULTS.reasoning.includeInResponse,
            format: () => options.settings['reasoning.format'] ??
                EPHEMERAL_DEFAULTS.reasoning.format,
            stripFromContext: () => options.settings['reasoning.stripFromContext'] ?? EPHEMERAL_DEFAULTS.reasoning.stripFromContext,
            effort: () => options.settings['reasoning.effort'],
            maxTokens: () => typeof options.settings['reasoning.maxTokens'] === 'number'
                ? options.settings['reasoning.maxTokens']
                : undefined,
        },
    };
    const providerRuntime = Object.freeze({
        ...options.providerRuntime,
        metadata: options.providerRuntime.metadata
            ? Object.freeze({ ...options.providerRuntime.metadata })
            : undefined,
    });
    const context = {
        state: options.state,
        history,
        ephemerals,
        telemetry: options.telemetry,
        provider: options.provider,
        tools: options.tools,
        providerRuntime,
    };
    return Object.freeze(context);
}
//# sourceMappingURL=createAgentRuntimeContext.js.map