@@ -81,6 +81,54 @@ describe("SessionModelRequest HTTP hooks", () => {
8181 } ) . pipe ( Effect . provideService ( SessionModelTransport . Service , transport ) ) ,
8282 )
8383
84+ it . effect ( "sends a kept request's original body and a replaced request's new body" , ( ) =>
85+ Effect . gen ( function * ( ) {
86+ const hooks = yield * PluginHooks . Service
87+ let replace = false
88+ yield * hooks . register ( "session" , "http.request" , ( event ) =>
89+ Effect . sync ( ( ) => {
90+ if ( replace ) {
91+ event . request = new Request ( event . request . url , { method : "POST" , body : '{"replaced":true}' } )
92+ return
93+ }
94+ event . request . headers . set ( "x-plugin" , "kept" )
95+ } ) ,
96+ )
97+ const requests = yield * SessionModelRequest . Service . pipe ( Effect . provide ( SessionModelRequest . layer ) )
98+ const prepared = yield * requests . primary ( {
99+ session,
100+ agent : Agent . ID . make ( "build" ) ,
101+ model,
102+ system : [ ] ,
103+ messages : [ ] ,
104+ } )
105+ const http = prepared . options . http
106+ if ( ! http ) throw new Error ( "Expected HTTP middleware" )
107+ const original = HttpClientRequest . post ( "https://example.test/v1/chat/completions" ) . pipe (
108+ HttpClientRequest . bodyText ( '{"messages":[]}' , "application/json" ) ,
109+ )
110+ const send = ( ) => {
111+ const sent : HttpClientRequest . HttpClientRequest [ ] = [ ]
112+ return http ( original , ( request ) =>
113+ Effect . sync ( ( ) => {
114+ sent . push ( request )
115+ return HttpClientResponse . fromWeb ( request , new Response ( "{}" , { status : 200 } ) )
116+ } ) ,
117+ ) . pipe ( Effect . map ( ( ) => sent [ 0 ] ! ) )
118+ }
119+
120+ const kept = yield * send ( )
121+ expect ( kept . body ) . toBe ( original . body )
122+ expect ( kept . headers [ "x-plugin" ] ) . toBe ( "kept" )
123+
124+ replace = true
125+ const replaced = yield * send ( )
126+ expect ( replaced . body . _tag ) . toBe ( "Uint8Array" )
127+ if ( replaced . body . _tag !== "Uint8Array" ) return
128+ expect ( new TextDecoder ( ) . decode ( replaced . body . body ) ) . toBe ( '{"replaced":true}' )
129+ } ) . pipe ( Effect . provideService ( SessionModelTransport . Service , transport ) ) ,
130+ )
131+
84132 it . effect ( "keeps hook edits to a tool schema out of later requests" , ( ) =>
85133 Effect . gen ( function * ( ) {
86134 const hooks = yield * PluginHooks . Service
0 commit comments