| 19 | } { |
| 20 | let captured: Captured | null = null |
| 21 | const capture = async ( |
| 22 | input: URL | RequestInfo, |
| 23 | init?: RequestInit, |
| 24 | ): Promise<Response> => { |
| 25 | const req = new Request(input as RequestInfo, init) |
| 26 | const body = await req.clone().text() |
| 27 | const headers: Record<string, string> = {} |
| 28 | req.headers.forEach((v, k) => { |
| 29 | headers[k.toLowerCase()] = v |
| 30 | }) |
| 31 | captured = { url: req.url, method: req.method, headers, body } |
| 32 | const streamBody = |
| 33 | 'event: message_start\ndata: {"type":"message_start","message":{"id":"m","type":"message","role":"assistant","content":[],"model":"x","stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":0,"output_tokens":0}}}\n\nevent: message_stop\ndata: {"type":"message_stop"}\n\n' |
| 34 | return new Response(streamBody, { |
| 35 | status: 200, |
| 36 | headers: { 'content-type': 'text/event-stream' }, |
| 37 | }) |
| 38 | } |
| 39 | // SDK only calls the fetch function form, never the static `preconnect` that |
| 40 | // Bun/Node's `typeof fetch` declares. Cast is safe (mirrors openai/client.ts). |
| 41 | return { fetch: capture as unknown as typeof fetch, get: () => captured } |