// @ts-nocheck // The claude request-serving implementation: runs the request through the TeaVM-compiled Java // ClaudeHandleOrchestrator. Loaded lazily (dynamic import in index.ts) so the ~MB TeaVM bundle // only evaluates on the first request, never at plugin registration. The Java orchestrator owns // every decision; this shell owns only host I/O (fetch+IP-proxy transport, account acquire/report // over the real manager, building the final Response). The provider-facing entry point is the // IR-native handleIr; handleViaJavaOrchestrator is its internal transport/orchestration core. import { proxyManager, getAutoCandidates, HandleIrError, lazyModule, safeJsonParse, initCoreAuth } from "@intisy-ai/core-auth"; import { manager } from "./index.js"; import { captureQuota, accountHasQuota } from "./accounts-controller.js"; import { getMaxAttempts, getDefaultCooldownSeconds, getMaxCooldownSeconds } from "./settings.js"; import { anthropicTranslator } from "@intisy-ai/anthropic-translator"; const PROVIDER_ID = "claude-code"; const LANE = "messages"; // Claude subscription limits are account-wide // The generated file is staged to src/generated/ by `core/teavm-build.mjs` at build time // and bundled by esbuild (deferred, so the ~MB TeaVM bundle only evaluates on first use). const claudeOrchestrator = lazyModule(() => import("../generated/claude-orchestrator.teavm.js")); // Rebuild a Fetch `Headers` object from the header map the orchestrator hands back as JSON, so // the real `captureQuota` (which iterates `headers.forEach`) sees the same shape as the live path. function headersFromJson(headersJson) { const obj = safeJsonParse(headersJson, {}); const headers = new Headers(); if (obj && typeof obj === "object") { for (const [name, value] of Object.entries(obj)) { if (value != null) headers.set(name, String(value)); } } return headers; } export async function handleViaJavaOrchestrator(request, ctx) { const log = (ctx && ctx.log) || (() => {}); // Defense-in-depth: manager.acquire(lane) already self-inits core-auth as the first action of // every attempt loop iteration, before any of the sync jsReports callbacks below can fire for // that same iteration. Awaited here too so a future caller can never race it. await initCoreAuth(); // Inputs the orchestrator needs; the leaderboard stays TS so its #1 candidate is passed in. let bodyText; try { bodyText = await request.clone().text(); } catch { bodyText = undefined; } const inputsJson = JSON.stringify({ url: request.url, method: request.method, headers: Object.fromEntries(request.headers), bodyText: bodyText ?? "", ctxModel: (ctx && ctx.model) || "", topAutoCandidate: getAutoCandidates(PROVIDER_ID)[0] || "", }); // Cooldown pair drives the Java orchestrator's no-reset-header exponential backoff, matching // the TS path's parseResetMs fallback. Read per-request like maxAttempts so a config edit // applies without a restart. const configJson = JSON.stringify({ maxAttempts: getMaxAttempts(), defaultCooldownSeconds: getDefaultCooldownSeconds(), maxCooldownSeconds: getMaxCooldownSeconds(), lane: LANE, }); // --- per-request host state (isolated to this call) --------------------------------------- const responses = []; // retained live Response objects, indexed by attemptRef const proxyByAccount = new Map(); // proxy URL used for each account this request const acquiredByAccount = new Map(); // the acquired account object (fresh-lookup fallback) // jsAcquire: await manager.acquire(lane); null means no acquired account. When an // account exists but has no access token, return access:"" so the orchestrator's missing-access // branch fires with the "missing access token" reportError, not the no-account branch. const jsAcquire = async (lane) => { const acquired = await manager.acquire(lane); if (!acquired || !acquired.account) return null; acquiredByAccount.set(acquired.account.id, acquired.account); return JSON.stringify({ accountId: acquired.account.id, access: acquired.access || "" }); }; // jsExec: pure transport: proxy select → fetch → on a proxy fetch error // reportResult(false)+retry-direct → on direct/no-proxy error return transportFailed → on // success reportResult(true, ms) if a proxy was used. Retains the live Response host-side; // only {status, headers} cross to Java. No body is ever read here. const jsExec = async (accountId, preparedJson) => { const prepared = safeJsonParse(preparedJson, {}); const url = prepared.request; const init = { method: prepared.method, headers: prepared.headers, body: prepared.body }; const proxyUrl = proxyManager.selectForAccount(accountId, PROVIDER_ID); proxyByAccount.set(accountId, proxyUrl || null); if (proxyUrl) init.proxy = proxyUrl; // Bun fetch honors .proxy let response; const started = Date.now(); let proxyOk = false; try { response = await fetch(url, init); proxyOk = !!proxyUrl; } catch (error) { if (proxyUrl) { proxyManager.reportResult(proxyUrl, false); // proxy unreachable -> retry directly (a dead proxy gives no isolation anyway) log("fetch via proxy " + proxyUrl + " failed: " + error + ", retrying directly"); try { const directInit = { ...init }; delete directInit.proxy; response = await fetch(url, directInit); } catch (directError) { log("direct retry failed: " + directError); return JSON.stringify({ status: 0, headers: {}, transportFailed: true, attemptRef: -1 }); } } else { log("fetch failed: " + error); return JSON.stringify({ status: 0, headers: {}, transportFailed: true, attemptRef: -1 }); } } if (proxyOk) proxyManager.reportResult(proxyUrl, true, Date.now() - started); const attemptRef = responses.push(response) - 1; return JSON.stringify({ status: response.status, headers: Object.fromEntries(response.headers), transportFailed: false, attemptRef, }); }; // jsReports: the synchronous account-reporting callbacks over the real `manager`. const jsReports = { reportError(accountId, lane, attempt, message) { manager.reportError(accountId, lane, attempt, message); }, // Re-fire the proxy signal: after manager.reportRateLimit, if a proxy was used for this // account, re-fire proxyManager.reportRateLimit with the ipSuspected quality signal derived // from the fresh account state. This signal must not be dropped. reportRateLimit(accountId, lane, resetMsJson) { const resetMs = JSON.parse(resetMsJson); // number | null manager.reportRateLimit(accountId, lane, resetMs); const proxyUrl = proxyByAccount.get(accountId); if (proxyUrl) { const fresh = manager.list().find((a) => a.id === accountId) || acquiredByAccount.get(accountId); proxyManager.reportRateLimit(proxyUrl, { ipSuspected: accountHasQuota(fresh) }); } }, reportSuccess(accountId) { manager.reportSuccess(accountId); }, disable(accountId, reason) { manager.mutate(accountId, (a) => { a.enabled = false; a.disabledReason = reason; }); }, listEnabledCount() { return manager.list().filter((a) => a.enabled !== false).length; }, captureQuota(accountId, headersJson) { captureQuota(manager, accountId, headersFromJson(headersJson)); }, }; const { handleClaudeRequestAsync } = await claudeOrchestrator.load(); const decisionJson = await handleClaudeRequestAsync(inputsJson, configJson, jsExec, jsAcquire, jsReports); const decision = JSON.parse(decisionJson); if (decision.kind === "SERVE") { // Return the retained live Response verbatim (SSE/stream intact); no body crossed to Java. const retained = responses[decision.attemptRef]; if (retained) return retained; // Defensive: a SERVE with no retained response should be impossible (every SERVE ref comes // from a jsExec success). Surface a 502 rather than throwing into the host. return new Response(JSON.stringify({ error: { message: "internal: serve ref not retained" } }), { status: 502, headers: { "content-type": "application/json" }, }); } // SYNTHETIC: build the body here from the decision JSON (terminal/exhaustion paths). return new Response(decision.body, { status: decision.status, headers: decision.headers }); } // ---- handleIr (IR-native entry point) --------------------------------------------- // // The synthetic loader-facing URL used to re-enter handleViaJavaOrchestrator from an IrRequest // that never had a real inbound wire Request to begin with (handleIr's contract is (ir, ctx), no // url/headers): matches AnthropicRequestTranslator.pathOf's own fallback for an absent url. const IR_SYNTHETIC_URL = "https://loader.local/v1/messages"; // Non-2xx responses out of handleIr (real upstream "surface as-is", or one of the orchestrator's // own SYNTHETIC error bodies: chatErrorBody/errorResponseBody) are carried out via core-auth's // canonical HandleIrError, not returned as data. Neither body is guaranteed to be Anthropic // MESSAGE-shaped JSON (a SYNTHETIC body in particular can be missing the "type":"error" wrapper // entirely, see errorResponseBody), so forcing it through anthropicTranslator.decodeResponse // would corrupt it: the codec always injects id/content/model/stop_reason keys a bare error // envelope never had. handleIr throws HandleIrError instead, carrying the EXACT original bytes; // the front-door (core-proxy's server.ts/Router.java) catches this typed error and reconstructs // the response verbatim, restoring status fidelity on the IR path, so re-export it for // callers that only import from this module. export { HandleIrError }; /** * IR-native alternative to handleViaJavaOrchestrator: encodes the IrRequest to Anthropic wire * text, runs it through the same transport/orchestrator flow (via handleViaJavaOrchestrator), * then decodes a genuine 2xx response back to the canonical IR: a streamed SSE response becomes * a true-streaming IrEventStream (never buffered); a non-streaming response becomes an * IrResponse. Any non-2xx outcome throws {@link HandleIrError} rather than forcing a non-message * body through the IR (see its own comment above), mirroring core-proxy's own reference handleIr * contract, where a provider signals failure by throwing, not by returning IR. */ export async function handleIr(ir, ctx) { const bodyText = await anthropicTranslator.encodeRequest(ir); const request = new Request(IR_SYNTHETIC_URL, { method: "POST", headers: { "content-type": "application/json" }, body: bodyText, }); const response = await handleViaJavaOrchestrator(request, ctx); if (!response.ok) { const body = await response.text(); throw new HandleIrError({ status: response.status, headers: Object.fromEntries(response.headers), body }); } const contentType = response.headers.get("content-type") || ""; if (contentType.includes("text/event-stream") && response.body) { const decodeStream = await anthropicTranslator.decodeStream(); return response.body.pipeThrough(decodeStream); } const wireText = await response.text(); return await anthropicTranslator.decodeResponse(wireText); }