Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy
Rust
add compaction telemetry logging
Raine Virtae32d2f935a7e
diff --git a/src/count-tokens.ts b/src/count-tokens.tsindex e976d24..ef8df5f 100644--- a/src/count-tokens.ts+++ b/src/count-tokens.ts@@ -1,5 +1,6 @@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"export function countTokens(req: AnthropicRequest): number {@@ -33,3 +34,36 @@ export function countTokens(req: AnthropicRequest): number {total += req.messages.length * 4return total}++export function countTranslatedTokens(req: Pick<ResponsesRequest, "instructions" | "input" | "tools">): number {+ let total = 0+ if (req.instructions) total += encode(req.instructions).length++ for (const item of req.input) {+ if (item.type === "message") {+ for (const part of item.content) {+ if (part.type === "input_text" || part.type === "output_text") {+ total += encode(part.text).length+ } else if (part.type === "input_image") {+ total += encode(part.image_url).length+ }+ }+ } else if (item.type === "function_call") {+ total += encode(item.call_id).length+ total += encode(item.name).length+ total += encode(item.arguments).length+ } else if (item.type === "function_call_output") {+ total += encode(item.call_id).length+ total += encode(item.output).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.parameters ?? {})).length+ }++ total += req.input.length * 4+ return total+}diff --git a/src/server.ts b/src/server.tsindex eb7c87f..cd82fc1 100644--- a/src/server.ts+++ b/src/server.ts@@ -5,10 +5,11 @@ import { translateRequest } from "./translate/request.ts"import { translateStream } from "./translate/stream.ts"import { accumulateResponse, UpstreamStreamError } from "./translate/accumulate.ts"import { CodexError, postCodex } from "./codex/client.ts"-import { countTokens } from "./count-tokens.ts"+import { countTokens, countTranslatedTokens } from "./count-tokens.ts"const log = createLogger("server")const VERBOSE = !!process.env.CCP_LOG_VERBOSE+const LOG_COMPACTION = !!process.env.CCP_LOG_COMPACTIONexport interface ServeOptions {port: number@@ -52,6 +53,16 @@ async function route(req: Request, url: URL, reqId: string): Promise<Response> {const body = (await req.json()) as AnthropicRequestconst tokens = countTokens(body)log.debug("count_tokens", { reqId, tokens })+ if (LOG_COMPACTION) {+ log.info("compaction telemetry", {+ reqId,+ path: url.pathname,+ model: body.model,+ tokens,+ messageCount: body.messages?.length ?? 0,+ toolCount: body.tools?.length ?? 0,+ })+ }return new Response(JSON.stringify({ input_tokens: tokens }), {headers: { "content-type": "application/json" },})@@ -103,6 +114,8 @@ async function handleMessages(req: Request, reqId: string): Promise<Response> {}const translated = translateRequest({ ...body, model: resolvedModel }, { sessionId })+ const localInputTokens = LOG_COMPACTION ? countTokens(body) : undefined+ const translatedInputTokens = LOG_COMPACTION ? countTranslatedTokens(translated) : undefinedlog.debug("translated request", {reqId,requestedModel: body.model,@@ -113,6 +126,20 @@ async function handleMessages(req: Request, reqId: string): Promise<Response> {promptCacheKey: translated.prompt_cache_key,})if (VERBOSE) log.debug("translated request body", { reqId, body: translated })+ if (LOG_COMPACTION) {+ log.info("compaction telemetry", {+ reqId,+ phase: "translated_request",+ requestedModel: body.model,+ resolvedModel,+ localInputTokens,+ translatedInputTokens,+ inputItems: translated.input.length,+ toolCount: translated.tools?.length ?? 0,+ hasInstructions: !!translated.instructions,+ sessionId,+ })+ }let upstreamtry {@@ -139,7 +166,28 @@ async function handleMessages(req: Request, reqId: string): Promise<Response> {}if (wantStream) {- const stream = translateStream(upstream.body, { messageId, model: body.model })+ const stream = translateStream(upstream.body, {+ messageId,+ model: body.model,+ onFinish: LOG_COMPACTION+ ? (finish) => {+ log.info("compaction telemetry", {+ reqId,+ phase: "upstream_finish",+ mode: "stream",+ requestedModel: body.model,+ resolvedModel,+ localInputTokens,+ translatedInputTokens,+ upstreamInputTokens: finish.usage?.input_tokens ?? 0,+ upstreamOutputTokens: finish.usage?.output_tokens ?? 0,+ upstreamCachedInputTokens: finish.usage?.input_tokens_details?.cached_tokens ?? 0,+ stopReason: finish.stopReason,+ sessionId,+ })+ }+ : undefined,+ })return new Response(stream, {status: 200,headers: {@@ -152,6 +200,22 @@ async function handleMessages(req: Request, reqId: string): Promise<Response> {try {const result = await accumulateResponse(upstream.body, { messageId, model: body.model })+ if (LOG_COMPACTION) {+ log.info("compaction telemetry", {+ reqId,+ phase: "upstream_finish",+ mode: "non_stream",+ requestedModel: body.model,+ resolvedModel,+ localInputTokens,+ translatedInputTokens,+ upstreamInputTokens: result.usage.input_tokens,+ upstreamOutputTokens: result.usage.output_tokens,+ upstreamCachedInputTokens: result.usage.cache_read_input_tokens,+ stopReason: result.stop_reason,+ sessionId,+ })+ }return new Response(JSON.stringify(result), {headers: { "content-type": "application/json" },})diff --git a/src/translate/stream.ts b/src/translate/stream.tsindex a551282..89c6c78 100644--- a/src/translate/stream.ts+++ b/src/translate/stream.ts@@ -14,7 +14,11 @@ const log = createLogger("translate.stream")*/export function translateStream(upstream: ReadableStream<Uint8Array>,- opts: { messageId: string; model: string },+ opts: {+ messageId: string+ model: string+ onFinish?: (finish: { stopReason: "end_turn" | "tool_use" | "max_tokens"; usage?: Parameters<typeof mapUsageToAnthropic>[0] }) => void+ },): ReadableStream<Uint8Array> {const encoder = new TextEncoder()return new ReadableStream<Uint8Array>({@@ -93,6 +97,7 @@ export function translateStream(breakcase "finish":ensureMessageStart()+ opts.onFinish?.({ stopReason: e.stopReason, usage: e.usage })emit("message_delta", {type: "message_delta",delta: { stop_reason: e.stopReason, stop_sequence: null },