Skip to content

Commit fa0504f

Browse files
committed
Set process env in react html as script, instead of trying to pass message
Signed-off-by: Geoff Wilson <[email protected]>
1 parent 9391ede commit fa0504f

File tree

6 files changed

+17
-27
lines changed

6 files changed

+17
-27
lines changed

src/api/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ export function buildApiHandler(configuration: ProviderSettings): ApiHandler {
7878

7979
switch (apiProvider) {
8080
case "anthropic":
81-
options.apiKey = getEnvVar("ANTHROPIC_API_KEY", options.apiKey)
81+
if (options.anthropicApiKeyUseEnvVar) {
82+
options.apiKey = getEnvVar("ANTHROPIC_API_KEY", options.apiKey)
83+
}
8284
return new AnthropicHandler(options)
8385
case "glama":
8486
options.glamaApiKey = getEnvVar("GLAMA_API_KEY", options.glamaApiKey)
@@ -93,7 +95,9 @@ export function buildApiHandler(configuration: ProviderSettings): ApiHandler {
9395
? new AnthropicVertexHandler(options)
9496
: new VertexHandler(options)
9597
case "openai":
96-
options.openAiApiKey = getEnvVar("OPEN_AI_API_KEY", options.openAiApiKey)
98+
if (options.openAiApiKeyUseEnvVar) {
99+
options.openAiApiKey = getEnvVar("OPEN_AI_API_KEY", options.openAiApiKey)
100+
}
97101
return new OpenAiHandler(options)
98102
case "ollama":
99103
return new OllamaHandler(options)

src/core/webview/ClineProvider.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -395,12 +395,6 @@ export class ClineProvider
395395
// and executes code based on the message that is recieved
396396
this.setWebviewMessageListener(webviewView.webview)
397397

398-
// Send process.env to the webview
399-
webviewView.webview.postMessage({
400-
type: "env",
401-
env: process.env,
402-
});
403-
404398
// Subscribe to code index status updates if the manager exists
405399
if (this.codeIndexManager) {
406400
this.codeIndexStatusSubscription = this.codeIndexManager.onProgressUpdate((update: IndexProgressUpdate) => {
@@ -720,6 +714,7 @@ export class ClineProvider
720714
window.IMAGES_BASE_URI = "${imagesUri}"
721715
window.AUDIO_BASE_URI = "${audioUri}"
722716
window.MATERIAL_ICONS_BASE_URI = "${materialIconsUri}"
717+
window.PROCESS_ENV = ${JSON.stringify(process.env)}
723718
</script>
724719
<title>Roo Code</title>
725720
</head>

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,7 @@ const ApiOptions = ({
329329
)}
330330

331331
{selectedProvider === "anthropic" && (
332-
<Anthropic
333-
apiConfiguration={apiConfiguration}
334-
setApiConfigurationField={setApiConfigurationField}
335-
env={env} />
332+
<Anthropic apiConfiguration={apiConfiguration} setApiConfigurationField={setApiConfigurationField}/>
336333
)}
337334

338335
{selectedProvider === "openai-native" && (

webview-ui/src/components/settings/SettingsView.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,6 @@ const SettingsView = forwardRef<SettingsViewRef, SettingsViewProps>(({ onDone, t
408408
if (message.type === "action" && message.action === "didBecomeVisible") {
409409
scrollToActiveTab()
410410
}
411-
if (message.type === "env") {
412-
setEnv(message.env)
413-
}
414411
}
415412

416413
window.addEventListener("message", handleMessage)

webview-ui/src/components/settings/providers/Anthropic.tsx

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,11 @@ import { inputEventTransform, noTransform } from "../transforms"
1212
type AnthropicProps = {
1313
apiConfiguration: ProviderSettings
1414
setApiConfigurationField: (field: keyof ProviderSettings, value: ProviderSettings[keyof ProviderSettings]) => void
15-
env?: Record<string, string | undefined>
1615
}
1716

18-
export const Anthropic = ({ apiConfiguration, setApiConfigurationField, env = {}}: AnthropicProps) => {
17+
export const Anthropic = ({ apiConfiguration, setApiConfigurationField }: AnthropicProps) => {
1918
const { t } = useAppTranslation()
20-
21-
const apiKeyEnvVarExists = !!env["ANTHROPIC_API_KEY"]
2219
const [anthropicBaseUrlSelected, setAnthropicBaseUrlSelected] = useState(!!apiConfiguration?.anthropicBaseUrl)
23-
const [useApiKeyEnvVar, setUseApiKeyEnvVar] = useState(!!apiConfiguration?.anthropicApiKeyUseEnvVar)
2420

2521
const handleInputChange = useCallback(
2622
<K extends keyof ProviderSettings, E>(
@@ -33,23 +29,24 @@ export const Anthropic = ({ apiConfiguration, setApiConfigurationField, env = {}
3329
[setApiConfigurationField],
3430
)
3531

32+
const env = (window as any).PROCESS_ENV || {}
33+
const apiKeyEnvVarExists = !!env["ANTHROPIC_API_KEY"]
34+
const [useApiKeyEnvVar, setUseApiKeyEnvVar] = useState(!!apiConfiguration?.anthropicApiKeyUseEnvVar)
35+
3636
const handleUseApiKeyEnvVarChange = (checked: boolean) => {
3737
setUseApiKeyEnvVar(checked)
3838
setApiConfigurationField("anthropicApiKeyUseEnvVar", checked)
3939
}
4040

41-
console.log("Environment from ApiOptions at Anthropic:", env)
42-
4341
return (
4442
<>
4543
<VSCodeTextField
46-
value={env["ANTHROPIC_API_KEY"]}
47-
type="text"
44+
value={apiConfiguration?.apiKey || ""}
45+
type="password"
4846
onInput={handleInputChange("apiKey")}
4947
placeholder={t("settings:placeholders.apiKey")}
5048
className="w-full"
51-
disabled={useApiKeyEnvVar}
52-
>
49+
disabled={useApiKeyEnvVar}>
5350
<label className="block font-medium mb-1">{t("settings:providers.anthropicApiKey")}</label>
5451
</VSCodeTextField>
5552
<div className="text-sm text-vscode-descriptionForeground -mt-2">

webview-ui/src/i18n/locales/en/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
"openRouterApiKey": "OpenRouter API Key",
136136
"getOpenRouterApiKey": "Get OpenRouter API Key",
137137
"apiKeyStorageNotice": "API keys are stored securely in VSCode's Secret Storage",
138-
"apiKeyUseEnvVar": "Get API key from env var {{name}}",
138+
"apiKeyUseEnvVar": "Use API key from env var {{name}}",
139139
"glamaApiKey": "Glama API Key",
140140
"getGlamaApiKey": "Get Glama API Key",
141141
"useCustomBaseUrl": "Use custom base URL",

0 commit comments

Comments
 (0)