Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions free/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ It covers **Claude, ChatGPT (Codex) and Kimi subscriptions** and **API keys** fo
- Claude Code on a ChatGPT login or a Codex API key: ask for a ChatGPT model (for example `ANTHROPIC_MODEL=gpt-5.6-sol`) and the request is translated to OpenAI Responses and the answer streamed back as Anthropic events, with thinking, tool calls and images, following CPA's Claude-to-Codex translator.
- Claude Code on a Gemini API key: ask for a Gemini model (for example `ANTHROPIC_MODEL=gemini-2.5-flash`) and the request goes out as Gemini `generateContent`, with thinking, tool calls and images, following CPA's Claude-to-Gemini translator. Unlike CPA, Google's default safety filters stay on; a blocked answer ends with a `refusal` stop reason.
- OpenAI Chat clients (Cursor, Cline, the OpenAI SDKs and the like) on a Claude API key: ask `/v1/chat/completions` for a Claude model and the request goes out as Anthropic Messages, with reasoning effort, tool calls, images, files and prompt caching, and the answer comes back as Chat Completions, following CPA's OpenAI-to-Claude translator. Claude logins aren't offered to these clients, since without CPA's cloaking Anthropic accepts a subscription token from Claude Code only.
- Each client talks in its own format: Anthropic (`/v1/messages`), OpenAI Chat (`/v1/chat/completions`), OpenAI Responses (`/v1/responses`) and Gemini (`/v1beta/models/{model}:generateContent`). A request goes to a provider that takes the same format, apart from the Claude translations above.
- OpenAI Responses clients on a Claude API key, the same way, following CPA's Responses-to-Claude translator: reasoning, function and custom tool calls, images and files, streamed as Responses events or returned whole.
- OpenAI Chat and Responses clients on any other provider too (a Gemini key, an OpenAI-compatible key, a Codex key or a ChatGPT login): the request is translated to Anthropic Messages and on to the provider's format, and the answer back the same way.
- Each client talks in its own format: Anthropic (`/v1/messages`), OpenAI Chat (`/v1/chat/completions`), OpenAI Responses (`/v1/responses`) and Gemini (`/v1beta/models/{model}:generateContent`). A request goes to a provider that takes the same format, apart from the translations above.
- Model lists as in CPA: `GET /v1/models` (OpenAI's shape, or Anthropic's for Claude Code and the Anthropic SDKs), `GET /v1beta/models` and `GET /v1beta/models/{model}` list every model across the logins and API keys, and so does the dashboard's **Available Models**. A login or key with no model list of its own lists CPA's catalog for its provider, and a ChatGPT login the catalog for its plan. The catalog is refreshed from CPA's model catalog every three hours, with a built-in copy as fallback; it only decides what is listed, never where a request goes.
- Usage and logs as in CPA: each login and API key shows its success and failure counts and the last 200 minutes in 10-minute buckets, and with **Logging to file** on, the **Logs** page shows a line per API request (the last 2,000). Lines hold the time, status, latency, client IP, path, provider and model, never request bodies or keys. Records are sent to the Durable Object in batches a few seconds apart.
- Everything is stored in one SQLite-backed Durable Object, which the free plan includes.

Claude Code and Codex CLI request bodies are never parsed, so a Worker spends well under 1 ms of its 10 ms CPU budget even on a 600 KB Claude Code request (see the CPU test in `test/app.test.ts`). Requests that need a model rewritten or a body adapted are parsed once. Translated requests cost the most, about 4 to 6 ms for a 600 KB conversation; very long conversations translated can run past the free plan's 10 ms, which the Container install doesn't have.

Not included yet: the other client translations (OpenAI Responses and Gemini clients to other providers), Gemini and other OAuth logins, the Codex CLI's own model list (`/v1/models?client_version=`), and CPA's per-request detail logs. Deliberately left out: CPA's request cloaking and TLS fingerprint spoofing. Clients other than Claude Code may be refused by Anthropic when they use a subscription token.
Not included yet: Gemini clients on other providers, Gemini and other OAuth logins, the Codex CLI's own model list (`/v1/models?client_version=`), and CPA's per-request detail logs. Deliberately left out: CPA's request cloaking and TLS fingerprint spoofing. Clients other than Claude Code may be refused by Anthropic when they use a subscription token.

## Try it

