feat(cursor): Add cursor Provider

This commit is contained in:
decolua
2026-02-05 11:06:20 +07:00
parent abaeb22863
commit 0a026c7af6
16 changed files with 1113 additions and 937 deletions

View File

@@ -8,6 +8,7 @@ export const FORMATS = {
GEMINI_CLI: "gemini-cli",
CODEX: "codex",
ANTIGRAVITY: "antigravity",
KIRO: "kiro"
KIRO: "kiro",
CURSOR: "cursor"
};

View File

@@ -34,6 +34,7 @@ function ensureInitialized() {
require("./request/openai-to-gemini.js");
require("./request/openai-responses.js");
require("./request/openai-to-kiro.js");
require("./request/openai-to-cursor.js");
// Response translators
require("./response/claude-to-openai.js");
@@ -41,6 +42,7 @@ function ensureInitialized() {
require("./response/gemini-to-openai.js");
require("./response/openai-responses.js");
require("./response/kiro-to-openai.js");
require("./response/cursor-to-openai.js");
}
// Translate request: source -> openai -> target

View File

@@ -0,0 +1,96 @@
/**
* OpenAI to Cursor Request Translator
* Converts OpenAI messages to Cursor simple format
*/
import { register } from "../index.js";
import { FORMATS } from "../formats.js";
/**
* Convert OpenAI messages to Cursor simple format
* - system → user with [System Instructions] prefix
* - tool → user with [Tool Result: name] prefix
* - assistant with tool_calls → append [Calling tool: name with args: {...}] to content
*/
function convertMessages(messages) {
const result = [];
for (const msg of messages) {
if (msg.role === "system") {
result.push({
role: "user",
content: `[System Instructions]\n${msg.content}`
});
continue;
}
if (msg.role === "tool") {
let toolContent = "";
if (typeof msg.content === "string") {
toolContent = msg.content;
} else if (Array.isArray(msg.content)) {
for (const part of msg.content) {
if (part.type === "text") {
toolContent += part.text;
}
}
}
const toolName = msg.name || "tool";
result.push({
role: "user",
content: `[Tool Result: ${toolName}]\n${toolContent}`
});
continue;
}
if (msg.role === "user" || msg.role === "assistant") {
let content = "";
if (typeof msg.content === "string") {
content = msg.content;
} else if (Array.isArray(msg.content)) {
for (const part of msg.content) {
if (part.type === "text") {
content += part.text;
}
}
}
if (msg.role === "assistant" && msg.tool_calls && msg.tool_calls.length > 0) {
if (content) {
result.push({ role: "assistant", content });
}
const toolCallsText = msg.tool_calls.map(tc => {
const funcName = tc.function?.name || "unknown";
const funcArgs = tc.function?.arguments || "{}";
return `[Calling tool: ${funcName} with args: ${funcArgs}]`;
}).join("\n");
result.push({
role: "assistant",
content: toolCallsText
});
} else if (content) {
result.push({ role: msg.role, content });
}
}
}
return result;
}
/**
* Transform OpenAI request to Cursor format
* Returns modified body with converted messages
*/
export function buildCursorRequest(model, body, stream, credentials) {
const messages = convertMessages(body.messages || []);
return {
...body,
messages
};
}
register(FORMATS.OPENAI, FORMATS.CURSOR, buildCursorRequest, null);

View File

@@ -0,0 +1,30 @@
/**
* Cursor to OpenAI Response Translator
* CursorExecutor already emits OpenAI format - this is a passthrough
*/
import { register } from "../index.js";
import { FORMATS } from "../formats.js";
/**
* Convert Cursor response to OpenAI format
* Since CursorExecutor.transformProtobufToSSE/JSON already emits OpenAI chunks,
* this is a passthrough translator (similar to Kiro pattern)
*/
export function convertCursorToOpenAI(chunk, state) {
if (!chunk) return null;
// If chunk is already in OpenAI format (from executor transform), return as-is
if (chunk.object === "chat.completion.chunk" && chunk.choices) {
return chunk;
}
// If chunk is a completion object (non-streaming), return as-is
if (chunk.object === "chat.completion" && chunk.choices) {
return chunk;
}
// Fallback: return chunk as-is (should not reach here)
return chunk;
}
register(FORMATS.CURSOR, FORMATS.OPENAI, null, convertCursorToOpenAI);

View File

@@ -1,205 +0,0 @@
import { register } from "../index.js";
import { FORMATS } from "../formats.js";
// Prefix for Claude OAuth tool names (must match request translator)
const CLAUDE_OAUTH_TOOL_PREFIX = "proxy_";
// Helper: stop thinking block if started
function stopThinkingBlock(state, results) {
if (!state.thinkingBlockStarted) return;
results.push({
type: "content_block_stop",
index: state.thinkingBlockIndex
});
state.thinkingBlockStarted = false;
}
// Helper: stop text block if started
function stopTextBlock(state, results) {
if (!state.textBlockStarted || state.textBlockClosed) return;
state.textBlockClosed = true;
results.push({
type: "content_block_stop",
index: state.textBlockIndex
});
state.textBlockStarted = false;
}
// Convert OpenAI stream chunk to Claude format
function openaiToClaudeResponse(chunk, state) {
if (!chunk || !chunk.choices?.[0]) return null;
const results = [];
const choice = chunk.choices[0];
const delta = choice.delta;
// First chunk - ALWAYS send message_start first
if (!state.messageStartSent) {
state.messageStartSent = true;
state.messageId = chunk.id?.replace("chatcmpl-", "") || `msg_${Date.now()}`;
if (!state.messageId || state.messageId === "chat" || state.messageId.length < 8) {
state.messageId = chunk.extend_fields?.requestId ||
chunk.extend_fields?.traceId ||
`msg_${Date.now()}`;
}
state.model = chunk.model || "unknown";
state.nextBlockIndex = 0;
results.push({
type: "message_start",
message: {
id: state.messageId,
type: "message",
role: "assistant",
model: state.model,
content: [],
stop_reason: null,
stop_sequence: null,
usage: { input_tokens: 0, output_tokens: 0 }
}
});
}
// Handle reasoning_content (thinking) - GLM, DeepSeek, etc.
const reasoningContent = delta?.reasoning_content || delta?.reasoning;
if (reasoningContent) {
stopTextBlock(state, results);
if (!state.thinkingBlockStarted) {
state.thinkingBlockIndex = state.nextBlockIndex++;
state.thinkingBlockStarted = true;
results.push({
type: "content_block_start",
index: state.thinkingBlockIndex,
content_block: { type: "thinking", thinking: "" }
});
}
results.push({
type: "content_block_delta",
index: state.thinkingBlockIndex,
delta: { type: "thinking_delta", thinking: reasoningContent }
});
}
// Handle regular content
if (delta?.content) {
stopThinkingBlock(state, results);
if (!state.textBlockStarted) {
state.textBlockIndex = state.nextBlockIndex++;
state.textBlockStarted = true;
state.textBlockClosed = false;
results.push({
type: "content_block_start",
index: state.textBlockIndex,
content_block: { type: "text", text: "" }
});
}
results.push({
type: "content_block_delta",
index: state.textBlockIndex,
delta: { type: "text_delta", text: delta.content }
});
}
// Tool calls - accumulate arguments instead of emitting immediately
if (delta?.tool_calls) {
for (const tc of delta.tool_calls) {
const idx = tc.index ?? 0;
if (tc.id) {
stopThinkingBlock(state, results);
stopTextBlock(state, results);
const toolBlockIndex = state.nextBlockIndex++;
// Strip prefix from tool name for response
let toolName = tc.function?.name || "";
if (toolName.startsWith(CLAUDE_OAUTH_TOOL_PREFIX)) {
toolName = toolName.slice(CLAUDE_OAUTH_TOOL_PREFIX.length);
}
// Initialize accumulator for this tool
state.toolCalls.set(idx, {
id: tc.id,
name: toolName,
blockIndex: toolBlockIndex,
arguments: "", // Accumulate arguments here
startEmitted: false // Track if content_block_start sent
});
}
// Accumulate arguments instead of emitting immediately
if (tc.function?.arguments) {
const toolInfo = state.toolCalls.get(idx);
if (toolInfo) {
toolInfo.arguments += tc.function.arguments;
}
}
}
}
// Finish - emit all accumulated tools in correct order
if (choice.finish_reason) {
stopThinkingBlock(state, results);
stopTextBlock(state, results);
// STEP 1: Emit all content_block_start for tools (like CLIProxyAPIPlus)
const sortedTools = Array.from(state.toolCalls.entries()).sort((a, b) => a[0] - b[0]);
for (const [, toolInfo] of sortedTools) {
if (!toolInfo.startEmitted) {
results.push({
type: "content_block_start",
index: toolInfo.blockIndex,
content_block: {
type: "tool_use",
id: toolInfo.id,
name: toolInfo.name,
input: {}
}
});
toolInfo.startEmitted = true;
}
}
// STEP 2: Emit input_json_delta + content_block_stop for each tool
for (const [, toolInfo] of sortedTools) {
if (toolInfo.arguments) {
results.push({
type: "content_block_delta",
index: toolInfo.blockIndex,
delta: { type: "input_json_delta", partial_json: toolInfo.arguments }
});
}
results.push({
type: "content_block_stop",
index: toolInfo.blockIndex
});
}
results.push({
type: "message_delta",
delta: { stop_reason: convertFinishReason(choice.finish_reason) },
usage: { output_tokens: 0 }
});
results.push({ type: "message_stop" });
}
return results.length > 0 ? results : null;
}
// Convert OpenAI finish_reason to Claude stop_reason
function convertFinishReason(reason) {
switch (reason) {
case "stop": return "end_turn";
case "length": return "max_tokens";
case "tool_calls": return "tool_use";
default: return "end_turn";
}
}
// Register
register(FORMATS.OPENAI, FORMATS.CLAUDE, null, openaiToClaudeResponse);