import { resolve } from 'node:path';
import { buildTasksFromArtifacts } from '../eval-core/task-planner.js';
import { loadSamples } from '../inputs/load-samples.js';
import { resolveArtifacts } from '../inputs/skill-loader.js';
import { buildVariantConfig } from '../eval-core/execution-strategy.js';
import { loadMcpConfig, resolveMcpUrls } from '../inputs/mcp-resolver.js';
import { resolveUrls } from '../inputs/url-fetcher.js';
export async function prepareEvaluationRun({ samplesPath, skillDir, variantSpecs, artifacts, dryRun, mcpConfig, strictBaseline, variantAllowedSkills, }) {
    const { samples, requires, baseDir: samplesBaseDir, sourceFiles: samplesSourceFiles } = loadSamples(samplesPath);
    // Build expressions from specs (preserving order) and resolve to artifacts.
    const variantExpressions = variantSpecs.map((spec) => spec.expr);
    const resolvedArtifacts = artifacts || resolveArtifacts(resolve(skillDir), variantExpressions, { strictBaseline, variantAllowedSkills });
    // Attach experimentRole to each artifact by matching spec.name to artifact.name.
    const roleByName = {};
    for (const spec of variantSpecs)
        roleByName[spec.name] = spec.role;
    for (const artifact of resolvedArtifacts) {
        if (artifact.experimentRole)
            continue; // preserve if already set (e.g. batch workflow)
        const role = roleByName[artifact.name];
        if (role)
            artifact.experimentRole = role;
    }
    if (!dryRun) {
        const mcpServers = loadMcpConfig(mcpConfig);
        const mcpResolved = mcpServers ? await resolveMcpUrls(samples, mcpServers) : new Set();
        await resolveUrls(samples, mcpResolved);
    }
    if (resolvedArtifacts.length === 0) {
        throw new Error(`未发现任何 variant。请检查：\n`
            + `  1. skill 目录是否存在：${resolve(skillDir)}\n`
            + `  2. 目录下是否有 .md 文件或含 SKILL.md 的子目录\n`
            + `  3. 通过 --control / --treatment 显式声明 variant 与角色，或用 --config eval.yaml`);
    }
    const tasks = buildTasksFromArtifacts(samples, resolvedArtifacts);
    const variantNames = resolvedArtifacts.map((artifact) => artifact.name);
    return {
        samples,
        artifacts: resolvedArtifacts,
        tasks,
        variantNames,
        requires,
        samplesBaseDir,
        samplesSourceFiles,
    };
}
export function buildDryRunTaskReport({ model, judgeModels, executorName, samplesPath, skillDir, tasks, variantNames, }) {
    return {
        dryRun: true,
        model,
        judgeModels,
        variants: variantNames,
        executor: executorName,
        samplesPath,
        skillDir,
        totalTasks: tasks.length,
        tasks: tasks.map((task) => {
            const config = buildVariantConfig(task.artifact);
            return {
                sample_id: task.sample_id,
                variant: task.variant,
                artifactKind: task.artifact.kind,
                artifactSource: task.artifact.source,
                executionStrategy: config.executionStrategy,
                experimentType: config.experimentType,
                experimentRole: config.experimentRole,
                cwd: task.cwd,
                promptPreview: task.prompt.slice(0, 100),
                hasRubric: Boolean(task.rubric),
                hasAssertions: Boolean(task.assertions?.length),
                hasDimensions: Boolean(task.dimensions && Object.keys(task.dimensions).length),
                hasSystem: Boolean(task.artifactContent),
            };
        }),
    };
}
//# sourceMappingURL=evaluation-preparation.js.map