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/bridge.ts at main · malhashemi/opencode-codex-computer-use

Expand file tree

/

bridge.ts

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
440 lines (403 loc) · 16.4 KB
/

bridge.ts

Copy path

File metadata and controls

440 lines (403 loc) · 16.4 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
import { AppServerClient, AppServerError } from "./app-server"
import { BUNDLED_CODEX_PATHS, CODEX_PATH_ENV, resolveCodexPath } from "./codex-path"
import type { Host } from "./host"
/** Codex's MCP server that hosts the Computer Use JavaScript runtime (`cua`). */
export const CUA_SERVER = "cua_repl"
export interface BridgeOptions {
/** The machine that runs Codex. */
host: Host
/** Path to `codex` on the host. */
codexPath?: string
idleShutdownMs: number
callTimeoutMs: number
/** The OpenCode location directory; Codex's working folder when Codex runs on this machine. */
cwd: string
version: string
log: (message: string) => void
/** JavaScript run before the model's code on every call (see `surfaceGuard`). */
codePrefix?: string
/** Receives every app-server message, for the optional debug log. */
onTraffic?: (direction: "send" | "receive", message: unknown) => void
}
export interface McpContentBlock {
type: string
text?: string
data?: string
mimeType?: string
[key: string]: unknown
}
export interface RunResult {
content: McpContentBlock[]
isError: boolean
meta?: Record<string, unknown>
notes: string[]
}
export class SetupError extends Error {
constructor(message: string) {
super(message)
this.name = "SetupError"
}
}
export const SETUP_HELP =
"Codex Computer Use needs the ChatGPT (or Codex) desktop app installed and signed in, with Computer Use enabled " +
"(on macOS: 14.4+ on Apple Silicon, with Accessibility and Screen Recording granted). " +
"Run /computer-use-doctor or see the opencode-codex-computer-use README."
interface SessionState {
readonly generation: number
readonly threadID: Promise<string>
resolvedThreadID?: string
/** Computer Use ran since the last `turn_ended`. */
dirty: boolean
/** Generated on the first call of an OpenCode turn and cleared when the turn ends. */
turnID?: string
}
export class ComputerUseBridge {
readonly host: Host
private client?: AppServerClient
private starting?: Promise<AppServerClient>
private generation = 0
/** Numbers every startup attempt, so a process from a failed attempt is never taken for a later one. */
private startups = 0
private readonly sessions = new Map<string, SessionState>()
private readonly approvalNotes = new Map<string, string[]>()
private readonly restartedSessions = new Set<string>()
private active = 0
private idleTimer?: ReturnType<typeof setTimeout>
private disposed = false
constructor(private readonly options: BridgeOptions) {
this.host = options.host
}
async run(
sessionID: string,
code: string,
input: { signal?: AbortSignal; callID?: string } = {},
): Promise<RunResult> {
return this.busy(async () => {
const client = await this.ensureClient()
const threadID = await this.thread(sessionID, client)
const state = this.sessions.get(sessionID)
if (state) {
state.dirty = true
state.turnID ??= crypto.randomUUID()
}
const notes: string[] = []
if (this.restartedSessions.delete(sessionID)) {
notes.push(
"The Computer Use runtime was restarted since the previous call, so JavaScript variables from earlier calls " +
'(such as `app` or `tab`) no longer exist. Bind them again, for example `let app = await cua.getApp("Notes")`.',
)
}
const onAbort = () => void this.endTurn(sessionID, "Interrupt")
input.signal?.addEventListener("abort", onAbort, { once: true })
try {
const result = await client.request(
"mcpServer/tool/call",
{
threadId: threadID,
server: CUA_SERVER,
tool: "js",
arguments: { code: this.options.codePrefix ? `${this.options.codePrefix}\n${code}` : code },
_meta: turnMetadata(threadID, state?.turnID, input.callID),
},
{ signal: input.signal, timeoutMs: this.options.callTimeoutMs },
)
notes.push(...this.takeApprovalNotes(threadID))
return {
content: Array.isArray(result?.content) ? result.content : [],
isError: result?.isError === true,
meta: result?._meta,
notes,
}
} catch (error) {
this.takeApprovalNotes(threadID).forEach((note) => notes.push(note))
throw await this.explain(error, client, threadID, notes)
} finally {
input.signal?.removeEventListener("abort", onAbort)
}
})
}
/** Resets the session's JavaScript runtime (`js_reset`). Does nothing if Computer Use has not run yet. */
async reset(sessionID: string): Promise<boolean> {
const state = this.sessions.get(sessionID)
if (!state || state.generation !== this.generation || !this.client || this.client.closed) return false
return this.busy(async () => {
const client = this.client!
const threadID = await state.threadID
await client.request(
"mcpServer/tool/call",
{ threadId: threadID, server: CUA_SERVER, tool: "js_reset", arguments: {} },
{ timeoutMs: 30_000 },
)
return true
})
}
/** Tells Computer Use the turn ended, as Codex does from its Stop/Interrupt hooks. */
async endTurn(sessionID: string, kind: "Stop" | "Interrupt"): Promise<void> {
const state = this.sessions.get(sessionID)
const client = this.client
if (!state?.dirty || !state.resolvedThreadID || !client || client.closed || state.generation !== this.generation)
return
const turnID = state.turnID ?? "opencode"
state.dirty = false
state.turnID = undefined
try {
await client.request(
"mcpServer/tool/call",
{
threadId: state.resolvedThreadID,
server: CUA_SERVER,
tool: "turn_ended",
arguments: {
hook_event_name: kind,
session_id: state.resolvedThreadID,
turn_id: turnID,
},
_meta: turnMetadata(state.resolvedThreadID, turnID),
},
{ timeoutMs: 10_000 },
)
} catch (error) {
this.options.log(`turn_ended failed for session ${sessionID}: ${describe(error)}`)
}
}
/** Ends the session's Codex thread; Codex then stops its Computer Use processes. */
async closeSession(sessionID: string): Promise<void> {
const state = this.sessions.get(sessionID)
if (!state) return
await this.endTurn(sessionID, "Stop")
this.sessions.delete(sessionID)
this.restartedSessions.delete(sessionID)
const client = this.client
if (!state.resolvedThreadID || !client || client.closed || state.generation !== this.generation) return
this.approvalNotes.delete(state.resolvedThreadID)
await client
.request("thread/unsubscribe", { threadId: state.resolvedThreadID }, { timeoutMs: 10_000 })
.catch((error) => this.options.log(`thread/unsubscribe failed: ${describe(error)}`))
}
/** Names of the MCP servers Codex has loaded for the session's thread, for diagnostics. */
async serverNames(sessionID: string): Promise<string[]> {
return this.busy(async () => {
const client = await this.ensureClient()
const threadID = await this.thread(sessionID, client)
const response = await client.request(
"mcpServerStatus/list",
{ threadId: threadID, detail: "toolsAndAuthOnly" },
{ timeoutMs: 30_000 },
)
return Array.isArray(response?.data)
? response.data.map((server: { name?: unknown }) => String(server?.name ?? ""))
: []
})
}
/** The effective Codex `approval_policy` (a granular policy is reported as "granular"), if Codex reports it. */
async approvalPolicy(): Promise<string | undefined> {
return this.busy(async () => {
const client = await this.ensureClient()
const cwd = await this.workingDirectory()
const response = await client.request("config/read", { cwd }, { timeoutMs: 15_000 })
const policy = response?.config?.approval_policy
if (typeof policy === "string") return policy
return policy && typeof policy === "object" ? "granular" : undefined
})
}
/** Where `codex` is on the host, or undefined when it is not installed there. */
codexPath(): Promise<string | undefined> {
return resolveCodexPath(this.host, this.options.codexPath)
}
async dispose(): Promise<void> {
this.disposed = true
clearTimeout(this.idleTimer)
// A startup in flight sees `disposed` and closes what it started; wait for it so no app-server outlives the plugin.
await this.starting?.catch(() => {})
await this.stop()
}
private async stop(): Promise<void> {
const client = this.client
if (!client) return
await Promise.all([...this.sessions.keys()].map((sessionID) => this.endTurn(sessionID, "Stop")))
await client.close()
}
private async busy<T>(work: () => Promise<T>): Promise<T> {
this.active++
clearTimeout(this.idleTimer)
try {
return await work()
} finally {
this.active--
this.scheduleIdleShutdown()
}
}
private scheduleIdleShutdown(): void {
clearTimeout(this.idleTimer)
if (this.active > 0 || !this.client || this.options.idleShutdownMs <= 0) return
this.idleTimer = setTimeout(() => {
if (this.active > 0) return
this.options.log(`idle for ${this.options.idleShutdownMs} ms; stopping codex app-server`)
void this.stop()
}, this.options.idleShutdownMs)
this.idleTimer.unref?.()
}
private ensureClient(): Promise<AppServerClient> {
if (this.disposed) return Promise.reject(unloadedError())
if (this.client && !this.client.closed) return Promise.resolve(this.client)
if (this.starting) return this.starting
this.starting = (async () => {
const codexPath = await this.codexPath()
// Finding codex can take an SSH round trip, long enough for the plugin to be unloaded meanwhile.
if (this.disposed) throw unloadedError()
const where = this.host.remote ? ` on ${this.host.label}` : ""
if (!codexPath) {
throw new SetupError(
`Could not find the \`codex\` executable${where} (looked at the \`codexPath\` option, ` +
`$${CODEX_PATH_ENV}, ${[...BUNDLED_CODEX_PATHS, "PATH"].join(", ")}). ${SETUP_HELP}`,
)
}
const generation = ++this.startups
const client = await AppServerClient.start({
host: this.host,
codexPath,
clientVersion: this.options.version,
handlers: {
onServerRequest: (method, params) => this.answerServerRequest(method, params),
onExit: (code, signal) => this.handleExit(generation, code, signal),
onTraffic: this.options.onTraffic,
},
})
if (this.disposed) {
await client.close()
throw unloadedError()
}
this.generation = generation
this.client = client
this.options.log(`started codex app-server (pid ${client.pid}) from ${codexPath}${where}`)
return client
})().finally(() => {
this.starting = undefined
})
return this.starting
}
private handleExit(generation: number, code: number | null, signal: NodeJS.Signals | null): void {
this.options.log(`codex app-server exited (code ${code}, signal ${signal})`)
if (generation !== this.generation) return
this.client = undefined
clearTimeout(this.idleTimer)
for (const [sessionID, state] of this.sessions) {
if (state.generation !== generation) continue
this.sessions.delete(sessionID)
this.restartedSessions.add(sessionID)
}
this.approvalNotes.clear()
}
private thread(sessionID: string, client: AppServerClient): Promise<string> {
const existing = this.sessions.get(sessionID)
if (existing && existing.generation === this.generation) return existing.threadID
const threadID = this.workingDirectory()
// No approvalPolicy: app access follows the user's Codex approval_policy.
.then((cwd) => client.request("thread/start", { ephemeral: true, cwd }, { timeoutMs: 60_000 }))
.then((response: any) => {
const id = response?.thread?.id
if (typeof id !== "string") throw new Error("thread/start returned no thread id")
return id
})
const state: SessionState = { generation: this.generation, threadID, dirty: false }
this.sessions.set(sessionID, state)
void (async () => {
try {
state.resolvedThreadID = await threadID
} catch {
if (this.sessions.get(sessionID) === state) this.sessions.delete(sessionID)
}
})()
return threadID
}
/** Codex's working folder must exist on the machine that runs it; its MCP servers fail to start otherwise. */
private async workingDirectory(): Promise<string> {
return this.host.remote ? (await this.host.info()).home : this.options.cwd
}
private async answerServerRequest(method: string, params: any): Promise<unknown> {
if (method !== "mcpServer/elicitation/request") {
throw new Error(`${method} is not supported by opencode-codex-computer-use`)
}
// OpenCode's plugin API cannot raise a permission question yet, so a prompt Codex forwards
// (because approval_policy is not "never") is declined, and the model is told how the user can allow the app.
const message = typeof params?.message === "string" ? params.message : "Computer Use requested approval"
this.addApprovalNote(params?.threadId, declinedNote(message, appName(params)))
this.options.log(`declined: ${message}`)
return { action: "decline", content: null }
}
private addApprovalNote(threadID: unknown, note: string): void {
if (typeof threadID !== "string") return
const notes = this.approvalNotes.get(threadID) ?? []
notes.push(note)
this.approvalNotes.set(threadID, notes)
}
private takeApprovalNotes(threadID: string): string[] {
const notes = this.approvalNotes.get(threadID) ?? []
this.approvalNotes.delete(threadID)
return notes
}
/** Turns low-level failures into actionable errors, checking whether Codex exposes `cua_repl` at all. */
private async explain(error: unknown, client: AppServerClient, threadID: string, notes: string[]): Promise<Error> {
const base = error instanceof Error ? error : new Error(String(error))
if (base.name === "AbortError") return base
if (base instanceof AppServerError && !client.closed) {
let available: boolean | undefined
try {
const response = await client.request(
"mcpServerStatus/list",
{ threadId: threadID, detail: "toolsAndAuthOnly" },
{ timeoutMs: 15_000 },
)
if (Array.isArray(response?.data)) {
available = response.data.some((server: { name?: unknown }) => server?.name === CUA_SERVER)
}
} catch {
available = undefined
}
if (available === false) {
return new SetupError(
`Codex has no \`${CUA_SERVER}\` MCP server, so Computer Use is not enabled. ${SETUP_HELP}`,
)
}
}
if (notes.length === 0) return base
const wrapped = new Error([base.message, ...notes].join("\n"))
wrapped.name = base.name
return wrapped
}
}
function unloadedError(): Error {
return new Error("opencode-codex-computer-use has been unloaded")
}
function appName(params: any): string | undefined {
const display = params?._meta?.tool_params_display
const entry = Array.isArray(display) ? display.find((item: { name?: string }) => item?.name === "app") : undefined
return typeof entry?.value === "string" ? entry.value : undefined
}
export function declinedNote(message: string, app?: string): string {
const target = app ? `"${app}"` : "this app"
return (
`Computer Use asked "${message}" and it could not be approved from OpenCode. Tell the user that Computer Use ` +
`needs permission to use ${target}. They can approve it permanently in the ChatGPT or Codex app (use the app ` +
'once through Computer Use there and choose to always allow it), or set approval_policy = "never" in ' +
"~/.codex/config.toml so Codex approves apps itself."
)
}
/**
* Per-call metadata Codex attaches to MCP tool calls during a turn. The browser surface of `cua_repl` requires
* `session_id` and `turn_id`; `call_id` links approval prompts to the originating tool call.
*/
export function turnMetadata(threadID: string, turnID: string | undefined, callID?: string) {
if (!turnID) return undefined
return {
"x-codex-turn-metadata": {
session_id: threadID,
thread_id: threadID,
turn_id: turnID,
...(callID ? { call_id: callID } : {}),
},
}
}
function describe(error: unknown): string {
return error instanceof Error ? error.message : String(error)
}
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.