/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
/**
 * @plan PLAN-20250120-DEBUGLOGGING.P15
 * @requirement REQ-INT-001.1
 */
import { DebugLogger } from '../../debug/index.js';
import { getActiveProviderRuntimeContext } from '../../runtime/providerRuntimeContext.js';
import { RESPONSES_API_MODELS } from './RESPONSES_API_MODELS.js';
// Create a single logger instance for the module (following singleton pattern)
const logger = new DebugLogger('llxprt:openai:provider');
/**
 * Retrieves OpenAI provider information from the current ProviderManager instance
 * @param providerManager The ProviderManager instance
 * @returns OpenAI provider info if available, null values otherwise
 */
export function getOpenAIProviderInfo(providerManager) {
    const result = {
        provider: null,
        conversationCache: null,
        isResponsesAPI: false,
        currentModel: null,
        remoteTokenInfo: {},
    };
    try {
        const runtime = getActiveProviderRuntimeContext();
        const settingsService = runtime.settingsService;
        const config = runtime.config;
        const runtimeManager = typeof config?.getProviderManager === 'function'
            ? config.getProviderManager()
            : undefined;
        const manager = providerManager ?? runtimeManager ?? null;
        const activeProviderName = (manager?.hasActiveProvider?.()
            ? manager.getActiveProviderName()
            : undefined) ??
            (typeof config?.getProvider === 'function'
                ? config.getProvider()
                : undefined);
        if (activeProviderName !== 'openai') {
            return result;
        }
        // Narrow to expected provider type using feature detection for ancillary data
        let openaiProvider = null;
        if (manager && manager.hasActiveProvider()) {
            const activeProvider = manager.getActiveProvider();
            if (activeProvider?.name === 'openai') {
                openaiProvider = activeProvider;
            }
        }
        result.provider = openaiProvider;
        if (openaiProvider) {
            if (typeof openaiProvider.getConversationCache === 'function') {
                result.conversationCache = openaiProvider.getConversationCache();
            }
            else if ('conversationCache' in openaiProvider) {
                result.conversationCache =
                    openaiProvider.conversationCache ?? null;
            }
        }
        const ephemeralModel = settingsService.get('model');
        const providerSettings = settingsService.getProviderSettings('openai') ??
            {};
        const providerModel = providerSettings.model;
        const configModel = typeof config?.getModel === 'function' ? config.getModel() : undefined;
        const normalizedModel = (typeof ephemeralModel === 'string' && ephemeralModel.trim() !== ''
            ? ephemeralModel.trim()
            : undefined) ??
            providerModel ??
            configModel;
        result.currentModel = normalizedModel ?? null;
        const configuredMode = providerSettings.apiMode ??
            providerSettings.responsesMode ??
            settingsService.get('responses-mode');
        if (configuredMode) {
            result.isResponsesAPI = configuredMode.toLowerCase() === 'responses';
        }
        else if (result.currentModel) {
            if (openaiProvider?.shouldUseResponses) {
                result.isResponsesAPI = openaiProvider.shouldUseResponses(result.currentModel);
            }
            else {
                result.isResponsesAPI = RESPONSES_API_MODELS.includes(result.currentModel);
            }
        }
        // Note: Remote token info would need to be tracked separately during API calls
        // This is a placeholder for where that information would be stored
    }
    catch (error) {
        logger.debug(() => `Error accessing OpenAI provider info: ${error}`);
    }
    return result;
}
/**
 * Example usage:
 *
 * const openAIInfo = getOpenAIProviderInfo(providerManager);
 * if (openAIInfo.provider && openAIInfo.conversationCache) {
 *   // Access conversation cache
 *   const cachedMessages = openAIInfo.conversationCache.get(conversationId, parentId);
 *
 *   // Check if using Responses API
 *   if (openAIInfo.isResponsesAPI) {
 *     console.log('Using OpenAI Responses API');
 *   }
 * }
 */
//# sourceMappingURL=getOpenAIProviderInfo.js.map