feat(cursor): harden daemon integration and model discovery

This commit is contained in:
Tam Nhu Tran committed 2026-02-17 04:09:06 +07:00
1 parent 4bd6877dcf
commit c639cefa7b
11 files changed
+953 -252

No files matched your search

+7
View File
@@ -153,6 +153,13 @@ 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 chat endpoint exists (requires auth, should not be 404)
const chatResponse = await fetch(`http://127.0.0.1:${port}/v1/chat/completions`, {
method: 'POST',
+129 -1
View File
@@ -2,7 +2,8 @@
* Unit tests for Cursor models module
*/
import { describe, it, expect } from 'bun:test';
import { describe, it, expect, beforeEach } from 'bun:test';
import * as http from 'http';
import {
DEFAULT_CURSOR_MODELS,
DEFAULT_CURSOR_PORT,
@@ -11,6 +12,9 @@ import {
detectProvider,
formatModelName,
fetchModelsFromDaemon,
fetchModelsFromCursorApi,
getModelsForDaemon,
clearCursorModelsCache,
} from '../../../src/cursor/cursor-models';
describe('DEFAULT_CURSOR_MODELS', () => {
@@ -102,3 +106,127 @@ describe('fetchModelsFromDaemon', () => {
expect(models).toEqual(DEFAULT_CURSOR_MODELS);
});
});
describe('fetchModelsFromCursorApi', () => {
it('parses model list from API response', async () => {
const server = http.createServer((_req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(
JSON.stringify({
data: [
{ id: 'gpt-5.3-codex', name: 'GPT-5.3 Codex', provider: 'openai' },
{ id: 'claude-4.6-opus', name: 'Claude 4.6 Opus', provider: 'anthropic' },
],
})
);
});
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
const address = server.address();
if (!address || typeof address === 'string') {
throw new Error('Unable to resolve test server port');
}
try {
const models = await fetchModelsFromCursorApi(
{
accessToken: 'test-token-123',
machineId: '1234567890abcdef1234567890abcdef',
},
{
endpoint: `http://127.0.0.1:${address.port}/v1/models`,
timeoutMs: 2000,
}
);
expect(models).not.toBeNull();
expect(models?.[0].id).toBe('gpt-5.3-codex');
expect(models?.[1].id).toBe('claude-4.6-opus');
} finally {
await new Promise<void>((resolve) => server.close(() => resolve()));
}
});
it('returns null for non-200 responses', async () => {
const server = http.createServer((_req, res) => {
res.writeHead(403, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'forbidden' }));
});
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
const address = server.address();
if (!address || typeof address === 'string') {
throw new Error('Unable to resolve test server port');
}
try {
const models = await fetchModelsFromCursorApi(
{
accessToken: 'test-token-123',
machineId: '1234567890abcdef1234567890abcdef',
},
{
endpoint: `http://127.0.0.1:${address.port}/v1/models`,
timeoutMs: 2000,
}
);
expect(models).toBeNull();
} finally {
await new Promise<void>((resolve) => server.close(() => resolve()));
}
});
});
describe('getModelsForDaemon', () => {
beforeEach(() => {
clearCursorModelsCache();
});
it('falls back to defaults without credentials', async () => {
const models = await getModelsForDaemon();
expect(models).toEqual(DEFAULT_CURSOR_MODELS);
});
it('uses cached live models when endpoint becomes unavailable', async () => {
const liveModelId = 'test-live-model';
const server = http.createServer((_req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(
JSON.stringify({
data: [{ id: liveModelId, name: 'Live Model', provider: 'openai' }],
})
);
});
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
const address = server.address();
if (!address || typeof address === 'string') {
throw new Error('Unable to resolve test server port');
}
const endpoint = `http://127.0.0.1:${address.port}/v1/models`;
try {
const first = await getModelsForDaemon({
credentials: {
accessToken: 'test-token-123',
machineId: '1234567890abcdef1234567890abcdef',
},
endpoint,
timeoutMs: 2000,
});
expect(first[0]?.id).toBe(liveModelId);
} finally {
await new Promise<void>((resolve) => server.close(() => resolve()));
}
const second = await getModelsForDaemon({
endpoint: 'http://127.0.0.1:9/v1/models',
timeoutMs: 250,
});
expect(second[0]?.id).toBe(liveModelId);
});
});
+61
View File
@@ -531,6 +531,28 @@ describe('CursorExecutor', () => {
const body = JSON.parse(bodyText);
expect(body.error.type).toBe('rate_limit_error');
});
it('should surface reasoning_content when thinking payload is present', async () => {
const textContent = 'Final answer';
const thinkingContent = 'Internal reasoning trail';
const thinkingField = encodeField(FIELD.Thinking.TEXT, WIRE_TYPE.LEN, thinkingContent);
const chatResponse = concatArrays(
encodeField(FIELD.ChatResponse.TEXT, WIRE_TYPE.LEN, textContent),
encodeField(FIELD.ChatResponse.THINKING, WIRE_TYPE.LEN, thinkingField)
);
const responseMsg = encodeField(FIELD.Response.RESPONSE, WIRE_TYPE.LEN, chatResponse);
const frame = wrapConnectRPCFrame(responseMsg, false);
const result = executor.transformProtobufToJSON(Buffer.from(frame), 'gpt-4', {
messages: [],
});
expect(result.status).toBe(200);
const bodyText = await result.text();
const body = JSON.parse(bodyText);
expect(body.choices[0].message.content).toBe(textContent);
expect(body.choices[0].message.reasoning_content).toBe(thinkingContent);
});
});
describe('transformProtobufToSSE', () => {
@@ -572,6 +594,23 @@ describe('CursorExecutor', () => {
const body = JSON.parse(bodyText);
expect(body.error.type).toBe('rate_limit_error');
});
it('should emit reasoning_content deltas for thinking payloads', async () => {
const thinkingContent = 'Deliberate reasoning';
const thinkingField = encodeField(FIELD.Thinking.TEXT, WIRE_TYPE.LEN, thinkingContent);
const chatResponse = encodeField(FIELD.ChatResponse.THINKING, WIRE_TYPE.LEN, thinkingField);
const responseMsg = encodeField(FIELD.Response.RESPONSE, WIRE_TYPE.LEN, chatResponse);
const frame = wrapConnectRPCFrame(responseMsg, false);
const result = executor.transformProtobufToSSE(Buffer.from(frame), 'gpt-4', {
messages: [],
});
expect(result.status).toBe(200);
const bodyText = await result.text();
expect(bodyText).toContain('reasoning_content');
expect(bodyText).toContain(thinkingContent);
});
});
describe('decompressPayload error handling', () => {
@@ -676,6 +715,16 @@ function buildTextFrame(text: string): Buffer {
return buildFrame(responseMsg);
}
/**
* Helper: build a protobuf thinking response frame
*/
function buildThinkingFrame(thinking: string): Buffer {
const thinkingField = encodeField(FIELD.Thinking.TEXT, WIRE_TYPE.LEN, thinking);
const responseField = encodeField(FIELD.ChatResponse.THINKING, WIRE_TYPE.LEN, thinkingField);
const responseMsg = encodeField(FIELD.Response.RESPONSE, WIRE_TYPE.LEN, responseField);
return buildFrame(responseMsg);
}
describe('StreamingFrameParser', () => {
it('should parse a complete single frame', () => {
const parser = new StreamingFrameParser();
@@ -784,6 +833,18 @@ describe('StreamingFrameParser', () => {
}
});
it('should parse thinking frames', () => {
const parser = new StreamingFrameParser();
const frame = buildThinkingFrame('Think step by step');
const results = parser.push(frame);
expect(results.length).toBe(1);
expect(results[0].type).toBe('thinking');
if (results[0].type === 'thinking') {
expect(results[0].text).toBe('Think step by step');
}
});
it('should report hasPartial() correctly', () => {
const parser = new StreamingFrameParser();
expect(parser.hasPartial()).toBe(false);