| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- #!/usr/bin/env node
- import { spawn } from "node:child_process";
- import { randomBytes } from "node:crypto";
- import { access, readFile } from "node:fs/promises";
- import { createServer } from "node:net";
- import { networkInterfaces } from "node:os";
- import { dirname, join, resolve } from "node:path";
- import { fileURLToPath, pathToFileURL } from "node:url";
- const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
- const { version } = JSON.parse(await readFile(join(root, "package.json"), "utf8"));
- const serverEntry = join(root, "server", "entry-http.js");
- const webRoot = join(root, "web");
- const agentEntry = join(root, "agent", "zcode.cjs");
- function usage() {
- return `Usage:
- zcode --web [--host <host>] [--port <port>] [--workspace <path>] [--open|--no-open] [--token <token>|--no-token]
- zcode --version
- `;
- }
- function readArgValue(argv, arg, index) {
- if (arg.includes("=")) {
- return { nextIndex: index, value: arg.slice(arg.indexOf("=") + 1) };
- }
- const value = argv[index + 1];
- if (!value || value.startsWith("--")) {
- throw new Error(`Missing value for ${arg}`);
- }
- return { nextIndex: index + 1, value };
- }
- function parseArgs(argv) {
- const options = {
- command: "serve",
- host: "127.0.0.1",
- open: undefined,
- port: undefined,
- token: undefined,
- tokenEnabled: undefined,
- workspace: process.cwd(),
- };
- for (let index = 0; index < argv.length; index += 1) {
- const arg = argv[index];
- if (arg === "--help" || arg === "-h") {
- options.command = "help";
- continue;
- }
- if (arg === "--version" || arg === "-v") {
- options.command = "version";
- continue;
- }
- if (arg === "--host" || arg.startsWith("--host=")) {
- const parsed = readArgValue(argv, arg, index);
- options.host = parsed.value;
- index = parsed.nextIndex;
- continue;
- }
- if (arg === "--port" || arg.startsWith("--port=")) {
- const parsed = readArgValue(argv, arg, index);
- options.port = Number(parsed.value);
- if (!Number.isInteger(options.port) || options.port < 0 || options.port > 65535) {
- throw new Error("--port must be an integer between 0 and 65535");
- }
- index = parsed.nextIndex;
- continue;
- }
- if (arg === "--workspace" || arg.startsWith("--workspace=")) {
- const parsed = readArgValue(argv, arg, index);
- options.workspace = resolve(parsed.value);
- index = parsed.nextIndex;
- continue;
- }
- if (arg === "--open") {
- options.open = true;
- continue;
- }
- if (arg === "--no-open") {
- options.open = false;
- continue;
- }
- if (arg === "--token" || arg.startsWith("--token=")) {
- const parsed = readArgValue(argv, arg, index);
- options.token = parsed.value;
- options.tokenEnabled = true;
- index = parsed.nextIndex;
- continue;
- }
- if (arg === "--no-token") {
- options.tokenEnabled = false;
- continue;
- }
- throw new Error(`Unknown option "${arg}".\n${usage()}`);
- }
- return options;
- }
- function isLocalHost(host) {
- return host === "127.0.0.1" || host === "localhost" || host === "::1";
- }
- function shouldProtectHost(host) {
- return !isLocalHost(host);
- }
- function createToken() {
- return randomBytes(24).toString("base64url");
- }
- function pickPort(host) {
- return new Promise((resolvePort, reject) => {
- const server = createServer();
- server.once("error", reject);
- server.listen(0, host, () => {
- const address = server.address();
- const port = typeof address === "object" && address ? address.port : 0;
- server.close((error) => {
- if (error) {
- reject(error);
- return;
- }
- resolvePort(port);
- });
- });
- });
- }
- function formatUrl(host, port, token) {
- const displayHost = host === "0.0.0.0" || host === "::" ? "127.0.0.1" : host;
- const base = `http://${displayHost}:${port}/`;
- return token ? `${base}?token=${encodeURIComponent(token)}` : base;
- }
- function networkUrls(port, token) {
- const urls = [];
- for (const entries of Object.values(networkInterfaces())) {
- for (const entry of entries ?? []) {
- if (entry.internal || entry.family !== "IPv4") {
- continue;
- }
- const base = `http://${entry.address}:${port}/`;
- urls.push(token ? `${base}?token=${encodeURIComponent(token)}` : base);
- }
- }
- return urls;
- }
- function openBrowser(url) {
- const command =
- process.platform === "darwin" ? "open" : process.platform === "win32" ? "cmd" : "xdg-open";
- const args = process.platform === "win32" ? ["/c", "start", "", url] : [url];
- const child = spawn(command, args, {
- detached: true,
- stdio: "ignore",
- });
- child.unref();
- }
- async function assertRuntimeFiles() {
- for (const file of [serverEntry, agentEntry, webRoot]) {
- await access(file).catch((cause) => {
- throw new Error(`Missing runtime file: ${file}`, { cause });
- });
- }
- }
- async function serve(options) {
- await assertRuntimeFiles();
- const port = options.port && options.port > 0 ? options.port : await pickPort(options.host);
- const protect = options.tokenEnabled ?? shouldProtectHost(options.host);
- const token = protect ? (options.token ?? createToken()) : "";
- const open = options.open ?? isLocalHost(options.host);
- const localUrl = formatUrl(options.host, port, token);
- const child = spawn(process.execPath, [serverEntry], {
- cwd: options.workspace,
- env: {
- ...process.env,
- PORT: String(port),
- ZCODE_AGENT_SERVER_ARGS_JSON: JSON.stringify([agentEntry, "app-server", "--stdio"]),
- ZCODE_AGENT_SERVER_COMMAND: process.execPath,
- ZCODE_SERVER_HOST: options.host,
- ZCODE_SERVER_WORKSPACE: options.workspace,
- ZCODE_WEB_STATIC_ROOT: webRoot,
- // 显式关闭 token 时必须清空继承值,否则 --no-token 仍会开启后端鉴权。
- ZCODE_SERVER_AUTH_TOKEN: token,
- },
- stdio: ["ignore", "pipe", "pipe"],
- });
- let shuttingDown = false;
- child.on("error", (error) => {
- console.error(`Unable to start Web server: ${error.message}`);
- process.exit(1);
- });
- child.stdout.on("data", (chunk) => process.stdout.write(chunk));
- child.stderr.on("data", (chunk) => process.stderr.write(chunk));
- child.on("exit", (code, signal) => {
- if (shuttingDown) {
- process.exit(0);
- }
- if (signal) {
- process.exit(1);
- }
- process.exit(code ?? 0);
- });
- console.log("");
- console.log("ZCode Web is running");
- console.log(`Local: ${localUrl}`);
- if (options.host === "0.0.0.0" || options.host === "::") {
- for (const url of networkUrls(port, token)) {
- console.log(`Network: ${url}`);
- }
- }
- console.log("Press Ctrl+C to stop.");
- console.log("");
- if (open) {
- setTimeout(() => openBrowser(localUrl), 500);
- }
- const shutdown = () => {
- if (shuttingDown) {
- return;
- }
- shuttingDown = true;
- child.kill("SIGTERM");
- setTimeout(() => process.exit(0), 1500).unref();
- };
- process.on("SIGINT", shutdown);
- process.on("SIGTERM", shutdown);
- if (process.stdin.isTTY) {
- process.stdin.setRawMode?.(true);
- process.stdin.resume();
- process.stdin.on("data", (chunk) => {
- if (chunk.includes(3)) {
- shutdown();
- }
- });
- process.stdin.on("error", () => {
- if (!shuttingDown) {
- shutdown();
- }
- });
- }
- }
- try {
- const argv = process.argv.slice(2);
- if (argv.length === 1 && ["--version", "-v"].includes(argv[0])) {
- console.log(version);
- } else if (argv[0] === "--web") {
- const options = parseArgs(argv.slice(1));
- if (options.command === "help") console.log(usage());
- else if (options.command === "version") console.log(version);
- else await serve(options);
- } else {
- if (argv.length === 1 && ["--help", "-h"].includes(argv[0])) {
- console.log("Web mode: zcode --web [options] (zcode --web --help for details)\n");
- }
- // CLI 自启动子进程依赖 argv[1];统一指向真正的 Agent 入口,保留 TTY 与所有原始参数。
- process.argv[1] = agentEntry;
- await import(pathToFileURL(agentEntry).href);
- }
- } catch (error) {
- console.error(error instanceof Error ? error.message : String(error));
- process.exit(1);
- }
|