mirror of
https://github.com/RooVetGit/Roo-Code.git
synced 2026-08-28 05:27:24 +00:00
Some checks are pending
Code QA Roo Code / check-translations (push) Waiting to run
Code QA Roo Code / knip (push) Waiting to run
Code QA Roo Code / compile (push) Waiting to run
Code QA Roo Code / platform-unit-test (ubuntu-latest) (push) Waiting to run
Code QA Roo Code / platform-unit-test (windows-latest) (push) Waiting to run
CodeQL Advanced / Analyze (javascript-typescript) (push) Waiting to run
Nightly Publish / publish-nightly (push) Waiting to run
Deploy roocode.com / check-secrets (push) Waiting to run
Deploy roocode.com / deploy (push) Blocked by required conditions
* Remove Roo Code Cloud and evals * Remove unused onboarding and web helper files * Update ChatView welcome tests after cloud removal
180 lines
5.7 KiB
TypeScript
180 lines
5.7 KiB
TypeScript
import { Anthropic } from "@anthropic-ai/sdk"
|
|
import OpenAI from "openai"
|
|
|
|
import { isRetiredProvider, type ProviderSettings, type ModelInfo } from "@roo-code/types"
|
|
|
|
import { ApiStream } from "./transform/stream"
|
|
|
|
import {
|
|
AnthropicHandler,
|
|
AwsBedrockHandler,
|
|
OpenRouterHandler,
|
|
PoeHandler,
|
|
VertexHandler,
|
|
AnthropicVertexHandler,
|
|
OpenAiHandler,
|
|
OpenAiCodexHandler,
|
|
LmStudioHandler,
|
|
GeminiHandler,
|
|
OpenAiNativeHandler,
|
|
DeepSeekHandler,
|
|
MoonshotHandler,
|
|
MistralHandler,
|
|
VsCodeLmHandler,
|
|
RequestyHandler,
|
|
UnboundHandler,
|
|
FakeAIHandler,
|
|
XAIHandler,
|
|
LiteLLMHandler,
|
|
QwenCodeHandler,
|
|
SambaNovaHandler,
|
|
ZAiHandler,
|
|
FireworksHandler,
|
|
VercelAiGatewayHandler,
|
|
MiniMaxHandler,
|
|
BasetenHandler,
|
|
} from "./providers"
|
|
import { NativeOllamaHandler } from "./providers/native-ollama"
|
|
|
|
export interface SingleCompletionHandler {
|
|
completePrompt(prompt: string): Promise<string>
|
|
}
|
|
|
|
export interface ApiHandlerCreateMessageMetadata {
|
|
/**
|
|
* Task ID used for tracking and provider-specific features:
|
|
* - Roo: Sent as X-Roo-Task-ID header
|
|
* - Requesty: Sent as trace_id
|
|
*/
|
|
taskId: string
|
|
/**
|
|
* Current mode slug for provider-specific tracking:
|
|
* - Requesty: Sent in extra metadata
|
|
*/
|
|
mode?: string
|
|
suppressPreviousResponseId?: boolean
|
|
/**
|
|
* Controls whether the response should be stored for 30 days in OpenAI's Responses API.
|
|
* When true (default), responses are stored and can be referenced in future requests
|
|
* using the previous_response_id for efficient conversation continuity.
|
|
* Set to false to opt out of response storage for privacy or compliance reasons.
|
|
* @default true
|
|
*/
|
|
store?: boolean
|
|
/**
|
|
* Optional array of tool definitions to pass to the model.
|
|
* For OpenAI-compatible providers, these are ChatCompletionTool definitions.
|
|
*/
|
|
tools?: OpenAI.Chat.ChatCompletionTool[]
|
|
/**
|
|
* Controls which (if any) tool is called by the model.
|
|
* Can be "none", "auto", "required", or a specific tool choice.
|
|
*/
|
|
tool_choice?: OpenAI.Chat.ChatCompletionCreateParams["tool_choice"]
|
|
/**
|
|
* Controls whether the model can return multiple tool calls in a single response.
|
|
* When true (default), parallel tool calls are enabled (OpenAI's parallel_tool_calls=true).
|
|
* When false, only one tool call is returned per response.
|
|
*/
|
|
parallelToolCalls?: boolean
|
|
/**
|
|
* Optional array of tool names that the model is allowed to call.
|
|
* When provided, all tool definitions are passed to the model (so it can reference
|
|
* historical tool calls), but only the specified tools can actually be invoked.
|
|
* This is used when switching modes to prevent model errors from missing tool
|
|
* definitions while still restricting callable tools to the current mode's permissions.
|
|
* Only applies to providers that support function calling restrictions (e.g., Gemini).
|
|
*/
|
|
allowedFunctionNames?: string[]
|
|
}
|
|
|
|
export interface ApiHandler {
|
|
createMessage(
|
|
systemPrompt: string,
|
|
messages: Anthropic.Messages.MessageParam[],
|
|
metadata?: ApiHandlerCreateMessageMetadata,
|
|
): ApiStream
|
|
|
|
getModel(): { id: string; info: ModelInfo }
|
|
|
|
/**
|
|
* Counts tokens for content blocks
|
|
* All providers extend BaseProvider which provides a default tiktoken implementation,
|
|
* but they can override this to use their native token counting endpoints
|
|
*
|
|
* @param content The content to count tokens for
|
|
* @returns A promise resolving to the token count
|
|
*/
|
|
countTokens(content: Array<Anthropic.Messages.ContentBlockParam>): Promise<number>
|
|
}
|
|
|
|
export function buildApiHandler(configuration: ProviderSettings): ApiHandler {
|
|
const { apiProvider, ...options } = configuration
|
|
|
|
if (apiProvider && isRetiredProvider(apiProvider)) {
|
|
throw new Error(
|
|
`Sorry, this provider is no longer supported. We saw very few Roo users actually using it and we need to reduce the surface area of our codebase so we can keep shipping fast and serving our community well in this space. It was a really hard decision but it lets us focus on what matters most to you. It sucks, we know.\n\nPlease select a different provider in your API profile settings.`,
|
|
)
|
|
}
|
|
|
|
switch (apiProvider) {
|
|
case "anthropic":
|
|
return new AnthropicHandler(options)
|
|
case "openrouter":
|
|
return new OpenRouterHandler(options)
|
|
case "bedrock":
|
|
return new AwsBedrockHandler(options)
|
|
case "vertex":
|
|
return options.apiModelId?.startsWith("claude")
|
|
? new AnthropicVertexHandler(options)
|
|
: new VertexHandler(options)
|
|
case "openai":
|
|
return new OpenAiHandler(options)
|
|
case "ollama":
|
|
return new NativeOllamaHandler(options)
|
|
case "lmstudio":
|
|
return new LmStudioHandler(options)
|
|
case "gemini":
|
|
return new GeminiHandler(options)
|
|
case "openai-codex":
|
|
return new OpenAiCodexHandler(options)
|
|
case "openai-native":
|
|
return new OpenAiNativeHandler(options)
|
|
case "deepseek":
|
|
return new DeepSeekHandler(options)
|
|
case "qwen-code":
|
|
return new QwenCodeHandler(options)
|
|
case "moonshot":
|
|
return new MoonshotHandler(options)
|
|
case "vscode-lm":
|
|
return new VsCodeLmHandler(options)
|
|
case "mistral":
|
|
return new MistralHandler(options)
|
|
case "requesty":
|
|
return new RequestyHandler(options)
|
|
case "unbound":
|
|
return new UnboundHandler(options)
|
|
case "fake-ai":
|
|
return new FakeAIHandler(options)
|
|
case "xai":
|
|
return new XAIHandler(options)
|
|
case "litellm":
|
|
return new LiteLLMHandler(options)
|
|
case "sambanova":
|
|
return new SambaNovaHandler(options)
|
|
case "zai":
|
|
return new ZAiHandler(options)
|
|
case "fireworks":
|
|
return new FireworksHandler(options)
|
|
case "vercel-ai-gateway":
|
|
return new VercelAiGatewayHandler(options)
|
|
case "minimax":
|
|
return new MiniMaxHandler(options)
|
|
case "baseten":
|
|
return new BasetenHandler(options)
|
|
case "poe":
|
|
return new PoeHandler(options)
|
|
default:
|
|
return new AnthropicHandler(options)
|
|
}
|
|
}
|