Expand Down
67 changes: 39 additions & 28 deletions free/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { hashKey, REFRESH_MARGIN_SECONDS, type Backend, type ServedLogin } from
import type { Fetcher } from "./claude-oauth.ts";
import { claudeToGemini, geminiClaudeMessage, GeminiClaudeStream } from "./claude-gemini.ts";
import { chatClaudeMessage, ChatClaudeStream, claudeToChat } from "./claude-openai.ts";
import { claudeEventStream, claudeMessage, ClaudeStream, claudeToResponses } from "./claude-responses.ts";
import { claudeEventStream, claudeMessage, ClaudeStream, claudeToResponses, type EventTranslator, type Json } from "./claude-responses.ts";
import { chatCompletion, ChatStream, chatToClaude } from "./openai-claude.ts";
import { kimiUpstreamModel } from "./kimi-oauth.ts";
import { responsesResponse, ResponsesStream, responsesToClaude } from "./responses-claude.ts";
import { MANAGEMENT_PREFIX } from "./management.ts";
import { availableModels, CatalogCache, claudeModelList, geminiModel, geminiModelList, openaiModelList } from "./models.ts";
import { loginPage, messagePage } from "./pages.ts";
Expand Down Expand Up @@ -222,13 +223,10 @@ async function viaCodexLogin(request: Request, url: URL, config: AppConfig, text
return collected(upstream);
}

// Formats a provider takes with translation: Claude clients can reach Responses, Chat and
// Gemini upstreams, and OpenAI Chat clients can reach Claude API keys.
// Formats a provider takes with translation: Claude, OpenAI Chat and Responses clients reach
// every provider through Anthropic Messages.
function servable(format: Format, kind: Candidate["credential"]["kind"]): boolean {
const native = NATIVE_FORMAT[kind];
if (takesNatively(kind, format)) return true;
if (format === "claude") return native === "openai-response" || native === "openai" || native === "gemini";
return format === "openai" && native === "claude";
return takesNatively(kind, format) || format !== "gemini";
}

