Skip to content

Commit 90abe03

Browse files
author
AlgerMusic Build Bot
committed
Add Anthropic Custom provider
1 parent f63f7a9 commit 90abe03

11 files changed

Lines changed: 558 additions & 11 deletions

File tree

‎packages/types/src/provider-settings.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export const isInternalProvider = (key: string): key is InternalProvider =>
8585
* Custom providers are completely configurable within Roo Code settings.
8686
*/
8787

88-
export const customProviders = ["openai"] as const
88+
export const customProviders = ["openai", "anthropic-custom"] as const
8989

9090
export type CustomProvider = (typeof customProviders)[number]
9191

@@ -262,6 +262,15 @@ const openAiSchema = baseProviderSettingsSchema.extend({
262262
openAiHeaders: z.record(z.string(), z.string()).optional(),
263263
})
264264

265+
const anthropicCustomSchema = baseProviderSettingsSchema.extend({
266+
anthropicCustomBaseUrl: z.string().optional(),
267+
anthropicCustomApiKey: z.string().optional(),
268+
anthropicCustomModelId: z.string().optional(),
269+
anthropicCustomModelInfo: modelInfoSchema.nullish(),
270+
anthropicCustomStreamingEnabled: z.boolean().optional(),
271+
anthropicCustomHeaders: z.record(z.string(), z.string()).optional(),
272+
})
273+
265274
const ollamaSchema = baseProviderSettingsSchema.extend({
266275
ollamaModelId: z.string().optional(),
267276
ollamaBaseUrl: z.string().optional(),
@@ -427,6 +436,7 @@ export const providerSettingsSchemaDiscriminated = z.discriminatedUnion("apiProv
427436
bedrockSchema.merge(z.object({ apiProvider: z.literal("bedrock") })),
428437
vertexSchema.merge(z.object({ apiProvider: z.literal("vertex") })),
429438
openAiSchema.merge(z.object({ apiProvider: z.literal("openai") })),
439+
anthropicCustomSchema.merge(z.object({ apiProvider: z.literal("anthropic-custom") })),
430440
ollamaSchema.merge(z.object({ apiProvider: z.literal("ollama") })),
431441
vsCodeLmSchema.merge(z.object({ apiProvider: z.literal("vscode-lm") })),
432442
lmStudioSchema.merge(z.object({ apiProvider: z.literal("lmstudio") })),
@@ -463,6 +473,7 @@ export const providerSettingsSchema = z.object({
463473
...bedrockSchema.shape,
464474
...vertexSchema.shape,
465475
...openAiSchema.shape,
476+
...anthropicCustomSchema.shape,
466477
...ollamaSchema.shape,
467478
...vsCodeLmSchema.shape,
468479
...lmStudioSchema.shape,
@@ -512,6 +523,7 @@ export const modelIdKeys = [
512523
"apiModelId",
513524
"openRouterModelId",
514525
"openAiModelId",
526+
"anthropicCustomModelId",
515527
"ollamaModelId",
516528
"lmStudioModelId",
517529
"lmStudioDraftModelId",
@@ -575,7 +587,7 @@ export const modelIdKeysByProvider: Record<TypicalProvider, ModelIdKey> = {
575587
*/
576588

577589
// Providers that use Anthropic-style API protocol.
578-
export const ANTHROPIC_STYLE_PROVIDERS: ProviderName[] = ["anthropic", "bedrock", "minimax"]
590+
export const ANTHROPIC_STYLE_PROVIDERS: ProviderName[] = ["anthropic", "anthropic-custom", "bedrock", "minimax"]
579591

580592
export const getApiProtocol = (provider: ProviderName | undefined, modelId?: string): "anthropic" | "openai" => {
581593
if (provider && ANTHROPIC_STYLE_PROVIDERS.includes(provider)) {
@@ -615,7 +627,7 @@ export const getApiProtocol = (provider: ProviderName | undefined, modelId?: str
615627
*/
616628

617629
export const MODELS_BY_PROVIDER: Record<
618-
Exclude<ProviderName, "fake-ai" | "gemini-cli" | "openai">,
630+
Exclude<ProviderName, "fake-ai" | "gemini-cli" | "openai" | "anthropic-custom">,
619631
{ id: ProviderName; label: string; models: string[] }
620632
> = {
621633
anthropic: {

‎src/api/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ export function buildApiHandler(configuration: ProviderSettings): ApiHandler {
133133

134134
switch (apiProvider) {
135135
case "anthropic":
136+
case "anthropic-custom":
136137
return new AnthropicHandler(options)
137138
case "openrouter":
138139
return new OpenRouterHandler(options)

‎src/api/providers/anthropic.ts‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
type AnthropicModelId,
99
anthropicDefaultModelId,
1010
anthropicModels,
11+
openAiModelInfoSaneDefaults,
1112
ANTHROPIC_DEFAULT_MAX_TOKENS,
1213
ApiProviderError,
1314
} from "@roo-code/types"
@@ -38,12 +39,17 @@ export class AnthropicHandler extends BaseProvider implements SingleCompletionHa
3839
super()
3940
this.options = options
4041

42+
const baseURL = this.options.anthropicCustomBaseUrl || this.options.anthropicBaseUrl || undefined
43+
const apiKey = this.options.anthropicCustomApiKey || this.options.apiKey
4144
const apiKeyFieldName =
42-
this.options.anthropicBaseUrl && this.options.anthropicUseAuthToken ? "authToken" : "apiKey"
45+
baseURL && this.options.anthropicUseAuthToken && !this.options.anthropicCustomApiKey
46+
? "authToken"
47+
: "apiKey"
4348

4449
this.client = new Anthropic({
45-
baseURL: this.options.anthropicBaseUrl || undefined,
46-
[apiKeyFieldName]: this.options.apiKey,
50+
baseURL,
51+
[apiKeyFieldName]: apiKey,
52+
defaultHeaders: this.options.anthropicCustomHeaders,
4753
timeout: this.timeoutMs,
4854
})
4955
}
@@ -352,9 +358,14 @@ export class AnthropicHandler extends BaseProvider implements SingleCompletionHa
352358
}
353359

354360
getModel() {
355-
const modelId = this.options.apiModelId
356-
const id = modelId && modelId in anthropicModels ? (modelId as AnthropicModelId) : anthropicDefaultModelId
357-
let info: ModelInfo = anthropicModels[id]
361+
const customModelId = this.options.anthropicCustomModelId
362+
const modelId = customModelId || this.options.apiModelId
363+
const id =
364+
customModelId ||
365+
(modelId && modelId in anthropicModels ? (modelId as AnthropicModelId) : anthropicDefaultModelId)
366+
let info: ModelInfo = customModelId
367+
? this.options.anthropicCustomModelInfo || openAiModelInfoSaneDefaults
368+
: anthropicModels[id as AnthropicModelId]
358369

359370
// If 1M context beta is enabled for supported models, update the model info
360371
if (

‎src/shared/ProfileValidator.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ export class ProfileValidator {
5353
switch (profile.apiProvider) {
5454
case "openai":
5555
return profile.openAiModelId
56+
case "anthropic-custom":
57+
return profile.anthropicCustomModelId
5658
case "anthropic":
5759
case "openai-native":
5860
case "bedrock":

‎webview-ui/src/components/settings/ApiOptions.tsx‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import {
4848

4949
import {
5050
Anthropic,
51+
AnthropicCustom,
5152
Baseten,
5253
Bedrock,
5354
DeepSeek,
@@ -468,6 +469,16 @@ const ApiOptions = ({
468469
/>
469470
)}
470471

472+
{selectedProvider === "anthropic-custom" && (
473+
<AnthropicCustom
474+
apiConfiguration={apiConfiguration}
475+
setApiConfigurationField={setApiConfigurationField}
476+
organizationAllowList={organizationAllowList}
477+
modelValidationError={modelValidationError}
478+
simplifySettings={fromWelcomeView}
479+
/>
480+
)}
481+
471482
{selectedProvider === "openai-codex" && (
472483
<OpenAICodex
473484
apiConfiguration={apiConfiguration}

‎webview-ui/src/components/settings/ModelPicker.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type ModelIdKey = keyof Pick<
3232
| "requestyModelId"
3333
| "unboundModelId"
3434
| "openAiModelId"
35+
| "anthropicCustomModelId"
3536
| "litellmModelId"
3637
| "vercelAiGatewayModelId"
3738
| "opencodeGoModelId"

‎webview-ui/src/components/settings/constants.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export const MODELS_BY_PROVIDER: Partial<Record<ProviderName, Record<string, Mod
4343
export const PROVIDERS = [
4444
{ value: "openrouter", label: "OpenRouter", proxy: false },
4545
{ value: "anthropic", label: "Anthropic", proxy: false },
46+
{ value: "anthropic-custom", label: "Anthropic Custom", proxy: true },
4647
{ value: "gemini", label: "Google Gemini", proxy: false },
4748
{ value: "deepseek", label: "DeepSeek", proxy: false },
4849
{ value: "moonshot", label: "Moonshot", proxy: false },

0 commit comments

Comments
 (0)