fix(cursor): harden anthropic response fallback paths

This commit is contained in:
Tam Nhu Tran committed 2026-03-16 14:42:15 -04:00
1 parent 9ff021e42b
commit eaee4a92ca
2 files changed
+101 -7

No files matched your search

@@ -90,6 +90,12 @@ describe('translateAnthropicRequest', () => {
]);
});
it('handles empty messages arrays', () => {
const translated = translateAnthropicRequest({ messages: [] });
expect(translated.messages).toEqual([]);
});
it('uses a distinct fallback prefix for missing tool_use ids', () => {
const translated = translateAnthropicRequest({
messages: [
@@ -125,6 +131,25 @@ describe('translateAnthropicRequest', () => {
]);
});
it('returns empty string for tool_result blocks without content', () => {
const translated = translateAnthropicRequest({
messages: [
{
role: 'user',
content: [{ type: 'tool_result', tool_use_id: 'toolu_1' }],
},
],
});
expect(translated.messages).toEqual([
{
role: 'tool',
tool_call_id: 'toolu_1',
content: '',
},
]);
});
it('falls back when tool_use input cannot be serialized', () => {
const circular: Record<string, unknown> = {};
circular.self = circular;
@@ -207,6 +232,61 @@ describe('createAnthropicProxyResponse', () => {
expect(body.content[2]?.input).toEqual({ q: 'cursor daemon' });
});
it('returns 502 when Cursor returns invalid JSON', async () => {
const transformed = await createAnthropicProxyResponse(
new Response('not json', {
status: 200,
headers: { 'Content-Type': 'application/json' },
})
);
expect(transformed.status).toBe(502);
const body = (await transformed.json()) as { error?: { type?: string; message?: string } };
expect(body.error?.type).toBe('api_error');
expect(body.error?.message).toBe('Failed to translate Cursor JSON response');
});
it('returns 502 when Cursor response is missing choices', async () => {
const transformed = await createAnthropicProxyResponse(
new Response(
JSON.stringify({
id: 'chatcmpl_missing_choices',
model: 'claude-sonnet-4.5',
}),
{
status: 200,
headers: { 'Content-Type': 'application/json' },
}
)
);
expect(transformed.status).toBe(502);
const body = (await transformed.json()) as { error?: { type?: string; message?: string } };
expect(body.error?.type).toBe('api_error');
expect(body.error?.message).toBe('Failed to translate Cursor JSON response');
});
it('returns 502 when Cursor response has empty choices', async () => {
const transformed = await createAnthropicProxyResponse(
new Response(
JSON.stringify({
id: 'chatcmpl_empty_choices',
model: 'claude-sonnet-4.5',
choices: [],
}),
{
status: 200,
headers: { 'Content-Type': 'application/json' },
}
)
);
expect(transformed.status).toBe(502);
const body = (await transformed.json()) as { error?: { type?: string; message?: string } };
expect(body.error?.type).toBe('api_error');
expect(body.error?.message).toBe('Failed to translate Cursor JSON response');
});
it('converts OpenAI SSE chunks into Anthropic SSE events', async () => {
const openAiSse = [
'data: {"id":"chatcmpl_2","object":"chat.completion.chunk","created":1,"model":"claude-sonnet-4.5","choices":[{"index":0,"delta":{"role":"assistant","content":"Hello"},"finish_reason":null}]}\n\n',