Update main.ts
Browse files
main.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
// file:
|
| 2 |
|
| 3 |
/**
|
| 4 |
* Deno script to proxy OpenAI chat completion requests to a Tau API.
|
|
@@ -7,6 +7,9 @@
|
|
| 7 |
* Also supports the /v1/models endpoint.
|
| 8 |
* Includes fix for model placement, configurable API key, explicit semicolons,
|
| 9 |
* quote cleaning, and debug logging to confirm newline escaping in JSON.
|
|
|
|
|
|
|
|
|
|
| 10 |
*/
|
| 11 |
|
| 12 |
// --- Constants ---
|
|
@@ -550,28 +553,42 @@ async function handleChatCompletions(request: Request): Promise<Response> {
|
|
| 550 |
}
|
| 551 |
|
| 552 |
|
| 553 |
-
// --- Main Request Handler Dispatcher ---
|
| 554 |
|
| 555 |
async function handler(request: Request): Promise<Response> {
|
| 556 |
const url = new URL(request.url);
|
| 557 |
const path = url.pathname;
|
| 558 |
const method = request.method;
|
| 559 |
|
| 560 |
-
console.log(`Received request: ${method} ${path}`)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 561 |
|
| 562 |
if (method === "GET" && path === "/v1/models") {
|
| 563 |
-
return handleListModels()
|
| 564 |
} else if (method === "POST" && path === "/v1/chat/completions") {
|
| 565 |
-
return handleChatCompletions(request)
|
| 566 |
} else {
|
| 567 |
-
return new Response("Not Found", { status: 404 })
|
| 568 |
}
|
| 569 |
}
|
| 570 |
|
| 571 |
|
| 572 |
// --- Start Server ---
|
| 573 |
const PORT = 8000;
|
| 574 |
-
const HOSTNAME = "0.0.0.0";
|
| 575 |
|
| 576 |
-
console.log(`Listening on http://${HOSTNAME}:${PORT}/`)
|
| 577 |
-
Deno.serve({ port: PORT, hostname: HOSTNAME }, handler)
|
|
|
|
| 1 |
+
// file: openai_tau_proxy_fixed.ts
|
| 2 |
|
| 3 |
/**
|
| 4 |
* Deno script to proxy OpenAI chat completion requests to a Tau API.
|
|
|
|
| 7 |
* Also supports the /v1/models endpoint.
|
| 8 |
* Includes fix for model placement, configurable API key, explicit semicolons,
|
| 9 |
* quote cleaning, and debug logging to confirm newline escaping in JSON.
|
| 10 |
+
*
|
| 11 |
+
* FIX: Added CORS preflight (OPTIONS) request handler to allow web clients
|
| 12 |
+
* like New API to make POST requests across different origins.
|
| 13 |
*/
|
| 14 |
|
| 15 |
// --- Constants ---
|
|
|
|
| 553 |
}
|
| 554 |
|
| 555 |
|
| 556 |
+
// --- Main Request Handler Dispatcher (FIXED) ---
|
| 557 |
|
| 558 |
async function handler(request: Request): Promise<Response> {
|
| 559 |
const url = new URL(request.url);
|
| 560 |
const path = url.pathname;
|
| 561 |
const method = request.method;
|
| 562 |
|
| 563 |
+
console.log(`Received request: ${method} ${path}`);
|
| 564 |
+
|
| 565 |
+
// Handle CORS preflight (OPTIONS) requests.
|
| 566 |
+
// This is crucial for web clients like New API to be allowed by the browser
|
| 567 |
+
// to send the actual POST request.
|
| 568 |
+
if (method === "OPTIONS") {
|
| 569 |
+
return new Response(null, {
|
| 570 |
+
status: 204, // No Content
|
| 571 |
+
headers: {
|
| 572 |
+
"Access-Control-Allow-Origin": "*", // Allow any website to connect
|
| 573 |
+
"Access-Control-Allow-Methods": "POST, GET, OPTIONS", // Allow these methods
|
| 574 |
+
"Access-Control-Allow-Headers": "Content-Type, Authorization", // Allow these headers in the request
|
| 575 |
+
},
|
| 576 |
+
});
|
| 577 |
+
}
|
| 578 |
|
| 579 |
if (method === "GET" && path === "/v1/models") {
|
| 580 |
+
return handleListModels();
|
| 581 |
} else if (method === "POST" && path === "/v1/chat/completions") {
|
| 582 |
+
return handleChatCompletions(request);
|
| 583 |
} else {
|
| 584 |
+
return new Response("Not Found", { status: 404 });
|
| 585 |
}
|
| 586 |
}
|
| 587 |
|
| 588 |
|
| 589 |
// --- Start Server ---
|
| 590 |
const PORT = 8000;
|
| 591 |
+
const HOSTNAME = "0.0.0.0";
|
| 592 |
|
| 593 |
+
console.log(`Listening on http://${HOSTNAME}:${PORT}/`);
|
| 594 |
+
Deno.serve({ port: PORT, hostname: HOSTNAME }, handler);
|