Skip to content

Commit 40c206c

Browse files
committed
add opencode attach command to connect to a remote opencode server
1 parent 259c722 commit 40c206c

6 files changed

Lines changed: 58 additions & 409 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { Global } from "../../global"
2+
import { cmd } from "./cmd"
3+
import path from "path"
4+
import fs from "fs/promises"
5+
import { Log } from "../../util/log"
6+
7+
import { $ } from "bun"
8+
9+
export const AttachCommand = cmd({
10+
command: "attach <server>",
11+
describe: "attach to a running opencode server",
12+
builder: (yargs) =>
13+
yargs.positional("server", {
14+
type: "string",
15+
describe: "http://localhost:4096",
16+
}),
17+
handler: async (args) => {
18+
let cmd = [] as string[]
19+
const tui = Bun.embeddedFiles.find((item) => (item as File).name.includes("tui")) as File
20+
if (tui) {
21+
let binaryName = tui.name
22+
if (process.platform === "win32" && !binaryName.endsWith(".exe")) {
23+
binaryName += ".exe"
24+
}
25+
const binary = path.join(Global.Path.cache, "tui", binaryName)
26+
const file = Bun.file(binary)
27+
if (!(await file.exists())) {
28+
await Bun.write(file, tui, { mode: 0o755 })
29+
if (process.platform !== "win32") await fs.chmod(binary, 0o755)
30+
}
31+
cmd = [binary]
32+
}
33+
if (!tui) {
34+
const dir = Bun.fileURLToPath(new URL("../../../../tui/cmd/opencode", import.meta.url))
35+
let binaryName = `./dist/tui${process.platform === "win32" ? ".exe" : ""}`
36+
await $`go build -o ${binaryName} ./main.go`.cwd(dir)
37+
cmd = [path.join(dir, binaryName)]
38+
}
39+
Log.Default.info("tui", {
40+
cmd,
41+
})
42+
const proc = Bun.spawn({
43+
cmd,
44+
stdout: "inherit",
45+
stderr: "inherit",
46+
stdin: "inherit",
47+
env: {
48+
...process.env,
49+
CGO_ENABLED: "0",
50+
OPENCODE_SERVER: args.server,
51+
},
52+
})
53+
54+
await proc.exited
55+
},
56+
})

packages/opencode/src/cli/cmd/tui.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import { Ide } from "../../ide"
1515

1616
import { Flag } from "../../flag/flag"
1717
import { Session } from "../../session"
18-
import { Instance } from "../../project/instance"
1918
import { $ } from "bun"
2019

2120
declare global {
@@ -151,7 +150,6 @@ export const TuiCommand = cmd({
151150
...process.env,
152151
CGO_ENABLED: "0",
153152
OPENCODE_SERVER: server.url.toString(),
154-
OPENCODE_PROJECT: JSON.stringify(Instance.project),
155153
},
156154
onExit: () => {
157155
server.stop()

packages/opencode/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { StatsCommand } from "./cli/cmd/stats"
1818
import { McpCommand } from "./cli/cmd/mcp"
1919
import { GithubCommand } from "./cli/cmd/github"
2020
import { ExportCommand } from "./cli/cmd/export"
21+
import { AttachCommand } from "./cli/cmd/attach"
2122

2223
const cancel = new AbortController()
2324

@@ -71,6 +72,7 @@ const cli = yargs(hideBin(process.argv))
7172
.usage("\n" + UI.logo())
7273
.command(McpCommand)
7374
.command(TuiCommand)
75+
.command(AttachCommand)
7476
.command(RunCommand)
7577
.command(GenerateCommand)
7678
.command(DebugCommand)

packages/opencode/src/plugin/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export namespace Plugin {
1515
const state = Instance.state(async () => {
1616
const client = createOpencodeClient({
1717
baseUrl: "http://localhost:4096",
18-
// @ts-expect-error
1918
fetch: async (...args) => Server.App().fetch(...args),
2019
})
2120
const config = await Config.get()

0 commit comments

Comments
 (0)