mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 13:38:31 +00:00
fix: sanitize Read tool args to prevent retry loops from non-Anthropic models (#1144)
* fix: sanitize Read tool args to prevent retry loops from non-Anthropic models * fix: sanitize invalid Read pages from tool args Non-Anthropic models sometimes emit optional Read args like pages: "" for non-PDF files, which Claude Code rejects before the tool runs. Drop invalid pages values, keep valid PDF page ranges, and coerce numeric string bounds before clamping limit/offset. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
dcc46f2eee
commit
96a9a2b36d
@@ -4,21 +4,40 @@ import { FORMATS } from "../formats.js";
|
|||||||
// Prefix for Claude OAuth tool names (must match request translator)
|
// Prefix for Claude OAuth tool names (must match request translator)
|
||||||
const CLAUDE_OAUTH_TOOL_PREFIX = "proxy_";
|
const CLAUDE_OAUTH_TOOL_PREFIX = "proxy_";
|
||||||
|
|
||||||
// Strip optional empty-string tool arguments that some providers emit.
|
// Sanitize tool call arguments to fix bad params from non-Anthropic models
|
||||||
// Claude Code's Read tool rejects pages: "" but accepts pages being absent.
|
function sanitizeToolArgs(toolName, argsJson) {
|
||||||
function sanitizeToolArguments(toolName, argsJson) {
|
|
||||||
try {
|
try {
|
||||||
const args = JSON.parse(argsJson);
|
const args = JSON.parse(argsJson);
|
||||||
if (typeof args === "object" && args !== null) {
|
const name = toolName.startsWith(CLAUDE_OAUTH_TOOL_PREFIX)
|
||||||
if (toolName === "Read" && args.pages === "") {
|
? toolName.slice(CLAUDE_OAUTH_TOOL_PREFIX.length)
|
||||||
delete args.pages;
|
: toolName;
|
||||||
}
|
if (name === "Read") sanitizeReadArgs(args);
|
||||||
return JSON.stringify(args);
|
return JSON.stringify(args);
|
||||||
}
|
|
||||||
} catch {
|
} catch {
|
||||||
// Not valid JSON yet (streaming chunk) — return as-is
|
return argsJson;
|
||||||
}
|
}
|
||||||
return argsJson;
|
}
|
||||||
|
|
||||||
|
function sanitizeReadArgs(args) {
|
||||||
|
if (typeof args.limit === "string" && /^\d+$/.test(args.limit)) args.limit = Number(args.limit);
|
||||||
|
if (typeof args.offset === "string" && /^-?\d+$/.test(args.offset)) args.offset = Number(args.offset);
|
||||||
|
|
||||||
|
if (typeof args.limit === "number") {
|
||||||
|
if (args.limit > 2000) args.limit = 2000;
|
||||||
|
if (args.limit < 1) delete args.limit;
|
||||||
|
}
|
||||||
|
if (typeof args.offset === "number" && args.offset < 0) args.offset = 0;
|
||||||
|
|
||||||
|
if ("pages" in args && !isValidPdfPagesArg(args.file_path, args.pages)) {
|
||||||
|
delete args.pages;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function isValidPdfPagesArg(filePath, pages) {
|
||||||
|
return typeof filePath === "string" &&
|
||||||
|
filePath.toLowerCase().endsWith(".pdf") &&
|
||||||
|
typeof pages === "string" &&
|
||||||
|
/^\d+(?:-\d+)?$/.test(pages);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper: stop thinking block if started
|
// Helper: stop thinking block if started
|
||||||
@@ -54,32 +73,32 @@ export function openaiToClaudeResponse(chunk, state) {
|
|||||||
if (chunk.usage && typeof chunk.usage === "object") {
|
if (chunk.usage && typeof chunk.usage === "object") {
|
||||||
const promptTokens = typeof chunk.usage.prompt_tokens === "number" ? chunk.usage.prompt_tokens : 0;
|
const promptTokens = typeof chunk.usage.prompt_tokens === "number" ? chunk.usage.prompt_tokens : 0;
|
||||||
const outputTokens = typeof chunk.usage.completion_tokens === "number" ? chunk.usage.completion_tokens : 0;
|
const outputTokens = typeof chunk.usage.completion_tokens === "number" ? chunk.usage.completion_tokens : 0;
|
||||||
|
|
||||||
// Extract cache tokens from prompt_tokens_details
|
// Extract cache tokens from prompt_tokens_details
|
||||||
const cachedTokens = chunk.usage.prompt_tokens_details?.cached_tokens;
|
const cachedTokens = chunk.usage.prompt_tokens_details?.cached_tokens;
|
||||||
const cacheCreationTokens = chunk.usage.prompt_tokens_details?.cache_creation_tokens;
|
const cacheCreationTokens = chunk.usage.prompt_tokens_details?.cache_creation_tokens;
|
||||||
const cacheReadTokens = typeof cachedTokens === "number" ? cachedTokens : 0;
|
const cacheReadTokens = typeof cachedTokens === "number" ? cachedTokens : 0;
|
||||||
const cacheCreateTokens = typeof cacheCreationTokens === "number" ? cacheCreationTokens : 0;
|
const cacheCreateTokens = typeof cacheCreationTokens === "number" ? cacheCreationTokens : 0;
|
||||||
|
|
||||||
// input_tokens = prompt_tokens - cached_tokens - cache_creation_tokens
|
// input_tokens = prompt_tokens - cached_tokens - cache_creation_tokens
|
||||||
// Because OpenAI's prompt_tokens includes all prompt-side tokens
|
// Because OpenAI's prompt_tokens includes all prompt-side tokens
|
||||||
const inputTokens = promptTokens - cacheReadTokens - cacheCreateTokens;
|
const inputTokens = promptTokens - cacheReadTokens - cacheCreateTokens;
|
||||||
|
|
||||||
state.usage = {
|
state.usage = {
|
||||||
input_tokens: inputTokens,
|
input_tokens: inputTokens,
|
||||||
output_tokens: outputTokens
|
output_tokens: outputTokens
|
||||||
};
|
};
|
||||||
|
|
||||||
// Add cache_read_input_tokens if present
|
// Add cache_read_input_tokens if present
|
||||||
if (cacheReadTokens > 0) {
|
if (cacheReadTokens > 0) {
|
||||||
state.usage.cache_read_input_tokens = cacheReadTokens;
|
state.usage.cache_read_input_tokens = cacheReadTokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add cache_creation_input_tokens if present
|
// Add cache_creation_input_tokens if present
|
||||||
if (cacheCreateTokens > 0) {
|
if (cacheCreateTokens > 0) {
|
||||||
state.usage.cache_creation_input_tokens = cacheCreateTokens;
|
state.usage.cache_creation_input_tokens = cacheCreateTokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note: completion_tokens_details.reasoning_tokens is already included in output_tokens
|
// Note: completion_tokens_details.reasoning_tokens is already included in output_tokens
|
||||||
// No need to add separately as Claude expects total output_tokens
|
// No need to add separately as Claude expects total output_tokens
|
||||||
}
|
}
|
||||||
@@ -165,13 +184,13 @@ export function openaiToClaudeResponse(chunk, state) {
|
|||||||
|
|
||||||
const toolBlockIndex = state.nextBlockIndex++;
|
const toolBlockIndex = state.nextBlockIndex++;
|
||||||
state.toolCalls.set(idx, { id: tc.id, name: tc.function?.name || "", blockIndex: toolBlockIndex });
|
state.toolCalls.set(idx, { id: tc.id, name: tc.function?.name || "", blockIndex: toolBlockIndex });
|
||||||
|
|
||||||
// Strip prefix from tool name for response
|
// Strip prefix from tool name for response
|
||||||
let toolName = tc.function?.name || "";
|
let toolName = tc.function?.name || "";
|
||||||
if (toolName.startsWith(CLAUDE_OAUTH_TOOL_PREFIX)) {
|
if (toolName.startsWith(CLAUDE_OAUTH_TOOL_PREFIX)) {
|
||||||
toolName = toolName.slice(CLAUDE_OAUTH_TOOL_PREFIX.length);
|
toolName = toolName.slice(CLAUDE_OAUTH_TOOL_PREFIX.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
results.push({
|
results.push({
|
||||||
type: "content_block_start",
|
type: "content_block_start",
|
||||||
index: toolBlockIndex,
|
index: toolBlockIndex,
|
||||||
@@ -187,12 +206,9 @@ export function openaiToClaudeResponse(chunk, state) {
|
|||||||
if (tc.function?.arguments) {
|
if (tc.function?.arguments) {
|
||||||
const toolInfo = state.toolCalls.get(idx);
|
const toolInfo = state.toolCalls.get(idx);
|
||||||
if (toolInfo) {
|
if (toolInfo) {
|
||||||
const sanitized = sanitizeToolArguments(toolInfo.name, tc.function.arguments);
|
// Buffer args instead of streaming — sanitize at finish to fix bad params
|
||||||
results.push({
|
if (!state.toolArgBuffers) state.toolArgBuffers = new Map();
|
||||||
type: "content_block_delta",
|
state.toolArgBuffers.set(idx, (state.toolArgBuffers.get(idx) || "") + tc.function.arguments);
|
||||||
index: toolInfo.blockIndex,
|
|
||||||
delta: { type: "input_json_delta", partial_json: sanitized }
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -203,7 +219,17 @@ export function openaiToClaudeResponse(chunk, state) {
|
|||||||
stopThinkingBlock(state, results);
|
stopThinkingBlock(state, results);
|
||||||
stopTextBlock(state, results);
|
stopTextBlock(state, results);
|
||||||
|
|
||||||
for (const [, toolInfo] of state.toolCalls) {
|
for (const [idx, toolInfo] of state.toolCalls) {
|
||||||
|
// Emit buffered + sanitized args as single delta before stop
|
||||||
|
const buffered = state.toolArgBuffers?.get(idx);
|
||||||
|
if (buffered) {
|
||||||
|
const sanitized = sanitizeToolArgs(toolInfo.name, buffered);
|
||||||
|
results.push({
|
||||||
|
type: "content_block_delta",
|
||||||
|
index: toolInfo.blockIndex,
|
||||||
|
delta: { type: "input_json_delta", partial_json: sanitized }
|
||||||
|
});
|
||||||
|
}
|
||||||
results.push({
|
results.push({
|
||||||
type: "content_block_stop",
|
type: "content_block_stop",
|
||||||
index: toolInfo.blockIndex
|
index: toolInfo.blockIndex
|
||||||
@@ -212,7 +238,7 @@ export function openaiToClaudeResponse(chunk, state) {
|
|||||||
|
|
||||||
// Mark finish for later usage injection in stream.js
|
// Mark finish for later usage injection in stream.js
|
||||||
state.finishReason = choice.finish_reason;
|
state.finishReason = choice.finish_reason;
|
||||||
|
|
||||||
// Use tracked usage (will be estimated in stream.js if not valid)
|
// Use tracked usage (will be estimated in stream.js if not valid)
|
||||||
const finalUsage = state.usage || { input_tokens: 0, output_tokens: 0 };
|
const finalUsage = state.usage || { input_tokens: 0, output_tokens: 0 };
|
||||||
results.push({
|
results.push({
|
||||||
@@ -238,4 +264,3 @@ function convertFinishReason(reason) {
|
|||||||
|
|
||||||
// Register
|
// Register
|
||||||
register(FORMATS.OPENAI, FORMATS.CLAUDE, null, openaiToClaudeResponse);
|
register(FORMATS.OPENAI, FORMATS.CLAUDE, null, openaiToClaudeResponse);
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { openaiToClaudeResponse } from "../../open-sse/translator/response/openai-to-claude.js";
|
||||||
|
|
||||||
|
function createState() {
|
||||||
|
return { toolCalls: new Map(), nextBlockIndex: 0 };
|
||||||
|
}
|
||||||
|
|
||||||
|
function getInputJsonDelta(events) {
|
||||||
|
return events.find((event) => event.type === "content_block_delta" && event.delta?.type === "input_json_delta")?.delta.partial_json;
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("openaiToClaudeResponse tool argument sanitization", () => {
|
||||||
|
it("drops invalid Read pages and clamps numeric bounds", () => {
|
||||||
|
const state = createState();
|
||||||
|
|
||||||
|
openaiToClaudeResponse({
|
||||||
|
id: "chatcmpl-test-read",
|
||||||
|
model: "test-model",
|
||||||
|
choices: [{ delta: { tool_calls: [{ index: 0, id: "toolu_read", function: { name: "Read" } }] } }],
|
||||||
|
}, state);
|
||||||
|
|
||||||
|
const events = openaiToClaudeResponse({
|
||||||
|
id: "chatcmpl-test-read",
|
||||||
|
model: "test-model",
|
||||||
|
choices: [{
|
||||||
|
delta: { tool_calls: [{ index: 0, function: { arguments: JSON.stringify({ file_path: "F:/repo/file.js", offset: -5, limit: 999999999, pages: "" }) } }] },
|
||||||
|
finish_reason: "tool_calls",
|
||||||
|
}],
|
||||||
|
}, state);
|
||||||
|
|
||||||
|
expect(JSON.parse(getInputJsonDelta(events))).toEqual({
|
||||||
|
file_path: "F:/repo/file.js",
|
||||||
|
offset: 0,
|
||||||
|
limit: 2000,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps valid PDF pages", () => {
|
||||||
|
const state = createState();
|
||||||
|
|
||||||
|
openaiToClaudeResponse({
|
||||||
|
id: "chatcmpl-test-pdf",
|
||||||
|
model: "test-model",
|
||||||
|
choices: [{ delta: { tool_calls: [{ index: 0, id: "toolu_pdf", function: { name: "proxy_Read" } }] } }],
|
||||||
|
}, state);
|
||||||
|
|
||||||
|
const events = openaiToClaudeResponse({
|
||||||
|
id: "chatcmpl-test-pdf",
|
||||||
|
model: "test-model",
|
||||||
|
choices: [{
|
||||||
|
delta: { tool_calls: [{ index: 0, function: { arguments: JSON.stringify({ file_path: "F:/repo/doc.pdf", pages: "1-3" }) } }] },
|
||||||
|
finish_reason: "tool_calls",
|
||||||
|
}],
|
||||||
|
}, state);
|
||||||
|
|
||||||
|
expect(JSON.parse(getInputJsonDelta(events))).toEqual({
|
||||||
|
file_path: "F:/repo/doc.pdf",
|
||||||
|
pages: "1-3",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user