@@ -67,6 +67,22 @@ const operationOf = (
6767 if ( method === "POST" && path === "/api/session" ) return { type : "session.create" , input : body } ;
6868 if ( method === "GET" && path === "/api/session/active" ) return { type : "session.active" } ;
6969 if ( method === "GET" && path === "/api/command" ) return { type : "command.list" , input : query } ;
70+ const mcp = / ^ \/ a p i \/ e x p e r i m e n t a l \/ m c p \/ ( [ ^ / ] + ) $ / . exec ( path ) ;
71+ if ( mcp !== null && method === "PUT" ) {
72+ return { type : "mcp.add" , input : { server : mcp [ 1 ] , ...query , ...( body as object ) } } ;
73+ }
74+ if ( mcp !== null && method === "DELETE" ) {
75+ return { type : "mcp.remove" , input : { server : mcp [ 1 ] , ...query } } ;
76+ }
77+ const entry = / ^ \/ a p i \/ e x p e r i m e n t a l \/ s e s s i o n \/ ( [ ^ / ] + ) \/ i n s t r u c t i o n s \/ e n t r i e s \/ ( [ ^ / ] + ) $ / . exec (
78+ path ,
79+ ) ;
80+ if ( entry !== null && method === "PUT" ) {
81+ return {
82+ type : "session.instructions.entry.put" ,
83+ input : { sessionID : entry [ 1 ] , key : entry [ 2 ] , ...( body as object ) } ,
84+ } ;
85+ }
7086 if ( method === "GET" && path === "/api/skill" ) return { type : "skill.list" , input : query } ;
7187 if ( session !== null ) {
7288 const [ , sessionID , rest = "" ] = session ;
@@ -76,6 +92,7 @@ const operationOf = (
7692 ) ;
7793 const input = { sessionID, ...query , ...Object . fromEntries ( fields ) } ;
7894 if ( method === "GET" && rest === "" ) return { type : "session.get" , input } ;
95+ if ( method === "DELETE" && rest === "" ) return { type : "session.remove" , input } ;
7996 if ( method === "PATCH" && rest === "" ) return { type : "session.update" , input } ;
8097 if ( method === "POST" && rest === "/prompt" ) return { type : "session.prompt" , input } ;
8198 if ( method === "POST" && rest === "/command" ) return { type : "session.command" , input } ;
@@ -118,14 +135,23 @@ const operationOf = (
118135 return { type : `${ method } ${ path } ` , input : { ...query , body } } ;
119136} ;
120137
121- /** An `HttpClient` that answers every request from the transcript. */
138+ /**
139+ * An `HttpClient` that answers every request from the transcript. A successful
140+ * `runtime_exit` ends the event stream: the server has stopped. When entries
141+ * follow it, the server was restarted and the next `event.subscribe` opens its
142+ * new stream; a transcript that ends there stays down, so every later request
143+ * fails to connect as it would against a stopped server.
144+ */
122145const replayHttpClient = (
123146 controller : OpenCodeReplayController ,
124147 replayGate : ProviderReplayGate | undefined ,
125148) =>
126149 HttpClient . make ( ( request , url ) =>
127150 Effect . tryPromise ( {
128151 try : async ( ) => {
152+ if ( controller . exited && controller . finished ) {
153+ throw new Error ( "The replayed OpenCode server has exited." ) ;
154+ }
129155 const raw =
130156 request . body . _tag === "Uint8Array"
131157 ? new TextDecoder ( ) . decode ( request . body . body )
@@ -142,6 +168,7 @@ const replayHttpClient = (
142168 if ( operation . type !== "event.subscribe" ) await controller . untilInboundDelivered ( ) ;
143169 await controller . expectOutbound ( operation ) ;
144170 if ( operation . type === "event.subscribe" ) {
171+ controller . exited = false ;
145172 const encoder = new TextEncoder ( ) ;
146173 const events = controller . events ( undefined , replayGate ?. beforeEmit ) ;
147174 const frames = events [ Symbol . asyncIterator ] ( ) ;
@@ -175,10 +202,18 @@ const replayHttpClient = (
175202 } ) . pipe ( Effect . map ( ( response ) => HttpClientResponse . fromWeb ( request , response ) ) ) ,
176203 ) ;
177204
178- /** The 2.x adapter over a replayed server, checking at scope close that the transcript ran out. */
179- const makeReplayAdapter = (
205+ /**
206+ * An OpenCode 2 server that answers from `transcript`, checking at scope close
207+ * that the transcript ran out.
208+ */
209+ export const replayServer = (
180210 transcript : ProviderReplayTranscript ,
181- options ?: { readonly external ?: boolean ; readonly replayGate ?: ProviderReplayGate } ,
211+ options ?: {
212+ readonly external ?: boolean ;
213+ readonly replayGate ?: ProviderReplayGate ;
214+ /** Counts the connections currently lent out, as the server owner's borrowers. */
215+ readonly borrowers ?: { current : number } ;
216+ } ,
182217) =>
183218 Effect . gen ( function * ( ) {
184219 const controller = new OpenCodeReplayController ( transcript ) ;
@@ -200,11 +235,38 @@ const makeReplayAdapter = (
200235 version : transcript . version ,
201236 external : options ?. external ?? false ,
202237 } ;
238+ const borrowers = options ?. borrowers ;
239+ return OpenCode2Server . OpenCode2Server . of ( {
240+ withConnection : ( use ) =>
241+ borrowers === undefined
242+ ? use ( connection )
243+ : Effect . acquireUseRelease (
244+ Effect . sync ( ( ) => {
245+ borrowers . current += 1 ;
246+ } ) ,
247+ ( ) => use ( connection ) ,
248+ ( ) =>
249+ Effect . sync ( ( ) => {
250+ borrowers . current -= 1 ;
251+ } ) ,
252+ ) ,
253+ } ) ;
254+ } ) ;
255+
256+ /** The 2.x adapter over a replayed server. */
257+ const makeReplayAdapter = (
258+ transcript : ProviderReplayTranscript ,
259+ options ?: {
260+ readonly external ?: boolean ;
261+ readonly replayGate ?: ProviderReplayGate ;
262+ /** Counts the connections currently lent out, as the server owner's borrowers. */
263+ readonly borrowers ?: { current : number } ;
264+ } ,
265+ ) =>
266+ Effect . gen ( function * ( ) {
267+ const server = yield * replayServer ( transcript , options ) ;
203268 return yield * OpenCode2AdapterV2 . make ( ProviderInstanceId . make ( "opencode" ) ) . pipe (
204- Effect . provideService (
205- OpenCode2Server . OpenCode2Server ,
206- OpenCode2Server . OpenCode2Server . of ( { withConnection : ( use ) => use ( connection ) } ) ,
207- ) ,
269+ Effect . provideService ( OpenCode2Server . OpenCode2Server , server ) ,
208270 ) ;
209271 } ) ;
210272
@@ -230,7 +292,7 @@ function makeRegistryLayer(
230292 */
231293export const openCode2ReplayRuntime = (
232294 entries : ReadonlyArray < ProviderReplayEntry > ,
233- options ?: { readonly external ?: boolean } ,
295+ options ?: { readonly external ?: boolean ; readonly borrowers ?: { current : number } } ,
234296) =>
235297 Effect . gen ( function * ( ) {
236298 const adapter = yield * makeReplayAdapter (
0 commit comments