@@ -139,29 +139,43 @@ func (m *Model) updateFileModal(msg tea.Msg) (Model, tea.Cmd, bool) {
139139 }
140140
141141 func (m *Model) fetchModelsCmd() tea.Cmd {
142+ // If we have a cache, open the modal immediately with cached data and
143+ // kick off a background refresh at the same time.
144+ if len(m.cachedModels) > 0 {
145+ cached := m.cachedModels
146+ registry := m.registry
147+ providerOpts := m.providerOpts
148+ return tea.Batch(
149+ func() tea.Msg { return modelsFetchedMsg{models: cached} },
150+ func() tea.Msg {
151+ log.Info().Msg("fetchModelsCmd: background refresh of all providers")
152+ models := registry.ListAllModels(context.Background(), providerOpts)
153+ log.Info().Int("count", len(models)).Msg("fetchModelsCmd: background refresh done")
154+ return modelsFetchedMsg{models: models}
155+ },
156+ )
157+ }
158+ registry := m.registry
159+ providerOpts := m.providerOpts
142160 return func() tea.Msg {
143- log.Info().Str("provider", m.provider.Name()).Msg("fetchModelsCmd called")
144- models, err := m.provider.ListModels(context.Background())
145- if err != nil {
146- log.Error().Err(err).Msg("ListModels error")
147- } else {
148- log.Info().Int("count", len(models)).Msg("ListModels returned")
149- }
150- return modelsFetchedMsg{models: models, err: err}
161+ log.Info().Msg("fetchModelsCmd: fetching all providers")
162+ models := registry.ListAllModels(context.Background(), providerOpts)
163+ log.Info().Int("count", len(models)).Msg("fetchModelsCmd: all providers returned")
164+ return modelsFetchedMsg{models: models}
151165 }
152166 }
153167
154-func (m *Model) openModelsModal(models []provider.Model) {
168+func (m *Model) openModelsModal(models []provider.TaggedModel) {
155169 items := make([]modal.Item, len(models))
156- for i, model := range models {
157- desc := model.ParamSize
158- if model.QuantLevel != "" {
159- if desc != "" {
160- desc += ", "
161- }
162- desc += model.QuantLevel
170+ for i, pm := range models {
171+ desc := pm.ProviderName
172+ if pm.Model.ParamSize != "" {
173+ desc += " · " + pm.Model.ParamSize
163174 }
164- items[i] = modal.Item{Name: model.Name, Desc: desc}
175+ if pm.Model.QuantLevel != "" {
176+ desc += " · " + pm.Model.QuantLevel
177+ }
178+ items[i] = modal.Item{Name: pm.ProviderName + "/" + pm.Model.Name, Desc: desc}
165179 }
166180 searchFn := func(query string) []modal.Item {
167181 if query == "" {
@@ -213,12 +227,24 @@ func (m *Model) updateModelsModal(msg tea.Msg) (Model, tea.Cmd, bool) {
213227 return *m, nil, false
214228 }
215229
216-func (m *Model) switchModelCmd(modelName string) tea.Cmd {
230+// switchModelCmd accepts a selection of the form "providerName/modelName" as
231+// produced by openModelsModal, or a bare model name (falls back to the current
232+// provider config name for backwards compatibility).
233+func (m *Model) switchModelCmd(selection string) tea.Cmd {
217234 registry := m.registry
218- providerConfigName := m.providerConfigName
235+ currentProviderConfigName := m.providerConfigName
219236 providerOpts := m.providerOpts
220237 oldProv := m.provider
221238
239+ // Parse "providerName/modelName". Use SplitN so model names containing
240+ // additional slashes (e.g. "org/repo/model") are preserved intact.
241+ providerConfigName := currentProviderConfigName
242+ modelName := selection
243+ if parts := strings.SplitN(selection, "/", 2); len(parts) == 2 {
244+ providerConfigName = parts[0]
245+ modelName = parts[1]
246+ }
247+
222248 return func() tea.Msg {
223249 if registry == nil {
224250 return modelSwitchedMsg{err: provider.ErrProviderNotFound}
@@ -231,7 +257,7 @@ func (m *Model) switchModelCmd(modelName string) tea.Cmd {
231257 if oldProv != nil {
232258 oldProv.Close()
233259 }
234- return modelSwitchedMsg{modelName: modelName, prov: newProv}
260+ return modelSwitchedMsg{modelName: modelName, providerName: providerConfigName, prov: newProv}
235261 }
236262 }
237263
@@ -246,6 +272,12 @@ func (m *Model) handleModelsFetched(msg modelsFetchedMsg) tea.Model {
246272 m.lastNetError = "No models available"
247273 return m
248274 }
275+ // Update cache whenever we receive a fresh (non-empty) list.
276+ m.cachedModels = msg.models
277+ // Don't clobber an already-open modal (background refresh case).
278+ if m.modelsModal != nil {
279+ return m
280+ }
249281 log.Info().Int("count", len(msg.models)).Msg("handleModelsFetched opening modal")
250282 m.openModelsModal(msg.models)
251283 return m
@@ -258,5 +290,8 @@ func (m *Model) handleModelSwitched(msg modelSwitchedMsg) tea.Model {
258290 }
259291 m.provider = msg.prov
260292 m.currentModelName = msg.modelName
293+ if msg.providerName != "" {
294+ m.providerConfigName = msg.providerName
295+ }
261296 return m
262297 }