/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { logApiRequest, logApiResponse, logApiError, } from '../telemetry/loggers.js';
import { ApiRequestEvent as LegacyApiRequestEvent, ApiResponseEvent as LegacyApiResponseEvent, ApiErrorEvent as LegacyApiErrorEvent, } from '../telemetry/types.js';
/**
 * Creates a mutable provider adapter backed by a ProviderManager instance.
 */
export function createProviderAdapterFromManager(manager) {
    if (!manager) {
        return {
            getActiveProvider: () => {
                throw new Error('AgentRuntimeContext provider adapter requires a ProviderManager instance.');
            },
            setActiveProvider: () => {
                throw new Error('AgentRuntimeContext provider adapter requires a ProviderManager instance.');
            },
            getProviderByName: () => {
                throw new Error('AgentRuntimeContext provider adapter requires a ProviderManager instance.');
            },
        };
    }
    return {
        getActiveProvider: () => manager.getActiveProvider(),
        setActiveProvider: (name) => manager.setActiveProvider(name),
        getProviderByName: typeof manager
            .getProviderByName === 'function'
            ? (name) => manager.getProviderByName(name)
            : undefined,
    };
}
/**
 * Creates a telemetry adapter that bridges to legacy Config-backed loggers.
 */
export function createTelemetryAdapterFromConfig(config) {
    return {
        logApiRequest: (event) => {
            const legacy = new LegacyApiRequestEvent(event.model, event.promptId ?? event.runtimeId ?? 'runtime', event.requestText);
            logApiRequest(config, legacy);
        },
        logApiResponse: (event) => {
            const legacy = new LegacyApiResponseEvent(event.model, event.durationMs, event.promptId ?? event.runtimeId ?? 'runtime', event.authType, event.usageMetadata, event.responseText, event.error);
            logApiResponse(config, legacy);
        },
        logApiError: (event) => {
            const legacy = new LegacyApiErrorEvent(event.model, event.error, event.durationMs, event.promptId ?? event.runtimeId ?? 'runtime', event.authType, event.errorType, event.statusCode);
            logApiError(config, legacy);
        },
    };
}
/**
 * Creates a ToolRegistryView from an optional ToolRegistry.
 */
export function createToolRegistryViewFromRegistry(registry) {
    if (!registry) {
        return {
            listToolNames: () => [],
            getToolMetadata: () => undefined,
        };
    }
    return {
        listToolNames: () => registry.getAllToolNames(),
        getToolMetadata: (name) => {
            const tool = registry.getTool(name);
            if (!tool) {
                return undefined;
            }
            const schema = tool
                .schema;
            const description = typeof schema?.description === 'string'
                ? schema.description
                : typeof tool.description === 'string'
                    ? tool.description
                    : '';
            const parameterSchema = schema?.parameters ??
                schema
                    ?.parametersJsonSchema;
            return {
                name: tool.name ?? name,
                description,
                parameterSchema,
            };
        },
    };
}
//# sourceMappingURL=runtimeAdapters.js.map