mirror of
https://github.com/tiennm99/ccs.git
synced 2026-10-14 14:17:11 +00:00
248 lines
7.8 KiB
TypeScript
248 lines
7.8 KiB
TypeScript
import { describe, expect, it } from 'bun:test';
|
|
import { createAnthropicProxyResponse } from '../../../src/cursor/cursor-anthropic-response';
|
|
import { translateAnthropicRequest } from '../../../src/cursor/cursor-anthropic-translator';
|
|
|
|
describe('translateAnthropicRequest', () => {
|
|
it('maps Anthropic system, tool use, and tool result blocks into Cursor OpenAI messages', () => {
|
|
const translated = translateAnthropicRequest({
|
|
model: 'claude-sonnet-4.5',
|
|
stream: true,
|
|
thinking: { type: 'enabled', budget_tokens: 9000 },
|
|
tools: [{ name: 'search', description: 'Search docs', input_schema: { type: 'object' } }],
|
|
system: 'You are helpful.',
|
|
messages: [
|
|
{ role: 'user', content: [{ type: 'text', text: 'Find release notes' }] },
|
|
{
|
|
role: 'assistant',
|
|
content: [{ type: 'tool_use', id: 'toolu_1', name: 'search', input: { q: 'release' } }],
|
|
},
|
|
{
|
|
role: 'user',
|
|
content: [
|
|
{
|
|
type: 'tool_result',
|
|
tool_use_id: 'toolu_1',
|
|
content: [{ type: 'text', text: 'v7.53.0' }],
|
|
},
|
|
{ type: 'text', text: 'Summarize it.' },
|
|
],
|
|
},
|
|
],
|
|
});
|
|
|
|
expect(translated.model).toBe('claude-sonnet-4.5');
|
|
expect(translated.stream).toBe(true);
|
|
expect(translated.reasoning_effort).toBe('high');
|
|
expect(translated.messages).toEqual([
|
|
{ role: 'system', content: 'You are helpful.' },
|
|
{ role: 'user', content: 'Find release notes' },
|
|
{
|
|
role: 'assistant',
|
|
content: '',
|
|
tool_calls: [
|
|
{
|
|
id: 'toolu_1',
|
|
type: 'function',
|
|
function: { name: 'search', arguments: '{"q":"release"}' },
|
|
},
|
|
],
|
|
},
|
|
{ role: 'tool', tool_call_id: 'toolu_1', content: 'v7.53.0' },
|
|
{ role: 'user', content: 'Summarize it.' },
|
|
]);
|
|
});
|
|
|
|
it('rejects unsupported content blocks', () => {
|
|
expect(() =>
|
|
translateAnthropicRequest({
|
|
messages: [{ role: 'user', content: [{ type: 'image' }] }],
|
|
})
|
|
).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' },
|
|
]);
|
|
});
|
|
|
|
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', () => {
|
|
it('converts OpenAI JSON into Anthropic message JSON', async () => {
|
|
const response = new Response(
|
|
JSON.stringify({
|
|
id: 'chatcmpl_1',
|
|
model: 'claude-sonnet-4.5',
|
|
choices: [
|
|
{
|
|
index: 0,
|
|
message: {
|
|
role: 'assistant',
|
|
content: 'Here is the result.',
|
|
reasoning_content: 'Need to call the tool first.',
|
|
tool_calls: [
|
|
{
|
|
id: 'toolu_2',
|
|
type: 'function',
|
|
function: { name: 'search', arguments: '{"q":"cursor daemon"}' },
|
|
},
|
|
],
|
|
},
|
|
finish_reason: 'tool_calls',
|
|
},
|
|
],
|
|
usage: { prompt_tokens: 12, completion_tokens: 4, total_tokens: 16 },
|
|
}),
|
|
{
|
|
status: 200,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
}
|
|
);
|
|
|
|
const transformed = await createAnthropicProxyResponse(response);
|
|
const body = (await transformed.json()) as {
|
|
type: string;
|
|
model: string;
|
|
stop_reason: string;
|
|
content: Array<{
|
|
type: string;
|
|
text?: string;
|
|
thinking?: string;
|
|
name?: string;
|
|
input?: Record<string, unknown>;
|
|
}>;
|
|
};
|
|
|
|
expect(body.type).toBe('message');
|
|
expect(body.model).toBe('claude-sonnet-4.5');
|
|
expect(body.stop_reason).toBe('tool_use');
|
|
expect(body.content.map((block) => block.type)).toEqual(['thinking', 'text', 'tool_use']);
|
|
expect(body.content[0]?.thinking).toContain('Need to call the tool first');
|
|
expect(body.content[2]?.name).toBe('search');
|
|
expect(body.content[2]?.input).toEqual({ q: 'cursor daemon' });
|
|
});
|
|
|
|
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',
|
|
'data: {"id":"chatcmpl_2","object":"chat.completion.chunk","created":1,"model":"claude-sonnet-4.5","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":5,"completion_tokens":1,"total_tokens":6}}\n\n',
|
|
'data: [DONE]\n\n',
|
|
].join('');
|
|
|
|
const transformed = await createAnthropicProxyResponse(
|
|
new Response(openAiSse, {
|
|
status: 200,
|
|
headers: { 'Content-Type': 'text/event-stream' },
|
|
})
|
|
);
|
|
|
|
const body = await transformed.text();
|
|
expect(body).toContain('event: message_start');
|
|
expect(body).toContain('event: content_block_start');
|
|
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');
|
|
});
|
|
});
|