Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/api/providers/openrouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,8 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH
}

try {
const response = await fetch("https://openrouter.ai/api/v1/chat/completions", {
const baseURL = this.options.openRouterBaseUrl || "https://openrouter.ai/api/v1"
const response = await fetch(`${baseURL}/chat/completions`, {
method: "POST",
headers: {
Authorization: `Bearer ${apiKey}`,
Expand Down
20 changes: 16 additions & 4 deletions src/core/webview/ClineProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ export class ClineProvider
webviewView.webview.html =
this.contextProxy.extensionMode === vscode.ExtensionMode.Development
? await this.getHMRHtmlContent(webviewView.webview)
: this.getHtmlContent(webviewView.webview)
: await this.getHtmlContent(webviewView.webview)

// Sets up an event listener to listen for messages passed from the webview view context
// and executes code based on the message that is received.
Expand Down Expand Up @@ -1019,6 +1019,12 @@ export class ClineProvider

const nonce = getNonce()

// Get the OpenRouter base URL from configuration
const { apiConfiguration } = await this.getState()
const openRouterBaseUrl = apiConfiguration.openRouterBaseUrl || "https://openrouter.ai"
// Extract the domain for CSP
const openRouterDomain = openRouterBaseUrl.match(/^(https?:\/\/[^\/]+)/)?.[1] || "https://openrouter.ai"

const stylesUri = getUri(webview, this.contextProxy.extensionUri, [
"webview-ui",
"build",
Expand Down Expand Up @@ -1055,7 +1061,7 @@ export class ClineProvider
`img-src ${webview.cspSource} https://storage.googleapis.com https://img.clerk.com data:`,
`media-src ${webview.cspSource}`,
`script-src 'unsafe-eval' ${webview.cspSource} https://* https://*.posthog.com http://${localServerUrl} http://0.0.0.0:${localPort} 'nonce-${nonce}'`,
`connect-src ${webview.cspSource} https://* https://*.posthog.com ws://${localServerUrl} ws://0.0.0.0:${localPort} http://${localServerUrl} http://0.0.0.0:${localPort}`,
`connect-src ${webview.cspSource} ${openRouterDomain} https://* https://*.posthog.com ws://${localServerUrl} ws://0.0.0.0:${localPort} http://${localServerUrl} http://0.0.0.0:${localPort}`,
]

return /*html*/ `
Expand Down Expand Up @@ -1094,7 +1100,7 @@ export class ClineProvider
* @returns A template string literal containing the HTML that should be
* rendered within the webview panel
*/
private getHtmlContent(webview: vscode.Webview): string {
private async getHtmlContent(webview: vscode.Webview): Promise<string> {
// Get the local path to main script run in the webview,
// then convert it to a uri we can use in the webview.

Expand Down Expand Up @@ -1129,6 +1135,12 @@ export class ClineProvider
*/
const nonce = getNonce()

// Get the OpenRouter base URL from configuration
const { apiConfiguration } = await this.getState()
const openRouterBaseUrl = apiConfiguration.openRouterBaseUrl || "https://openrouter.ai"
// Extract the domain for CSP
const openRouterDomain = openRouterBaseUrl.match(/^(https?:\/\/[^\/]+)/)?.[1] || "https://openrouter.ai"

// Tip: Install the es6-string-html VS Code extension to enable code highlighting below
return /*html*/ `
<!DOCTYPE html>
Expand All @@ -1137,7 +1149,7 @@ export class ClineProvider
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; font-src ${webview.cspSource} data:; style-src ${webview.cspSource} 'unsafe-inline'; img-src ${webview.cspSource} https://storage.googleapis.com https://img.clerk.com data:; media-src ${webview.cspSource}; script-src ${webview.cspSource} 'wasm-unsafe-eval' 'nonce-${nonce}' https://us-assets.i.posthog.com 'strict-dynamic'; connect-src ${webview.cspSource} https://openrouter.ai https://api.requesty.ai https://us.i.posthog.com https://us-assets.i.posthog.com;">
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; font-src ${webview.cspSource} data:; style-src ${webview.cspSource} 'unsafe-inline'; img-src ${webview.cspSource} https://storage.googleapis.com https://img.clerk.com data:; media-src ${webview.cspSource}; script-src ${webview.cspSource} 'wasm-unsafe-eval' 'nonce-${nonce}' https://us-assets.i.posthog.com 'strict-dynamic'; connect-src ${webview.cspSource} ${openRouterDomain} https://api.requesty.ai https://us.i.posthog.com https://us-assets.i.posthog.com;">
<link rel="stylesheet" type="text/css" href="${stylesUri}">
<link href="${codiconsUri}" rel="stylesheet" />
<script nonce="${nonce}">
Expand Down
18 changes: 11 additions & 7 deletions webview-ui/src/components/settings/ApiOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,17 @@ const ApiOptions = ({

const { data: routerModels, refetch: refetchRouterModels } = useRouterModels()

const { data: openRouterModelProviders } = useOpenRouterModelProviders(apiConfiguration?.openRouterModelId, {
enabled:
!!apiConfiguration?.openRouterModelId &&
routerModels?.openrouter &&
Object.keys(routerModels.openrouter).length > 1 &&
apiConfiguration.openRouterModelId in routerModels.openrouter,
})
const { data: openRouterModelProviders } = useOpenRouterModelProviders(
apiConfiguration?.openRouterModelId,
apiConfiguration?.openRouterBaseUrl,
{
enabled:
!!apiConfiguration?.openRouterModelId &&
routerModels?.openrouter &&
Object.keys(routerModels.openrouter).length > 1 &&
apiConfiguration.openRouterModelId in routerModels.openrouter,
},
)

// Update `apiModelId` whenever `selectedModelId` changes.
useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ type OpenRouterModelProvider = ModelInfo & {
label: string
}

async function getOpenRouterProvidersForModel(modelId: string) {
async function getOpenRouterProvidersForModel(modelId: string, baseUrl?: string) {
const models: Record<string, OpenRouterModelProvider> = {}

try {
const response = await axios.get(`https://openrouter.ai/api/v1/models/${modelId}/endpoints`)
const apiBaseUrl = baseUrl || "https://openrouter.ai/api/v1"
const response = await axios.get(`${apiBaseUrl}/models/${modelId}/endpoints`)
const result = openRouterEndpointsSchema.safeParse(response.data)

if (!result.success) {
Expand Down Expand Up @@ -100,9 +101,13 @@ type UseOpenRouterModelProvidersOptions = Omit<
"queryKey" | "queryFn"
>

export const useOpenRouterModelProviders = (modelId?: string, options?: UseOpenRouterModelProvidersOptions) =>
export const useOpenRouterModelProviders = (
modelId?: string,
baseUrl?: string,
options?: UseOpenRouterModelProvidersOptions,
) =>
useQuery<Record<string, OpenRouterModelProvider>>({
queryKey: ["openrouter-model-providers", modelId],
queryFn: () => (modelId ? getOpenRouterProvidersForModel(modelId) : {}),
queryKey: ["openrouter-model-providers", modelId, baseUrl],
queryFn: () => (modelId ? getOpenRouterProvidersForModel(modelId, baseUrl) : {}),
...options,
})