"use strict";
// Model Intelligence Layer — Unified tracking + conditioning
// Merged from original GAP 1 (tracking) + GAP 6 (conditioning)
Object.defineProperty(exports, "__esModule", { value: true });
exports.trackSignal = trackSignal;
exports.getConditioning = getConditioning;
exports.getInsight = getInsight;
exports.compareModels = compareModels;
exports.exportModelData = exportModelData;
exports.importModelData = importModelData;
const modelData = new Map();
function defaultMetrics() {
    return { verbosityScore: 0.5, errorRate: 0, completionRate: 0.5, latencyP50: 0, userAcceptance: 0.5, signalCount: 0, qualityScore: 0.5 };
}
function trackSignal(model, provider, accepted, edited, latencyMs, tokenCount, expectedTokens) {
    const key = `${provider}:${model}`;
    let m = modelData.get(key) || defaultMetrics();
    m.signalCount++;
    m.verbosityScore = bayesianUpdate(m.verbosityScore, tokenCount / Math.max(expectedTokens, 1), m.signalCount);
    if (!accepted || edited)
        m.errorRate = bayesianUpdate(m.errorRate, 1, m.signalCount);
    else
        m.errorRate = bayesianUpdate(m.errorRate, 0, m.signalCount);
    m.completionRate = bayesianUpdate(m.completionRate, accepted ? 1 : 0, m.signalCount);
    m.latencyP50 = bayesianUpdate(m.latencyP50, latencyMs / 1000, m.signalCount);
    m.userAcceptance = bayesianUpdate(m.userAcceptance, accepted ? 1 : 0, m.signalCount);
    m.qualityScore = (m.verbosityScore + (1 - m.errorRate) + m.completionRate + m.userAcceptance) / 4;
    modelData.set(key, m);
}
function bayesianUpdate(prior, observation, n) {
    const weight = Math.min(n / 50, 1);
    return prior * (1 - weight) + observation * weight;
}
function getConditioning(model, provider) {
    const key = `${provider}:${model}`;
    const m = modelData.get(key);
    if (!m || m.signalCount < 50)
        return '';
    const hints = [];
    if (m.verbosityScore > 0.7)
        hints.push('Be concise. Max 50 lines.');
    if (m.errorRate > 0.15)
        hints.push('Double-check syntax before output.');
    if (m.completionRate < 0.7)
        hints.push('Complete the full task.');
    if (m.latencyP50 > 3)
        hints.push('Prioritize speed.');
    return hints.length ? `[MODEL: ${hints.join(' ')}]` : '';
}
function getInsight(model, provider) {
    const key = `${provider}:${model}`;
    const m = modelData.get(key);
    if (!m)
        return null;
    return { model, provider, metrics: m, conditioning: getConditioning(model, provider), recommendation: m.qualityScore > 0.8 ? 'Recommended' : m.qualityScore > 0.6 ? 'Acceptable' : 'Avoid' };
}
function compareModels() {
    return Array.from(modelData.entries()).map(([key, m]) => {
        const lastColon = key.lastIndexOf(':');
        const provider = key.substring(0, lastColon);
        const model = key.substring(lastColon + 1);
        return { model, provider, metrics: m, conditioning: getConditioning(model, provider), recommendation: m.qualityScore > 0.8 ? 'Recommended' : m.qualityScore > 0.6 ? 'Acceptable' : 'Avoid' };
    }).sort((a, b) => b.metrics.qualityScore - a.metrics.qualityScore);
}
function exportModelData() {
    return Object.fromEntries(modelData);
}
function importModelData(data) {
    for (const [key, metrics] of Object.entries(data)) {
        modelData.set(key, metrics);
    }
}
//# sourceMappingURL=model-intelligence.js.map