This commit is contained in:
decolua
2026-04-11 11:36:33 +07:00
parent ed17a8ffac
commit 875a1282ea
5 changed files with 102 additions and 34 deletions
+14 -10
View File
@@ -3,9 +3,10 @@
// Anthropic tool_use.id must match: ^[a-zA-Z0-9_-]+$
const TOOL_ID_PATTERN = /^[a-zA-Z0-9_-]+$/;
// Generate unique tool call ID (always valid for Anthropic)
export function generateToolCallId() {
return `call_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 9)}`;
// Generate deterministic tool call ID from position + tool name (cache-friendly)
export function generateToolCallId(msgIndex = 0, tcIndex = 0, toolName = "") {
const name = toolName ? `_${toolName.replace(/[^a-zA-Z0-9_-]/g, "")}` : "";
return `call_msg${msgIndex}_tc${tcIndex}${name}`;
}
// Sanitize ID to match Anthropic pattern: keep only alphanumeric, underscore, hyphen
@@ -19,13 +20,15 @@ function sanitizeToolId(id) {
export function ensureToolCallIds(body) {
if (!body.messages || !Array.isArray(body.messages)) return body;
for (const msg of body.messages) {
for (let i = 0; i < body.messages.length; i++) {
const msg = body.messages[i];
if (msg.role === "assistant" && msg.tool_calls && Array.isArray(msg.tool_calls)) {
for (const tc of msg.tool_calls) {
for (let j = 0; j < msg.tool_calls.length; j++) {
const tc = msg.tool_calls[j];
// Validate or regenerate ID for Anthropic compatibility
if (!tc.id || !TOOL_ID_PATTERN.test(tc.id)) {
const sanitized = sanitizeToolId(tc.id);
tc.id = sanitized || generateToolCallId();
tc.id = sanitized || generateToolCallId(i, j, tc.function?.name);
}
if (!tc.type) {
tc.type = "function";
@@ -40,20 +43,21 @@ export function ensureToolCallIds(body) {
// Validate tool_call_id in tool messages (role: "tool")
if (msg.role === "tool" && msg.tool_call_id && !TOOL_ID_PATTERN.test(msg.tool_call_id)) {
const sanitized = sanitizeToolId(msg.tool_call_id);
msg.tool_call_id = sanitized || generateToolCallId();
msg.tool_call_id = sanitized || generateToolCallId(i, 0);
}
// Also validate tool_use blocks in content (Claude format)
if (Array.isArray(msg.content)) {
for (const block of msg.content) {
for (let k = 0; k < msg.content.length; k++) {
const block = msg.content[k];
if (block.type === "tool_use" && block.id && !TOOL_ID_PATTERN.test(block.id)) {
const sanitized = sanitizeToolId(block.id);
block.id = sanitized || generateToolCallId();
block.id = sanitized || generateToolCallId(i, k, block.name);
}
// Validate tool_use_id in tool_result blocks
if (block.type === "tool_result" && block.tool_use_id && !TOOL_ID_PATTERN.test(block.tool_use_id)) {
const sanitized = sanitizeToolId(block.tool_use_id);
block.tool_use_id = sanitized || generateToolCallId();
block.tool_use_id = sanitized || generateToolCallId(i, k);
}
}
}