-
Notifications
You must be signed in to change notification settings - Fork 4
/
tui.go
190 lines (163 loc) · 4.57 KB
/
tui.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
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/adrg/xdg"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss"
"github.com/gaurav-gosain/gollama/internal/api"
"github.com/gaurav-gosain/gollama/internal/chat"
"github.com/gaurav-gosain/gollama/internal/chatpicker"
"github.com/gaurav-gosain/gollama/internal/client"
"github.com/gaurav-gosain/gollama/internal/utils"
zone "github.com/lrstanley/bubblezone"
)
// The entry point for the TUI
func tui() {
err := client.GollamaInstance.InitDB() // initializes and migrates the sqlite database
if err != nil {
utils.PrintError(err, true)
}
defer client.GollamaInstance.DB.Close()
// keeps the TUI running until the user explicitly exits (or an error occurs)
for {
chats, err := client.GollamaInstance.ListChats()
if err != nil {
utils.PrintError(err, true)
}
var chatSettings client.Chat
var exitReason chatpicker.ExitReason
// if we have at least one chat, we show the chat picker, else show the new chat screen
if len(chats) > 0 {
items := []list.Item{}
for _, chat := range chats {
items = append(items, list.Item(chat))
}
// spawns a chat picker with the list of chats
var chatPicker client.Chat
chatPicker, exitReason, err = chatpicker.NewChatPicker(items)
if err != nil {
utils.PrintError(err, true)
}
switch exitReason {
case chatpicker.ExitReasonError:
fmt.Println("Error")
case chatpicker.ExitReasonCancel:
fmt.Println("Cancelled")
case chatpicker.ExitReasonDeleteChat:
title := lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("205")).
Render(chatPicker.ChatTitle)
deleteChat := true
form := huh.NewForm(
huh.NewGroup(
huh.NewConfirm().
Title("Exit").
Description("Do you want to delete " + title + "?").
Value(&deleteChat),
),
).WithProgramOptions(tea.WithAltScreen())
if err := form.Run(); err != nil {
utils.PrintError(err, true)
}
if deleteChat {
// remove chat from disk
if err := os.Remove(filepath.Join(
xdg.DataHome,
"gollama",
"chats",
chatPicker.ID+".gob",
)); err != nil {
utils.PrintError(err, true)
}
// delete chat from db
if err := client.GollamaInstance.DeleteChat(chatPicker.ID); err != nil {
utils.PrintError(err, true)
}
fmt.Println(
lipgloss.NewStyle().Padding(1, 2).Render(
fmt.Sprint(
"Chat ",
title,
" deleted successfully",
),
),
)
}
case chatpicker.ExitReasonSelect:
chatSettings = chatPicker
case chatpicker.ExitReasonNewChat:
chatSettings, err = chat.NewChatSettingsForm()
if err != nil {
utils.PrintError(err, true)
}
}
} else {
// default case is to start a new chat (i.e. when no chats exist)
exitReason = chatpicker.ExitReasonNewChat
chatSettings, err = chat.NewChatSettingsForm()
if err != nil {
utils.PrintError(err, true)
}
}
// if a new chat is selected or a chat is created, we start the chat
if exitReason == chatpicker.ExitReasonNewChat ||
exitReason == chatpicker.ExitReasonSelect {
gollamaChat := chat.NewChat(chatSettings)
p := tea.NewProgram(
gollamaChat,
tea.WithAltScreen(),
tea.WithMouseCellMotion(),
)
ollamaAPI, err := api.NewOllamaAPI()
if err != nil {
utils.PrintError(err, true)
}
// creates a new client and connects it an instance of the ollamaAPI
client.GollamaInstance.Connect(ollamaAPI, p)
// bubblezone is used to add mouse interactivity to the TUI
zone.NewGlobal()
var m tea.Model
if m, err = p.Run(); err != nil {
utils.PrintError(err, true)
}
gollamaChat = m.(*chat.Chat)
// save the chat history to a .gob file if the chat is not anonymous
if !gollamaChat.ChatSettings.IsAnonymous {
// dump chat history to .gob file
file, err := os.Create(filepath.Join(
xdg.DataHome,
"gollama",
"chats",
gollamaChat.ChatSettings.ID+".gob",
))
if err != nil {
utils.PrintError(err, true)
}
defer file.Close() //nolint:errcheck
if err := chat.EncodeGob(file, &gollamaChat.ChatHistory); err != nil {
utils.PrintError(err, true)
}
}
}
wantToExit := true
form := huh.NewForm(
huh.NewGroup(
huh.NewConfirm().
Title("Exit").
Description("Do you want to exit gollama?").
Value(&wantToExit),
),
)
if err := form.Run(); err != nil {
utils.PrintError(err, true)
}
if wantToExit {
return
}
}
}