Skip to content

Commit 9391ede

Browse files
committed
WIP for passing process environment to webview components
Signed-off-by: Geoff Wilson <[email protected]>
1 parent 8d5d4f8 commit 9391ede

File tree

6 files changed

+29
-6
lines changed

6 files changed

+29
-6
lines changed

src/core/webview/ClineProvider.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,12 @@ 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+
398404
// Subscribe to code index status updates if the manager exists
399405
if (this.codeIndexManager) {
400406
this.codeIndexStatusSubscription = this.codeIndexManager.onProgressUpdate((update: IndexProgressUpdate) => {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export interface ApiOptionsProps {
6060
fromWelcomeView?: boolean
6161
errorMessage: string | undefined
6262
setErrorMessage: React.Dispatch<React.SetStateAction<string | undefined>>
63+
env?: Record<string, string | undefined>
6364
}
6465

6566
const ApiOptions = ({
@@ -69,6 +70,7 @@ const ApiOptions = ({
6970
fromWelcomeView,
7071
errorMessage,
7172
setErrorMessage,
73+
env = {}
7274
}: ApiOptionsProps) => {
7375
const { t } = useAppTranslation()
7476

@@ -327,7 +329,10 @@ const ApiOptions = ({
327329
)}
328330

329331
{selectedProvider === "anthropic" && (
330-
<Anthropic apiConfiguration={apiConfiguration} setApiConfigurationField={setApiConfigurationField} />
332+
<Anthropic
333+
apiConfiguration={apiConfiguration}
334+
setApiConfigurationField={setApiConfigurationField}
335+
env={env} />
331336
)}
332337

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

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ const SettingsView = forwardRef<SettingsViewRef, SettingsViewProps>(({ onDone, t
114114
const confirmDialogHandler = useRef<() => void>()
115115

116116
const [cachedState, setCachedState] = useState(extensionState)
117+
const [env, setEnv] = useState<Record<string, string | undefined>>({})
117118

118119
const {
119120
alwaysAllowReadOnly,
@@ -407,6 +408,9 @@ const SettingsView = forwardRef<SettingsViewRef, SettingsViewProps>(({ onDone, t
407408
if (message.type === "action" && message.action === "didBecomeVisible") {
408409
scrollToActiveTab()
409410
}
411+
if (message.type === "env") {
412+
setEnv(message.env)
413+
}
410414
}
411415

412416
window.addEventListener("message", handleMessage)
@@ -551,6 +555,7 @@ const SettingsView = forwardRef<SettingsViewRef, SettingsViewProps>(({ onDone, t
551555
setApiConfigurationField={setApiConfigurationField}
552556
errorMessage={errorMessage}
553557
setErrorMessage={setErrorMessage}
558+
env={env}
554559
/>
555560
</Section>
556561
</div>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ const renderApiOptions = (props: Partial<ApiOptionsProps> = {}) => {
225225
uriScheme={undefined}
226226
apiConfiguration={{}}
227227
setApiConfigurationField={() => {}}
228+
env={{}} // Mock environment variables
228229
{...props}
229230
/>
230231
</QueryClientProvider>

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ 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>
1516
}
1617

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

@@ -36,11 +38,13 @@ export const Anthropic = ({ apiConfiguration, setApiConfigurationField }: Anthro
3638
setApiConfigurationField("anthropicApiKeyUseEnvVar", checked)
3739
}
3840

41+
console.log("Environment from ApiOptions at Anthropic:", env)
42+
3943
return (
4044
<>
4145
<VSCodeTextField
42-
value={apiConfiguration?.apiKey || ""}
43-
type="password"
46+
value={env["ANTHROPIC_API_KEY"]}
47+
type="text"
4448
onInput={handleInputChange("apiKey")}
4549
placeholder={t("settings:placeholders.apiKey")}
4650
className="w-full"
@@ -54,7 +58,8 @@ export const Anthropic = ({ apiConfiguration, setApiConfigurationField }: Anthro
5458
<Checkbox
5559
checked={useApiKeyEnvVar}
5660
onChange={handleUseApiKeyEnvVarChange}
57-
className="mb-2">
61+
className="mb-2"
62+
disabled={!apiKeyEnvVarExists}>
5863
{t("settings:providers.apiKeyUseEnvVar", { name: "ANTHROPIC_API_KEY"})}
5964
</Checkbox>
6065
{(!(apiConfiguration?.apiKey || apiConfiguration?.anthropicApiKeyUseEnvVar)) && (

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +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}}",
138139
"glamaApiKey": "Glama API Key",
139140
"getGlamaApiKey": "Get Glama API Key",
140141
"useCustomBaseUrl": "Use custom base URL",

0 commit comments

Comments
 (0)