import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { getLogger } from "../../util/logger.js";
import { OperationPassthrough } from "../../util/operation-queue.js";
import { createDefaultVirtualFileSystem } from "../app-interface/filesystem/virtual-file-system.js";
import { YamlPathValidator } from "../app-interface/filesystem/yaml-path-validator.js";
import { AppShell } from "../app-interface/shell.js";
import { LLMClient } from "../llm/client.js";
import { ContextManagerV2 } from "../llm/context-v2/manager.js";
import { LocalContextStorageV2 } from "../llm/context-v2/storage/local.js";
import { createLLMProvider } from "../llm/provider.js";
import { PlaywrightMcpServerManager } from "../mcp/playwright-server.js";
import { JudgeService } from "./judge-service.js";
import { CsvJudgeStorage } from "./storage/csv-storage.js";
export async function runJudgeServiceEvaluation(options) {
    const logger = getLogger();
    const repoRoot = findWorkspaceRoot(process.cwd());
    const jwtProvider = () => options.jwt ??
        process.env.JUDGE_JWT ??
        process.env.SUPERBLOCKS_JWT ??
        process.env.SUPERBLOCKS_TOKEN ??
        undefined;
    const { judgeService } = await createJudgeService(repoRoot, logger, jwtProvider);
    try {
        const evaluation = await judgeService.evaluateApplication(options.promptId, options.prompt, options.appId ?? options.promptId, {
            appUrl: options.appUrl,
            complexity: options.complexity ?? "medium",
            storageStatePath: options.storageStatePath,
            jwt: options.jwt,
            llmConfig: options.llmConfig,
            storageStateData: options.storageStateData,
            sessionStorageData: options.sessionStorageData,
            extraOrigins: options.extraOrigins,
            config: options.configOverrides,
        });
        return evaluation;
    }
    finally {
        await judgeService.dispose();
    }
}
async function createJudgeService(repoRoot, logger = getLogger(), getJwt) {
    const fsQueue = new OperationPassthrough();
    const appShell = new AppShell({
        appRootDirPath: repoRoot,
        fsOperationQueue: fsQueue,
        virtualFileSystem: createDefaultVirtualFileSystem({
            useSystemSkills: false,
        }),
        pathValidator: new YamlPathValidator(),
    });
    const contextManagerV2 = new ContextManagerV2({
        storage: new LocalContextStorageV2(path.join(repoRoot, ".superblocks/context")),
        rootDir: repoRoot,
    });
    const llmClient = new LLMClient({
        appShell,
        contextManagerV2,
    });
    const llmProviderSettings = buildLlmProviderSettings();
    const llmProvider = createLLMProvider(llmProviderSettings, getJwt);
    const storageLocation = resolveJudgeStorageLocation(repoRoot);
    await fs.promises.mkdir(storageLocation.dir, { recursive: true });
    const judgeStorage = new CsvJudgeStorage(storageLocation.dir, storageLocation.filename);
    const mcpServerManager = new PlaywrightMcpServerManager(logger);
    const judgeService = new JudgeService({
        llmClient,
        llmProvider,
        storage: judgeStorage,
        mcpServerManager,
        logger,
    });
    return { judgeService };
}
function buildLlmProviderSettings() {
    const baseUrl = process.env.SUPERBLOCKS_BASE_URL || "http://localhost:3000";
    return {
        anthropic: {
            baseURL: `${baseUrl}/api/v1/ai/proxy/anthropic`,
            apiKey: process.env.ANTHROPIC_API_KEY ?? "placeholder",
        },
        clark: {
            baseURL: `${baseUrl}/api/v1/ai/inference`,
            upstreamProvider: process.env.SUPERBLOCKS_UPSTREAM_PROVIDER === "vertex"
                ? "vertex"
                : "bedrock",
        },
    };
}
function resolveJudgeStorageLocation(repoRoot) {
    const configuredPath = process.env.JUDGE_STORAGE_PATH;
    if (configuredPath) {
        const expanded = configuredPath.startsWith("~/")
            ? path.join(os.homedir(), configuredPath.slice(2))
            : configuredPath;
        if (expanded.endsWith(".csv")) {
            return {
                dir: path.dirname(expanded),
                filename: path.basename(expanded),
            };
        }
        return { dir: expanded, filename: "evaluations.csv" };
    }
    const defaultDir = path.join(repoRoot, ".superblocks/judge-evaluations");
    return {
        dir: defaultDir,
        filename: "evaluations.csv",
    };
}
function findWorkspaceRoot(startDir) {
    let current = startDir;
    let last = "";
    while (current !== last) {
        const packageJsonPath = path.join(current, "package.json");
        if (fs.existsSync(packageJsonPath)) {
            try {
                const pkg = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
                if (pkg.workspaces) {
                    return current;
                }
            }
            catch {
                // ignore parse errors and continue
            }
        }
        last = current;
        current = path.dirname(current);
    }
    return startDir;
}
//# sourceMappingURL=judge-eval-service-runner.js.map