Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion proxy/model-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Transform } from 'stream';
const ANTHROPIC_FALLBACK = 'https://api.anthropic.com';
const MODEL_PATHS = ['/v1/messages'];
const REQUEST_TIMEOUT_MS = 5 * 60 * 1000; // 5 min per request
const MAX_BODY_SIZE = 50 * 1024 * 1024; // 50 MB

const PRICING_PER_M = {
deepseek: { input: 0.44, output: 0.87 },
Expand Down Expand Up @@ -274,8 +275,20 @@ export function startModelProxy({ targetUrl, apiKey, startPort = 3200, backends,
}

const chunks = [];
clientReq.on('data', c => chunks.push(c));
let bodySize = 0;
clientReq.on('data', c => {
bodySize += c.length;
if (bodySize > MAX_BODY_SIZE) {
console.error(`[MODEL-PROXY] #${reqCount} BODY_TOO_LARGE (${bodySize} bytes)`);
clientRes.writeHead(413, { 'content-type': 'application/json' });
clientRes.end(JSON.stringify({ error: { message: 'Request body too large' } }));
clientReq.destroy();
return;
}
chunks.push(c);
});
clientReq.on('end', () => {
if (bodySize > MAX_BODY_SIZE) return;
const body = Buffer.concat(chunks);
const opts = {
hostname: dest.hostname,
Expand Down