thinking sanitization

This commit is contained in:
Badri Narayanan S
2025-12-19 22:41:01 +05:30
parent da48b9898b
commit f625377bdf
2 changed files with 28 additions and 14 deletions

View File

@@ -188,11 +188,6 @@ function buildCloudCodeRequest(anthropicRequest, projectId) {
requestId: 'agent-' + crypto.randomUUID() requestId: 'agent-' + crypto.randomUUID()
}; };
// Debug: log if tools are present
if (googleRequest.tools) {
console.log('[CloudCode] Tools in request:', JSON.stringify(googleRequest.tools).substring(0, 500));
}
return payload; return payload;
} }

View File

@@ -149,17 +149,27 @@ export function removeTrailingThinkingBlocks(content) {
/** /**
* Sanitize a thinking block by removing extra fields like cache_control. * Sanitize a thinking block by removing extra fields like cache_control.
* Only keeps: type, thinking, signature * Only keeps: type, thinking, signature (for thinking) or type, data (for redacted_thinking)
*/ */
function sanitizeAnthropicThinkingBlock(block) { function sanitizeAnthropicThinkingBlock(block) {
if (!block || block.type !== 'thinking') return block; if (!block) return block;
if (block.type === 'thinking') {
const sanitized = { type: 'thinking' }; const sanitized = { type: 'thinking' };
if (block.thinking !== undefined) sanitized.thinking = block.thinking; if (block.thinking !== undefined) sanitized.thinking = block.thinking;
if (block.signature !== undefined) sanitized.signature = block.signature; if (block.signature !== undefined) sanitized.signature = block.signature;
return sanitized; return sanitized;
} }
if (block.type === 'redacted_thinking') {
const sanitized = { type: 'redacted_thinking' };
if (block.data !== undefined) sanitized.data = block.data;
return sanitized;
}
return block;
}
/** /**
* Filter thinking blocks: keep only those with valid signatures. * Filter thinking blocks: keep only those with valid signatures.
* Blocks without signatures are dropped (API requires signatures). * Blocks without signatures are dropped (API requires signatures).
@@ -201,7 +211,15 @@ export function restoreThinkingSignatures(content) {
*/ */
export function reorderAssistantContent(content) { export function reorderAssistantContent(content) {
if (!Array.isArray(content)) return content; if (!Array.isArray(content)) return content;
if (content.length <= 1) return content;
// Even for single-element arrays, we need to sanitize thinking blocks
if (content.length === 1) {
const block = content[0];
if (block && (block.type === 'thinking' || block.type === 'redacted_thinking')) {
return [sanitizeAnthropicThinkingBlock(block)];
}
return content;
}
const thinkingBlocks = []; const thinkingBlocks = [];
const textBlocks = []; const textBlocks = [];
@@ -211,7 +229,7 @@ export function reorderAssistantContent(content) {
for (const block of content) { for (const block of content) {
if (!block) continue; if (!block) continue;
if (block.type === 'thinking') { if (block.type === 'thinking' || block.type === 'redacted_thinking') {
// Sanitize thinking blocks to remove cache_control and other extra fields // Sanitize thinking blocks to remove cache_control and other extra fields
thinkingBlocks.push(sanitizeAnthropicThinkingBlock(block)); thinkingBlocks.push(sanitizeAnthropicThinkingBlock(block));
} else if (block.type === 'tool_use') { } else if (block.type === 'tool_use') {
@@ -423,7 +441,8 @@ export function convertAnthropicToGoogle(anthropicRequest) {
} }
// Convert messages to contents, then filter unsigned thinking blocks // Convert messages to contents, then filter unsigned thinking blocks
for (const msg of messages) { for (let i = 0; i < messages.length; i++) {
const msg = messages[i];
let msgContent = msg.content; let msgContent = msg.content;
// For assistant messages, process thinking blocks and reorder content // For assistant messages, process thinking blocks and reorder content