Skip to content

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

Rust

kimi: add stream and accumulation summary logging

Raine Virta610c2a2fe50e
diff --git a/src/providers/kimi/translate/accumulate.ts b/src/providers/kimi/translate/accumulate.ts
index 1d55a40..5efef66 100644
--- a/src/providers/kimi/translate/accumulate.ts
+++ b/src/providers/kimi/translate/accumulate.ts
@@ -44,8 +44,12 @@ export async function accumulateResponse(
let stopReason: AnthropicNonStreamResponse["stop_reason"] = null
let usage: ReturnType<typeof mapUsageToAnthropic> | undefined
let rawUsage: KimiUsage | undefined
+ let reasoningChars = 0
+ let contentChars = 0
+ let toolCount = 0
+ const stats = { chunkCount: 0 }
- for await (const e of reduceUpstream(upstream, opts.log)) {
+ for await (const e of reduceUpstream(upstream, opts.log, stats)) {
switch (e.kind) {
case "thinking-start":
blocks.set(e.index, { kind: "thinking", text: "" })
@@ -53,7 +57,10 @@ export async function accumulateResponse(
break
case "thinking-delta": {
const b = blocks.get(e.index)
- if (b?.kind === "thinking") b.text += e.text
+ if (b?.kind === "thinking") {
+ b.text += e.text
+ reasoningChars += e.text.length
+ }
break
}
case "text-start":
@@ -62,10 +69,14 @@ export async function accumulateResponse(
break
case "text-delta": {
const b = blocks.get(e.index)
- if (b?.kind === "text") b.text += e.text
+ if (b?.kind === "text") {
+ b.text += e.text
+ contentChars += e.text.length
+ }
break
}
case "tool-start":
+ toolCount++
blocks.set(e.index, { kind: "tool", id: e.id, name: e.name, args: "" })
ordered.push(e.index)
break
@@ -110,6 +121,15 @@ export async function accumulateResponse(
}
}
+ opts.log.debug("accumulate summary", {
+ chunkCount: stats.chunkCount,
+ reasoningChars,
+ contentChars,
+ toolCount,
+ stopReason,
+ usage: rawUsage,
+ })
+
return {
rawUsage,
response: {
diff --git a/src/providers/kimi/translate/stream.ts b/src/providers/kimi/translate/stream.ts
index 4206c11..e4dc228 100644
--- a/src/providers/kimi/translate/stream.ts
+++ b/src/providers/kimi/translate/stream.ts
@@ -53,8 +53,19 @@ export function translateStream(
emit("ping", { type: "ping" })
}
+ const streamStart = Date.now()
+ let firstChunkAt: number | undefined
+ let reasoningChars = 0
+ let contentChars = 0
+ let toolCount = 0
+ let finishUsage: KimiUsage | undefined
+ let finishStopReason: string | undefined
+ const stats = { chunkCount: 0 }
+
try {
- for await (const e of reduceUpstream(upstream, opts.log)) {
+ for await (const e of reduceUpstream(upstream, opts.log, stats)) {
+ if (firstChunkAt === undefined) firstChunkAt = Date.now()
+
switch (e.kind) {
case "thinking-start":
ensureMessageStart()
@@ -65,6 +76,7 @@ export function translateStream(
})
break
case "thinking-delta":
+ reasoningChars += e.text.length
emit("content_block_delta", {
type: "content_block_delta",
index: e.index,
@@ -95,6 +107,7 @@ export function translateStream(
})
break
case "text-delta":
+ contentChars += e.text.length
emit("content_block_delta", {
type: "content_block_delta",
index: e.index,
@@ -105,6 +118,7 @@ export function translateStream(
emit("content_block_stop", { type: "content_block_stop", index: e.index })
break
case "tool-start":
+ toolCount++
activeTools.set(e.index, { id: e.id, name: e.name })
ensureMessageStart()
emit("content_block_start", {
@@ -131,6 +145,8 @@ export function translateStream(
break
case "finish":
ensureMessageStart()
+ finishUsage = e.usage
+ finishStopReason = e.stopReason
opts.onFinish?.({ stopReason: e.stopReason, usage: e.usage })
emit("message_delta", {
type: "message_delta",
@@ -172,6 +188,16 @@ export function translateStream(
})
}
} finally {
+ opts.log.debug("stream summary", {
+ chunkCount: stats.chunkCount,
+ firstChunkMs: firstChunkAt ? firstChunkAt - streamStart : undefined,
+ totalMs: Date.now() - streamStart,
+ reasoningChars,
+ contentChars,
+ toolCount,
+ stopReason: finishStopReason,
+ usage: finishUsage,
+ })
controller.close()
}
},