Skip to content

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

Rust

kimi: implement chat translation and wire up handlers

Raine Virta70eae93e53b6
diff --git a/src/cli.ts b/src/cli.ts
index 6b62788..5b44bb5 100755
--- a/src/cli.ts
+++ b/src/cli.ts
@@ -31,6 +31,8 @@ async function main() {
console.log(` export ANTHROPIC_AUTH_TOKEN="anything"`)
if (provider.name === "codex") {
console.log(` export ANTHROPIC_MODEL="gpt-5.4"`)
+ } else if (provider.name === "kimi") {
+ console.log(` export ANTHROPIC_MODEL="kimi-for-coding"`)
}
console.log(` export CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC="1"`)
return
diff --git a/src/providers/kimi/client.ts b/src/providers/kimi/client.ts
new file mode 100644
index 0000000..29dea5b
--- /dev/null
+++ b/src/providers/kimi/client.ts
@@ -0,0 +1,104 @@
+import { API_BASE_URL } from "./auth/constants.ts"
+import { commonHeaders } from "./auth/headers.ts"
+import { forceRefresh, getAuth, KimiAuthUnauthorizedError } from "./auth/manager.ts"
+import { createLogger } from "../../log.ts"
+import type { KimiChatRequest } from "./translate/request.ts"
+
+const log = createLogger("kimi.client")
+
+export interface KimiPostOptions {
+ sessionId?: string
+ signal?: AbortSignal
+}
+
+export interface KimiResponse {
+ body: ReadableStream<Uint8Array>
+ status: number
+ headers: Headers
+}
+
+export class KimiError extends Error {
+ constructor(
+ public status: number,
+ message: string,
+ public detail?: string,
+ public meta?: { retryAfter?: string },
+ ) {
+ super(message)
+ this.name = "KimiError"
+ }
+}
+
+export async function postKimi(
+ body: KimiChatRequest,
+ opts: KimiPostOptions = {},
+): Promise<KimiResponse> {
+ let auth = await getAuth()
+ let resp = await doFetch(auth.access, body, opts)
+
+ if (resp.status === 401) {
+ log.warn("got 401, refreshing token", {})
+ try {
+ auth = await forceRefresh()
+ resp = await doFetch(auth.access, body, opts)
+ } catch (err) {
+ if (err instanceof KimiAuthUnauthorizedError) {
+ throw new KimiError(401, "Unauthorized", err.message)
+ }
+ log.error("refresh after 401 failed", { err: String(err) })
+ }
+ }
+
+ if (resp.status === 429) {
+ const retryAfter = resp.headers.get("retry-after") || undefined
+ const text = await safeText(resp)
+ throw new KimiError(429, "Rate limited", text, { retryAfter })
+ }
+
+ if (!resp.ok) {
+ const text = await safeText(resp)
+ const type = resp.status === 401 || resp.status === 403 ? "Unauthorized" : "Upstream error"
+ throw new KimiError(resp.status, type, text)
+ }
+
+ if (!resp.body) throw new KimiError(500, "Upstream returned no body")
+
+ return { body: resp.body, status: resp.status, headers: resp.headers }
+}
+
+async function doFetch(
+ accessToken: string,
+ body: KimiChatRequest,
+ opts: KimiPostOptions,
+): Promise<Response> {
+ const fp = await commonHeaders()
+ const headers = new Headers({
+ "Content-Type": "application/json",
+ Accept: "application/json",
+ Authorization: `Bearer ${accessToken}`,
+ ...fp,
+ })
+
+ log.debug("posting to kimi", {
+ url: `${API_BASE_URL}/chat/completions`,
+ model: body.model,
+ messageCount: body.messages.length,
+ toolCount: body.tools?.length ?? 0,
+ sessionId: opts.sessionId,
+ })
+
+ return fetch(`${API_BASE_URL}/chat/completions`, {
+ method: "POST",
+ headers,
+ body: JSON.stringify(body),
+ signal: opts.signal,
+ })
+}
+
+async function safeText(resp: Response): Promise<string> {
+ try {
+ return await resp.text()
+ } catch {
+ return ""
+ }
+}
diff --git a/src/providers/kimi/count-tokens.ts b/src/providers/kimi/count-tokens.ts
new file mode 100644
index 0000000..8819f91
--- /dev/null
+++ b/src/providers/kimi/count-tokens.ts
@@ -0,0 +1,79 @@
+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"
+
+const IMAGE_TOKEN_ESTIMATE = 2000
+
+// 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)
+ if (system) total += encode(system).length
+
+ for (const msg of req.messages) {
+ const blocks = normalizeContent(msg.content)
+ for (const block of blocks) {
+ if (block.type === "text") {
+ total += encode(block.text).length
+ } else if (block.type === "image") {
+ total += IMAGE_TOKEN_ESTIMATE
+ } else if (block.type === "tool_use") {
+ total += encode(block.name).length
+ total += encode(JSON.stringify(block.input ?? {})).length
+ } else if (block.type === "tool_result") {
+ total += encode(toolResultToString(block.content)).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 += req.messages.length * 4
+ return total
+}
+
+export function countTranslatedTokens(req: KimiChatRequest): number {
+ let total = 0
+ for (const m of req.messages) {
+ if (m.role === "system") {
+ total += encode(m.content).length
+ } else if (m.role === "user") {
+ if (typeof m.content === "string") total += encode(m.content).length
+ else {
+ for (const p of m.content) {
+ if (p.type === "text") total += encode(p.text).length
+ else total += IMAGE_TOKEN_ESTIMATE
+ }
+ }
+ } else if (m.role === "assistant") {
+ if (typeof m.content === "string") total += encode(m.content).length
+ for (const tc of m.tool_calls ?? []) {
+ total += encode(tc.function.name).length
+ total += encode(tc.function.arguments).length
+ }
+ } else if (m.role === "tool") {
+ if (typeof m.content === "string") total += encode(m.content).length
+ else {
+ for (const p of m.content) {
+ if (p.type === "text") total += encode(p.text).length
+ else total += IMAGE_TOKEN_ESTIMATE
+ }
+ }
+ }
+ }
+
+ 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 += req.messages.length * 4
+ return total
+}
diff --git a/src/providers/kimi/index.ts b/src/providers/kimi/index.ts
index 1621ea0..b02830f 100644
--- a/src/providers/kimi/index.ts
+++ b/src/providers/kimi/index.ts
@@ -1,19 +1,160 @@
import type { Provider, CliHandlers, RequestContext } from "../types.ts"
import type { AnthropicRequest } from "../../anthropic/schema.ts"
+import { createLogger } from "../../log.ts"
+import {
+ assertAllowedModel,
+ ModelNotAllowedError,
+ resolveModel,
+} from "./translate/model-allowlist.ts"
+import { translateRequest } from "./translate/request.ts"
+import { translateStream } from "./translate/stream.ts"
+import { accumulateResponse, UpstreamStreamError } from "./translate/accumulate.ts"
+import { countTokens, countTranslatedTokens } from "./count-tokens.ts"
+import { KimiError, postKimi } from "./client.ts"
import { runDeviceLogin } from "./auth/login.ts"
import { persistInitialTokens } from "./auth/manager.ts"
import { loadAuth, clearAuth, authPath } from "./auth/token-store.ts"
-function notImplemented(_body: AnthropicRequest, _ctx: RequestContext): Promise<Response> {
- return Promise.resolve(
- new Response(
- JSON.stringify({
- type: "error",
- error: { type: "api_error", message: "kimi provider: chat not implemented yet" },
- }),
- { status: 501, headers: { "content-type": "application/json" } },
- ),
+const log = createLogger("provider.kimi")
+const VERBOSE = !!process.env.CCP_LOG_VERBOSE
+
+function jsonError(status: number, type: string, message: string): Response {
+ return new Response(JSON.stringify({ type: "error", error: { type, message } }), {
+ status,
+ headers: { "content-type": "application/json" },
+ })
+}
+
+async function handleCountTokens(body: AnthropicRequest, ctx: RequestContext): Promise<Response> {
+ const resolvedModel = resolveModel(body.model)
+ const translated = translateRequest({ ...body, model: resolvedModel })
+ const tokens = countTranslatedTokens(translated)
+ log.debug("count_tokens", { reqId: ctx.reqId, tokens })
+ return new Response(JSON.stringify({ input_tokens: tokens }), {
+ headers: { "content-type": "application/json" },
+ })
+}
+
+async function handleMessages(body: AnthropicRequest, ctx: RequestContext): Promise<Response> {
+ const messageId = `msg_${crypto.randomUUID().replace(/-/g, "")}`
+ const wantStream = body.stream !== false
+ const messageCount = body.messages?.length ?? 0
+ const toolCount = body.tools?.length ?? 0
+
+ log.debug("anthropic request", {
+ reqId: ctx.reqId,
+ model: body.model,
+ messageCount,
+ toolCount,
+ stream: wantStream,
+ sessionId: ctx.sessionId,
+ requestedMaxTokens: body.max_tokens,
+ })
+ if (VERBOSE) log.debug("anthropic request body", { reqId: ctx.reqId, body })
+
+ const resolvedModel = resolveModel(body.model)
+ try {
+ assertAllowedModel(resolvedModel)
+ } catch (err) {
+ if (err instanceof ModelNotAllowedError) {
+ return jsonError(
+ 400,
+ "invalid_request_error",
+ `Model "${body.model}" resolves to unsupported model "${err.model}"`,
+ )
+ }
+ throw err
+ }
+
+ const translated = translateRequest(
+ { ...body, model: resolvedModel },
+ { sessionId: ctx.sessionId },
)
+ const localInputTokens = VERBOSE ? countTokens(body) : undefined
+ const translatedInputTokens = VERBOSE ? countTranslatedTokens(translated) : undefined
+ log.debug("translated request", {
+ reqId: ctx.reqId,
+ requestedModel: body.model,
+ resolvedModel,
+ messageCount: translated.messages.length,
+ toolCount: translated.tools?.length ?? 0,
+ localInputTokens,
+ translatedInputTokens,
+ promptCacheKey: translated.prompt_cache_key,
+ reasoningEffort: translated.reasoning_effort,
+ thinking: translated.thinking?.type,
+ maxTokens: translated.max_tokens,
+ })
+ if (VERBOSE) log.debug("translated request body", { reqId: ctx.reqId, body: translated })
+
+ let upstream
+ try {
+ upstream = await postKimi(translated, { sessionId: ctx.sessionId, signal: ctx.signal })
+ } catch (err) {
+ if (err instanceof KimiError) {
+ log.warn("kimi error", { reqId: ctx.reqId, status: err.status, detail: err.detail })
+ if (err.status === 429) {
+ const headers: Record<string, string> = { "content-type": "application/json" }
+ if (err.meta?.retryAfter) headers["retry-after"] = err.meta.retryAfter
+ return new Response(
+ JSON.stringify({
+ type: "error",
+ error: { type: "rate_limit_error", message: 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)
+ }
+ throw err
+ }
+
+ if (wantStream) {
+ const stream = translateStream(upstream.body, {
+ messageId,
+ model: body.model,
+ reqId: ctx.reqId,
+ sessionId: ctx.sessionId,
+ })
+ return new Response(stream, {
+ status: 200,
+ headers: {
+ "content-type": "text/event-stream",
+ "cache-control": "no-cache",
+ connection: "keep-alive",
+ },
+ })
+ }
+
+ try {
+ const result = await accumulateResponse(upstream.body, { messageId, model: body.model })
+ return new Response(JSON.stringify(result.response), {
+ headers: { "content-type": "application/json" },
+ })
+ } catch (err) {
+ if (err instanceof UpstreamStreamError) {
+ log.warn("upstream stream error (non-streaming)", {
+ reqId: ctx.reqId,
+ kind: err.kind,
+ message: err.message,
+ })
+ if (err.kind === "rate_limit") {
+ const headers: Record<string, string> = { "content-type": "application/json" }
+ if (err.retryAfterSeconds) headers["retry-after"] = String(err.retryAfterSeconds)
+ return new Response(
+ JSON.stringify({
+ type: "error",
+ error: { type: "rate_limit_error", message: err.message },
+ }),
+ { status: 429, headers },
+ )
+ }
+ return jsonError(502, "api_error", err.message)
+ }
+ throw err
+ }
}
const cli: CliHandlers = {
@@ -45,7 +186,7 @@ const cli: CliHandlers = {
export const kimiProvider: Provider = {
name: "kimi",
- handleMessages: notImplemented,
- handleCountTokens: notImplemented,
+ handleMessages,
+ handleCountTokens,
cli,
}
diff --git a/src/providers/kimi/translate/accumulate.ts b/src/providers/kimi/translate/accumulate.ts
new file mode 100644
index 0000000..083cb47
--- /dev/null
+++ b/src/providers/kimi/translate/accumulate.ts
@@ -0,0 +1,123 @@
+import { mapUsageToAnthropic, reduceUpstream, type KimiUsage } from "./reducer.ts"
+
+export { UpstreamStreamError } from "./reducer.ts"
+
+export interface AnthropicNonStreamResponse {
+ id: string
+ type: "message"
+ role: "assistant"
+ model: string
+ content: Array<
+ | { type: "text"; text: string }
+ | { type: "thinking"; thinking: string }
+ | { type: "tool_use"; id: string; name: string; input: unknown }
+ >
+ stop_reason: "end_turn" | "tool_use" | "max_tokens" | null
+ stop_sequence: null
+ usage: {
+ input_tokens: number
+ output_tokens: number
+ cache_creation_input_tokens: number
+ cache_read_input_tokens: number
+ }
+}
+
+export interface AccumulatedResponse {
+ response: AnthropicNonStreamResponse
+ rawUsage?: KimiUsage
+}
+
+export async function accumulateResponse(
+ upstream: ReadableStream<Uint8Array>,
+ opts: { messageId: string; model: string },
+): Promise<AccumulatedResponse> {
+ type Block =
+ | { kind: "thinking"; text: string }
+ | { kind: "text"; text: string }
+ | { kind: "tool"; id: string; name: string; args: string }
+
+ const ordered: number[] = []
+ const blocks = new Map<number, Block>()
+ let stopReason: AnthropicNonStreamResponse["stop_reason"] = null
+ let usage: ReturnType<typeof mapUsageToAnthropic> | undefined
+ let rawUsage: KimiUsage | undefined
+
+ for await (const e of reduceUpstream(upstream)) {
+ switch (e.kind) {
+ case "thinking-start":
+ blocks.set(e.index, { kind: "thinking", text: "" })
+ ordered.push(e.index)
+ break
+ case "thinking-delta": {
+ const b = blocks.get(e.index)
+ if (b?.kind === "thinking") b.text += e.text
+ break
+ }
+ case "text-start":
+ blocks.set(e.index, { kind: "text", text: "" })
+ ordered.push(e.index)
+ break
+ case "text-delta": {
+ const b = blocks.get(e.index)
+ if (b?.kind === "text") b.text += e.text
+ break
+ }
+ case "tool-start":
+ blocks.set(e.index, { kind: "tool", id: e.id, name: e.name, args: "" })
+ ordered.push(e.index)
+ break
+ case "tool-delta": {
+ const b = blocks.get(e.index)
+ if (b?.kind === "tool") b.args += e.partialJson
+ break
+ }
+ case "thinking-stop":
+ case "text-stop":
+ case "tool-stop":
+ break
+ case "finish":
+ stopReason = e.stopReason
+ rawUsage = e.usage
+ usage = mapUsageToAnthropic(e.usage)
+ break
+ }
+ }
+
+ const content: AnthropicNonStreamResponse["content"] = []
+ for (const i of ordered) {
+ const b = blocks.get(i)
+ if (!b) continue
+ if (b.kind === "thinking") {
+ if (b.text) content.push({ type: "thinking", thinking: b.text })
+ } else if (b.kind === "text") {
+ if (b.text) content.push({ type: "text", text: b.text })
+ } else {
+ let input: unknown = {}
+ try {
+ input = b.args ? JSON.parse(b.args) : {}
+ } catch {
+ input = { _raw: b.args }
+ }
+ content.push({ type: "tool_use", id: b.id, name: b.name, input })
+ }
+ }
+
+ return {
+ rawUsage,
+ response: {
+ id: opts.messageId,
+ type: "message",
+ role: "assistant",
+ model: opts.model,
+ 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,
+ },
+ },
+ }
+}
diff --git a/src/providers/kimi/translate/model-allowlist.ts b/src/providers/kimi/translate/model-allowlist.ts
new file mode 100644
index 0000000..4ebf640
--- /dev/null
+++ b/src/providers/kimi/translate/model-allowlist.ts
@@ -0,0 +1,32 @@
+export const KIMI_DEFAULT_MODEL = "kimi-for-coding"
+
+export const ALLOWED_MODELS = new Set([KIMI_DEFAULT_MODEL])
+
+// Every incoming model name collapses to kimi-for-coding — the only model
+// Kimi Code exposes. Explicit aliases exist so `ANTHROPIC_MODEL=haiku` works
+// for portability with the codex provider's aliasing.
+const ALIAS_TARGETS: Record<string, string> = {
+ haiku: KIMI_DEFAULT_MODEL,
+ "claude-haiku-4-5": KIMI_DEFAULT_MODEL,
+ "claude-haiku-4-5-20251001": KIMI_DEFAULT_MODEL,
+ sonnet: KIMI_DEFAULT_MODEL,
+ "claude-sonnet-4-6": KIMI_DEFAULT_MODEL,
+ opus: KIMI_DEFAULT_MODEL,
+ "claude-opus-4-7": KIMI_DEFAULT_MODEL,
+ "kimi-for-coding": KIMI_DEFAULT_MODEL,
+}
+
+export function resolveModel(model: string): string {
+ return ALIAS_TARGETS[model] ?? KIMI_DEFAULT_MODEL
+}
+
+export function assertAllowedModel(model: string): void {
+ if (!ALLOWED_MODELS.has(model)) throw new ModelNotAllowedError(model)
+}
+
+export class ModelNotAllowedError extends Error {
+ constructor(public model: string) {
+ super(`Model not allowed: ${model}`)
+ this.name = "ModelNotAllowedError"
+ }
+}
diff --git a/src/providers/kimi/translate/reducer.ts b/src/providers/kimi/translate/reducer.ts
new file mode 100644
index 0000000..23a7bf7
--- /dev/null
+++ b/src/providers/kimi/translate/reducer.ts
@@ -0,0 +1,216 @@
+import { parseSseStream } from "../../../sse.ts"
+import { createLogger } from "../../../log.ts"
+
+const log = createLogger("kimi.reducer")
+const VERBOSE = !!process.env.CCP_LOG_VERBOSE
+
+export class UpstreamStreamError extends Error {
+ constructor(
+ public kind: "rate_limit" | "failed",
+ message: string,
+ public retryAfterSeconds?: number,
+ ) {
+ super(message)
+ this.name = "UpstreamStreamError"
+ }
+}
+
+export interface KimiUsage {
+ prompt_tokens?: number
+ completion_tokens?: number
+ total_tokens?: number
+ cached_tokens?: number
+ prompt_tokens_details?: { cached_tokens?: number }
+ completion_tokens_details?: { reasoning_tokens?: number }
+}
+
+export type StopReason = "end_turn" | "tool_use" | "max_tokens"
+
+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 }
+ | { kind: "finish"; stopReason: StopReason; usage: KimiUsage | undefined }
+
+interface StreamChunk {
+ choices?: Array<{
+ delta?: {
+ role?: string
+ content?: string | null
+ reasoning_content?: string | null
+ tool_calls?: Array<{
+ index: number
+ id?: string
+ type?: string
+ function?: { name?: string; arguments?: string }
+ }>
+ }
+ finish_reason?: string | null
+ }>
+ usage?: KimiUsage
+ error?: { message?: string; type?: string }
+}
+
+interface ToolSlot {
+ blockIndex: number
+ id: string
+ name: string
+}
+
+/**
+ * Translate a Kimi/OpenAI-style chat-completions SSE stream into a stream
+ * of typed, downstream-agnostic ReducerEvents consumed by both the
+ * streaming and non-streaming frontends.
+ */
+export async function* reduceUpstream(
+ upstream: ReadableStream<Uint8Array>,
+): AsyncGenerator<ReducerEvent> {
+ let nextBlockIndex = 0
+ let thinkingIndex: number | undefined
+ let textIndex: number | undefined
+ const toolSlots = new Map<number, ToolSlot>() // keyed by upstream tool_calls[].index
+
+ let sawToolCalls = false
+ let finishReason: string | null | undefined
+ let finalUsage: KimiUsage | undefined
+
+ const closeThinking = function* () {
+ if (thinkingIndex !== undefined) {
+ const idx = thinkingIndex
+ thinkingIndex = undefined
+ yield { kind: "thinking-stop" as const, index: idx }
+ }
+ }
+ const closeText = function* () {
+ if (textIndex !== undefined) {
+ const idx = textIndex
+ textIndex = undefined
+ yield { kind: "text-stop" as const, index: idx }
+ }
+ }
+ const closeAllTools = function* () {
+ for (const slot of toolSlots.values()) {
+ yield { kind: "tool-stop" as const, index: slot.blockIndex }
+ }
+ toolSlots.clear()
+ }
+
+ for await (const evt of parseSseStream(upstream)) {
+ if (!evt.data) continue
+ const data = evt.data.trim()
+ if (data === "[DONE]") continue
+
+ let chunk: StreamChunk
+ try {
+ chunk = JSON.parse(data) as StreamChunk
+ } catch {
+ continue
+ }
+
+ if (VERBOSE) {
+ const d = chunk.choices?.[0]?.delta
+ log.debug("upstream chunk", {
+ hasReasoning: typeof d?.reasoning_content === "string",
+ hasContent: typeof d?.content === "string" && d.content.length > 0,
+ toolCalls: d?.tool_calls?.length,
+ finish: chunk.choices?.[0]?.finish_reason,
+ })
+ }
+
+ if (chunk.error) {
+ throw new UpstreamStreamError("failed", chunk.error.message || "Upstream error")
+ }
+
+ if (chunk.usage && !chunk.choices?.length) {
+ finalUsage = chunk.usage
+ continue
+ }
+
+ const choice = chunk.choices?.[0]
+ if (!choice) continue
+ const delta = choice.delta ?? {}
+
+ if (typeof delta.reasoning_content === "string" && delta.reasoning_content.length > 0) {
+ if (thinkingIndex === undefined) {
+ thinkingIndex = nextBlockIndex++
+ yield { kind: "thinking-start", index: thinkingIndex }
+ }
+ yield { kind: "thinking-delta", index: thinkingIndex, text: delta.reasoning_content }
+ }
+
+ if (typeof delta.content === "string" && delta.content.length > 0) {
+ yield* closeThinking()
+ if (textIndex === undefined) {
+ textIndex = nextBlockIndex++
+ yield { kind: "text-start", index: textIndex }
+ }
+ yield { kind: "text-delta", index: textIndex, text: delta.content }
+ }
+
+ if (delta.tool_calls?.length) {
+ yield* closeThinking()
+ yield* closeText()
+ for (const tc of delta.tool_calls) {
+ const upstreamIdx = tc.index
+ let slot = toolSlots.get(upstreamIdx)
+ if (!slot) {
+ // First fragment of this tool call: must carry id + function.name.
+ const id = tc.id ?? ""
+ const name = tc.function?.name ?? ""
+ if (!id || !name) {
+ // Defensive: out-of-order fragment before the initial one. Skip.
+ continue
+ }
+ sawToolCalls = true
+ slot = { blockIndex: nextBlockIndex++, id, name }
+ toolSlots.set(upstreamIdx, slot)
+ yield { kind: "tool-start", index: slot.blockIndex, id, name }
+ }
+ const argsDelta = tc.function?.arguments
+ if (typeof argsDelta === "string" && argsDelta.length > 0) {
+ yield { kind: "tool-delta", index: slot.blockIndex, partialJson: argsDelta }
+ }
+ }
+ }
+
+ if (choice.finish_reason) {
+ finishReason = choice.finish_reason
+ if (chunk.usage) finalUsage = chunk.usage
+ }
+ }
+
+ yield* closeThinking()
+ yield* closeText()
+ yield* closeAllTools()
+
+ const stopReason: StopReason =
+ finishReason === "length"
+ ? "max_tokens"
+ : finishReason === "tool_calls" || sawToolCalls
+ ? "tool_use"
+ : "end_turn"
+
+ yield { kind: "finish", stopReason, usage: finalUsage }
+}
+
+export function mapUsageToAnthropic(u: KimiUsage | undefined): {
+ input_tokens: number
+ output_tokens: number
+ cache_creation_input_tokens: number
+ cache_read_input_tokens: number
+} {
+ 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,
+ }
+}
diff --git a/src/providers/kimi/translate/request.ts b/src/providers/kimi/translate/request.ts
new file mode 100644
index 0000000..8ca9178
--- /dev/null
+++ b/src/providers/kimi/translate/request.ts
@@ -0,0 +1,267 @@
+import type {
+ AnthropicContentBlock,
+ AnthropicImageBlock,
+ AnthropicMessage,
+ AnthropicRequest,
+ AnthropicTextBlock,
+ AnthropicTool,
+} from "../../../anthropic/schema.ts"
+
+// OpenAI-compatible chat-completions request shape used by Kimi.
+// Only the fields kimi-cli is known to send are included; unknown
+// fields are not forwarded.
+export interface KimiChatRequest {
+ model: string
+ messages: KimiMessage[]
+ tools?: KimiTool[]
+ tool_choice?: KimiToolChoice
+ stream: true
+ stream_options: { include_usage: true }
+ max_tokens: number
+ reasoning_effort?: "low" | "medium" | "high"
+ thinking?: { type: "enabled" }
+ prompt_cache_key?: string
+}
+
+export type KimiToolChoice =
+ | "auto"
+ | "none"
+ | "required"
+ | { type: "function"; function: { name: string } }
+
+export type KimiMessage =
+ | { role: "system"; content: string }
+ | { role: "user"; content: string | KimiUserContentPart[] }
+ | {
+ role: "assistant"
+ content?: string | null
+ tool_calls?: KimiAssistantToolCall[]
+ }
+ | { role: "tool"; tool_call_id: string; content: string | KimiToolResultPart[] }
+
+export type KimiUserContentPart =
+ | { type: "text"; text: string }
+ | { type: "image_url"; image_url: { url: string } }
+
+export type KimiToolResultPart =
+ | { type: "text"; text: string }
+ | { type: "image_url"; image_url: { url: string } }
+
+export interface KimiAssistantToolCall {
+ id: string
+ type: "function"
+ function: { name: string; arguments: string }
+}
+
+export interface KimiTool {
+ type: "function"
+ function: { name: string; description?: string; parameters: unknown }
+}
+
+export interface TranslateOptions {
+ sessionId?: string
+}
+
+const DEFAULT_MAX_TOKENS = 32000
+
+export function translateRequest(
+ req: AnthropicRequest,
+ opts: TranslateOptions = {},
+): KimiChatRequest {
+ const messages = buildMessages(req)
+ const tools = req.tools?.map(toKimiTool)
+
+ const thinkingDisabled = req.thinking?.type === "disabled"
+ const effort = req.output_config?.effort ?? "high"
+
+ const out: KimiChatRequest = {
+ model: req.model,
+ messages,
+ stream: true,
+ stream_options: { include_usage: true },
+ max_tokens: clampMaxTokens(req.max_tokens),
+ }
+ if (!thinkingDisabled) {
+ out.reasoning_effort = effort
+ out.thinking = { type: "enabled" }
+ }
+ if (tools && tools.length) out.tools = tools
+ const tool_choice = mapToolChoice(req.tool_choice)
+ if (tool_choice !== "auto") out.tool_choice = tool_choice
+ if (opts.sessionId) out.prompt_cache_key = opts.sessionId
+ return out
+}
+
+function clampMaxTokens(requested: number | undefined): number {
+ if (!requested || requested <= 0) return DEFAULT_MAX_TOKENS
+ return Math.min(requested, DEFAULT_MAX_TOKENS)
+}
+
+function mapToolChoice(choice: AnthropicRequest["tool_choice"]): KimiToolChoice {
+ if (!choice) return "auto"
+ switch (choice.type) {
+ case "auto":
+ return "auto"
+ case "none":
+ return "none"
+ case "any":
+ return "required"
+ case "tool":
+ return choice.name
+ ? { type: "function", function: { name: choice.name } }
+ : "required"
+ }
+}
+
+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")
+}
+
+function buildMessages(req: AnthropicRequest): KimiMessage[] {
+ const out: KimiMessage[] = []
+ const system = buildSystemMessage(req.system)
+ if (system) out.push({ role: "system", content: system })
+
+ for (const msg of req.messages) {
+ const blocks = normalizeContent(msg.content)
+ if (msg.role === "user") {
+ pushUserMessages(out, blocks)
+ } else {
+ pushAssistantMessage(out, blocks)
+ }
+ }
+ return out
+}
+
+// In Anthropic-land a single user message can carry a mix of text, image,
+// and tool_result blocks. OpenAI-style chat-completions requires tool
+// results to be their own role="tool" messages, so we split on tool_result
+// boundaries, preserving order.
+function pushUserMessages(out: KimiMessage[], blocks: AnthropicContentBlock[]): void {
+ let buffer: KimiUserContentPart[] = []
+ const flushBuffer = () => {
+ if (!buffer.length) return
+ // Collapse to a string when every part is text — matches what kimi-cli
+ // sends and keeps the wire payload compact.
+ const allText = buffer.every((p) => p.type === "text")
+ if (allText) {
+ out.push({
+ role: "user",
+ content: buffer.map((p) => (p as { type: "text"; text: string }).text).join(""),
+ })
+ } else {
+ out.push({ role: "user", content: buffer })
+ }
+ buffer = []
+ }
+
+ for (const block of blocks) {
+ 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) } })
+ } else if (block.type === "tool_result") {
+ flushBuffer()
+ out.push({
+ role: "tool",
+ tool_call_id: block.tool_use_id,
+ content: toolResultContent(block.content, block.is_error),
+ })
+ }
+ }
+ flushBuffer()
+}
+
+function pushAssistantMessage(out: KimiMessage[], blocks: AnthropicContentBlock[]): void {
+ const textParts: string[] = []
+ const toolCalls: KimiAssistantToolCall[] = []
+ for (const block of blocks) {
+ if (block.type === "text") {
+ if (block.text) textParts.push(block.text)
+ } else if (block.type === "tool_use") {
+ toolCalls.push({
+ id: block.id,
+ type: "function",
+ function: {
+ name: block.name,
+ arguments: JSON.stringify(block.input ?? {}),
+ },
+ })
+ }
+ // thinking / image blocks from the assistant are dropped — Kimi's
+ // assistant schema does not express them.
+ }
+
+ if (!textParts.length && !toolCalls.length) return
+
+ const msg: Extract<KimiMessage, { role: "assistant" }> = { role: "assistant" }
+ if (textParts.length) msg.content = textParts.join("")
+ else msg.content = ""
+ 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}`
+}
+
+export function toolResultContent(
+ content: string | Array<AnthropicTextBlock | AnthropicImageBlock>,
+ isError: boolean | undefined,
+): string | KimiToolResultPart[] {
+ const prefix = isError ? "[tool execution error]\n" : ""
+ if (typeof content === "string") return prefix + content
+
+ const parts: KimiToolResultPart[] = []
+ if (prefix) parts.push({ type: "text", text: prefix })
+ for (const b of content) {
+ if (b.type === "text") {
+ parts.push({ type: "text", text: b.text })
+ } else {
+ // Kimi accepts image_url parts inside role="tool" content (its own
+ // ReadMediaFile tool uses exactly this shape).
+ parts.push({ type: "image_url", image_url: { url: imageToUrl(b) } })
+ }
+ }
+ if (parts.length === 1 && parts[0]!.type === "text") return parts[0]!.text
+ return parts
+}
+
+export function toolResultToString(
+ content: string | Array<AnthropicTextBlock | AnthropicImageBlock>,
+): string {
+ // Kept for the token counter, which wants a flat string.
+ if (typeof content === "string") return content
+ return content
+ .map((b) => {
+ if (b.type === "text") return b.text
+ const mt = b.source.type === "base64" ? b.source.media_type : "url"
+ return `[image omitted: ${mt}]`
+ })
+ .join("\n")
+}
+
+function toKimiTool(tool: AnthropicTool): KimiTool {
+ return {
+ type: "function",
+ function: {
+ name: tool.name,
+ description: tool.description,
+ parameters: tool.input_schema,
+ },
+ }
+}
diff --git a/src/providers/kimi/translate/stream.ts b/src/providers/kimi/translate/stream.ts
new file mode 100644
index 0000000..232380d
--- /dev/null
+++ b/src/providers/kimi/translate/stream.ts
@@ -0,0 +1,173 @@
+import { encodeSseEvent } from "../../../sse.ts"
+import { createLogger } from "../../../log.ts"
+import { mapUsageToAnthropic, reduceUpstream, UpstreamStreamError } from "./reducer.ts"
+
+const log = createLogger("kimi.stream")
+
+/**
+ * Translate a Kimi chat-completions SSE stream into Anthropic SSE events.
+ * HTTP status 200 is already committed before the first upstream event is
+ * read, so rate-limit and upstream-failed conditions surface here as SSE
+ * `error` events instead of non-200 statuses.
+ */
+export function translateStream(
+ upstream: ReadableStream<Uint8Array>,
+ opts: {
+ messageId: string
+ model: string
+ reqId: string
+ sessionId?: 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>({
+ async start(controller) {
+ const emit = (event: string, data: unknown) => {
+ controller.enqueue(encoder.encode(encodeSseEvent(event, data)))
+ }
+ const activeTools = new Map<number, { id: string; name: string }>()
+ let messageStarted = false
+ const ensureMessageStart = () => {
+ if (messageStarted) return
+ messageStarted = true
+ emit("message_start", {
+ type: "message_start",
+ message: {
+ id: opts.messageId,
+ type: "message",
+ role: "assistant",
+ model: opts.model,
+ content: [],
+ stop_reason: null,
+ stop_sequence: null,
+ usage: {
+ input_tokens: 0,
+ output_tokens: 0,
+ cache_creation_input_tokens: 0,
+ cache_read_input_tokens: 0,
+ },
+ },
+ })
+ emit("ping", { type: "ping" })
+ }
+
+ try {
+ for await (const e of reduceUpstream(upstream)) {
+ switch (e.kind) {
+ case "thinking-start":
+ ensureMessageStart()
+ emit("content_block_start", {
+ type: "content_block_start",
+ index: e.index,
+ content_block: { type: "thinking", thinking: "" },
+ })
+ break
+ case "thinking-delta":
+ emit("content_block_delta", {
+ type: "content_block_delta",
+ index: e.index,
+ delta: { type: "thinking_delta", thinking: e.text },
+ })
+ break
+ case "thinking-stop":
+ emit("content_block_stop", { type: "content_block_stop", index: e.index })
+ break
+ case "text-start":
+ ensureMessageStart()
+ emit("content_block_start", {
+ type: "content_block_start",
+ index: e.index,
+ content_block: { type: "text", text: "" },
+ })
+ break
+ case "text-delta":
+ emit("content_block_delta", {
+ type: "content_block_delta",
+ index: e.index,
+ delta: { type: "text_delta", text: e.text },
+ })
+ break
+ case "text-stop":
+ emit("content_block_stop", { type: "content_block_stop", index: e.index })
+ break
+ case "tool-start":
+ activeTools.set(e.index, { id: e.id, name: e.name })
+ ensureMessageStart()
+ emit("content_block_start", {
+ type: "content_block_start",
+ index: e.index,
+ content_block: {
+ type: "tool_use",
+ id: e.id,
+ name: e.name,
+ input: {},
+ },
+ })
+ break
+ case "tool-delta":
+ emit("content_block_delta", {
+ type: "content_block_delta",
+ index: e.index,
+ delta: { type: "input_json_delta", partial_json: e.partialJson },
+ })
+ break
+ case "tool-stop":
+ activeTools.delete(e.index)
+ emit("content_block_stop", { type: "content_block_stop", index: e.index })
+ break
+ case "finish":
+ ensureMessageStart()
+ opts.onFinish?.({ stopReason: e.stopReason, usage: e.usage })
+ emit("message_delta", {
+ type: "message_delta",
+ delta: { stop_reason: e.stopReason, stop_sequence: null },
+ usage: mapUsageToAnthropic(e.usage),
+ })
+ emit("message_stop", { type: "message_stop" })
+ break
+ }
+ }
+ } catch (err) {
+ const activeToolNames = Array.from(activeTools.values(), (t) => t.name)
+ const activeToolCalls = Array.from(activeTools.values())
+ if (err instanceof UpstreamStreamError) {
+ log.warn("upstream stream error", {
+ reqId: opts.reqId,
+ sessionId: opts.sessionId,
+ kind: err.kind,
+ message: err.message,
+ activeToolNames,
+ activeToolCalls,
+ })
+ ensureMessageStart()
+ emit("error", {
+ type: "error",
+ error: {
+ type: err.kind === "rate_limit" ? "rate_limit_error" : "api_error",
+ message: err.message,
+ },
+ })
+ } else {
+ log.error("stream translation error", {
+ reqId: opts.reqId,
+ sessionId: opts.sessionId,
+ err: String(err),
+ activeToolNames,
+ activeToolCalls,
+ })
+ ensureMessageStart()
+ emit("error", {
+ type: "error",
+ error: { type: "api_error", message: String(err) },
+ })
+ }
+ } finally {
+ controller.close()
+ }
+ },
+ })
+}