fix: stabilize code agent trace and gateway flow

This commit is contained in:
Codex
2026-05-24 17:06:22 +00:00
parent b71a3cc89e
commit 9b08caa1a2
22 changed files with 795 additions and 226 deletions
+54 -7
View File
@@ -2,16 +2,19 @@
import http from "node:http";
import https from "node:https";
import { randomUUID } from "node:crypto";
import { readFileSync } from "node:fs";
const DEFAULT_API_BASE_URL = "http://127.0.0.1:6667";
const DEFAULT_PROJECT_ID = "prj_mvp_topology";
const DEFAULT_GATEWAY_SESSION_ID = "gws_DESKTOP-1MHOD9I";
const DEFAULT_RESOURCE_ID = "res_windows_host";
const DEFAULT_CAPABILITY_ID = "cap_windows_cmd_exec";
const POWERSHELL_PROLOGUE = "[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false); $OutputEncoding = [Console]::OutputEncoding;";
async function main() {
const args = parseArgs(process.argv.slice(2));
if (args.help || !args.command) {
const command = await resolveCommand(args);
if (args.help || !command) {
printHelp();
process.exit(args.help ? 0 : 2);
}
@@ -36,7 +39,7 @@ async function main() {
resourceId: args.resourceId || DEFAULT_RESOURCE_ID,
capabilityId: args.capabilityId || DEFAULT_CAPABILITY_ID,
input: {
command: args.command,
command,
cwd: args.cwd || undefined,
timeoutMs
}
@@ -58,19 +61,22 @@ async function main() {
function parseArgs(argv) {
const args = {};
const booleanFlags = new Set(["json", "help", "h", "powershellStdin", "psStdin"]);
for (let index = 0; index < argv.length; index += 1) {
const item = argv[index];
if (item === "--help" || item === "-h") {
if ((item === "--help") || (item === "-h")) {
args.help = true;
} else if (item === "--json") {
args.json = true;
} else if (item.startsWith("--") && item.includes("=")) {
const [key, ...rest] = item.slice(2).split("=");
args[toCamel(key)] = rest.join("=");
} else if (item.startsWith("--")) {
const key = toCamel(item.slice(2));
args[key] = argv[index + 1] ?? "";
index += 1;
if (booleanFlags.has(key)) {
args[key] = true;
} else {
args[key] = argv[index + 1] ?? "";
index += 1;
}
}
}
return args;
@@ -83,10 +89,16 @@ function toCamel(value) {
function printHelp() {
process.stdout.write([
"Usage: node tools/hwlab-gateway-shell.mjs --command \"cmd /c ...\" [--json]",
" node tools/hwlab-gateway-shell.mjs --powershell-command \"Get-ChildItem ...\" [--json]",
" node tools/hwlab-gateway-shell.mjs --powershell-stdin [--json] < script.ps1",
"",
"Options:",
" --api-base-url URL Cloud API/edge base URL, default http://127.0.0.1:6667",
" --command CMD Windows cmd command to execute through the registered PC gateway",
" --powershell-command PS1 Encode PS1 as UTF-16LE and run powershell.exe -EncodedCommand",
" --powershell-file PATH Read a local PS1 file, encode it, and run through the gateway",
" --powershell-stdin Read PS1 from stdin, encode it, and run through the gateway",
" --powershell-exe NAME Default powershell.exe",
" --timeout-ms N Short dispatch timeout in milliseconds",
" --project-id ID Default prj_mvp_topology",
" --gateway-session-id ID Default gws_DESKTOP-1MHOD9I",
@@ -96,6 +108,41 @@ function printHelp() {
].join("\n") + "\n");
}
async function resolveCommand(args) {
if (args.powershellStdin || args.psStdin) {
return powershellEncodedCommand(await readStdin(), args);
}
if (args.powershellFile || args.psFile) {
return powershellEncodedCommand(readFileSync(args.powershellFile || args.psFile, "utf8"), args);
}
if (args.powershellCommand || args.psCommand || args.powershell) {
return powershellEncodedCommand(args.powershellCommand || args.psCommand || args.powershell, args);
}
return args.command || "";
}
function powershellEncodedCommand(script, args = {}) {
const source = String(script || "").trim();
if (!source) return "";
const shell = args.powershellExe || "powershell.exe";
const prologue = args.noPowershellPrologue ? "" : `${POWERSHELL_PROLOGUE}\n`;
const encoded = Buffer.from(`${prologue}${source}`, "utf16le").toString("base64");
return `${shell} -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass -EncodedCommand ${encoded}`;
}
function readStdin() {
return new Promise((resolve, reject) => {
let text = "";
process.stdin.setEncoding("utf8");
process.stdin.on("data", (chunk) => {
text += chunk;
});
process.stdin.on("end", () => resolve(text));
process.stdin.on("error", reject);
process.stdin.resume();
});
}
function requestJson(url, { method, headers, body, timeoutMs }) {
return new Promise((resolve, reject) => {
const target = new URL(url);