"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.resolveAgentRuntimeSelection = resolveAgentRuntimeSelection;
exports.createAgentOrchestrator = createAgentOrchestrator;
const agentv3_1 = require("../agentv3");
const providerManager_1 = require("../services/providerManager");
function parseRuntimeEnv(value) {
    switch (value) {
        case 'claude-agent-sdk':
        case 'openai-agents-sdk':
            return value;
        default:
            return undefined;
    }
}
function resolveAgentRuntimeSelection(providerId, runtimeOverride, providerScope) {
    const providerSvc = (0, providerManager_1.getProviderService)();
    const provider = typeof providerId === 'string'
        ? providerSvc.getRawProvider(providerId, providerScope)
        : providerId === null || runtimeOverride
            ? undefined
            : providerSvc.getRawEffectiveProvider(providerScope);
    if (typeof providerId === 'string' && !provider) {
        throw new Error(`Provider not found: ${providerId}`);
    }
    if (provider) {
        return {
            kind: providerSvc.resolveAgentRuntime(provider),
            source: 'provider',
            providerId: provider.id,
            providerName: provider.name,
            providerType: provider.type,
        };
    }
    if (runtimeOverride) {
        return { kind: runtimeOverride, source: 'snapshot' };
    }
    const explicitRuntime = parseRuntimeEnv(process.env.SMARTPERFETTO_AGENT_RUNTIME);
    if (explicitRuntime) {
        return { kind: explicitRuntime, source: 'env' };
    }
    if (process.env.SMARTPERFETTO_AGENT_RUNTIME) {
        throw new Error(`Unsupported SMARTPERFETTO_AGENT_RUNTIME="${process.env.SMARTPERFETTO_AGENT_RUNTIME}". ` +
            'Use "claude-agent-sdk" or "openai-agents-sdk".');
    }
    return { kind: 'claude-agent-sdk', source: 'default' };
}
function createAgentOrchestrator(input) {
    const selection = resolveAgentRuntimeSelection(input.providerId, input.runtimeOverride, input.providerScope);
    switch (selection.kind) {
        case 'openai-agents-sdk': {
            // Lazy import keeps the OpenAI runtime isolated from Claude-only startup
            // paths and avoids circular imports while both SDKs remain first-class.
            // eslint-disable-next-line @typescript-eslint/no-var-requires
            const { createOpenAIRuntime } = require('../agentOpenAI');
            return createOpenAIRuntime(input.traceProcessorService);
        }
        case 'claude-agent-sdk':
        default:
            return (0, agentv3_1.createClaudeRuntime)(input.traceProcessorService);
    }
}
//# sourceMappingURL=runtimeSelection.js.map