@@ -10,7 +10,18 @@ import * as vscode from "vscode"
1010
1111import { changeLanguage , t } from "../../i18n"
1212import { setPanel } from "../../activate/registerCommands"
13- import { ApiConfiguration , ApiProvider , ModelInfo , API_CONFIG_KEYS } from "../../shared/api"
13+ import {
14+ ApiConfiguration ,
15+ ApiProvider ,
16+ ModelInfo ,
17+ API_CONFIG_KEYS ,
18+ requestyDefaultModelId ,
19+ requestyDefaultModelInfo ,
20+ openRouterDefaultModelId ,
21+ openRouterDefaultModelInfo ,
22+ glamaDefaultModelId ,
23+ glamaDefaultModelInfo ,
24+ } from "../../shared/api"
1425import { findLast } from "../../shared/array"
1526import { supportPrompt } from "../../shared/support-prompt"
1627import { GlobalFileNames } from "../../shared/globalFileNames"
@@ -593,6 +604,8 @@ export class ClineProvider extends EventEmitter<ClineProviderEvents> implements
593604 "codicon.css" ,
594605 ] )
595606
607+ const imagesUri = getUri ( webview , this . contextProxy . extensionUri , [ "assets" , "images" ] )
608+
596609 const file = "src/index.tsx"
597610 const scriptUri = `http://${ localServerUrl } /${ file } `
598611
@@ -611,7 +624,7 @@ export class ClineProvider extends EventEmitter<ClineProviderEvents> implements
611624 `font-src ${ webview . cspSource } ` ,
612625 `style-src ${ webview . cspSource } 'unsafe-inline' https://* http://${ localServerUrl } http://0.0.0.0:${ localPort } ` ,
613626 `img-src ${ webview . cspSource } data:` ,
614- `script-src 'unsafe-eval' https://* https://*.posthog.com http://${ localServerUrl } http://0.0.0.0:${ localPort } 'nonce-${ nonce } '` ,
627+ `script-src 'unsafe-eval' ${ webview . cspSource } https://* https://*.posthog.com http://${ localServerUrl } http://0.0.0.0:${ localPort } 'nonce-${ nonce } '` ,
615628 `connect-src https://* https://*.posthog.com ws://${ localServerUrl } ws://0.0.0.0:${ localPort } http://${ localServerUrl } http://0.0.0.0:${ localPort } ` ,
616629 ]
617630
@@ -624,6 +637,9 @@ export class ClineProvider extends EventEmitter<ClineProviderEvents> implements
624637 <meta http-equiv="Content-Security-Policy" content="${ csp . join ( "; " ) } ">
625638 <link rel="stylesheet" type="text/css" href="${ stylesUri } ">
626639 <link href="${ codiconsUri } " rel="stylesheet" />
640+ <script nonce="${ nonce } ">
641+ window.IMAGES_BASE_URI = "${ imagesUri } "
642+ </script>
627643 <title>Roo Code</title>
628644 </head>
629645 <body>
@@ -672,6 +688,8 @@ export class ClineProvider extends EventEmitter<ClineProviderEvents> implements
672688 "codicon.css" ,
673689 ] )
674690
691+ const imagesUri = getUri ( webview , this . contextProxy . extensionUri , [ "assets" , "images" ] )
692+
675693 // const scriptUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, "assets", "main.js"))
676694
677695 // const styleResetUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, "assets", "reset.css"))
@@ -704,6 +722,9 @@ export class ClineProvider extends EventEmitter<ClineProviderEvents> implements
704722 <meta http-equiv="Content-Security-Policy" content="default-src 'none'; font-src ${ webview . cspSource } ; style-src ${ webview . cspSource } 'unsafe-inline'; img-src ${ webview . cspSource } data:; script-src 'nonce-${ nonce } ' https://us-assets.i.posthog.com; connect-src https://openrouter.ai https://us.i.posthog.com https://us-assets.i.posthog.com;">
705723 <link rel="stylesheet" type="text/css" href="${ stylesUri } ">
706724 <link href="${ codiconsUri } " rel="stylesheet" />
725+ <script nonce="${ nonce } ">
726+ window.IMAGES_BASE_URI = "${ imagesUri } "
727+ </script>
707728 <title>Roo Code</title>
708729 </head>
709730 <body>
@@ -1811,23 +1832,7 @@ export class ClineProvider extends EventEmitter<ClineProviderEvents> implements
18111832 break
18121833 case "upsertApiConfiguration" :
18131834 if ( message . text && message . apiConfiguration ) {
1814- try {
1815- await this . configManager . saveConfig ( message . text , message . apiConfiguration )
1816- const listApiConfig = await this . configManager . listConfig ( )
1817-
1818- await Promise . all ( [
1819- this . updateGlobalState ( "listApiConfigMeta" , listApiConfig ) ,
1820- this . updateApiConfiguration ( message . apiConfiguration ) ,
1821- this . updateGlobalState ( "currentApiConfigName" , message . text ) ,
1822- ] )
1823-
1824- await this . postStateToWebview ( )
1825- } catch ( error ) {
1826- this . outputChannel . appendLine (
1827- `Error create new api configuration: ${ JSON . stringify ( error , Object . getOwnPropertyNames ( error ) , 2 ) } ` ,
1828- )
1829- vscode . window . showErrorMessage ( t ( "common:errors.create_api_config" ) )
1830- }
1835+ await this . upsertApiConfiguration ( message . text , message . apiConfiguration )
18311836 }
18321837 break
18331838 case "renameApiConfiguration" :
@@ -2251,9 +2256,10 @@ export class ClineProvider extends EventEmitter<ClineProviderEvents> implements
22512256 // OpenRouter
22522257
22532258 async handleOpenRouterCallback ( code : string ) {
2259+ let { apiConfiguration, currentApiConfigName } = await this . getState ( )
2260+
22542261 let apiKey : string
22552262 try {
2256- const { apiConfiguration } = await this . getState ( )
22572263 const baseUrl = apiConfiguration . openRouterBaseUrl || "https://openrouter.ai/api/v1"
22582264 // Extract the base domain for the auth endpoint
22592265 const baseUrlDomain = baseUrl . match ( / ^ ( h t t p s ? : \/ \/ [ ^ \/ ] + ) / ) ?. [ 1 ] || "https://openrouter.ai"
@@ -2270,17 +2276,15 @@ export class ClineProvider extends EventEmitter<ClineProviderEvents> implements
22702276 throw error
22712277 }
22722278
2273- const openrouter : ApiProvider = "openrouter"
2274- await this . contextProxy . setValues ( {
2275- apiProvider : openrouter ,
2279+ const newConfiguration : ApiConfiguration = {
2280+ ... apiConfiguration ,
2281+ apiProvider : " openrouter" ,
22762282 openRouterApiKey : apiKey ,
2277- } )
2278-
2279- await this . postStateToWebview ( )
2280- if ( this . getCurrentCline ( ) ) {
2281- this . getCurrentCline ( ) ! . api = buildApiHandler ( { apiProvider : openrouter , openRouterApiKey : apiKey } )
2283+ openRouterModelId : apiConfiguration ?. openRouterModelId || openRouterDefaultModelId ,
2284+ openRouterModelInfo : apiConfiguration ?. openRouterModelInfo || openRouterDefaultModelInfo ,
22822285 }
2283- // await this.postMessageToWebview({ type: "action", action: "settingsButtonClicked" }) // bad ux if user is on welcome
2286+
2287+ await this . upsertApiConfiguration ( currentApiConfigName , newConfiguration )
22842288 }
22852289
22862290 // Glama
@@ -2301,19 +2305,55 @@ export class ClineProvider extends EventEmitter<ClineProviderEvents> implements
23012305 throw error
23022306 }
23032307
2304- const glama : ApiProvider = "glama"
2305- await this . contextProxy . setValues ( {
2306- apiProvider : glama ,
2308+ const { apiConfiguration, currentApiConfigName } = await this . getState ( )
2309+
2310+ const newConfiguration : ApiConfiguration = {
2311+ ...apiConfiguration ,
2312+ apiProvider : "glama" ,
23072313 glamaApiKey : apiKey ,
2308- } )
2309- await this . postStateToWebview ( )
2310- if ( this . getCurrentCline ( ) ) {
2311- this . getCurrentCline ( ) ! . api = buildApiHandler ( {
2312- apiProvider : glama ,
2313- glamaApiKey : apiKey ,
2314- } )
2314+ glamaModelId : apiConfiguration ?. glamaModelId || glamaDefaultModelId ,
2315+ glamaModelInfo : apiConfiguration ?. glamaModelInfo || glamaDefaultModelInfo ,
2316+ }
2317+
2318+ await this . upsertApiConfiguration ( currentApiConfigName , newConfiguration )
2319+ }
2320+
2321+ // Requesty
2322+
2323+ async handleRequestyCallback ( code : string ) {
2324+ let { apiConfiguration, currentApiConfigName } = await this . getState ( )
2325+
2326+ const newConfiguration : ApiConfiguration = {
2327+ ...apiConfiguration ,
2328+ apiProvider : "requesty" ,
2329+ requestyApiKey : code ,
2330+ requestyModelId : apiConfiguration ?. requestyModelId || requestyDefaultModelId ,
2331+ requestyModelInfo : apiConfiguration ?. requestyModelInfo || requestyDefaultModelInfo ,
2332+ }
2333+
2334+ await this . upsertApiConfiguration ( currentApiConfigName , newConfiguration )
2335+ }
2336+
2337+ // Save configuration
2338+
2339+ async upsertApiConfiguration ( configName : string , apiConfiguration : ApiConfiguration ) {
2340+ try {
2341+ await this . configManager . saveConfig ( configName , apiConfiguration )
2342+ const listApiConfig = await this . configManager . listConfig ( )
2343+
2344+ await Promise . all ( [
2345+ this . updateGlobalState ( "listApiConfigMeta" , listApiConfig ) ,
2346+ this . updateApiConfiguration ( apiConfiguration ) ,
2347+ this . updateGlobalState ( "currentApiConfigName" , configName ) ,
2348+ ] )
2349+
2350+ await this . postStateToWebview ( )
2351+ } catch ( error ) {
2352+ this . outputChannel . appendLine (
2353+ `Error create new api configuration: ${ JSON . stringify ( error , Object . getOwnPropertyNames ( error ) , 2 ) } ` ,
2354+ )
2355+ vscode . window . showErrorMessage ( t ( "common:errors.create_api_config" ) )
23152356 }
2316- // await this.postMessageToWebview({ type: "action", action: "settingsButtonClicked" }) // bad ux if user is on welcome
23172357 }
23182358
23192359 // Task history
@@ -2627,14 +2667,7 @@ export class ClineProvider extends EventEmitter<ClineProviderEvents> implements
26272667 if ( stateValues . apiProvider ) {
26282668 apiProvider = stateValues . apiProvider
26292669 } else {
2630- // Either new user or legacy user that doesn't have the apiProvider stored in state
2631- // (If they're using OpenRouter or Bedrock, then apiProvider state will exist)
2632- if ( secretValues . apiKey ) {
2633- apiProvider = "anthropic"
2634- } else {
2635- // New users should default to openrouter
2636- apiProvider = "openrouter"
2637- }
2670+ apiProvider = "anthropic"
26382671 }
26392672
26402673 // Build the apiConfiguration object combining state values and secrets
0 commit comments