import { listConfiguredModels } from '../config.js';
import { resolveModelCapabilities } from '../model-capabilities.js';
function legacyAvailability(availability) {
    switch (availability.kind) {
        case 'ok':
            return 'available';
        case 'unknown':
            return 'unknown';
        default:
            return 'unavailable';
    }
}
export async function handleModels(_req, res, config, modelTruth) {
    const models = listConfiguredModels(config);
    const snapshot = modelTruth ? await modelTruth.getSnapshot() : null;
    const data = models.map(m => {
        const contextCapability = snapshot?.byModelId[m.id]?.contextCapability
            ?? resolveModelCapabilities(m).context;
        return {
            id: m.id,
            display_name: m.label,
            created_at: '2026-01-01T00:00:00Z',
            type: 'model',
            availability: snapshot?.byModelId[m.id]
                ? legacyAvailability(snapshot.byModelId[m.id].availability)
                : (m.availability ?? 'unknown'),
            context_window: contextCapability.contextWindow,
            context_window_source: contextCapability.source,
            context_window_confidence: contextCapability.confidence,
            context_window_labels: contextCapability.labels,
        };
    });
    const responseBody = {
        data,
        has_more: false,
        first_id: data[0]?.id ?? null,
        last_id: data[data.length - 1]?.id ?? null,
    };
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Headers', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
    res.writeHead(200, { 'Content-Type': 'application/json' });
    res.end(JSON.stringify(responseBody));
}
//# sourceMappingURL=models.js.map