xonecas.io
62159
Sy

symb / commit 0a8e7c5

feat: list models and switch to them from any configured provider

xonecas2026-02-19 0a8e7c517c6093c338bb3cdd604fcdbe7ae58829 parent 5d6a1d0

4 files changed

internal/provider/provider.go+39−0
@@ -161,3 +161,42 @@ func (r *Registry) List() []string {
161161 }
162162 return names
163163 }
164+
165+// TaggedModel pairs a provider config name with a model.
166+type TaggedModel struct {
167+ ProviderName string
168+ Model Model
169+}
170+
171+// ListAllModels concurrently fetches models from every registered provider and
172+// returns the combined list. Errors from individual providers are logged and
173+// skipped so a single unavailable provider does not block the rest.
174+func (r *Registry) ListAllModels(ctx context.Context, opts Options) []TaggedModel {
175+ type result struct {
176+ name string
177+ models []Model
178+ }
179+ ch := make(chan result, len(r.factories))
180+ for name := range r.factories {
181+ name := name
182+ go func() {
183+ prov := r.factories[name].Create("", opts)
184+ models, err := prov.ListModels(ctx)
185+ prov.Close()
186+ if err != nil {
187+ log.Warn().Str("provider", name).Err(err).Msg("ListAllModels: provider error")
188+ ch <- result{name: name}
189+ return
190+ }
191+ ch <- result{name: name, models: models}
192+ }()
193+ }
194+ var all []TaggedModel
195+ for range r.factories {
196+ res := <-ch
197+ for _, m := range res.models {
198+ all = append(all, TaggedModel{ProviderName: res.name, Model: m})
199+ }
200+ }
201+ return all
202+}
internal/tui/messages.go+5−4
@@ -60,14 +60,15 @@ type llmHistoryMsg struct{ msg provider.Message }
6060 type llmErrorMsg struct{ err error }
6161
6262 type modelsFetchedMsg struct {
63- models []provider.Model
63+ models []provider.TaggedModel
6464 err error
6565 }
6666
6767 type modelSwitchedMsg struct {
68- modelName string
69- prov provider.Provider
70- err error
68+ modelName string
69+ providerName string
70+ prov provider.Provider
71+ err error
7172 }
7273
7374 type userMsgSavedMsg struct {
internal/tui/tui.go+1−0
@@ -255,6 +255,7 @@ type Model struct {
255255 registry *provider.Registry
256256 providerOpts provider.Options
257257 currentModelName string
258+ cachedModels []provider.TaggedModel // cached across all providers
258259 // Pending tool calls: maps tool call ID → arguments for line extraction
259260 pendingToolCalls map[string]provider.ToolCall
260261
internal/tui/update_modals.go+55−20
@@ -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 }