MCPcopy Index your code
hub / github.com/AI45Lab/Code / startFakeOpenAiServer

Function startFakeOpenAiServer

scripts/docs_api_contract_smoke.mjs:21–96  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

19} from '../sdk/node/index.js';
20
21function startFakeOpenAiServer() {
22 const requests = [];
23 const server = http.createServer((req, res) => {
24 let body = '';
25 req.setEncoding('utf8');
26 req.on('data', chunk => {
27 body += chunk;
28 });
29 req.on('end', () => {
30 const parsed = body ? JSON.parse(body) : {};
31 requests.push(parsed);
32
33 if (req.url !== '/v1/chat/completions') {
34 res.writeHead(404);
35 res.end('not found');
36 return;
37 }
38
39 if (parsed.stream) {
40 res.writeHead(200, {
41 'content-type': 'text/event-stream',
42 'cache-control': 'no-cache',
43 connection: 'keep-alive',
44 });
45 res.write(
46 `data: ${JSON.stringify({
47 id: 'chatcmpl-docs-stream',
48 object: 'chat.completion.chunk',
49 model: parsed.model,
50 choices: [{ index: 0, delta: { content: 'docs smoke stream ok' }, finish_reason: null }],
51 })}\n\n`,
52 );
53 res.write(
54 `data: ${JSON.stringify({
55 id: 'chatcmpl-docs-stream',
56 object: 'chat.completion.chunk',
57 model: parsed.model,
58 choices: [{ index: 0, delta: {}, finish_reason: 'stop' }],
59 usage: { prompt_tokens: 3, completion_tokens: 2, total_tokens: 5 },
60 })}\n\n`,
61 );
62 res.end('data: [DONE]\n\n');
63 return;
64 }
65
66 res.writeHead(200, { 'content-type': 'application/json' });
67 res.end(
68 JSON.stringify({
69 id: 'chatcmpl-docs',
70 object: 'chat.completion',
71 created: Math.floor(Date.now() / 1000),
72 model: parsed.model,
73 choices: [
74 {
75 index: 0,
76 message: { role: 'assistant', content: `docs smoke response ${requests.length}` },
77 finish_reason: 'stop',
78 },

Callers 1

Calls 3

writeMethod · 0.80
parseMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected