-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.mjs
More file actions
477 lines (428 loc) · 13.7 KB
/
server.mjs
File metadata and controls
477 lines (428 loc) · 13.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
import http from "node:http";
import { randomUUID } from "node:crypto";
const env = process.env;
const HOST = env.HOST || "127.0.0.1";
const PORT = Number(env.PORT || 8787);
const BRIDGE_API_KEY = env.BRIDGE_API_KEY || "";
const OPENAI_MODEL_ID = env.OPENAI_MODEL_ID || "opencode-local";
const OPENCODE_BASE_URL = (env.OPENCODE_BASE_URL || "http://127.0.0.1:4096").replace(/\/+$/, "");
const OPENCODE_AUTH_MODE = (env.OPENCODE_AUTH_MODE || "basic").toLowerCase();
const OPENCODE_AUTH_USERNAME = env.OPENCODE_AUTH_USERNAME || "opencode";
const OPENCODE_AUTH_PASSWORD = env.OPENCODE_AUTH_PASSWORD || env.OPENCODE_SERVER_PASSWORD || "";
const OPENCODE_DIRECTORY = env.OPENCODE_DIRECTORY || "";
const DEFAULT_PROVIDER_ID = env.OPENCODE_PROVIDER_ID || "";
const DEFAULT_MODEL_ID = env.OPENCODE_MODEL_ID || "";
const DEFAULT_AGENT = env.OPENCODE_AGENT || "";
const DEFAULT_SYSTEM = env.OPENCODE_SYSTEM || "";
const OPENCODE_SESSION_TIMEOUT_MS = Number(env.OPENCODE_SESSION_TIMEOUT_MS || 15000);
const OPENCODE_MESSAGE_TIMEOUT_MS = Number(env.OPENCODE_MESSAGE_TIMEOUT_MS || 60000);
const OPENCODE_RETRY_COUNT = Number(env.OPENCODE_RETRY_COUNT || 1);
const OPENCODE_RETRY_DELAY_MS = Number(env.OPENCODE_RETRY_DELAY_MS || 750);
let modelMap = {};
if (env.MODEL_MAP_JSON) {
try {
modelMap = JSON.parse(env.MODEL_MAP_JSON);
} catch (error) {
console.error("Invalid MODEL_MAP_JSON:", error.message);
process.exit(1);
}
}
function json(res, status, body) {
const text = JSON.stringify(body);
res.writeHead(status, {
"Content-Type": "application/json; charset=utf-8",
"Content-Length": Buffer.byteLength(text),
});
res.end(text);
}
function sse(res) {
res.writeHead(200, {
"Content-Type": "text/event-stream; charset=utf-8",
"Cache-Control": "no-cache, no-transform",
Connection: "keep-alive",
"X-Accel-Buffering": "no",
});
}
function readBody(req, maxBytes = 2 * 1024 * 1024) {
return new Promise((resolve, reject) => {
let size = 0;
const chunks = [];
req.on("data", (chunk) => {
size += chunk.length;
if (size > maxBytes) {
reject(new Error("Request body too large"));
req.destroy();
return;
}
chunks.push(chunk);
});
req.on("end", () => {
resolve(Buffer.concat(chunks).toString("utf8"));
});
req.on("error", reject);
});
}
function unauthorized(res, message = "Unauthorized") {
return json(res, 401, { error: { message, type: "invalid_request_error" } });
}
function requireBridgeAuth(req, res) {
if (!BRIDGE_API_KEY) return true;
const auth = req.headers.authorization || "";
const token = auth.startsWith("Bearer ") ? auth.slice(7).trim() : "";
if (token !== BRIDGE_API_KEY) {
unauthorized(res);
return false;
}
return true;
}
function opencodeHeaders(extra = {}) {
const headers = { ...extra };
if (OPENCODE_AUTH_PASSWORD) {
if (OPENCODE_AUTH_MODE === "bearer") {
headers.Authorization = `Bearer ${OPENCODE_AUTH_PASSWORD}`;
} else if (OPENCODE_AUTH_MODE === "basic") {
const raw = `${OPENCODE_AUTH_USERNAME}:${OPENCODE_AUTH_PASSWORD}`;
headers.Authorization = `Basic ${Buffer.from(raw, "utf8").toString("base64")}`;
}
}
return headers;
}
function opencodeUrl(pathname) {
const url = new URL(pathname, `${OPENCODE_BASE_URL}/`);
if (OPENCODE_DIRECTORY) {
url.searchParams.set("directory", OPENCODE_DIRECTORY);
}
return url;
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function parseResponseBody(text) {
let data = null;
if (text) {
try {
data = JSON.parse(text);
} catch {
data = text;
}
}
return data;
}
function isAbortLikeError(error) {
return error?.name === "AbortError" || error?.code === "ABORT_ERR";
}
async function opencodeFetch(pathname, init = {}, options = {}) {
const timeoutMs = Number(options.timeoutMs) > 0 ? Number(options.timeoutMs) : 0;
const controller = timeoutMs > 0 ? new AbortController() : null;
const timer = controller ? setTimeout(() => controller.abort(), timeoutMs) : null;
let res;
try {
res = await fetch(opencodeUrl(pathname), {
...init,
headers: opencodeHeaders(init.headers || {}),
signal: controller ? controller.signal : init.signal,
});
} catch (error) {
if (timer) clearTimeout(timer);
if (isAbortLikeError(error)) {
const timeoutError = new Error(`opencode timeout after ${timeoutMs}ms`);
timeoutError.status = 504;
timeoutError.code = "OPENCODE_TIMEOUT";
throw timeoutError;
}
throw error;
}
let text;
try {
text = await res.text();
} catch (error) {
if (timer) clearTimeout(timer);
if (isAbortLikeError(error)) {
const timeoutError = new Error(`opencode timeout after ${timeoutMs}ms`);
timeoutError.status = 504;
timeoutError.code = "OPENCODE_TIMEOUT";
throw timeoutError;
}
throw error;
}
if (timer) clearTimeout(timer);
const data = parseResponseBody(text);
if (!res.ok) {
const msg =
(data && typeof data === "object" && (data.message || data.error?.message)) ||
`opencode error ${res.status}`;
const error = new Error(msg);
error.status = res.status;
error.payload = data;
throw error;
}
return data;
}
function textFromOpenAIContent(content) {
if (typeof content === "string") return content;
if (!Array.isArray(content)) return "";
return content
.map((part) => {
if (!part || typeof part !== "object") return "";
if (part.type === "text") return part.text || "";
if (part.type === "input_text") return part.text || "";
return "";
})
.filter(Boolean)
.join("\n");
}
function buildPromptFromMessages(messages) {
const lines = [];
const systemLines = [];
for (const msg of messages || []) {
if (!msg || typeof msg !== "object") continue;
const role = String(msg.role || "user");
const text = textFromOpenAIContent(msg.content);
if (!text) continue;
if (role === "system") {
systemLines.push(text);
continue;
}
const label = role.toUpperCase();
lines.push(`${label}:\n${text}`);
}
return {
prompt: lines.join("\n\n"),
system: systemLines.join("\n\n").trim(),
};
}
function resolveOpencodeModel(openaiModel) {
if (openaiModel && modelMap[openaiModel]) return modelMap[openaiModel];
if (openaiModel && typeof openaiModel === "string") {
if (openaiModel.includes("/")) {
const [providerID, ...rest] = openaiModel.split("/");
if (providerID && rest.length) {
return { providerID, modelID: rest.join("/") };
}
}
if (openaiModel.includes(":")) {
const [providerID, ...rest] = openaiModel.split(":");
if (providerID && rest.length) {
return { providerID, modelID: rest.join(":") };
}
}
}
if (DEFAULT_PROVIDER_ID && DEFAULT_MODEL_ID) {
return { providerID: DEFAULT_PROVIDER_ID, modelID: DEFAULT_MODEL_ID };
}
return null;
}
function extractAssistantText(opencodeResponse) {
const parts = Array.isArray(opencodeResponse?.parts) ? opencodeResponse.parts : [];
return parts
.filter((p) => p && p.type === "text" && typeof p.text === "string")
.map((p) => p.text)
.join("");
}
function summarizePartTypes(opencodeResponse) {
const parts = Array.isArray(opencodeResponse?.parts) ? opencodeResponse.parts : [];
return parts
.map((part) => (part && typeof part === "object" && part.type ? String(part.type) : "unknown"))
.filter(Boolean);
}
function toOpenAICompletion({ reqBody, content, usage }) {
const now = Math.floor(Date.now() / 1000);
return {
id: `chatcmpl-${randomUUID()}`,
object: "chat.completion",
created: now,
model: reqBody.model || OPENAI_MODEL_ID,
choices: [
{
index: 0,
message: {
role: "assistant",
content,
},
finish_reason: "stop",
},
],
usage: {
prompt_tokens: usage?.input ?? 0,
completion_tokens: usage?.output ?? 0,
total_tokens: (usage?.input ?? 0) + (usage?.output ?? 0),
},
};
}
function writeOpenAIStream(res, reqBody, content) {
sse(res);
const id = `chatcmpl-${randomUUID()}`;
const created = Math.floor(Date.now() / 1000);
const model = reqBody.model || OPENAI_MODEL_ID;
const chunk1 = {
id,
object: "chat.completion.chunk",
created,
model,
choices: [{ index: 0, delta: { role: "assistant" }, finish_reason: null }],
};
res.write(`data: ${JSON.stringify(chunk1)}\n\n`);
if (content) {
const chunk2 = {
id,
object: "chat.completion.chunk",
created,
model,
choices: [{ index: 0, delta: { content }, finish_reason: null }],
};
res.write(`data: ${JSON.stringify(chunk2)}\n\n`);
}
const chunk3 = {
id,
object: "chat.completion.chunk",
created,
model,
choices: [{ index: 0, delta: {}, finish_reason: "stop" }],
};
res.write(`data: ${JSON.stringify(chunk3)}\n\n`);
res.write("data: [DONE]\n\n");
res.end();
}
async function createAndPromptOpencode(reqBody) {
const messages = Array.isArray(reqBody.messages) ? reqBody.messages : [];
if (!messages.length) {
const error = new Error("messages is required");
error.status = 400;
throw error;
}
const resolvedModel = resolveOpencodeModel(reqBody.model);
const { prompt, system } = buildPromptFromMessages(messages);
if (!prompt.trim()) {
const error = new Error("No usable text found in messages");
error.status = 400;
throw error;
}
const session = await opencodeFetch("/session", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ title: "openclaw-bridge" }),
}, {
timeoutMs: OPENCODE_SESSION_TIMEOUT_MS,
});
const body = {
parts: [{ type: "text", text: prompt }],
};
if (resolvedModel) body.model = resolvedModel;
if (DEFAULT_AGENT) body.agent = DEFAULT_AGENT;
if (DEFAULT_SYSTEM || system) body.system = [DEFAULT_SYSTEM, system].filter(Boolean).join("\n\n");
const response = await opencodeFetch(`/session/${encodeURIComponent(session.id)}/message`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
}, {
timeoutMs: OPENCODE_MESSAGE_TIMEOUT_MS,
});
return response;
}
function shouldRetryOpencodeError(error) {
const status = Number(error?.status) || 0;
if (error?.code === "OPENCODE_TIMEOUT") return false;
return status === 408 || status === 429 || status >= 500;
}
async function createAndPromptOpencodeWithRetry(reqBody) {
const maxAttempts = Math.max(1, OPENCODE_RETRY_COUNT + 1);
let lastError = null;
for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
try {
const opencodeResp = await createAndPromptOpencode(reqBody);
const content = extractAssistantText(opencodeResp);
if (content && content.trim()) {
return {
opencodeResp,
content,
usage: opencodeResp?.info?.tokens,
};
}
const error = new Error("Model returned no assistant text");
error.status = 502;
error.code = "EMPTY_ASSISTANT_TEXT";
error.payload = {
finish: opencodeResp?.info?.finish || null,
partTypes: summarizePartTypes(opencodeResp),
};
throw error;
} catch (error) {
lastError = error;
const canRetry = attempt < maxAttempts && (error.code === "EMPTY_ASSISTANT_TEXT" || shouldRetryOpencodeError(error));
if (!canRetry) break;
console.warn(
`[bridge] retrying opencode request attempt=${attempt + 1}/${maxAttempts} reason=${error.code || error.message}`,
);
if (OPENCODE_RETRY_DELAY_MS > 0) {
await sleep(OPENCODE_RETRY_DELAY_MS);
}
}
}
throw lastError;
}
async function handleChatCompletions(req, res) {
if (!requireBridgeAuth(req, res)) return;
let reqBody;
try {
const raw = await readBody(req);
reqBody = raw ? JSON.parse(raw) : {};
} catch (error) {
return json(res, 400, { error: { message: error.message, type: "invalid_request_error" } });
}
try {
const { content, usage } = await createAndPromptOpencodeWithRetry(reqBody);
if (reqBody.stream) {
return writeOpenAIStream(res, reqBody, content);
}
return json(res, 200, toOpenAICompletion({ reqBody, content, usage }));
} catch (error) {
const status = Number(error.status) || 500;
return json(res, status, {
error: {
message: error.message || "Internal error",
type: status >= 500 ? "server_error" : "invalid_request_error",
details: error.payload || undefined,
},
});
}
}
function handleModels(req, res) {
if (!requireBridgeAuth(req, res)) return;
const model = {
id: OPENAI_MODEL_ID,
object: "model",
created: 0,
owned_by: "opencode-bridge",
};
return json(res, 200, { object: "list", data: [model] });
}
function handleHealth(res) {
return json(res, 200, {
ok: true,
service: "openclaw-opencode-bridge",
opencodeBaseUrl: OPENCODE_BASE_URL,
model: OPENAI_MODEL_ID,
});
}
const server = http.createServer(async (req, res) => {
const url = new URL(req.url || "/", `http://${req.headers.host || "localhost"}`);
try {
if (req.method === "GET" && url.pathname === "/health") {
return handleHealth(res);
}
if (req.method === "GET" && url.pathname === "/v1/models") {
return handleModels(req, res);
}
if (req.method === "POST" && url.pathname === "/v1/chat/completions") {
return handleChatCompletions(req, res);
}
return json(res, 404, { error: { message: "Not found" } });
} catch (error) {
console.error("Unhandled error:", error);
return json(res, 500, { error: { message: "Internal server error" } });
}
});
server.listen(PORT, HOST, () => {
console.log(`openclaw-opencode-bridge listening on http://${HOST}:${PORT}`);
console.log(`opencode base: ${OPENCODE_BASE_URL}`);
});