chore: add Gemini 3.1 Pro models to provider configurations

This commit is contained in:
decolua
2026-02-22 15:20:24 +07:00
parent 930e917092
commit a5eb5a864e
9 changed files with 250 additions and 158 deletions

View File

@@ -1,97 +1,58 @@
/**
* OpenAI to Cursor Request Translator
* Converts OpenAI messages to Cursor simple format
* - assistant tool_calls → kept as-is (Cursor generates tool calls)
* - tool results → converted to user message string
*/
import { register } from "../index.js";
import { FORMATS } from "../formats.js";
/**
* Convert OpenAI messages to Cursor format with native tool_results support
* - system → user with [System Instructions] prefix
* - tool → accumulate into tool_results array for next user/assistant message
* - assistant with tool_calls → keep tool_calls structure (Cursor supports it natively)
*/
function extractContent(content) {
if (typeof content === "string") return content;
if (Array.isArray(content)) {
return content.filter(p => p.type === "text").map(p => p.text).join("");
}
return "";
}
function convertMessages(messages) {
const result = [];
let pendingToolResults = [];
for (let i = 0; i < messages.length; i++) {
const msg = messages[i];
if (msg.role === "system") {
result.push({
role: "user",
content: `[System Instructions]\n${msg.content}`
});
result.push({ role: "user", content: `[System Instructions]\n${msg.content}` });
continue;
}
if (msg.role === "user") {
result.push({ role: "user", content: extractContent(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";
// Strip system-reminder tags injected by Claude Code
const raw = extractContent(msg.content) || "";
const toolContent = raw.replace(/<system-reminder>[\s\S]*?<\/system-reminder>/g, "").trim();
// Find matching tool name from previous assistant message
const prevMsg = result[result.length - 1];
const toolName = prevMsg?.tool_calls?.[0]?.function?.name || "";
const toolCallId = msg.tool_call_id || "";
// Accumulate tool result
pendingToolResults.push({
tool_call_id: toolCallId,
name: toolName,
index: pendingToolResults.length,
raw_args: toolContent
result.push({
role: "user",
content: `<tool_result>\n<tool_name>${toolName}</tool_name>\n<tool_call_id>${toolCallId}</tool_call_id>\n<result>${toolContent}</result>\n</tool_result>`
});
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;
}
}
}
// Keep tool_calls structure for assistant messages
if (msg.role === "assistant" && msg.tool_calls && msg.tool_calls.length > 0) {
const assistantMsg = { role: "assistant" };
if (content) {
assistantMsg.content = content;
}
assistantMsg.tool_calls = msg.tool_calls;
// Attach pending tool results to assistant message with tool_calls
if (pendingToolResults.length > 0) {
assistantMsg.tool_results = pendingToolResults;
pendingToolResults = [];
}
result.push(assistantMsg);
} else if (content || pendingToolResults.length > 0) {
const msgObj = {
role: msg.role,
content: content || ""
};
// Attach pending tool results to this message
if (pendingToolResults.length > 0) {
msgObj.tool_results = pendingToolResults;
pendingToolResults = [];
}
result.push(msgObj);
if (msg.role === "assistant") {
const content = extractContent(msg.content) || "";
if (msg.tool_calls && msg.tool_calls.length > 0) {
// Strip `index` field — not needed in history, may confuse Cursor
const tool_calls = msg.tool_calls.map(({ index, ...tc }) => tc);
result.push({ role: "assistant", content, tool_calls });
} else if (content) {
result.push({ role: "assistant", content });
}
}
}
@@ -99,16 +60,14 @@ function convertMessages(messages) {
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 || []);
// Strip fields irrelevant to Cursor (OpenAI/Anthropic-specific)
const { user, metadata, tool_choice, stream_options, system, ...rest } = body;
return {
...body,
messages
...rest,
messages,
max_tokens: 32000
};
}