2024-03-12 01:10:54 -06:00
|
|
|
package tui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2024-09-15 18:48:45 -06:00
|
|
|
"git.mlow.ca/mlow/lmcli/pkg/api"
|
2024-03-12 01:10:54 -06:00
|
|
|
"git.mlow.ca/mlow/lmcli/pkg/lmcli"
|
2024-09-15 18:48:45 -06:00
|
|
|
"git.mlow.ca/mlow/lmcli/pkg/tui/model"
|
2024-05-30 00:44:40 -06:00
|
|
|
"git.mlow.ca/mlow/lmcli/pkg/tui/shared"
|
2024-09-22 17:34:53 -06:00
|
|
|
tuiutil "git.mlow.ca/mlow/lmcli/pkg/tui/util"
|
2024-05-30 00:44:40 -06:00
|
|
|
"git.mlow.ca/mlow/lmcli/pkg/tui/views/chat"
|
|
|
|
"git.mlow.ca/mlow/lmcli/pkg/tui/views/conversations"
|
2024-03-12 01:10:54 -06:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2024-09-22 17:34:53 -06:00
|
|
|
"github.com/charmbracelet/lipgloss"
|
2024-03-12 01:10:54 -06:00
|
|
|
)
|
|
|
|
|
2024-09-15 18:48:45 -06:00
|
|
|
type LaunchOptions struct {
|
|
|
|
InitialConversation *api.Conversation
|
|
|
|
InitialView shared.View
|
|
|
|
}
|
2024-03-29 18:41:12 -06:00
|
|
|
|
2024-09-15 18:48:45 -06:00
|
|
|
type Model struct {
|
2024-09-16 09:40:04 -06:00
|
|
|
App *model.AppModel
|
2024-09-16 08:04:08 -06:00
|
|
|
|
2024-09-22 17:34:53 -06:00
|
|
|
// window size
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
|
|
|
|
// errors we will display to the user and allow them to dismiss
|
|
|
|
errs []error
|
|
|
|
|
2024-09-16 09:40:04 -06:00
|
|
|
activeView shared.View
|
|
|
|
views map[shared.View]shared.ViewModel
|
2024-03-31 17:51:45 -06:00
|
|
|
}
|
2024-03-12 01:10:54 -06:00
|
|
|
|
2024-09-22 17:34:53 -06:00
|
|
|
func initialModel(ctx *lmcli.Context, opts LaunchOptions) *Model {
|
2024-09-16 09:40:04 -06:00
|
|
|
app := &model.AppModel{
|
|
|
|
Ctx: ctx,
|
|
|
|
Conversation: opts.InitialConversation,
|
|
|
|
}
|
2024-09-22 21:00:03 -06:00
|
|
|
app.NewConversation()
|
2024-09-16 09:40:04 -06:00
|
|
|
|
2024-05-30 00:44:40 -06:00
|
|
|
m := Model{
|
2024-09-16 09:40:04 -06:00
|
|
|
App: app,
|
|
|
|
activeView: opts.InitialView,
|
|
|
|
views: map[shared.View]shared.ViewModel{
|
2024-09-22 17:34:53 -06:00
|
|
|
shared.ViewChat: chat.Chat(app),
|
|
|
|
shared.ViewConversations: conversations.Conversations(app),
|
2024-03-31 17:51:45 -06:00
|
|
|
},
|
2024-03-30 20:03:53 -06:00
|
|
|
}
|
2024-05-30 00:44:40 -06:00
|
|
|
|
2024-09-22 17:34:53 -06:00
|
|
|
return &m
|
2024-03-29 14:43:19 -06:00
|
|
|
}
|
2024-03-12 01:10:54 -06:00
|
|
|
|
2024-09-22 17:34:53 -06:00
|
|
|
func (m *Model) Init() tea.Cmd {
|
|
|
|
cmds := []tea.Cmd{
|
2024-04-01 11:03:49 -06:00
|
|
|
func() tea.Msg {
|
2024-09-16 09:40:04 -06:00
|
|
|
return shared.MsgViewChange(m.activeView)
|
2024-04-01 11:03:49 -06:00
|
|
|
},
|
2024-09-22 17:34:53 -06:00
|
|
|
}
|
|
|
|
for _, v := range m.views {
|
|
|
|
cmds = append(cmds, v.Init())
|
|
|
|
}
|
|
|
|
return tea.Batch(cmds...)
|
2024-03-12 01:10:54 -06:00
|
|
|
}
|
|
|
|
|
2024-09-16 09:40:04 -06:00
|
|
|
func (m *Model) handleGlobalInput(msg tea.KeyMsg) tea.Cmd {
|
|
|
|
view, cmd := m.views[m.activeView].Update(msg)
|
|
|
|
m.views[m.activeView] = view
|
|
|
|
if cmd != nil {
|
|
|
|
return cmd
|
2024-03-29 18:41:12 -06:00
|
|
|
}
|
2024-09-16 09:40:04 -06:00
|
|
|
|
2024-03-31 19:06:13 -06:00
|
|
|
switch msg.String() {
|
|
|
|
case "ctrl+c", "ctrl+q":
|
2024-09-16 09:40:04 -06:00
|
|
|
return tea.Quit
|
2024-03-31 19:06:13 -06:00
|
|
|
}
|
2024-09-16 09:40:04 -06:00
|
|
|
return nil
|
2024-03-29 18:41:12 -06:00
|
|
|
}
|
|
|
|
|
2024-09-22 17:34:53 -06:00
|
|
|
func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
2024-03-29 18:41:12 -06:00
|
|
|
switch msg := msg.(type) {
|
2024-09-22 17:34:53 -06:00
|
|
|
case tea.WindowSizeMsg:
|
|
|
|
m.width, m.height = msg.Width, msg.Height
|
2024-03-29 18:41:12 -06:00
|
|
|
case tea.KeyMsg:
|
2024-09-16 09:40:04 -06:00
|
|
|
cmd := m.handleGlobalInput(msg)
|
|
|
|
if cmd != nil {
|
2024-03-29 18:41:12 -06:00
|
|
|
return m, cmd
|
|
|
|
}
|
2024-05-30 00:44:40 -06:00
|
|
|
case shared.MsgViewChange:
|
2024-09-16 09:40:04 -06:00
|
|
|
m.activeView = shared.View(msg)
|
2024-09-22 17:34:53 -06:00
|
|
|
return m, tea.Batch(tea.WindowSize(), shared.ViewEnter())
|
|
|
|
case shared.MsgError:
|
|
|
|
m.errs = append(m.errs, msg)
|
2024-03-29 18:41:12 -06:00
|
|
|
}
|
|
|
|
|
2024-09-16 09:40:04 -06:00
|
|
|
view, cmd := m.views[m.activeView].Update(msg)
|
|
|
|
m.views[m.activeView] = view
|
|
|
|
return m, cmd
|
2024-03-12 01:10:54 -06:00
|
|
|
}
|
|
|
|
|
2024-09-22 17:34:53 -06:00
|
|
|
func (m *Model) View() string {
|
|
|
|
if m.width == 0 || m.height == 0 {
|
|
|
|
// we're dimensionless!
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
header := m.views[m.activeView].Header(m.width)
|
|
|
|
footer := m.views[m.activeView].Footer(m.width)
|
|
|
|
fixedUIHeight := tuiutil.Height(header) + tuiutil.Height(footer)
|
|
|
|
|
|
|
|
errBanners := make([]string, len(m.errs))
|
|
|
|
for idx, err := range m.errs {
|
|
|
|
errBanners[idx] = tuiutil.ErrorBanner(err, m.width)
|
|
|
|
fixedUIHeight += tuiutil.Height(errBanners[idx])
|
|
|
|
}
|
|
|
|
|
|
|
|
content := m.views[m.activeView].Content(m.width, m.height-fixedUIHeight)
|
|
|
|
|
|
|
|
sections := make([]string, 0, 4)
|
|
|
|
if header != "" {
|
|
|
|
sections = append(sections, header)
|
|
|
|
}
|
|
|
|
if content != "" {
|
|
|
|
sections = append(sections, content)
|
|
|
|
}
|
|
|
|
if len(errBanners) > 0 {
|
|
|
|
sections = append(sections, lipgloss.JoinVertical(lipgloss.Left, errBanners...))
|
|
|
|
}
|
|
|
|
if footer != "" {
|
|
|
|
sections = append(sections, footer)
|
|
|
|
}
|
|
|
|
return lipgloss.JoinVertical(lipgloss.Left, sections...)
|
2024-03-12 01:10:54 -06:00
|
|
|
}
|
|
|
|
|
2024-09-15 18:48:45 -06:00
|
|
|
type LaunchOption func(*LaunchOptions)
|
|
|
|
|
|
|
|
func WithInitialConversation(conv *api.Conversation) LaunchOption {
|
|
|
|
return func(opts *LaunchOptions) {
|
|
|
|
opts.InitialConversation = conv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithInitialView(view shared.View) LaunchOption {
|
|
|
|
return func(opts *LaunchOptions) {
|
|
|
|
opts.InitialView = view
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Launch(ctx *lmcli.Context, options ...LaunchOption) error {
|
|
|
|
opts := &LaunchOptions{
|
2024-09-16 09:40:04 -06:00
|
|
|
InitialView: shared.ViewChat,
|
2024-09-15 18:48:45 -06:00
|
|
|
}
|
|
|
|
for _, opt := range options {
|
|
|
|
opt(opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
program := tea.NewProgram(initialModel(ctx, *opts), tea.WithAltScreen())
|
|
|
|
if _, err := program.Run(); err != nil {
|
2024-03-12 01:10:54 -06:00
|
|
|
return fmt.Errorf("Error running program: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|