Repository navigation
Expand file tree
/
Copy pathproxy.mjs
More file actions
104 lines (99 loc) · 3.87 KB
/
Copy pathproxy.mjs
File metadata and controls
104 lines (99 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Logging pass-through proxy for an OpenAI-compatible API.
// Usage: node proxy.mjs <port> <run-dir>
// Forwards every request to https://api.commandcode.ai unchanged, streams the
// response back unchanged, and records per request: timings, status, usage,
// errors (calls.ndjson) plus the full request and response bodies (bodies/).
import fs from 'node:fs'
import http from 'node:http'
import path from 'node:path'
import zlib from 'node:zlib'
import { createRequire } from 'node:module'
const { fetch, EnvHttpProxyAgent } = createRequire(import.meta.url)('undici')
const UPSTREAM = 'https://api.commandcode.ai'
const [port, dir] = process.argv.slice(2)
fs.mkdirSync(path.join(dir, 'bodies'), { recursive: true })
const log = fs.createWriteStream(path.join(dir, 'calls.ndjson'), { flags: 'a' })
const dispatcher = new EnvHttpProxyAgent({ bodyTimeout: 0, headersTimeout: 0 })
let seq = 0
const readBody = (req) =>
new Promise((resolve, reject) => {
const chunks = []
req.on('data', (c) => chunks.push(c))
req.on('end', () => resolve(Buffer.concat(chunks)))
req.on('error', reject)
})
const hasContent = (line) => {
if (!line.startsWith('data:') || line.includes('[DONE]')) return false
try {
const d = JSON.parse(line.slice(5)).choices?.[0]?.delta
return Boolean(d && (d.content || d.reasoning_content || d.reasoning || d.tool_calls))
} catch {
return false
}
}
const usageOf = (line) => {
if (!line.startsWith('data:') || line.includes('[DONE]')) return null
try {
return JSON.parse(line.slice(5)).usage ?? null
} catch {
return null
}
}
const server = http.createServer(async (req, res) => {
const id = String(++seq).padStart(5, '0')
const rec = { id, tStart: Date.now(), method: req.method, path: req.url }
const body = await readBody(req)
fs.writeFileSync(path.join(dir, 'bodies', `${id}.req.gz`), zlib.gzipSync(body))
try {
const j = JSON.parse(body.toString() || '{}')
Object.assign(rec, { model: j.model, stream: j.stream, effort: j.reasoning_effort, thinking: j.thinking, messages: j.messages?.length, reqBytes: body.length })
} catch {}
let clientGone = false
res.on('close', () => {
if (!res.writableFinished) clientGone = true
})
const headers = { ...req.headers }
delete headers.host
delete headers['content-length']
delete headers['accept-encoding']
const resChunks = []
try {
const up = await fetch(UPSTREAM + req.url, { method: req.method, headers, body: ['GET', 'HEAD'].includes(req.method) ? undefined : body, dispatcher })
rec.status = up.status
rec.tHeaders = Date.now()
const outHeaders = Object.fromEntries([...up.headers].filter(([k]) => !['content-encoding', 'content-length', 'transfer-encoding', 'connection'].includes(k)))
res.writeHead(up.status, outHeaders)
let tail = ''
for await (const chunk of up.body) {
const buf = Buffer.from(chunk)
resChunks.push(buf)
if (!clientGone) res.write(buf)
const text = tail + buf.toString()
const lines = text.split('\n')
tail = lines.pop()
for (const line of lines) {
if (!rec.tFirst && hasContent(line)) rec.tFirst = Date.now()
const u = usageOf(line)
if (u) rec.usage = u
}
}
if (tail && !rec.usage) {
try { rec.usage = JSON.parse(tail).usage } catch {}
}
res.end()
} catch (e) {
rec.error = String(e?.cause?.code || e?.code || e?.message || e)
if (!res.headersSent) res.writeHead(502, { 'content-type': 'application/json' })
res.end(JSON.stringify({ error: { message: `proxy: ${rec.error}` } }))
}
rec.tEnd = Date.now()
if (clientGone) rec.clientAborted = true
const all = Buffer.concat(resChunks)
rec.resBytes = all.length
if (rec.status >= 400) rec.errorBody = all.toString().slice(0, 2000)
fs.writeFileSync(path.join(dir, 'bodies', `${id}.res.gz`), zlib.gzipSync(all))
log.write(JSON.stringify(rec) + '\n')
})
server.requestTimeout = 0
server.headersTimeout = 0
server.listen(Number(port), '127.0.0.1', () => console.log(`proxy ${port} -> ${UPSTREAM}`))