Skip to content

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

Rust

share provider accumulate helpers

Raine Virta189a76f3f82b
diff --git a/src/providers/codex/translate/accumulate.ts b/src/providers/codex/translate/accumulate.ts
index bad7635..e5cc24d 100644
--- a/src/providers/codex/translate/accumulate.ts
+++ b/src/providers/codex/translate/accumulate.ts
@@ -3,7 +3,12 @@ import type { CodexUsage, ReducerEvent } from "./reducer.ts";
import type { Logger } from "../../../log.ts";
import type { TrafficCapture } from "../../types.ts";
import { attachTrafficCapture, createUpstreamStreamDiagnostics } from "./reducer.ts";
-import { createBlockAccumulator, parseToolInputJsonOrRaw } from "../../translate/accumulate.ts";
+import {
+ collectAnthropicContentFromAccumulatedBlocks,
+ createBlockAccumulator,
+ defaultAnthropicNonStreamUsage,
+ parseToolInputJsonOrRaw,
+} from "../../translate/accumulate.ts";
export { UpstreamStreamError } from "./reducer.ts";
@@ -88,19 +93,18 @@ export async function accumulateResponse(
}
}
- const content: AnthropicNonStreamResponse["content"] = [];
- for (const b of blockAccumulator.orderedBlocks()) {
- if (b.kind === "text") {
- if (b.text) content.push({ type: "text", text: b.text });
- } else if (b.kind === "tool") {
- content.push({
+ const content = collectAnthropicContentFromAccumulatedBlocks<AnthropicNonStreamResponse["content"][number]>(
+ blockAccumulator.orderedBlocks(),
+ {
+ onText: (_index, text) => (text ? { type: "text", text } : undefined),
+ onTool: (_index, id, name, args) => ({
type: "tool_use",
- id: b.id,
- name: b.name,
- input: parseToolInputJsonOrRaw(b.args),
- });
- }
- }
+ id,
+ name,
+ input: parseToolInputJsonOrRaw(args),
+ }),
+ },
+ );
return {
rawUsage,
@@ -116,12 +120,7 @@ export async function accumulateResponse(
content,
stop_reason: stopReason,
stop_sequence: null,
- usage: usage ?? {
- input_tokens: 0,
- output_tokens: 0,
- cache_creation_input_tokens: 0,
- cache_read_input_tokens: 0,
- },
+ usage: usage ?? defaultAnthropicNonStreamUsage(),
},
};
}
diff --git a/src/providers/codex/translate/reducer.ts b/src/providers/codex/translate/reducer.ts
index afa722a..6e8f45a 100644
--- a/src/providers/codex/translate/reducer.ts
+++ b/src/providers/codex/translate/reducer.ts
@@ -2,6 +2,8 @@ import { createSseStreamStats, parseSseStream, type SseStreamStats } from "../..
import type { Logger } from "../../../log.ts";
import { logVerbose } from "../../../config.ts";
import type { TrafficCapture } from "../../types.ts";
+import type { TextToolReducerEvent } from "../../translate/accumulate.ts";
+import { mapCachedInputUsageToAnthropicUsage } from "../../translate/accumulate.ts";
import type { ResponsesInputItem } from "./request.ts";
export class UpstreamStreamError extends Error {
@@ -26,14 +28,9 @@ export type StopReason = "end_turn" | "tool_use" | "max_tokens";
export type TerminalType = "response.completed" | "response.incomplete" | "response.done";
export type ReducerEvent =
- | { kind: "text-start"; index: number }
- | { kind: "text-delta"; index: number; text: string }
- | { kind: "text-stop"; index: number }
- | { kind: "tool-start"; index: number; id: string; name: string }
- | { kind: "tool-delta"; index: number; partialJson: string }
+ | TextToolReducerEvent
| { kind: "tool-progress"; index: number }
| { kind: "progress" }
- | { kind: "tool-stop"; index: number }
| {
kind: "finish";
stopReason: StopReason;
@@ -517,17 +514,14 @@ export function mapUsageToAnthropic(u: CodexUsage | undefined): {
cache_creation_input_tokens: number;
cache_read_input_tokens: number;
} {
- const cachedTokens = u?.input_tokens_details?.cached_tokens ?? 0;
- const totalInputTokens = u?.input_tokens ?? 0;
- return {
- // OpenAI-style usage reports cached prompt tokens inside input_tokens.
- // Anthropic-style usage reports cache reads separately, and Claude Code
- // sums input_tokens + cache_read_input_tokens when deciding context size.
- // Subtract cached reads here so the downstream total matches the real
- // prompt window instead of double-counting cached context.
- input_tokens: Math.max(0, totalInputTokens - cachedTokens),
- output_tokens: u?.output_tokens ?? 0,
- cache_creation_input_tokens: 0,
- cache_read_input_tokens: cachedTokens,
- };
+ // OpenAI-style usage reports cached prompt tokens inside input_tokens.
+ // Anthropic-style usage reports cache reads separately, and Claude Code
+ // sums input_tokens + cache_read_input_tokens when deciding context size.
+ // Subtract cached reads here so the downstream total matches the real
+ // prompt window instead of double-counting cached context.
+ return mapCachedInputUsageToAnthropicUsage({
+ inputTokens: u?.input_tokens,
+ outputTokens: u?.output_tokens,
+ cachedInputTokens: u?.input_tokens_details?.cached_tokens,
+ });
}
diff --git a/src/providers/kimi/translate/accumulate.ts b/src/providers/kimi/translate/accumulate.ts
index d064ed2..381ff49 100644
--- a/src/providers/kimi/translate/accumulate.ts
+++ b/src/providers/kimi/translate/accumulate.ts
@@ -1,7 +1,12 @@
import { mapUsageToAnthropic, reduceUpstream, type KimiUsage } from "./reducer.ts";
import { makeThinkingSignature } from "./signature.ts";
import type { TrafficCapture } from "../../types.ts";
-import { createBlockAccumulator, parseToolInputJsonOrRaw } from "../../translate/accumulate.ts";
+import {
+ collectAnthropicContentFromAccumulatedBlocks,
+ createBlockAccumulator,
+ defaultAnthropicNonStreamUsage,
+ parseToolInputJsonOrRaw,
+} from "../../translate/accumulate.ts";
export { UpstreamStreamError } from "./reducer.ts";
@@ -85,26 +90,26 @@ export async function accumulateResponse(
}
}
- const content: AnthropicNonStreamResponse["content"] = [];
- for (const b of blockAccumulator.orderedBlocks()) {
- if (b.kind === "thinking") {
- if (b.text)
- content.push({
- type: "thinking",
- thinking: b.text,
- signature: makeThinkingSignature(opts.messageId, b.index),
- });
- } else if (b.kind === "text") {
- if (b.text) content.push({ type: "text", text: b.text });
- } else {
- content.push({
+ const content = collectAnthropicContentFromAccumulatedBlocks<AnthropicNonStreamResponse["content"][number]>(
+ blockAccumulator.orderedBlocks(),
+ {
+ onThinking: (index, text) =>
+ text
+ ? {
+ type: "thinking",
+ thinking: text,
+ signature: makeThinkingSignature(opts.messageId, index),
+ }
+ : undefined,
+ onText: (_index, text) => (text ? { type: "text", text } : undefined),
+ onTool: (_index, id, name, args) => ({
type: "tool_use",
- id: b.id,
- name: b.name,
- input: parseToolInputJsonOrRaw(b.args),
- });
- }
- }
+ id,
+ name,
+ input: parseToolInputJsonOrRaw(args),
+ }),
+ },
+ );
opts.log.debug("accumulate summary", {
chunkCount: stats.chunkCount,
@@ -125,12 +130,7 @@ export async function accumulateResponse(
content,
stop_reason: stopReason,
stop_sequence: null,
- usage: usage ?? {
- input_tokens: 0,
- output_tokens: 0,
- cache_creation_input_tokens: 0,
- cache_read_input_tokens: 0,
- },
+ usage: usage ?? defaultAnthropicNonStreamUsage(),
},
};
}
diff --git a/src/providers/kimi/translate/reducer.ts b/src/providers/kimi/translate/reducer.ts
index dadfeef..07121df 100644
--- a/src/providers/kimi/translate/reducer.ts
+++ b/src/providers/kimi/translate/reducer.ts
@@ -1,6 +1,8 @@
import { parseSseStream } from "../../../sse.ts";
import type { Logger } from "../../../log.ts";
import type { TrafficCapture } from "../../types.ts";
+import type { TextToolReducerEvent } from "../../translate/accumulate.ts";
+import { mapCachedInputUsageToAnthropicUsage } from "../../translate/accumulate.ts";
export class UpstreamStreamError extends Error {
constructor(
@@ -28,12 +30,7 @@ export type ReducerEvent =
| { kind: "thinking-start"; index: number }
| { kind: "thinking-delta"; index: number; text: string }
| { kind: "thinking-stop"; index: number }
- | { kind: "text-start"; index: number }
- | { kind: "text-delta"; index: number; text: string }
- | { kind: "text-stop"; index: number }
- | { kind: "tool-start"; index: number; id: string; name: string }
- | { kind: "tool-delta"; index: number; partialJson: string }
- | { kind: "tool-stop"; index: number }
+ | TextToolReducerEvent
| { kind: "finish"; stopReason: StopReason; usage: KimiUsage | undefined };
interface StreamChunk {
@@ -208,10 +205,9 @@ export function mapUsageToAnthropic(u: KimiUsage | undefined): {
} {
const cached = u?.prompt_tokens_details?.cached_tokens ?? u?.cached_tokens ?? 0;
const totalPrompt = u?.prompt_tokens ?? 0;
- return {
- input_tokens: Math.max(0, totalPrompt - cached),
- output_tokens: u?.completion_tokens ?? 0,
- cache_creation_input_tokens: 0,
- cache_read_input_tokens: cached,
- };
+ return mapCachedInputUsageToAnthropicUsage({
+ inputTokens: totalPrompt,
+ outputTokens: u?.completion_tokens,
+ cachedInputTokens: cached,
+ });
}
diff --git a/src/providers/translate/accumulate.ts b/src/providers/translate/accumulate.ts
index f5ba34e..7db91d6 100644
--- a/src/providers/translate/accumulate.ts
+++ b/src/providers/translate/accumulate.ts
@@ -33,6 +33,76 @@ export interface BlockAccumulator {
orderedBlocks(): readonly AccumulatedBlock[];
}
+export interface AnthropicNonStreamUsage {
+ input_tokens: number;
+ output_tokens: number;
+ cache_creation_input_tokens: number;
+ cache_read_input_tokens: number;
+}
+
+export interface CachedInputUsage {
+ inputTokens?: number;
+ outputTokens?: number;
+ cachedInputTokens?: number;
+}
+
+export type TextToolReducerEvent =
+ | { kind: "text-start"; index: number }
+ | { kind: "text-delta"; index: number; text: string }
+ | { kind: "text-stop"; index: number }
+ | { kind: "tool-start"; index: number; id: string; name: string }
+ | { kind: "tool-delta"; index: number; partialJson: string }
+ | { kind: "tool-stop"; index: number };
+
+export function defaultAnthropicNonStreamUsage(): AnthropicNonStreamUsage {
+ return {
+ input_tokens: 0,
+ output_tokens: 0,
+ cache_creation_input_tokens: 0,
+ cache_read_input_tokens: 0,
+ };
+}
+
+export function mapCachedInputUsageToAnthropicUsage(
+ usage: CachedInputUsage = {},
+): AnthropicNonStreamUsage {
+ const inputTokens = usage.inputTokens ?? 0;
+ const outputTokens = usage.outputTokens ?? 0;
+ const cachedTokens = Math.max(0, usage.cachedInputTokens ?? 0);
+ return {
+ input_tokens: Math.max(0, inputTokens - cachedTokens),
+ output_tokens: outputTokens,
+ cache_creation_input_tokens: 0,
+ cache_read_input_tokens: cachedTokens,
+ };
+}
+
+export function collectAnthropicContentFromAccumulatedBlocks<TContent>(
+ blocks: readonly AccumulatedBlock[],
+ handlers: {
+ onThinking?: (index: number, text: string) => TContent | undefined;
+ onText: (index: number, text: string) => TContent | undefined;
+ onTool: (index: number, id: string, name: string, args: string) => TContent | undefined;
+ },
+): TContent[] {
+ const content: TContent[] = [];
+ for (const block of blocks) {
+ if (block.kind === "thinking") {
+ const item = handlers.onThinking?.(block.index, block.text);
+ if (item !== undefined) content.push(item);
+ continue;
+ }
+ if (block.kind === "text") {
+ const item = handlers.onText(block.index, block.text);
+ if (item !== undefined) content.push(item);
+ continue;
+ }
+ const item = handlers.onTool(block.index, block.id, block.name, block.args);
+ if (item !== undefined) content.push(item);
+ }
+ return content;
+}
+
export function createBlockAccumulator(options?: { includeThinking?: boolean }): BlockAccumulator {
const ordered: number[] = [];
const blocks = new Map<number, AccumulatedBlock>();