|
| 1 | +import {createServer, type IncomingHttpHeaders, type IncomingMessage, type Server, type ServerResponse} from "node:http"; |
| 2 | +import type {AddressInfo, Socket} from "node:net"; |
| 3 | + |
| 4 | +const LOOPBACK_HOST = "127.0.0.1"; |
| 5 | +const CONTENT_TYPE_JSON = "application/json"; |
| 6 | +const CONTENT_TYPE_SSE = "text/event-stream"; |
| 7 | + |
| 8 | +export interface CapturedRequest { |
| 9 | + readonly method: string; |
| 10 | + readonly url: string; |
| 11 | + readonly headers: IncomingHttpHeaders; |
| 12 | + readonly body: string; |
| 13 | +} |
| 14 | + |
| 15 | +export interface MockGatewayServerOptions { |
| 16 | + readonly replyText?: string; |
| 17 | +} |
| 18 | + |
| 19 | +export class MockGatewayServer { |
| 20 | + private readonly server: Server; |
| 21 | + private readonly capturedRequests: CapturedRequest[]; |
| 22 | + private readonly openSockets: Set<Socket>; |
| 23 | + private readonly url: string; |
| 24 | + |
| 25 | + private constructor(server: Server, url: string, capturedRequests: CapturedRequest[], openSockets: Set<Socket>) { |
| 26 | + this.server = server; |
| 27 | + this.url = url; |
| 28 | + this.capturedRequests = capturedRequests; |
| 29 | + this.openSockets = openSockets; |
| 30 | + } |
| 31 | + |
| 32 | + static async start(options: MockGatewayServerOptions = {}): Promise<MockGatewayServer> { |
| 33 | + const capturedRequests: CapturedRequest[] = []; |
| 34 | + const openSockets = new Set<Socket>(); |
| 35 | + |
| 36 | + const server = createServer((req, res) => handleRequest(req, res, capturedRequests, options)); |
| 37 | + server.on("connection", (socket) => { |
| 38 | + openSockets.add(socket); |
| 39 | + socket.once("close", () => openSockets.delete(socket)); |
| 40 | + }); |
| 41 | + |
| 42 | + await new Promise<void>((resolve, reject) => { |
| 43 | + server.once("error", reject); |
| 44 | + server.listen(0, LOOPBACK_HOST, () => { |
| 45 | + server.off("error", reject); |
| 46 | + resolve(); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + const address = server.address() as AddressInfo; |
| 51 | + return new MockGatewayServer(server, `http://${LOOPBACK_HOST}:${address.port}`, capturedRequests, openSockets); |
| 52 | + } |
| 53 | + |
| 54 | + get baseUrl(): string { |
| 55 | + return this.url; |
| 56 | + } |
| 57 | + |
| 58 | + get requests(): readonly CapturedRequest[] { |
| 59 | + return this.capturedRequests; |
| 60 | + } |
| 61 | + |
| 62 | + async stop(): Promise<void> { |
| 63 | + for (const socket of this.openSockets) { |
| 64 | + socket.destroy(); |
| 65 | + } |
| 66 | + this.openSockets.clear(); |
| 67 | + await new Promise<void>((resolve) => { |
| 68 | + this.server.close(() => resolve()); |
| 69 | + }); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +function handleRequest( |
| 74 | + req: IncomingMessage, |
| 75 | + res: ServerResponse, |
| 76 | + requests: CapturedRequest[], |
| 77 | + options: MockGatewayServerOptions, |
| 78 | +): void { |
| 79 | + const chunks: Buffer[] = []; |
| 80 | + req.on("data", (chunk: Buffer) => chunks.push(chunk)); |
| 81 | + req.on("end", () => { |
| 82 | + requests.push({ |
| 83 | + method: req.method ?? "", |
| 84 | + url: req.url ?? "", |
| 85 | + headers: req.headers, |
| 86 | + body: Buffer.concat(chunks).toString("utf8"), |
| 87 | + }); |
| 88 | + |
| 89 | + if (options.replyText !== undefined) { |
| 90 | + writeResponsesSseReply(res, options.replyText); |
| 91 | + } else { |
| 92 | + res.writeHead(500, {"Content-Type": CONTENT_TYPE_JSON}); |
| 93 | + res.end('{"error":"mock gateway"}'); |
| 94 | + } |
| 95 | + }); |
| 96 | +} |
| 97 | + |
| 98 | +function writeResponsesSseReply(res: ServerResponse, replyText: string): void { |
| 99 | + res.writeHead(200, { |
| 100 | + "Content-Type": CONTENT_TYPE_SSE, |
| 101 | + "Cache-Control": "no-cache", |
| 102 | + "Connection": "keep-alive", |
| 103 | + }); |
| 104 | + |
| 105 | + const responseId = "resp_mock"; |
| 106 | + const messageId = "msg_mock"; |
| 107 | + const createdAt = 1_700_000_000; |
| 108 | + const model = "test-model"; |
| 109 | + const messageItem = { |
| 110 | + id: messageId, |
| 111 | + type: "message", |
| 112 | + role: "assistant", |
| 113 | + status: "completed", |
| 114 | + content: [{type: "output_text", text: replyText, annotations: []}], |
| 115 | + }; |
| 116 | + |
| 117 | + const events: Array<{event: string; data: Record<string, unknown>}> = [ |
| 118 | + { |
| 119 | + event: "response.created", |
| 120 | + data: { |
| 121 | + type: "response.created", |
| 122 | + sequence_number: 1, |
| 123 | + response: { |
| 124 | + id: responseId, |
| 125 | + object: "response", |
| 126 | + status: "in_progress", |
| 127 | + created_at: createdAt, |
| 128 | + model, |
| 129 | + output: [], |
| 130 | + }, |
| 131 | + }, |
| 132 | + }, |
| 133 | + { |
| 134 | + event: "response.in_progress", |
| 135 | + data: { |
| 136 | + type: "response.in_progress", |
| 137 | + sequence_number: 2, |
| 138 | + response: {id: responseId, status: "in_progress", output: []}, |
| 139 | + }, |
| 140 | + }, |
| 141 | + { |
| 142 | + event: "response.output_item.added", |
| 143 | + data: { |
| 144 | + type: "response.output_item.added", |
| 145 | + sequence_number: 3, |
| 146 | + output_index: 0, |
| 147 | + item: {id: messageId, type: "message", role: "assistant", status: "in_progress", content: []}, |
| 148 | + }, |
| 149 | + }, |
| 150 | + { |
| 151 | + event: "response.content_part.added", |
| 152 | + data: { |
| 153 | + type: "response.content_part.added", |
| 154 | + sequence_number: 4, |
| 155 | + item_id: messageId, |
| 156 | + output_index: 0, |
| 157 | + content_index: 0, |
| 158 | + part: {type: "output_text", text: "", annotations: []}, |
| 159 | + }, |
| 160 | + }, |
| 161 | + { |
| 162 | + event: "response.output_text.delta", |
| 163 | + data: { |
| 164 | + type: "response.output_text.delta", |
| 165 | + sequence_number: 5, |
| 166 | + item_id: messageId, |
| 167 | + output_index: 0, |
| 168 | + content_index: 0, |
| 169 | + delta: replyText, |
| 170 | + }, |
| 171 | + }, |
| 172 | + { |
| 173 | + event: "response.output_text.done", |
| 174 | + data: { |
| 175 | + type: "response.output_text.done", |
| 176 | + sequence_number: 6, |
| 177 | + item_id: messageId, |
| 178 | + output_index: 0, |
| 179 | + content_index: 0, |
| 180 | + text: replyText, |
| 181 | + }, |
| 182 | + }, |
| 183 | + { |
| 184 | + event: "response.content_part.done", |
| 185 | + data: { |
| 186 | + type: "response.content_part.done", |
| 187 | + sequence_number: 7, |
| 188 | + item_id: messageId, |
| 189 | + output_index: 0, |
| 190 | + content_index: 0, |
| 191 | + part: {type: "output_text", text: replyText, annotations: []}, |
| 192 | + }, |
| 193 | + }, |
| 194 | + { |
| 195 | + event: "response.output_item.done", |
| 196 | + data: { |
| 197 | + type: "response.output_item.done", |
| 198 | + sequence_number: 8, |
| 199 | + output_index: 0, |
| 200 | + item: messageItem, |
| 201 | + }, |
| 202 | + }, |
| 203 | + { |
| 204 | + event: "response.completed", |
| 205 | + data: { |
| 206 | + type: "response.completed", |
| 207 | + sequence_number: 9, |
| 208 | + response: { |
| 209 | + id: responseId, |
| 210 | + object: "response", |
| 211 | + status: "completed", |
| 212 | + created_at: createdAt, |
| 213 | + model, |
| 214 | + output: [messageItem], |
| 215 | + usage: {input_tokens: 1, output_tokens: 1, total_tokens: 2}, |
| 216 | + }, |
| 217 | + }, |
| 218 | + }, |
| 219 | + ]; |
| 220 | + |
| 221 | + for (const {event, data} of events) { |
| 222 | + res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`); |
| 223 | + } |
| 224 | + res.end(); |
| 225 | +} |
0 commit comments