/**
 * Core compilation logic for knowledge probe tasks.
 *
 * Compiles a knowledge probe task definition into Promptfoo configuration.
 * This is intentionally minimal — knowledge probes map almost 1:1 to
 * basic Promptfoo test cases. The AILF value-add is type-safe authoring,
 * cross-model comparison, and score normalization.
 */
import { mapKnowledgeProbeAssertion } from "./assertions.js";
import { validateKnowledgeProbeTask } from "./validation.js";
// ---------------------------------------------------------------------------
// Public compilation entry point
// ---------------------------------------------------------------------------
/**
 * Compile a knowledge probe task definition into Promptfoo configuration.
 *
 * This is intentionally minimal — knowledge probes map almost 1:1 to
 * basic Promptfoo test cases. The AILF value-add is type-safe authoring,
 * cross-model comparison, and score normalization.
 */
export function compileKnowledgeProbeTask(task, options) {
    const warnings = [];
    // Validate
    const validationErrors = validateKnowledgeProbeTask(task);
    for (const err of validationErrors) {
        warnings.push(`Knowledge probe "${task.id}": ${err.field} — ${err.message}`);
    }
    // Build providers from model list (or use a default placeholder)
    const providers = buildProviders(options);
    // Build prompts — knowledge probes use a single no-docs prompt
    const prompts = buildPrompts(task);
    // Build test cases
    const tests = buildTestCases(task, options, warnings);
    // Build metadata
    const metadata = {
        mode: "knowledge-probe",
        probeStrategy: task.probeStrategy ?? "breadth-first",
        noDocContext: true,
        retrievalMetrics: false,
    };
    return { providers, tests, prompts, metadata, warnings };
}
// ---------------------------------------------------------------------------
// Provider assembly
// ---------------------------------------------------------------------------
function buildProviders(options) {
    if (options?.models && options.models.length > 0) {
        return options.models.map((model) => ({
            id: model.id,
            label: model.label,
            config: model.config,
        }));
    }
    // No models specified — return empty (caller should provide models)
    return [];
}
// ---------------------------------------------------------------------------
// Prompt assembly
// ---------------------------------------------------------------------------
function buildPrompts(task) {
    // Knowledge probes use a single prompt — no with-docs/without-docs split.
    // The prompt IS the probe question.
    const promptText = task.prompt?.text ??
        task.prompt?.vars?.task ??
        task.description ??
        `Knowledge probe: ${task.title}`;
    const systemMessage = task.prompt?.systemMessage;
    return [
        {
            id: "knowledge-probe",
            label: `Probe: ${task.title}`,
            raw: systemMessage
                ? `[system]\n${systemMessage}\n\n[user]\n${String(promptText)}`
                : String(promptText),
        },
    ];
}
// ---------------------------------------------------------------------------
// Test case assembly
// ---------------------------------------------------------------------------
function buildTestCases(task, options, warnings) {
    // Build assertions
    const assertions = [];
    if (task.assertions) {
        for (const assertion of task.assertions) {
            const raw = assertion;
            const mapped = mapKnowledgeProbeAssertion(raw, options, warnings);
            if (mapped)
                assertions.push(mapped);
        }
    }
    // Build vars — intentionally no docs
    const vars = {
        task: task.prompt?.vars?.task ??
            task.description ??
            `Knowledge probe: ${task.title}`,
        ...(task.prompt?.vars ?? {}),
        // Metadata for scoring pipeline
        __mode: "knowledge-probe",
        __probeStrategy: task.probeStrategy ?? "breadth-first",
    };
    // Explicitly do NOT include docs
    // This is the defining characteristic of knowledge-probe mode
    delete vars.docs;
    return [
        {
            description: `${task.id} — ${task.title}`,
            vars,
            ...(assertions.length > 0 ? { assert: assertions } : {}),
        },
    ];
}
