Files
plugins/packages/agent-console/daemon/src/config.ts
T
qingnian108 a431798fe1 agent-console 1.0.0: a page plugin that drives the local Agent CLIs (#535)
Adds Agent Console (@magpie-community/page-agent-console 1.0.0), a community page plugin that drives the Agent CLIs on this computer (Claude Code, Codex, ACP agents) through a local daemon on 127.0.0.1, with its source in packages/agent-console and a registry entry. Claude Code is the user's own `claude` CLI driven over stream-json (daemon/src/providers/claude/cli.ts); no Anthropic SDK is installed, bundled or in the branch's history. Cold install ~45 MB (node-pty ~26 MB), stated in the README and registry summary in en and zh.

By qingnian108 (#535).

What was run at 8e8f910 (mote, macOS, throwaway HOME, no real accounts):
- packages/agent-console: `npm ci --ignore-scripts --workspaces=false`; `node scripts/verify-build.mjs`: all five bundles byte-identical (daemon/dist/main.js 08c95aa3…, app/app.js 9a6911f3…); `vitest run`: 45 files, 254 passed, 17 skipped; `npm run check` clean.
- Real Claude Code 2.1.316 (npm @anthropic-ai/claude-code, sandbox HOME/CLAUDE_CONFIG_DIR, local fake Messages API): `AC_REAL_CLAUDE=… vitest run --config vitest.real.config.ts test/real/claude-cli.real.test.ts`: 4 of 4 (initialize lists 51 slash commands, streaming turn, Bash permission approved and denied, interrupt, --resume with set_model).
- Probe: the driver's exact argv with `--permission-mode bypassPermissions --allow-dangerously-skip-permissions`: the tool runs with no can_use_tool request.
- Repo root: `HOME=$(mktemp -d) bun test`: 4006 tests, 0 fail; `bun scripts/check.mjs` exit 0.
- Install as magpie does: `npm pack --ignore-scripts` (552 KB) then `bun add --ignore-scripts`: 45 MB node_modules, no @anthropic-ai/*, index.mjs imports.
- Read every changed line since 41c7a94: argv values limited to SAFE_ARG tokens (no cmd.exe metacharacters reach the shim path), no new network hosts, no install scripts, no eval; no commit in main..8e8f910 contains claude-agent-sdk.

Not tried: magpie's desktop window loading the page; a Windows .cmd/.ps1 claude shim end to end.
2026-10-31 20:11:22 +08:00

66 lines
2.7 KiB
TypeScript

// Arguments and environment: port, dataDir, token, version, idleExitMinutes
import { randomBytes } from "node:crypto";
import { readFileSync } from "node:fs";
import { homedir } from "node:os";
import { join, resolve, dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { daemonBuildStamp } from "./util/buildStamp.js";
export type Config = {
port: number;
dataDir: string;
token: string;
version: string;
idleExitMinutes: number;
buildStamp: string;
parentPid?: number;
};
function pkgVersion(): string {
try {
const here = dirname(fileURLToPath(import.meta.url));
// dist/main.js → ../../package.json; src/main.ts also ends up in dist via esbuild
const pkg = JSON.parse(readFileSync(join(here, "..", "..", "package.json"), "utf8"));
return String(pkg.version || "0.0.0");
} catch {
return "0.0.0";
}
}
// --data <dir> sets the data root (equivalent to setting XDG_CONFIG_HOME and then joining magpie/agent-console?
// EXEC §5.2: --data <dir> (data root, equivalent to setting XDG_CONFIG_HOME; default $XDG_CONFIG_HOME
// or ~/.config, then joined with magpie/agent-console). i.e. the --data value has XDG_CONFIG_HOME semantics.
export function parseConfig(argv: string[]): Config {
let port: number | null = null;
let data: string | null = null;
let token: string | null = null;
let parentPid: number | undefined;
for (let i = 0; i < argv.length; i++) {
const a = argv[i];
if (a === "--port" && argv[i + 1]) port = Number(argv[++i]);
else if (a.startsWith("--port=")) port = Number(a.slice(7));
else if (a === "--data" && argv[i + 1]) data = argv[++i];
else if (a.startsWith("--data=")) data = a.slice(7);
else if (a === "--token" && argv[i + 1]) token = argv[++i];
else if (a.startsWith("--token=")) token = a.slice(8);
else if (a === "--parent-pid" && argv[i + 1]) parentPid = Number(argv[++i]);
else if (a.startsWith("--parent-pid=")) parentPid = Number(a.slice(13));
}
if (port == null || !Number.isInteger(port) || port < 0 || port > 65535) {
const envPort = Number(process.env.AGENT_CONSOLE_PORT || "");
port = Number.isInteger(envPort) && envPort >= 0 && envPort <= 65535 ? envPort : 3488;
}
const xdg = data ?? process.env.XDG_CONFIG_HOME ?? join(homedir(), ".config");
const dataDir = resolve(join(xdg, "magpie", "agent-console"));
const idleEnv = Number(process.env.AGENT_CONSOLE_IDLE_EXIT_MIN || "");
return {
port,
dataDir,
token: token ?? process.env.AGENT_CONSOLE_TOKEN ?? randomBytes(16).toString("hex"),
version: pkgVersion(),
idleExitMinutes: Number.isFinite(idleEnv) && idleEnv > 0 ? idleEnv : 60,
buildStamp: daemonBuildStamp(),
parentPid: Number.isInteger(parentPid) ? parentPid : undefined,
};
}