From 387dd7534c7973f7b34f889a837a13b9a7b25a40 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Wed, 13 Mar 2024 23:07:29 +0000 Subject: [PATCH] tui: generate titles for conversations --- pkg/tui/tui.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/pkg/tui/tui.go b/pkg/tui/tui.go index 9e1c828..fcc3990 100644 --- a/pkg/tui/tui.go +++ b/pkg/tui/tui.go @@ -12,6 +12,7 @@ import ( "fmt" "strings" + cmdutil "git.mlow.ca/mlow/lmcli/pkg/cmd/util" "git.mlow.ca/mlow/lmcli/pkg/lmcli" models "git.mlow.ca/mlow/lmcli/pkg/lmcli/model" "github.com/charmbracelet/bubbles/textarea" @@ -66,6 +67,8 @@ type ( msgAssistantReply models.Message // sent when a conversation is (re)loaded msgConversationLoaded *models.Conversation + // sent when a new conversation title is set + msgConversationTitleChanged string // send when a conversation's messages are laoded msgMessagesLoaded []models.Message // sent when an error occurs @@ -180,12 +183,25 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } } + if m.conversation.Title == "" { + cmds = append(cmds, m.generateConversationTitle()) + } + m.updateContent() cmds = append(cmds, m.waitForReply()) case msgResponseEnd: m.replyCancelFunc = nil m.waitingForReply = false m.status = "Press ctrl+s to send" + case msgConversationTitleChanged: + title := string(msg) + m.conversation.Title = title + if m.persistence { + err := m.ctx.Store.SaveConversation(m.conversation) + if err != nil { + cmds = append(cmds, wrapError(err)) + } + } } if len(cmds) > 0 { @@ -410,6 +426,16 @@ func (m *model) waitForChunk() tea.Cmd { } } +func (m *model) generateConversationTitle() tea.Cmd { + return func() tea.Msg { + title, err := cmdutil.GenerateTitle(m.ctx, m.conversation) + if err != nil { + return msgError(err) + } + return msgConversationTitleChanged(title) + } +} + func (m *model) promptLLM() tea.Cmd { return func() tea.Msg { completionProvider, err := m.ctx.GetCompletionProvider(*m.ctx.Config.Defaults.Model)