xonecas.io
62159
Sy

symb / commit 85c907d

feat: major provider cleanup, list models and change on the fly

xonecas2026-02-18 85c907d1819ac066d5c44962c8a3c208a5bd98c8 parent 0cfcba8

11 files changed

cmd/symb/main.go+4−3
@@ -63,9 +63,10 @@ func main() {
6363
6464 providerName, providerCfg := resolveProvider(cfg, registry)
6565
66- prov, err := registry.Create(providerName, providerCfg.Model, provider.Options{
66+ providerOpts := provider.Options{
6767 Temperature: providerCfg.Temperature,
68- })
68+ }
69+ prov, err := registry.Create(providerName, providerCfg.Model, providerOpts)
6970 if err != nil {
7071 fmt.Printf("Error creating provider: %v\n", err)
7172 os.Exit(1)
@@ -134,7 +135,7 @@ func main() {
134135 }
135136
136137 p := tea.NewProgram(
137- tui.New(prov, svc.proxy, tools, providerCfg.Model, svc.webCache, sessionID, tsIndex, svc.deltaTracker, svc.fileTracker, providerName, svc.scratchpad, resumeHistory),
138+ tui.New(prov, svc.proxy, tools, providerCfg.Model, svc.webCache, sessionID, tsIndex, svc.deltaTracker, svc.fileTracker, providerName, svc.scratchpad, resumeHistory, registry, providerOpts),
138139 tea.WithFilter(tui.MouseEventFilter),
139140 )
140141 svc.lspManager.SetCallback(func(absPath string, lines map[int]int) {
internal/provider/ollama.go+61−0
@@ -80,6 +80,48 @@ func (p *OllamaProvider) ChatStream(ctx context.Context, messages []Message, too
8080 return ch, nil
8181 }
8282
83+func (p *OllamaProvider) ListModels(ctx context.Context) ([]Model, error) {
84+ baseURL := strings.TrimRight(p.baseURL, "/v1")
85+ url := baseURL + "/api/tags"
86+
87+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
88+ if err != nil {
89+ return nil, err
90+ }
91+ req.Header.Set("Accept", "application/json")
92+
93+ resp, err := p.httpClient.Do(req)
94+ if err != nil {
95+ return nil, err
96+ }
97+ defer resp.Body.Close()
98+
99+ if resp.StatusCode != http.StatusOK {
100+ body, _ := io.ReadAll(resp.Body)
101+ return nil, fmt.Errorf("list models status %d: %s", resp.StatusCode, strings.TrimSpace(string(body)))
102+ }
103+
104+ var listResp ollamaListResponse
105+ if err := json.NewDecoder(resp.Body).Decode(&listResp); err != nil {
106+ return nil, err
107+ }
108+
109+ models := make([]Model, len(listResp.Models))
110+ for i, m := range listResp.Models {
111+ models[i] = Model{
112+ Name: m.Name,
113+ Size: m.Size,
114+ Digest: m.Digest,
115+ ModifiedAt: m.ModifiedAt,
116+ Format: m.Details.Format,
117+ Family: m.Details.Family,
118+ ParamSize: m.Details.ParamSize,
119+ QuantLevel: m.Details.QuantLevel,
120+ }
121+ }
122+ return models, nil
123+}
124+
83125 func (p *OllamaProvider) Close() error {
84126 if p.httpClient != nil {
85127 p.httpClient.CloseIdleConnections()
@@ -87,6 +129,25 @@ func (p *OllamaProvider) Close() error {
87129 return nil
88130 }
89131
132+type ollamaListResponse struct {
133+ Models []ollamaModel `json:"models"`
134+}
135+
136+type ollamaModel struct {
137+ Name string `json:"name"`
138+ Size int64 `json:"size"`
139+ Digest string `json:"digest"`
140+ ModifiedAt time.Time `json:"modified_at"`
141+ Details ollamaModelDetails `json:"details"`
142+}
143+
144+type ollamaModelDetails struct {
145+ Format string `json:"format"`
146+ Family string `json:"family"`
147+ ParamSize string `json:"parameter_size"`
148+ QuantLevel string `json:"quantization_level"`
149+}
150+
90151 type ollamaChatRequest struct {
91152 Model string `json:"model"`
92153 Messages []ollamaReqMessage `json:"messages"`
internal/provider/provider.go+14−0
@@ -87,6 +87,17 @@ type StreamEvent struct {
8787 Err error
8888 }
8989
90+type Model struct {
91+ Name string
92+ Size int64
93+ Digest string
94+ ModifiedAt time.Time
95+ Format string
96+ Family string
97+ ParamSize string
98+ QuantLevel string
99+}
100+
90101 // Provider defines the interface for LLM providers.
91102 type Provider interface {
92103 // Name returns the provider's identifier.
@@ -97,6 +108,9 @@ type Provider interface {
97108 // Pass nil tools for simple chat without tool calling.
98109 ChatStream(ctx context.Context, messages []Message, tools []Tool) (<-chan StreamEvent, error)
99110
111+ // ListModels returns available models from the provider.
112+ ListModels(ctx context.Context) ([]Model, error)
113+
100114 // Close closes idle HTTP connections and cleans up resources.
101115 Close() error
102116 }
internal/tui/golden_test.go+2−1
@@ -6,6 +6,7 @@ import (
66
77 tea "charm.land/bubbletea/v2"
88 "github.com/charmbracelet/x/exp/golden"
9+ "github.com/xonecas/symb/internal/provider"
910 )
1011
1112 // stripANSI removes ANSI escape codes for golden file comparison
@@ -26,7 +27,7 @@ func TestLayout(t *testing.T) {
2627
2728 for _, tt := range tests {
2829 t.Run(tt.name, func(t *testing.T) {
29- m := New(nil, nil, nil, "test-model", nil, "test-session", nil, nil, nil, "test-provider", nil, nil)
30+ m := New(nil, nil, nil, "test-model", nil, "test-session", nil, nil, nil, "test-provider", nil, nil, nil, provider.Options{})
3031 updated, _ := m.Update(tea.WindowSizeMsg{Width: tt.width, Height: tt.height})
3132 m = updated.(Model)
3233
internal/tui/messages.go+11−0
@@ -59,6 +59,17 @@ type storeBatch struct {
5959 type llmHistoryMsg struct{ msg provider.Message }
6060 type llmErrorMsg struct{ err error }
6161
62+type modelsFetchedMsg struct {
63+ models []provider.Model
64+ err error
65+}
66+
67+type modelSwitchedMsg struct {
68+ modelName string
69+ prov provider.Provider
70+ err error
71+}
72+
6273 type userMsgSavedMsg struct {
6374 convIdx int
6475 dbMsgID int64
internal/tui/tui.go+13−3
@@ -247,7 +247,14 @@ type Model struct {
247247 fileModal *modal.Model
248248 // Keybinds modal
249249 keybindsModal *modal.Model
250- searcher *filesearch.Searcher
250+ // Models modal
251+ modelsModal *modal.Model
252+ searcher *filesearch.Searcher
253+
254+ // Provider switching
255+ registry *provider.Registry
256+ providerOpts provider.Options
257+ currentModelName string
251258 // Pending tool calls: maps tool call ID → arguments for line extraction
252259 pendingToolCalls map[string]provider.ToolCall
253260
@@ -281,7 +288,7 @@ type Model struct {
281288 // New creates a new TUI model.
282289 // If resumeHistory is non-nil, the session is being resumed and messages are
283290 // loaded from the database instead of creating a fresh system prompt.
284-func New(prov provider.Provider, proxy *mcp.Proxy, tools []mcp.Tool, modelID string, db *store.Cache, sessionID string, idx *treesitter.Index, dt *delta.Tracker, ft FileReadResetter, providerConfigName string, pad llm.ScratchpadReader, resumeHistory []provider.Message) Model {
291+func New(prov provider.Provider, proxy *mcp.Proxy, tools []mcp.Tool, modelID string, db *store.Cache, sessionID string, idx *treesitter.Index, dt *delta.Tracker, ft FileReadResetter, providerConfigName string, pad llm.ScratchpadReader, resumeHistory []provider.Message, registry *provider.Registry, providerOpts provider.Options) Model {
285292 sty := DefaultStyles()
286293 cursorStyle := lipgloss.NewStyle().Foreground(ColorHighlight)
287294
@@ -357,7 +364,10 @@ func New(prov provider.Provider, proxy *mcp.Proxy, tools []mcp.Tool, modelID str
357364 fileTracker: ft,
358365 tsIndex: idx,
359366
360- searcher: newSearcherOrNil("."),
367+ searcher: newSearcherOrNil("."),
368+ registry: registry,
369+ providerOpts: providerOpts,
370+ currentModelName: modelID,
361371
362372 streamEntryStart: -1,
363373
internal/tui/update.go+8−0
@@ -40,6 +40,10 @@ func (m Model) handleModalMsg(msg tea.Msg) (tea.Model, tea.Cmd, bool) {
4040 if mdl, cmd, handled := m.updateFileModal(msg); handled {
4141 return mdl, cmd, true
4242 }
43+ // Models modal intercepts all input when open.
44+ if mdl, cmd, handled := m.updateModelsModal(msg); handled {
45+ return mdl, cmd, true
46+ }
4347 return m, nil, false
4448 }
4549
@@ -97,6 +101,10 @@ func (m Model) handleSystemEvent(msg tea.Msg) (tea.Model, tea.Cmd, bool) {
97101 case gitBranchMsg:
98102 mdl, cmd := m.handleGitBranch(msg)
99103 return mdl, cmd, true
104+ case modelsFetchedMsg:
105+ return m.handleModelsFetched(msg), nil, true
106+ case modelSwitchedMsg:
107+ return m.handleModelSwitched(msg), nil, true
100108 }
101109 return m, nil, false
102110 }
internal/tui/update_keypress.go+8−0
@@ -23,6 +23,7 @@ func (m *Model) keyPressHandlers() map[string]func(*Model) (Model, tea.Cmd, bool
2323 "enter": (*Model).handleEnter,
2424 "ctrl+f": (*Model).handleCtrlF,
2525 "ctrl+h": (*Model).handleCtrlH,
26+ "ctrl+m": (*Model).handleCtrlM,
2627 }
2728 }
2829
@@ -95,6 +96,13 @@ func (m *Model) handleCtrlH() (Model, tea.Cmd, bool) {
9596 return *m, nil, true
9697 }
9798
99+func (m *Model) handleCtrlM() (Model, tea.Cmd, bool) {
100+ if m.turnCancel != nil || m.turnPending || m.undoInFlight {
101+ return *m, nil, false
102+ }
103+ return *m, m.fetchModelsCmd(), true
104+}
105+
98106 func (m *Model) flushAndQuit() tea.Cmd {
99107 queue := m.storeQueue
100108 done := m.storeQueueDone
internal/tui/update_modals.go+115−0
@@ -6,6 +6,7 @@ import (
66
77 tea "charm.land/bubbletea/v2"
88 "github.com/xonecas/symb/internal/filesearch"
9+ "github.com/xonecas/symb/internal/provider"
910 "github.com/xonecas/symb/internal/tui/modal"
1011 )
1112
@@ -45,6 +46,7 @@ func (m *Model) openKeybindsModal() {
4546 items := []modal.Item{
4647 {Name: "ctrl+h", Desc: "keybinds"},
4748 {Name: "ctrl+f", Desc: "file search"},
49+ {Name: "ctrl+m", Desc: "switch model"},
4850 {Name: "ctrl+s", Desc: "save editor (send diff) [experimental]"},
4951 {Name: "ctrl+shift+c", Desc: "copy selection"},
5052 {Name: "ctrl+shift+v", Desc: "paste"},
@@ -134,3 +136,116 @@ func (m *Model) updateFileModal(msg tea.Msg) (Model, tea.Cmd, bool) {
134136 }
135137 return *m, nil, false
136138 }
139+
140+func (m *Model) fetchModelsCmd() tea.Cmd {
141+ return func() tea.Msg {
142+ models, err := m.provider.ListModels(context.Background())
143+ return modelsFetchedMsg{models: models, err: err}
144+ }
145+}
146+
147+func (m *Model) openModelsModal(models []provider.Model) {
148+ items := make([]modal.Item, len(models))
149+ for i, model := range models {
150+ desc := model.ParamSize
151+ if model.QuantLevel != "" {
152+ if desc != "" {
153+ desc += ", "
154+ }
155+ desc += model.QuantLevel
156+ }
157+ items[i] = modal.Item{Name: model.Name, Desc: desc}
158+ }
159+ searchFn := func(query string) []modal.Item {
160+ if query == "" {
161+ return items
162+ }
163+ q := strings.ToLower(query)
164+ var filtered []modal.Item
165+ for _, item := range items {
166+ name := strings.ToLower(item.Name)
167+ desc := strings.ToLower(item.Desc)
168+ if strings.Contains(name, q) || strings.Contains(desc, q) {
169+ filtered = append(filtered, item)
170+ }
171+ }
172+ return filtered
173+ }
174+ md := modal.New(searchFn, "Model: ", modal.Colors{
175+ Fg: palette.Fg,
176+ Bg: palette.Bg,
177+ Dim: palette.Dim,
178+ SelFg: palette.Bg,
179+ SelBg: palette.Fg,
180+ Border: palette.Border,
181+ })
182+ md.WidthPct = 60
183+ m.modelsModal = &md
184+}
185+
186+func (m *Model) updateModelsModal(msg tea.Msg) (Model, tea.Cmd, bool) {
187+ if m.modelsModal == nil {
188+ return *m, nil, false
189+ }
190+ action, cmd := m.modelsModal.HandleMsg(msg)
191+ switch a := action.(type) {
192+ case modal.ActionClose:
193+ m.modelsModal = nil
194+ return *m, nil, true
195+ case modal.ActionSelect:
196+ m.modelsModal = nil
197+ return *m, m.switchModelCmd(a.Item.Name), true
198+ }
199+ if cmd != nil {
200+ return *m, cmd, true
201+ }
202+ switch msg.(type) {
203+ case tea.KeyPressMsg, tea.MouseMsg:
204+ return *m, nil, true
205+ }
206+ return *m, nil, false
207+}
208+
209+func (m *Model) switchModelCmd(modelName string) tea.Cmd {
210+ registry := m.registry
211+ providerConfigName := m.providerConfigName
212+ providerOpts := m.providerOpts
213+ oldProv := m.provider
214+
215+ return func() tea.Msg {
216+ if registry == nil {
217+ return modelSwitchedMsg{err: provider.ErrProviderNotFound}
218+ }
219+ newProv, err := registry.Create(providerConfigName, modelName, providerOpts)
220+ if err != nil {
221+ return modelSwitchedMsg{err: err}
222+ }
223+ if oldProv != nil {
224+ oldProv.Close()
225+ }
226+ return modelSwitchedMsg{modelName: modelName, prov: newProv}
227+ }
228+}
229+
230+func (m *Model) handleModelsFetched(msg modelsFetchedMsg) tea.Model {
231+ if msg.err != nil {
232+ m.lastNetError = "Failed to list models: " + msg.err.Error()
233+ return m
234+ }
235+ if len(msg.models) == 0 {
236+ m.lastNetError = "No models available"
237+ return m
238+ }
239+ m.openModelsModal(msg.models)
240+ return m
241+}
242+
243+func (m *Model) handleModelSwitched(msg modelSwitchedMsg) tea.Model {
244+ if msg.err != nil {
245+ m.lastNetError = "Failed to switch model: " + msg.err.Error()
246+ return m
247+ }
248+ m.provider = msg.prov
249+ m.currentModelName = msg.modelName
250+ return m
251+}
internal/tui/view.go+2−2
@@ -17,12 +17,12 @@ func (m Model) View() tea.View {
1717 content = m.keybindsModal.View(m.width, m.height)
1818 case m.fileModal != nil:
1919 content = m.fileModal.View(m.width, m.height)
20+ case m.modelsModal != nil:
21+ content = m.modelsModal.View(m.width, m.height)
2022 }
2123 v := tea.NewView(content)
2224 v.AltScreen = true
2325 v.MouseMode = tea.MouseModeAllMotion
24- // Keyboard enhancements are automatically enabled in v2 (Kitty protocol).
25- // This gives us ctrl+shift+c/v disambiguation.
2626 return v
2727 }
2828
internal/tui/view_status.go+6−2
@@ -55,8 +55,12 @@ func (m Model) renderStatusBar(b *strings.Builder, bgFill lipgloss.Style) {
5555 rightParts = append(rightParts, m.styles.Error.Render("✗ "+errText))
5656 }
5757
58- // Provider config name
59- rightParts = append(rightParts, m.styles.StatusText.Render(m.providerConfigName))
58+ // Provider config name + model
59+ providerLabel := m.providerConfigName
60+ if m.currentModelName != "" {
61+ providerLabel += "/" + m.currentModelName
62+ }
63+ rightParts = append(rightParts, m.styles.StatusText.Render(providerLabel))
6064
6165 // Animated braille spinner — red on error, accent otherwise
6266 frame := brailleFrames[m.spinFrame%len(brailleFrames)]