11import { useCallback , useState , useEffect , useRef } from "react"
22import { 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"
410import { useAppTranslation } from "@src/i18n/TranslationContext"
511
612import { 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-
3429type 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