Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions open-sse/translator/helpers/toolCallShim.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Defensive shims for tool calls whose strict-schema fields can be malformed
// by upstream models (e.g. MiMo emitting empty objects/strings instead of
// arrays for Capy's submit_pr_review).
//
// Applied on the assembled OpenAI tool-call arguments after streaming, just
// before they are re-emitted as a single Claude input_json_delta.
//
// To add a new shim: register a (input) => input transformer in TOOL_SHIMS
// keyed by the tool name. The transformer must accept arbitrary input and
// return a JSON-safe value.

type ShimFn = (input: unknown) => unknown;

function coerceToArray(v: unknown): unknown[] {
if (Array.isArray(v)) return v;
if (v == null) return [];
if (typeof v === "string") {
if (v === "") return [];
try {
const parsed = JSON.parse(v);
return Array.isArray(parsed) ? parsed : [];
} catch {
return [];
}
}
// Plain object or other non-array → empty
return [];
}

const TOOL_SHIMS: Record<string, ShimFn> = {
submit_pr_review: (input) => {
if (typeof input !== "object" || input === null || Array.isArray(input)) return input;
const patched = { ...(input as Record<string, unknown>) };
for (const key of ["functionalChanges", "findings"]) {
patched[key] = coerceToArray(patched[key]);
}
return patched;
},
};

export function hasToolCallShim(name: string | undefined | null): boolean {
return typeof name === "string" && Object.prototype.hasOwnProperty.call(TOOL_SHIMS, name);
}

/**
* Apply the registered shim for a tool call's raw assembled arguments string.
* Returns a stringified JSON value safe to emit as input_json_delta.partial_json.
* If the buffer is unparseable, returns the empty-object JSON `{}` after applying
* the shim with `{}` as input (so required arrays still get injected).
*/
export function applyToolCallShimToBuffer(name: string, raw: string): string {
const shim = TOOL_SHIMS[name];
if (!shim) return raw;

let parsed: unknown;
try {
parsed = raw && raw.length > 0 ? JSON.parse(raw) : {};
} catch {
parsed = {};
}

const patched = shim(parsed);
return JSON.stringify(patched);
}

// Exposed for unit tests only.
export const __test = { coerceToArray, TOOL_SHIMS };
36 changes: 31 additions & 5 deletions open-sse/translator/response/openai-to-claude.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { register } from "../registry.ts";
import { FORMATS } from "../formats.ts";
import { CLAUDE_OAUTH_TOOL_PREFIX } from "../request/openai-to-claude.ts";
import { hasToolCallShim, applyToolCallShimToBuffer } from "../helpers/toolCallShim.ts";

// Helper: stop thinking block if started
function stopThinkingBlock(state, results) {
Expand Down Expand Up @@ -157,18 +158,23 @@ export function openaiToClaudeResponse(chunk, state) {
stopTextBlock(state, results);

const toolBlockIndex = state.nextBlockIndex++;
state.toolCalls.set(idx, {
id: tc.id,
name: tc.function?.name || "",
blockIndex: toolBlockIndex,
});

// Strip prefix from tool name for response
let toolName = tc.function?.name || "";
if (toolName.startsWith(CLAUDE_OAUTH_TOOL_PREFIX)) {
toolName = toolName.slice(CLAUDE_OAUTH_TOOL_PREFIX.length);
}

state.toolCalls.set(idx, {
id: tc.id,
name: toolName,
blockIndex: toolBlockIndex,
// Shimmed tools buffer their raw args and emit a single corrected
// input_json_delta at content_block_stop time (see finish handler).
shimmed: hasToolCallShim(toolName),
argBuffer: "",
});

results.push({
type: "content_block_start",
index: toolBlockIndex,
Expand All @@ -184,6 +190,15 @@ export function openaiToClaudeResponse(chunk, state) {
if (tc.function?.arguments) {
const toolInfo = state.toolCalls.get(idx);
if (toolInfo) {
// Always buffer the raw stream so shimmed tools can re-emit a
// corrected JSON at stop time.
toolInfo.argBuffer = (toolInfo.argBuffer || "") + tc.function.arguments;

if (toolInfo.shimmed) {
// Suppress passthrough; we emit one corrective delta at finish.
continue;
}
Comment on lines +193 to +200

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current implementation buffers tool arguments for all tool calls, which introduces significant memory overhead for non-shimmed tools. For large tool outputs (e.g., file contents or large data sets), this could lead to excessive memory consumption or OOM errors in high-traffic environments. Buffering should be restricted to only those tools that require shimming.

Additionally, since argBuffer is initialized to "" at line 175, the (toolInfo.argBuffer || "") check is redundant.

          if (toolInfo.shimmed) {
            // Buffer the raw stream for shimmed tools to re-emit corrected JSON at stop time.
            toolInfo.argBuffer += tc.function.arguments;
            // Suppress passthrough; we emit one corrective delta at finish.
            continue;
          }


let deltaStr = tc.function.arguments;

// Fix #1852: Strip empty string and array placeholders from streaming tool arguments
Expand Down Expand Up @@ -211,6 +226,17 @@ export function openaiToClaudeResponse(chunk, state) {
stopTextBlock(state, results);

for (const [, toolInfo] of state.toolCalls) {
// For shimmed tools, emit one corrective input_json_delta with the
// fully patched JSON before closing the block.
if (toolInfo.shimmed) {
const patched = applyToolCallShimToBuffer(toolInfo.name, toolInfo.argBuffer || "");
results.push({
type: "content_block_delta",
index: toolInfo.blockIndex,
delta: { type: "input_json_delta", partial_json: patched },
});
}

results.push({
type: "content_block_stop",
index: toolInfo.blockIndex,
Expand Down
165 changes: 13 additions & 152 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading