@@ -12,19 +12,32 @@ export const { use: useGlobalSDK, provider: GlobalSDKProvider } = createSimpleCo
1212 const platform = usePlatform ( )
1313 const abort = new AbortController ( )
1414
15+ const password = typeof window === "undefined" ? undefined : window . __OPENCODE__ ?. serverPassword
16+
1517 const auth = ( ( ) => {
16- if ( typeof window === "undefined" ) return
17- const password = window . __OPENCODE__ ?. serverPassword
1818 if ( ! password ) return
19+ if ( ! server . isLocal ( ) ) return
1920 return {
2021 Authorization : `Basic ${ btoa ( `opencode:${ password } ` ) } ` ,
2122 }
2223 } ) ( )
2324
25+ const eventFetch = ( ( ) => {
26+ if ( ! platform . fetch ) return
27+ try {
28+ const url = new URL ( server . url )
29+ const loopback = url . hostname === "localhost" || url . hostname === "127.0.0.1" || url . hostname === "::1"
30+ if ( url . protocol === "http:" && ! loopback ) return platform . fetch
31+ } catch {
32+ return
33+ }
34+ } ) ( )
35+
2436 const eventSdk = createOpencodeClient ( {
2537 baseUrl : server . url ,
2638 signal : abort . signal ,
27- headers : auth ,
39+ fetch : eventFetch ,
40+ headers : eventFetch ? undefined : auth ,
2841 } )
2942 const emitter = createGlobalEmitter < {
3043 [ key : string ] : Event
@@ -80,7 +93,17 @@ export const { use: useGlobalSDK, provider: GlobalSDKProvider } = createSimpleCo
8093 let streamErrorLogged = false
8194
8295 void ( async ( ) => {
83- const events = await eventSdk . global . event ( )
96+ const events = await eventSdk . global . event ( {
97+ onSseError : ( error ) => {
98+ if ( streamErrorLogged ) return
99+ streamErrorLogged = true
100+ console . error ( "[global-sdk] event stream error" , {
101+ url : server . url ,
102+ fetch : eventFetch ? "platform" : "webview" ,
103+ error,
104+ } )
105+ } ,
106+ } )
84107 let yielded = Date . now ( )
85108 for await ( const event of events . stream ) {
86109 const directory = event . directory ?? "global"
@@ -106,7 +129,11 @@ export const { use: useGlobalSDK, provider: GlobalSDKProvider } = createSimpleCo
106129 . catch ( ( error ) => {
107130 if ( streamErrorLogged ) return
108131 streamErrorLogged = true
109- console . error ( "[global-sdk] event stream failed" , error )
132+ console . error ( "[global-sdk] event stream failed" , {
133+ url : server . url ,
134+ fetch : eventFetch ? "platform" : "webview" ,
135+ error,
136+ } )
110137 } )
111138
112139 onCleanup ( ( ) => {
0 commit comments