Skip to content

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

Rust

keep codex streams alive while buffering tools

Raine Virta66187eef7852
diff --git a/src/providers/codex/translate/reducer.ts b/src/providers/codex/translate/reducer.ts
index 0fcd366..7773aa1 100644
--- a/src/providers/codex/translate/reducer.ts
+++ b/src/providers/codex/translate/reducer.ts
@@ -28,6 +28,7 @@ export type ReducerEvent =
| { kind: "text-stop"; index: number }
| { kind: "tool-start"; index: number; id: string; name: string }
| { kind: "tool-delta"; index: number; partialJson: string }
+ | { kind: "tool-progress"; index: number }
| { kind: "tool-stop"; index: number }
| { kind: "finish"; stopReason: StopReason; usage: CodexUsage | undefined };
@@ -189,6 +190,8 @@ export async function* reduceUpstream(
if (!state.bufferUntilDone) {
state.emittedArgs = true;
yield { kind: "tool-delta", index: state.index, partialJson: delta };
+ } else {
+ yield { kind: "tool-progress", index: state.index };
}
continue;
}
diff --git a/src/providers/codex/translate/stream.test.ts b/src/providers/codex/translate/stream.test.ts
new file mode 100644
index 0000000..a8cf2cb
--- /dev/null
+++ b/src/providers/codex/translate/stream.test.ts
@@ -0,0 +1,105 @@
+import { describe, expect, it } from "bun:test";
+import { translateStream } from "./stream.ts";
+
+const silentLog = {
+ debug: () => {},
+ info: () => {},
+ warn: () => {},
+ error: () => {},
+ child: () => silentLog,
+};
+
+function sse(type: string, payload: Record<string, unknown>): string {
+ return `data: ${JSON.stringify({ type, ...payload })}\n\n`;
+}
+
+function upstreamFromChunks(chunks: string[], advanceMs = 0): ReadableStream<Uint8Array> {
+ const encoder = new TextEncoder();
+ let index = 0;
+ return new ReadableStream<Uint8Array>({
+ pull(controller) {
+ if (index >= chunks.length) {
+ controller.close();
+ return;
+ }
+ now += advanceMs;
+ controller.enqueue(encoder.encode(chunks[index++]));
+ },
+ });
+}
+
+function abortingUpstream(err: Error): ReadableStream<Uint8Array> {
+ return new ReadableStream<Uint8Array>({
+ pull(controller) {
+ controller.error(err);
+ },
+ });
+}
+
+async function collect(stream: ReadableStream<Uint8Array>): Promise<string> {
+ const reader = stream.getReader();
+ const decoder = new TextDecoder();
+ let out = "";
+ while (true) {
+ const { done, value } = await reader.read();
+ if (done) return out;
+ out += decoder.decode(value, { stream: true });
+ }
+}
+
+let now = 0;
+const realNow = Date.now;
+
+describe("translateStream", () => {
+ it("emits keepalive pings while Read arguments are buffered", async () => {
+ Date.now = () => now;
+ try {
+ const chunks = [
+ sse("response.output_item.added", {
+ output_index: 0,
+ item: { type: "function_call", call_id: "call_read", name: "Read" },
+ }),
+ sse("response.function_call_arguments.delta", { output_index: 0, delta: "{\"file_path\"" }),
+ sse("response.function_call_arguments.delta", { output_index: 0, delta: ":\"/tmp/a\"}" }),
+ sse("response.output_item.done", {
+ output_index: 0,
+ item: { type: "function_call", arguments: "{\"file_path\":\"/tmp/a\"}" },
+ }),
+ sse("response.completed", { response: { usage: {} } }),
+ ];
+
+ const output = await collect(
+ translateStream(upstreamFromChunks(chunks, 16_000), {
+ messageId: "msg_1",
+ model: "gpt-5.5",
+ log: silentLog,
+ }),
+ );
+
+ expect(output).toContain("event: content_block_start");
+ expect(output).toContain("event: content_block_delta");
+ expect(output.match(/event: ping/g)?.length).toBeGreaterThanOrEqual(2);
+ expect(output).toContain("event: message_stop");
+ } finally {
+ Date.now = realNow;
+ now = 0;
+ }
+ });
+
+ it("treats aborted upstream reads as cancellation", async () => {
+ const abort = new AbortController();
+ abort.abort();
+ const err = new DOMException("The connection was closed.", "AbortError");
+
+ const output = await collect(
+ translateStream(abortingUpstream(err), {
+ messageId: "msg_1",
+ model: "gpt-5.5",
+ log: silentLog,
+ signal: abort.signal,
+ }),
+ );
+
+ expect(output).not.toContain("event: error");
+ });
+});
diff --git a/src/providers/codex/translate/stream.ts b/src/providers/codex/translate/stream.ts
index 7760f42..e6c18d6 100644
--- a/src/providers/codex/translate/stream.ts
+++ b/src/providers/codex/translate/stream.ts
@@ -18,6 +18,8 @@ import {
* upstream event is consumed, so rate-limit and upstream-failed cases
* surface as SSE error events rather than non-200 statuses.
*/
+const KEEPALIVE_INTERVAL_MS = 15_000;
+
export function translateStream(
upstream: ReadableStream<Uint8Array>,
opts: {
@@ -36,12 +38,38 @@ export function translateStream(
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 }>();
const diagnostics = createUpstreamStreamDiagnostics();
+ let closed = false;
let messageStarted = false;
+ let lastEmitAt = 0;
+ const safeClose = () => {
+ if (closed) return;
+ closed = true;
+ try {
+ controller.close();
+ } catch {
+ // The stream can be closed by cancellation before the producer observes it.
+ }
+ };
+ const emit = (event: string, data: unknown) => {
+ if (closed || opts.signal?.aborted || controller.desiredSize === null) return false;
+ try {
+ controller.enqueue(encoder.encode(encodeSseEvent(event, data)));
+ lastEmitAt = Date.now();
+ return true;
+ } catch (err) {
+ if (opts.signal?.aborted || isClosedControllerError(err)) {
+ closed = true;
+ return false;
+ }
+ throw err;
+ }
+ };
+ const emitPingIfStale = () => {
+ if (!messageStarted || Date.now() - lastEmitAt < KEEPALIVE_INTERVAL_MS) return;
+ emit("ping", { type: "ping" });
+ };
const ensureMessageStart = () => {
if (messageStarted) return;
messageStarted = true;
@@ -108,6 +136,9 @@ export function translateStream(
delta: { type: "input_json_delta", partial_json: e.partialJson },
});
break;
+ case "tool-progress":
+ emitPingIfStale();
+ break;
case "tool-stop":
activeTools.delete(e.index);
emit("content_block_stop", { type: "content_block_stop", index: e.index });
@@ -127,6 +158,15 @@ export function translateStream(
} catch (err) {
const activeToolNames = Array.from(activeTools.values(), (tool) => tool.name);
const activeToolCalls = Array.from(activeTools.values());
+ if (opts.signal?.aborted) {
+ opts.log.info("stream cancelled", {
+ err: describeError(err),
+ activeToolNames,
+ activeToolCalls,
+ diagnostics: describeDiagnostics(diagnostics),
+ });
+ return;
+ }
if (err instanceof UpstreamStreamError) {
const detail = {
kind: err.kind,
@@ -165,12 +205,16 @@ export function translateStream(
});
}
} finally {
- controller.close();
+ safeClose();
}
},
});
}
+function isClosedControllerError(err: unknown): boolean {
+ return err instanceof TypeError && err.message.includes("Controller is already closed");
+}
+
function describeDiagnostics(diagnostics: ReturnType<typeof createUpstreamStreamDiagnostics>) {
const now = Date.now();
return {