Skip to content

Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy

Rust

extract shared request translation helpers

Raine Virtad92631ed21d1
diff --git a/src/providers/codex/count-tokens.ts b/src/providers/codex/count-tokens.ts
index 374d38b..13262e2 100644
--- a/src/providers/codex/count-tokens.ts
+++ b/src/providers/codex/count-tokens.ts
@@ -1,13 +1,17 @@
import { encode } from "gpt-tokenizer/model/gpt-4o";
import type { AnthropicRequest } from "../../anthropic/schema.ts";
import type { ResponsesRequest } from "./translate/request.ts";
-import { buildInstructions, normalizeContent, toolResultToString } from "./translate/request.ts";
+import {
+ flattenSystemText,
+ normalizeContent,
+ toolResultToString,
+} from "../translate/anthropic-content.ts";
const IMAGE_TOKEN_ESTIMATE = 2000;
export function countTokens(req: AnthropicRequest): number {
let total = 0;
- const instructions = buildInstructions(req.system);
+ const instructions = flattenSystemText(req.system);
if (instructions) total += encode(instructions).length;
for (const msg of req.messages) {
diff --git a/src/providers/codex/translate/request.ts b/src/providers/codex/translate/request.ts
index a741133..410ab1f 100644
--- a/src/providers/codex/translate/request.ts
+++ b/src/providers/codex/translate/request.ts
@@ -1,13 +1,17 @@
import type {
AnthropicContentBlock,
- AnthropicImageBlock,
AnthropicMessage,
AnthropicRequest,
- AnthropicTextBlock,
AnthropicTool,
- AnthropicToolResultContentBlock,
} from "../../../anthropic/schema.ts";
import { codexEffort, codexServiceTier } from "../../../config.ts";
+import {
+ assertValidEffort,
+ flattenSystemText,
+ imageBlockToUrl,
+ normalizeContent,
+ toolResultToString,
+} from "../../translate/anthropic-content.ts";
export type Effort = "none" | "low" | "medium" | "high" | "xhigh";
export type ServiceTier = "priority" | "flex";
@@ -101,18 +105,8 @@ export interface TranslateOptions {
const VALID_EFFORTS = new Set<Effort>(["none", "low", "medium", "high", "xhigh"]);
-const ANTHROPIC_EFFORTS = new Set(["low", "medium", "high", "max"]);
-
const VALID_SERVICE_TIERS = new Set(["fast", "priority", "flex"]);
-function assertValidEffort(effort: unknown): void {
- if (effort !== undefined && !ANTHROPIC_EFFORTS.has(effort as string)) {
- throw new Error(
- `Invalid output_config.effort: ${JSON.stringify(effort)}. Must be one of: ${Array.from(ANTHROPIC_EFFORTS).join(", ")}`,
- );
- }
-}
-
function toCodexEffort(
effort: NonNullable<AnthropicRequest["output_config"]>["effort"],
): Effort | undefined {
@@ -218,17 +212,9 @@ function mapToolChoice(choice: AnthropicRequest["tool_choice"]): ResponsesReques
}
}
-export function buildInstructions(system: AnthropicRequest["system"]): string | undefined {
- if (!system) return undefined;
- const blocks: AnthropicTextBlock[] =
- typeof system === "string" ? [{ type: "text", text: system }] : system;
- const texts = blocks
- .filter((b) => b && b.type === "text" && typeof b.text === "string")
- .map((b) => b.text)
- .filter((t) => !t.startsWith("x-anthropic-billing-header:"));
- if (!texts.length) return undefined;
- return texts.join("\n\n");
-}
+export const buildInstructions = flattenSystemText;
+
+export { normalizeContent, toolResultToString };
function buildInput(messages: AnthropicMessage[]): ResponsesInputItem[] {
const out: ResponsesInputItem[] = [];
@@ -241,7 +227,7 @@ function buildInput(messages: AnthropicMessage[]): ResponsesInputItem[] {
if (block.type === "text") {
parts.push({ type: "input_text", text: block.text });
} else if (block.type === "image") {
- parts.push({ type: "input_image", image_url: imageToUrl(block) });
+ parts.push({ type: "input_image", image_url: imageBlockToUrl(block) });
} else if (block.type === "tool_result") {
if (parts.length) {
out.push({ type: "message", role: "user", content: parts.splice(0) });
@@ -257,7 +243,7 @@ function buildInput(messages: AnthropicMessage[]): ResponsesInputItem[] {
if (parts.length) out.push({ type: "message", role: "user", content: parts });
} else if (msg.role === "system") {
const parts = blocks
- .filter((block): block is AnthropicTextBlock => block.type === "text")
+ .filter((block): block is AnthropicContentBlock & { type: "text" } => block.type === "text")
.map((block) => ({ type: "input_text" as const, text: block.text }));
if (parts.length) out.push({ type: "message", role: "developer", content: parts });
} else {
@@ -287,59 +273,10 @@ function buildInput(messages: AnthropicMessage[]): ResponsesInputItem[] {
return out;
}
-export function normalizeContent(content: AnthropicMessage["content"]): AnthropicContentBlock[] {
- if (typeof content === "string") return [{ type: "text", text: content }];
- return content;
-}
-
-function imageToUrl(block: Extract<AnthropicContentBlock, { type: "image" }>): string {
- if (block.source.type === "url") return block.source.url;
- return `data:${block.source.media_type};base64,${block.source.data}`;
-}
-
-function unsupportedToolResultBlockToString(block: AnthropicToolResultContentBlock): string {
- const type = typeof block.type === "string" ? block.type : "unknown";
- return `[unsupported content block omitted: ${type}]`;
-}
-
-function isToolResultTextBlock(
- block: AnthropicToolResultContentBlock,
-): block is AnthropicTextBlock {
- return block.type === "text" && typeof block.text === "string";
-}
-
function isRecord(value: unknown): value is Record<string, unknown> {
return !!value && typeof value === "object";
}
-function isToolResultImageBlock(
- block: AnthropicToolResultContentBlock,
-): block is AnthropicImageBlock {
- if (block.type !== "image") return false;
- const source = block.source;
- if (!isRecord(source)) return false;
- if (source.type === "url") return typeof source.url === "string";
- return (
- source.type === "base64" &&
- typeof source.media_type === "string" &&
- typeof source.data === "string"
- );
-}
-
-export function toolResultToString(content: string | AnthropicToolResultContentBlock[]): string {
- if (typeof content === "string") return content;
- return content
- .map((b) => {
- if (isToolResultTextBlock(b)) return b.text;
- if (isToolResultImageBlock(b)) {
- const mt = b.source.type === "base64" ? b.source.media_type : "url";
- return `[image omitted: ${mt}]`;
- }
- return unsupportedToolResultBlockToString(b);
- })
- .join("\n");
-}
-
function toResponsesTool(tool: AnthropicTool): ResponsesTool {
return {
type: "function",
diff --git a/src/providers/kimi/count-tokens.ts b/src/providers/kimi/count-tokens.ts
index 3a4d919..809077d 100644
--- a/src/providers/kimi/count-tokens.ts
+++ b/src/providers/kimi/count-tokens.ts
@@ -1,15 +1,19 @@
import { encode } from "gpt-tokenizer/model/gpt-4o";
import type { AnthropicRequest } from "../../anthropic/schema.ts";
import type { KimiChatRequest } from "./translate/request.ts";
-import { buildSystemMessage, normalizeContent, toolResultToString } from "./translate/request.ts";
+import {
+ flattenSystemText,
+ normalizeContent,
+ toolResultToString,
+} from "../translate/anthropic-content.ts";
const IMAGE_TOKEN_ESTIMATE = 2000;
-// Approximate — Kimi's tokenizer isn't gpt-tokenizer, but Claude Code's
+// Approximate: Kimi's tokenizer isn't gpt-tokenizer, but Claude Code's
// compaction logic only needs a monotonic estimate, not an exact count.
export function countTokens(req: AnthropicRequest): number {
let total = 0;
- const system = buildSystemMessage(req.system);
+ const system = flattenSystemText(req.system);
if (system) total += encode(system).length;
for (const msg of req.messages) {
diff --git a/src/providers/kimi/translate/request.ts b/src/providers/kimi/translate/request.ts
index 9bb2821..fc5195c 100644
--- a/src/providers/kimi/translate/request.ts
+++ b/src/providers/kimi/translate/request.ts
@@ -1,12 +1,19 @@
import type {
AnthropicContentBlock,
- AnthropicImageBlock,
- AnthropicMessage,
AnthropicRequest,
- AnthropicTextBlock,
AnthropicTool,
AnthropicToolResultContentBlock,
} from "../../../anthropic/schema.ts";
+import {
+ assertValidEffort,
+ flattenSystemText,
+ imageBlockToUrl,
+ isToolResultImageBlock,
+ isToolResultTextBlock,
+ normalizeContent,
+ toolResultToString,
+ unsupportedToolResultBlockToString,
+} from "../../translate/anthropic-content.ts";
// OpenAI-compatible chat-completions request shape used by Kimi.
// Only the fields kimi-cli is known to send are included; unknown
@@ -71,8 +78,8 @@ const DEFAULT_MAX_TOKENS = 32000;
// Kimi's `kimi-for-coding` is a reasoning model: it always produces
// reasoning_content and its server always enforces that prior assistant
// tool_call messages carry reasoning_content back. The `thinking` /
-// `reasoning_effort` flags on the request don't gate that contract —
-// they're a model-level property. So we always enable thinking on the
+// `reasoning_effort` flags on the request don't gate that contract.
+// They're a model-level property. So we always enable thinking on the
// outbound Kimi request and always forward/replay reasoning end-to-end.
export function translateRequest(
req: AnthropicRequest,
@@ -103,16 +110,6 @@ function clampMaxTokens(requested: number | undefined): number {
return Math.min(requested, DEFAULT_MAX_TOKENS);
}
-const ANTHROPIC_EFFORTS = new Set(["low", "medium", "high", "max"]);
-
-function assertValidEffort(effort: unknown): void {
- if (effort !== undefined && !ANTHROPIC_EFFORTS.has(effort as string)) {
- throw new Error(
- `Invalid output_config.effort: ${JSON.stringify(effort)}. Must be one of: ${Array.from(ANTHROPIC_EFFORTS).join(", ")}`,
- );
- }
-}
-
// Kimi's reasoning_effort is capped at "high"; collapse the proxy's "max"
// to "high" since Kimi has no stronger tier, and default to "medium" when no
// effort is requested.
@@ -137,17 +134,9 @@ function mapToolChoice(choice: AnthropicRequest["tool_choice"]): KimiToolChoice
}
}
-export function buildSystemMessage(system: AnthropicRequest["system"]): string | undefined {
- if (!system) return undefined;
- const blocks: AnthropicTextBlock[] =
- typeof system === "string" ? [{ type: "text", text: system }] : system;
- const texts = blocks
- .filter((b) => b && b.type === "text" && typeof b.text === "string")
- .map((b) => b.text)
- .filter((t) => !t.startsWith("x-anthropic-billing-header:"));
- if (!texts.length) return undefined;
- return texts.join("\n\n");
-}
+export const buildSystemMessage = flattenSystemText;
+
+export { normalizeContent, toolResultToString };
function buildMessages(req: AnthropicRequest): KimiMessage[] {
const out: KimiMessage[] = [];
@@ -173,7 +162,7 @@ function pushUserMessages(out: KimiMessage[], blocks: AnthropicContentBlock[]):
let buffer: KimiUserContentPart[] = [];
const flushBuffer = () => {
if (!buffer.length) return;
- // Collapse to a string when every part is text — matches what kimi-cli
+ // Collapse to a string when every part is text. This matches what kimi-cli
// sends and keeps the wire payload compact.
const allText = buffer.every((p) => p.type === "text");
if (allText) {
@@ -191,7 +180,7 @@ function pushUserMessages(out: KimiMessage[], blocks: AnthropicContentBlock[]):
if (block.type === "text") {
buffer.push({ type: "text", text: block.text });
} else if (block.type === "image") {
- buffer.push({ type: "image_url", image_url: { url: imageToUrl(block) } });
+ buffer.push({ type: "image_url", image_url: { url: imageBlockToUrl(block) } });
} else if (block.type === "tool_result") {
flushBuffer();
out.push({
@@ -223,7 +212,7 @@ function pushAssistantMessage(out: KimiMessage[], blocks: AnthropicContentBlock[
},
});
}
- // image blocks from the assistant are dropped — Kimi's assistant
+ // image blocks from the assistant are dropped because Kimi's assistant
// schema does not express them.
}
@@ -232,51 +221,13 @@ function pushAssistantMessage(out: KimiMessage[], blocks: AnthropicContentBlock[
const msg: KimiAssistantMessage = { role: "assistant" };
msg.content = textParts.length ? textParts.join("") : "";
// Kimi accepts one reasoning_content string per assistant turn. If a turn
- // has multiple interleaved thinking blocks we concatenate in order —
- // exact block/tool_use pairing can't be preserved over the wire.
+ // has multiple interleaved thinking blocks we concatenate in order.
+ // Exact block/tool_use pairing can't be preserved over the wire.
if (thinkingParts.length) msg.reasoning_content = thinkingParts.join("\n\n");
if (toolCalls.length) msg.tool_calls = toolCalls;
out.push(msg);
}
-export function normalizeContent(content: AnthropicMessage["content"]): AnthropicContentBlock[] {
- if (typeof content === "string") return [{ type: "text", text: content }];
- return content;
-}
-
-function imageToUrl(block: Extract<AnthropicContentBlock, { type: "image" }>): string {
- if (block.source.type === "url") return block.source.url;
- return `data:${block.source.media_type};base64,${block.source.data}`;
-}
-
-function unsupportedToolResultBlockToString(block: AnthropicToolResultContentBlock): string {
- const type = typeof block.type === "string" ? block.type : "unknown";
- return `[unsupported content block omitted: ${type}]`;
-}
-
-function isToolResultTextBlock(
- block: AnthropicToolResultContentBlock,
-): block is AnthropicTextBlock {
- return block.type === "text" && typeof block.text === "string";
-}
-
-function isRecord(value: unknown): value is Record<string, unknown> {
- return !!value && typeof value === "object";
-}
-
-function isToolResultImageBlock(
- block: AnthropicToolResultContentBlock,
-): block is AnthropicImageBlock {
- if (block.type !== "image") return false;
- const source = block.source;
- if (!isRecord(source)) return false;
- if (source.type === "url") return typeof source.url === "string";
- return (
- source.type === "base64" &&
- typeof source.media_type === "string" &&
- typeof source.data === "string"
- );
-}
export function toolResultContent(
content: string | AnthropicToolResultContentBlock[],
@@ -291,7 +242,7 @@ export function toolResultContent(
if (isToolResultTextBlock(b)) {
parts.push({ type: "text", text: b.text });
} else if (isToolResultImageBlock(b)) {
- parts.push({ type: "image_url", image_url: { url: imageToUrl(b) } });
+ parts.push({ type: "image_url", image_url: { url: imageBlockToUrl(b) } });
} else {
parts.push({ type: "text", text: unsupportedToolResultBlockToString(b) });
}
@@ -300,21 +251,6 @@ export function toolResultContent(
return parts;
}
-export function toolResultToString(content: string | AnthropicToolResultContentBlock[]): string {
- // Kept for the token counter, which wants a flat string.
- if (typeof content === "string") return content;
- return content
- .map((b) => {
- if (isToolResultTextBlock(b)) return b.text;
- if (isToolResultImageBlock(b)) {
- const mt = b.source.type === "base64" ? b.source.media_type : "url";
- return `[image omitted: ${mt}]`;
- }
- return unsupportedToolResultBlockToString(b);
- })
- .join("\n");
-}
-
function toKimiTool(tool: AnthropicTool): KimiTool {
return {
type: "function",
diff --git a/src/providers/translate/anthropic-content.ts b/src/providers/translate/anthropic-content.ts
new file mode 100644
index 0000000..95c84e6
--- /dev/null
+++ b/src/providers/translate/anthropic-content.ts
@@ -0,0 +1,85 @@
+import type {
+ AnthropicContentBlock,
+ AnthropicImageBlock,
+ AnthropicMessage,
+ AnthropicRequest,
+ AnthropicTextBlock,
+ AnthropicToolResultContentBlock,
+} from "../../anthropic/schema.ts";
+
+const ANTHROPIC_EFFORTS = new Set(["low", "medium", "high", "max"]);
+
+export function assertValidEffort(effort: unknown): void {
+ if (effort !== undefined && !ANTHROPIC_EFFORTS.has(effort as string)) {
+ throw new Error(
+ `Invalid output_config.effort: ${JSON.stringify(effort)}. Must be one of: ${Array.from(ANTHROPIC_EFFORTS).join(", ")}`,
+ );
+ }
+}
+
+export function flattenSystemText(system: AnthropicRequest["system"]): string | undefined {
+ if (!system) return undefined;
+ const blocks: AnthropicTextBlock[] =
+ typeof system === "string" ? [{ type: "text", text: system }] : system;
+ const texts = blocks
+ .filter((b) => b && b.type === "text" && typeof b.text === "string")
+ .map((b) => b.text)
+ .filter((t) => !t.startsWith("x-anthropic-billing-header:"));
+ if (!texts.length) return undefined;
+ return texts.join("\n\n");
+}
+
+export function normalizeContent(content: AnthropicMessage["content"]): AnthropicContentBlock[] {
+ if (typeof content === "string") return [{ type: "text", text: content }];
+ return content;
+}
+
+export function imageBlockToUrl(block: Extract<AnthropicContentBlock, { type: "image" }>): string {
+ if (block.source.type === "url") return block.source.url;
+ return `data:${block.source.media_type};base64,${block.source.data}`;
+}
+
+export function unsupportedToolResultBlockToString(
+ block: AnthropicToolResultContentBlock,
+): string {
+ const type = typeof block.type === "string" ? block.type : "unknown";
+ return `[unsupported content block omitted: ${type}]`;
+}
+
+export function isToolResultTextBlock(
+ block: AnthropicToolResultContentBlock,
+): block is AnthropicTextBlock {
+ return block.type === "text" && typeof block.text === "string";
+}
+
+function isRecord(value: unknown): value is Record<string, unknown> {
+ return !!value && typeof value === "object";
+}
+
+export function isToolResultImageBlock(
+ block: AnthropicToolResultContentBlock,
+): block is AnthropicImageBlock {
+ if (block.type !== "image") return false;
+ const source = block.source;
+ if (!isRecord(source)) return false;
+ if (source.type === "url") return typeof source.url === "string";
+ return (
+ source.type === "base64" &&
+ typeof source.media_type === "string" &&
+ typeof source.data === "string"
+ );
+}
+
+export function toolResultToString(content: string | AnthropicToolResultContentBlock[]): string {
+ if (typeof content === "string") return content;
+ return content
+ .map((b) => {
+ if (isToolResultTextBlock(b)) return b.text;
+ if (isToolResultImageBlock(b)) {
+ const mt = b.source.type === "base64" ? b.source.media_type : "url";
+ return `[image omitted: ${mt}]`;
+ }
+ return unsupportedToolResultBlockToString(b);
+ })
+ .join("\n");
+}