Skip to content

Commit f34d191

Browse files
remove REGION_TO_URL duplication and fix event handler
1 parent 3cc95c1 commit f34d191

File tree

3 files changed

+62
-53
lines changed

3 files changed

+62
-53
lines changed

packages/types/src/providers/ibm-watsonx.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import type { ModelInfo } from "../model.js"
22

3+
export const REGION_TO_URL: Record<string, string> = {
4+
"us-south": "https://us-south.ml.cloud.ibm.com",
5+
"eu-de": "https://eu-de.ml.cloud.ibm.com",
6+
"eu-gb": "https://eu-gb.ml.cloud.ibm.com",
7+
"jp-tok": "https://jp-tok.ml.cloud.ibm.com",
8+
"au-syd": "https://au-syd.ml.cloud.ibm.com",
9+
"ca-tor": "https://ca-tor.ml.cloud.ibm.com",
10+
"ap-south-1": "https://ap-south-1.aws.wxai.ibm.com",
11+
}
12+
313
export type WatsonxAIModelId = keyof typeof watsonxModels
414
export const watsonxDefaultModelId = "ibm/granite-3-3-8b-instruct"
515

src/api/providers/fetchers/ibm-watsonx.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ModelInfo } from "@roo-code/types"
1+
import { ModelInfo, REGION_TO_URL } from "@roo-code/types"
22
import { IamAuthenticator, CloudPakForDataAuthenticator, UserOptions } from "ibm-cloud-sdk-core"
33
import { WatsonXAI } from "@ibm-cloud/watsonx-ai"
44
import WatsonxAiMlVml_v1 from "@ibm-cloud/watsonx-ai/dist/watsonx-ai-ml/vml_v1.js"
@@ -148,16 +148,6 @@ export async function getWatsonxModels(
148148
}
149149
}
150150

