Skip to content

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

Rust

share non-stream accumulation helpers

Raine Virtab3800d9d1ff2
diff --git a/src/providers/codex/translate/accumulate.ts b/src/providers/codex/translate/accumulate.ts
index 92f824f..bad7635 100644
--- a/src/providers/codex/translate/accumulate.ts
+++ b/src/providers/codex/translate/accumulate.ts
@@ -3,6 +3,7 @@ import type { CodexUsage, ReducerEvent } from "./reducer.ts";
import type { Logger } from "../../../log.ts";
import type { TrafficCapture } from "../../types.ts";
import { attachTrafficCapture, createUpstreamStreamDiagnostics } from "./reducer.ts";
+import { createBlockAccumulator, parseToolInputJsonOrRaw } from "../../translate/accumulate.ts";
export { UpstreamStreamError } from "./reducer.ts";
@@ -45,12 +46,7 @@ export async function accumulateResponse(
upstream: ReadableStream<Uint8Array>,
opts: { messageId: string; model: string; log: Logger; traffic?: TrafficCapture },
): Promise<AccumulatedResponse> {
- type Block =
- | { kind: "text"; text: string }
- | { kind: "tool"; id: string; name: string; args: string };
-
- const ordered: number[] = [];
- const blocks = new Map<number, Block>();
+ const blockAccumulator = createBlockAccumulator();
let stopReason: AnthropicNonStreamResponse["stop_reason"] = null;
let usage: ReturnType<typeof mapUsageToAnthropic> | undefined;
let rawUsage: CodexUsage | undefined;
@@ -64,21 +60,17 @@ export async function accumulateResponse(
for await (const e of reduceUpstream(upstream, opts.log, diagnostics)) {
switch (e.kind) {
case "text-start":
- blocks.set(e.index, { kind: "text", text: "" });
- ordered.push(e.index);
+ blockAccumulator.onTextStart(e.index);
break;
case "text-delta": {
- const b = blocks.get(e.index);
- if (b?.kind === "text") b.text += e.text;
+ blockAccumulator.onTextDelta(e.index, e.text);
break;
}
case "tool-start":
- blocks.set(e.index, { kind: "tool", id: e.id, name: e.name, args: "" });
- ordered.push(e.index);
+ blockAccumulator.onToolStart(e.index, e.id, e.name);
break;
case "tool-delta": {
- const b = blocks.get(e.index);
- if (b?.kind === "tool") b.args += e.partialJson;
+ blockAccumulator.onToolDelta(e.index, e.partialJson);
break;
}
case "text-stop":
@@ -97,19 +89,16 @@ export async function accumulateResponse(
}
const content: AnthropicNonStreamResponse["content"] = [];
- for (const i of ordered) {
- const b = blocks.get(i);
- if (!b) continue;
+ for (const b of blockAccumulator.orderedBlocks()) {
if (b.kind === "text") {
if (b.text) content.push({ type: "text", text: b.text });
- } else {
- let input: unknown = {};
- try {
- input = b.args ? JSON.parse(b.args) : {};
- } catch {
- input = { _raw: b.args };
- }
- content.push({ type: "tool_use", id: b.id, name: b.name, input });
+ } else if (b.kind === "tool") {
+ content.push({
+ type: "tool_use",
+ id: b.id,
+ name: b.name,
+ input: parseToolInputJsonOrRaw(b.args),
+ });
}
}
diff --git a/src/providers/kimi/translate/accumulate.ts b/src/providers/kimi/translate/accumulate.ts
index 8708470..d064ed2 100644
--- a/src/providers/kimi/translate/accumulate.ts
+++ b/src/providers/kimi/translate/accumulate.ts
@@ -1,6 +1,7 @@
import { mapUsageToAnthropic, reduceUpstream, type KimiUsage } from "./reducer.ts";
import { makeThinkingSignature } from "./signature.ts";
import type { TrafficCapture } from "../../types.ts";
+import { createBlockAccumulator, parseToolInputJsonOrRaw } from "../../translate/accumulate.ts";
export { UpstreamStreamError } from "./reducer.ts";
@@ -35,13 +36,7 @@ export async function accumulateResponse(
upstream: ReadableStream<Uint8Array>,
opts: { messageId: string; model: string; log: Logger; traffic?: TrafficCapture },
): Promise<AccumulatedResponse> {
- type Block =
- | { kind: "thinking"; text: string }
- | { kind: "text"; text: string }
- | { kind: "tool"; id: string; name: string; args: string };
-
- const ordered: number[] = [];
- const blocks = new Map<number, Block>();
+ const blockAccumulator = createBlockAccumulator({ includeThinking: true });
let stopReason: AnthropicNonStreamResponse["stop_reason"] = null;
let usage: ReturnType<typeof mapUsageToAnthropic> | undefined;
let rawUsage: KimiUsage | undefined;
@@ -53,37 +48,29 @@ export async function accumulateResponse(
for await (const e of reduceUpstream(upstream, stats, opts.log)) {
switch (e.kind) {
case "thinking-start":
- blocks.set(e.index, { kind: "thinking", text: "" });
- ordered.push(e.index);
+ blockAccumulator.onThinkingStart(e.index);
break;
case "thinking-delta": {
- const b = blocks.get(e.index);
- if (b?.kind === "thinking") {
- b.text += e.text;
+ if (blockAccumulator.onThinkingDelta(e.index, e.text)) {
reasoningChars += e.text.length;
}
break;
}
case "text-start":
- blocks.set(e.index, { kind: "text", text: "" });
- ordered.push(e.index);
+ blockAccumulator.onTextStart(e.index);
break;
case "text-delta": {
- const b = blocks.get(e.index);
- if (b?.kind === "text") {
- b.text += e.text;
+ if (blockAccumulator.onTextDelta(e.index, 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);
+ blockAccumulator.onToolStart(e.index, e.id, e.name);
break;
case "tool-delta": {
- const b = blocks.get(e.index);
- if (b?.kind === "tool") b.args += e.partialJson;
+ blockAccumulator.onToolDelta(e.index, e.partialJson);
break;
}
case "thinking-stop":
@@ -99,26 +86,23 @@ export async function accumulateResponse(
}
const content: AnthropicNonStreamResponse["content"] = [];
- for (const i of ordered) {
- const b = blocks.get(i);
- if (!b) continue;
+ for (const b of blockAccumulator.orderedBlocks()) {
if (b.kind === "thinking") {
if (b.text)
content.push({
type: "thinking",
thinking: b.text,
- signature: makeThinkingSignature(opts.messageId, i),
+ signature: makeThinkingSignature(opts.messageId, b.index),
});
} else if (b.kind === "text") {
if (b.text) content.push({ type: "text", text: b.text });
} else {
- let input: unknown = {};
- try {
- input = b.args ? JSON.parse(b.args) : {};
- } catch {
- input = { _raw: b.args };
- }
- content.push({ type: "tool_use", id: b.id, name: b.name, input });
+ content.push({
+ type: "tool_use",
+ id: b.id,
+ name: b.name,
+ input: parseToolInputJsonOrRaw(b.args),
+ });
}
}
diff --git a/src/providers/translate/accumulate.ts b/src/providers/translate/accumulate.ts
new file mode 100644
index 0000000..f5ba34e
--- /dev/null
+++ b/src/providers/translate/accumulate.ts
@@ -0,0 +1,94 @@
+export type AccumulatedTextBlock = {
+ kind: "text";
+ index: number;
+ text: string;
+};
+
+export type AccumulatedThinkingBlock = {
+ kind: "thinking";
+ index: number;
+ text: string;
+};
+
+export type AccumulatedToolBlock = {
+ kind: "tool";
+ index: number;
+ id: string;
+ name: string;
+ args: string;
+};
+
+export type AccumulatedBlock =
+ | AccumulatedTextBlock
+ | AccumulatedThinkingBlock
+ | AccumulatedToolBlock;
+
+export interface BlockAccumulator {
+ onTextStart(index: number): void;
+ onTextDelta(index: number, text: string): boolean;
+ onThinkingStart(index: number): void;
+ onThinkingDelta(index: number, text: string): boolean;
+ onToolStart(index: number, id: string, name: string): void;
+ onToolDelta(index: number, partialJson: string): boolean;
+ orderedBlocks(): readonly AccumulatedBlock[];
+}
+
+export function createBlockAccumulator(options?: { includeThinking?: boolean }): BlockAccumulator {
+ const ordered: number[] = [];
+ const blocks = new Map<number, AccumulatedBlock>();
+ const includeThinking = options?.includeThinking === true;
+
+ return {
+ onTextStart(index: number): void {
+ blocks.set(index, { kind: "text", index, text: "" });
+ ordered.push(index);
+ },
+ onTextDelta(index: number, text: string): boolean {
+ const block = blocks.get(index);
+ if (block?.kind === "text") {
+ block.text += text;
+ return true;
+ }
+ return false;
+ },
+ onThinkingStart(index: number): void {
+ if (!includeThinking) return;
+ blocks.set(index, { kind: "thinking", index, text: "" });
+ ordered.push(index);
+ },
+ onThinkingDelta(index: number, text: string): boolean {
+ const block = blocks.get(index);
+ if (block?.kind === "thinking") {
+ block.text += text;
+ return true;
+ }
+ return false;
+ },
+ onToolStart(index: number, id: string, name: string): void {
+ blocks.set(index, { kind: "tool", index, id, name, args: "" });
+ ordered.push(index);
+ },
+ onToolDelta(index: number, partialJson: string): boolean {
+ const block = blocks.get(index);
+ if (block?.kind === "tool") {
+ block.args += partialJson;
+ return true;
+ }
+ return false;
+ },
+ orderedBlocks(): readonly AccumulatedBlock[] {
+ return ordered.flatMap((index) => {
+ const block = blocks.get(index);
+ return block ? [block] : [];
+ });
+ },
+ };
+}
+
+export function parseToolInputJsonOrRaw(args: string): unknown {
+ try {
+ return args ? JSON.parse(args) : {};
+ } catch {
+ return { _raw: args };
+ }
+}