function parsedObject(text: string): Record<string, unknown> | null {
Expand Down Expand Up @@ -256,6 +254,11 @@ async function viaTranslation(request: Request, url: URL, route: Route, config:
const claude = parsedObject(text);
if (claude === null) return formatError("claude", 400, "invalid_request_error", "The request body must be a JSON object.");
if (url.pathname.endsWith("/count_tokens")) return Response.json({ input_tokens: Math.ceil(text.length / 4) }, { headers: { "cache-control": "no-store" } });
return translatedClaude(request, route, config, candidate, claude);
}

async function translatedClaude(request: Request, route: Route, config: AppConfig, candidate: Candidate, claude: Json): Promise<Response> {
const url = new URL(request.url);
const requested = typeof claude.model === "string" ? claude.model : candidate.model;
const native = NATIVE_FORMAT[candidate.credential.kind];
const translated = native === "openai" ? { ...claudeToChat(claude, candidate.model), toolNames: new Map<string, string>() } : native === "gemini" ? claudeToGemini(claude, candidate.model) : await claudeToResponses(claude, candidate.model);
Expand Down Expand Up @@ -301,25 +304,33 @@ async function viaTranslation(request: Request, url: URL, route: Route, config:
return Response.json(claudeMessage((await final.json()) as Record<string, unknown>, requested, translated.toolNames), { headers: { "cache-control": "no-store" } });
}

// An OpenAI Chat request served by a Claude API key: the body is translated to Anthropic
// Messages, and the answer translated back, streamed or whole.
async function viaClaudeKey(request: Request, config: AppConfig, candidate: Candidate, text: string): Promise<Response> {
const chat = parsedObject(text);
if (chat === null) return formatError("openai", 400, "invalid_request_error", "The request body must be a JSON object.");
const requested = typeof chat.model === "string" ? chat.model : candidate.model;
const translated = chatToClaude(chat, candidate.model);
// The client's own headers describe an OpenAI request, so none of them go upstream.
const bare = new Request(request.url, { method: "POST" });
const target = new URL("/v1/messages", request.url);
const upstream = await config.upstream(providerRequest(bare, target, { method: "POST", format: "claude" }, candidate.credential, candidate.model, JSON.stringify(translated.body))).catch(() => null);
if (upstream === null) return formatError("openai", 502, "api_error", "The upstream couldn't be reached.");
if (!upstream.ok) return formatError("openai", upstream.status, upstream.status === 429 ? "rate_limit_error" : "api_error", await upstreamMessage(upstream));
// An OpenAI Chat or Responses request: the body is translated to Anthropic Messages and sent
// to a Claude API key, or translated on to the candidate's own format; the answer comes back
// the same way, streamed or whole.
async function viaClaudeMessages(request: Request, url: URL, route: Route & { format: Format }, config: AppConfig, candidate: Candidate, text: string): Promise<Response> {
const client = parsedObject(text);
if (client === null) return formatError(route.format, 400, "invalid_request_error", "The request body must be a JSON object.");
const requested = typeof client.model === "string" ? client.model : candidate.model;
const translated = route.format === "openai-response" ? responsesToClaude(client, candidate.model) : { ...chatToClaude(client, candidate.model), customTools: new Set<string>() };
let upstream: Response | null;
if (NATIVE_FORMAT[candidate.credential.kind] === "claude") {
// The client's own headers describe an OpenAI request, so none of them go upstream.
const bare = new Request(request.url, { method: "POST" });
const target = new URL("/v1/messages", request.url);
upstream = await config.upstream(providerRequest(bare, target, { method: "POST", format: "claude" }, candidate.credential, candidate.model, JSON.stringify(translated.body))).catch(() => null);
} else {
upstream = await translatedClaude(new Request(new URL("/v1/messages", url), { method: "POST" }), { method: "POST", format: "claude" }, config, candidate, translated.body);
}
if (upstream === null) return formatError(route.format, 502, "api_error", "The upstream couldn't be reached.");
if (!upstream.ok) return formatError(route.format, upstream.status, upstream.status === 429 ? "rate_limit_error" : "api_error", await upstreamMessage(upstream));
if (translated.stream && upstream.body !== null) {
return new Response(claudeEventStream(upstream.body, new ChatStream(requested, translated.toolNames)), { headers: { "content-type": "text/event-stream; charset=utf-8", "cache-control": "no-cache" } });
const translator: EventTranslator = route.format === "openai-response" ? new ResponsesStream(requested, translated.toolNames, translated.customTools) : new ChatStream(requested, translated.toolNames);
return new Response(claudeEventStream(upstream.body, translator), { headers: { "content-type": "text/event-stream; charset=utf-8", "cache-control": "no-cache" } });
}
const final = parsedObject(await upstream.text());
if (final === null) return formatError("openai", 502, "api_error", "The upstream sent a response that isn't JSON.");
return Response.json(chatCompletion(final, requested, translated.toolNames), { headers: { "cache-control": "no-store" } });
if (final === null) return formatError(route.format, 502, "api_error", "The upstream sent a response that isn't JSON.");
const answer = route.format === "openai-response" ? responsesResponse(final, requested, translated.toolNames, translated.customTools) : chatCompletion(final, requested, translated.toolNames);
return Response.json(answer, { headers: { "cache-control": "no-store" } });
}

// Statuses after which another credential may well succeed.
Expand Down Expand Up @@ -415,8 +426,8 @@ async function proxied(request: Request, url: URL, route: Route & { format: Form
if (route.format === "claude" && table.claudeOAuth && /^claude-/i.test(model)) usable.push(loginCandidate("claude", model));
// ChatGPT logins take Codex's own models, and any Responses model nothing else serves.
if (route.format === "openai-response" && table.codexOAuth && (inDefaultFamily("codex", model) || usable.length === 0)) usable.push(loginCandidate("codex", model));
// Claude Code asking for a ChatGPT model by name, e.g. ANTHROPIC_MODEL=gpt-5.6-sol.
if (route.format === "claude" && table.codexOAuth && inDefaultFamily("codex", model)) usable.push(loginCandidate("codex", model));
// Claude Code or a Chat client asking for a ChatGPT model by name, e.g. ANTHROPIC_MODEL=gpt-5.6-sol.
if ((route.format === "claude" || route.format === "openai") && table.codexOAuth && inDefaultFamily("codex", model)) usable.push(loginCandidate("codex", model));
// Vertex service accounts take Gemini models, from Gemini clients and translated Claude ones.
if ((route.format === "gemini" || route.format === "claude") && table.vertexOAuth === true && inDefaultFamily("vertex", model)) usable.push(loginCandidate("vertex", model));
// Kimi logins take Kimi's models in Anthropic and both OpenAI formats, untranslated.
Expand All @@ -429,8 +440,8 @@ async function proxied(request: Request, url: URL, route: Route & { format: Form
}
// Without CPA's cloaking a Claude login serves Claude Code only, so it isn't offered here.
const reason =
route.format === "openai" && table.claudeOAuth && /^claude-/i.test(model)
? `Claude logins serve Claude Code only. To use ${model} from an OpenAI client, add a Claude API key under AI Providers in the dashboard.`
route.format !== "claude" && table.claudeOAuth && /^claude-/i.test(model)
? `Claude logins serve Claude Code only. To use ${model} from another client, add a Claude API key under AI Providers in the dashboard.`
: found.length > 0
? `Model ${model} is served by a ${found[0]?.credential.kind} provider, which doesn't take this request format yet.`
: `No provider is configured for model ${model}. Add one under AI Providers in the dashboard.`;
Expand Down Expand Up @@ -464,7 +475,7 @@ async function proxied(request: Request, url: URL, route: Route & { format: Form
const translated = !takesNatively(candidate.credential.kind, route.format);
if (translated || candidate.credential.id === CLAUDE_LOGIN_ID || candidate.credential.id === CODEX_LOGIN_ID) {
let response: Response;
if (translated && route.format === "openai") response = await viaClaudeKey(request, config, candidate, text);
if (translated && route.format !== "claude") response = await viaClaudeMessages(request, url, route, config, candidate, text);
else if (translated) response = await viaTranslation(request, url, route, config, candidate, text);
else if (candidate.credential.id === CLAUDE_LOGIN_ID) response = await viaClaudeLogin(request, url, config, body);
else response = await viaCodexLogin(request, url, config, body);
Expand Down
20 changes: 10 additions & 10 deletions free/src/openai-claude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { isObject, text, type EventTranslator, type Json } from "./claude-respon
// Translation between OpenAI Chat Completions clients and Anthropic Messages, for Claude API
// keys, ported from CPA's translator/claude/openai/chat-completions.

const DEFAULT_MAX_TOKENS = 32000;
const FABLE_MAX_TOKENS = 64000;
export const DEFAULT_MAX_TOKENS = 32000;
export const FABLE_MAX_TOKENS = 64000;
// Claude 4.5 and older take a thinking budget; newer models think adaptively at an effort.
const BUDGET_THINKING = /^claude-(?:3|(?:opus|sonnet|haiku)-4(?:-[0-5])?(?:-\d{8})?$|(?:opus|sonnet|haiku)-4-[0-5]-)/i;
const EFFORT_BUDGET: Record<string, number> = { minimal: 512, low: 1024, medium: 8192, high: 24576, xhigh: 32768, max: 128000 };
Expand All @@ -15,15 +15,15 @@ export function claudeName(name: string): string {
return name.replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 64) || "_";
}

function toolId(id: string): string {
export function toolId(id: string): string {
return id.replace(/[^a-zA-Z0-9_-]/g, "_") || `toolu_${crypto.randomUUID().replaceAll("-", "").slice(0, 24)}`;
}

function withCache(block: Json, from: Json): Json {
export function withCache(block: Json, from: Json): Json {
return isObject(from.cache_control) ? { ...block, cache_control: from.cache_control } : block;
}

function dataUrl(url: string): { mediaType: string; data: string } | null {
export function dataUrl(url: string): { mediaType: string; data: string } | null {
if (!url.startsWith("data:")) return null;
const comma = url.indexOf(",");
if (comma === -1) return null;
Expand Down Expand Up @@ -64,7 +64,7 @@ function toolResultContent(content: unknown): string | Json[] {
}

// Structured output has no Claude field, so CPA asks for it in the system prompt.
function formatInstruction(format: unknown): string {
export function formatInstruction(format: unknown): string {
if (!isObject(format)) return "";
const plain = "You must format your entire response as a valid JSON object. Do not include any explanations, markdown code blocks (such as ```json), or any text outside of the JSON object.";
const type = text(format.type).trim().toLowerCase();
Expand All @@ -83,7 +83,7 @@ function formatInstruction(format: unknown): string {
}

// Claude wants an object schema without a root-level union: object branches are merged in.
function inputSchema(raw: unknown): Json {
export function inputSchema(raw: unknown): Json {
if (!isObject(raw)) return { type: "object", properties: {} };
const { anyOf, oneOf, allOf, ...schema } = raw;
const properties: Json = isObject(schema.properties) ? { ...schema.properties } : {};
Expand All @@ -102,7 +102,7 @@ function inputSchema(raw: unknown): Json {
return { ...schema, type: "object", properties };
}

function thinking(effort: string, model: string, maxTokens: number): Json | null {
export function thinking(effort: string, model: string, maxTokens: number): Json | null {
if (effort === "") return null;
if (effort === "none") return { thinking: { type: "disabled" } };
if (!BUDGET_THINKING.test(model)) {
Expand Down Expand Up @@ -281,15 +281,15 @@ function finishReason(stop: string): string {
return "stop";
}

interface Usage {
export interface Usage {
input: number;
output: number;
cacheWrite: number;
cacheRead: number;
thinking: number | null;
}

function mergeUsage(usage: Usage, raw: unknown): boolean {
export function mergeUsage(usage: Usage, raw: unknown): boolean {
if (!isObject(raw)) return false;
const number = (value: unknown) => (typeof value === "number" && Number.isFinite(value) ? value : undefined);
usage.input = number(raw.input_tokens) ?? usage.input;
Expand Down
Loading
Loading