feat(cursor): add Anthropic daemon endpoint

This commit is contained in:
Tam Nhu Tran committed 2026-03-16 14:42:15 -04:00
1 parent 1b8627367e
commit daad5d1f50
7 files changed
+561 -26

No files matched your search

@@ -0,0 +1,139 @@
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');
});
});
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');
});
});
+48 -22
View File
@@ -151,34 +151,46 @@ describe('startDaemon', () => {
const running = await isDaemonRunning(port);
expect(running).toBe(true);
// Verify models endpoint exists and is OpenAI-compatible list shape
const modelsResponse = await fetch(`http://127.0.0.1:${port}/v1/models`);
expect(modelsResponse.status).toBe(200);
const modelsJson = (await modelsResponse.json()) as { object?: string; data?: unknown[] };
expect(modelsJson.object).toBe('list');
expect(Array.isArray(modelsJson.data)).toBe(true);
// Verify models endpoint exists and is OpenAI-compatible list shape
const modelsResponse = await fetch(`http://127.0.0.1:${port}/v1/models`);
expect(modelsResponse.status).toBe(200);
const modelsJson = (await modelsResponse.json()) as { object?: string; data?: unknown[] };
expect(modelsJson.object).toBe('list');
expect(Array.isArray(modelsJson.data)).toBe(true);
// Verify chat endpoint exists (requires auth, should not be 404)
const chatResponse = await fetch(`http://127.0.0.1:${port}/v1/chat/completions`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
model: 'gpt-4.1',
messages: [{ role: 'user', content: 'hello' }],
}),
});
expect(chatResponse.status).toBe(401);
// Verify chat endpoint exists (requires auth, should not be 404)
const chatResponse = await fetch(`http://127.0.0.1:${port}/v1/chat/completions`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
model: 'gpt-4.1',
messages: [{ role: 'user', content: 'hello' }],
}),
});
expect(chatResponse.status).toBe(401);
const anthropicResponse = await fetch(`http://127.0.0.1:${port}/v1/messages`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'anthropic-version': '2023-06-01',
},
body: JSON.stringify({
model: 'claude-sonnet-4.5',
max_tokens: 256,
messages: [{ role: 'user', content: 'hello' }],
}),
});
expect(anthropicResponse.status).toBe(401);
// Stop
const stopResult = await stopDaemon();
expect(stopResult.success).toBe(true);
// Verify stopped
const stillRunning = await isDaemonRunning(port);
expect(stillRunning).toBe(false);
},
35000
);
// Verify stopped
const stillRunning = await isDaemonRunning(port);
expect(stillRunning).toBe(false);
}, 35000);
it('returns 404 for unknown routes', async () => {
const port = 10000 + Math.floor(Math.random() * 50000);
@@ -248,6 +260,20 @@ describe('startDaemon', () => {
});
expect(invalidSchema.status).toBe(400);
const invalidAnthropic = await fetch(`http://127.0.0.1:${port}/v1/messages`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'anthropic-version': '2023-06-01',
},
body: JSON.stringify({
model: 'claude-sonnet-4.5',
max_tokens: 256,
messages: [{ role: 'user', content: [{ type: 'image' }] }],
}),
});
expect(invalidAnthropic.status).toBe(400);
const oversized = await fetch(`http://127.0.0.1:${port}/v1/chat/completions`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },