Connect my computer: works from Docker and on Windows
- Docker passes BROKE_HOSTED through (setting it in .env did nothing before) - the helper finds claude.exe / claude.cmd on Windows (PATH uses ';' there), runs a .cmd through cmd.exe with the question on stdin only, and keeps the Windows variables Claude needs to find its login; opencode on Windows only as .exe - the page gives Windows a PowerShell command (curl.exe, file then node) - Copy works on a plain http LAN address Tested on Windows end to end with a stand-in claude.cmd: pairing, a question with quotes, & and %, and every lockdown flag arriving intact. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ea53718e5d
commit
0232766260
4 files changed
+54
-12
No files matched your search
@@ -4,7 +4,9 @@
|
||||
const { spawn, execFile } = require("node:child_process");
|
||||
const { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } = require("node:fs");
|
||||
const { homedir } = require("node:os");
|
||||
const { join } = require("node:path");
|
||||
const { delimiter, join } = require("node:path");
|
||||
|
||||
const WIN = process.platform === "win32";
|
||||
|
||||
const site = (process.argv[2] || "").replace(/\/$/, "");
|
||||
const code = process.argv[3];
|
||||
@@ -19,22 +21,37 @@ mkdirSync(WORK, { recursive: true });
|
||||
|
||||
// Only what the AI apps need to find their login. Drops API keys, which would switch them to API billing.
|
||||
const env = { HOME: homedir(), PATH: process.env.PATH, LANG: process.env.LANG || "en_US.UTF-8" };
|
||||
for (const k of ["USER", "LOGNAME", "TMPDIR", "SHELL"]) if (process.env[k]) env[k] = process.env[k];
|
||||
const findBin = (name) => {
|
||||
const dirs = [...(process.env.PATH || "").split(":"), join(homedir(), ".local/bin"), "/opt/homebrew/bin", "/usr/local/bin", join(homedir(), ".npm-global/bin")];
|
||||
return dirs.map((d) => join(d, name)).find((p) => existsSync(p)) || null;
|
||||
// Windows: the AI apps find their login and Windows itself through these.
|
||||
const WIN_VARS = ["USERPROFILE", "APPDATA", "LOCALAPPDATA", "SystemRoot", "SystemDrive", "windir", "TEMP", "TMP", "PATHEXT", "ComSpec", "ProgramData", "ProgramFiles", "HOMEDRIVE", "HOMEPATH"];
|
||||
for (const k of ["USER", "LOGNAME", "TMPDIR", "SHELL", ...(WIN ? WIN_VARS : [])]) if (process.env[k]) env[k] = process.env[k];
|
||||
// Windows: claude.exe (native install) or claude.cmd (npm). cmdOk false: only a real .exe, never through cmd.exe.
|
||||
const findBin = (name, cmdOk = true) => {
|
||||
const dirs = [...(process.env.PATH || "").split(delimiter).filter(Boolean), join(homedir(), ".local", "bin"),
|
||||
...(WIN ? [join(process.env.APPDATA || "", "npm")] : ["/opt/homebrew/bin", "/usr/local/bin"]), join(homedir(), ".npm-global", "bin")];
|
||||
const names = WIN ? [`${name}.exe`, ...(cmdOk ? [`${name}.cmd`] : [])] : [name];
|
||||
for (const d of dirs) for (const n of names) if (existsSync(join(d, n))) return join(d, n);
|
||||
return null;
|
||||
};
|
||||
// A .cmd only runs through cmd.exe. Safe here because every argument is fixed or checked by safeModel (no quotes, no
|
||||
// %), and the question itself goes in on stdin, never on the command line.
|
||||
const viaCmd = (bin) => WIN && /\.cmd$/i.test(bin);
|
||||
const q = (a) => `"${a.replace(/"/g, '\\"')}"`;
|
||||
const launch = (bin, args, opts) => viaCmd(bin) ? spawn(q(bin), args.map(q), { ...opts, shell: true, windowsHide: true }) : spawn(bin, args, { ...opts, windowsHide: true });
|
||||
const OPENCODE_ENV = {
|
||||
...env, XDG_CONFIG_HOME: join(HOME, "opencode-config"), OPENCODE_DISABLE_CLAUDE_CODE: "1", OPENCODE_DISABLE_EXTERNAL_SKILLS: "1", OPENCODE_DISABLE_PROJECT_CONFIG: "1",
|
||||
OPENCODE_DISABLE_AUTOUPDATE: "1", OPENCODE_DISABLE_DEFAULT_PLUGINS: "1", OPENCODE_CONFIG_CONTENT: JSON.stringify({ share: "disabled", autoupdate: false, mcp: {}, permission: { "*": "deny" } }),
|
||||
};
|
||||
const output = (bin, args, e) => new Promise((res) => execFile(bin, args, { env: e, timeout: 8000 }, (err, out) => res(err ? "" : String(out))));
|
||||
const output = (bin, args, e) => new Promise((res) => {
|
||||
const done = (err, out) => res(err ? "" : String(out));
|
||||
if (viaCmd(bin)) execFile(q(bin), args.map(q), { env: e, timeout: 8000, shell: true, windowsHide: true }, done);
|
||||
else execFile(bin, args, { env: e, timeout: 8000, windowsHide: true }, done);
|
||||
});
|
||||
|
||||
async function findClis() {
|
||||
const clis = [];
|
||||
const claude = findBin("claude");
|
||||
if (claude && /"loggedIn":\s*true/.test(await output(claude, ["auth", "status"], env))) clis.push({ id: "claude" });
|
||||
const opencode = findBin("opencode");
|
||||
const opencode = findBin("opencode", false); // its question goes on the command line, so never through cmd.exe
|
||||
const models = opencode ? (await output(opencode, ["models"], OPENCODE_ENV)).split("\n").map((s) => s.trim()).filter(Boolean) : [];
|
||||
if (models.length) clis.push({ id: "opencode", models });
|
||||
return clis;
|
||||
@@ -46,7 +63,7 @@ const safeModel = (m) => (typeof m === "string" && /^[\w][\w.:/-]*$/.test(m) ? m
|
||||
function runClaude(job, onText, stop) {
|
||||
const model = safeModel(job.model);
|
||||
const args = ["-p", "--output-format", "text", "--no-session-persistence", "--restricted", "--tools", "", "--strict-mcp-config", "--mcp-config", '{"mcpServers":{}}', "--permission-mode", "dontAsk", ...(model ? ["--model", model] : [])];
|
||||
const child = spawn(findBin("claude"), args, { cwd: WORK, env, stdio: ["pipe", "pipe", "ignore"] });
|
||||
const child = launch(findBin("claude"), args, { cwd: WORK, env, stdio: ["pipe", "pipe", "ignore"] });
|
||||
stop(() => child.kill());
|
||||
child.stdin.end(job.prompt);
|
||||
let wrote = false;
|
||||
@@ -57,7 +74,7 @@ function runClaude(job, onText, stop) {
|
||||
function runOpencode(job, onText, stop) {
|
||||
const model = safeModel(job.model);
|
||||
if (!model) return Promise.resolve(false);
|
||||
const child = spawn(findBin("opencode"), ["run", job.prompt, "--format", "json", "--print-logs", "--log-level", "ERROR", "-m", model], { cwd: WORK, env: OPENCODE_ENV, stdio: ["ignore", "pipe", "pipe"] });
|
||||
const child = spawn(findBin("opencode", false), ["run", job.prompt, "--format", "json", "--print-logs", "--log-level", "ERROR", "-m", model], { cwd: WORK, env: OPENCODE_ENV, stdio: ["ignore", "pipe", "pipe"] });
|
||||
stop(() => child.kill());
|
||||
let wrote = false, buf = "";
|
||||
// A rate limit or sign-in error: opencode would wait forever, so stop it and say why.
|
||||
|
||||
Reference in new issue
Block a user