GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — opencode-codex-computer-use/src/host.ts at main · malhashemi/opencode-codex-computer-use

Expand file tree

/

host.ts

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
244 lines (229 loc) · 9.6 KB
/

host.ts

Copy path

File metadata and controls

244 lines (229 loc) · 9.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import { spawn, type ChildProcessWithoutNullStreams } from "node:child_process"
import { accessSync, constants, existsSync } from "node:fs"
import { homedir } from "node:os"
import { delimiter, join } from "node:path"
/** Facts about the machine that runs Codex Computer Use. */
export interface HostInfo {
/** Node-style platform: "darwin", "linux", "win32"; other systems: lowercased `uname -s`. */
platform: string
/** Node-style architecture: "arm64" (arm64/aarch64), "x64" (x86_64/amd64); otherwise raw `uname -m`. */
arch: string
/** Home directory on the host. */
home: string
/** $CODEX_HOME on the host, or <home>/.codex. */
codexHome: string
/** `sw_vers -productVersion`, only on darwin (undefined if unavailable). */
osVersion?: string
}
export interface RunOptions {
/** Written to stdin, which is then closed. Without it stdin is closed immediately. */
input?: Uint8Array | string
/** Default 15_000. On timeout the process is killed and the promise rejects. */
timeoutMs?: number
/** Maximum stdout bytes, default 16 MiB; exceeding it kills the process and rejects. */
maxBuffer?: number
}
/** Where Codex Computer Use runs: this machine, or another one reached over SSH. */
export interface Host {
/** True when commands run on another machine over SSH. */
readonly remote: boolean
/** For messages: "this machine", or "<destination> over SSH". */
readonly label: string
/** Program and arguments that run `command args` on the host. Local: unchanged. */
command(command: string, args: readonly string[]): { file: string; args: string[] }
/** Cached after the first success; failures (e.g. SSH unreachable) are not cached. */
info(): Promise<HostInfo>
/** First candidate that is an executable file on the host (undefined/empty entries skipped); otherwise `name`
* looked up on the host's PATH; undefined when nothing is found. */
findExecutable(candidates: readonly (string | undefined)[], name: string): Promise<string | undefined>
/** Whether `path` exists on the host. Any failure reads as false. */
exists(path: string): Promise<boolean>
}
/**
* Added to every SSH connection: no prompts (a password prompt would hang the plugin), a bounded connection time,
* and keep-alives so a dead connection ends the app-server instead of leaving calls waiting.
*/
export const SSH_OPTIONS: readonly string[] = [
"-T",
"-o",
"BatchMode=yes",
"-o",
"ConnectTimeout=15",
"-o",
"ServerAliveInterval=15",
"-o",
"ServerAliveCountMax=4",
]
const DEFAULT_MAX_BUFFER = 16 * 1024 * 1024
/** This machine. */
export function localHost(): Host {
const host: Host = {
remote: false,
label: "this machine",
command: (command, args) => ({ file: command, args: [...args] }),
info: cacheSuccess(async () => {
const home = homedir()
return {
platform: process.platform,
arch: process.arch,
home,
codexHome: process.env.CODEX_HOME || join(home, ".codex"),
osVersion:
process.platform === "darwin"
? await runOnHost(host, "/usr/bin/sw_vers", ["-productVersion"]).then(
(out) => out.trim() || undefined,
() => undefined,
)
: undefined,
}
}),
findExecutable: async (candidates, name) => {
const onPath = (process.env.PATH ?? process.env.Path ?? "")
.split(delimiter)
.filter(Boolean)
.map((directory) => join(directory, process.platform === "win32" ? `${name}.exe` : name))
return [...candidates, ...onPath].find((candidate): candidate is string => !!candidate && isExecutable(candidate))
},
exists: async (path) => existsSync(path),
}
return host
}
// The remote side needs a POSIX `sh` (macOS or Linux). Values reach these scripts as positional arguments, never
// inside the script text.
const INFO_SCRIPT =
'uname -s; uname -m; printf "%s\\n" "$HOME" "${CODEX_HOME:-$HOME/.codex}"; sw_vers -productVersion 2>/dev/null || echo'
const FIND_SCRIPT =
'name=$1; shift; for p in "$@"; do if [ -f "$p" ] && [ -x "$p" ]; then printf "%s\\n" "$p"; exit 0; fi; done; ' +
'command -v "$name" || true'
/** Another machine, reached with the system `ssh` and the user's SSH configuration for `destination`. */
export function sshHost(destination: string): Host {
// Neither script can fail on its own, so a failure means the machine could not be reached; users see these first,
// from a tool call or the doctor, so they name the machine.
const unreachable = (error: unknown) =>
new Error(`Could not reach ${host.label}: ${error instanceof Error ? error.message : String(error)}`)
const host: Host = {
remote: true,
label: `${destination} over SSH`,
// ssh joins the remote command into one string for the remote login shell; quoting each word makes the round
// trip exact.
command: (command, args) => ({
file: "ssh",
args: [...SSH_OPTIONS, "--", destination, [command, ...args].map(shellQuote).join(" ")],
}),
info: cacheSuccess(async () => {
// Usually the first contact with the machine: wait longer than ssh's ConnectTimeout so ssh's own reason gets
// through.
const output = await runOnHost(host, "sh", ["-c", INFO_SCRIPT], { timeoutMs: 30_000 }).catch((error: unknown) => {
throw unreachable(error)
})
const [system = "", machine = "", home = "", codexHome = "", version = ""] = output.split("\n")
if (!system.trim() || !home) throw new Error(`unexpected system information: ${JSON.stringify(output)}`)
const platform = system.trim().toLowerCase()
return {
platform,
arch: normalizeArch(machine.trim()),
home,
codexHome,
osVersion: platform === "darwin" ? version.trim() || undefined : undefined,
}
}),
findExecutable: async (candidates, name) => {
const paths = candidates.filter((candidate): candidate is string => !!candidate)
const output = await runOnHost(host, "sh", ["-c", FIND_SCRIPT, "sh", name, ...paths]).catch((error: unknown) => {
throw unreachable(error)
})
return output.trim() || undefined
},
exists: (path) =>
runOnHost(host, "test", ["-e", path]).then(
() => true,
() => false,
),
}
return host
}
/** Starts `command args` on the host with piped stdio (env: process.env). */
export function spawnOnHost(host: Host, command: string, args: readonly string[]): ChildProcessWithoutNullStreams {
const target = host.command(command, args)
return spawn(target.file, target.args, { stdio: ["pipe", "pipe", "pipe"], env: process.env })
}
/** Runs `command args` on the host to completion; resolves with stdout (utf8). Rejects on spawn error, non-zero
* exit (message: trimmed stderr, else "<command> exited with code N"), timeout, or maxBuffer. */
export function runOnHost(
host: Host,
command: string,
args: readonly string[],
options: RunOptions = {},
): Promise<string> {
const timeoutMs = options.timeoutMs ?? 15_000
const maxBuffer = options.maxBuffer ?? DEFAULT_MAX_BUFFER
return new Promise((resolve, reject) => {
const child = spawnOnHost(host, command, args)
const stdout: Buffer[] = []
const stderr: Buffer[] = []
let size = 0
let settled = false
const settle = (error: Error | undefined, output = "") => {
if (settled) return
settled = true
clearTimeout(timer)
if (error) reject(error)
else resolve(output)
}
// Settles at once instead of waiting for "close": descendants of the process (or an ssh ProxyCommand) can keep
// its output open long after it is killed, and our ends of the pipes must not wait for them.
const abandon = (error: Error) => {
settle(error)
child.kill("SIGKILL")
child.stdin.destroy()
child.stdout.destroy()
child.stderr.destroy()
}
const timer = setTimeout(() => abandon(new Error(`${command} timed out after ${timeoutMs} ms`)), timeoutMs)
child.stdout.on("data", (chunk: Buffer) => {
if (settled) return
size += chunk.length
if (size > maxBuffer) return abandon(new Error(`${command} wrote more than ${maxBuffer} bytes`))
stdout.push(chunk)
})
child.stderr.on("data", (chunk: Buffer) => stderr.push(chunk))
child.once("error", (error) => settle(error))
child.once("close", (code, signal) => {
if (code === 0) return settle(undefined, Buffer.concat(stdout).toString("utf8"))
const message = Buffer.concat(stderr).toString("utf8").trim()
const status = code === null ? `was stopped by ${signal}` : `exited with code ${code}`
settle(new Error(message || `${command} ${status}`))
})
// The process may exit without reading its input.
child.stdin.on("error", () => {})
child.stdin.end(options.input)
})
}
/** POSIX single-quoting of one word: 'it'\''s'. The empty string becomes ''. */
export function shellQuote(value: string): string {
return `'${value.replaceAll("'", "'\\''")}'`
}
/** Caches a successful lookup; a failed one (for example while SSH is unreachable) is retried on the next call. */
function cacheSuccess<T>(load: () => Promise<T>): () => Promise<T> {
let cached: Promise<T> | undefined
return () => {
cached ??= load().catch((error: unknown) => {
cached = undefined
throw error
})
return cached
}
}
function normalizeArch(machine: string): string {
if (machine === "arm64" || machine === "aarch64") return "arm64"
if (machine === "x86_64" || machine === "amd64") return "x64"
return machine
}
function isExecutable(path: string): boolean {
try {
accessSync(path, process.platform === "win32" ? constants.F_OK : constants.X_OK)
return true
} catch {
return false
}
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.