151-
export const REGION_TO_URL: Record<string, string> = {
152-
"us-south": "https://us-south.ml.cloud.ibm.com",
153-
"eu-de": "https://eu-de.ml.cloud.ibm.com",
154-
"eu-gb": "https://eu-gb.ml.cloud.ibm.com",
155-
"jp-tok": "https://jp-tok.ml.cloud.ibm.com",
156-
"au-syd": "https://au-syd.ml.cloud.ibm.com",
157-
"ca-tor": "https://ca-tor.ml.cloud.ibm.com",
158-
"ap-south-1": "https://ap-south-1.aws.wxai.ibm.com",
159-
custom: "",
160-
}
161151
/**
162152
* Returns the base URL for IBM Watsonx services corresponding to the given region.
163153
*

webview-ui/src/components/settings/providers/ibm-watsonx.tsx

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { useCallback, useState, useEffect, useRef } from "react"
22
import { VSCodeTextField } from "@vscode/webview-ui-toolkit/react"
3-
import { ModelInfo, watsonxDefaultModelId, type OrganizationAllowList, type ProviderSettings } from "@roo-code/types"
3+
import {
4+
ModelInfo,
5+
watsonxDefaultModelId,
6+
REGION_TO_URL,
7+
type OrganizationAllowList,
8+
type ProviderSettings,
9+
} from "@roo-code/types"
410
import { useAppTranslation } from "@src/i18n/TranslationContext"
511

612
import { vscode } from "@src/utils/vscode"
@@ -20,17 +26,6 @@ const WATSONX_REGIONS = {
2026
"ap-south-1": "Mumbai",
2127
}
2228

23-
const REGION_TO_URL = {
24-
"us-south": "https://us-south.ml.cloud.ibm.com",
25-
"eu-de": "https://eu-de.ml.cloud.ibm.com",
26-
"eu-gb": "https://eu-gb.ml.cloud.ibm.com",
27-
"jp-tok": "https://jp-tok.ml.cloud.ibm.com",
28-
"au-syd": "https://au-syd.ml.cloud.ibm.com",
29-
"ca-tor": "https://ca-tor.ml.cloud.ibm.com",
30-
"ap-south-1": "https://ap-south-1.aws.wxai.ibm.com",
31-
custom: "",
32-
}
33-
3429
type WatsonxAIProps = {
3530
apiConfiguration: ProviderSettings
3631
setApiConfigurationField: (field: keyof ProviderSettings, value: ProviderSettings[keyof ProviderSettings]) => void
@@ -51,6 +46,48 @@ export const WatsonxAI = ({
5146
const watsonxErrorJustReceived = useRef(false)
5247
const initialModelFetchAttempted = useRef(false)
5348

49+
const refreshStatusRef = useRef(refreshStatus)
50+
const refreshErrorRef = useRef(refreshError)
51+
const tRef = useRef(t)
52+
53+
useEffect(() => {
54+
refreshStatusRef.current = refreshStatus
55+
}, [refreshStatus])
56+
useEffect(() => {
57+
refreshErrorRef.current = refreshError
58+
}, [refreshError])
59+
useEffect(() => {
60+
tRef.current = t
61+
}, [t])
62+
63+
useEffect(() => {
64+
const handleMessage = (event: MessageEvent<ExtensionMessage>) => {
65+
const message = event.data
66+
if (message.type === "singleRouterModelFetchResponse" && !message.success) {
67+
const providerName = message.values?.provider as RouterName
68+
if (providerName === "ibm-watsonx") {
69+
watsonxErrorJustReceived.current = true
70+
setRefreshStatus("error")
71+
setRefreshError(message.error)
72+
}
73+
} else if (message.type === "watsonxModels") {
74+
setWatsonxModels(message.watsonxModels ?? {})
75+
if (refreshStatusRef.current === "loading") {
76+
if (!watsonxErrorJustReceived.current) {
77+
setRefreshStatus("success")
78+
} else {
79+
watsonxErrorJustReceived.current = false
80+
}
81+
}
82+
}
83+
}
84+
85+
window.addEventListener("message", handleMessage)
86+
return () => {
87+
window.removeEventListener("message", handleMessage)
88+
}
89+
}, [])
90+
5491
useEffect(() => {
5592
if (!apiConfiguration.watsonxPlatform) {
5693
setApiConfigurationField("watsonxPlatform", "ibmCloud")
@@ -109,34 +146,6 @@ export const WatsonxAI = ({
109146
[setApiConfigurationField],
110147
)
111148

112-
useEffect(() => {
113-
const handleMessage = (event: MessageEvent<ExtensionMessage>) => {
114-
const message = event.data
115-
if (message.type === "singleRouterModelFetchResponse" && !message.success) {
116-
const providerName = message.values?.provider as RouterName
117-
if (providerName === "ibm-watsonx") {
118-
watsonxErrorJustReceived.current = true
119-
setRefreshStatus("error")
120-
setRefreshError(message.error)
121-
}
122-
} else if (message.type === "watsonxModels") {
123-
setWatsonxModels(message.watsonxModels ?? {})
124-
if (refreshStatus === "loading") {
125-
if (!watsonxErrorJustReceived.current) {
126-
setRefreshStatus("success")
127-
} else {
128-
watsonxErrorJustReceived.current = false
129-
}
130-
}
131-
}
132-
}
133-
134-
window.addEventListener("message", handleMessage)
135-
return () => {
136-
window.removeEventListener("message", handleMessage)
137-
}
138-
}, [refreshStatus, refreshError, t])
139-
140149
const handleInputChange = useCallback(
141150
<E,>(field: keyof ProviderSettings, transform: (event: E) => any = inputEventTransform) =>
142151
(event: E | Event) => {
@@ -212,7 +221,7 @@ export const WatsonxAI = ({
212221
authType: authType,
213222
username: username,
214223
password: password,
215-
region: selectedRegion,
224+
region: platform === "ibmCloud" ? selectedRegion : undefined,
216225
},
217226
})
218227
}, [apiConfiguration, setRefreshStatus, setRefreshError, t, selectedRegion])
@@ -381,7 +390,7 @@ export const WatsonxAI = ({
381390
) : (
382391
<div className="w-full mb-1">
383392
<VSCodeTextField
384-
value={apiConfiguration.watsonxPassword || ""}
393+
value={apiConfiguration.watsonxPassword || ""}
385394
type="password"
386395
onInput={handleInputChange("watsonxPassword")}
387396
placeholder={t("settings:providers.watsonx.password")}

0 commit comments

Comments
 (0)