/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
function cloneAndFreeze(value) {
    if (!value) {
        return undefined;
    }
    const clone = Object.assign({}, value);
    return Object.freeze(clone);
}
export function createRuntimeInvocationContext(init) {
    const runtimeId = typeof init.runtime.runtimeId === 'string' &&
        init.runtime.runtimeId.trim() !== ''
        ? init.runtime.runtimeId
        : (init.fallbackRuntimeId ?? '');
    if (!runtimeId) {
        throw new Error('RuntimeInvocationContext requires a non-empty runtimeId.');
    }
    const mergedMetadata = Object.freeze({
        ...(init.runtime.metadata ?? {}),
        ...(init.metadata ?? {}),
    });
    if (init.ephemeralsSnapshot === undefined) {
        throw new Error(`RuntimeInvocationContext requires provider ephemerals for provider "${init.providerName}".`);
    }
    const ephemerals = Object.freeze({ ...init.ephemeralsSnapshot });
    const redaction = cloneAndFreeze(init.redaction) ?? undefined;
    const userMemory = init.userMemory;
    const context = {
        runtimeId,
        metadata: mergedMetadata,
        settings: init.settings,
        ephemerals,
        telemetry: init.telemetry,
        userMemory,
        redaction,
        getEphemeral(key) {
            return ephemerals[key];
        },
        getProviderOverrides(providerName) {
            const raw = ephemerals[providerName];
            if (!raw || typeof raw !== 'object') {
                return undefined;
            }
            return raw;
        },
    };
    return Object.freeze(context);
}
//# sourceMappingURL=RuntimeInvocationContext.js.map