@@ -9,6 +9,7 @@ import { Config } from "@/config/config"
99import { GlobalBus } from "@/bus/global"
1010import { createOpencodeClient , type Event } from "@opencode-ai/sdk/v2"
1111import type { BunWebSocketData } from "hono/bun"
12+ import { Flag } from "@/flag/flag"
1213
1314await 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
9699export 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
137145Rpc . 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