@@ -10,6 +10,7 @@ import { filterModels } from "../../provider-workspace/report";
1010
1111export default function ProviderModels ( {
1212 item,
13+ apiBase,
1314 availableModels,
1415 selectedModels,
1516 modelsLoading = false ,
@@ -19,6 +20,7 @@ export default function ProviderModels({
1920 onOpenAccounts,
2021} : {
2122 item : WorkspaceItem ;
23+ apiBase : string ;
2224 availableModels : string [ ] ;
2325 selectedModels : string [ ] ;
2426 modelsLoading ?: boolean ;
@@ -30,15 +32,54 @@ export default function ProviderModels({
3032} ) {
3133 const t = useT ( ) ;
3234 const [ query , setQuery ] = useState ( "" ) ;
35+ const [ customModelId , setCustomModelId ] = useState ( "" ) ;
36+ const [ customSaving , setCustomSaving ] = useState ( false ) ;
37+ const [ customError , setCustomError ] = useState ( "" ) ;
38+ const [ customSuccess , setCustomSuccess ] = useState ( "" ) ;
39+ const [ customModelIds , setCustomModelIds ] = useState < string [ ] > ( [ ] ) ;
40+ const [ customModelsReady , setCustomModelsReady ] = useState ( false ) ;
3341 const [ copiedId , setCopiedId ] = useState < string | null > ( null ) ;
3442 const copyResetRef = useRef < number | null > ( null ) ;
3543 const selectedSet = useMemo ( ( ) => new Set ( selectedModels ) , [ selectedModels ] ) ;
3644 const configuredModels = useMemo ( ( ) => item . models ?? [ ] , [ item . models ] ) ;
45+ const trimmedCustomModelId = customModelId . trim ( ) ;
46+ const customModelInvalid = ! customModelsReady
47+ || ! trimmedCustomModelId
48+ || trimmedCustomModelId . includes ( "/" )
49+ || availableModels . includes ( trimmedCustomModelId )
50+ || customModelIds . includes ( trimmedCustomModelId )
51+ || configuredModels . includes ( trimmedCustomModelId )
52+ || item . defaultModel === trimmedCustomModelId ;
3753 const models = useMemo (
38- ( ) => filterModels ( availableModels , item . defaultModel , query , configuredModels ) ,
39- [ availableModels , item . defaultModel , query , configuredModels ] ,
54+ ( ) => filterModels ( availableModels , item . defaultModel , query , configuredModels , customModelIds ) ,
55+ [ availableModels , item . defaultModel , query , configuredModels , customModelIds ] ,
4056 ) ;
4157
58+ useEffect ( ( ) => {
59+ let active = true ;
60+ void fetch ( `${ apiBase } /api/custom-models` )
61+ . then ( response => {
62+ if ( ! response . ok ) throw new Error ( ) ;
63+ return response . json ( ) ;
64+ } )
65+ . then ( ( rows : unknown ) => {
66+ if ( ! Array . isArray ( rows ) ) throw new Error ( "Invalid custom model list" ) ;
67+ if ( ! active ) return ;
68+ setCustomModelIds ( rows . flatMap ( row => {
69+ if ( ! row || typeof row !== "object" ) return [ ] ;
70+ const model = row as { provider ?: unknown ; modelId ?: unknown } ;
71+ return model . provider === item . name && typeof model . modelId === "string" ? [ model . modelId ] : [ ] ;
72+ } ) ) ;
73+ setCustomModelsReady ( true ) ;
74+ } )
75+ . catch ( ( ) => {
76+ if ( ! active ) return ;
77+ setCustomModelIds ( [ ] ) ;
78+ setCustomError ( t ( "models.networkError" ) ) ;
79+ } ) ;
80+ return ( ) => { active = false ; } ;
81+ } , [ apiBase , item . name , t ] ) ;
82+
4283 useEffect ( ( ) => ( ) => {
4384 if ( copyResetRef . current != null ) window . clearTimeout ( copyResetRef . current ) ;
4485 } , [ ] ) ;
@@ -57,7 +98,36 @@ export default function ProviderModels({
5798 }
5899 } ;
59100
60- const emptyBase = availableModels . length === 0 && configuredModels . length === 0 && ! item . defaultModel ;
101+ const addCustomModel = async ( ) => {
102+ if ( customModelInvalid || customSaving ) return ;
103+ setCustomSaving ( true ) ;
104+ setCustomError ( "" ) ;
105+ setCustomSuccess ( "" ) ;
106+ try {
107+ const response = await fetch ( `${ apiBase } /api/custom-models` , {
108+ method : "POST" ,
109+ headers : { "Content-Type" : "application/json" } ,
110+ body : JSON . stringify ( { provider : item . name , modelId : trimmedCustomModelId } ) ,
111+ } ) ;
112+ if ( response . ok ) {
113+ setCustomModelIds ( ids => ids . includes ( trimmedCustomModelId ) ? ids : [ ...ids , trimmedCustomModelId ] ) ;
114+ setCustomModelId ( "" ) ;
115+ setCustomSuccess ( t ( "models.customAdded" ) ) ;
116+ onRetryModels ?.( ) ;
117+ } else {
118+ setCustomError ( t ( "models.customSaveFailed" ) ) ;
119+ }
120+ } catch {
121+ setCustomError ( t ( "models.networkError" ) ) ;
122+ } finally {
123+ setCustomSaving ( false ) ;
124+ }
125+ } ;
126+
127+ const emptyBase = availableModels . length === 0
128+ && configuredModels . length === 0
129+ && customModelIds . length === 0
130+ && ! item . defaultModel ;
61131 const showingConfiguredFallback = availableModels . length === 0 && configuredModels . length > 0 ;
62132 // Aggregators (OpenRouter etc.) can return thousands of ids; capping the mounted
63133 // chips keeps the tab responsive. Filtering narrows the list, so the cap only
@@ -87,6 +157,31 @@ export default function ProviderModels({
87157 { showingConfiguredFallback && ! needsReauth && (
88158 < p className = "muted text-label" style = { { marginBottom : 10 } } > { t ( "pws.modelsConfiguredFallback" ) } </ p >
89159 ) }
160+ < label className = "text-label pws-custom-model-label" htmlFor = { `pws-custom-model-${ item . name } ` } >
161+ { t ( "models.customAdd" ) }
162+ </ label >
163+ < div className = "row pws-custom-model-row" >
164+ < input
165+ id = { `pws-custom-model-${ item . name } ` }
166+ className = "input"
167+ value = { customModelId }
168+ onChange = { event => setCustomModelId ( event . target . value ) }
169+ onKeyDown = { event => { if ( event . key === "Enter" ) void addCustomModel ( ) ; } }
170+ placeholder = { t ( "models.customFieldModelIdPlaceholder" ) }
171+ aria-label = { t ( "models.customAdd" ) }
172+ disabled = { customSaving }
173+ />
174+ < button
175+ type = "button"
176+ className = "btn btn-primary btn-sm"
177+ onClick = { ( ) => { void addCustomModel ( ) ; } }
178+ disabled = { customSaving || customModelInvalid }
179+ >
180+ { customSaving ? t ( "models.customSaving" ) : t ( "models.customAddBtn" ) }
181+ </ button >
182+ </ div >
183+ { customSuccess && < p className = "muted text-label" role = "status" > { customSuccess } </ p > }
184+ { customError && < p className = "pws-inline-error" role = "alert" > { customError } </ p > }
90185 { ! emptyBase && (
91186 < input
92187 type = "search"
0 commit comments