Files
9router/open-sse/services/provider.js
T
decoluaandCursor 3a26d5fb40 refactor(open-sse): remove dead buildProviderUrl/Headers path (A1)
These translate-path builders had no runtime consumers: the translator route
uses executor.buildUrl/buildHeaders, and the barrel re-exports were unused.
Removing them eliminates the parallel URL/header build path (single source of
truth = executors). Drop their private helpers and the now-unused clineAuth
import. Golden executor snapshots + gate: no regression.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-13 17:34:39 +07:00

161 lines
5.5 KiB
JavaScript

import { PROVIDERS } from "../config/providers.js";
const OPENAI_COMPATIBLE_PREFIX = "openai-compatible-";
const OPENAI_COMPATIBLE_DEFAULTS = {
baseUrl: "https://api.openai.com/v1",
};
const ANTHROPIC_COMPATIBLE_PREFIX = "anthropic-compatible-";
const ANTHROPIC_COMPATIBLE_DEFAULTS = {
baseUrl: "https://api.anthropic.com/v1",
};
function isOpenAICompatible(provider) {
return typeof provider === "string" && provider.startsWith(OPENAI_COMPATIBLE_PREFIX);
}
function isAnthropicCompatible(provider) {
return typeof provider === "string" && provider.startsWith(ANTHROPIC_COMPATIBLE_PREFIX);
}
function getOpenAICompatibleType(provider) {
if (!isOpenAICompatible(provider)) return "chat";
return provider.includes("responses") ? "responses" : "chat";
}
// Detect request format from body structure
export function detectFormat(body) {
// OpenAI Responses API: has input (array or string) instead of messages[]
// The Responses API accepts both input as array and input as a plain string
if (body.input && (Array.isArray(body.input) || typeof body.input === "string") && !body.messages) {
return "openai-responses";
}
// Antigravity format: Gemini wrapped in body.request
if (body.request?.contents && body.userAgent === "antigravity") {
return "antigravity";
}
// Gemini format: has contents array
if (body.contents && Array.isArray(body.contents)) {
return "gemini";
}
// OpenAI-specific indicators (check BEFORE Claude)
// These fields are OpenAI-specific and never appear in Claude format
if (
body.stream_options || // OpenAI streaming options
body.response_format || // JSON mode, etc.
body.logprobs !== undefined || // Log probabilities
body.top_logprobs !== undefined ||
body.n !== undefined || // Number of completions
body.presence_penalty !== undefined || // Penalties
body.frequency_penalty !== undefined ||
body.logit_bias || // Token biasing
body.user // User identifier
) {
return "openai";
}
// Claude format: messages with content as array of objects with type
// Claude requires content to be array with specific structure
if (body.messages && Array.isArray(body.messages)) {
const firstMsg = body.messages[0];
// If content is array, check if it follows Claude structure
if (firstMsg?.content && Array.isArray(firstMsg.content)) {
const firstContent = firstMsg.content[0];
// Claude format has specific types: text, image, tool_use, tool_result
// OpenAI multimodal has: text, image_url (note the difference)
if (firstContent?.type === "text" && !body.model?.includes("/")) {
// Could be Claude or OpenAI multimodal
// Check for Claude-specific fields
if (body.system || body.anthropic_version) {
return "claude";
}
// Check if image format is Claude (source.type) vs OpenAI (image_url.url)
const hasClaudeImage = firstMsg.content.some(c =>
c.type === "image" && c.source?.type === "base64"
);
const hasOpenAIImage = firstMsg.content.some(c =>
c.type === "image_url" && c.image_url?.url
);
if (hasClaudeImage) return "claude";
if (hasOpenAIImage) return "openai";
// If still unclear, check for tool format
const hasClaudeTool = firstMsg.content.some(c =>
c.type === "tool_use" || c.type === "tool_result"
);
if (hasClaudeTool) return "claude";
}
}
// If content is string, it's likely OpenAI (Claude also supports this)
// Check for other Claude-specific indicators
if (body.system !== undefined || body.anthropic_version) {
return "claude";
}
}
// Default to OpenAI format
return "openai";
}
// Get provider config
export function getProviderConfig(provider) {
if (isOpenAICompatible(provider)) {
const apiType = getOpenAICompatibleType(provider);
return {
...PROVIDERS.openai,
format: apiType === "responses" ? "openai-responses" : "openai",
baseUrl: OPENAI_COMPATIBLE_DEFAULTS.baseUrl,
};
}
if (isAnthropicCompatible(provider)) {
return {
...PROVIDERS.anthropic, // Use Anthropic defaults (header: x-api-key)
format: "claude",
baseUrl: ANTHROPIC_COMPATIBLE_DEFAULTS.baseUrl,
};
}
return PROVIDERS[provider] || PROVIDERS.openai;
}
// Get target format for provider
export function getTargetFormat(provider) {
if (isOpenAICompatible(provider)) {
return getOpenAICompatibleType(provider) === "responses" ? "openai-responses" : "openai";
}
if (isAnthropicCompatible(provider)) {
return "claude";
}
const config = getProviderConfig(provider);
return config.format || "openai";
}
// Check if last message is from user
export function isLastMessageFromUser(body) {
const messages = body.messages || body.contents;
if (!messages?.length) return true;
const lastMsg = messages[messages.length - 1];
return lastMsg?.role === "user";
}
// Check if request has thinking config
export function hasThinkingConfig(body) {
return !!(body.reasoning_effort || body.thinking?.type === "enabled");
}
// Normalize thinking config based on last message role
// - If lastMessage is not user → remove thinking config
// - If lastMessage is user AND has thinking config → keep it (force enable)
export function normalizeThinkingConfig(body) {
if (!isLastMessageFromUser(body)) {
delete body.reasoning_effort;
delete body.thinking;
}
return body;
}