Skip to content
Closed
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
2 changes: 2 additions & 0 deletions packages/opencode/src/cli/cmd/tui/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ export function tui(input: {
directory={input.directory}
fetch={input.fetch}
events={input.events}
username={input.args.username}
password={input.args.password}
>
<SyncProvider>
<ThemeProvider mode={mode}>
Expand Down
13 changes: 12 additions & 1 deletion packages/opencode/src/cli/cmd/tui/attach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,23 @@ export const AttachCommand = cmd({
alias: ["s"],
type: "string",
describe: "session id to continue",
})
.option("username", {
alias: ["u"],
type: "string",
default: "opencode",
describe: "username to authenticate with",
})
.option("password", {
alias: ["p"],
type: "string",
describe: "password to authenticate with",
}),
handler: async (args) => {
if (args.dir) process.chdir(args.dir)
await tui({
url: args.url,
args: { sessionID: args.session },
args: { sessionID: args.session, username: args.username, password: args.password },
directory: args.dir ? process.cwd() : undefined,
})
},
Expand Down
2 changes: 2 additions & 0 deletions packages/opencode/src/cli/cmd/tui/context/args.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { createSimpleContext } from "./helper"

export interface Args {
username?: string
password?: string
model?: string
agent?: string
prompt?: string
Expand Down
7 changes: 6 additions & 1 deletion packages/opencode/src/cli/cmd/tui/context/sdk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ export type EventSource = {

export const { use: useSDK, provider: SDKProvider } = createSimpleContext({
name: "SDK",
init: (props: { url: string; directory?: string; fetch?: typeof fetch; events?: EventSource }) => {
init: (props: { url: string; directory?: string; fetch?: typeof fetch; events?: EventSource, username?: string; password?: string }) => {
const abort = new AbortController()
const sdk = createOpencodeClient({
baseUrl: props.url,
signal: abort.signal,
directory: props.directory,
fetch: props.fetch,
headers: props.password && props.username
? {
Authorization: `Basic ${Buffer.from(`${props.username}:${props.password}`).toString("base64")}`
}
: undefined,
})

const emitter = createGlobalEmitter<{
Expand Down
45 changes: 23 additions & 22 deletions packages/opencode/src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,29 @@ export namespace Server {
status: 500,
})
})
.use(
cors({
origin(input) {
if (!input) return

if (input.startsWith("http://localhost:")) return input
if (input.startsWith("http://127.0.0.1:")) return input
if (input === "tauri://localhost" || input === "http://tauri.localhost") return input

// *.opencode.ai (https only, adjust if needed)
if (/^https:\/\/([a-z0-9-]+\.)*opencode\.ai$/.test(input)) {
return input
}
if (_corsWhitelist.includes(input)) {
return input
}

return
},
}),
)
.use((c, next) => {
if (c.req.path === "/global/health") return next()
const password = Flag.OPENCODE_SERVER_PASSWORD
if (!password) return next()
const username = Flag.OPENCODE_SERVER_USERNAME ?? "opencode"
Expand All @@ -120,27 +142,6 @@ export namespace Server {
timer.stop()
}
})
.use(
cors({
origin(input) {
if (!input) return

if (input.startsWith("http://localhost:")) return input
if (input.startsWith("http://127.0.0.1:")) return input
if (input === "tauri://localhost" || input === "http://tauri.localhost") return input

// *.opencode.ai (https only, adjust if needed)
if (/^https:\/\/([a-z0-9-]+\.)*opencode\.ai$/.test(input)) {
return input
}
if (_corsWhitelist.includes(input)) {
return input
}

return
},
}),
)
.get(
"/global/health",
describeRoute({
Expand All @@ -159,7 +160,7 @@ export namespace Server {
},
}),
async (c) => {
return c.json({ healthy: true, version: Installation.VERSION })
return c.json({ healthy: true, version: Installation.VERSION, authenticated: !!Flag.OPENCODE_SERVER_PASSWORD })
},
)
.get(
Expand Down
2 changes: 2 additions & 0 deletions packages/web/src/content/docs/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ This starts an HTTP server that provides API access to opencode functionality wi
| `--hostname` | Hostname to listen on |
| `--mdns` | Enable mDNS discovery |
| `--cors` | Additional browser origin(s) to allow CORS |
| `--username | Username set on server for basic auth |
| `--password | Password set on server for basic auth |

---

Expand Down