-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.mjs
More file actions
566 lines (508 loc) · 19.5 KB
/
server.mjs
File metadata and controls
566 lines (508 loc) · 19.5 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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
import express from "express";
import crypto from "crypto";
import https from "https";
import fs from "fs";
const app = express();
app.use(express.json({ limit: "10mb" }));
const PORT = process.env.PROXY_PORT || 6446;
const OC_VERSION = "1.15.0";
const PROXY_VERSION = "9";
// ── API Keys ───────────────────────────────────────────────────────
const keysFile = process.env.KEYS_FILE || "./api-keys.json";
let apiKeys = {};
function loadKeys() {
try { apiKeys = JSON.parse(fs.readFileSync(keysFile, "utf8")); } catch {}
if (Object.keys(apiKeys).length === 0) {
apiKeys = {
admin: "oc-" + crypto.randomBytes(20).toString("hex"),
"user-default": "oc-" + crypto.randomBytes(20).toString("hex"),
};
fs.writeFileSync(keysFile, JSON.stringify(apiKeys, null, 2));
console.log("[INIT] Generated new API keys →", keysFile);
}
}
loadKeys();
function auth(req) {
const hdr = req.headers.authorization || req.headers["x-api-key"] || "";
const tok = hdr.startsWith("Bearer ") ? hdr.slice(7) : hdr;
for (const [name, key] of Object.entries(apiKeys)) {
if (tok === key) return name;
}
return null;
}
// ── Helpers ────────────────────────────────────────────────────────
function ocId(prefix) {
const ts = Date.now().toString(16);
const rnd = crypto.randomBytes(12).toString("base64url").slice(0, 16);
return `${prefix}_${ts}${rnd}`;
}
const MODELS = [
"deepseek-v4-flash-free",
"big-pickle",
"minimax-m2.5-free",
"nemotron-3-super-free",
"qwen3.6-plus-free",
];
// Track sessions per user (rotate every 30 min)
const userSessions = {};
function getSession(user) {
const now = Date.now();
if (!userSessions[user] || now - userSessions[user].ts > 30 * 60 * 1000) {
userSessions[user] = { id: ocId("ses"), ts: now };
}
return userSessions[user].id;
}
// ── Zen API transport ──────────────────────────────────────────────
function zenRequest(model, messages, stream, tools, tool_choice, sessionId) {
const reqBody = { model, messages, stream: !!stream };
if (tools?.length) reqBody.tools = tools;
if (tool_choice) reqBody.tool_choice = tool_choice;
const body = JSON.stringify(reqBody);
const requestId = ocId("msg");
return {
body,
options: {
hostname: "opencode.ai",
port: 443,
path: "/zen/v1/chat/completions",
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(body),
"Authorization": "Bearer public",
"User-Agent": `opencode/${OC_VERSION} ai-sdk/provider-utils/4.0.23 runtime/bun/1.3.13`,
"x-opencode-client": "cli",
"x-opencode-project": "global",
"x-opencode-request": requestId,
"x-opencode-session": sessionId,
},
timeout: 120000,
},
};
}
// Pipe Zen response to client (OpenAI format passthrough)
function pipeZenResponse(zenOpts, body, stream, res) {
const req = https.request(zenOpts, (zenRes) => {
let firstChunk = null;
let headersSent = false;
zenRes.on("data", (chunk) => {
if (!firstChunk) {
firstChunk = chunk;
const str = chunk.toString().trim();
if (str.startsWith("{") && (str.includes("FreeUsageLimitError") || str.includes('"error"'))) {
try {
const parsed = JSON.parse(str);
if (parsed.error || parsed.type === "error") {
const errMsg = parsed.error?.message || parsed.message || "Rate limit exceeded";
console.log("[ZEN RATE LIMITED]", errMsg);
if (!res.headersSent) {
res.status(429).json({
error: { message: errMsg + " (free model rate limit)", type: "rate_limit_error", code: "rate_limit_exceeded" }
});
}
zenRes.resume();
return;
}
} catch {}
}
headersSent = true;
if (stream) {
res.writeHead(200, {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache, no-transform",
"Connection": "keep-alive",
"X-Accel-Buffering": "no",
"Transfer-Encoding": "chunked",
});
res.flushHeaders();
} else {
res.writeHead(zenRes.statusCode, { "Content-Type": "application/json" });
}
res.write(firstChunk);
if (res.flush) res.flush();
return;
}
if (headersSent) {
res.write(chunk);
if (res.flush) res.flush();
}
});
zenRes.on("end", () => {
if (!headersSent && !firstChunk) {
console.log("[ZEN EMPTY] No response from Zen API");
if (!res.headersSent) {
res.status(502).json({ error: { message: "Empty response from upstream", type: "upstream_error" } });
}
return;
}
if (headersSent) res.end();
});
});
req.on("error", (e) => {
console.log("[ZEN ERROR]", e.message);
if (!res.headersSent) {
res.status(502).json({ error: { message: "Upstream error: " + e.message, type: "upstream_error" } });
}
});
req.on("timeout", () => {
req.destroy();
console.log("[ZEN TIMEOUT]");
if (!res.headersSent) {
res.status(504).json({ error: { message: "Upstream timeout", type: "timeout_error" } });
}
});
req.write(body);
req.end();
}
// Collect full Zen response (non-streaming) and return parsed JSON
function zenRequestFull(zenOpts, body) {
return new Promise((resolve, reject) => {
const req = https.request(zenOpts, (zenRes) => {
const chunks = [];
zenRes.on("data", (c) => chunks.push(c));
zenRes.on("end", () => {
const raw = Buffer.concat(chunks).toString();
try {
resolve({ status: zenRes.statusCode, data: JSON.parse(raw), raw });
} catch {
resolve({ status: zenRes.statusCode, data: null, raw });
}
});
});
req.on("error", reject);
req.on("timeout", () => { req.destroy(); reject(new Error("timeout")); });
req.write(body);
req.end();
});
}
// ── Anthropic Messages → OpenAI conversion ─────────────────────────
function anthropicToOpenAI(body) {
const messages = [];
if (body.system) {
const sys = typeof body.system === "string" ? body.system
: Array.isArray(body.system) ? body.system.map(b => b.text || "").join("\n") : "";
if (sys) messages.push({ role: "system", content: sys });
}
for (const msg of body.messages || []) {
if (typeof msg.content === "string") {
messages.push({ role: msg.role, content: msg.content });
} else if (Array.isArray(msg.content)) {
const text = msg.content
.filter(b => b.type === "text")
.map(b => b.text)
.join("\n");
// tool_use blocks → assistant tool_calls
const toolUses = msg.content.filter(b => b.type === "tool_use");
if (toolUses.length && msg.role === "assistant") {
messages.push({
role: "assistant",
content: text || null,
tool_calls: toolUses.map(t => ({
id: t.id,
type: "function",
function: { name: t.name, arguments: JSON.stringify(t.input || {}) },
})),
});
} else if (msg.content.some(b => b.type === "tool_result")) {
for (const b of msg.content.filter(b => b.type === "tool_result")) {
const resultText = typeof b.content === "string" ? b.content
: Array.isArray(b.content) ? b.content.map(c => c.text || "").join("\n") : "";
messages.push({ role: "tool", tool_call_id: b.tool_use_id, content: resultText });
}
} else {
messages.push({ role: msg.role, content: text });
}
}
}
const tools = (body.tools || []).map(t => ({
type: "function",
function: {
name: t.name,
description: t.description || "",
parameters: t.input_schema || {},
},
}));
return { messages, tools: tools.length ? tools : undefined };
}
// OpenAI response → Anthropic Messages format
function openAIToAnthropic(oaiResp, model, inputTokens) {
const choice = oaiResp.choices?.[0];
if (!choice) {
return {
id: ocId("msg"),
type: "message",
role: "assistant",
content: [{ type: "text", text: "" }],
model,
stop_reason: "end_turn",
usage: { input_tokens: inputTokens || 0, output_tokens: 0, cache_creation_input_tokens: 0, cache_read_input_tokens: 0 },
};
}
const content = [];
if (choice.message?.content) {
content.push({ type: "text", text: choice.message.content });
}
if (choice.message?.tool_calls) {
for (const tc of choice.message.tool_calls) {
let input = {};
try { input = JSON.parse(tc.function.arguments); } catch {}
content.push({
type: "tool_use",
id: tc.id || ocId("toolu"),
name: tc.function.name,
input,
});
}
}
if (!content.length) content.push({ type: "text", text: "" });
let stopReason = "end_turn";
if (choice.finish_reason === "tool_calls") stopReason = "tool_use";
else if (choice.finish_reason === "length") stopReason = "max_tokens";
else if (choice.finish_reason === "stop") stopReason = "end_turn";
return {
id: ocId("msg"),
type: "message",
role: "assistant",
content,
model,
stop_reason: stopReason,
usage: {
input_tokens: oaiResp.usage?.prompt_tokens || inputTokens || 0,
output_tokens: oaiResp.usage?.completion_tokens || 0,
cache_creation_input_tokens: 0,
cache_read_input_tokens: 0,
},
};
}
// Stream OpenAI SSE → Anthropic SSE
function pipeZenAsAnthropic(zenOpts, body, model, res, inputTokens) {
const msgId = ocId("msg");
const req = https.request(zenOpts, (zenRes) => {
let headersSent = false;
let buffer = "";
let outputTokens = 0;
let contentIdx = 0;
let toolIdx = -1;
let firstChunkHandled = false;
function sendSSE(event, data) {
res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`);
if (res.flush) res.flush();
}
function sendHeaders() {
if (headersSent) return;
headersSent = true;
res.writeHead(200, {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache, no-transform",
"Connection": "keep-alive",
"X-Accel-Buffering": "no",
});
res.flushHeaders();
sendSSE("message_start", {
type: "message_start",
message: {
id: msgId, type: "message", role: "assistant", content: [],
model, stop_reason: null,
usage: { input_tokens: inputTokens || 0, output_tokens: 0, cache_creation_input_tokens: 0, cache_read_input_tokens: 0 },
},
});
}
zenRes.on("data", (chunk) => {
const str = chunk.toString();
// Check for errors on first chunk
if (!firstChunkHandled) {
firstChunkHandled = true;
const trimmed = str.trim();
if (trimmed.startsWith("{") && (trimmed.includes("FreeUsageLimitError") || trimmed.includes('"error"'))) {
try {
const parsed = JSON.parse(trimmed);
if (parsed.error || parsed.type === "error") {
const errMsg = parsed.error?.message || parsed.message || "Rate limit";
if (!res.headersSent) {
res.writeHead(429, { "Content-Type": "application/json" });
res.end(JSON.stringify({
type: "error",
error: { type: "rate_limit_error", message: errMsg + " (free model rate limit)" },
}));
}
zenRes.resume();
return;
}
} catch {}
}
}
buffer += str;
const lines = buffer.split("\n");
buffer = lines.pop() || "";
for (const line of lines) {
if (!line.startsWith("data: ")) continue;
const payload = line.slice(6).trim();
if (payload === "[DONE]") continue;
let parsed;
try { parsed = JSON.parse(payload); } catch { continue; }
const delta = parsed.choices?.[0]?.delta;
if (!delta) continue;
sendHeaders();
// Text content
if (delta.content) {
if (contentIdx === 0 && toolIdx === -1) {
sendSSE("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } });
contentIdx = 1;
}
sendSSE("content_block_delta", {
type: "content_block_delta", index: 0,
delta: { type: "text_delta", text: delta.content },
});
outputTokens += Math.ceil(delta.content.length / 4);
}
// Tool calls
if (delta.tool_calls) {
for (const tc of delta.tool_calls) {
const idx = tc.index ?? 0;
if (idx > toolIdx) {
// Close previous text block if open
if (toolIdx === -1 && contentIdx > 0) {
sendSSE("content_block_stop", { type: "content_block_stop", index: 0 });
}
toolIdx = idx;
const blockIdx = contentIdx > 0 ? idx + 1 : idx;
sendSSE("content_block_start", {
type: "content_block_start", index: blockIdx,
content_block: { type: "tool_use", id: tc.id || ocId("toolu"), name: tc.function?.name || "" },
});
}
if (tc.function?.arguments) {
const blockIdx = contentIdx > 0 ? idx + 1 : idx;
sendSSE("content_block_delta", {
type: "content_block_delta", index: blockIdx,
delta: { type: "input_json_delta", partial_json: tc.function.arguments },
});
outputTokens += Math.ceil(tc.function.arguments.length / 4);
}
}
}
// Finish
if (parsed.choices?.[0]?.finish_reason) {
const fr = parsed.choices[0].finish_reason;
// Close open blocks
const totalBlocks = (contentIdx > 0 ? 1 : 0) + (toolIdx >= 0 ? toolIdx + 1 : 0);
for (let i = 0; i < totalBlocks; i++) {
sendSSE("content_block_stop", { type: "content_block_stop", index: i });
}
let stopReason = "end_turn";
if (fr === "tool_calls") stopReason = "tool_use";
else if (fr === "length") stopReason = "max_tokens";
sendSSE("message_delta", {
type: "message_delta",
delta: { stop_reason: stopReason },
usage: { output_tokens: outputTokens },
});
sendSSE("message_stop", { type: "message_stop" });
}
}
});
zenRes.on("end", () => {
if (!headersSent) {
if (!res.headersSent) {
res.status(502).json({ type: "error", error: { type: "upstream_error", message: "Empty response" } });
}
return;
}
res.end();
});
});
req.on("error", (e) => {
console.log("[ZEN ERROR]", e.message);
if (!res.headersSent) {
res.status(502).json({ type: "error", error: { type: "upstream_error", message: e.message } });
}
});
req.on("timeout", () => {
req.destroy();
if (!res.headersSent) {
res.status(504).json({ type: "error", error: { type: "timeout_error", message: "Upstream timeout" } });
}
});
req.write(body);
req.end();
}
// ── Routes: OpenAI format ──────────────────────────────────────────
app.get("/v1/models", (_req, res) => {
res.json({
object: "list",
data: MODELS.map((id) => ({
id, object: "model", created: 1779000000, owned_by: "opencode-free",
})),
});
});
app.post("/v1/chat/completions", (req, res) => {
const user = auth(req);
if (!user) return res.status(401).json({ error: { message: "Invalid API key" } });
const { model, messages, stream, tools, tool_choice } = req.body;
if (!MODELS.includes(model)) {
return res.status(400).json({ error: { message: `Unknown model: ${model}. Available: ${MODELS.join(", ")}` } });
}
const sessionId = getSession(user);
const msgSummary = (messages || []).map(m => ({ role: m.role, len: (typeof m.content === "string" ? m.content : JSON.stringify(m.content || "")).length }));
console.log("[OAI]", new Date().toISOString(), user, model, stream ? "stream" : "sync", "msgs:", JSON.stringify(msgSummary));
const { body, options } = zenRequest(model, messages, stream, tools, tool_choice, sessionId);
pipeZenResponse(options, body, stream, res);
});
// ── Routes: Anthropic Messages format ──────────────────────────────
app.post("/v1/messages", async (req, res) => {
const user = auth(req);
if (!user) {
return res.status(401).json({ type: "error", error: { type: "authentication_error", message: "Invalid API key" } });
}
const { model, stream } = req.body;
if (!MODELS.includes(model)) {
return res.status(400).json({
type: "error",
error: { type: "invalid_request_error", message: `Unknown model: ${model}. Available: ${MODELS.join(", ")}` },
});
}
const sessionId = getSession(user);
const { messages, tools } = anthropicToOpenAI(req.body);
const inputTokens = JSON.stringify(messages).length / 4 | 0;
console.log("[ANT]", new Date().toISOString(), user, model, stream ? "stream" : "sync", "msgs:", messages.length);
const { body, options } = zenRequest(model, messages, stream, tools, undefined, sessionId);
if (stream) {
pipeZenAsAnthropic(options, body, model, res, inputTokens);
} else {
try {
const zenResp = await zenRequestFull(options, body);
if (zenResp.status === 429 || zenResp.data?.error) {
const errMsg = zenResp.data?.error?.message || "Rate limit exceeded";
return res.status(429).json({
type: "error", error: { type: "rate_limit_error", message: errMsg + " (free model rate limit)" },
});
}
if (!zenResp.data?.choices) {
return res.status(502).json({
type: "error", error: { type: "upstream_error", message: "Invalid upstream response" },
});
}
res.json(openAIToAnthropic(zenResp.data, model, inputTokens));
} catch (e) {
console.log("[ZEN ERROR]", e.message);
res.status(502).json({ type: "error", error: { type: "upstream_error", message: e.message } });
}
}
});
// ── Health ──────────────────────────────────────────────────────────
app.get("/health", (_req, res) => res.json({
status: "ok", version: `v${PROXY_VERSION}`, models: MODELS.length,
endpoints: ["/v1/chat/completions", "/v1/messages", "/v1/models"],
}));
// ── Start ──────────────────────────────────────────────────────────
app.listen(PORT, "0.0.0.0", () => {
console.log(`OpenCode Free Proxy v${PROXY_VERSION} on http://0.0.0.0:${PORT}`);
console.log(" OpenAI: POST /v1/chat/completions");
console.log(" Anthropic: POST /v1/messages");
console.log(" Models: GET /v1/models");
console.log(" Health: GET /health");
console.log(" Models:", MODELS.join(", "));
for (const [name, key] of Object.entries(apiKeys)) {
console.log(` ${name.padEnd(15)} ${key}`);
}
});