"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLocalRuntimeReadiness = void 0;
const LOCAL_RUNTIME_DEFINITIONS = [
    {
        id: 'ollama',
        name: 'Ollama',
        endpoint: 'http://127.0.0.1:11434',
        modelsUrl: 'http://127.0.0.1:11434/api/tags',
        commandHint: 'ollama serve',
        pullCommandHint: 'ollama pull',
        recommendedModel: 'gemma4:e4b',
        parseModelIds: (payload) => {
            const models = payload?.models;
            return Array.isArray(models)
                ? models
                    .map((model) => (typeof model?.name === 'string' ? model.name.trim() : ''))
                    .filter(Boolean)
                : [];
        },
    },
    {
        id: 'llamacpp',
        name: 'llama.cpp',
        endpoint: 'http://127.0.0.1:8080',
        modelsUrl: 'http://127.0.0.1:8080/v1/models',
        commandHint: './server --host 0.0.0.0 --port 8080 -m model.gguf',
        recommendedModel: null,
        parseModelIds: (payload) => {
            const models = payload?.data;
            return Array.isArray(models)
                ? models
                    .map((model) => (typeof model?.id === 'string' ? model.id.trim() : ''))
                    .filter(Boolean)
                : [];
        },
    },
];
async function fetchModelIds(definition) {
    const response = await fetch(definition.modelsUrl, {
        signal: AbortSignal.timeout(1500),
    });
    if (!response.ok) {
        throw new Error(`${definition.id} readiness probe failed (${response.status})`);
    }
    return definition.parseModelIds(await response.json());
}
function toReadiness(definition, modelIds) {
    const recommendedModelInstalled = definition.recommendedModel
        ? modelIds.some((modelId) => modelId === definition.recommendedModel)
        : modelIds.length > 0;
    const status = modelIds.length === 0 || !recommendedModelInstalled ? 'missing-model' : 'ready';
    return {
        id: definition.id,
        name: definition.name,
        endpoint: definition.endpoint,
        status,
        commandHint: status === 'missing-model' && definition.recommendedModel && definition.pullCommandHint
            ? `${definition.pullCommandHint} ${definition.recommendedModel}`
            : definition.commandHint,
        recommendedModel: definition.recommendedModel,
        recommendedModelInstalled,
        detectedModelCount: modelIds.length,
    };
}
function toOffline(definition) {
    return {
        id: definition.id,
        name: definition.name,
        endpoint: definition.endpoint,
        status: 'offline',
        commandHint: definition.commandHint,
        recommendedModel: definition.recommendedModel,
        recommendedModelInstalled: false,
        detectedModelCount: 0,
    };
}
async function getLocalRuntimeReadiness() {
    return Promise.all(LOCAL_RUNTIME_DEFINITIONS.map(async (definition) => {
        try {
            return toReadiness(definition, await fetchModelIds(definition));
        }
        catch {
            return toOffline(definition);
        }
    }));
}
exports.getLocalRuntimeReadiness = getLocalRuntimeReadiness;
//# sourceMappingURL=local-runtime-readiness.js.map