Browse Source

Add anthropicBaseUrl option

Saoud Rizwan 1 year ago
parent
commit
ca4ad1d0b3

+ 4 - 1
src/api/anthropic.ts

@@ -8,7 +8,10 @@ export class AnthropicHandler implements ApiHandler {
 
 
 	constructor(options: ApiHandlerOptions) {
 	constructor(options: ApiHandlerOptions) {
 		this.options = options
 		this.options = options
-		this.client = new Anthropic({ apiKey: this.options.apiKey })
+		this.client = new Anthropic({
+			apiKey: this.options.apiKey,
+			baseURL: this.options.anthropicBaseUrl || undefined,
+		})
 	}
 	}
 
 
 	async createMessage(
 	async createMessage(

+ 6 - 0
src/providers/ClaudeDevProvider.ts

@@ -32,6 +32,7 @@ type GlobalStateKey =
 	| "openAiModelId"
 	| "openAiModelId"
 	| "ollamaModelId"
 	| "ollamaModelId"
 	| "ollamaBaseUrl"
 	| "ollamaBaseUrl"
+	| "anthropicBaseUrl"
 
 
 export class ClaudeDevProvider implements vscode.WebviewViewProvider {
 export class ClaudeDevProvider implements vscode.WebviewViewProvider {
 	public static readonly sideBarId = "claude-dev.SidebarProvider" // used in package.json as the view's id. This value cannot be changed due to how vscode caches views based on their id, and updating the id would break existing instances of the extension.
 	public static readonly sideBarId = "claude-dev.SidebarProvider" // used in package.json as the view's id. This value cannot be changed due to how vscode caches views based on their id, and updating the id would break existing instances of the extension.
@@ -326,6 +327,7 @@ export class ClaudeDevProvider implements vscode.WebviewViewProvider {
 								openAiModelId,
 								openAiModelId,
 								ollamaModelId,
 								ollamaModelId,
 								ollamaBaseUrl,
 								ollamaBaseUrl,
+								anthropicBaseUrl,
 							} = message.apiConfiguration
 							} = message.apiConfiguration
 							await this.updateGlobalState("apiProvider", apiProvider)
 							await this.updateGlobalState("apiProvider", apiProvider)
 							await this.updateGlobalState("apiModelId", apiModelId)
 							await this.updateGlobalState("apiModelId", apiModelId)
@@ -342,6 +344,7 @@ export class ClaudeDevProvider implements vscode.WebviewViewProvider {
 							await this.updateGlobalState("openAiModelId", openAiModelId)
 							await this.updateGlobalState("openAiModelId", openAiModelId)
 							await this.updateGlobalState("ollamaModelId", ollamaModelId)
 							await this.updateGlobalState("ollamaModelId", ollamaModelId)
 							await this.updateGlobalState("ollamaBaseUrl", ollamaBaseUrl)
 							await this.updateGlobalState("ollamaBaseUrl", ollamaBaseUrl)
+							await this.updateGlobalState("anthropicBaseUrl", anthropicBaseUrl)
 							this.claudeDev?.updateApi(message.apiConfiguration)
 							this.claudeDev?.updateApi(message.apiConfiguration)
 						}
 						}
 						await this.postStateToWebview()
 						await this.postStateToWebview()
@@ -656,6 +659,7 @@ export class ClaudeDevProvider implements vscode.WebviewViewProvider {
 			openAiModelId,
 			openAiModelId,
 			ollamaModelId,
 			ollamaModelId,
 			ollamaBaseUrl,
 			ollamaBaseUrl,
+			anthropicBaseUrl,
 			lastShownAnnouncementId,
 			lastShownAnnouncementId,
 			customInstructions,
 			customInstructions,
 			alwaysAllowReadOnly,
 			alwaysAllowReadOnly,
@@ -676,6 +680,7 @@ export class ClaudeDevProvider implements vscode.WebviewViewProvider {
 			this.getGlobalState("openAiModelId") as Promise<string | undefined>,
 			this.getGlobalState("openAiModelId") as Promise<string | undefined>,
 			this.getGlobalState("ollamaModelId") as Promise<string | undefined>,
 			this.getGlobalState("ollamaModelId") as Promise<string | undefined>,
 			this.getGlobalState("ollamaBaseUrl") as Promise<string | undefined>,
 			this.getGlobalState("ollamaBaseUrl") as Promise<string | undefined>,
+			this.getGlobalState("anthropicBaseUrl") as Promise<string | undefined>,
 			this.getGlobalState("lastShownAnnouncementId") as Promise<string | undefined>,
 			this.getGlobalState("lastShownAnnouncementId") as Promise<string | undefined>,
 			this.getGlobalState("customInstructions") as Promise<string | undefined>,
 			this.getGlobalState("customInstructions") as Promise<string | undefined>,
 			this.getGlobalState("alwaysAllowReadOnly") as Promise<boolean | undefined>,
 			this.getGlobalState("alwaysAllowReadOnly") as Promise<boolean | undefined>,
@@ -713,6 +718,7 @@ export class ClaudeDevProvider implements vscode.WebviewViewProvider {
 				openAiModelId,
 				openAiModelId,
 				ollamaModelId,
 				ollamaModelId,
 				ollamaBaseUrl,
 				ollamaBaseUrl,
+				anthropicBaseUrl,
 			},
 			},
 			lastShownAnnouncementId,
 			lastShownAnnouncementId,
 			customInstructions,
 			customInstructions,

+ 1 - 0
src/shared/api.ts

@@ -3,6 +3,7 @@ export type ApiProvider = "anthropic" | "openrouter" | "bedrock" | "vertex" | "o
 export interface ApiHandlerOptions {
 export interface ApiHandlerOptions {
 	apiModelId?: string
 	apiModelId?: string
 	apiKey?: string // anthropic
 	apiKey?: string // anthropic
+	anthropicBaseUrl?: string
 	openRouterApiKey?: string
 	openRouterApiKey?: string
 	awsAccessKey?: string
 	awsAccessKey?: string
 	awsSecretKey?: string
 	awsSecretKey?: string

+ 28 - 1
webview-ui/src/components/ApiOptions.tsx

@@ -5,6 +5,7 @@ import {
 	VSCodeRadio,
 	VSCodeRadio,
 	VSCodeRadioGroup,
 	VSCodeRadioGroup,
 	VSCodeTextField,
 	VSCodeTextField,
+	VSCodeCheckbox,
 } from "@vscode/webview-ui-toolkit/react"
 } from "@vscode/webview-ui-toolkit/react"
 import { memo, useCallback, useEffect, useMemo, useState } from "react"
 import { memo, useCallback, useEffect, useMemo, useState } from "react"
 import {
 import {
@@ -34,6 +35,7 @@ interface ApiOptionsProps {
 const ApiOptions = ({ showModelOptions, apiErrorMessage }: ApiOptionsProps) => {
 const ApiOptions = ({ showModelOptions, apiErrorMessage }: ApiOptionsProps) => {
 	const { apiConfiguration, setApiConfiguration, uriScheme } = useExtensionState()
 	const { apiConfiguration, setApiConfiguration, uriScheme } = useExtensionState()
 	const [ollamaModels, setOllamaModels] = useState<string[]>([])
 	const [ollamaModels, setOllamaModels] = useState<string[]>([])
+	const [anthropicBaseUrlSelected, setAnthropicBaseUrlSelected] = useState(!!apiConfiguration?.anthropicBaseUrl)
 
 
 	const handleInputChange = (field: keyof ApiConfiguration) => (event: any) => {
 	const handleInputChange = (field: keyof ApiConfiguration) => (event: any) => {
 		setApiConfiguration({ ...apiConfiguration, [field]: event.target.value })
 		setApiConfiguration({ ...apiConfiguration, [field]: event.target.value })
@@ -126,10 +128,35 @@ const ApiOptions = ({ showModelOptions, apiErrorMessage }: ApiOptionsProps) => {
 						placeholder="Enter API Key...">
 						placeholder="Enter API Key...">
 						<span style={{ fontWeight: 500 }}>Anthropic API Key</span>
 						<span style={{ fontWeight: 500 }}>Anthropic API Key</span>
 					</VSCodeTextField>
 					</VSCodeTextField>
+
+					<div style={{ marginTop: 2 }}>
+						<VSCodeCheckbox
+							checked={anthropicBaseUrlSelected}
+							onChange={(e: any) => {
+								const isChecked = e.target.checked === true
+								setAnthropicBaseUrlSelected(isChecked)
+								if (!isChecked) {
+									setApiConfiguration({ ...apiConfiguration, anthropicBaseUrl: "" })
+								}
+							}}>
+							Use custom base URL
+						</VSCodeCheckbox>
+					</div>
+
+					{anthropicBaseUrlSelected && (
+						<VSCodeTextField
+							value={apiConfiguration?.anthropicBaseUrl || ""}
+							style={{ width: "100%", marginTop: 2 }}
+							type="url"
+							onInput={handleInputChange("anthropicBaseUrl")}
+							placeholder="Default: https://api.anthropic.com"
+						/>
+					)}
+
 					<p
 					<p
 						style={{
 						style={{
 							fontSize: "12px",
 							fontSize: "12px",
-							marginTop: "5px",
+							marginTop: 3,
 							color: "var(--vscode-descriptionForeground)",
 							color: "var(--vscode-descriptionForeground)",
 						}}>
 						}}>
 						This key is stored locally and only used to make API requests from this extension.
 						This key is stored locally and only used to make API requests from this extension.