-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from azevedoguigo/development
v0.1.0
- Loading branch information
Showing
13 changed files
with
749 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/azevedoguigo/ollama-chat-tui/internal/app" | ||
) | ||
|
||
func main() { | ||
if err := app.Run(); err != nil { | ||
log.Fatalf("Erro ao rodar a aplicação: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,19 @@ | ||
module github.com/azevedoguigo/ollama-chat-tui | ||
|
||
go 1.23.4 | ||
|
||
require ( | ||
github.com/google/uuid v1.6.0 | ||
github.com/rivo/tview v0.0.0-20241227133733-17b7edb88c57 | ||
) | ||
|
||
require ( | ||
github.com/gdamore/encoding v1.0.0 // indirect | ||
github.com/gdamore/tcell/v2 v2.7.1 | ||
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect | ||
github.com/mattn/go-runewidth v0.0.15 // indirect | ||
github.com/rivo/uniseg v0.4.7 // indirect | ||
golang.org/x/sys v0.17.0 // indirect | ||
golang.org/x/term v0.17.0 // indirect | ||
golang.org/x/text v0.14.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko= | ||
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg= | ||
github.com/gdamore/tcell v1.4.0 h1:vUnHwJRvcPQa3tzi+0QI4U9JINXYJlOz9yiaiPQ2wMU= | ||
github.com/gdamore/tcell v1.4.0/go.mod h1:vxEiSDZdW3L+Uhjii9c3375IlDmR05bzxY404ZVSMo0= | ||
github.com/gdamore/tcell/v2 v2.7.1 h1:TiCcmpWHiAU7F0rA2I3S2Y4mmLmO9KHxJ7E1QhYzQbc= | ||
github.com/gdamore/tcell/v2 v2.7.1/go.mod h1:dSXtXTSK0VsW1biw65DZLZ2NKr7j0qP/0J7ONmsraWg= | ||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= | ||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= | ||
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= | ||
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= | ||
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= | ||
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= | ||
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= | ||
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= | ||
github.com/rivo/tview v0.0.0-20241227133733-17b7edb88c57 h1:LmsF7Fk5jyEDhJk0fYIqdWNuTxSyid2W42A0L2YWjGE= | ||
github.com/rivo/tview v0.0.0-20241227133733-17b7edb88c57/go.mod h1:02iFIz7K/A9jGCvrizLPvoqr4cEIx7q54RH5Qudkrss= | ||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= | ||
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= | ||
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= | ||
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= | ||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= | ||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= | ||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= | ||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= | ||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= | ||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= | ||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= | ||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= | ||
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= | ||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= | ||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= | ||
golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U= | ||
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= | ||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= | ||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= | ||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= | ||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= | ||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= | ||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= | ||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= | ||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
package app | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/azevedoguigo/ollama-chat-tui/internal/handler" | ||
"github.com/azevedoguigo/ollama-chat-tui/internal/ollama" | ||
"github.com/azevedoguigo/ollama-chat-tui/internal/storage" | ||
"github.com/azevedoguigo/ollama-chat-tui/internal/ui" | ||
"github.com/gdamore/tcell/v2" | ||
"github.com/rivo/tview" | ||
) | ||
|
||
func Run() error { | ||
configDir := ".deepseek-tui" | ||
chatsDir := "chats" | ||
|
||
chatManager, err := handler.NewChatManager(configDir, chatsDir) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
currentModel := "deepseek-r1" | ||
availableModels := []string{"deepseek-r1", "gemma2", "mistral"} | ||
|
||
chatList := ui.NewChatList(chatManager) | ||
chatView := ui.NewChatView() | ||
inputField := ui.NewInputField() | ||
|
||
chatFlex := tview.NewFlex().SetDirection(tview.FlexRow). | ||
AddItem(chatView.GetPrimitive(), 0, 1, false). | ||
AddItem(inputField.GetPrimitive(), 3, 1, true) | ||
|
||
mainChatLayout := tview.NewFlex(). | ||
AddItem(chatList.GetPrimitive(), 20, 1, false). | ||
AddItem(chatFlex, 0, 1, true) | ||
|
||
pages := tview.NewPages(). | ||
AddPage("chat", mainChatLayout, true, true) | ||
|
||
app := tview.NewApplication() | ||
|
||
var currentChat *storage.ChatSession | ||
|
||
updateChatView := func() { | ||
if currentChat != nil { | ||
chatView.Update(currentChat) | ||
} | ||
} | ||
|
||
inputField.SetDoneFunc(func(key tcell.Key) { | ||
if key == tcell.KeyEnter { | ||
userInput := inputField.GetText() | ||
inputField.SetText("") | ||
if userInput == "" { | ||
return | ||
} | ||
|
||
if currentChat == nil { | ||
title := fmt.Sprintf("Chat %d", len(chatManager.GetAllChats())+1) | ||
currentChat = chatManager.AddChat(title) | ||
chatList.Refresh() | ||
} | ||
|
||
chatID := currentChat.ID.String() | ||
|
||
if err := chatManager.AppendMessage(chatID, "user", userInput); err != nil { | ||
chatView.SetText(fmt.Sprintf("[red]Error adding message: %v[-]", err)) | ||
return | ||
} | ||
|
||
if err := chatManager.AppendMessage(chatID, "assistant", ""); err != nil { | ||
chatView.SetText(fmt.Sprintf("[red]Error creating response message: %v[-]", err)) | ||
return | ||
} | ||
|
||
updateChatView() | ||
|
||
history := make([]storage.Message, len(currentChat.Messages)) | ||
copy(history, currentChat.Messages) | ||
|
||
go func() { | ||
err := ollama.QueryOllamaStream(currentModel, history[:len(history)-1], func(chunk string) { | ||
if err := chatManager.UpdateLastMessage(chatID, chunk); err != nil { | ||
log.Printf("Error updating message: %v", err) | ||
} | ||
|
||
updatedChat, _ := chatManager.GetChatByID(chatID) | ||
currentChat = updatedChat | ||
app.QueueUpdateDraw(func() { | ||
updateChatView() | ||
}) | ||
}) | ||
if err != nil { | ||
errMsg := fmt.Sprintf("\n\n[red]Error: %v[-]", err) | ||
if updateErr := chatManager.UpdateLastMessage(chatID, errMsg); updateErr != nil { | ||
log.Printf("Error updating message with error: %v", updateErr) | ||
} | ||
app.QueueUpdateDraw(func() { | ||
updateChatView() | ||
}) | ||
} | ||
}() | ||
} | ||
}) | ||
|
||
chatList.SetSelectedFunc(func(index int, mainText, secondaryText string, shortcut rune) { | ||
if index == 0 { | ||
currentChat = nil | ||
chatView.Clear() | ||
inputField.SetText("") | ||
} else { | ||
chat, exists := chatManager.GetChatByID(secondaryText) | ||
if exists { | ||
currentChat = chat | ||
updateChatView() | ||
} | ||
} | ||
}) | ||
|
||
chatList.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { | ||
if event.Key() == tcell.KeyDelete || event.Rune() == 'd' { | ||
currentItem := chatList.GetCurrentItemIndex() | ||
if currentItem > 0 { | ||
chatID := chatList.GetSecondaryText(currentItem) | ||
modal := tview.NewModal(). | ||
SetText("Delete this chat permanently?"). | ||
AddButtons([]string{"Cancel", "Delete"}). | ||
SetDoneFunc(func(buttonIndex int, buttonLabel string) { | ||
if buttonLabel == "Delete" { | ||
if err := chatManager.DeleteChat(chatID); err != nil { | ||
chatView.SetText(fmt.Sprintf("[red]Error deleting chat: %v[-]", err)) | ||
} else { | ||
if currentChat != nil && currentChat.ID.String() == chatID { | ||
currentChat = nil | ||
chatView.Clear() | ||
} | ||
chatList.Refresh() | ||
} | ||
} | ||
app.SetRoot(pages, true) | ||
}) | ||
app.SetRoot(modal, false) | ||
} | ||
return nil | ||
} | ||
if event.Rune() == 's' { | ||
openSettings(app, pages, currentModel, availableModels, func(newModel string) { | ||
currentModel = newModel | ||
pages.SwitchToPage("chat") | ||
app.SetFocus(inputField.GetPrimitive()) | ||
}) | ||
return nil | ||
} | ||
return event | ||
}) | ||
|
||
mainChatLayout.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { | ||
if event.Rune() == rune(tcell.KeyCtrlS) { | ||
openSettings(app, pages, currentModel, availableModels, func(newModel string) { | ||
currentModel = newModel | ||
pages.SwitchToPage("chat") | ||
app.SetFocus(inputField.GetPrimitive()) | ||
}) | ||
return nil | ||
} | ||
return event | ||
}) | ||
|
||
return app.SetRoot(pages, true).EnableMouse(true).Run() | ||
} | ||
|
||
func openSettings( | ||
app *tview.Application, | ||
pages *tview.Pages, | ||
currentModel string, | ||
availableModels []string, | ||
onSave func(newModel string), | ||
) { | ||
settingsPage := ui.NewSettingsPage(currentModel, availableModels, onSave) | ||
pages.AddAndSwitchToPage("settings", settingsPage.GetPrimitive(), true) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package handler | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
"sync" | ||
"time" | ||
|
||
"github.com/azevedoguigo/ollama-chat-tui/internal/storage" | ||
"github.com/google/uuid" | ||
) | ||
|
||
type ChatManager struct { | ||
chats map[string]*storage.ChatSession | ||
mutex sync.RWMutex | ||
configDir string | ||
chatsDir string | ||
} | ||
|
||
func NewChatManager(configDir, chatsDir string) (*ChatManager, error) { | ||
chats, err := storage.LoadChats(configDir, chatsDir) | ||
if err != nil { | ||
chats = make(map[string]*storage.ChatSession) | ||
} | ||
return &ChatManager{ | ||
chats: chats, | ||
configDir: configDir, | ||
chatsDir: chatsDir, | ||
}, nil | ||
} | ||
|
||
func (cm *ChatManager) AddChat(title string) *storage.ChatSession { | ||
cm.mutex.Lock() | ||
defer cm.mutex.Unlock() | ||
|
||
chat := &storage.ChatSession{ | ||
ID: uuid.New(), | ||
Title: title, | ||
Messages: []storage.Message{}, | ||
CreatedAt: time.Now(), | ||
} | ||
cm.chats[chat.ID.String()] = chat | ||
|
||
if err := storage.SaveChat(cm.configDir, cm.chatsDir, chat); err != nil { | ||
fmt.Printf("Error when saving new chat: %v\n", err) | ||
} | ||
return chat | ||
} | ||
|
||
func (cm *ChatManager) GetChatByID(id string) (*storage.ChatSession, bool) { | ||
cm.mutex.RLock() | ||
defer cm.mutex.RUnlock() | ||
chat, exists := cm.chats[id] | ||
return chat, exists | ||
} | ||
|
||
func (cm *ChatManager) GetAllChats() []*storage.ChatSession { | ||
cm.mutex.RLock() | ||
defer cm.mutex.RUnlock() | ||
var chatsList []*storage.ChatSession | ||
for _, chat := range cm.chats { | ||
chatsList = append(chatsList, chat) | ||
} | ||
sort.Slice(chatsList, func(i, j int) bool { | ||
return chatsList[i].CreatedAt.Before(chatsList[j].CreatedAt) | ||
}) | ||
return chatsList | ||
} | ||
|
||
func (cm *ChatManager) AppendMessage(chatID, role, content string) error { | ||
cm.mutex.Lock() | ||
defer cm.mutex.Unlock() | ||
chat, exists := cm.chats[chatID] | ||
if !exists { | ||
return fmt.Errorf("chat with ID %s not found", chatID) | ||
} | ||
chat.Messages = append(chat.Messages, storage.Message{ | ||
Role: role, | ||
Content: content, | ||
}) | ||
return storage.SaveChat(cm.configDir, cm.chatsDir, chat) | ||
} | ||
|
||
func (cm *ChatManager) UpdateLastMessage(chatID, content string) error { | ||
cm.mutex.Lock() | ||
defer cm.mutex.Unlock() | ||
chat, exists := cm.chats[chatID] | ||
if !exists { | ||
return fmt.Errorf("chat with ID %s not found", chatID) | ||
} | ||
if len(chat.Messages) == 0 { | ||
return fmt.Errorf("the chat has no messages to update") | ||
} | ||
chat.Messages[len(chat.Messages)-1].Content += content | ||
return storage.SaveChat(cm.configDir, cm.chatsDir, chat) | ||
} | ||
|
||
func (cm *ChatManager) DeleteChat(chatID string) error { | ||
cm.mutex.Lock() | ||
defer cm.mutex.Unlock() | ||
chat, exists := cm.chats[chatID] | ||
if !exists { | ||
return fmt.Errorf("chat with ID %s not found", chatID) | ||
} | ||
if err := storage.DeleteChat(chat, cm.chats); err != nil { | ||
return err | ||
} | ||
delete(cm.chats, chatID) | ||
return nil | ||
} |
Oops, something went wrong.