Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy
Rust
translate: unify stream and accumulate on a shared reducer
Raine Virta24469f651da2
diff --git a/src/translate/accumulate.ts b/src/translate/accumulate.tsindex cbf2c8b..9e1ae95 100644--- a/src/translate/accumulate.ts+++ b/src/translate/accumulate.ts@@ -1,15 +1,6 @@-import { parseSseStream } from "./sse.ts"+import { mapUsageToAnthropic, reduceUpstream } from "./reducer.ts"-export class UpstreamStreamError extends Error {- constructor(- public kind: "rate_limit" | "failed",- message: string,- public retryAfterSeconds?: number,- ) {- super(message)- this.name = "UpstreamStreamError"- }-}+export { UpstreamStreamError } from "./reducer.ts"export interface AnthropicNonStreamResponse {id: string@@ -31,101 +22,56 @@ export interface AnthropicNonStreamResponse {}/**- * Drive the Codex SSE stream to completion and produce a single Anthropic- * non-streaming response object. Used when the client asked for stream:false.+ * Drive the Codex SSE stream to completion through the shared reducer+ * and fold the ReducerEvents into a single Anthropic non-streaming+ * response object. Throws UpstreamStreamError on rate_limit or failed+ * upstream; server translates to a proper HTTP status.*/export async function accumulateResponse(upstream: ReadableStream<Uint8Array>,opts: { messageId: string; model: string },): Promise<AnthropicNonStreamResponse> {- const blocks = new Map<- number,+ type Block =| { kind: "text"; text: string }| { kind: "tool"; id: string; name: string; args: string }- >()- const itemToOutputIndex = new Map<string, number>()- let sawToolUse = false- let incomplete = false- let usage: any- const orderedIndices: number[] = []- const ensureOrder = (i: number) => {- if (!orderedIndices.includes(i)) orderedIndices.push(i)- }+ const ordered: number[] = []+ const blocks = new Map<number, Block>()+ let stopReason: AnthropicNonStreamResponse["stop_reason"] = null+ let usage: ReturnType<typeof mapUsageToAnthropic> | undefined- for await (const evt of parseSseStream(upstream)) {- if (!evt.data) continue- let p: any- try {- p = JSON.parse(evt.data)- } catch {- continue- }- const t = p.type || evt.event || ""- if (t === "codex.rate_limits") {- if (p.rate_limits?.limit_reached) {- throw new UpstreamStreamError(- "rate_limit",- "rate limit reached",- p.rate_limits?.primary?.reset_after_seconds,- )- }- continue- }- if (t === "response.failed" || t === "response.error" || t === "error") {- const message = p?.response?.error?.message || p?.error?.message || "Upstream error"- throw new UpstreamStreamError("failed", message)- }- if (t === "response.output_item.added") {- const item = p.item- const oi = p.output_index- if (!item) continue- if (item.type === "message") {- blocks.set(oi, { kind: "text", text: "" })- ensureOrder(oi)- if (item.id) itemToOutputIndex.set(item.id, oi)- } else if (item.type === "function_call") {- sawToolUse = true- blocks.set(oi, { kind: "tool", id: item.call_id, name: item.name, args: "" })- ensureOrder(oi)+ for await (const e of reduceUpstream(upstream)) {+ switch (e.kind) {+ 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}- continue- }- if (t === "response.output_text.delta") {- let oi = p.output_index- if (typeof oi !== "number" && p.item_id) oi = itemToOutputIndex.get(p.item_id)- const b = typeof oi === "number" ? blocks.get(oi) : undefined- if (b && b.kind === "text") b.text += p.delta ?? ""- continue- }- if (t === "response.function_call_arguments.delta") {- const b = blocks.get(p.output_index)- if (b && b.kind === "tool") b.args += p.delta ?? ""- continue- }- if (t === "response.function_call_arguments.done") {- const b = blocks.get(p.output_index)- if (b && b.kind === "tool" && !b.args && typeof p.arguments === "string") b.args = p.arguments- continue- }- if (t === "response.output_item.done") {- const item = p.item- const b = blocks.get(p.output_index)- if (b && b.kind === "tool" && !b.args && typeof item?.arguments === "string") b.args = item.arguments- continue- }- if (t === "response.completed" || t === "response.incomplete") {- usage = p.response?.usage- const reason = p.response?.incomplete_details?.reason- if (t === "response.incomplete" || reason === "max_output_tokens" || p.response?.status === "incomplete") {- incomplete = true+ 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}- continue+ case "text-stop":+ case "tool-stop":+ break+ case "finish":+ stopReason = e.stopReason+ usage = mapUsageToAnthropic(e.usage)+ break}}const content: AnthropicNonStreamResponse["content"] = []- for (const i of orderedIndices) {+ for (const i of ordered) {const b = blocks.get(i)if (!b) continueif (b.kind === "text") {@@ -147,13 +93,13 @@ export async function accumulateResponse(role: "assistant",model: opts.model,content,- stop_reason: incomplete ? "max_tokens" : sawToolUse ? "tool_use" : "end_turn",+ stop_reason: stopReason,stop_sequence: null,- usage: {- input_tokens: usage?.input_tokens ?? 0,- output_tokens: usage?.output_tokens ?? 0,+ usage: usage ?? {+ input_tokens: 0,+ output_tokens: 0,cache_creation_input_tokens: 0,- cache_read_input_tokens: usage?.input_tokens_details?.cached_tokens ?? 0,+ cache_read_input_tokens: 0,},}}diff --git a/src/translate/reducer.ts b/src/translate/reducer.tsnew file mode 100644index 0000000..78a32c8--- /dev/null+++ b/src/translate/reducer.ts@@ -0,0 +1,212 @@+import { parseSseStream } from "./sse.ts"++export class UpstreamStreamError extends Error {+ constructor(+ public kind: "rate_limit" | "failed",+ message: string,+ public retryAfterSeconds?: number,+ ) {+ super(message)+ this.name = "UpstreamStreamError"+ }+}++export interface CodexUsage {+ input_tokens?: number+ output_tokens?: number+ input_tokens_details?: { cached_tokens?: number }+ output_tokens_details?: { reasoning_tokens?: number }+}++export type StopReason = "end_turn" | "tool_use" | "max_tokens"++export type ReducerEvent =+ | { 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: CodexUsage | undefined }++interface TextState {+ kind: "text"+ index: number+}+interface ToolState {+ kind: "tool"+ index: number+ callId: string+ name: string+ argsAccum: string+ hadDelta: boolean+}+type BlockState = TextState | ToolState++/**+ * Single source of truth for translating Codex Responses SSE into a+ * stream of typed, downstream-agnostic ReducerEvents. Both the streaming+ * and non-streaming frontends consume this generator.+ *+ * Throws UpstreamStreamError on codex.rate_limits.limit_reached or+ * response.failed/response.error. Any usage that arrived before the+ * failure is discarded.+ */+export async function* reduceUpstream(+ upstream: ReadableStream<Uint8Array>,+): AsyncGenerator<ReducerEvent> {+ const blocksByOutputIndex = new Map<number, BlockState>()+ const itemIdToOutputIndex = new Map<string, number>()+ let anthropicIndex = 0+ let sawToolUse = false+ let finalUsage: CodexUsage | undefined+ let incomplete = false++ for await (const evt of parseSseStream(upstream)) {+ if (!evt.data) continue+ let p: any+ try {+ p = JSON.parse(evt.data)+ } catch {+ continue+ }+ const t: string = p.type || evt.event || ""++ if (t === "codex.rate_limits") {+ if (p.rate_limits?.limit_reached) {+ throw new UpstreamStreamError(+ "rate_limit",+ "rate limit reached",+ p.rate_limits?.primary?.reset_after_seconds,+ )+ }+ continue+ }+ if (t === "response.failed" || t === "response.error" || t === "error") {+ const message = p?.response?.error?.message || p?.error?.message || "Upstream error"+ throw new UpstreamStreamError("failed", message)+ }++ if (t === "response.output_item.added") {+ const item = p.item+ const outputIndex: number = p.output_index+ if (!item) continue+ if (item.type === "reasoning") continue+ if (item.type === "message") {+ const idx = anthropicIndex+++ blocksByOutputIndex.set(outputIndex, { kind: "text", index: idx })+ if (item.id) itemIdToOutputIndex.set(item.id, outputIndex)+ yield { kind: "text-start", index: idx }+ continue+ }+ if (item.type === "function_call") {+ sawToolUse = true+ const idx = anthropicIndex+++ blocksByOutputIndex.set(outputIndex, {+ kind: "tool",+ index: idx,+ callId: item.call_id,+ name: item.name,+ argsAccum: "",+ hadDelta: false,+ })+ yield { kind: "tool-start", index: idx, id: item.call_id, name: item.name }+ continue+ }+ continue+ }++ if (t === "response.output_text.delta") {+ const outputIndex: number | undefined = p.output_index+ const itemId: string | undefined = p.item_id+ let state: BlockState | undefined+ if (typeof outputIndex === "number") state = blocksByOutputIndex.get(outputIndex)+ if (!state && itemId) {+ const mapped = itemIdToOutputIndex.get(itemId)+ if (mapped !== undefined) state = blocksByOutputIndex.get(mapped)+ }+ if (!state || state.kind !== "text") continue+ const delta: string = p.delta ?? ""+ if (!delta) continue+ yield { kind: "text-delta", index: state.index, text: delta }+ continue+ }++ if (t === "response.function_call_arguments.delta") {+ const state = blocksByOutputIndex.get(p.output_index)+ if (!state || state.kind !== "tool") continue+ const delta: string = p.delta ?? ""+ if (!delta) continue+ state.argsAccum += delta+ state.hadDelta = true+ yield { kind: "tool-delta", index: state.index, partialJson: delta }+ continue+ }++ if (t === "response.function_call_arguments.done") {+ const state = blocksByOutputIndex.get(p.output_index)+ if (!state || state.kind !== "tool") continue+ if (typeof p.arguments === "string" && !state.argsAccum) {+ state.argsAccum = p.arguments+ }+ continue+ }++ if (t === "response.output_item.done") {+ const item = p.item+ const state = blocksByOutputIndex.get(p.output_index)+ if (!state) continue+ if (!item) {+ // defensive+ if (state.kind === "text") yield { kind: "text-stop", index: state.index }+ else yield { kind: "tool-stop", index: state.index }+ blocksByOutputIndex.delete(p.output_index)+ continue+ }+ if (item.type === "reasoning") continue+ if (state.kind === "tool" && !state.hadDelta) {+ const finalArgs =+ (typeof item.arguments === "string" && item.arguments.length+ ? item.arguments+ : state.argsAccum) || ""+ if (finalArgs.length) {+ yield { kind: "tool-delta", index: state.index, partialJson: finalArgs }+ }+ }+ if (state.kind === "text") yield { kind: "text-stop", index: state.index }+ else yield { kind: "tool-stop", index: state.index }+ blocksByOutputIndex.delete(p.output_index)+ continue+ }++ if (t === "response.completed" || t === "response.incomplete") {+ finalUsage = p.response?.usage+ const reason = p.response?.incomplete_details?.reason+ if (+ t === "response.incomplete" ||+ reason === "max_output_tokens" ||+ p.response?.status === "incomplete"+ ) {+ incomplete = true+ }+ continue+ }+ }++ const stopReason: StopReason = incomplete ? "max_tokens" : sawToolUse ? "tool_use" : "end_turn"+ yield { kind: "finish", stopReason, usage: finalUsage }+}++export function mapUsageToAnthropic(u: CodexUsage | undefined): {+ input_tokens: number+ output_tokens: number+ cache_creation_input_tokens: number+ cache_read_input_tokens: number+} {+ return {+ input_tokens: u?.input_tokens ?? 0,+ output_tokens: u?.output_tokens ?? 0,+ cache_creation_input_tokens: 0,+ cache_read_input_tokens: u?.input_tokens_details?.cached_tokens ?? 0,+ }+}diff --git a/src/translate/stream.ts b/src/translate/stream.tsindex 4e40375..a551282 100644--- a/src/translate/stream.ts+++ b/src/translate/stream.ts@@ -1,42 +1,16 @@-import { encodeSseEvent, parseSseStream } from "./sse.ts"+import { encodeSseEvent } from "./sse.ts"import { createLogger } from "../log.ts"+import { mapUsageToAnthropic, reduceUpstream, UpstreamStreamError } from "./reducer.ts"const log = createLogger("translate.stream")-interface CodexUsage {- input_tokens?: number- output_tokens?: number- input_tokens_details?: { cached_tokens?: number }- output_tokens_details?: { reasoning_tokens?: number }-}--interface TextBlockState {- kind: "text"- index: number-}--interface ToolBlockState {- kind: "tool"- index: number- itemId: string- callId: string- name: string- argsAccum: string- hadDelta: boolean-}--type BlockState = TextBlockState | ToolBlockState--export interface StreamTranslateOptions {- messageId: string- model: string- // If provided, writes translated events here; otherwise returns a ReadableStream- onEvent?: (event: string, data: unknown) => void-}-/*** Translate a Codex Responses SSE stream into Anthropic SSE events.- * Returns a ReadableStream<Uint8Array> of Anthropic SSE ready to pipe to client.+ * Returns a ReadableStream<Uint8Array> ready to pipe to the client.+ *+ * The HTTP status has already been flushed (200) before the first+ * upstream event is consumed, so rate-limit and upstream-failed cases+ * surface as SSE error events rather than non-200 statuses.*/export function translateStream(upstream: ReadableStream<Uint8Array>,@@ -48,262 +22,108 @@ export function translateStream(const emit = (event: string, data: unknown) => {controller.enqueue(encoder.encode(encodeSseEvent(event, data)))}- try {- await runStream(upstream, opts, emit)- } catch (err) {- log.error("stream translation error", { err: String(err) })- emit("error", { type: "error", error: { type: "api_error", message: String(err) } })- } finally {- controller.close()- }- },- })-}--async function runStream(- upstream: ReadableStream<Uint8Array>,- opts: { messageId: string; model: string },- emit: (event: string, data: unknown) => void,-): Promise<void> {- let messageStarted = false- let anthropicIndex = 0- // Keyed by output_index- const blocksByIndex = new Map<number, BlockState>()- // Map item_id → output_index for text deltas- const itemToOutputIndex = new Map<string, number>()- let sawToolUse = false- let finalUsage: CodexUsage | undefined- let incomplete = false- let rateLimitReached = false- let rateLimitResetSeconds: number | undefined-- 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" })- }-- for await (const evt of parseSseStream(upstream)) {- if (!evt.data) continue- let payload: any- try {- payload = JSON.parse(evt.data)- } catch {- log.warn("invalid json in sse data", { data: evt.data.slice(0, 200) })- continue- }- const type: string = payload.type || evt.event || ""-- if (type === "codex.rate_limits") {- if (payload.rate_limits?.limit_reached) {- rateLimitReached = true- rateLimitResetSeconds = payload.rate_limits?.primary?.reset_after_seconds- }- continue- }-- if (type === "response.created" || type === "response.in_progress") {- ensureMessageStart()- continue- }-- if (type === "response.output_item.added") {- const item = payload.item- const outputIndex: number = payload.output_index- if (!item) continue- if (item.type === "reasoning") {- // drop- continue- }- if (item.type === "message") {- ensureMessageStart()- const idx = anthropicIndex++- const state: TextBlockState = { kind: "text", index: idx }- blocksByIndex.set(outputIndex, state)- if (item.id) itemToOutputIndex.set(item.id, outputIndex)- emit("content_block_start", {- type: "content_block_start",- index: idx,- content_block: { type: "text", text: "" },- })- continue- }- if (item.type === "function_call") {- ensureMessageStart()- sawToolUse = true- const idx = anthropicIndex++- const state: ToolBlockState = {- kind: "tool",- index: idx,- itemId: item.id,- callId: item.call_id,- name: item.name,- argsAccum: "",- hadDelta: false,- }- blocksByIndex.set(outputIndex, state)- emit("content_block_start", {- type: "content_block_start",- index: idx,- content_block: {- type: "tool_use",- id: item.call_id,- name: item.name,- input: {},+ 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,+ },},})- continue+ emit("ping", { type: "ping" })}- continue- }- if (type === "response.output_text.delta") {- const outputIndex: number | undefined = payload.output_index- const itemId: string | undefined = payload.item_id- let state: BlockState | undefined- if (typeof outputIndex === "number") state = blocksByIndex.get(outputIndex)- if (!state && itemId) {- const mapped = itemToOutputIndex.get(itemId)- if (mapped !== undefined) state = blocksByIndex.get(mapped)- }- if (!state || state.kind !== "text") continue- const delta: string = payload.delta ?? ""- if (!delta) continue- emit("content_block_delta", {- type: "content_block_delta",- index: state.index,- delta: { type: "text_delta", text: delta },- })- continue- }-- if (type === "response.function_call_arguments.delta") {- const outputIndex: number = payload.output_index- const state = blocksByIndex.get(outputIndex)- if (!state || state.kind !== "tool") continue- const delta: string = payload.delta ?? ""- if (!delta) continue- state.argsAccum += delta- state.hadDelta = true- emit("content_block_delta", {- type: "content_block_delta",- index: state.index,- delta: { type: "input_json_delta", partial_json: delta },- })- continue- }-- if (type === "response.function_call_arguments.done") {- // Full args available; nothing to emit here (deltas already sent, or- // we flush on output_item.done if none were sent).- const outputIndex: number = payload.output_index- const state = blocksByIndex.get(outputIndex)- if (!state || state.kind !== "tool") continue- if (typeof payload.arguments === "string" && !state.argsAccum) {- state.argsAccum = payload.arguments- }- continue- }-- if (type === "response.output_item.done") {- const item = payload.item- const outputIndex: number = payload.output_index- if (!item) continue- if (item.type === "reasoning") continue- const state = blocksByIndex.get(outputIndex)- if (!state) continue- if (state.kind === "tool") {- if (!state.hadDelta) {- const finalArgs =- (typeof item.arguments === "string" && item.arguments.length- ? item.arguments- : state.argsAccum) || ""- if (finalArgs.length) {- emit("content_block_delta", {- type: "content_block_delta",- index: state.index,- delta: { type: "input_json_delta", partial_json: finalArgs },- })+ try {+ for await (const e of reduceUpstream(upstream)) {+ switch (e.kind) {+ 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":+ 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":+ emit("content_block_stop", { type: "content_block_stop", index: e.index })+ break+ case "finish":+ ensureMessageStart()+ 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) {+ if (err instanceof UpstreamStreamError) {+ log.warn("upstream stream error", { kind: err.kind, message: err.message })+ 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", { err: String(err) })+ ensureMessageStart()+ emit("error", {+ type: "error",+ error: { type: "api_error", message: String(err) },+ })+ }+ } finally {+ controller.close()}- emit("content_block_stop", { type: "content_block_stop", index: state.index })- blocksByIndex.delete(outputIndex)- continue- }-- if (type === "response.completed" || type === "response.incomplete") {- finalUsage = payload.response?.usage- const reason = payload.response?.incomplete_details?.reason- if (type === "response.incomplete" || reason === "max_output_tokens" || payload.response?.status === "incomplete") {- incomplete = true- }- continue- }-- if (type === "response.failed" || type === "response.error" || type === "error") {- const message = payload?.response?.error?.message || payload?.error?.message || "Upstream error"- emit("error", { type: "error", error: { type: "api_error", message } })- return- }- }-- if (!messageStarted) {- // Upstream produced nothing — still emit a minimal envelope so Claude Code doesn't hang.- ensureMessageStart()- }-- if (rateLimitReached) {- emit("error", {- type: "error",- error: {- type: "rate_limit_error",- message: `rate limit reached; retry after ${rateLimitResetSeconds ?? "?"}s`,- },- })- return- }-- const usage = mapUsage(finalUsage)- emit("message_delta", {- type: "message_delta",- delta: {- stop_reason: incomplete ? "max_tokens" : sawToolUse ? "tool_use" : "end_turn",- stop_sequence: null,},- usage,})- emit("message_stop", { type: "message_stop" })-}--function mapUsage(u: CodexUsage | undefined): {- input_tokens: number- output_tokens: number- cache_creation_input_tokens: number- cache_read_input_tokens: number-} {- return {- input_tokens: u?.input_tokens ?? 0,- output_tokens: u?.output_tokens ?? 0,- cache_creation_input_tokens: 0,- cache_read_input_tokens: u?.input_tokens_details?.cached_tokens ?? 0,- }}