http.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { Effect, Layer, Ref } from "effect"
  2. import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstable/http"
  3. import { LLMClient, RequestExecutor } from "../../src/route"
  4. import type { Service as LLMClientService } from "../../src/route/client"
  5. import type { Service as RequestExecutorService } from "../../src/route/executor"
  6. export type HandlerInput = {
  7. readonly request: HttpClientRequest.HttpClientRequest
  8. readonly text: string
  9. readonly respond: (
  10. body: ConstructorParameters<typeof Response>[0],
  11. init?: ResponseInit,
  12. ) => HttpClientResponse.HttpClientResponse
  13. }
  14. export type Handler = (input: HandlerInput) => Effect.Effect<HttpClientResponse.HttpClientResponse>
  15. const handlerLayer = (handler: Handler): Layer.Layer<HttpClient.HttpClient> =>
  16. Layer.succeed(
  17. HttpClient.HttpClient,
  18. HttpClient.make((request) =>
  19. Effect.gen(function* () {
  20. const web = yield* HttpClientRequest.toWeb(request).pipe(Effect.orDie)
  21. const text = yield* Effect.promise(() => web.text())
  22. return yield* handler({
  23. request,
  24. text,
  25. respond: (body, init) => HttpClientResponse.fromWeb(request, new Response(body, init)),
  26. })
  27. }),
  28. ),
  29. )
  30. export type RuntimeEnv = RequestExecutorService | LLMClientService
  31. export const runtimeLayer = (layer: Layer.Layer<HttpClient.HttpClient>): Layer.Layer<RuntimeEnv> => {
  32. const requestExecutorLayer = RequestExecutor.layer.pipe(Layer.provide(layer))
  33. const llmClientLayer = LLMClient.layer.pipe(Layer.provide(requestExecutorLayer))
  34. return Layer.mergeAll(requestExecutorLayer, llmClientLayer)
  35. }
  36. const SSE_HEADERS = { "content-type": "text/event-stream" } as const
  37. /**
  38. * Layer that returns a single fixed response body. Use for stream-parser
  39. * fixture tests where the request shape is irrelevant. The body type widens
  40. * to whatever `Response` accepts so binary fixtures (`Uint8Array`,
  41. * `ReadableStream`, etc.) flow through without casts.
  42. */
  43. export const fixedResponse = (
  44. body: ConstructorParameters<typeof Response>[0],
  45. init: ResponseInit = { headers: SSE_HEADERS },
  46. ) => runtimeLayer(handlerLayer((input) => Effect.succeed(input.respond(body, init))))
  47. /**
  48. * Layer that builds a response per request. Useful for echo servers.
  49. */
  50. export const dynamicResponse = (handler: Handler) => runtimeLayer(handlerLayer(handler))
  51. /**
  52. * Layer that emits the supplied SSE chunks and then aborts mid-stream. Used to
  53. * exercise transport errors that surface during parsing.
  54. */
  55. export const truncatedStream = (chunks: ReadonlyArray<string>) =>
  56. dynamicResponse((input) =>
  57. Effect.sync(() => {
  58. const encoder = new TextEncoder()
  59. const stream = new ReadableStream({
  60. start(controller) {
  61. for (const chunk of chunks) controller.enqueue(encoder.encode(chunk))
  62. controller.error(new Error("connection reset"))
  63. },
  64. })
  65. return input.respond(stream, { headers: SSE_HEADERS })
  66. }),
  67. )
  68. /**
  69. * Layer that returns successive bodies on each request. Useful for scripting
  70. * multi-step model exchanges (e.g. tool-call loops). The last body in the
  71. * array is reused if the test makes more requests than scripted.
  72. */
  73. export const scriptedResponses = (bodies: ReadonlyArray<string>, init: ResponseInit = { headers: SSE_HEADERS }) => {
  74. if (bodies.length === 0) throw new Error("scriptedResponses requires at least one body")
  75. return Layer.unwrap(
  76. Effect.gen(function* () {
  77. const cursor = yield* Ref.make(0)
  78. return dynamicResponse((input) =>
  79. Effect.gen(function* () {
  80. const index = yield* Ref.getAndUpdate(cursor, (n) => n + 1)
  81. return input.respond(bodies[index] ?? bodies[bodies.length - 1], init)
  82. }),
  83. )
  84. }),
  85. )
  86. }