Skip to content

Commit bee4b68

Browse files
fix(TUI): make tui work when OPENCODE_SERVER_PASSWORD is set (anomalyco#8179)
1 parent 3565d8e commit bee4b68

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Config } from "@/config/config"
99
import { GlobalBus } from "@/bus/global"
1010
import { createOpencodeClient, type Event } from "@opencode-ai/sdk/v2"
1111
import type { BunWebSocketData } from "hono/bun"
12+
import { Flag } from "@/flag/flag"
1213

1314
await Log.init({
1415
print: process.argv.includes("--print-logs"),
@@ -50,6 +51,8 @@ const startEventStream = (directory: string) => {
5051

5152
const fetchFn = (async (input: RequestInfo | URL, init?: RequestInit) => {
5253
const request = new Request(input, init)
54+
const auth = getAuthorizationHeader()
55+
if (auth) request.headers.set("Authorization", auth)
5356
return Server.App().fetch(request)
5457
}) as typeof globalThis.fetch
5558

@@ -95,9 +98,14 @@ startEventStream(process.cwd())
9598

9699
export const rpc = {
97100
async fetch(input: { url: string; method: string; headers: Record<string, string>; body?: string }) {
101+
const headers = { ...input.headers }
102+
const auth = getAuthorizationHeader()
103+
if (auth && !headers["authorization"] && !headers["Authorization"]) {
104+
headers["Authorization"] = auth
105+
}
98106
const request = new Request(input.url, {
99107
method: input.method,
100-
headers: input.headers,
108+
headers,
101109
body: input.body,
102110
})
103111
const response = await Server.App().fetch(request)
@@ -135,3 +143,10 @@ export const rpc = {
135143
}
136144

137145
Rpc.listen(rpc)
146+
147+
function getAuthorizationHeader(): string | undefined {
148+
const password = Flag.OPENCODE_SERVER_PASSWORD
149+
if (!password) return undefined
150+
const username = Flag.OPENCODE_SERVER_USERNAME ?? "opencode"
151+
return `Basic ${btoa(`${username}:${password}`)}`
152+
}

0 commit comments

Comments
 (0)