mirror of
https://github.com/tiennm99/ccs.git
synced 2026-07-17 06:17:09 +00:00
fix(cursor): harden anthropic translation edge cases
This commit is contained in:
@@ -13,6 +13,9 @@ export interface TranslatedAnthropicRequest {
|
||||
messages: CursorOpenAIMessage[];
|
||||
}
|
||||
|
||||
const TOOL_RESULT_SERIALIZATION_FALLBACK = '[unserializable content]';
|
||||
const TOOL_USE_ARGUMENTS_FALLBACK = '{}';
|
||||
|
||||
function assertObject(value: unknown, label: string): Record<string, unknown> {
|
||||
if (typeof value !== 'object' || value === null) {
|
||||
throw new Error(`${label} must be an object`);
|
||||
@@ -20,6 +23,19 @@ function assertObject(value: unknown, label: string): Record<string, unknown> {
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
|
||||
function safeJsonStringify(value: unknown, fallback: string): string {
|
||||
try {
|
||||
const serialized = JSON.stringify(value);
|
||||
return typeof serialized === 'string' ? serialized : fallback;
|
||||
} catch {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
function createFallbackToolId(messageIndex: number, blockIndex: number): string {
|
||||
return `toolu_ccs_fallback_${messageIndex}_${blockIndex}`;
|
||||
}
|
||||
|
||||
function flattenTextContent(content: unknown, label: string): string {
|
||||
if (typeof content === 'string') {
|
||||
return content;
|
||||
@@ -49,7 +65,7 @@ function toToolResultContent(content: unknown, label: string): string {
|
||||
if (Array.isArray(content)) {
|
||||
return flattenTextContent(content, label);
|
||||
}
|
||||
return JSON.stringify(content);
|
||||
return safeJsonStringify(content, TOOL_RESULT_SERIALIZATION_FALLBACK);
|
||||
}
|
||||
|
||||
function mapThinkingToReasoningEffort(
|
||||
@@ -125,11 +141,11 @@ export function translateAnthropicRequest(raw: unknown): TranslatedAnthropicRequ
|
||||
id:
|
||||
typeof parsed.id === 'string' && parsed.id.length > 0
|
||||
? parsed.id
|
||||
: `toolu_${messageIndex}_${blockIndex}`,
|
||||
: createFallbackToolId(messageIndex, blockIndex),
|
||||
type: 'function',
|
||||
function: {
|
||||
name: typeof parsed.name === 'string' ? parsed.name : 'tool',
|
||||
arguments: JSON.stringify(parsed.input ?? {}),
|
||||
arguments: safeJsonStringify(parsed.input ?? {}, TOOL_USE_ARGUMENTS_FALLBACK),
|
||||
},
|
||||
});
|
||||
return;
|
||||
|
||||
@@ -89,6 +89,67 @@ describe('translateAnthropicRequest', () => {
|
||||
{ role: 'tool', tool_call_id: 'toolu_1', content: 'done' },
|
||||
]);
|
||||
});
|
||||
|
||||
it('uses a distinct fallback prefix for missing tool_use ids', () => {
|
||||
const translated = translateAnthropicRequest({
|
||||
messages: [
|
||||
{
|
||||
role: 'assistant',
|
||||
content: [{ type: 'tool_use', name: 'search', input: { q: 'x' } }],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
expect(translated.messages[0]?.tool_calls?.[0]?.id).toBe('toolu_ccs_fallback_0_0');
|
||||
});
|
||||
|
||||
it('falls back when tool_result content cannot be serialized', () => {
|
||||
const circular: Record<string, unknown> = {};
|
||||
circular.self = circular;
|
||||
|
||||
const translated = translateAnthropicRequest({
|
||||
messages: [
|
||||
{
|
||||
role: 'user',
|
||||
content: [{ type: 'tool_result', tool_use_id: 'toolu_1', content: circular }],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
expect(translated.messages).toEqual([
|
||||
{
|
||||
role: 'tool',
|
||||
tool_call_id: 'toolu_1',
|
||||
content: '[unserializable content]',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('falls back when tool_use input cannot be serialized', () => {
|
||||
const circular: Record<string, unknown> = {};
|
||||
circular.self = circular;
|
||||
|
||||
const translated = translateAnthropicRequest({
|
||||
messages: [
|
||||
{
|
||||
role: 'assistant',
|
||||
content: [{ type: 'tool_use', name: 'search', input: circular }],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
expect(translated.messages[0]).toEqual({
|
||||
role: 'assistant',
|
||||
content: '',
|
||||
tool_calls: [
|
||||
{
|
||||
id: 'toolu_ccs_fallback_0_0',
|
||||
type: 'function',
|
||||
function: { name: 'search', arguments: '{}' },
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('createAnthropicProxyResponse', () => {
|
||||
@@ -166,4 +227,21 @@ describe('createAnthropicProxyResponse', () => {
|
||||
expect(body).toContain('"type":"text_delta"');
|
||||
expect(body).toContain('event: message_stop');
|
||||
});
|
||||
|
||||
it('emits Anthropic-style error events when SSE translation fails', async () => {
|
||||
const oversizedChunk = `data: ${'x'.repeat(1024 * 1024 + 32)}`;
|
||||
|
||||
const transformed = await createAnthropicProxyResponse(
|
||||
new Response(oversizedChunk, {
|
||||
status: 200,
|
||||
headers: { 'Content-Type': 'text/event-stream' },
|
||||
})
|
||||
);
|
||||
|
||||
const body = await transformed.text();
|
||||
expect(body).toContain('event: error');
|
||||
expect(body).toContain('"type":"error"');
|
||||
expect(body).toContain('"error":{"type":"api_error"');
|
||||
expect(body).toContain('Failed to translate Cursor SSE response');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user