// Force strict mode and setup for ESM
"use strict";
const __papertOriginalEmitWarning = process.emitWarning.bind(process);
process.emitWarning = function patchedEmitWarning(warning, ...args) {
  if (!process.env.PAPERT_SHOW_NODE_DEPRECATIONS) {
    const warningCode =
      (warning && typeof warning === 'object' && 'code' in warning && warning.code) ||
      (args.length > 1 && typeof args[1] === 'string' && args[1]) ||
      (args.length > 0 &&
        args[0] &&
        typeof args[0] === 'object' &&
        'code' in args[0] &&
        args[0].code);
    if (warningCode === 'DEP0040' || warningCode === 'DEP0169') {
      return;
    }
  }
  return __papertOriginalEmitWarning(warning, ...args);
};
import {
  __name,
  init_esbuild_shims
} from "./chunk-NCFEMX7E.js";

// packages/cli/src/commands/server.ts
init_esbuild_shims();
import { spawn } from "node:child_process";
import * as fs from "node:fs";
import * as path from "node:path";
import { fileURLToPath } from "node:url";
import { createRequire } from "node:module";
function resolveA2aServerEntrypoint() {
  const candidates = [];
  const commandFile = fileURLToPath(import.meta.url);
  const commandDir = path.dirname(commandFile);
  const papertEntrypoint = process.argv[1];
  candidates.push(
    path.resolve(process.cwd(), "packages/a2a-server/dist/src/http/server.js")
  );
  candidates.push(
    path.resolve(
      commandDir,
      "../../../../packages/a2a-server/dist/src/http/server.js"
    )
  );
  candidates.push(
    path.resolve(
      commandDir,
      "../../../../../packages/a2a-server/dist/src/http/server.js"
    )
  );
  if (papertEntrypoint) {
    candidates.push(
      path.resolve(
        path.dirname(papertEntrypoint),
        "../packages/a2a-server/dist/src/http/server.js"
      )
    );
    candidates.push(
      path.resolve(
        path.dirname(papertEntrypoint),
        "../../packages/a2a-server/dist/src/http/server.js"
      )
    );
  }
  try {
    const require2 = createRequire(import.meta.url);
    const resolved = require2.resolve(
      "@papert-code/papert-code-a2a-server/dist/src/http/server.js"
    );
    candidates.push(resolved);
  } catch {
  }
  for (const candidate of candidates) {
    if (fs.existsSync(candidate)) {
      return candidate;
    }
  }
  return void 0;
}
__name(resolveA2aServerEntrypoint, "resolveA2aServerEntrypoint");
var serverCommand = {
  command: "server",
  describe: "Run Papert Code daemon (remote driving server).",
  builder: /* @__PURE__ */ __name((yargs) => yargs.option("port", {
    type: "number",
    description: "Port to bind the daemon to.",
    default: 41242
  }).option("token", {
    type: "string",
    description: "Server token required to create a remote session. If omitted, a random token is generated and printed."
  }).option("session-ttl-ms", {
    type: "number",
    description: "Session lease TTL in milliseconds.",
    default: 6e4
  }).option("host", {
    type: "string",
    description: "Host to bind to.",
    default: "0.0.0.0"
  }).option("docs", {
    type: "boolean",
    description: "Enable OpenAPI docs UI at /docs and spec at /openapi.json.",
    default: false
  }).version(false), "builder"),
  handler: /* @__PURE__ */ __name(async (argv) => {
    const token = typeof argv["token"] === "string" && argv["token"].trim().length > 0 ? argv["token"].trim() : void 0;
    const env = {
      ...process.env,
      PAPERT_REMOTE_ENABLED: "1",
      PAPERT_REMOTE_SERVER_TOKEN: token ?? "",
      PAPERT_REMOTE_SESSION_TTL_MS: String(argv["session-ttl-ms"] ?? 6e4),
      PAPERT_REMOTE_DOCS_ENABLED: argv["docs"] ? "1" : "0",
      CODER_AGENT_PORT: String(argv["port"] ?? 41242),
      CODER_AGENT_HOST: String(argv["host"] ?? "0.0.0.0")
    };
    const url = `http://127.0.0.1:${Number(argv["port"] ?? 41242)}`;
    console.error(
      `[papert] starting a2a server
  url:   ${url}
  port:  ${env.CODER_AGENT_PORT}
  token: ${env.PAPERT_REMOTE_SERVER_TOKEN}
  cmd:   papert-a2a-server`
    );
    const localServerEntrypoint = resolveA2aServerEntrypoint();
    const spawnCommand = localServerEntrypoint ? {
      command: process.execPath,
      args: [localServerEntrypoint],
      display: `${process.execPath} ${localServerEntrypoint}`
    } : {
      command: "papert-a2a-server",
      args: [],
      display: "papert-a2a-server"
    };
    console.error(`[papert] spawning: ${spawnCommand.display}`);
    let child;
    try {
      child = spawn(spawnCommand.command, spawnCommand.args, {
        stdio: "inherit",
        env
      });
    } catch (err) {
      console.error(`[papert] failed to spawn daemon:`, err);
      process.exit(1);
      return;
    }
    child.on("error", (err) => {
      console.error(`[papert] daemon spawn error:`, err);
      process.exit(1);
    });
    child.on("close", (code) => {
      if (code && code !== 0) {
        console.error(`[papert] papert-a2a-server exited with code ${code}`);
      }
      process.exit(code ?? 0);
    });
  }, "handler")
};

export {
  serverCommand
};
/**
 * @license
 * Copyright 2026 Papert-code
 * SPDX-License-Identifier: Apache-2.0
 */
