Skip to content

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

Rust

add request_completed log and handle abort errors gracefully

Raine Virtac19b96bbb6fa
diff --git a/src/providers/kimi/translate/stream.ts b/src/providers/kimi/translate/stream.ts
index c3739ea..90c3b8f 100644
--- a/src/providers/kimi/translate/stream.ts
+++ b/src/providers/kimi/translate/stream.ts
@@ -3,6 +3,10 @@ import type { Logger } from "../../../log.ts"
import { mapUsageToAnthropic, reduceUpstream, UpstreamStreamError, type KimiUsage } from "./reducer.ts"
import { makeThinkingSignature } from "./signature.ts"
+function isAbortError(err: unknown): boolean {
+ return err instanceof Error && err.name === "AbortError"
+}
+
/**
* Translate a Kimi chat-completions SSE stream into Anthropic SSE events.
* HTTP status 200 is already committed before the first upstream event is
@@ -161,7 +165,9 @@ export function translateStream(
} catch (err) {
const activeToolNames = Array.from(activeTools.values(), (t) => t.name)
const activeToolCalls = Array.from(activeTools.values())
- if (err instanceof UpstreamStreamError) {
+ if (isAbortError(err)) {
+ opts.log.info("client disconnected")
+ } else if (err instanceof UpstreamStreamError) {
opts.log.warn("upstream stream error", {
kind: err.kind,
message: err.message,
@@ -204,7 +210,11 @@ export function translateStream(
stopReason: finishStopReason,
usage: finishUsage,
})
- controller.close()
+ try {
+ controller.close()
+ } catch {
+ // ignore if controller already errored or cancelled
+ }
}
},
})
diff --git a/src/server.ts b/src/server.ts
index 2afe17f..54b56c6 100644
--- a/src/server.ts
+++ b/src/server.ts
@@ -36,9 +36,15 @@ export function startServer(opts: ServeOptions): { stop: () => void; port: numbe
})
try {
const resp = await route(req, url, reqId)
- rootLog.info("response", { reqId, status: resp.status, ms: Date.now() - start })
- return resp
+ const ms = Date.now() - start
+ rootLog.info("response", { reqId, status: resp.status, ms })
+ if (!resp.body) return resp
+ return wrapStreamResponse(resp, reqId, start, rootLog)
} catch (err) {
+ if (isAbortError(err)) {
+ rootLog.info("client disconnected", { reqId, ms: Date.now() - start })
+ return new Response(null, { status: 499 })
+ }
rootLog.error("handler error", { reqId, err: String(err), stack: (err as Error)?.stack })
return jsonError(500, "internal_error", String(err))
}
@@ -136,6 +142,48 @@ async function parseJsonBody(req: Request): Promise<AnthropicRequest | Response>
}
}
+function isAbortError(err: unknown): boolean {
+ return err instanceof Error && err.name === "AbortError"
+}
+
+function wrapStreamResponse(
+ resp: Response,
+ reqId: string,
+ start: number,
+ log: ReturnType<typeof createLogger>,
+): Response {
+ const body = resp.body!
+ const reader = body.getReader()
+ const stream = new ReadableStream<Uint8Array>({
+ async pull(controller) {
+ try {
+ const { done, value } = await reader.read()
+ if (done) {
+ log.info("request_completed", { reqId, status: resp.status, ms: Date.now() - start })
+ controller.close()
+ return
+ }
+ controller.enqueue(value)
+ } catch (err) {
+ if (isAbortError(err)) {
+ log.info("client disconnected", { reqId, ms: Date.now() - start })
+ } else {
+ log.error("stream error", { reqId, err: String(err) })
+ }
+ controller.error(err)
+ }
+ },
+ cancel() {
+ reader.cancel().catch(() => {})
+ },
+ })
+ return new Response(stream, {
+ status: resp.status,
+ statusText: resp.statusText,
+ headers: resp.headers,
+ })
+}
+
function jsonError(status: number, type: string, message: string): Response {
return new Response(JSON.stringify({ type: "error", error: { type, message } }), {
status,