Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy
Rust
log codex stream transport diagnostics
Raine Virta4af6cf348057
diff --git a/src/providers/codex/client.ts b/src/providers/codex/client.tsindex 65425af..392304e 100644--- a/src/providers/codex/client.ts+++ b/src/providers/codex/client.ts@@ -107,6 +107,7 @@ async function doFetch(headers,body: JSON.stringify(body),signal,+ verbose: true,});}diff --git a/src/providers/codex/index.ts b/src/providers/codex/index.tsindex 7a6633f..97fcb9d 100644--- a/src/providers/codex/index.ts+++ b/src/providers/codex/index.ts@@ -261,6 +261,8 @@ async function handleMessages(body: AnthropicRequest, ctx: RequestContext): PrommessageId,model: body.model,log: ctx.childLogger("codex.stream"),+ signal: ctx.signal,+ upstreamHeaders: upstream.headers,onFinish: logVerbose()? (finish) => {const mappedUsage = finish.usage ? mapUsageToAnthropic(finish.usage) : undefined;diff --git a/src/providers/codex/translate/reducer.ts b/src/providers/codex/translate/reducer.tsindex e2ebbb3..0fcd366 100644--- a/src/providers/codex/translate/reducer.ts+++ b/src/providers/codex/translate/reducer.ts@@ -1,4 +1,4 @@-import { parseSseStream } from "../../../sse.ts";+import { createSseStreamStats, parseSseStream, type SseStreamStats } from "../../../sse.ts";import type { Logger } from "../../../log.ts";import { logVerbose } from "../../../config.ts";@@ -75,9 +75,23 @@ function sanitizeToolArgs(name: string, args: string): string {* response.failed/response.error. Any usage that arrived before the* failure is discarded.*/+export interface UpstreamStreamDiagnostics {+ stats: SseStreamStats;+ lastEventType?: string;+ sawTerminalEvent: boolean;+}++export function createUpstreamStreamDiagnostics(): UpstreamStreamDiagnostics {+ return {+ stats: createSseStreamStats(),+ sawTerminalEvent: false,+ };+}+export async function* reduceUpstream(upstream: ReadableStream<Uint8Array>,log: Logger,+ diagnostics = createUpstreamStreamDiagnostics(),): AsyncGenerator<ReducerEvent> {const blocksByOutputIndex = new Map<number, BlockState>();const itemIdToOutputIndex = new Map<string, number>();@@ -86,7 +100,7 @@ export async function* reduceUpstream(let finalUsage: CodexUsage | undefined;let incomplete = false;- for await (const evt of parseSseStream(upstream)) {+ for await (const evt of parseSseStream(upstream, diagnostics.stats)) {if (!evt.data) continue;let p: any;try {@@ -96,6 +110,7 @@ export async function* reduceUpstream(continue;}const t: string = p.type || evt.event || "";+ diagnostics.lastEventType = t;if (logVerbose())log.debug("upstream event", { type: t, output_index: p.output_index, item_id: p.item_id });@@ -229,6 +244,7 @@ export async function* reduceUpstream(}if (t === "response.completed" || t === "response.incomplete") {+ diagnostics.sawTerminalEvent = true;finalUsage = p.response?.usage;const reason = p.response?.incomplete_details?.reason;if (diff --git a/src/providers/codex/translate/stream.ts b/src/providers/codex/translate/stream.tsindex 8403f29..7c66b91 100644--- a/src/providers/codex/translate/stream.ts+++ b/src/providers/codex/translate/stream.ts@@ -1,6 +1,11 @@import { encodeSseEvent } from "../../../sse.ts";import type { Logger } from "../../../log.ts";-import { mapUsageToAnthropic, reduceUpstream, UpstreamStreamError } from "./reducer.ts";+import {+ createUpstreamStreamDiagnostics,+ mapUsageToAnthropic,+ reduceUpstream,+ UpstreamStreamError,+} from "./reducer.ts";/*** Translate a Codex Responses SSE stream into Anthropic SSE events.@@ -16,6 +21,8 @@ export function translateStream(messageId: string;model: string;log: Logger;+ signal?: AbortSignal;+ upstreamHeaders?: Headers;onFinish?: (finish: {stopReason: "end_turn" | "tool_use" | "max_tokens";usage?: Parameters<typeof mapUsageToAnthropic>[0];@@ -29,6 +36,7 @@ export function translateStream(controller.enqueue(encoder.encode(encodeSseEvent(event, data)));};const activeTools = new Map<number, { id: string; name: string }>();+ const diagnostics = createUpstreamStreamDiagnostics();let messageStarted = false;const ensureMessageStart = () => {if (messageStarted) return;@@ -55,7 +63,12 @@ export function translateStream(};try {- for await (const e of reduceUpstream(upstream, opts.log)) {+ opts.log.info("upstream stream start", {+ upstreamHeaders: opts.upstreamHeaders+ ? Object.fromEntries(opts.upstreamHeaders.entries())+ : undefined,+ });+ for await (const e of reduceUpstream(upstream, opts.log, diagnostics)) {switch (e.kind) {case "text-start":ensureMessageStart();@@ -121,6 +134,8 @@ export function translateStream(message: err.message,activeToolNames,activeToolCalls,+ clientAborted: opts.signal?.aborted ?? false,+ diagnostics: describeDiagnostics(diagnostics),});ensureMessageStart();emit("error", {@@ -132,9 +147,11 @@ export function translateStream(});} else {opts.log.error("stream translation error", {- err: String(err),+ err: describeError(err),activeToolNames,activeToolCalls,+ clientAborted: opts.signal?.aborted ?? false,+ diagnostics: describeDiagnostics(diagnostics),});ensureMessageStart();emit("error", {@@ -143,8 +160,52 @@ export function translateStream(});}} finally {+ opts.log.info("upstream stream end", { diagnostics: describeDiagnostics(diagnostics) });controller.close();}},});}++function describeDiagnostics(diagnostics: ReturnType<typeof createUpstreamStreamDiagnostics>) {+ const now = Date.now();+ return {+ bytesRead: diagnostics.stats.bytesRead,+ chunkCount: diagnostics.stats.chunkCount,+ eventCount: diagnostics.stats.eventCount,+ durationMs: now - diagnostics.stats.startedAt,+ msSinceLastChunk: diagnostics.stats.lastChunkAt ? now - diagnostics.stats.lastChunkAt : undefined,+ msSinceLastEvent: diagnostics.stats.lastEventAt ? now - diagnostics.stats.lastEventAt : undefined,+ lastEventType: diagnostics.lastEventType,+ sawTerminalEvent: diagnostics.sawTerminalEvent,+ };+}++function describeError(err: unknown) {+ if (!(err instanceof Error)) return { message: String(err) };+ const detail = err as Error & {+ code?: unknown;+ errno?: unknown;+ cause?: unknown;+ };+ return {+ name: err.name,+ message: err.message,+ code: detail.code,+ errno: detail.errno,+ cause: describeCause(detail.cause),+ stack: err.stack,+ };+}++function describeCause(cause: unknown): unknown {+ if (!(cause instanceof Error)) return cause;+ const detail = cause as Error & { code?: unknown; errno?: unknown };+ return {+ name: cause.name,+ message: cause.message,+ code: detail.code,+ errno: detail.errno,+ stack: cause.stack,+ };+}diff --git a/src/sse.ts b/src/sse.tsindex 8b2dc29..d054ff4 100644--- a/src/sse.ts+++ b/src/sse.ts@@ -3,13 +3,34 @@ export interface SseEvent {data: string;}+export interface SseStreamStats {+ bytesRead: number;+ chunkCount: number;+ eventCount: number;+ startedAt: number;+ lastChunkAt?: number;+ lastEventAt?: number;+}++export function createSseStreamStats(now = Date.now()): SseStreamStats {+ return {+ bytesRead: 0,+ chunkCount: 0,+ eventCount: 0,+ startedAt: now,+ };+}+export function encodeSseEvent(event: string, data: unknown): string {return `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`;}const BOUNDARY = /\r\n\r\n|\n\n|\r\r/;-export async function* parseSseStream(body: ReadableStream<Uint8Array>): AsyncGenerator<SseEvent> {+export async function* parseSseStream(+ body: ReadableStream<Uint8Array>,+ stats = createSseStreamStats(),+): AsyncGenerator<SseEvent> {const reader = body.getReader();const decoder = new TextDecoder();let buf = "";@@ -17,18 +38,29 @@ export async function* parseSseStream(body: ReadableStream<Uint8Array>): AsyncGewhile (true) {const { value, done } = await reader.read();if (done) break;+ stats.bytesRead += value.byteLength;+ stats.chunkCount += 1;+ stats.lastChunkAt = Date.now();buf += decoder.decode(value, { stream: true });let match: RegExpExecArray | null;while ((match = BOUNDARY.exec(buf)) !== null) {const raw = buf.slice(0, match.index);buf = buf.slice(match.index + match[0].length);const evt = parseEventBlock(raw);- if (evt) yield evt;+ if (evt) {+ stats.eventCount += 1;+ stats.lastEventAt = Date.now();+ yield evt;+ }}}if (buf.trim()) {const evt = parseEventBlock(buf);- if (evt) yield evt;+ if (evt) {+ stats.eventCount += 1;+ stats.lastEventAt = Date.now();+ yield evt;+ }}} finally {reader.releaseLock();