-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
543 lines (486 loc) · 20.7 KB
/
server.js
File metadata and controls
543 lines (486 loc) · 20.7 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
"use strict";
const http = require("node:http");
const { randomUUID } = require("node:crypto");
const fs = require("node:fs");
const path = require("node:path");
const core = require("./src/core.js");
const {
tryParseJson,
requestNeedsBridge,
requestNeedsXmlBridge,
transformRequestForBridge,
buildAggregateFromChatCompletion,
buildChatCompletionFromBridge,
acceptNativeJson,
acceptNativeSSE,
sseLine,
createStreamingBridgeParser,
buildBridgeResultFromText,
getBridgeProtocol
} = core;
const LISTEN_HOST = process.env.PROXY_HOST || "127.0.0.1";
const LISTEN_PORT = Number(process.env.PROXY_PORT || "8787");
const UPSTREAM_BASE_URL = process.env.UPSTREAM_BASE_URL || "https://nano-gpt.com/api/v1";
const DEBUG_FLAG_FILE = path.join(__dirname, ".debug-logging");
const ENABLE_DEBUG_LOGS = process.env.NANO_PROXY_DEBUG === "1" || process.env.NANO_PROXY_DEBUG === "true" || fs.existsSync(DEBUG_FLAG_FILE);
const LOG_DIR = path.join(__dirname, "Logs");
if (ENABLE_DEBUG_LOGS) fs.mkdirSync(LOG_DIR, { recursive: true });
const SESSION_START = new Date().toISOString().replace(/[:.]/g, "-");
const SESSION_LOG = ENABLE_DEBUG_LOGS ? path.join(LOG_DIR, `session-${SESSION_START}.log`) : null;
let requestCounter = 0;
function log(text) {
if (!ENABLE_DEBUG_LOGS || !SESSION_LOG) return;
fs.appendFileSync(SESSION_LOG, text + "\n", "utf8");
}
function logSection(title, content) {
log(`--- ${title} ---`);
log(typeof content === "string" ? content : JSON.stringify(content, null, 2));
log("");
}
function nowStamp() {
return new Date().toISOString().replace(/[:.]/g, "-");
}
function redactHeaders(headersLike) {
const out = {};
for (const [key, value] of Object.entries(headersLike || {})) {
out[key] = /(authorization|api-key|x-api-key)/i.test(key) ? "[redacted]" : value;
}
return out;
}
function buildUpstreamUrl(requestPath) {
const base = UPSTREAM_BASE_URL.replace(/\/+$/, "");
const suffix = String(requestPath || "").replace(/^\/+/, "");
return `${base}/${suffix}`;
}
async function readRequestBody(req) {
const chunks = [];
for await (const chunk of req) chunks.push(Buffer.from(chunk));
return Buffer.concat(chunks);
}
function buildUpstreamHeaders(reqHeaders, bodyLength) {
const headers = new Headers();
for (const [key, value] of Object.entries(reqHeaders || {})) {
if (key.toLowerCase() === "host") continue;
if (Array.isArray(value)) headers.set(key, value.join(", "));
else if (value !== undefined) headers.set(key, value);
}
if (bodyLength !== undefined) headers.set("content-length", String(bodyLength));
return headers;
}
function copyResponseHeaders(upstreamHeaders, res, bodyLength) {
upstreamHeaders.forEach((value, key) => {
const lower = key.toLowerCase();
if (lower === "content-length" || lower === "content-encoding" || lower === "transfer-encoding") return;
res.setHeader(key, value);
});
if (bodyLength !== undefined) res.setHeader("content-length", String(bodyLength));
}
async function fetchUpstream(req, upstreamUrl, bodyBuffer) {
return fetch(upstreamUrl, {
method: req.method,
headers: buildUpstreamHeaders(req.headers, bodyBuffer.length),
body: ["GET", "HEAD"].includes(req.method) ? undefined : bodyBuffer
});
}
function buildInvalidBridgeRetryBuffer(rewrittenBody, protocol = "xml") {
const retryBody = JSON.parse(JSON.stringify(rewrittenBody));
retryBody.messages = Array.isArray(retryBody.messages) ? retryBody.messages.slice() : [];
const content = protocol === "object"
? "Your previous response was invalid because it contained no visible content or tool call. Return exactly one valid JSON turn object that matches the required bridge contract. Do not return an empty response."
: "Your previous response was invalid because it contained no visible content and no XML tool call. Do not return an empty response. If you need to act, emit the XML tool call now. If no tool is needed, provide a normal visible reply.";
retryBody.messages.push({ role: "system", content });
return Buffer.from(JSON.stringify(retryBody), "utf8");
}
async function proxyRequest(req, res) {
if (req.method === "GET" && req.url === "/health") {
const payload = { ok: true, mode: `${getBridgeProtocol()}-bridge`, port: LISTEN_PORT, upstream: UPSTREAM_BASE_URL, debugLogs: ENABLE_DEBUG_LOGS };
if (ENABLE_DEBUG_LOGS) payload.logDir = LOG_DIR;
const body = JSON.stringify(payload);
res.writeHead(200, { "content-type": "application/json", "content-length": Buffer.byteLength(body) });
res.end(body);
return;
}
requestCounter++;
const reqNum = requestCounter;
const requestId = `${nowStamp()}-${randomUUID().slice(0, 8)}`;
const upstreamUrl = buildUpstreamUrl(req.url);
const reqBuffer = await readRequestBody(req);
const reqText = reqBuffer.toString("utf8");
const parsed = tryParseJson(reqText);
const isJson = (req.headers["content-type"] || "").includes("application/json") && parsed.ok;
let upstreamBuffer = reqBuffer;
let bridgeMeta = null;
let attemptNativeFirst = false;
if (isJson && requestNeedsXmlBridge(parsed.value) && !requestNeedsBridge(parsed.value)) {
attemptNativeFirst = true;
}
log(`\n${"=".repeat(80)}`);
log(`REQUEST #${reqNum} | ${new Date().toISOString()} | ${req.method} ${req.url}`);
log(`${"=".repeat(80)}`);
if (ENABLE_DEBUG_LOGS) logSection("REQUEST HEADERS", redactHeaders(req.headers));
if (isJson && requestNeedsXmlBridge(parsed.value) && !attemptNativeFirst) {
bridgeMeta = transformRequestForBridge(parsed.value);
upstreamBuffer = Buffer.from(JSON.stringify(bridgeMeta.rewritten), "utf8");
log(`--- BRIDGE ACTIVE | Tools: [${bridgeMeta.toolNames.join(", ")}] ---\n`);
} else if (attemptNativeFirst) {
log(`--- NATIVE-FIRST ACTIVE | Model: ${parsed.value && parsed.value.model ? parsed.value.model : "(unknown)"} ---\n`);
}
let upstreamResponse = await fetchUpstream(req, upstreamUrl, upstreamBuffer);
let contentType = upstreamResponse.headers.get("content-type") || "";
if (attemptNativeFirst) {
let nativeSucceeded = false;
let bufferedBody = null;
let bufferedText = "";
try {
if (contentType.includes("text/event-stream")) {
bufferedText = await upstreamResponse.text();
bufferedBody = Buffer.from(bufferedText, "utf8");
nativeSucceeded = acceptNativeSSE(upstreamResponse.status, bufferedText);
} else if (contentType.includes("application/json")) {
bufferedText = await upstreamResponse.text();
bufferedBody = Buffer.from(bufferedText, "utf8");
const nativeParsed = tryParseJson(bufferedText);
nativeSucceeded = nativeParsed.ok && acceptNativeJson(upstreamResponse.status, nativeParsed.value);
} else if (upstreamResponse.status >= 200 && upstreamResponse.status < 300) {
bufferedBody = Buffer.from(await upstreamResponse.arrayBuffer());
nativeSucceeded = true;
}
} catch (_) {
nativeSucceeded = false;
}
log(`--- NATIVE-FIRST RESULT ---`);
log(nativeSucceeded ? "accepted" : "rejected; falling back to xml bridge");
log("");
if (nativeSucceeded) {
copyResponseHeaders(upstreamResponse.headers, res, bufferedBody ? bufferedBody.length : undefined);
res.writeHead(upstreamResponse.status);
if (bufferedBody) res.end(bufferedBody);
else res.end();
return;
}
bridgeMeta = transformRequestForBridge(parsed.value);
upstreamBuffer = Buffer.from(JSON.stringify(bridgeMeta.rewritten), "utf8");
log(`--- BRIDGE ACTIVE | Tools: [${bridgeMeta.toolNames.join(", ")}] ---\n`);
upstreamResponse = await fetchUpstream(req, upstreamUrl, upstreamBuffer);
contentType = upstreamResponse.headers.get("content-type") || "";
}
if (!upstreamResponse.ok) {
log(`--- UPSTREAM ERROR ${upstreamResponse.status} ${upstreamResponse.statusText} ---`);
try {
const errBody = await upstreamResponse.clone().text();
log(errBody.substring(0, 2000));
} catch (_) {}
log("");
}
if (bridgeMeta && upstreamResponse.ok && contentType.includes("text/event-stream")) {
res.writeHead(200, {
"content-type": "text/event-stream; charset=utf-8",
"cache-control": "no-cache, no-transform",
connection: "keep-alive"
});
const id = "chatcmpl_" + randomUUID();
let model = "nanoproxy";
let created = Math.floor(Date.now() / 1000);
const SSE_HEARTBEAT_INTERVAL_MS = 15000;
let lastSseWriteAt = Date.now();
const writeSse = (payload) => {
lastSseWriteAt = Date.now();
return res.write(sseLine(payload));
};
const writeSseComment = (text) => {
lastSseWriteAt = Date.now();
return res.write(`: ${text}\n\n`);
};
const heartbeatTimer = setInterval(() => {
if (res.destroyed || res.writableEnded) return;
if (Date.now() - lastSseWriteAt < SSE_HEARTBEAT_INTERVAL_MS) return;
try { writeSseComment("keepalive"); } catch (_) {}
}, SSE_HEARTBEAT_INTERVAL_MS);
if (typeof heartbeatTimer.unref === "function") heartbeatTimer.unref();
writeSse({
id, object: "chat.completion.chunk", created, model,
choices: [{ index: 0, delta: { role: "assistant" }, finish_reason: null }]
});
const STRIP_TAGS = ["<open>", "</open>"];
let stripBuf = "";
function emitContent(text) {
writeSse({
id, object: "chat.completion.chunk", created, model,
choices: [{ index: 0, delta: { content: text }, finish_reason: null }]
});
}
function feedContent(char) {
if (stripBuf.length > 0) {
stripBuf += char;
let couldMatch = false;
let exactMatch = false;
for (const tag of STRIP_TAGS) {
if (tag.startsWith(stripBuf)) couldMatch = true;
if (tag === stripBuf) exactMatch = true;
}
if (exactMatch) {
stripBuf = "";
} else if (!couldMatch) {
for (const c of stripBuf) emitContent(c);
stripBuf = "";
}
} else if (char === "<") {
stripBuf = "<";
} else {
emitContent(char);
}
}
function flushStripBuf() {
if (stripBuf.length > 0) {
let exactMatch = false;
for (const tag of STRIP_TAGS) {
if (tag === stripBuf) exactMatch = true;
}
if (!exactMatch) {
for (const c of stripBuf) emitContent(c);
}
stripBuf = "";
}
}
const retryBuffer = buildInvalidBridgeRetryBuffer(bridgeMeta.rewritten, bridgeMeta.protocol);
let activeResponse = upstreamResponse;
let activeContentType = contentType;
let finalToolIndex = 0;
let finalFinishReason = null;
let invalidNotice = null;
for (let attempt = 0; attempt < 2; attempt++) {
if (!(activeResponse.ok && activeContentType.includes("text/event-stream"))) break;
if (attempt > 0) {
log(`--- RETRY ATTEMPT #${attempt + 1} ---`);
log("");
}
const parser = createStreamingBridgeParser(bridgeMeta.normalizedTools, {
onContent: (text) => {
if (bridgeMeta.protocol === "object") emitContent(text);
else for (const c of text) feedContent(c);
},
onToolCall: (call, index) => {
flushStripBuf();
writeSse({
id, object: "chat.completion.chunk", created, model,
choices: [{
index: 0,
delta: {
tool_calls: [{
index,
id: call.id,
type: "function",
function: { name: call.function.name, arguments: call.function.arguments }
}]
},
finish_reason: null
}]
});
}
});
let rawContent = "";
let rawThinking = "";
let openTagCount = 0;
let buffer = "";
let upstreamFinishReason = null;
const decoder = new TextDecoder("utf-8");
const processSseBlock = (block) => {
const lines = String(block || "").split(/\r?\n/).filter((l) => l.startsWith("data:"));
for (const line of lines) {
const payloadStr = line.slice(5).trim();
if (!payloadStr || payloadStr === "[DONE]") continue;
const parsedPayload = tryParseJson(payloadStr);
if (!parsedPayload.ok) continue;
const payload = parsedPayload.value;
if (payload.model) model = payload.model;
if (payload.created) created = payload.created;
const choice = Array.isArray(payload.choices) ? payload.choices[0] : null;
if (!choice) continue;
if (choice.finish_reason != null) upstreamFinishReason = choice.finish_reason;
const delta = choice.delta || {};
if (delta.reasoning || delta.reasoning_content) {
rawThinking += (delta.reasoning || delta.reasoning_content);
writeSse({
id, object: "chat.completion.chunk", created, model,
choices: [{ index: 0, delta: { reasoning: delta.reasoning || delta.reasoning_content }, finish_reason: null }]
});
}
if (delta.content) {
rawContent += delta.content;
if (bridgeMeta.protocol === "xml") {
const openMatches = delta.content.match(/<open>/g);
if (openMatches) openTagCount += openMatches.length;
}
parser.feed(delta.content);
}
}
};
for await (const chunk of activeResponse.body) {
const textChunk = decoder.decode(chunk, { stream: true });
buffer += textChunk;
const parts = buffer.split(/\n\n+/);
buffer = parts.pop();
for (const block of parts) processSseBlock(block);
}
if (buffer.trim()) processSseBlock(buffer);
parser.flush();
flushStripBuf();
const bridgeResult = buildBridgeResultFromText(rawContent, rawThinking, bridgeMeta.normalizedTools);
if (rawThinking) {
log(`--- THINKING (${rawThinking.length} chars) ---`);
log(rawThinking.length > 500 ? rawThinking.substring(0, 500) + "\n... [truncated]" : rawThinking);
log("");
}
log(`--- CONTENT (${rawContent.length} chars) | <open> tags: ${openTagCount} ---`);
log(rawContent || "(empty)");
log("");
if (parser.completedCalls.length > 0) {
log(`--- TOOL CALLS: ${parser.completedCalls.length} ---`);
for (const tc of parser.completedCalls) {
const args = tc.function?.arguments || "";
const preview = args.length > 200 ? args.substring(0, 200) + "..." : args;
log(` [${tc.function?.name}] id=${tc.id} args=${preview}`);
}
log("");
} else {
log("--- NO TOOL CALLS ---\n");
}
log(`--- UPSTREAM FINISH REASON ---`);
log(String(upstreamFinishReason ?? "(none)"));
log("");
const recoveredCalls = bridgeResult.kind === "tool_calls" && Array.isArray(bridgeResult.message?.tool_calls)
? bridgeResult.message.tool_calls
: [];
if (bridgeResult.kind !== "invalid") {
if (bridgeMeta.protocol === "object" && !parser.messageEmitted && bridgeResult.message?.content) {
emitContent(bridgeResult.message.content);
}
if (bridgeMeta.protocol === "object" && recoveredCalls.length > parser.completedCalls.length) {
for (const [offset, call] of recoveredCalls.slice(parser.completedCalls.length).entries()) {
flushStripBuf();
writeSse({
id, object: "chat.completion.chunk", created, model,
choices: [{
index: 0,
delta: {
tool_calls: [{
index: parser.completedCalls.length + offset,
id: call.id,
type: "function",
function: { name: call.function.name, arguments: call.function.arguments }
}]
},
finish_reason: null
}]
});
}
}
}
if (bridgeResult.kind === "invalid") {
const notice = `[NanoProxy] Invalid bridged completion: upstream returned no visible content or tool call for a tool-enabled turn${upstreamFinishReason ? ` (finish_reason=${upstreamFinishReason})` : ""}.`;
if (parser.toolIndex === 0 && attempt === 0) {
log(`--- INVALID BRIDGE COMPLETION ---`);
log(`${notice} Retrying once.`);
log("");
activeResponse = await fetchUpstream(req, upstreamUrl, retryBuffer);
activeContentType = activeResponse.headers.get("content-type") || "";
continue;
}
if (parser.toolIndex === 0) {
log(`--- INVALID BRIDGE COMPLETION ---`);
log(notice);
log("");
invalidNotice = notice;
}
}
finalToolIndex = recoveredCalls.length || parser.toolIndex;
finalFinishReason = recoveredCalls.length > 0 ? "tool_calls" : (upstreamFinishReason || "stop");
break;
}
if (invalidNotice) emitContent(invalidNotice);
writeSse({
id, object: "chat.completion.chunk", created, model,
choices: [{ index: 0, delta: {}, finish_reason: finalFinishReason || "stop" }]
});
res.write("data: [DONE]\n\n");
clearInterval(heartbeatTimer);
res.end();
console.log(`[NanoProxy] #${reqNum} SSE done. Tool calls: ${finalToolIndex}${ENABLE_DEBUG_LOGS && SESSION_LOG ? ` | Log: ${path.basename(SESSION_LOG)}` : ""}`);
return;
}
if (bridgeMeta && upstreamResponse.ok && contentType.includes("application/json")) {
const retryBuffer = buildInvalidBridgeRetryBuffer(bridgeMeta.rewritten, bridgeMeta.protocol);
let activeResponse = upstreamResponse;
for (let attempt = 0; attempt < 2; attempt++) {
const upstreamText = await activeResponse.text();
const upstreamJson = tryParseJson(upstreamText);
if (!upstreamJson.ok) break;
const aggregate = buildAggregateFromChatCompletion(upstreamJson.value);
const bridgeResult = buildBridgeResultFromText(aggregate.content, aggregate.reasoning, bridgeMeta.normalizedTools);
if (bridgeResult.kind === "invalid" && attempt === 0) {
logSection("RAW MODEL OUTPUT", aggregate.content);
logSection("INVALID BRIDGE COMPLETION", { retrying: true, upstream_finish_reason: aggregate.finishReason });
activeResponse = await fetchUpstream(req, upstreamUrl, retryBuffer);
continue;
}
if (bridgeResult.kind === "invalid") {
const errorPayload = { error: { code: bridgeResult.error.code, message: bridgeResult.error.message, upstream_finish_reason: aggregate.finishReason } };
logSection("RAW MODEL OUTPUT", aggregate.content);
logSection("INVALID BRIDGE COMPLETION", errorPayload);
const buf = Buffer.from(JSON.stringify(errorPayload), "utf8");
res.writeHead(502, { "content-type": "application/json; charset=utf-8", "content-length": String(buf.length) });
res.end(buf);
return;
}
const translated = buildChatCompletionFromBridge(aggregate, bridgeMeta.normalizedTools);
logSection("RAW MODEL OUTPUT", aggregate.content);
logSection("TRANSLATED RESPONSE", translated);
const buf = Buffer.from(JSON.stringify(translated), "utf8");
res.writeHead(200, { "content-type": "application/json; charset=utf-8", "content-length": String(buf.length) });
res.end(buf);
return;
}
}
if (contentType.includes("text/event-stream")) {
res.writeHead(upstreamResponse.status, {
"content-type": "text/event-stream; charset=utf-8",
"cache-control": "no-cache, no-transform",
connection: "keep-alive"
});
for await (const chunk of upstreamResponse.body) res.write(chunk);
res.end();
log(`--- PASSTHROUGH SSE (status ${upstreamResponse.status}) ---\n`);
return;
}
const rawBuffer = Buffer.from(await upstreamResponse.arrayBuffer());
copyResponseHeaders(upstreamResponse.headers, res, rawBuffer.length);
res.writeHead(upstreamResponse.status);
res.end(rawBuffer);
log(`--- PASSTHROUGH (status ${upstreamResponse.status}) ---\n`);
}
function startServer() {
const server = http.createServer((req, res) => {
proxyRequest(req, res).catch((error) => {
log(`--- ERROR ---\n${error && error.stack ? error.stack : error}\n`);
console.error("[NanoProxy Error]", error);
if (!res.headersSent) {
res.writeHead(502, { "content-type": "application/json; charset=utf-8" });
res.end(JSON.stringify({ error: { message: String(error && error.message ? error.message : error) } }));
} else {
res.end();
}
});
});
server.listen(LISTEN_PORT, LISTEN_HOST, () => {
console.log(`NanoProxy listening on http://${LISTEN_HOST}:${LISTEN_PORT}`);
if (ENABLE_DEBUG_LOGS && SESSION_LOG) console.log(`Session log: ${SESSION_LOG}`);
});
return server;
}
if (require.main === module) {
startServer();
}
module.exports = {
startServer,
...core
};