11import { useEffect , useRef , useState } from "react" ;
22import { guardrailsFetch } from "@/app/lib/guardrailsClient" ;
33import { ConfigEditorPaneProps , Tool } from "@/app/lib/types/promptEditor" ;
4- import { CompletionConfig , CompletionParams } from "@/app/lib/types/configs" ;
5- import { ConfigType , MODEL_OPTIONS , getModelsForType } from "@/app/lib/models" ;
4+ import {
5+ CompletionConfig ,
6+ CompletionParams ,
7+ ProviderType ,
8+ } from "@/app/lib/types/configs" ;
9+ import { ConfigType , getModelsForType } from "@/app/lib/models" ;
610import { PROVIDER_TYPES } from "@/app/lib/constants" ;
711import {
812 getAllProviders ,
13+ getCompletionTypesForProvider ,
914 getModelSchema ,
1015 getParamLabel ,
1116 getProviderLabel ,
@@ -87,6 +92,10 @@ export default function ConfigEditorPane({
8792 value : p ,
8893 label : getProviderLabel ( p ) ,
8994 } ) ) ;
95+ const availableTypes = getCompletionTypesForProvider ( provider ) ;
96+ const typeOptions = PROVIDER_TYPES . filter ( ( t ) =>
97+ availableTypes . includes ( t . value as ConfigType ) ,
98+ ) ;
9099
91100 const selectedConfig = savedConfigs . find ( ( c ) => c . id === selectedConfigId ) ;
92101 const isBoundToSavedConfig = ! ! boundConfigId ;
@@ -110,35 +119,14 @@ export default function ConfigEditorPane({
110119 } ) ;
111120 } ;
112121
113- const handleProviderChange = ( newProvider : string ) => {
114- const candidates = getModelsForType ( newProvider , currentType ) ;
115- const fallback = MODEL_OPTIONS [ newProvider ] ?. [ 0 ] ?. value ?? "" ;
116- const nextModel = candidates [ 0 ] ?. value ?? fallback ;
117- handleConfigChange ( {
118- completion : { provider : newProvider as CompletionConfig [ "provider" ] } ,
119- params : { model : nextModel } ,
120- } ) ;
121- } ;
122-
123- const handleTypeChange = ( newType : ConfigType ) => {
124- const provider = configBlob . completion . provider ;
125- const candidates = getModelsForType ( provider , newType ) ;
126- const stillValid = candidates . some ( ( m ) => m . value === params . model ) ;
127- const nextModel = stillValid
128- ? params . model
129- : ( candidates [ 0 ] ?. value ??
130- MODEL_OPTIONS [ provider ] ?. [ 0 ] ?. value ??
131- params . model ) ;
132- handleConfigChange ( {
133- completion : { type : newType } ,
134- params : { model : nextModel } ,
135- } ) ;
136- } ;
137-
138- const handleModelChange = ( model : string ) => {
139- const nextSchema = getModelSchema ( provider , model ) ;
122+ const applyProviderTypeModel = (
123+ nextProvider : string ,
124+ nextType : ConfigType ,
125+ nextModel : string ,
126+ ) => {
127+ const nextSchema = getModelSchema ( nextProvider , nextModel ) ;
140128 const nextSchemaParams = nextSchema
141- ? reconcileParamsForModel ( provider , model , params )
129+ ? reconcileParamsForModel ( nextProvider , nextModel , params )
142130 : { } ;
143131 const oldSchemaKeys = modelSchema ? Object . keys ( modelSchema . config ) : [ ] ;
144132 const carryover = { ...params } ;
@@ -148,11 +136,33 @@ export default function ConfigEditorPane({
148136 ...configBlob ,
149137 completion : {
150138 ...configBlob . completion ,
151- params : { ...carryover , model, ...nextSchemaParams } ,
139+ provider : nextProvider as ProviderType ,
140+ type : nextType ,
141+ params : { ...carryover , model : nextModel , ...nextSchemaParams } ,
152142 } ,
153143 } ) ;
154144 } ;
155145
146+ const handleProviderChange = ( newProvider : string ) => {
147+ const types = getCompletionTypesForProvider ( newProvider ) ;
148+ const nextType = types . includes ( currentType )
149+ ? currentType
150+ : ( types [ 0 ] ?? currentType ) ;
151+ const nextModel = getModelsForType ( newProvider , nextType ) [ 0 ] ?. value ?? "" ;
152+ applyProviderTypeModel ( newProvider , nextType , nextModel ) ;
153+ } ;
154+
155+ const handleTypeChange = ( newType : ConfigType ) => {
156+ const candidates = getModelsForType ( provider , newType ) ;
157+ const stillValid = candidates . some ( ( m ) => m . value === params . model ) ;
158+ const nextModel = stillValid ? params . model : ( candidates [ 0 ] ?. value ?? "" ) ;
159+ applyProviderTypeModel ( provider , newType , nextModel ) ;
160+ } ;
161+
162+ const handleModelChange = ( model : string ) => {
163+ applyProviderTypeModel ( provider , currentType , model ) ;
164+ } ;
165+
156166 const saveDisabled = ! configName . trim ( ) || isSaving ;
157167
158168 return (
@@ -212,14 +222,14 @@ export default function ConfigEditorPane({
212222 }
213223 className = { inputClass }
214224 >
215- { PROVIDER_TYPES . map ( ( option ) => (
225+ { typeOptions . map ( ( option ) => (
216226 < option key = { option . value } value = { option . value } >
217227 { option . label }
218228 </ option >
219229 ) ) }
220230 </ select >
221231 < p className = "text-xs mt-1.5 text-text-secondary" >
222- { PROVIDER_TYPES . find (
232+ { typeOptions . find (
223233 ( t ) => t . value === ( configBlob . completion . type || "text" ) ,
224234 ) ?. description ?? "" }
225235 </ p >
0 commit comments