#!/usr/bin/env node
import {
  buildOpenApi,
  handleVersion,
  runRpcBatch,
  runRpcOnce,
  startServer
} from "./chunk-6REHLN5J.js";
import "./chunk-IHDHUN2X.js";
import "./chunk-PC4UYEBM.js";
import "./chunk-3BFEG2F6.js";
import "./chunk-PZ5AY32C.js";

// src/cli.ts
import { writeFileSync } from "fs";
function parseArgs(argv) {
  const [command, ...rest] = argv;
  const positional = [];
  const flags = {};
  for (let i = 0; i < rest.length; i++) {
    const tok = rest[i];
    if (tok.startsWith("--")) {
      const key = tok.slice(2);
      const next = rest[i + 1];
      if (next != null && !next.startsWith("--")) {
        flags[key] = next;
        i++;
      } else {
        flags[key] = "true";
      }
    } else {
      positional.push(tok);
    }
  }
  return { command: command ?? "help", positional, flags };
}
var HELP = `agent-eval \u2014 wire-protocol entry point.

Commands:
  serve [--port 5005] [--host 127.0.0.1]
        Start the HTTP server. POST /v1/judge, GET /v1/rubrics, GET /v1/version, GET /openapi.json.
  rpc <method>
        Read one JSON object from stdin (the params for <method>), write one
        JSON object to stdout. Method \u2208 {judge, listRubrics, version}.
  rpc-batch <method>
        Like 'rpc' but JSONL in / JSONL out.
  openapi [--out openapi.json]
        Write the OpenAPI 3.1 spec.
  version
        Print server + wire-protocol version JSON.

Without arguments, prints this help.`;
async function main() {
  const { command, positional, flags } = parseArgs(process.argv.slice(2));
  switch (command) {
    case "serve": {
      const port = Number(flags.port ?? 5005);
      const host = flags.host ?? "127.0.0.1";
      const server = startServer({ port, host });
      const shutdown = (sig) => {
        console.log(`[agent-eval] received ${sig}, shutting down`);
        server.close(() => process.exit(0));
        setTimeout(() => process.exit(1), 5e3).unref();
      };
      process.on("SIGINT", () => shutdown("SIGINT"));
      process.on("SIGTERM", () => shutdown("SIGTERM"));
      await new Promise(() => {
      });
      return 0;
    }
    case "rpc": {
      const [method] = positional;
      return await runRpcOnce(method);
    }
    case "rpc-batch": {
      const [method] = positional;
      return await runRpcBatch(method);
    }
    case "openapi": {
      const out = flags.out ?? "openapi.json";
      const spec = buildOpenApi(handleVersion().version);
      writeFileSync(out, `${JSON.stringify(spec, null, 2)}
`, "utf-8");
      console.log(`[agent-eval] wrote OpenAPI 3.1 spec to ${out}`);
      return 0;
    }
    case "version": {
      process.stdout.write(`${JSON.stringify(handleVersion(), null, 2)}
`);
      return 0;
    }
    case "help":
    case "--help":
    case "-h":
    case "":
      process.stdout.write(`${HELP}
`);
      return 0;
    default:
      process.stderr.write(`unknown command: ${command}
${HELP}
`);
      return 1;
  }
}
main().then((code) => process.exit(code)).catch((err) => {
  console.error("[agent-eval] cli error:", err);
  process.exit(1);
});
//# sourceMappingURL=cli.js.map