1- import {
2- chatModelOptions ,
3- defaultChatModel ,
4- requestChatCompletion ,
5- streamChatCompletion ,
6- } from './api/completions.js'
1+ import { requestChatCompletion , streamChatCompletion } from './api/completions.js'
72import {
83 formatModelAccessErrorMessage ,
94 isCredentialError ,
105 isModelAccessError ,
116 isModelAccessStatusMessage ,
127 toChatText ,
13- toModelId ,
148 toRepositoryLabel ,
159 toRepositoryUrl ,
1610} from './utils.js'
1711import { createChatKeyControls } from './key-controls.js'
12+ import { createChatModelPicker } from './model-picker.js'
1813import {
1914 buildActiveTabEditorContext ,
2015 normalizeWorkspaceTabContext ,
@@ -177,28 +172,37 @@ export const createChatDrawer = ({
177172 pendingAbortController = null
178173 }
179174
180- const setModelSelectDisabled = isDisabled => {
181- if ( ! ( modelSelect instanceof HTMLSelectElement ) ) {
182- return
183- }
184-
185- modelSelect . disabled = isDisabled
186- }
187-
188175 const keyControls = createChatKeyControls ( {
189176 root : keyRoot ,
190177 input : keyInput ,
191178 addButton : keyAddButton ,
192179 deleteButton : keyDeleteButton ,
193180 onKeyChange : nextKey => {
194- syncModelSelectionForKey ( nextKey )
181+ modelPicker . invalidateCatalogCache ( )
182+ modelPicker . syncModelSelectionForKey ( nextKey )
195183 syncComposerAvailability ( )
184+
185+ const keyPresent = typeof nextKey === 'string' && nextKey . trim ( ) . length > 0
186+
187+ if ( open && keyPresent ) {
188+ void modelPicker . loadModelOptionsFromCatalog ( { force : true } )
189+ }
196190 } ,
197191 } )
198192
199193 const getChatKey = ( ) => keyControls . getKey ( )
200194 const hasChatKey = ( ) => keyControls . hasKey ( )
201195
196+ const modelPicker = createChatModelPicker ( {
197+ modelSelect,
198+ getChatKey,
199+ resetModelAccessStatus : ( ) => {
200+ if ( isModelAccessStatusMessage ( statusNode ?. textContent ) ) {
201+ setChatStatus ( 'Idle' , 'neutral' )
202+ }
203+ } ,
204+ } )
205+
202206 const syncComposerAvailability = ( ) => {
203207 const keyPresent = hasChatKey ( )
204208
@@ -211,57 +215,7 @@ export const createChatDrawer = ({
211215 }
212216 }
213217
214- const replaceModelOptions = ( { modelIds, selectedModel } ) => {
215- if ( ! ( modelSelect instanceof HTMLSelectElement ) ) {
216- return
217- }
218-
219- const nextSelectedModel = toModelId ( selectedModel )
220- const nextModelIds = [ ...new Set ( [ defaultChatModel , ...modelIds ] ) ]
221-
222- modelSelect . replaceChildren ( )
223-
224- for ( const modelId of nextModelIds ) {
225- const option = document . createElement ( 'option' )
226- option . value = modelId
227- option . textContent = modelId
228- option . selected = modelId === nextSelectedModel
229- modelSelect . append ( option )
230- }
231-
232- if ( ! nextModelIds . includes ( nextSelectedModel ) ) {
233- modelSelect . value = defaultChatModel
234- }
235- }
236-
237- const getSelectedModel = ( ) => {
238- if ( ! ( modelSelect instanceof HTMLSelectElement ) ) {
239- return defaultChatModel
240- }
241-
242- return toModelId ( modelSelect . value )
243- }
244-
245- const initializeModelOptions = ( ) => {
246- replaceModelOptions ( {
247- modelIds : chatModelOptions ,
248- selectedModel : defaultChatModel ,
249- } )
250- }
251-
252- const syncModelSelectionForKey = key => {
253- const keyPresent = typeof key === 'string' && key . trim ( ) . length > 0
254-
255- setModelSelectDisabled ( ! keyPresent )
256-
257- if ( ! keyPresent && modelSelect instanceof HTMLSelectElement ) {
258- modelSelect . value = defaultChatModel
259- }
260-
261- if ( keyPresent && isModelAccessStatusMessage ( statusNode ?. textContent ) ) {
262- setChatStatus ( 'Idle' , 'neutral' )
263- }
264- }
218+ const getSelectedModel = ( ) => modelPicker . getSelectedModel ( )
265219
266220 const setOpen = nextOpen => {
267221 open = nextOpen === true
@@ -280,6 +234,10 @@ export const createChatDrawer = ({
280234 if ( open && promptInput instanceof HTMLTextAreaElement ) {
281235 promptInput . focus ( )
282236 }
237+
238+ if ( open && hasChatKey ( ) ) {
239+ void modelPicker . loadModelOptionsFromCatalog ( )
240+ }
283241 }
284242
285243 const setChatStatus = ( text , level = 'neutral' ) => {
@@ -989,8 +947,8 @@ export const createChatDrawer = ({
989947
990948 toggleButton ?. setAttribute ( 'aria-expanded' , 'false' )
991949 drawer ?. setAttribute ( 'hidden' , '' )
992- initializeModelOptions ( )
993- syncModelSelectionForKey ( getChatKey ( ) )
950+ modelPicker . initializeModelOptions ( )
951+ modelPicker . syncModelSelectionForKey ( getChatKey ( ) )
994952 syncComposerAvailability ( )
995953 syncRepositoryLabel ( )
996954 ensureUndoActionsNode ( )
0 commit comments