import { getLocalRuntimeProfile, resolveReferenceRuntimeProfile } from "@topaca/nova-ai";
function isTruthy(value) {
    if (!value)
        return false;
    return value === "1" || value.toLowerCase() === "true" || value.toLowerCase() === "yes";
}
function resolveProfileId() {
    const explicit = process.env.NOVA_LOCAL_PROFILE?.trim();
    if (explicit)
        return explicit;
    const reference = process.env.NOVA_LOCAL_REFERENCE?.trim();
    if (reference === "qwen3_vllm" || reference === "qwen3_sglang" || reference === "qwen3_ollama_litellm") {
        return resolveReferenceRuntimeProfile(reference).id;
    }
    if (isTruthy(process.env.NOVA_LOCAL_MODE)) {
        return resolveReferenceRuntimeProfile("qwen3_ollama_litellm").id;
    }
    return undefined;
}
function mapProfileToCompat(profile) {
    if (!profile)
        return undefined;
    return {
        supportsDeveloperRole: profile.supportsDeveloperRole,
        supportsReasoningEffort: profile.supportsReasoningEffort,
        requiresToolResultName: profile.requiresToolResultName,
        requiresAssistantAfterToolResult: profile.requiresAssistantAfterToolResult,
        maxTokensField: profile.maxTokensField,
        supportsStrictMode: profile.supportsJsonSchema,
    };
}
export function applyConfiguredLocalRuntimeProfile(modelRegistry) {
    const diagnostics = [];
    const profileId = resolveProfileId();
    if (!profileId) {
        return diagnostics;
    }
    const profile = getLocalRuntimeProfile(profileId);
    if (!profile) {
        diagnostics.push({
            type: "error",
            message: `Unknown NOVA_LOCAL_PROFILE "${profileId}".`,
        });
        return diagnostics;
    }
    const provider = process.env.NOVA_LOCAL_PROVIDER?.trim() || "openai";
    const baseUrl = process.env.NOVA_LOCAL_BASE_URL || process.env.LITELLM_BASE_URL || process.env.OPENAI_BASE_URL;
    const apiKey = process.env.NOVA_LOCAL_API_KEY || process.env.OPENAI_API_KEY;
    const compat = mapProfileToCompat(profile);
    try {
        modelRegistry.registerProvider(provider, {
            baseUrl,
            apiKey,
            compat,
        });
        diagnostics.push({
            type: "info",
            message: `Applied local runtime profile "${profile.id}" to provider "${provider}".`,
        });
    }
    catch (error) {
        diagnostics.push({
            type: "error",
            message: `Failed to apply local runtime profile "${profile.id}": ${error instanceof Error ? error.message : String(error)}`,
        });
    }
    return diagnostics;
}
//# sourceMappingURL=local-runtime-profile.js.map