sanitize thinking blocks
This commit is contained in:
@@ -147,20 +147,42 @@ export function removeTrailingThinkingBlocks(content) {
|
|||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sanitize a thinking block by removing extra fields like cache_control.
|
||||||
|
* Only keeps: type, thinking, signature
|
||||||
|
*/
|
||||||
|
function sanitizeAnthropicThinkingBlock(block) {
|
||||||
|
if (!block || block.type !== 'thinking') return block;
|
||||||
|
|
||||||
|
const sanitized = { type: 'thinking' };
|
||||||
|
if (block.thinking !== undefined) sanitized.thinking = block.thinking;
|
||||||
|
if (block.signature !== undefined) sanitized.signature = block.signature;
|
||||||
|
return sanitized;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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).
|
||||||
|
* Also sanitizes blocks to remove extra fields like cache_control.
|
||||||
*/
|
*/
|
||||||
export function restoreThinkingSignatures(content) {
|
export function restoreThinkingSignatures(content) {
|
||||||
if (!Array.isArray(content)) return content;
|
if (!Array.isArray(content)) return content;
|
||||||
|
|
||||||
const originalLength = content.length;
|
const originalLength = content.length;
|
||||||
const filtered = content.filter(block => {
|
const filtered = [];
|
||||||
if (!block || block.type !== 'thinking') return true;
|
|
||||||
|
|
||||||
// Keep blocks with valid signatures (>= 50 chars)
|
for (const block of content) {
|
||||||
return block.signature && block.signature.length >= 50;
|
if (!block || block.type !== 'thinking') {
|
||||||
});
|
filtered.push(block);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keep blocks with valid signatures (>= 50 chars), sanitized
|
||||||
|
if (block.signature && block.signature.length >= 50) {
|
||||||
|
filtered.push(sanitizeAnthropicThinkingBlock(block));
|
||||||
|
}
|
||||||
|
// Unsigned thinking blocks are dropped
|
||||||
|
}
|
||||||
|
|
||||||
if (filtered.length < originalLength) {
|
if (filtered.length < originalLength) {
|
||||||
console.log(`[FormatConverter] Dropped ${originalLength - filtered.length} unsigned thinking block(s)`);
|
console.log(`[FormatConverter] Dropped ${originalLength - filtered.length} unsigned thinking block(s)`);
|
||||||
@@ -190,7 +212,8 @@ export function reorderAssistantContent(content) {
|
|||||||
if (!block) continue;
|
if (!block) continue;
|
||||||
|
|
||||||
if (block.type === 'thinking') {
|
if (block.type === 'thinking') {
|
||||||
thinkingBlocks.push(block);
|
// Sanitize thinking blocks to remove cache_control and other extra fields
|
||||||
|
thinkingBlocks.push(sanitizeAnthropicThinkingBlock(block));
|
||||||
} else if (block.type === 'tool_use') {
|
} else if (block.type === 'tool_use') {
|
||||||
toolUseBlocks.push(block);
|
toolUseBlocks.push(block);
|
||||||
} else if (block.type === 'text') {
|
} else if (block.type === 'text') {
|
||||||
|
|||||||
Reference in New Issue
Block a user