"use strict";
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2024-2026 Gracker (Chris)
// This file is part of SmartPerfetto. See LICENSE for details.
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildRuntimeHealthPayload = buildRuntimeHealthPayload;
const config_1 = require("../config");
const agentOpenAI_1 = require("../agentOpenAI");
const claudeConfig_1 = require("../agentv3/claudeConfig");
const envCredentialSources_1 = require("./envCredentialSources");
const runtimeSelection_1 = require("./runtimeSelection");
const providerManager_1 = require("../services/providerManager");
const version_1 = require("../version");
function buildRuntimeHealthPayload(now = new Date()) {
    const runtimeSelection = (0, runtimeSelection_1.resolveAgentRuntimeSelection)();
    const providerSvc = (0, providerManager_1.getProviderService)();
    const activeProvider = providerSvc.list().find(p => p.isActive);
    const selectedProviderId = runtimeSelection.source === 'provider'
        ? runtimeSelection.providerId
        : null;
    const claudeDiagnostics = (0, claudeConfig_1.getClaudeRuntimeDiagnostics)(runtimeSelection.kind === 'claude-agent-sdk' ? selectedProviderId : null);
    const openAIDiagnostics = (0, agentOpenAI_1.getOpenAIRuntimeDiagnostics)(runtimeSelection.kind === 'openai-agents-sdk' ? selectedProviderId : null);
    const selectedDiagnostics = runtimeSelection.kind === 'openai-agents-sdk'
        ? openAIDiagnostics
        : claudeDiagnostics;
    const envSources = (0, envCredentialSources_1.collectEnvCredentialSources)(process.env, 'health');
    const providerOverridesEnv = runtimeSelection.source === 'provider' && envSources.length > 0;
    return {
        status: 'OK',
        timestamp: now.toISOString(),
        environment: config_1.serverConfig.nodeEnv,
        version: (0, version_1.getSmartPerfettoVersion)(),
        aiEngine: {
            runtime: runtimeSelection.kind,
            model: selectedDiagnostics.model,
            providerMode: selectedDiagnostics.providerMode,
            configured: selectedDiagnostics.configured,
            source: runtimeSelection.source,
            credentialSource: runtimeSelection.source === 'provider'
                ? 'provider-manager'
                : 'env-or-default',
            envCredentialSources: envSources,
            providerOverridesEnv,
            ...(activeProvider ? {
                activeProvider: {
                    id: activeProvider.id,
                    name: activeProvider.name,
                    type: activeProvider.type,
                },
            } : {}),
            authRequired: !!process.env.SMARTPERFETTO_API_KEY
                || process.env.SMARTPERFETTO_ENTERPRISE === 'true'
                || !!process.env.SMARTPERFETTO_OIDC_ISSUER_URL,
            diagnostics: selectedDiagnostics,
        },
    };
}
//# sourceMappingURL=runtimeHealth.js.map