chat.go

  1package page
  2
  3import (
  4	"context"
  5
  6	"github.com/charmbracelet/bubbles/key"
  7	tea "github.com/charmbracelet/bubbletea"
  8	"github.com/kujtimiihoxha/opencode/internal/app"
  9	"github.com/kujtimiihoxha/opencode/internal/session"
 10	"github.com/kujtimiihoxha/opencode/internal/tui/components/chat"
 11	"github.com/kujtimiihoxha/opencode/internal/tui/layout"
 12	"github.com/kujtimiihoxha/opencode/internal/tui/util"
 13)
 14
 15var ChatPage PageID = "chat"
 16
 17type chatPage struct {
 18	app         *app.App
 19	editor      layout.Container
 20	messages    layout.Container
 21	layout      layout.SplitPaneLayout
 22	session     session.Session
 23	editingMode bool
 24}
 25
 26type ChatKeyMap struct {
 27	NewSession key.Binding
 28	Cancel     key.Binding
 29}
 30
 31var keyMap = ChatKeyMap{
 32	NewSession: key.NewBinding(
 33		key.WithKeys("ctrl+n"),
 34		key.WithHelp("ctrl+n", "new session"),
 35	),
 36	Cancel: key.NewBinding(
 37		key.WithKeys("ctrl+x"),
 38		key.WithHelp("ctrl+x", "cancel"),
 39	),
 40}
 41
 42func (p *chatPage) Init() tea.Cmd {
 43	cmds := []tea.Cmd{
 44		p.layout.Init(),
 45	}
 46	return tea.Batch(cmds...)
 47}
 48
 49func (p *chatPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 50	var cmds []tea.Cmd
 51	switch msg := msg.(type) {
 52	case tea.WindowSizeMsg:
 53		cmd := p.layout.SetSize(msg.Width, msg.Height)
 54		cmds = append(cmds, cmd)
 55	case chat.SendMsg:
 56		cmd := p.sendMessage(msg.Text)
 57		if cmd != nil {
 58			return p, cmd
 59		}
 60	case chat.EditorFocusMsg:
 61		p.editingMode = bool(msg)
 62	case tea.KeyMsg:
 63		switch {
 64		case key.Matches(msg, keyMap.NewSession):
 65			p.session = session.Session{}
 66			return p, tea.Batch(
 67				p.clearSidebar(),
 68				util.CmdHandler(chat.SessionClearedMsg{}),
 69			)
 70		case key.Matches(msg, keyMap.Cancel):
 71			if p.session.ID != "" {
 72				// Cancel the current session's generation process
 73				// This allows users to interrupt long-running operations
 74				p.app.CoderAgent.Cancel(p.session.ID)
 75				return p, nil
 76			}
 77		}
 78	}
 79	u, cmd := p.layout.Update(msg)
 80	cmds = append(cmds, cmd)
 81	p.layout = u.(layout.SplitPaneLayout)
 82	return p, tea.Batch(cmds...)
 83}
 84
 85func (p *chatPage) setSidebar() tea.Cmd {
 86	sidebarContainer := layout.NewContainer(
 87		chat.NewSidebarCmp(p.session, p.app.History),
 88		layout.WithPadding(1, 1, 1, 1),
 89	)
 90	return tea.Batch(p.layout.SetRightPanel(sidebarContainer), sidebarContainer.Init())
 91}
 92
 93func (p *chatPage) clearSidebar() tea.Cmd {
 94	return p.layout.SetRightPanel(nil)
 95}
 96
 97func (p *chatPage) sendMessage(text string) tea.Cmd {
 98	var cmds []tea.Cmd
 99	if p.session.ID == "" {
100		session, err := p.app.Sessions.Create(context.Background(), "New Session")
101		if err != nil {
102			return util.ReportError(err)
103		}
104
105		p.session = session
106		cmd := p.setSidebar()
107		if cmd != nil {
108			cmds = append(cmds, cmd)
109		}
110		cmds = append(cmds, util.CmdHandler(chat.SessionSelectedMsg(session)))
111	}
112
113	p.app.CoderAgent.Run(context.Background(), p.session.ID, text)
114	return tea.Batch(cmds...)
115}
116
117func (p *chatPage) SetSize(width, height int) tea.Cmd {
118	return p.layout.SetSize(width, height)
119}
120
121func (p *chatPage) GetSize() (int, int) {
122	return p.layout.GetSize()
123}
124
125func (p *chatPage) View() string {
126	return p.layout.View()
127}
128
129func (p *chatPage) BindingKeys() []key.Binding {
130	bindings := layout.KeyMapToSlice(keyMap)
131	if p.editingMode {
132		bindings = append(bindings, p.editor.BindingKeys()...)
133	} else {
134		bindings = append(bindings, p.messages.BindingKeys()...)
135	}
136	return bindings
137}
138
139func NewChatPage(app *app.App) tea.Model {
140	messagesContainer := layout.NewContainer(
141		chat.NewMessagesCmp(app),
142		layout.WithPadding(1, 1, 0, 1),
143	)
144
145	editorContainer := layout.NewContainer(
146		chat.NewEditorCmp(app),
147		layout.WithBorder(true, false, false, false),
148	)
149	return &chatPage{
150		app:         app,
151		editor:      editorContainer,
152		messages:    messagesContainer,
153		editingMode: true,
154		layout: layout.NewSplitPane(
155			layout.WithLeftPanel(messagesContainer),
156			layout.WithBottomPanel(editorContainer),
157		),
158	}
159}