Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy
Rust
share provider core helpers
Raine Virta146efbe34674
diff --git a/src/providers/codex/cli.ts b/src/providers/codex/cli.tsindex 9030714..5107c6d 100644--- a/src/providers/codex/cli.ts+++ b/src/providers/codex/cli.ts@@ -3,6 +3,7 @@ import { runDeviceLogin } from "./auth/device.ts";import { persistInitialTokens } from "./auth/manager.ts";import { runBrowserLogin } from "./auth/pkce.ts";import { authPath, clearAuth, loadAuth } from "./auth/token-store.ts";+import { printAuthStatus } from "../shared/cli-status.ts";export const codexCli: CliHandlers = {async login() {@@ -18,15 +19,11 @@ export const codexCli: CliHandlers = {if (saved.accountId) console.log(`Account: ${saved.accountId}`);},async status() {- const auth = await loadAuth();- if (!auth) {- console.log("Not authenticated");- process.exit(1);- }- const ms = auth.expires - Date.now();- console.log(`Account: ${auth.accountId ?? "(none)"}`);- console.log(`Expires: ${new Date(auth.expires).toISOString()} (in ${Math.floor(ms / 1000)}s)`);- console.log(`Storage: ${authPath()}`);+ await printAuthStatus({+ loadAuth,+ authPath,+ formatIdentityLine: (auth) => `Account: ${auth.accountId ?? "(none)"}`,+ });},async logout() {await clearAuth();diff --git a/src/providers/codex/count-tokens.ts b/src/providers/codex/count-tokens.tsindex 13262e2..204ae21 100644--- a/src/providers/codex/count-tokens.ts+++ b/src/providers/codex/count-tokens.ts@@ -6,6 +6,7 @@ import {normalizeContent,toolResultToString,} from "../translate/anthropic-content.ts";+import { countToolSchemaTokens } from "../shared/tool-schema.ts";const IMAGE_TOKEN_ESTIMATE = 2000;@@ -30,11 +31,12 @@ export function countTokens(req: AnthropicRequest): number {}}- for (const tool of req.tools ?? []) {- total += encode(tool.name).length;- if (tool.description) total += encode(tool.description).length;- total += encode(JSON.stringify(tool.input_schema ?? {})).length;- }+ total += countToolSchemaTokens(+ req.tools,+ (tool) => tool.name,+ (tool) => tool.description,+ (tool) => tool.input_schema,+ );total += req.messages.length * 4;return total;@@ -65,11 +67,12 @@ export function countTranslatedTokens(}}- for (const tool of req.tools ?? []) {- total += encode(tool.name).length;- if (tool.description) total += encode(tool.description).length;- total += encode(JSON.stringify(tool.parameters ?? {})).length;- }+ total += countToolSchemaTokens(+ req.tools,+ (tool) => tool.name,+ (tool) => tool.description,+ (tool) => tool.parameters,+ );if (req.text?.format?.type === "json_schema") {total += encode(req.text.format.name).length;diff --git a/src/providers/codex/index.ts b/src/providers/codex/index.tsindex d49c5cb..d84975e 100644--- a/src/providers/codex/index.ts+++ b/src/providers/codex/index.ts@@ -1,12 +1,7 @@import type { AnthropicRequest } from "../../anthropic/schema.ts";import { wantsDownstreamStream } from "../../anthropic/stream.ts";import type { Provider, RequestContext } from "../types.ts";-import {- anthropicErrorBody,- jsonError,- jsonResponse,- sseResponse,-} from "../../anthropic/response.ts";+import { jsonError, jsonResponse, sseResponse } from "../../anthropic/response.ts";import {ALLOWED_MODELS,assertAllowedModel,@@ -24,6 +19,7 @@ import { summarizeCodexRequestSize, type CodexRequestSizeSummary } from "./requeimport { codexPreviousResponseId, logVerbose } from "../../config.ts";import { clearContinuation, continuationCandidate, recordContinuation } from "./continuation.ts";import { codexCli } from "./cli.ts";+import { mapUpstreamHttpErrorToResponse, mapUpstreamStreamErrorToResponse } from "../shared/upstream-errors.ts";interface SessionCountSnapshot {reqId: string;@@ -298,19 +294,7 @@ async function handleMessages(body: AnthropicRequest, ctx: RequestContext): PromclearContinuation(ctx.sessionId);if (err instanceof CodexError) {log.warn("codex error", { status: err.status, detail: err.detail });- if (err.status === 429) {- const headers: Record<string, string> = {};- if (err.meta?.retryAfter) headers["retry-after"] = err.meta.retryAfter;- 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";- return jsonError(err.status, type, err.detail || err.message);+ return mapUpstreamHttpErrorToResponse(err);}throw err;}@@ -412,15 +396,7 @@ async function handleMessages(body: AnthropicRequest, ctx: RequestContext): Promkind: err.kind,message: err.message,});- if (err.kind === "rate_limit") {- const headers: Record<string, string> = {};- if (err.retryAfterSeconds) headers["retry-after"] = String(err.retryAfterSeconds);- return jsonResponse(- anthropicErrorBody("rate_limit_error", err.message),- { status: 429, headers },- );- }- return jsonError(502, "api_error", err.message);+ return mapUpstreamStreamErrorToResponse(err);}throw err;}diff --git a/src/providers/cursor/count-tokens.ts b/src/providers/cursor/count-tokens.tsindex 244b404..a265ba8 100644--- a/src/providers/cursor/count-tokens.ts+++ b/src/providers/cursor/count-tokens.ts@@ -1,13 +1,15 @@import { encode } from "gpt-tokenizer/model/gpt-4o";import type { AnthropicRequest } from "../../anthropic/schema.ts";import { renderCursorPrompt } from "./translate/request.ts";+import { countToolSchemaTokens } from "../shared/tool-schema.ts";export function countCursorTokens(req: AnthropicRequest): number {let total = encode(renderCursorPrompt(req)).length;- for (const tool of req.tools ?? []) {- total += encode(tool.name).length;- if (tool.description) total += encode(tool.description).length;- total += encode(JSON.stringify(tool.input_schema ?? {})).length;- }+ total += countToolSchemaTokens(+ req.tools,+ (tool) => tool.name,+ (tool) => tool.description,+ (tool) => tool.input_schema,+ );return total;}diff --git a/src/providers/kimi/cli.ts b/src/providers/kimi/cli.tsindex 7583845..6cc17ba 100644--- a/src/providers/kimi/cli.ts+++ b/src/providers/kimi/cli.ts@@ -2,6 +2,7 @@ import type { CliHandlers } from "../types.ts";import { runDeviceLogin } from "./auth/login.ts";import { persistInitialTokens } from "./auth/manager.ts";import { authPath, clearAuth, loadAuth } from "./auth/token-store.ts";+import { printAuthStatus } from "../shared/cli-status.ts";export const kimiCli: CliHandlers = {async login() {@@ -13,16 +14,12 @@ export const kimiCli: CliHandlers = {console.log(`Expires in ${secs}s`);},async status() {- const auth = await loadAuth();- if (!auth) {- console.log("Not authenticated");- process.exit(1);- }- const ms = auth.expires - Date.now();- console.log(`User: ${auth.userId ?? "(none)"}`);- console.log(`Expires: ${new Date(auth.expires).toISOString()} (in ${Math.floor(ms / 1000)}s)`);- console.log(`Scope: ${auth.scope ?? "(none)"}`);- console.log(`Storage: ${authPath()}`);+ await printAuthStatus({+ loadAuth,+ authPath,+ formatIdentityLine: (auth) => `User: ${auth.userId ?? "(none)"}`,+ extraLines: (auth) => [`Scope: ${auth.scope ?? "(none)"}`],+ });},async logout() {await clearAuth();diff --git a/src/providers/kimi/count-tokens.ts b/src/providers/kimi/count-tokens.tsindex 809077d..96e4f75 100644--- a/src/providers/kimi/count-tokens.ts+++ b/src/providers/kimi/count-tokens.ts@@ -6,6 +6,7 @@ import {normalizeContent,toolResultToString,} from "../translate/anthropic-content.ts";+import { countToolSchemaTokens } from "../shared/tool-schema.ts";const IMAGE_TOKEN_ESTIMATE = 2000;@@ -34,11 +35,12 @@ export function countTokens(req: AnthropicRequest): number {}}- for (const tool of req.tools ?? []) {- total += encode(tool.name).length;- if (tool.description) total += encode(tool.description).length;- total += encode(JSON.stringify(tool.input_schema ?? {})).length;- }+ total += countToolSchemaTokens(+ req.tools,+ (tool) => tool.name,+ (tool) => tool.description,+ (tool) => tool.input_schema,+ );total += req.messages.length * 4;return total;@@ -75,11 +77,12 @@ export function countTranslatedTokens(req: KimiChatRequest): number {}}- for (const tool of req.tools ?? []) {- total += encode(tool.function.name).length;- if (tool.function.description) total += encode(tool.function.description).length;- total += encode(JSON.stringify(tool.function.parameters ?? {})).length;- }+ total += countToolSchemaTokens(+ req.tools,+ (tool) => tool.function.name,+ (tool) => tool.function.description,+ (tool) => tool.function.parameters,+ );total += req.messages.length * 4;return total;diff --git a/src/providers/kimi/index.ts b/src/providers/kimi/index.tsindex d09c5b3..1d3fb72 100644--- a/src/providers/kimi/index.ts+++ b/src/providers/kimi/index.ts@@ -1,12 +1,7 @@import type { Provider, 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 { jsonError, jsonResponse, sseResponse } from "../../anthropic/response.ts";import {assertAllowedModel,ModelNotAllowedError,@@ -20,6 +15,7 @@ import { countTokens, countTranslatedTokens } from "./count-tokens.ts";import { KimiError, postKimi } from "./client.ts";import { logVerbose } from "../../config.ts";import { kimiCli } from "./cli.ts";+import { mapUpstreamHttpErrorToResponse, mapUpstreamStreamErrorToResponse } from "../shared/upstream-errors.ts";async function handleCountTokens(body: AnthropicRequest, ctx: RequestContext): Promise<Response> {const log = ctx.childLogger("provider.kimi");@@ -86,19 +82,7 @@ async function handleMessages(body: AnthropicRequest, ctx: RequestContext): Prom} catch (err) {if (err instanceof KimiError) {log.warn("kimi error", { status: err.status, detail: err.detail });- if (err.status === 429) {- const headers: Record<string, string> = {};- if (err.meta?.retryAfter) headers["retry-after"] = err.meta.retryAfter;- 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";- return jsonError(err.status, type, err.detail || err.message);+ return mapUpstreamHttpErrorToResponse(err);}throw err;}@@ -142,15 +126,7 @@ async function handleMessages(body: AnthropicRequest, ctx: RequestContext): Promkind: err.kind,message: err.message,});- if (err.kind === "rate_limit") {- const headers: Record<string, string> = {};- if (err.retryAfterSeconds) headers["retry-after"] = String(err.retryAfterSeconds);- return jsonResponse(- anthropicErrorBody("rate_limit_error", err.message),- { status: 429, headers },- );- }- return jsonError(502, "api_error", err.message);+ return mapUpstreamStreamErrorToResponse(err);}throw err;}diff --git a/src/providers/shared/cli-status.ts b/src/providers/shared/cli-status.tsnew file mode 100644index 0000000..9f988a8--- /dev/null+++ b/src/providers/shared/cli-status.ts@@ -0,0 +1,27 @@+export interface AuthStatus {+ expires: number;+}++export interface AuthStatusPrinter<TAuth extends AuthStatus> {+ loadAuth: () => Promise<TAuth | undefined>;+ authPath: () => string;+ formatIdentityLine: (auth: TAuth) => string;+ extraLines?: (auth: TAuth) => string[];+}++export async function printAuthStatus<TAuth extends AuthStatus>(+ options: AuthStatusPrinter<TAuth>,+): Promise<void> {+ const auth = await options.loadAuth();+ if (!auth) {+ console.log("Not authenticated");+ process.exit(1);+ }+ const ms = auth.expires - Date.now();+ console.log(options.formatIdentityLine(auth));+ console.log(`Expires: ${new Date(auth.expires).toISOString()} (in ${Math.floor(ms / 1000)}s)`);+ for (const line of options.extraLines?.(auth) ?? []) {+ console.log(line);+ }+ console.log(`Storage: ${options.authPath()}`);+}diff --git a/src/providers/shared/tool-schema.ts b/src/providers/shared/tool-schema.tsnew file mode 100644index 0000000..10be422--- /dev/null+++ b/src/providers/shared/tool-schema.ts@@ -0,0 +1,17 @@+import { encode } from "gpt-tokenizer/model/gpt-4o";++export function countToolSchemaTokens<T>(+ tools: Array<T> | undefined,+ readName: (tool: T) => string,+ readDescription: (tool: T) => string | undefined,+ readSchema: (tool: T) => unknown,+): number {+ let total = 0;+ for (const tool of tools ?? []) {+ total += encode(readName(tool)).length;+ const description = readDescription(tool);+ if (description) total += encode(description).length;+ total += encode(JSON.stringify(readSchema(tool) ?? {})).length;+ }+ return total;+}diff --git a/src/providers/shared/upstream-errors.ts b/src/providers/shared/upstream-errors.tsnew file mode 100644index 0000000..d67b444--- /dev/null+++ b/src/providers/shared/upstream-errors.ts@@ -0,0 +1,42 @@+import { anthropicErrorBody, jsonError, jsonResponse } from "../../anthropic/response.ts";++export interface UpstreamHttpError {+ status: number;+ detail?: string;+ message: string;+ meta?: {+ retryAfter?: string;+ };+}++export interface UpstreamStreamErrorLike {+ kind: string;+ retryAfterSeconds?: number;+ message: string;+}++export function mapUpstreamHttpErrorToResponse(err: UpstreamHttpError): Response {+ if (err.status === 429) {+ const headers: Record<string, string> = {};+ if (err.meta?.retryAfter) headers["retry-after"] = err.meta.retryAfter;+ 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";+ return jsonError(err.status, type, err.detail || err.message);+}++export function mapUpstreamStreamErrorToResponse(err: UpstreamStreamErrorLike): Response {+ if (err.kind === "rate_limit") {+ const headers: Record<string, string> = {};+ if (err.retryAfterSeconds) headers["retry-after"] = String(err.retryAfterSeconds);+ return jsonResponse(anthropicErrorBody("rate_limit_error", err.message), {+ status: 429,+ headers,+ });+ }+ return jsonError(502, "api_error", err.message);+}