|
| 1 | +import http from 'http'; |
| 2 | +import { once } from 'events'; |
| 3 | +import type { AddressInfo } from 'net'; |
| 4 | + |
| 5 | +export type MockAssistantResponse = { |
| 6 | + status: number; |
| 7 | + body: string; |
| 8 | +}; |
| 9 | + |
| 10 | +function sendStreamingResponse(res: http.ServerResponse, content: string) { |
| 11 | + // OpenAI Responses API streaming response format using Server-Sent Events |
| 12 | + res.writeHead(200, { |
| 13 | + 'Content-Type': 'text/event-stream; charset=utf-8', |
| 14 | + 'Cache-Control': 'no-cache', |
| 15 | + Connection: 'keep-alive', |
| 16 | + 'Transfer-Encoding': 'chunked', |
| 17 | + }); |
| 18 | + |
| 19 | + const responseId = `resp_${Date.now()}`; |
| 20 | + const itemId = `item_${Date.now()}`; |
| 21 | + let sequenceNumber = 0; |
| 22 | + |
| 23 | + // Send response.created event |
| 24 | + res.write( |
| 25 | + `data: ${JSON.stringify({ |
| 26 | + type: 'response.created', |
| 27 | + response: { |
| 28 | + id: responseId, |
| 29 | + object: 'realtime.response', |
| 30 | + status: 'in_progress', |
| 31 | + output: [], |
| 32 | + usage: { |
| 33 | + input_tokens: 0, |
| 34 | + output_tokens: 0, |
| 35 | + total_tokens: 0, |
| 36 | + }, |
| 37 | + }, |
| 38 | + sequence_number: sequenceNumber++, |
| 39 | + })}\n\n` |
| 40 | + ); |
| 41 | + |
| 42 | + // Send output_item.added event |
| 43 | + res.write( |
| 44 | + `data: ${JSON.stringify({ |
| 45 | + type: 'response.output_item.added', |
| 46 | + response_id: responseId, |
| 47 | + output_index: 0, |
| 48 | + item: { |
| 49 | + id: itemId, |
| 50 | + object: 'realtime.item', |
| 51 | + type: 'message', |
| 52 | + role: 'assistant', |
| 53 | + content: [], |
| 54 | + }, |
| 55 | + sequence_number: sequenceNumber++, |
| 56 | + })}\n\n` |
| 57 | + ); |
| 58 | + |
| 59 | + // Send the content in chunks |
| 60 | + const words = content.split(' '); |
| 61 | + let index = 0; |
| 62 | + |
| 63 | + const sendChunk = () => { |
| 64 | + if (index < words.length) { |
| 65 | + const word = words[index] + (index < words.length - 1 ? ' ' : ''); |
| 66 | + // Send output_text.delta event |
| 67 | + res.write( |
| 68 | + `data: ${JSON.stringify({ |
| 69 | + type: 'response.output_text.delta', |
| 70 | + response_id: responseId, |
| 71 | + item_id: itemId, |
| 72 | + output_index: 0, |
| 73 | + delta: word, |
| 74 | + sequence_number: sequenceNumber++, |
| 75 | + })}\n\n` |
| 76 | + ); |
| 77 | + index++; |
| 78 | + setTimeout(sendChunk, 10); |
| 79 | + } else { |
| 80 | + // Send output_item.done event |
| 81 | + res.write( |
| 82 | + `data: ${JSON.stringify({ |
| 83 | + type: 'response.output_item.done', |
| 84 | + response_id: responseId, |
| 85 | + output_index: 0, |
| 86 | + item: { |
| 87 | + id: itemId, |
| 88 | + object: 'realtime.item', |
| 89 | + type: 'message', |
| 90 | + role: 'assistant', |
| 91 | + content: [ |
| 92 | + { |
| 93 | + type: 'text', |
| 94 | + text: content, |
| 95 | + }, |
| 96 | + ], |
| 97 | + }, |
| 98 | + sequence_number: sequenceNumber++, |
| 99 | + })}\n\n` |
| 100 | + ); |
| 101 | + |
| 102 | + // Send response.completed event |
| 103 | + const tokenCount = Math.ceil(content.split(' ').length * 1.3); |
| 104 | + res.write( |
| 105 | + `data: ${JSON.stringify({ |
| 106 | + type: 'response.completed', |
| 107 | + response: { |
| 108 | + id: responseId, |
| 109 | + object: 'realtime.response', |
| 110 | + status: 'completed', |
| 111 | + output: [ |
| 112 | + { |
| 113 | + id: itemId, |
| 114 | + object: 'realtime.item', |
| 115 | + type: 'message', |
| 116 | + role: 'assistant', |
| 117 | + content: [ |
| 118 | + { |
| 119 | + type: 'text', |
| 120 | + text: content, |
| 121 | + }, |
| 122 | + ], |
| 123 | + }, |
| 124 | + ], |
| 125 | + usage: { |
| 126 | + input_tokens: 10, |
| 127 | + output_tokens: tokenCount, |
| 128 | + total_tokens: 10 + tokenCount, |
| 129 | + }, |
| 130 | + }, |
| 131 | + sequence_number: sequenceNumber++, |
| 132 | + })}\n\n` |
| 133 | + ); |
| 134 | + |
| 135 | + res.write('data: [DONE]\n\n'); |
| 136 | + res.end(); |
| 137 | + } |
| 138 | + }; |
| 139 | + |
| 140 | + sendChunk(); |
| 141 | +} |
| 142 | + |
| 143 | +export async function startMockAssistantServer( |
| 144 | + { |
| 145 | + response: _response, |
| 146 | + }: { |
| 147 | + response: MockAssistantResponse; |
| 148 | + } = { |
| 149 | + response: { |
| 150 | + status: 200, |
| 151 | + body: 'This is a test response from the AI assistant.', |
| 152 | + }, |
| 153 | + } |
| 154 | +): Promise<{ |
| 155 | + clearRequests: () => void; |
| 156 | + getResponse: () => MockAssistantResponse; |
| 157 | + setResponse: (response: MockAssistantResponse) => void; |
| 158 | + getRequests: () => { |
| 159 | + content: any; |
| 160 | + req: any; |
| 161 | + }[]; |
| 162 | + endpoint: string; |
| 163 | + server: http.Server; |
| 164 | + stop: () => Promise<void>; |
| 165 | +}> { |
| 166 | + let requests: { |
| 167 | + content: any; |
| 168 | + req: any; |
| 169 | + }[] = []; |
| 170 | + let response = _response; |
| 171 | + const server = http |
| 172 | + .createServer((req, res) => { |
| 173 | + // Only handle POST requests for chat completions |
| 174 | + if (req.method !== 'POST') { |
| 175 | + res.writeHead(404); |
| 176 | + return res.end('Not Found'); |
| 177 | + } |
| 178 | + |
| 179 | + let body = ''; |
| 180 | + req |
| 181 | + .setEncoding('utf8') |
| 182 | + .on('data', (chunk) => { |
| 183 | + body += chunk; |
| 184 | + }) |
| 185 | + .on('end', () => { |
| 186 | + let jsonObject; |
| 187 | + try { |
| 188 | + jsonObject = JSON.parse(body); |
| 189 | + } catch { |
| 190 | + res.writeHead(400); |
| 191 | + res.setHeader('Content-Type', 'application/json'); |
| 192 | + return res.end(JSON.stringify({ error: 'Invalid JSON' })); |
| 193 | + } |
| 194 | + |
| 195 | + requests.push({ |
| 196 | + req, |
| 197 | + content: jsonObject, |
| 198 | + }); |
| 199 | + |
| 200 | + if (response.status !== 200) { |
| 201 | + res.writeHead(response.status); |
| 202 | + res.setHeader('Content-Type', 'application/json'); |
| 203 | + return res.end(JSON.stringify({ error: response.body })); |
| 204 | + } |
| 205 | + |
| 206 | + // Send streaming response |
| 207 | + return sendStreamingResponse(res, response.body); |
| 208 | + }); |
| 209 | + }) |
| 210 | + .listen(0); |
| 211 | + await once(server, 'listening'); |
| 212 | + |
| 213 | + // address() returns either a string or AddressInfo. |
| 214 | + const address = server.address() as AddressInfo; |
| 215 | + |
| 216 | + const endpoint = `http://localhost:${address.port}`; |
| 217 | + |
| 218 | + async function stop() { |
| 219 | + server.close(); |
| 220 | + await once(server, 'close'); |
| 221 | + } |
| 222 | + |
| 223 | + function clearRequests() { |
| 224 | + requests = []; |
| 225 | + } |
| 226 | + |
| 227 | + function getRequests() { |
| 228 | + return requests; |
| 229 | + } |
| 230 | + |
| 231 | + function getResponse() { |
| 232 | + return response; |
| 233 | + } |
| 234 | + |
| 235 | + function setResponse(newResponse: MockAssistantResponse) { |
| 236 | + response = newResponse; |
| 237 | + } |
| 238 | + |
| 239 | + return { |
| 240 | + clearRequests, |
| 241 | + getRequests, |
| 242 | + endpoint, |
| 243 | + server, |
| 244 | + getResponse, |
| 245 | + setResponse, |
| 246 | + stop, |
| 247 | + }; |
| 248 | +} |
0 commit comments