| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- #!/usr/bin/env node
- import { createHash } from "node:crypto";
- import { createWriteStream, mkdirSync, writeFileSync } from "node:fs";
- import { homedir } from "node:os";
- import { join } from "node:path";
- import { StringDecoder } from "node:string_decoder";
- import { spawn } from "node:child_process";
- function printUsageAndExit() {
- process.stderr.write(
- [
- "Usage: zcode-stdio-tap.mjs --workspace-key <key> [--log-dir <dir>] -- <command> [...args]",
- "",
- ].join("\n"),
- );
- process.exit(2);
- }
- function parseArgs(argv) {
- const separatorIndex = argv.indexOf("--");
- if (separatorIndex < 0) {
- printUsageAndExit();
- }
- const options = argv.slice(0, separatorIndex);
- const commandAndArgs = argv.slice(separatorIndex + 1);
- if (commandAndArgs.length === 0) {
- printUsageAndExit();
- }
- let workspaceKey = "";
- let logDir = process.env.ZCODE_STDIO_TAP_LOG_DIR?.trim() || "";
- for (let index = 0; index < options.length; index += 1) {
- const option = options[index];
- if (option === "--workspace-key") {
- workspaceKey = options[index + 1] ?? "";
- index += 1;
- continue;
- }
- if (option === "--log-dir") {
- logDir = options[index + 1] ?? "";
- index += 1;
- continue;
- }
- printUsageAndExit();
- }
- if (!workspaceKey.trim()) {
- printUsageAndExit();
- }
- return {
- command: commandAndArgs[0],
- args: commandAndArgs.slice(1),
- logDir: logDir || join(homedir(), ".zcode", "v2", "dev", "stdio-traffic"),
- workspaceKey,
- };
- }
- function workspaceHash(workspaceKey) {
- return createHash("sha256").update(workspaceKey).digest("hex").slice(0, 12);
- }
- function tryParseJsonLine(raw) {
- const trimmed = raw.trim();
- if (!trimmed) {
- return { message: null, parseError: null };
- }
- try {
- return { message: JSON.parse(trimmed), parseError: null };
- } catch (error) {
- return {
- message: null,
- parseError: error instanceof Error ? error.message : String(error),
- };
- }
- }
- function extractProtocolMeta(message) {
- if (!message || typeof message !== "object") {
- return {};
- }
- const params = "params" in message && typeof message.params === "object" ? message.params : null;
- const payload =
- params && "payload" in params && typeof params.payload === "object" ? params.payload : null;
- return {
- id: "id" in message ? message.id : undefined,
- method: "method" in message ? message.method : undefined,
- sessionId:
- params && "sessionId" in params
- ? params.sessionId
- : payload && "sessionId" in payload
- ? payload.sessionId
- : undefined,
- inputId:
- params && "inputId" in params
- ? params.inputId
- : payload && "inputId" in payload
- ? payload.inputId
- : undefined,
- };
- }
- function createRecorder({ childPid, stream, workspaceKey }) {
- return function recordLine(direction, raw, bytes) {
- const parsed =
- direction === "agent-stderr" ? { message: null, parseError: null } : tryParseJsonLine(raw);
- const meta = extractProtocolMeta(parsed.message);
- const record = {
- ts: new Date().toISOString(),
- workspaceKey,
- proxyPid: process.pid,
- pid: childPid,
- direction,
- bytes,
- raw,
- message: parsed.message,
- parseError: parsed.parseError,
- ...meta,
- };
- stream.write(`${JSON.stringify(record)}\n`);
- };
- }
- function createLineTap(direction, recordLine) {
- const decoder = new StringDecoder("utf8");
- let pending = "";
- return {
- push(chunk) {
- pending += decoder.write(chunk);
- const lines = pending.split("\n");
- pending = lines.pop() ?? "";
- for (const line of lines) {
- recordLine(
- direction,
- line.endsWith("\r") ? line.slice(0, -1) : line,
- Buffer.byteLength(line),
- );
- }
- },
- flush() {
- const tail = `${pending}${decoder.end()}`;
- pending = "";
- if (tail.length > 0) {
- recordLine(
- direction,
- tail.endsWith("\r") ? tail.slice(0, -1) : tail,
- Buffer.byteLength(tail),
- );
- }
- },
- };
- }
- const config = parseArgs(process.argv.slice(2));
- const dir = join(config.logDir, workspaceHash(config.workspaceKey));
- mkdirSync(dir, { recursive: true });
- const child = spawn(config.command, config.args, {
- cwd: process.cwd(),
- env: process.env,
- stdio: ["pipe", "pipe", "pipe"],
- windowsHide: true,
- });
- const logFilePath = join(
- dir,
- `${new Date().toISOString().replace(/[:.]/g, "-")}-proxy-${process.pid}-agent-${child.pid ?? "unknown"}.ndjson`,
- );
- writeFileSync(join(dir, "latest.txt"), `${logFilePath}\n`);
- const stream = createWriteStream(logFilePath, { flags: "a" });
- const recordLine = createRecorder({
- childPid: child.pid ?? null,
- stream,
- workspaceKey: config.workspaceKey,
- });
- const stdinTap = createLineTap("app-to-agent", recordLine);
- const stdoutTap = createLineTap("agent-to-app", recordLine);
- const stderrTap = createLineTap("agent-stderr", recordLine);
- child.on("error", (error) => {
- recordLine(
- "agent-stderr",
- `[tap] spawn failed: ${error.message}`,
- Buffer.byteLength(error.message),
- );
- process.stderr.write(`[zcode-stdio-tap] spawn failed: ${error.message}\n`);
- });
- child.stdout.on("data", (chunk) => {
- process.stdout.write(chunk);
- stdoutTap.push(chunk);
- });
- child.stderr.on("data", (chunk) => {
- process.stderr.write(chunk);
- stderrTap.push(chunk);
- });
- process.stdin.on("data", (chunk) => {
- stdinTap.push(chunk);
- if (!child.stdin.write(chunk)) {
- process.stdin.pause();
- }
- });
- child.stdin.on("drain", () => {
- process.stdin.resume();
- });
- process.stdin.on("end", () => {
- stdinTap.flush();
- child.stdin.end();
- });
- function forwardSignal(signal) {
- if (!child.killed) {
- child.kill(signal);
- }
- }
- process.on("SIGINT", () => forwardSignal("SIGINT"));
- process.on("SIGTERM", () => forwardSignal("SIGTERM"));
- child.on("close", (code, signal) => {
- stdinTap.flush();
- stdoutTap.flush();
- stderrTap.flush();
- stream.end(() => {
- process.exit(code ?? (signal ? 1 : 0));
- });
- });
|