-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·731 lines (643 loc) · 23.6 KB
/
index.js
File metadata and controls
executable file
·731 lines (643 loc) · 23.6 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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
#!/usr/bin/env node
import express from "express";
import { CopilotClient, approveAll } from "@github/copilot-sdk";
import { v4 as uuidv4 } from "uuid";
import { Command } from "commander";
import { readFileSync } from "fs";
import { fileURLToPath } from "url";
import { dirname, join } from "path";
// Read package.json for version
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const pkg = JSON.parse(readFileSync(join(__dirname, "package.json"), "utf-8"));
// Parse command-line arguments
const program = new Command();
program
.name("copilot-proxy")
.description("OpenAI-compatible proxy server for GitHub Copilot SDK")
.version(pkg.version, "-V, --version", "output the version number")
.option("-p, --port <number>", "port to listen on", "3001")
.option("-m, --model <name>", "default model id to use (ex. 'gpt-5.2')")
.option("-l, --list", "list available models and exit")
.option("--cli-path <path>", "path to copilot CLI binary (if not in $PATH)", process.env.COPILOT_CLI_PATH)
.option("-v, --verbose", "enable verbose log output")
.helpOption("-h, --help", "display this help")
.parse(process.argv);
const opts = program.opts();
// -m is required unless --list is used
if (!opts.list && !opts.model) {
program.error("required option '-m, --model <name>' not specified");
}
const app = express();
app.use(express.json({ limit: "10mb" }));
const PORT = opts.port;
const DEFAULT_MODEL = opts.model;
function writeLog(...args) {
const parts = args.map(a => typeof a === 'string' ? a : JSON.stringify(a, null, 2));
process.stderr.write(`[${new Date().toISOString()}] ${parts.join(' ')}\n`);
}
// Informational messages — only printed when --verbose is specified
function log(...args) {
if (opts.verbose) writeLog(...args);
}
// Errors — always printed to stderr
const error = writeLog;
// Singleton CopilotClient instance
let copilotClient = null;
async function getCopilotClient() {
if (!copilotClient) {
copilotClient = new CopilotClient(opts.cliPath ? { cliPath: opts.cliPath } : {});
await copilotClient.start();
log("CopilotClient started");
}
return copilotClient;
}
async function stopAndExit(code = 0) {
if (copilotClient) {
try { await copilotClient.stop(); } catch {}
}
process.exit(code);
}
// Cache for dynamically fetched models
let cachedModels = null;
let modelsCacheTime = 0;
const MODELS_CACHE_TTL = 5 * 60 * 1000; // 5 minutes
async function getAvailableModels() {
const now = Date.now();
if (cachedModels && (now - modelsCacheTime) < MODELS_CACHE_TTL) {
return cachedModels;
}
const client = await getCopilotClient();
const models = await client.listModels();
log("Fetched models from SDK:", models.map((m) => m.id));
cachedModels = models;
modelsCacheTime = now;
return models;
}
// Infer owner from model id
function inferOwnedBy(modelId) {
if (modelId.startsWith("gpt-") || modelId.startsWith("o1") || modelId.startsWith("o3") || modelId.startsWith("o4")) return "openai";
if (modelId.startsWith("claude-")) return "anthropic";
if (modelId.startsWith("gemini-")) return "google";
return "unknown";
}
// Format a model object for the OpenAI API
function toModelObject(modelId) {
return {
id: modelId,
object: "model",
created: Math.floor(Date.now() / 1000),
owned_by: inferOwnedBy(modelId),
};
}
// Convert OpenAI messages array to a single prompt string
function messagesToPrompt(messages) {
return messages
.map((msg) => {
const role = msg.role.charAt(0).toUpperCase() + msg.role.slice(1);
const content =
typeof msg.content === "string"
? msg.content
: msg.content
.filter((c) => c.type === "text")
.map((c) => c.text)
.join("\n");
return `${role}: ${content}`;
})
.join("\n\n");
}
// Extract system message from messages array
function extractSystemMessage(messages) {
const text = messages.filter((m) => m.role === "system").map((m) => m.content).join("\n");
return text || null;
}
// Send a JSON error response
function sendError(res, status, message, type, code) {
res.status(status).json({ error: { message, type, code } });
}
// Convert to OpenAI chat completion response format
function toOpenAIResponse(content, model) {
return {
id: `chatcmpl-${uuidv4()}`,
object: "chat.completion",
created: Math.floor(Date.now() / 1000),
model,
choices: [
{
index: 0,
message: { role: "assistant", content },
finish_reason: "stop",
},
],
usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 },
};
}
// Convert to OpenAI streaming chunk format
function toOpenAIStreamChunk(content, model, isFirst = false, isDone = false) {
const chunk = {
id: `chatcmpl-${uuidv4()}`,
object: "chat.completion.chunk",
created: Math.floor(Date.now() / 1000),
model,
choices: [
{
index: 0,
delta: isDone ? {} : isFirst ? { role: "assistant", content } : { content },
finish_reason: isDone ? "stop" : null,
},
],
};
return `data: ${JSON.stringify(chunk)}\n\n`;
}
// ---------------------------------------------------------------------------
// Responses API helpers (/v1/responses)
// ---------------------------------------------------------------------------
// In-memory store for previous responses (for conversation threading)
// Maps response ID → { messages, outputContent, model, previousResponseId, timestamp }
const responseStore = new Map();
const RESPONSE_STORE_MAX_SIZE = 1000;
const RESPONSE_STORE_TTL = 60 * 60 * 1000; // 1 hour
function storeResponse(responseId, messages, outputContent, model, previousResponseId) {
// Evict oldest entries if we exceed max size
if (responseStore.size >= RESPONSE_STORE_MAX_SIZE) {
const oldest = responseStore.keys().next().value;
responseStore.delete(oldest);
}
responseStore.set(responseId, {
messages,
outputContent,
model,
previousResponseId: previousResponseId || null,
timestamp: Date.now(),
});
}
function getStoredResponse(responseId) {
const entry = responseStore.get(responseId);
if (!entry) return null;
// Check TTL
if (Date.now() - entry.timestamp > RESPONSE_STORE_TTL) {
responseStore.delete(responseId);
return null;
}
return entry;
}
// Rebuild full conversation history by walking the previous_response_id chain
function rebuildConversationHistory(previousResponseId) {
const chain = [];
let currentId = previousResponseId;
while (currentId) {
const stored = getStoredResponse(currentId);
if (!stored) break;
chain.unshift(stored);
currentId = stored.previousResponseId || null;
}
return chain;
}
// Build the full messages array including conversation history from previous_response_id
function buildFullMessages(input, instructions, previousResponseId) {
const messages = [];
// Prepend instructions as a system message when provided
if (instructions) {
messages.push({ role: "system", content: instructions });
}
// If there's a previous_response_id, rebuild conversation history
if (previousResponseId) {
const chain = rebuildConversationHistory(previousResponseId);
for (const entry of chain) {
// Add the user messages from that turn (excluding system messages,
// since we use the current instructions)
for (const msg of entry.messages) {
if (msg.role !== "system") {
messages.push(msg);
}
}
// Add the assistant response from that turn
if (entry.outputContent) {
messages.push({ role: "assistant", content: entry.outputContent });
}
}
}
// Now add the current input
if (typeof input === "string") {
messages.push({ role: "user", content: input });
} else if (Array.isArray(input)) {
for (const item of input) {
if (typeof item === "string") {
messages.push({ role: "user", content: item });
} else if (item.role && item.content) {
const text =
typeof item.content === "string"
? item.content
: Array.isArray(item.content)
? item.content
.filter((c) => c.type === "input_text" || c.type === "text")
.map((c) => c.text)
.join("\n")
: String(item.content);
messages.push({ role: item.role, content: text });
}
}
}
return messages;
}
// Normalise the Responses API "input" field into an OpenAI-style messages array.
// "input" can be:
// - a plain string → single user message
// - an array of message objects (role + content)
// - an array that mixes strings and message objects
function responsesInputToMessages(input, instructions) {
const messages = [];
// Prepend instructions as a system message when provided
if (instructions) {
messages.push({ role: "system", content: instructions });
}
if (typeof input === "string") {
messages.push({ role: "user", content: input });
return messages;
}
if (Array.isArray(input)) {
for (const item of input) {
if (typeof item === "string") {
messages.push({ role: "user", content: item });
} else if (item.role && item.content) {
// content may be a string or an array of content parts
const text =
typeof item.content === "string"
? item.content
: Array.isArray(item.content)
? item.content
.filter((c) => c.type === "input_text" || c.type === "text")
.map((c) => c.text)
.join("\n")
: String(item.content);
messages.push({ role: item.role, content: text });
}
}
}
return messages;
}
// Build a Responses API response object (non-streaming)
function toResponsesAPIResponse(content, model, responseId) {
const id = responseId || `resp_${uuidv4().replace(/-/g, "")}`;
return {
id,
object: "response",
created_at: Math.floor(Date.now() / 1000),
model,
output: [
{
type: "message",
id: `msg_${uuidv4().replace(/-/g, "")}`,
role: "assistant",
status: "completed",
content: [
{ type: "output_text", text: content },
],
},
],
status: "completed",
usage: { input_tokens: 0, output_tokens: 0, total_tokens: 0 },
};
}
// Responses API streaming event helpers
function responsesStreamEvent(type, data) {
// Include the event type inside the JSON data payload — many clients
// (including Obsidian Copilot) parse the type from the JSON rather than
// from the SSE `event:` line.
const payload = { type, ...data };
return `event: ${type}\ndata: ${JSON.stringify(payload)}\n\n`;
}
// POST /v1/chat/completions - Main chat endpoint
app.post("/v1/chat/completions", async (req, res) => {
try {
log("POST /v1/chat/completions request:", req.body);
const { messages, model, stream = false } = req.body;
if (!messages || !Array.isArray(messages) || messages.length === 0) {
return sendError(res, 400, "messages is required and must be a non-empty array", "invalid_request_error", "invalid_messages");
}
const selectedModel = model || DEFAULT_MODEL;
const client = await getCopilotClient();
// Build session config
const sessionConfig = {
model: selectedModel,
streaming: stream,
onPermissionRequest: approveAll,
infiniteSessions: { enabled: false },
};
// Extract and set system message if present
const systemMessage = extractSystemMessage(messages);
if (systemMessage) {
sessionConfig.systemMessage = { content: systemMessage };
}
// Create session
const session = await client.createSession(sessionConfig);
try {
// Filter out system messages and convert remaining to prompt
const nonSystemMessages = messages.filter((m) => m.role !== "system");
const prompt = messagesToPrompt(nonSystemMessages);
if (stream) {
// Streaming response
res.setHeader("Content-Type", "text/event-stream");
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Connection", "keep-alive");
res.setHeader("X-Accel-Buffering", "no");
let isFirst = true;
let fullContent = "";
const done = new Promise((resolve, reject) => {
session.on((event) => {
try {
if (event.type === "assistant.message_delta") {
fullContent += event.data.deltaContent;
res.write(toOpenAIStreamChunk(event.data.deltaContent, selectedModel, isFirst));
isFirst = false;
} else if (event.type === "session.idle") {
res.write(toOpenAIStreamChunk("", selectedModel, false, true));
res.write("data: [DONE]\n\n");
log("POST /v1/chat/completions streaming response complete:", { model: selectedModel, content: fullContent });
resolve();
}
} catch (err) {
reject(err);
}
});
});
await session.send({ prompt });
await done;
res.end();
} else {
// Non-streaming response
const result = await session.sendAndWait({ prompt });
const content = result?.data?.content || "";
const response = toOpenAIResponse(content, selectedModel);
log("POST /v1/chat/completions response:", response);
res.json(response);
}
} finally {
await session.disconnect();
}
} catch (err) {
error("Error in /v1/chat/completions:", err);
sendError(res, 500, err.message || "Internal server error", "api_error", "internal_error");
}
});
// POST /v1/responses - OpenAI Responses API endpoint
app.post("/v1/responses", async (req, res) => {
try {
log("POST /v1/responses request:", req.body);
const {
input,
model,
instructions,
stream = false,
previous_response_id,
temperature,
top_p,
max_output_tokens,
store = true, // default true per OpenAI spec
} = req.body;
if (input === undefined || input === null) {
return sendError(res, 400, "input is required", "invalid_request_error", "invalid_input");
}
const selectedModel = model || DEFAULT_MODEL;
const client = await getCopilotClient();
// Build full messages array including conversation history
const messages = buildFullMessages(input, instructions, previous_response_id);
log("POST /v1/responses resolved messages:", messages);
// Build session config
const sessionConfig = {
model: selectedModel,
streaming: stream,
onPermissionRequest: approveAll,
infiniteSessions: { enabled: false },
};
// Forward supported model parameters when provided
if (temperature !== undefined) {
sessionConfig.temperature = temperature;
}
if (top_p !== undefined) {
sessionConfig.topP = top_p;
}
if (max_output_tokens !== undefined) {
sessionConfig.maxTokens = max_output_tokens;
}
// Extract and set system message if present
const systemMessage = extractSystemMessage(messages);
if (systemMessage) {
sessionConfig.systemMessage = { content: systemMessage };
}
// Create session
const session = await client.createSession(sessionConfig);
const responseId = `resp_${uuidv4().replace(/-/g, "")}`;
const messageId = `msg_${uuidv4().replace(/-/g, "")}`;
try {
// Filter out system messages and convert remaining to prompt
const nonSystemMessages = messages.filter((m) => m.role !== "system");
const prompt = messagesToPrompt(nonSystemMessages);
if (stream) {
// Streaming response using Responses API SSE format
res.setHeader("Content-Type", "text/event-stream");
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Connection", "keep-alive");
res.setHeader("X-Accel-Buffering", "no");
// Send response.created
const responseObj = {
id: responseId, object: "response", status: "in_progress",
model: selectedModel, output: [],
};
res.write(responsesStreamEvent("response.created", {
response: responseObj,
}));
// Send response.in_progress
res.write(responsesStreamEvent("response.in_progress", {
response: responseObj,
}));
// Send output_item.added
const outputItem = {
type: "message", id: messageId, role: "assistant",
status: "in_progress", content: [],
};
res.write(responsesStreamEvent("response.output_item.added", {
output_index: 0, item: outputItem,
}));
// Send content_part.added
res.write(responsesStreamEvent("response.content_part.added", {
item_id: messageId, output_index: 0, content_index: 0,
part: { type: "output_text", text: "", annotations: [] },
}));
let fullContent = "";
const done = new Promise((resolve, reject) => {
session.on((event) => {
try {
if (event.type === "assistant.message_delta") {
const delta = event.data.deltaContent;
fullContent += delta;
res.write(responsesStreamEvent("response.output_text.delta", {
item_id: messageId, output_index: 0, content_index: 0, delta,
}));
} else if (event.type === "session.idle") {
// Send output_text.done
res.write(responsesStreamEvent("response.output_text.done", {
item_id: messageId, output_index: 0, content_index: 0, text: fullContent,
}));
// Send content_part.done
res.write(responsesStreamEvent("response.content_part.done", {
item_id: messageId, output_index: 0, content_index: 0,
part: { type: "output_text", text: fullContent },
}));
// Send output_item.done
res.write(responsesStreamEvent("response.output_item.done", {
output_index: 0,
item: {
type: "message", id: messageId, role: "assistant",
status: "completed",
content: [{ type: "output_text", text: fullContent }],
},
}));
// Send response.completed
res.write(responsesStreamEvent("response.completed", {
response: {
id: responseId, object: "response", status: "completed",
model: selectedModel,
output: [{
type: "message", id: messageId, role: "assistant",
status: "completed",
content: [{ type: "output_text", text: fullContent }],
}],
usage: { input_tokens: 0, output_tokens: 0, total_tokens: 0 },
},
}));
// Store the response for conversation threading
if (store) {
const currentTurnMessages = responsesInputToMessages(input, null);
storeResponse(responseId, currentTurnMessages, fullContent, selectedModel, previous_response_id);
}
log("POST /v1/responses streaming response complete:", { model: selectedModel, content: fullContent });
resolve();
}
} catch (err) {
reject(err);
}
});
});
await session.send({ prompt });
await done;
res.end();
} else {
// Non-streaming response
const result = await session.sendAndWait({ prompt });
const content = result?.data?.content || "";
const response = toResponsesAPIResponse(content, selectedModel, responseId);
log("POST /v1/responses response:", response);
// Store the response for conversation threading
if (store) {
const currentTurnMessages = responsesInputToMessages(input, null);
storeResponse(responseId, currentTurnMessages, content, selectedModel, previous_response_id);
}
res.json(response);
}
} finally {
await session.disconnect();
}
} catch (err) {
error("Error in /v1/responses:", err);
sendError(res, 500, err.message || "Internal server error", "api_error", "internal_error");
}
});
// GET /v1/models - List available models
app.get("/v1/models", async (req, res) => {
try {
const models = await getAvailableModels();
const response = { object: "list", data: models.map((m) => toModelObject(m.id)) };
log("GET /v1/models response:", response);
res.json(response);
} catch (err) {
error("Error fetching models:", err);
sendError(res, 500, err.message || "Failed to fetch models", "api_error", "internal_error");
}
});
// GET /v1/models/:model - Get specific model
app.get("/v1/models/:model", async (req, res) => {
try {
const models = await getAvailableModels();
const model = models.find((m) => m.id === req.params.model);
if (!model) {
return sendError(res, 404, `Model '${req.params.model}' not found`, "invalid_request_error", "model_not_found");
}
const response = toModelObject(model.id);
log("GET /v1/models/:model response:", response);
res.json(response);
} catch (err) {
error("Error fetching model:", err);
sendError(res, 500, err.message || "Failed to fetch model", "api_error", "internal_error");
}
});
// Health check endpoint
app.get("/health", (req, res) => {
res.json({ status: "ok" });
});
// Log unmatched requests
app.use((req, res) => {
log("Unmatched request:", req.method, req.originalUrl, req.body);
sendError(res, 404, `No route found for ${req.method} ${req.originalUrl}`, "invalid_request_error", "not_found");
});
// Graceful shutdown
async function shutdown() {
log("Shutting down...");
if (copilotClient) {
try {
await copilotClient.stop();
log("CopilotClient stopped");
} catch (err) {
error("Error stopping CopilotClient:", err);
}
}
process.exit(0);
}
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);
// Handle --list: print available models and exit
if (opts.list) {
(async () => {
try {
const client = await getCopilotClient();
const models = await client.listModels();
for (const model of models) {
console.error(`${model.id} [${model.name}]`);
}
} catch (err) {
console.error("Error fetching models:", err.message || err);
await stopAndExit(1);
} finally {
await stopAndExit(0);
}
})();
} else {
// Validate the default model before starting the server
(async () => {
try {
const models = await getAvailableModels();
const modelExists = models.some((m) => m.id === DEFAULT_MODEL);
if (!modelExists) {
console.error(`Error: Model '${DEFAULT_MODEL}' is not available.`);
console.error("Available models:");
for (const m of models) {
console.error(` ${m.id} [${m.name}]`);
}
await stopAndExit(1);
}
} catch (err) {
console.error("Error validating model:", err.message || err);
await stopAndExit(1);
}
// Start server
app.listen(PORT, '127.0.0.1', () => {
console.error(`OpenAI-compatible proxy server running at http://localhost:${PORT}`);
console.error(`Default model: ${DEFAULT_MODEL}`);
console.error("Endpoints:");
console.error(` POST http://localhost:${PORT}/v1/chat/completions`);
console.error(` POST http://localhost:${PORT}/v1/responses`);
console.error(` GET http://localhost:${PORT}/v1/models`);
console.error(` GET http://localhost:${PORT}/health`);
});
})();
}