11import { describe , expect } from "bun:test"
22import { Deferred , Effect , Fiber , Layer , Ref , Stream } from "effect"
3- import { Headers , HttpClientError , HttpClientRequest } from "effect/unstable/http"
4- import { LLM , AIError , HttpContext , InvalidProviderOutputError , TransportError } from "../src/index.js"
5- import { LLMClient , RequestExecutor , WebSocketTransport , type WebSocketChannelExecutor } from "../src/route.js"
3+ import { Headers , HttpClientError , HttpClientRequest , HttpClientResponse } from "effect/unstable/http"
4+ import { LLM , AIError , HttpContext , InvalidProviderOutputError , TransportError , isRetryable } from "../src/index.js"
5+ import {
6+ LLMClient ,
7+ RequestExecutor ,
8+ WebSocketTransport ,
9+ type HttpMiddleware ,
10+ type WebSocketChannelExecutor ,
11+ } from "../src/route.js"
612import { route } from "../src/protocols/openai-chat.js"
713import { configure } from "../src/providers/openai.js"
8- import { dynamicResponse , fixedResponse , handlerLayer , systemError } from "./lib/http.js"
14+ import { dynamicResponse , fixedResponse , handlerLayer , systemError , truncatedStream } from "./lib/http.js"
915import { deltaChunk } from "./lib/openai-chunks.js"
1016import { sseEvents , sseRaw } from "./lib/sse.js"
1117import { it } from "./lib/effect.js"
@@ -18,6 +24,8 @@ const secretRequest = HttpClientRequest.post("https://provider.test/v1/chat?api_
1824 HttpClientRequest . setHeaders ( Headers . fromInput ( { authorization : "Bearer header-secret-456" } ) ) ,
1925)
2026
27+ const sseRequest = HttpClientRequest . post ( "https://provider.test/v1/messages" )
28+
2129const expectAIError = ( error : unknown ) => {
2230 expect ( error ) . toBeInstanceOf ( AIError )
2331 if ( ! ( error instanceof AIError ) ) throw new Error ( "expected AIError" )
@@ -93,7 +101,9 @@ describe("RequestExecutor", () => {
93101 const error = yield * RequestExecutor . stream ( executor , secretRequest ) . pipe ( Stream . runDrain , Effect . flip )
94102
95103 expectAIError ( error )
96- expect ( error . message ) . toBe ( "ECONNRESET: disconnected query-secret-123 header-secret-456" )
104+ expect ( error . message ) . toBe (
105+ "Connection lost while reading the response: ECONNRESET: disconnected query-secret-123 header-secret-456" ,
106+ )
97107 expect ( error . reason . http ) . toMatchObject ( { status : 200 , url : secretRequest . url } )
98108 expect ( error . reason . cause ) . toMatchObject ( { code : "ECONNRESET" } )
99109 expect ( error . reason ) . toMatchObject ( {
@@ -123,7 +133,7 @@ describe("RequestExecutor", () => {
123133 const error = yield * RequestExecutor . stream ( executor , secretRequest ) . pipe ( Stream . runDrain , Effect . flip )
124134
125135 expectAIError ( error )
126- expect ( error . message ) . toBe ( "ECONNRESET: socket closed" )
136+ expect ( error . message ) . toBe ( "Connection lost while reading the response: ECONNRESET: socket closed" )
127137 expect ( error . reason . cause ) . toBeInstanceOf ( TypeError )
128138 expect ( error . reason ) . toMatchObject ( {
129139 _tag : "Transport" ,
@@ -144,6 +154,54 @@ describe("RequestExecutor", () => {
144154 ) ,
145155 )
146156
157+ it . effect ( "reports a connection lost mid-stream through middleware that re-wraps the body" , ( ) =>
158+ Effect . gen ( function * ( ) {
159+ const executor = yield * RequestExecutor . Service
160+ const chunks : Array < Uint8Array > = [ ]
161+ // Session HTTP hooks hand plugins a web Response, so the body is re-wrapped around the original stream.
162+ const rewrap : HttpMiddleware = ( input , handler ) =>
163+ Effect . gen ( function * ( ) {
164+ const response = yield * handler ( input )
165+ const body = yield * Stream . toReadableStreamEffect ( response . stream )
166+ return HttpClientResponse . fromWeb (
167+ input ,
168+ new Response ( body , { status : response . status , headers : response . headers } ) ,
169+ )
170+ } )
171+ const error = yield * RequestExecutor . stream ( executor , sseRequest , rewrap ) . pipe (
172+ Stream . runForEach ( ( chunk ) => Effect . sync ( ( ) => chunks . push ( chunk ) ) ) ,
173+ Effect . flip ,
174+ )
175+
176+ expectAIError ( error )
177+ expect ( new TextDecoder ( ) . decode ( chunks [ 0 ] ) ) . toBe ( 'data: {"type":"ping"}\n\n' )
178+ expect ( error . message ) . toBe ( "Connection lost while reading the response: ECONNRESET: other side closed" )
179+ expect ( error . reason . cause ) . toBeInstanceOf ( TypeError )
180+ expect ( error . reason . http ) . toMatchObject ( { status : 200 } )
181+ expect ( error . reason ) . toMatchObject ( { _tag : "Transport" , operation : "read" , code : "ECONNRESET" } )
182+ expect ( isRetryable ( error ) ) . toBeTrue ( )
183+ } ) . pipe (
184+ Effect . provide (
185+ truncatedStream (
186+ [ 'data: {"type":"ping"}\n\n' ] ,
187+ new TypeError ( "terminated" , { cause : systemError ( "ECONNRESET" , "other side closed" ) } ) ,
188+ ) ,
189+ ) ,
190+ ) ,
191+ )
192+
193+ it . effect ( "does not report a body read failure without a native cause as a decode error" , ( ) =>
194+ Effect . gen ( function * ( ) {
195+ const executor = yield * RequestExecutor . Service
196+ const error = yield * RequestExecutor . stream ( executor , sseRequest ) . pipe ( Stream . runDrain , Effect . flip )
197+
198+ expectAIError ( error )
199+ expect ( error . message ) . toBe ( "Connection lost while reading the response" )
200+ expect ( error . reason ) . toMatchObject ( { _tag : "Transport" , operation : "read" , code : undefined } )
201+ expect ( isRetryable ( error ) ) . toBeTrue ( )
202+ } ) . pipe ( Effect . provide ( truncatedStream ( [ 'data: {"type":"ping"}\n\n' ] , new DOMException ( "aborted" , "AbortError" ) ) ) ) ,
203+ )
204+
147205 it . effect ( "preserves middleware error messages" , ( ) =>
148206 Effect . gen ( function * ( ) {
149207 const executor = yield * RequestExecutor . Service
0 commit comments