@@ -11,6 +11,7 @@ import { createRefCountMap } from "@/runtime/server/refcount"
1111import { createRequestQueue } from "@/runtime/server/request-queue"
1212import { ServerScope } from "@/runtime/server/scope"
1313import { useServer } from "./current"
14+ import { createEndpointSyncState , transitionEndpointSync , type EndpointSyncEvent } from "./server-sync/endpoint-machine"
1415
1516type OpenCodeEventMap = { [ Type in OpenCodeEvent [ "type" ] ] : Extract < OpenCodeEvent , { type : Type } > }
1617
@@ -76,9 +77,13 @@ type ServerSDKBase = {
7677 event : OpenCodeEventSource
7778}
7879
80+ // Where a plugin server's transport points until the plugin reports the server ready; it never answers. The server has
81+ // a controller, and so a transport, from the moment it is listed.
82+ const unready = { url : "http://127.0.0.1:0" }
83+
7984function createServerSdkContextBase ( server : ServerConnection . Any , scope : ServerScope ) : ServerSDKBase {
8085 const platform = usePlatform ( )
81- const transport = createServerTransport ( { http : server . http , fetch : platform . fetch } )
86+ const transport = createServerTransport ( { http : ServerConnection . http ( server ) ?? unready , fetch : platform . fetch } )
8287 const media = createMediaCache ( ( ) => transport . api )
8388 const serverKey = ServerConnection . key ( server )
8489 setMediaBlobLoader ( serverKey , ( uri ) => media . load ( uri ) )
@@ -89,9 +94,38 @@ function createServerSdkContextBase(server: ServerConnection.Any, scope: ServerS
8994
9095 const events = createOpenCodeEventSource ( )
9196 const reconnect = resolveEndpoint ( server )
97+ // Read only by `dispatchEndpoint`, the transport's only writer.
98+ let endpoint = createEndpointSyncState ( transport . http )
99+
100+ function dispatchEndpoint ( event : EndpointSyncEvent ) {
101+ const result = transitionEndpointSync ( endpoint , event )
102+ endpoint = result . state
103+ result . effects . forEach ( ( effect ) => {
104+ if ( effect . type === "transport.update" ) return void transport . update ( effect . http )
105+ connection . internal . resync ( "Server endpoint changed" )
106+ } )
107+ }
92108
93109 const connection = createClientConnection ( transport . api , {
94- reconnect : reconnect ? async ( signal ) => transport . update ( await reconnect ( signal ) ) : undefined ,
110+ // Returns the transport's client rather than the resolved one: when the plugin reported a newer endpoint while
111+ // this resolution ran, the stream connects to that one.
112+ reconnect : reconnect
113+ ? async ( signal ) => {
114+ dispatchEndpoint ( { type : "resolve.started" } )
115+
116+ if ( endpoint . type !== "resolving" ) return transport . api
117+ const attempt = endpoint . attempt
118+ const resolution = reconnect ( signal )
119+
120+ void resolution . catch ( ( ) => dispatchEndpoint ( { type : "resolve.failed" , attempt } ) )
121+
122+ return resolution . then ( ( http ) => {
123+ dispatchEndpoint ( { type : "resolve.succeeded" , attempt, http } )
124+
125+ return transport . api
126+ } )
127+ }
128+ : undefined ,
95129 flushInterval : 16 ,
96130 pageLifecycle : true ,
97131 onEvent ( event ) {
@@ -111,17 +145,24 @@ function createServerSdkContextBase(server: ServerConnection.Any, scope: ServerS
111145 if ( server . type === "plugin" || server . type === "sidecar" ) {
112146 createEffect (
113147 on (
114- ( ) => `${ server . http . url } \0${ server . http . password ?? "" } ` ,
115148 ( ) => {
116- if ( sameEndpoint ( transport . http , server . http ) ) return
117- transport . update ( server . http )
118- connection . internal . resync ( "Server endpoint changed" )
149+ const http = ServerConnection . http ( server )
150+
151+ return http && `${ http . url } \0${ http . password ?? "" } `
152+ } ,
153+ ( ) => {
154+ const http = ServerConnection . http ( server )
155+
156+ // A plugin server that stops being ready keeps its last endpoint until the plugin reports it ready again.
157+ if ( http ) dispatchEndpoint ( { type : "endpoint.reported" , http } )
119158 } ,
120159 { defer : true } ,
121160 ) ,
122161 )
123162 }
124163
164+ onCleanup ( ( ) => dispatchEndpoint ( { type : "disposed" } ) )
165+
125166 return {
126167 server,
127168 scope,
@@ -141,14 +182,22 @@ function createServerSdkContextBase(server: ServerConnection.Any, scope: ServerS
141182}
142183
143184/**
144- * Where the event stream connects after a disconnect. A plugin's server can exist before the plugin reports its
145- * endpoint (a server restored from the plugin's last inventory starts on a placeholder) and can move at any time, so
146- * its stream always resolves the plugin's current endpoint, through the plugin's `reconnect` when it has one then.
185+ * Where the event stream connects after a disconnect. A plugin's server can exist before it is ready (a server restored
186+ * from the plugin's last inventory starts on `unready`) and can move at any time, so its stream always resolves the
187+ * plugin's current endpoint, through the plugin's `reconnect` when it has one then. A server that is not ready has none
188+ * to resolve, so the stream keeps retrying where it is.
147189 */
148190function resolveEndpoint (
149191 server : ServerConnection . Any ,
150192) : ( ( signal : AbortSignal ) => Promise < ServerConnection . HttpBase > ) | undefined {
151- if ( server . type === "plugin" ) return ( signal ) => server . reconnect ?.( signal ) ?? Promise . resolve ( server . http )
193+ if ( server . type === "plugin" )
194+ return ( signal ) => {
195+ const http = ServerConnection . http ( server )
196+
197+ return (
198+ server . reconnect ?.( signal ) ?? ( http ? Promise . resolve ( http ) : Promise . reject ( new Error ( "Server is not ready" ) ) )
199+ )
200+ }
152201
153202 if ( server . type === "sidecar" ) return server . reconnect
154203
0 commit comments