-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlazyollama.go
More file actions
351 lines (289 loc) · 6.79 KB
/
Copy pathlazyollama.go
File metadata and controls
351 lines (289 loc) · 6.79 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package main
import (
"context"
"fmt"
"log"
"os"
"os/exec"
"strings"
"time"
"github.com/jesseduffield/gocui"
)
const (
MODELS_VIEW = "models"
FOOTER_VIEW = "footer"
)
type Model struct {
Name string
Size string
Modified string
Running bool
}
type App struct {
gui *gocui.Gui
models []Model
selectedModel int
ctx context.Context
cancel context.CancelFunc
}
func main() {
app := NewApp()
if err := app.Run(); err != nil {
log.Printf("Error running lazyollama: %v", err)
}
}
func NewApp() *App {
ctx, cancel := context.WithCancel(context.Background())
return &App{
ctx: ctx,
cancel: cancel,
}
}
func (a *App) Run() error {
g, err := gocui.NewGui(gocui.NewGuiOpts{
OutputMode: gocui.OutputNormal,
})
if err != nil {
return fmt.Errorf("failed to create GUI: %w", err)
}
defer g.Close()
a.gui = g
g.Cursor = false
g.Mouse = true
g.SetManagerFunc(a.layout)
// Load initial data
a.refreshModels()
// Start background refresh
go a.backgroundRefresh()
if err := g.MainLoop(); err != nil && !gocui.IsQuit(err) {
return err
}
a.cancel()
return nil
}
func (a *App) layout(g *gocui.Gui) error {
maxX, maxY := g.Size()
// Models panel (full screen minus footer)
if v, err := g.SetView(MODELS_VIEW, 0, 0, maxX-1, maxY-2, 0); err != nil {
if !gocui.IsUnknownView(err) {
return err
}
v.Title = " Models "
v.Highlight = true
v.SelBgColor = gocui.ColorBlue
v.SelFgColor = gocui.ColorWhite
// Set rounded border using custom frame runes
v.FrameRunes = []rune{'─', '│', '╭', '╮', '╰', '╯'}
a.updateModelsView()
if _, err := g.SetCurrentView(MODELS_VIEW); err != nil {
return err
}
// Set keybindings
a.setKeybindings()
}
// Footer (borderless instructions)
if v, err := g.SetView(FOOTER_VIEW, 0, maxY-2, maxX-1, maxY, 0); err != nil {
if !gocui.IsUnknownView(err) {
return err
}
v.Frame = false // Remove border
v.BgColor = gocui.ColorDefault
v.FgColor = gocui.ColorWhite
}
a.updateFooter()
return nil
}
func (a *App) setKeybindings() error {
g := a.gui
// Global keybindings
g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, a.quit)
g.SetKeybinding("", 'q', gocui.ModNone, a.quit)
g.SetKeybinding("", 'r', gocui.ModNone, a.refresh)
// Models view keybindings
g.SetKeybinding(MODELS_VIEW, gocui.KeyArrowDown, gocui.ModNone, a.nextModel)
g.SetKeybinding(MODELS_VIEW, 'j', gocui.ModNone, a.nextModel)
g.SetKeybinding(MODELS_VIEW, gocui.KeyArrowUp, gocui.ModNone, a.prevModel)
g.SetKeybinding(MODELS_VIEW, 'k', gocui.ModNone, a.prevModel)
g.SetKeybinding(MODELS_VIEW, gocui.KeyEnter, gocui.ModNone, a.chatWithModel)
g.SetKeybinding(MODELS_VIEW, 'x', gocui.ModNone, a.stopModel)
return nil
}
func (a *App) refreshModels() {
models, err := a.getOllamaModels()
if err != nil {
return
}
a.models = models
a.updateModelsView()
}
func (a *App) getOllamaModels() ([]Model, error) {
// Get list of models
cmd := exec.Command("ollama", "list")
output, err := cmd.Output()
if err != nil {
return nil, err
}
lines := strings.Split(string(output), "\n")
var models []Model
// Skip header line
for i, line := range lines {
if i == 0 || strings.TrimSpace(line) == "" {
continue
}
fields := strings.Fields(line)
if len(fields) >= 3 {
model := Model{
Name: fields[0],
Size: fields[1],
Modified: strings.Join(fields[2:], " "),
}
models = append(models, model)
}
}
// Check running models
runningModels := a.getRunningModels()
for i, model := range models {
for _, running := range runningModels {
if model.Name == running {
models[i].Running = true
break
}
}
}
return models, nil
}
func (a *App) getRunningModels() []string {
cmd := exec.Command("ollama", "ps")
output, err := cmd.Output()
if err != nil {
return nil
}
lines := strings.Split(string(output), "\n")
var running []string
for i, line := range lines {
if i == 0 || strings.TrimSpace(line) == "" {
continue
}
fields := strings.Fields(line)
if len(fields) > 0 {
running = append(running, fields[0])
}
}
return running
}
func (a *App) updateModelsView() {
v, err := a.gui.View(MODELS_VIEW)
if err != nil {
return
}
v.Clear()
for _, model := range a.models {
status := "○"
if model.Running {
status = "●"
}
fmt.Fprintf(v, " %s %s\n", status, model.Name)
}
// Set cursor to selected model
if len(a.models) > 0 && a.selectedModel < len(a.models) {
v.SetCursor(0, a.selectedModel)
}
}
func (a *App) updateFooter() {
v, err := a.gui.View(FOOTER_VIEW)
if err != nil {
return
}
v.Clear()
if len(a.models) > 0 && a.selectedModel < len(a.models) && a.models[a.selectedModel].Running {
fmt.Fprintf(v, " [j/k] navigate [Enter] chat [x] stop [r] refresh [q] quit")
} else {
fmt.Fprintf(v, " [j/k] navigate [Enter] chat [r] refresh [q] quit")
}
}
func (a *App) backgroundRefresh() {
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
for {
select {
case <-a.ctx.Done():
return
case <-ticker.C:
a.gui.Update(func(g *gocui.Gui) error {
a.refreshModels()
return nil
})
}
}
}
// Event handlers
func (a *App) quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}
func (a *App) refresh(g *gocui.Gui, v *gocui.View) error {
a.refreshModels()
return nil
}
func (a *App) nextModel(g *gocui.Gui, v *gocui.View) error {
if len(a.models) > 0 && a.selectedModel < len(a.models)-1 {
a.selectedModel++
a.updateModelsView()
a.updateFooter()
}
return nil
}
func (a *App) prevModel(g *gocui.Gui, v *gocui.View) error {
if len(a.models) > 0 && a.selectedModel > 0 {
a.selectedModel--
a.updateModelsView()
a.updateFooter()
}
return nil
}
func (a *App) chatWithModel(g *gocui.Gui, v *gocui.View) error {
if len(a.models) == 0 || a.selectedModel >= len(a.models) {
return nil
}
model := a.models[a.selectedModel]
// Temporarily close the GUI for chat
g.Close()
// Run ollama with proper stdin/stdout/stderr
cmd := exec.Command("ollama", "run", model.Name)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
// Restart the GUI after chat
newGui, err := gocui.NewGui(gocui.NewGuiOpts{
OutputMode: gocui.OutputNormal,
})
if err != nil {
return err
}
a.gui = newGui
a.gui.Cursor = false
a.gui.Mouse = true
a.gui.SetManagerFunc(a.layout)
// Refresh models and restart
a.refreshModels()
return a.gui.MainLoop()
}
func (a *App) stopModel(g *gocui.Gui, v *gocui.View) error {
if len(a.models) == 0 || a.selectedModel >= len(a.models) {
return nil
}
model := a.models[a.selectedModel]
if !model.Running {
return nil
}
go func() {
cmd := exec.Command("ollama", "stop", model.Name)
if err := cmd.Run(); err == nil {
a.gui.Update(func(g *gocui.Gui) error {
a.refreshModels()
return nil
})
}
}()
return nil
}