Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy
Rust
extract anthropic response helpers
Raine Virta712c1a838e10
diff --git a/src/anthropic/response.test.ts b/src/anthropic/response.test.tsnew file mode 100644index 0000000..221c97e--- /dev/null+++ b/src/anthropic/response.test.ts@@ -0,0 +1,86 @@+import { describe, expect, it } from "bun:test";+import {+ anthropicErrorBody,+ jsonError,+ jsonResponse,+ sseResponse,+ streamResponse,+} from "./response.ts";++describe("anthropicErrorBody", () => {+ it("returns Anthropic error payload shape", () => {+ expect(anthropicErrorBody("invalid_request_error", "Bad request")).toEqual({+ type: "error",+ error: { type: "invalid_request_error", message: "Bad request" },+ });+ });+});++describe("jsonResponse and jsonError", () => {+ it("normalizes JSON response shape and content type", async () => {+ const response = jsonError(400, "invalid_request_error", "Bad request");+ const body = (await response.json()) as { type: "error"; error: { type: string; message: string } };++ expect(response.status).toBe(400);+ expect(response.headers.get("content-type")).toBe("application/json");+ expect(body).toEqual({+ type: "error",+ error: { type: "invalid_request_error", message: "Bad request" },+ });+ });++ it("preserves custom headers when returning JSON errors", () => {+ const response = jsonError(429, "rate_limit_error", "Slow down", { "retry-after": "10" });+ expect(response.headers.get("retry-after")).toBe("10");+ expect(response.headers.get("content-type")).toBe("application/json");+ });++ it("preserves headers on generic JSON responses", async () => {+ const response = jsonResponse({ ok: true }, { headers: { "x-trace-id": "abc" } });+ expect(response.headers.get("content-type")).toBe("application/json");+ expect(response.headers.get("x-trace-id")).toBe("abc");+ expect(await response.json()).toEqual({ ok: true });+ });+});++describe("sseResponse", () => {+ it("adds SSE headers and preserves extra headers", () => {+ const response = sseResponse("data", {+ headers: {+ "x-upstream": "origin",+ "cache-control": "custom",+ connection: "close",+ },+ });++ expect(response.status).toBe(200);+ expect(response.headers.get("content-type")).toBe("text/event-stream");+ expect(response.headers.get("cache-control")).toBe("no-cache");+ expect(response.headers.get("connection")).toBe("keep-alive");+ expect(response.headers.get("x-upstream")).toBe("origin");+ });+});++describe("streamResponse", () => {+ it("strips hop-by-hop headers but preserves status, statusText and passthrough headers", async () => {+ const upstream = new Response("stream-data", {+ status: 203,+ statusText: "Accepted",+ headers: {+ "content-encoding": "gzip",+ "content-length": "10",+ "transfer-encoding": "chunked",+ "x-upstream": "provider",+ },+ });+ const response = streamResponse(upstream, upstream.body);++ expect(response.status).toBe(203);+ expect(response.statusText).toBe("Accepted");+ expect(response.headers.get("content-encoding")).toBeNull();+ expect(response.headers.get("content-length")).toBeNull();+ expect(response.headers.get("transfer-encoding")).toBeNull();+ expect(response.headers.get("x-upstream")).toBe("provider");+ expect(await response.text()).toBe("stream-data");+ });+});diff --git a/src/anthropic/response.ts b/src/anthropic/response.tsnew file mode 100644index 0000000..3086680--- /dev/null+++ b/src/anthropic/response.ts@@ -0,0 +1,64 @@+type ResponseBody = ConstructorParameters<typeof Response>[0];+type ResponseHeaders = ResponseInit["headers"];+type HeadersConstructorInput = ConstructorParameters<typeof Headers>[0];++export interface AnthropicErrorBody {+ type: "error";+ error: {+ type: string;+ message: string;+ };+}++export function anthropicErrorBody(type: string, message: string): AnthropicErrorBody {+ return { type: "error", error: { type, message } };+}++export function jsonError(status: number, type: string, message: string, headers?: ResponseHeaders): Response {+ return jsonResponse(anthropicErrorBody(type, message), { status, headers });+}++export function jsonResponse(body: unknown, init: ResponseInit = {}): Response {+ return new Response(JSON.stringify(body), {+ ...init,+ headers: jsonHeaders(init.headers),+ });+}++export function sseResponse(body: ResponseBody, init: ResponseInit = {}): Response {+ return new Response(body, {+ status: 200,+ ...init,+ headers: sseHeaders(init.headers),+ });+}++export function streamResponse(resp: Response, body: ResponseBody): Response {+ return new Response(body, {+ status: resp.status,+ statusText: resp.statusText,+ headers: streamResponseHeaders(resp.headers),+ });+}++export function streamResponseHeaders(headers: ResponseHeaders): Headers {+ const out = new Headers(headers as HeadersConstructorInput);+ out.delete("content-encoding");+ out.delete("content-length");+ out.delete("transfer-encoding");+ return out;+}++function jsonHeaders(headers: ResponseHeaders): Headers {+ const out = new Headers(headers as HeadersConstructorInput);+ out.set("content-type", "application/json");+ return out;+}++function sseHeaders(headers: ResponseHeaders): Headers {+ const out = new Headers(headers as HeadersConstructorInput);+ out.set("content-type", "text/event-stream");+ out.set("cache-control", "no-cache");+ out.set("connection", "keep-alive");+ return out;+}diff --git a/src/providers/codex/index.test.ts b/src/providers/codex/index.test.tsindex 8fbfa0a..4c7bce2 100644--- a/src/providers/codex/index.test.ts+++ b/src/providers/codex/index.test.ts@@ -31,6 +31,7 @@ describe("codexProvider", () => {);expect(resp.status).toBe(400);+ expect(resp.headers.get("content-type")).toBe("application/json");expect(await resp.json()).toEqual({type: "error",error: {@@ -49,6 +50,7 @@ describe("codexProvider", () => {);expect(resp.status).toBe(400);+ expect(resp.headers.get("content-type")).toBe("application/json");expect(await resp.json()).toEqual({type: "error",error: {diff --git a/src/providers/codex/index.ts b/src/providers/codex/index.tsindex 37dc74b..31f6ee6 100644--- a/src/providers/codex/index.ts+++ b/src/providers/codex/index.ts@@ -1,6 +1,12 @@import type { AnthropicRequest } from "../../anthropic/schema.ts";import { wantsDownstreamStream } from "../../anthropic/stream.ts";import type { Provider, RequestContext, CliHandlers } from "../types.ts";+import {+ anthropicErrorBody,+ jsonError,+ jsonResponse,+ sseResponse,+} from "../../anthropic/response.ts";import {ALLOWED_MODELS,assertAllowedModel,@@ -127,13 +133,6 @@ function warnForHeavyImages(});}-function jsonError(status: number, type: string, message: string): Response {- return new Response(JSON.stringify({ type: "error", error: { type, message } }), {- status,- headers: { "content-type": "application/json" },- });-}-function invalidServiceTierResponse(err: InvalidServiceTierError): Response {return jsonError(400, "invalid_request_error", err.message);}@@ -190,9 +189,7 @@ async function handleCountTokens(body: AnthropicRequest, ctx: RequestContext): PpreviousMessageTranslatedInputTokens: state?.lastMessage?.translatedInputTokens,});}- return new Response(JSON.stringify({ input_tokens: tokens }), {- headers: { "content-type": "application/json" },- });+ return jsonResponse({ input_tokens: tokens });}async function handleMessages(body: AnthropicRequest, ctx: RequestContext): Promise<Response> {@@ -305,14 +302,14 @@ async function handleMessages(body: AnthropicRequest, ctx: RequestContext): Promif (err instanceof CodexError) {log.warn("codex error", { status: err.status, detail: err.detail });if (err.status === 429) {- const headers: Record<string, string> = { "content-type": "application/json" };+ const headers: Record<string, string> = {};if (err.meta?.retryAfter) headers["retry-after"] = err.meta.retryAfter;- return new Response(- JSON.stringify({- type: "error",- error: { type: "rate_limit_error", message: err.detail || err.message },- }),- { status: 429, headers },+ return jsonResponse(+ anthropicErrorBody("rate_limit_error", err.detail || err.message),+ {+ status: 429,+ headers,+ },);}const type = err.status === 401 || err.status === 403 ? "authentication_error" : "api_error";@@ -368,14 +365,7 @@ async function handleMessages(body: AnthropicRequest, ctx: RequestContext): Prom},onInvalidateContinuation: () => clearContinuation(ctx.sessionId),});- return new Response(stream, {- status: 200,- headers: {- "content-type": "text/event-stream",- "cache-control": "no-cache",- connection: "keep-alive",- },- });+ return sseResponse(stream);}try {@@ -417,9 +407,7 @@ async function handleMessages(body: AnthropicRequest, ctx: RequestContext): PromstopReason: result.response.stop_reason,});}- return new Response(JSON.stringify(result.response), {- headers: { "content-type": "application/json" },- });+ return jsonResponse(result.response);} catch (err) {clearContinuation(ctx.sessionId);if (err instanceof UpstreamStreamError) {@@ -428,13 +416,10 @@ async function handleMessages(body: AnthropicRequest, ctx: RequestContext): Prommessage: err.message,});if (err.kind === "rate_limit") {- const headers: Record<string, string> = { "content-type": "application/json" };+ const headers: Record<string, string> = {};if (err.retryAfterSeconds) headers["retry-after"] = String(err.retryAfterSeconds);- return new Response(- JSON.stringify({- type: "error",- error: { type: "rate_limit_error", message: err.message },- }),+ return jsonResponse(+ anthropicErrorBody("rate_limit_error", err.message),{ status: 429, headers },);}diff --git a/src/providers/cursor/index.test.ts b/src/providers/cursor/index.test.tsindex 3c8f2f2..745824d 100644--- a/src/providers/cursor/index.test.ts+++ b/src/providers/cursor/index.test.ts@@ -28,6 +28,7 @@ describe("Cursor provider auth errors", () => {};expect(response.status).toBe(401);+ expect(response.headers.get("content-type")).toBe("application/json");expect(body.error.type).toBe("authentication_error");expect(body.error.message).toContain("expired or near expiry");});@@ -56,6 +57,7 @@ describe("Cursor provider messages", () => {const body = (await response.json()) as { content: Array<{ type: string; text?: string }> };expect(response.status).toBe(200);+ expect(response.headers.get("content-type")).toBe("application/json");expect(body.content).toEqual([{ type: "text", text: "hello" }]);});@@ -85,6 +87,8 @@ describe("Cursor provider messages", () => {}expect(response.headers.get("content-type")).toBe("text/event-stream");+ expect(response.headers.get("cache-control")).toBe("no-cache");+ expect(response.headers.get("connection")).toBe("keep-alive");expect(events.map((event) => event.event)).toContain("message_start");expect(events.find((event) => event.event === "content_block_delta")?.data.delta.text).toBe("streamed",diff --git a/src/providers/cursor/index.ts b/src/providers/cursor/index.tsindex d338d89..def68d7 100644--- a/src/providers/cursor/index.ts+++ b/src/providers/cursor/index.ts@@ -1,5 +1,6 @@import type { AnthropicRequest } from "../../anthropic/schema.ts";import { wantsDownstreamStream } from "../../anthropic/stream.ts";+import { jsonError, jsonResponse, sseResponse } from "../../anthropic/response.ts";import { logVerbose } from "../../config.ts";import type { CliHandlers, Provider, RequestContext } from "../types.ts";import {@@ -42,19 +43,10 @@ const defaultDeps: CursorProviderDeps = {runAgent: runCursorAgent,};-function jsonError(status: number, type: string, message: string): Response {- return new Response(JSON.stringify({ type: "error", error: { type, message } }), {- status,- headers: { "content-type": "application/json" },- });-}-async function handleCountTokens(body: AnthropicRequest, ctx: RequestContext): Promise<Response> {const tokens = countCursorTokens(body);ctx.childLogger("provider.cursor").debug("count_tokens", { tokens });- return new Response(JSON.stringify({ input_tokens: tokens }), {- headers: { "content-type": "application/json" },- });+ return jsonResponse({ input_tokens: tokens });}async function handleMessages(@@ -124,14 +116,7 @@ async function handleMessages(proto: deps.proto,onSession,});- return new Response(stream, {- status: 200,- headers: {- "content-type": "text/event-stream",- "cache-control": "no-cache",- connection: "keep-alive",- },- });+ return sseResponse(stream);}try {@@ -143,9 +128,7 @@ async function handleMessages(proto: deps.proto,onSession,});- return new Response(JSON.stringify(result.response), {- headers: { "content-type": "application/json" },- });+ return jsonResponse(result.response);} catch (err) {log.warn("cursor accumulate error", { err: String(err) });if (err instanceof CursorError) {diff --git a/src/providers/kimi/index.test.ts b/src/providers/kimi/index.test.tsnew file mode 100644index 0000000..9422d3e--- /dev/null+++ b/src/providers/kimi/index.test.ts@@ -0,0 +1,38 @@+import { afterEach, describe, expect, it } from "bun:test";+import type { RequestContext } from "../types.ts";+import { loadConfig } from "../../config.ts";+import { kimiProvider } from "./index.ts";++afterEach(() => {+ loadConfig({ forceReload: true });+});++describe("kimiProvider", () => {+ it("returns token count JSON responses", async () => {+ const response = await kimiProvider.handleCountTokens(+ { model: "kimi-for-coding", messages: [{ role: "user", content: "hello" }] },+ fakeCtx(),+ );+ const body = (await response.json()) as { input_tokens: number };++ expect(response.status).toBe(200);+ expect(response.headers.get("content-type")).toBe("application/json");+ expect(body.input_tokens).toBeGreaterThan(0);+ });+});++function fakeCtx(): RequestContext {+ return {+ reqId: "kimi-req",+ signal: new AbortController().signal,+ childLogger: () => ({+ debug() {},+ info() {},+ warn() {},+ error() {},+ child() {+ return this;+ },+ }),+ };+}diff --git a/src/providers/kimi/index.ts b/src/providers/kimi/index.tsindex 087fcc6..564f150 100644--- a/src/providers/kimi/index.ts+++ b/src/providers/kimi/index.ts@@ -1,6 +1,12 @@import type { Provider, CliHandlers, RequestContext } from "../types.ts";import type { AnthropicRequest } from "../../anthropic/schema.ts";import { wantsDownstreamStream } from "../../anthropic/stream.ts";+import {+ anthropicErrorBody,+ jsonError,+ jsonResponse,+ sseResponse,+} from "../../anthropic/response.ts";import {assertAllowedModel,ModelNotAllowedError,@@ -17,22 +23,13 @@ import { persistInitialTokens } from "./auth/manager.ts";import { loadAuth, clearAuth, authPath } from "./auth/token-store.ts";import { logVerbose } from "../../config.ts";-function jsonError(status: number, type: string, message: string): Response {- return new Response(JSON.stringify({ type: "error", error: { type, message } }), {- status,- headers: { "content-type": "application/json" },- });-}-async function handleCountTokens(body: AnthropicRequest, ctx: RequestContext): Promise<Response> {const log = ctx.childLogger("provider.kimi");const resolvedModel = resolveModel(body.model);const translated = translateRequest({ ...body, model: resolvedModel });const tokens = countTranslatedTokens(translated);log.debug("count_tokens", { tokens });- return new Response(JSON.stringify({ input_tokens: tokens }), {- headers: { "content-type": "application/json" },- });+ return jsonResponse({ input_tokens: tokens });}async function handleMessages(body: AnthropicRequest, ctx: RequestContext): Promise<Response> {@@ -92,14 +89,14 @@ async function handleMessages(body: AnthropicRequest, ctx: RequestContext): Promif (err instanceof KimiError) {log.warn("kimi error", { status: err.status, detail: err.detail });if (err.status === 429) {- const headers: Record<string, string> = { "content-type": "application/json" };+ const headers: Record<string, string> = {};if (err.meta?.retryAfter) headers["retry-after"] = err.meta.retryAfter;- return new Response(- JSON.stringify({- type: "error",- error: { type: "rate_limit_error", message: err.detail || err.message },- }),- { status: 429, headers },+ return jsonResponse(+ anthropicErrorBody("rate_limit_error", err.detail || err.message),+ {+ status: 429,+ headers,+ },);}const type = err.status === 401 || err.status === 403 ? "authentication_error" : "api_error";@@ -130,14 +127,7 @@ async function handleMessages(body: AnthropicRequest, ctx: RequestContext): Prom});},});- return new Response(stream, {- status: 200,- headers: {- "content-type": "text/event-stream",- "cache-control": "no-cache",- connection: "keep-alive",- },- });+ return sseResponse(stream);}try {@@ -147,9 +137,7 @@ async function handleMessages(body: AnthropicRequest, ctx: RequestContext): Promlog: ctx.childLogger("kimi.accumulate"),traffic: ctx.traffic,});- return new Response(JSON.stringify(result.response), {- headers: { "content-type": "application/json" },- });+ return jsonResponse(result.response);} catch (err) {if (err instanceof UpstreamStreamError) {log.warn("upstream stream error (non-streaming)", {@@ -157,13 +145,10 @@ async function handleMessages(body: AnthropicRequest, ctx: RequestContext): Prommessage: err.message,});if (err.kind === "rate_limit") {- const headers: Record<string, string> = { "content-type": "application/json" };+ const headers: Record<string, string> = {};if (err.retryAfterSeconds) headers["retry-after"] = String(err.retryAfterSeconds);- return new Response(- JSON.stringify({- type: "error",- error: { type: "rate_limit_error", message: err.message },- }),+ return jsonResponse(+ anthropicErrorBody("rate_limit_error", err.message),{ status: 429, headers },);}diff --git a/src/server.test.ts b/src/server.test.tsindex ff862f7..71b1546 100644--- a/src/server.test.ts+++ b/src/server.test.ts@@ -1,6 +1,7 @@import { afterEach, describe, expect, it } from "bun:test";import { loadConfig } from "./config.ts";-import { startServer, normalizeIncomingModel } from "./server.ts";+import type { createLogger } from "./log.ts";+import { startServer, normalizeIncomingModel, wrapStreamResponse } from "./server.ts";import { startTestServer } from "./test/server.ts";const servers: Array<{ stop: () => void }> = [];@@ -29,6 +30,87 @@ describe("normalizeIncomingModel", () => {});});+describe("server error responses", () => {+ it("returns JSON for health checks", async () => {+ const server = startTestServer(startServer);+ servers.push(server);++ const resp = await fetch(`http://127.0.0.1:${server.port}/healthz`);+ const body = (await resp.json()) as { ok: boolean };++ expect(resp.status).toBe(200);+ expect(resp.headers.get("content-type")).toBe("application/json");+ expect(body).toEqual({ ok: true });+ });++ it("returns JSON for unknown routes", async () => {+ const server = startTestServer(startServer);+ servers.push(server);++ const resp = await fetch(`http://127.0.0.1:${server.port}/not-a-route`);+ const body = (await resp.json()) as { type: string; error: { type: string; message: string } };++ expect(resp.status).toBe(404);+ expect(resp.headers.get("content-type")).toBe("application/json");+ expect(body).toEqual({+ type: "error",+ error: {+ type: "not_found",+ message: "No route for GET /not-a-route",+ },+ });+ });++ it("keeps invalid JSON request parsing failures as JSON", async () => {+ const server = startTestServer(startServer);+ servers.push(server);++ const resp = await fetch(`http://127.0.0.1:${server.port}/v1/messages`, {+ method: "POST",+ headers: { "content-type": "application/json" },+ body: "{",+ });+ const body = (await resp.json()) as { error: { type: string; message: string } };++ expect(resp.status).toBe(400);+ expect(resp.headers.get("content-type")).toBe("application/json");+ expect(body.error.type).toBe("invalid_request_error");+ });+});++describe("stream wrapper", () => {+ it("strips hop-by-hop headers when wrapping upstream streams", async () => {+ const upstream = new Response("hello", {+ status: 207,+ statusText: "Multi-Status",+ headers: {+ "content-encoding": "gzip",+ "content-length": "5",+ "transfer-encoding": "chunked",+ "x-upstream": "ok",+ },+ });++ const wrapped = wrapStreamResponse(upstream, "test", 0, {+ info() {},+ error() {},+ warn() {},+ debug() {},+ child() {+ return this;+ },+ } as ReturnType<typeof createLogger>);++ expect(wrapped.status).toBe(207);+ expect(wrapped.statusText).toBe("Multi-Status");+ expect(wrapped.headers.get("content-encoding")).toBeNull();+ expect(wrapped.headers.get("content-length")).toBeNull();+ expect(wrapped.headers.get("transfer-encoding")).toBeNull();+ expect(wrapped.headers.get("x-upstream")).toBe("ok");+ expect(await wrapped.text()).toBe("hello");+ });+});+describe("server session-aware alias routing", () => {it("routes aliases to the concrete provider used earlier in the session", async () => {loadConfig({ env: { CCP_CODEX_SERVICE_TIER: "standard" }, forceReload: true });diff --git a/src/server.ts b/src/server.tsindex ce6b80c..cab4a73 100644--- a/src/server.ts+++ b/src/server.ts@@ -1,5 +1,6 @@import { createLogger, logDir, REDACT_KEYS } from "./log.ts";import { createTrafficCapture, headersToRecord } from "./traffic.ts";+import { jsonError, jsonResponse, streamResponse } from "./anthropic/response.ts";import type { AnthropicRequest } from "./anthropic/schema.ts";import type { AliasProvider } from "./config.ts";@@ -111,9 +112,7 @@ export function startServer(opts: ServeOptions): { stop: () => void; port: numbeasync function route(req: Request, url: URL, reqId: string): Promise<Response> {if (url.pathname === "/healthz") {- return new Response(JSON.stringify({ ok: true }), {- headers: { "content-type": "application/json" },- });+ return jsonResponse({ ok: true });}if (req.method === "POST" && url.pathname === "/v1/messages/count_tokens") {@@ -252,7 +251,7 @@ function isClosedControllerError(err: unknown): boolean {return err instanceof TypeError && err.message.includes("Controller is already closed");}-function wrapStreamResponse(+export function wrapStreamResponse(resp: Response,reqId: string,start: number,@@ -311,15 +310,7 @@ function wrapStreamResponse(reader.cancel().catch(() => {});},});- const headers = new Headers(resp.headers);- headers.delete("content-encoding");- headers.delete("content-length");- headers.delete("transfer-encoding");- return new Response(stream, {- status: resp.status,- statusText: resp.statusText,- headers,- });+ return streamResponse(resp, stream);}function redactedQuery(url: URL): Record<string, string> {@@ -329,10 +320,3 @@ function redactedQuery(url: URL): Record<string, string> {}return out;}--function jsonError(status: number, type: string, message: string): Response {- return new Response(JSON.stringify({ type: "error", error: { type, message } }), {- status,- headers: { "content-type": "application/json" },- });-}