diff --git a/src/cursor/cursor-anthropic-translator.ts b/src/cursor/cursor-anthropic-translator.ts index 4c84cdb5..30c8db5b 100644 --- a/src/cursor/cursor-anthropic-translator.ts +++ b/src/cursor/cursor-anthropic-translator.ts @@ -102,6 +102,7 @@ export function translateAnthropicRequest(raw: unknown): TranslatedAnthropicRequ const textParts: string[] = []; const toolCalls: NonNullable = []; + let sawToolResult = false; content.forEach((block, blockIndex) => { const parsed = assertObject( @@ -140,6 +141,7 @@ export function translateAnthropicRequest(raw: unknown): TranslatedAnthropicRequ `messages[${messageIndex}].content[${blockIndex}] tool_result requires user role` ); } + sawToolResult = true; translatedMessages.push({ role: 'tool', tool_call_id: typeof parsed.tool_use_id === 'string' ? parsed.tool_use_id : '', @@ -165,7 +167,7 @@ export function translateAnthropicRequest(raw: unknown): TranslatedAnthropicRequ return; } - if (textParts.length > 0 || toolCalls.length === 0) { + if (textParts.length > 0 || !sawToolResult) { translatedMessages.push({ role, content: textParts.join('\n'), diff --git a/tests/unit/cursor/cursor-anthropic-translator.test.ts b/tests/unit/cursor/cursor-anthropic-translator.test.ts index 3587b965..8ae0e619 100644 --- a/tests/unit/cursor/cursor-anthropic-translator.test.ts +++ b/tests/unit/cursor/cursor-anthropic-translator.test.ts @@ -59,6 +59,36 @@ describe('translateAnthropicRequest', () => { }) ).toThrow('is not supported'); }); + + it('does not append an empty user message after tool_result-only turns', () => { + const translated = translateAnthropicRequest({ + messages: [ + { + role: 'assistant', + content: [{ type: 'tool_use', id: 'toolu_1', name: 'search', input: { q: 'x' } }], + }, + { + role: 'user', + content: [{ type: 'tool_result', tool_use_id: 'toolu_1', content: 'done' }], + }, + ], + }); + + expect(translated.messages).toEqual([ + { + role: 'assistant', + content: '', + tool_calls: [ + { + id: 'toolu_1', + type: 'function', + function: { name: 'search', arguments: '{"q":"x"}' }, + }, + ], + }, + { role: 'tool', tool_call_id: 'toolu_1', content: 'done' }, + ]); + }); }); describe('createAnthropicProxyResponse', () => {