fix: Vercel serverless function crash and await async completions routes

- Make generateDeviceFingerprint resilient to os.cpus() and os.hostname() returning empty or throwing in lambda sandboxes.
- Await async handleChatCompletion and handleAnthropicMessages calls in route handlers to prevent unhandled promise rejections.
- Catch runtime exceptions from completions and return clean HTTP 500 JSON error responses.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
MythEclipse
2026-06-24 20:40:09 +07:00
co-authored by Claude Opus 4.8
parent 2171a01974
commit d1d6f607b0
3 changed files with 75 additions and 110 deletions
+20 -5
View File
@@ -34,11 +34,26 @@ const EXPIRY_BUFFER_MS = 300_000; // 5 minutes
* Uses Web Crypto API (available in Bun, Workers, and Node.js 20+).
*/
async function generateDeviceFingerprint(): Promise<string> {
const hostname = os.hostname();
const platform = process.platform;
const arch = process.arch;
const cpus = os.cpus();
const cpuModel = cpus.length > 0 ? (cpus[0]?.model ?? "unknown") : "unknown";
let hostname = "unknown";
try {
hostname = os.hostname() ?? "unknown";
} catch {
// OS module hostname not available in this sandbox
}
const platform = process.platform ?? "unknown";
const arch = process.arch ?? "unknown";
let cpuModel = "unknown";
try {
const cpus = os.cpus();
if (cpus && cpus.length > 0) {
cpuModel = cpus[0]?.model ?? "unknown";
}
} catch {
// OS module cpus not available in this sandbox
}
const username = process.env.USER ?? process.env.USERNAME ?? "unknown";
const raw = `${hostname}|${platform}|${arch}|${cpuModel}|${username}`;