Skip to content

Commit 4d14dbe

Browse files
authored
[Refactor] Treat apiConfiguration as always defined in the webview (#2234)
ExtensionState.apiConfiguration is required and initialized to {}, so drop optional typing and optional chaining on it in webview consumers. - useSelectedModel, ModelSelector, getWelcomeApiConfiguration and ExperimentalSettings now require apiConfiguration (ExperimentalSettings also loses an 'any') - Replace apiConfiguration?. with apiConfiguration. across settings provider panels, ApiOptions, ChatView, CodeIndexPopover and others - SettingsView uses cachedState.apiConfiguration directly - Update tests to pass {} instead of undefined
1 parent 0952a89 commit 4d14dbe

41 files changed

Lines changed: 217 additions & 199 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎webview-ui/src/components/chat/ChatView.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
107107
const [showRetiredProviderWarning, setShowRetiredProviderWarning] = useState(false)
108108

109109
// When the provider changes, clear the retired-provider warning.
110-
const providerName = apiConfiguration?.apiProvider
110+
const providerName = apiConfiguration.apiProvider
111111
useEffect(() => {
112112
setShowRetiredProviderWarning(false)
113113
}, [providerName])
@@ -646,7 +646,7 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
646646
if (text || images.length > 0) {
647647
// Intercept when the active provider is retired — show a
648648
// WarningRow instead of sending anything to the backend.
649-
if (apiConfiguration?.apiProvider && isRetiredProvider(apiConfiguration.apiProvider)) {
649+
if (apiConfiguration.apiProvider && isRetiredProvider(apiConfiguration.apiProvider)) {
650650
setShowRetiredProviderWarning(true)
651651
return
652652
}
@@ -721,7 +721,7 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
721721
sendingDisabled,
722722
isStreaming,
723723
messageQueue.length,
724-
apiConfiguration?.apiProvider,
724+
apiConfiguration.apiProvider,
725725
], // messagesRef and clineAskRef are stable
726726
)
727727

‎webview-ui/src/components/chat/CodeIndexPopover.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ export const CodeIndexPopover: React.FC<CodeIndexPopoverProps> = ({
728728
// if the main API provider is also configured for Bedrock
729729
if (
730730
value === providerIdentifiers.bedrock &&
731-
apiConfiguration?.apiProvider === providerIdentifiers.bedrock
731+
apiConfiguration.apiProvider === providerIdentifiers.bedrock
732732
) {
733733
// Only populate if currently empty
734734
if (

‎webview-ui/src/components/chat/ModelSelector.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function getModelIdField(provider: ProviderName): string | undefined {
4747
return getProviderModelConfig(provider)?.field
4848
}
4949

50-
function useModelListForProvider(provider: ProviderName | undefined, apiConfiguration?: ProviderSettings) {
50+
function useModelListForProvider(provider: ProviderName | undefined, apiConfiguration: ProviderSettings) {
5151
const isStatic = provider ? isStaticModelProvider(provider) : false
5252
const field = provider ? getModelIdField(provider) : undefined
5353
const isUnsupported = !provider || !field || UNSUPPORTED_QUICK_PICK_PROVIDERS.includes(provider)
@@ -71,7 +71,7 @@ function useModelListForProvider(provider: ProviderName | undefined, apiConfigur
7171
}
7272

7373
interface ModelSelectorProps {
74-
apiConfiguration?: ProviderSettings
74+
apiConfiguration: ProviderSettings
7575
disabled?: boolean
7676
title: string
7777
triggerClassName?: string
@@ -90,7 +90,7 @@ export const ModelSelector = ({
9090
const { t } = useAppTranslation()
9191
const { organizationAllowList, currentApiConfigName } = useExtensionState()
9292

93-
const providerRaw = apiConfiguration?.apiProvider || providerIdentifiers.openrouter
93+
const providerRaw = apiConfiguration.apiProvider || providerIdentifiers.openrouter
9494
const provider: ProviderName | undefined = isRetiredProvider(providerRaw) ? undefined : providerRaw
9595

9696
const { id: selectedModelId, info: selectedModelInfo } = useSelectedModel(apiConfiguration)

‎webview-ui/src/components/chat/__tests__/ChatTextArea.spec.tsx‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ describe("ChatTextArea", () => {
113113
;(useExtensionState as ReturnType<typeof vi.fn>).mockReturnValue({
114114
filePaths: [],
115115
openedTabs: [],
116+
apiConfiguration: {
117+
apiProvider: providerIdentifiers.anthropic,
118+
},
116119
taskHistory: [],
117120
cwd: "/test/workspace",
118121
})
@@ -306,6 +309,9 @@ describe("ChatTextArea", () => {
306309
;(useExtensionState as ReturnType<typeof vi.fn>).mockReturnValue({
307310
filePaths: [],
308311
openedTabs: [],
312+
apiConfiguration: {
313+
apiProvider: providerIdentifiers.anthropic,
314+
},
309315
cwd: mockCwd,
310316
})
311317
mockConvertToMentionPath.mockClear()
@@ -1007,6 +1013,9 @@ describe("ChatTextArea", () => {
10071013
;(useExtensionState as ReturnType<typeof vi.fn>).mockReturnValue({
10081014
filePaths: [],
10091015
openedTabs: [],
1016+
apiConfiguration: {
1017+
apiProvider: providerIdentifiers.anthropic,
1018+
},
10101019
taskHistory: [],
10111020
cwd: "/test/workspace",
10121021
commands: mockCommands,
@@ -1116,6 +1125,9 @@ describe("ChatTextArea", () => {
11161125
;(useExtensionState as ReturnType<typeof vi.fn>).mockReturnValue({
11171126
filePaths: [],
11181127
openedTabs: [],
1128+
apiConfiguration: {
1129+
apiProvider: providerIdentifiers.anthropic,
1130+
},
11191131
taskHistory: [],
11201132
cwd: "/test/workspace",
11211133
commands: undefined,
@@ -1157,6 +1169,9 @@ describe("ChatTextArea", () => {
11571169
;(useExtensionState as ReturnType<typeof vi.fn>).mockReturnValue({
11581170
filePaths: [],
11591171
openedTabs: [],
1172+
apiConfiguration: {
1173+
apiProvider: providerIdentifiers.anthropic,
1174+
},
11601175
taskHistory: [],
11611176
cwd: "/test/workspace",
11621177
})
@@ -1180,6 +1195,9 @@ describe("ChatTextArea", () => {
11801195
;(useExtensionState as ReturnType<typeof vi.fn>).mockReturnValue({
11811196
filePaths: [],
11821197
openedTabs: [],
1198+
apiConfiguration: {
1199+
apiProvider: providerIdentifiers.anthropic,
1200+
},
11831201
taskHistory: [],
11841202
cwd: "/test/workspace",
11851203
enterBehavior: "newline",

‎webview-ui/src/components/chat/__tests__/CodeIndexPopover.test.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ describe("CodeIndexPopover OpenRouter provider lookup", () => {
205205
},
206206
{
207207
name: "unset API provider",
208-
apiConfiguration: undefined,
208+
apiConfiguration: {},
209209
expectedRegion: "",
210210
expectedProfile: "",
211211
},

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

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -134,25 +134,25 @@ const ApiOptions = ({
134134
useExtensionState()
135135

136136
const [customHeaders, setCustomHeaders] = useState<[string, string][]>(() => {
137-
const headers = apiConfiguration?.openAiHeaders || {}
137+
const headers = apiConfiguration.openAiHeaders || {}
138138
return Object.entries(headers)
139139
})
140140

141141
useEffect(() => {
142-
const propHeaders = apiConfiguration?.openAiHeaders || {}
142+
const propHeaders = apiConfiguration.openAiHeaders || {}
143143

144144
if (JSON.stringify(customHeaders) !== JSON.stringify(Object.entries(propHeaders))) {
145145
setCustomHeaders(Object.entries(propHeaders))
146146
}
147-
}, [apiConfiguration?.openAiHeaders, customHeaders])
147+
}, [apiConfiguration.openAiHeaders, customHeaders])
148148

149149
// Helper to convert array of tuples to object (filtering out empty keys).
150150

151151
// Debounced effect to update the main configuration when local
152152
// customHeaders state stabilizes.
153153
useDebounce(
154154
() => {
155-
const currentConfigHeaders = apiConfiguration?.openAiHeaders || {}
155+
const currentConfigHeaders = apiConfiguration.openAiHeaders || {}
156156
const newHeadersObject = convertHeadersToObject(customHeaders)
157157

158158
// Only update if the processed object is different from the current config.
@@ -161,7 +161,7 @@ const ApiOptions = ({
161161
}
162162
},
163163
300,
164-
[customHeaders, apiConfiguration?.openAiHeaders, setApiConfigurationField],
164+
[customHeaders, apiConfiguration.openAiHeaders, setApiConfigurationField],
165165
)
166166

167167
const [isAdvancedSettingsOpen, setIsAdvancedSettingsOpen] = useState(false)
@@ -197,11 +197,11 @@ const ApiOptions = ({
197197
useZooGatewayRouterModelsSync()
198198

199199
const { data: openRouterModelProviders } = useOpenRouterModelProviders(
200-
apiConfiguration?.openRouterModelId,
201-
apiConfiguration?.openRouterBaseUrl,
200+
apiConfiguration.openRouterModelId,
201+
apiConfiguration.openRouterBaseUrl,
202202
{
203203
enabled:
204-
!!apiConfiguration?.openRouterModelId &&
204+
!!apiConfiguration.openRouterModelId &&
205205
routerModels?.openrouter &&
206206
Object.keys(routerModels.openrouter).length > 1 &&
207207
apiConfiguration.openRouterModelId in routerModels.openrouter,
@@ -232,8 +232,8 @@ const ApiOptions = ({
232232
vscode.postMessage({
233233
type: OpenAiModelsMessageType.requestOpenAiModels,
234234
values: {
235-
baseUrl: apiConfiguration?.openAiBaseUrl,
236-
apiKey: apiConfiguration?.openAiApiKey,
235+
baseUrl: apiConfiguration.openAiBaseUrl,
236+
apiKey: apiConfiguration.openAiApiKey,
237237
customHeaders: {}, // Reserved for any additional headers.
238238
openAiHeaders: headerObject,
239239
},
@@ -242,12 +242,12 @@ const ApiOptions = ({
242242
vscode.postMessage({
243243
type: OllamaModelsMessageType.requestOllamaModels,
244244
values: {
245-
baseUrl: apiConfiguration?.ollamaBaseUrl,
246-
apiKey: apiConfiguration?.ollamaApiKey,
245+
baseUrl: apiConfiguration.ollamaBaseUrl,
246+
apiKey: apiConfiguration.ollamaApiKey,
247247
},
248248
})
249249
} else if (selectedProvider === providerIdentifiers.lmstudio) {
250-
requestLmStudioModels(apiConfiguration?.lmStudioBaseUrl)
250+
requestLmStudioModels(apiConfiguration.lmStudioBaseUrl)
251251
} else if (
252252
selectedProvider === providerIdentifiers.vscodeLm ||
253253
selectedProvider === providerIdentifiers.githubCopilot
@@ -264,8 +264,8 @@ const ApiOptions = ({
264264
type: RouterModelsMessageType.requestRouterModels,
265265
values: {
266266
provider: providerIdentifiers.litellm,
267-
litellmApiKey: apiConfiguration?.litellmApiKey,
268-
litellmBaseUrl: apiConfiguration?.litellmBaseUrl,
267+
litellmApiKey: apiConfiguration.litellmApiKey,
268+
litellmBaseUrl: apiConfiguration.litellmBaseUrl,
269269
},
270270
})
271271
} else if (selectedProvider === providerIdentifiers.poe) {
@@ -278,16 +278,16 @@ const ApiOptions = ({
278278
250,
279279
[
280280
selectedProvider,
281-
apiConfiguration?.requestyApiKey,
282-
apiConfiguration?.openAiBaseUrl,
283-
apiConfiguration?.openAiApiKey,
284-
apiConfiguration?.ollamaBaseUrl,
285-
apiConfiguration?.ollamaApiKey,
286-
apiConfiguration?.lmStudioBaseUrl,
287-
apiConfiguration?.litellmBaseUrl,
288-
apiConfiguration?.litellmApiKey,
289-
apiConfiguration?.poeApiKey,
290-
apiConfiguration?.poeBaseUrl,
281+
apiConfiguration.requestyApiKey,
282+
apiConfiguration.openAiBaseUrl,
283+
apiConfiguration.openAiApiKey,
284+
apiConfiguration.ollamaBaseUrl,
285+
apiConfiguration.ollamaApiKey,
286+
apiConfiguration.lmStudioBaseUrl,
287+
apiConfiguration.litellmBaseUrl,
288+
apiConfiguration.litellmApiKey,
289+
apiConfiguration.poeApiKey,
290+
apiConfiguration.poeBaseUrl,
291291
customHeaders,
292292
],
293293
)
@@ -894,7 +894,7 @@ const ApiOptions = ({
894894
/>
895895
{selectedProvider === providerIdentifiers.poe && (
896896
<VSCodeTextField
897-
value={apiConfiguration?.poeBaseUrl || ""}
897+
value={apiConfiguration.poeBaseUrl || ""}
898898
onInput={handleInputChange("poeBaseUrl")}
899899
placeholder="https://api.poe.com/v1"
900900
className="w-full">
@@ -917,7 +917,7 @@ const ApiOptions = ({
917917
</div>
918918
<Select
919919
value={
920-
apiConfiguration?.openRouterSpecificProvider ||
920+
apiConfiguration.openRouterSpecificProvider ||
921921
OPENROUTER_DEFAULT_PROVIDER_NAME
922922
}
923923
onValueChange={(value) =>

‎webview-ui/src/components/settings/ExperimentalSettings.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { HTMLAttributes } from "react"
22

3-
import type { Experiments, ImageGenerationProvider } from "@roo-code/types"
3+
import type { Experiments, ImageGenerationProvider, ProviderSettings } from "@roo-code/types"
44

55
import { EXPERIMENT_IDS, experimentConfigsMap } from "@roo/experiments"
66

@@ -18,7 +18,7 @@ import { CustomToolsSettings } from "./CustomToolsSettings"
1818
type ExperimentalSettingsProps = HTMLAttributes<HTMLDivElement> & {
1919
experiments: Experiments
2020
setExperimentEnabled: SetExperimentEnabled
21-
apiConfiguration?: any
21+
apiConfiguration: ProviderSettings
2222
setApiConfigurationField?: any
2323
imageGenerationProvider?: ImageGenerationProvider
2424
openRouterImageApiKey?: string

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ const SettingsView = forwardRef<SettingsViewRef, SettingsViewProps>(({ onDone, t
223223
autoCloseZooOpenedNewFiles,
224224
} = cachedState
225225

226-
const apiConfiguration = useMemo(() => cachedState.apiConfiguration ?? {}, [cachedState.apiConfiguration])
226+
const apiConfiguration = cachedState.apiConfiguration
227227

228228
useEffect(() => {
229229
// Update when currentApiConfigName or mode changes.
@@ -263,11 +263,11 @@ const SettingsView = forwardRef<SettingsViewRef, SettingsViewProps>(({ onDone, t
263263
const setApiConfigurationField = useCallback(
264264
<K extends keyof ProviderSettings>(field: K, value: ProviderSettings[K], isUserAction: boolean = true) => {
265265
setCachedState((prevState) => {
266-
if (prevState.apiConfiguration?.[field] === value) {
266+
if (prevState.apiConfiguration[field] === value) {
267267
return prevState
268268
}
269269

270-
const previousValue = prevState.apiConfiguration?.[field]
270+
const previousValue = prevState.apiConfiguration[field]
271271

272272
// Helper to check if two values are semantically equal
273273
const areValuesEqual = (a: any, b: any): boolean => {

‎webview-ui/src/components/settings/__tests__/ExperimentalSettings.spec.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ vi.mock("@src/i18n/TranslationContext", () => ({
1313
describe("ExperimentalSettings", () => {
1414
const defaultProps = {
1515
experiments: experimentDefault,
16+
apiConfiguration: {},
1617
setExperimentEnabled: vi.fn(),
1718
setImageGenerationProvider: vi.fn(),
1819
setOpenRouterImageApiKey: vi.fn(),

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const Anthropic = ({ apiConfiguration, setApiConfigurationField }: Anthro
2020
const { t } = useAppTranslation()
2121
const selectedModel = useSelectedModel(apiConfiguration)
2222

23-
const [anthropicBaseUrlSelected, setAnthropicBaseUrlSelected] = useState(!!apiConfiguration?.anthropicBaseUrl)
23+
const [anthropicBaseUrlSelected, setAnthropicBaseUrlSelected] = useState(!!apiConfiguration.anthropicBaseUrl)
2424

2525
// Check if the current model supports 1M context beta
2626
const supports1MContextBeta =
@@ -43,7 +43,7 @@ export const Anthropic = ({ apiConfiguration, setApiConfigurationField }: Anthro
4343
return (
4444
<>
4545
<VSCodeTextField
46-
value={apiConfiguration?.apiKey || ""}
46+
value={apiConfiguration.apiKey || ""}
4747
type="password"
4848
onInput={handleInputChange("apiKey")}
4949
placeholder={t("settings:placeholders.apiKey")}
@@ -53,7 +53,7 @@ export const Anthropic = ({ apiConfiguration, setApiConfigurationField }: Anthro
5353
<div className="text-sm text-vscode-descriptionForeground -mt-2">
5454
{t("settings:providers.apiKeyStorageNotice")}
5555
</div>
56-
{!apiConfiguration?.apiKey && (
56+
{!apiConfiguration.apiKey && (
5757
<VSCodeButtonLink href="https://console.anthropic.com/settings/keys" appearance="secondary">
5858
{t("settings:providers.getAnthropicApiKey")}
5959
</VSCodeButtonLink>
@@ -74,14 +74,14 @@ export const Anthropic = ({ apiConfiguration, setApiConfigurationField }: Anthro
7474
{anthropicBaseUrlSelected && (
7575
<>
7676
<VSCodeTextField
77-
value={apiConfiguration?.anthropicBaseUrl || ""}
77+
value={apiConfiguration.anthropicBaseUrl || ""}
7878
type="url"
7979
onInput={handleInputChange("anthropicBaseUrl")}
8080
placeholder="https://api.anthropic.com"
8181
className="w-full mt-1"
8282
/>
8383
<Checkbox
84-
checked={apiConfiguration?.anthropicUseAuthToken ?? false}
84+
checked={apiConfiguration.anthropicUseAuthToken ?? false}
8585
onChange={handleInputChange("anthropicUseAuthToken", noTransform)}
8686
className="w-full mt-1">
8787
{t("settings:providers.anthropicUseAuthToken")}
@@ -92,7 +92,7 @@ export const Anthropic = ({ apiConfiguration, setApiConfigurationField }: Anthro
9292
{supports1MContextBeta && (
9393
<div>
9494
<Checkbox
95-
checked={apiConfiguration?.anthropicBeta1MContext ?? false}
95+
checked={apiConfiguration.anthropicBeta1MContext ?? false}
9696
onChange={(checked: boolean) => {
9797
setApiConfigurationField("anthropicBeta1MContext", checked)
9898
}}>

0 commit comments

Comments
 (0)