Skip to content

An open-source, faster Devin alternative

TypeScript

cli: model connect review fixes (work 082)

Igor Zalutski via Computer98ad769ea2b1
diff --git a/cli/src/commands.ts b/cli/src/commands.ts
index 5d442233..4f669633 100644
--- a/cli/src/commands.ts
+++ b/cli/src/commands.ts
@@ -325,9 +325,10 @@ async function connectComputerSubscription(
client: OpenComputerClient,
globals: GlobalOptions,
): Promise<void> {
- // Progress and the provider's own UI go to stderr under --json so stdout
- // carries only the JSON answer.
- const display = globals.json ? process.stderr : process.stdout;
+ // Progress and the provider's own UI go to stderr under --json (stdout
+ // carries only the JSON answer) or when stdout is not the terminal.
+ const display =
+ globals.json || !process.stdout.isTTY ? process.stderr : process.stdout;
let body: ComputerConnectRequest;
if (provider === "codex") {
display.write(
@@ -358,6 +359,12 @@ async function connectComputerSubscription(
}
async function claudeTokenFromStdin(): Promise<string> {
+ if (process.stdin.isTTY)
+ throw new CLIError(
+ "value_stdin_required",
+ "--token-stdin needs the token piped in.",
+ 'Use `printf %s "$TOKEN" | npx -y @opencomputer/cli model connect claude --code <code> --token-stdin`, or drop --token-stdin to run `claude setup-token`.',
+ );
const token = (await readStdinValue(true)).trim();
if (!isClaudeSetupToken(token))
throw new CLIError(
@@ -393,6 +400,8 @@ async function claudeTokenFromSetupToken(
"Run the command again; the connect code still works until it is used or expires.",
);
// Windows (no PTY recorder), or output this CLI could not read: ask.
+ if (result.pty)
+ display.write("\nCould not read the token from `claude setup-token`'s output.\n");
const pasted = (
await readTemplateSecretValue(
"Paste the token `claude setup-token` printed (input hidden): ",
diff --git a/cli/src/computer-connect.ts b/cli/src/computer-connect.ts
index db930527..27376047 100644
--- a/cli/src/computer-connect.ts
+++ b/cli/src/computer-connect.ts
@@ -1,4 +1,4 @@
-import { spawn } from "node:child_process";
+import { spawn, spawnSync } from "node:child_process";
import { accessSync, constants } from "node:fs";
import { delimiter, join } from "node:path";
import { StringDecoder } from "node:string_decoder";
@@ -33,7 +33,8 @@ export function takeModelConnectArgs(
const tokenStdin = take.flag("--token-stdin");
if (tokenStdin && provider !== "claude")
throw new Error("--token-stdin is only for `model connect claude`");
- if (code !== undefined && !/^occ_[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/.test(code))
+ // The server is the authority on codes; this only catches a mangled paste.
+ if (code !== undefined && !/^occ_[\x21-\x7e]+$/.test(code))
throw new CLIError(
"connect_code_invalid",
"That is not a connect code (it starts with occ_).",
@@ -231,10 +232,14 @@ export function runClaudeSetupToken(
// its PTY closes and the TUI behind it is hung up.
let interrupted: NodeJS.Signals | undefined;
let escalation: NodeJS.Timeout | undefined;
+ let forceKilled = false;
const forward = (signal: NodeJS.Signals): void => {
interrupted ??= signal;
child.kill("SIGTERM");
- escalation ??= setTimeout(() => child.kill("SIGKILL"), 2_000);
+ escalation ??= setTimeout(() => {
+ forceKilled = true;
+ child.kill("SIGKILL");
+ }, 2_000);
escalation.unref();
};
const signals: NodeJS.Signals[] = ["SIGINT", "SIGTERM", "SIGHUP"];
@@ -260,6 +265,9 @@ export function runClaudeSetupToken(
? claudeTokenFromTerminalOutput(captured, process.stdout.rows)
: undefined;
captured = "";
+ // A SIGKILLed `script` cannot restore the terminal mode it changed.
+ if (forceKilled && spec.pty && process.stdin.isTTY)
+ spawnSync("stty", ["sane"], { stdio: "inherit" });
if (interrupted) {
reject(
new CLIError(