Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"dev:web": "bun --cwd packages/app dev",
"dev:storybook": "bun --cwd packages/storybook storybook",
"typecheck": "bun turbo typecheck",
"postinstall": "bun run --cwd packages/opencode fix-node-pty",
"prepare": "husky",
"random": "echo 'Random script'",
"hello": "echo 'Hello World!'",
Expand Down Expand Up @@ -98,6 +99,7 @@
},
"trustedDependencies": [
"esbuild",
"node-pty",
"protobufjs",
"tree-sitter",
"tree-sitter-bash",
Expand Down
7 changes: 7 additions & 0 deletions packages/opencode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"typecheck": "tsgo --noEmit",
"test": "bun test --timeout 30000 registry",
"build": "bun run script/build.ts",
"fix-node-pty": "bun run script/fix-node-pty.ts",
"dev": "bun run --conditions=browser ./src/index.ts",
"random": "echo 'Random script updated at $(date)' && echo 'Change queued successfully' && echo 'Another change made' && echo 'Yet another change' && echo 'One more change' && echo 'Final change' && echo 'Another final change' && echo 'Yet another final change'",
"clean": "echo 'Cleaning up...' && rm -rf node_modules dist",
Expand All @@ -30,6 +31,11 @@
"bun": "./src/storage/db.bun.ts",
"node": "./src/storage/db.node.ts",
"default": "./src/storage/db.bun.ts"
},
"#pty": {
"bun": "./src/pty/pty.bun.ts",
"node": "./src/pty/pty.node.ts",
"default": "./src/pty/pty.bun.ts"
}
},
"devDependencies": {
Expand Down Expand Up @@ -129,6 +135,7 @@
"jsonc-parser": "3.3.1",
"mime-types": "3.0.2",
"minimatch": "10.0.3",
"node-pty": "1.1.0",
"open": "10.1.2",
"opentui-spinner": "0.0.6",
"partial-json": "0.1.7",
Expand Down
2 changes: 1 addition & 1 deletion packages/opencode/script/build-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ await Bun.build({
entrypoints: ["./src/node.ts"],
outdir: "./dist",
format: "esm",
external: ["jsonc-parser"],
external: ["jsonc-parser", "node-pty"],
define: {
OPENCODE_MIGRATIONS: JSON.stringify(migrations),
},
Expand Down
28 changes: 28 additions & 0 deletions packages/opencode/script/fix-node-pty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bun

import fs from "fs/promises"
import path from "path"
import { fileURLToPath } from "url"

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const dir = path.resolve(__dirname, "..")

if (process.platform !== "win32") {
const root = path.join(dir, "node_modules", "node-pty", "prebuilds")
const dirs = await fs.readdir(root, { withFileTypes: true }).catch(() => [])
const files = dirs.filter((x) => x.isDirectory()).map((x) => path.join(root, x.name, "spawn-helper"))
const result = await Promise.all(
files.map(async (file) => {
const stat = await fs.stat(file).catch(() => undefined)
if (!stat) return
if ((stat.mode & 0o111) === 0o111) return
await fs.chmod(file, stat.mode | 0o755)
return file
}),
)
const fixed = result.filter(Boolean)
if (fixed.length) {
console.log(`fixed node-pty permissions for ${fixed.length} helper${fixed.length === 1 ? "" : "s"}`)
}
}
11 changes: 4 additions & 7 deletions packages/opencode/src/pty/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BusEvent } from "@/bus/bus-event"
import { Bus } from "@/bus"
import { type IPty } from "bun-pty"
import type { Proc } from "#pty"
import z from "zod"
import { Identifier } from "../id/id"
import { Log } from "../util/log"
Expand Down Expand Up @@ -35,10 +35,7 @@ export namespace Pty {
return out
}

const pty = lazy(async () => {
const { spawn } = await import("bun-pty")
return spawn
})
const pty = lazy(() => import("#pty"))

export const Info = z
.object({
Expand Down Expand Up @@ -85,7 +82,7 @@ export namespace Pty {

interface ActiveSession {
info: Info
process: IPty
process: Proc
buffer: string
bufferCursor: number
cursor: number
Expand Down Expand Up @@ -144,7 +141,7 @@ export namespace Pty {
}
log.info("creating session", { id, cmd: command, args, cwd })

const spawn = await pty()
const { spawn } = await pty()
const ptyProcess = spawn(command, args, {
name: "xterm-256color",
cwd,
Expand Down
26 changes: 26 additions & 0 deletions packages/opencode/src/pty/pty.bun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { spawn as create } from "bun-pty"
import type { Opts, Proc } from "./pty"

export type { Disp, Exit, Opts, Proc } from "./pty"

export function spawn(file: string, args: string[], opts: Opts): Proc {
const pty = create(file, args, opts)
return {
pid: pty.pid,
onData(listener) {
return pty.onData(listener)
},
onExit(listener) {
return pty.onExit(listener)
},
write(data) {
pty.write(data)
},
resize(cols, rows) {
pty.resize(cols, rows)
},
kill(signal) {
pty.kill(signal)
},
}
}
26 changes: 26 additions & 0 deletions packages/opencode/src/pty/pty.node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as pty from "node-pty"
import type { Opts, Proc } from "./pty"

export type { Disp, Exit, Opts, Proc } from "./pty"

export function spawn(file: string, args: string[], opts: Opts): Proc {
const proc = pty.spawn(file, args, opts)
return {
pid: proc.pid,
onData(listener) {
return proc.onData(listener)
},
onExit(listener) {
return proc.onExit(listener)
},
write(data) {
proc.write(data)
},
resize(cols, rows) {
proc.resize(cols, rows)
},
kill(signal) {
proc.kill(signal)
},
}
}
25 changes: 25 additions & 0 deletions packages/opencode/src/pty/pty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export type Disp = {
dispose(): void
}

export type Exit = {
exitCode: number
signal?: number | string
}

export type Opts = {
name: string
cols?: number
rows?: number
cwd?: string
env?: Record<string, string>
}

export type Proc = {
pid: number
onData(listener: (data: string) => void): Disp
onExit(listener: (event: Exit) => void): Disp
write(data: string): void
resize(cols: number, rows: number): void
kill(signal?: string): void
}
4 changes: 2 additions & 2 deletions packages/opencode/src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { WorkspaceContext } from "../control-plane/workspace-context"
import { WorkspaceRouterMiddleware } from "../control-plane/workspace-router-middleware"
import { ProjectRoutes } from "./routes/project"
import { SessionRoutes } from "./routes/session"
// import { PtyRoutes } from "./routes/pty"
import { PtyRoutes } from "./routes/pty"
import { McpRoutes } from "./routes/mcp"
import { FileRoutes } from "./routes/file"
import { ConfigRoutes } from "./routes/config"
Expand Down Expand Up @@ -559,7 +559,7 @@ export namespace Server {
})
},
)
// .route("/pty", PtyRoutes(ws.upgradeWebSocket))
.route("/pty", PtyRoutes(ws.upgradeWebSocket))
.all("/*", async (c) => {
const path = c.req.path

Expand Down
Loading