-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
194 lines (160 loc) · 4.46 KB
/
provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"fmt"
"log/slog"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss"
bolt "go.etcd.io/bbolt"
)
type llmProvider interface {
name() string
availableModels() []string
isConfigured() bool
form(int, int, *huh.KeyMap) *huh.Form
saveForm(*bolt.DB, *huh.Form) (llmProvider, bool, error)
Title() string
Description() string
list.Item
new(llmSetting) llm
supportEmbedding() bool
newEmbedder(llmSetting) embedder
}
const (
providerOllama = "Ollama"
providerAnthropic = "Anthropic"
providerOpenAI = "OpenAI"
)
func loadLLMProviders(db *bolt.DB) ([]llmProvider, error) {
o, err := loadOllamaSettings(db)
if err != nil {
return nil, fmt.Errorf("failed to load ollama settings: %w", err)
}
a, err := loadAnthropicSettings(db)
if err != nil {
return nil, fmt.Errorf("failed to load anthropic settings: %w", err)
}
oa, err := loadOpenAISettings(db)
if err != nil {
return nil, fmt.Errorf("failed to load openai settings: %w", err)
}
return []llmProvider{o, a, oa}, nil
}
func (m mainModel) providersIsConfigured() bool {
for _, p := range m.providers {
if p.isConfigured() {
return true
}
}
return false
}
func (m mainModel) initProviders() (mainModel, error) {
var err error
m.providers, err = loadLLMProviders(m.db)
if err != nil {
return mainModel{}, fmt.Errorf("failed to load llm providers: %w", err)
}
items := make([]list.Item, 0, len(m.providers))
for _, item := range m.providers {
items = append(items, item)
}
m.providersList = defaultList("Providers", m.keymap, func() []key.Binding {
return []key.Binding{
m.keymap.escape,
}
}, func() []key.Binding {
return []key.Binding{
m.keymap.pick,
m.keymap.escape,
}
})
m.providersList.SetItems(items)
m.providersList.SetFilteringEnabled(false)
m.providersList.SetShowStatusBar(false)
return m, nil
}
func (m mainModel) updateProvidersSize() mainModel {
height := m.height - logoHeight()
if m.err != nil {
height -= errHeight(m.width, m.err)
}
m.providersList.SetSize(m.width, height)
return m
}
func (m mainModel) handleProvidersEvents(msg tea.Msg) (mainModel, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m = m.updateProvidersSize()
case tea.KeyMsg:
switch {
case key.Matches(msg, m.keymap.escape):
return m.setViewState(viewStateOptions).updateOptionsSize(), nil
case key.Matches(msg, m.keymap.pick):
return m.selectProvider(m.providersList.Index())
}
}
var cmd tea.Cmd
m.providersList, cmd = m.providersList.Update(msg)
return m, cmd
}
func (m mainModel) providersView() string {
return lipgloss.JoinVertical(lipgloss.Left,
logoView(),
m.providersList.View(),
)
}
func (m mainModel) selectProvider(index int) (mainModel, tea.Cmd) {
m.selectedProviderIndex = index
return m.setViewState(viewStateProviderForm).
updateFormSize().
newProviderForm()
}
func (m mainModel) newProviderForm() (mainModel, tea.Cmd) {
selectedProvider := m.providers[m.selectedProviderIndex]
m.providerForm = selectedProvider.form(m.formWidth, m.formHeight, m.keymap.formKeymap)
return m, m.providerForm.PrevField()
}
func (m mainModel) handleProviderFormEvents(msg tea.Msg) (mainModel, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m = m.updateFormSize()
case tea.KeyMsg:
if key.Matches(msg, m.keymap.escape) {
return m.setViewState(viewStateProviders), nil
}
}
form, cmd := m.providerForm.Update(msg)
if f, ok := form.(*huh.Form); ok {
m.providerForm = f
}
if m.providerForm.State != huh.StateCompleted {
return m, cmd
}
provider, confirmed, err := m.providers[m.selectedProviderIndex].saveForm(m.db, m.providerForm)
if err != nil {
m.err = fmt.Errorf("error saving provider settings: %w", err)
slog.Error(m.err.Error())
return m.updateFormSize(), nil
}
if !confirmed {
return m.setViewState(viewStateProviders), nil
}
m.providers[m.selectedProviderIndex] = provider
m.providersList.SetItem(m.selectedProviderIndex, provider)
// We need to refresh the optionsList
return m.initOptions().setViewState(viewStateProviders), nil
}
func (m mainModel) providerFormView() string {
selectedProvider := m.providers[m.selectedProviderIndex]
title := selectedProvider.Title()
if selectedProvider.isConfigured() {
title = "Edit " + title
}
return lipgloss.JoinVertical(lipgloss.Left,
logoView(),
titleStyle.Render(title),
m.providerForm.View(),
)
}