chat.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package page
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/charmbracelet/bubbles/key"
  6. tea "github.com/charmbracelet/bubbletea"
  7. "github.com/sst/opencode/internal/app"
  8. "github.com/sst/opencode/internal/message"
  9. "github.com/sst/opencode/internal/session"
  10. "github.com/sst/opencode/internal/status"
  11. "github.com/sst/opencode/internal/tui/components/chat"
  12. "github.com/sst/opencode/internal/tui/components/dialog"
  13. "github.com/sst/opencode/internal/tui/layout"
  14. "github.com/sst/opencode/internal/tui/state"
  15. "github.com/sst/opencode/internal/tui/util"
  16. )
  17. var ChatPage PageID = "chat"
  18. type chatPage struct {
  19. app *app.App
  20. editor layout.Container
  21. messages layout.Container
  22. layout layout.SplitPaneLayout
  23. }
  24. type ChatKeyMap struct {
  25. NewSession key.Binding
  26. Cancel key.Binding
  27. ToggleTools key.Binding
  28. }
  29. var keyMap = ChatKeyMap{
  30. NewSession: key.NewBinding(
  31. key.WithKeys("ctrl+n"),
  32. key.WithHelp("ctrl+n", "new session"),
  33. ),
  34. Cancel: key.NewBinding(
  35. key.WithKeys("esc"),
  36. key.WithHelp("esc", "cancel"),
  37. ),
  38. ToggleTools: key.NewBinding(
  39. key.WithKeys("ctrl+h"),
  40. key.WithHelp("ctrl+h", "toggle tools"),
  41. ),
  42. }
  43. func (p *chatPage) Init() tea.Cmd {
  44. cmds := []tea.Cmd{
  45. p.layout.Init(),
  46. }
  47. return tea.Batch(cmds...)
  48. }
  49. func (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, msg.Attachments)
  57. if cmd != nil {
  58. return p, cmd
  59. }
  60. case dialog.CommandRunCustomMsg:
  61. // Check if the agent is busy before executing custom commands
  62. if p.app.PrimaryAgent.IsBusy() {
  63. status.Warn("Agent is busy, please wait before executing a command...")
  64. return p, nil
  65. }
  66. // Handle custom command execution
  67. cmd := p.sendMessage(msg.Content, nil)
  68. if cmd != nil {
  69. return p, cmd
  70. }
  71. case state.SessionSelectedMsg:
  72. cmd := p.setSidebar()
  73. cmds = append(cmds, cmd)
  74. case state.SessionClearedMsg:
  75. cmd := p.setSidebar()
  76. cmds = append(cmds, cmd)
  77. case state.CompactSessionMsg:
  78. if p.app.CurrentSession.ID == "" {
  79. status.Warn("No active session to compact.")
  80. return p, nil
  81. }
  82. // Run compaction in background
  83. go func(sessionID string) {
  84. err := p.app.PrimaryAgent.CompactSession(context.Background(), sessionID, false)
  85. if err != nil {
  86. status.Error(fmt.Sprintf("Compaction failed: %v", err))
  87. } else {
  88. status.Info("Conversation compacted successfully.")
  89. }
  90. }(p.app.CurrentSession.ID)
  91. return p, nil
  92. case tea.KeyMsg:
  93. switch {
  94. case key.Matches(msg, keyMap.NewSession):
  95. p.app.CurrentSession = &session.Session{}
  96. return p, tea.Batch(
  97. p.clearSidebar(),
  98. util.CmdHandler(state.SessionClearedMsg{}),
  99. )
  100. case key.Matches(msg, keyMap.Cancel):
  101. if p.app.CurrentSession.ID != "" {
  102. // Cancel the current session's generation process
  103. // This allows users to interrupt long-running operations
  104. p.app.PrimaryAgent.Cancel(p.app.CurrentSession.ID)
  105. return p, nil
  106. }
  107. case key.Matches(msg, keyMap.ToggleTools):
  108. return p, util.CmdHandler(chat.ToggleToolMessagesMsg{})
  109. }
  110. }
  111. u, cmd := p.layout.Update(msg)
  112. cmds = append(cmds, cmd)
  113. p.layout = u.(layout.SplitPaneLayout)
  114. return p, tea.Batch(cmds...)
  115. }
  116. func (p *chatPage) setSidebar() tea.Cmd {
  117. sidebarContainer := layout.NewContainer(
  118. chat.NewSidebarCmp(p.app),
  119. layout.WithPadding(1, 1, 1, 1),
  120. )
  121. return tea.Batch(p.layout.SetRightPanel(sidebarContainer), sidebarContainer.Init())
  122. }
  123. func (p *chatPage) clearSidebar() tea.Cmd {
  124. return p.layout.ClearRightPanel()
  125. }
  126. func (p *chatPage) sendMessage(text string, attachments []message.Attachment) tea.Cmd {
  127. var cmds []tea.Cmd
  128. if p.app.CurrentSession.ID == "" {
  129. newSession, err := p.app.Sessions.Create(context.Background(), "New Session")
  130. if err != nil {
  131. status.Error(err.Error())
  132. return nil
  133. }
  134. p.app.CurrentSession = &newSession
  135. cmd := p.setSidebar()
  136. if cmd != nil {
  137. cmds = append(cmds, cmd)
  138. }
  139. cmds = append(cmds, util.CmdHandler(state.SessionSelectedMsg(&newSession)))
  140. }
  141. _, err := p.app.PrimaryAgent.Run(context.Background(), p.app.CurrentSession.ID, text, attachments...)
  142. if err != nil {
  143. status.Error(err.Error())
  144. return nil
  145. }
  146. return tea.Batch(cmds...)
  147. }
  148. func (p *chatPage) SetSize(width, height int) tea.Cmd {
  149. return p.layout.SetSize(width, height)
  150. }
  151. func (p *chatPage) GetSize() (int, int) {
  152. return p.layout.GetSize()
  153. }
  154. func (p *chatPage) View() string {
  155. return p.layout.View()
  156. }
  157. func (p *chatPage) BindingKeys() []key.Binding {
  158. bindings := layout.KeyMapToSlice(keyMap)
  159. bindings = append(bindings, p.messages.BindingKeys()...)
  160. bindings = append(bindings, p.editor.BindingKeys()...)
  161. return bindings
  162. }
  163. func NewChatPage(app *app.App) tea.Model {
  164. messagesContainer := layout.NewContainer(
  165. chat.NewMessagesCmp(app),
  166. layout.WithPadding(1, 1, 0, 1),
  167. )
  168. editorContainer := layout.NewContainer(
  169. chat.NewEditorCmp(app),
  170. layout.WithBorder(true, false, false, false),
  171. )
  172. return &chatPage{
  173. app: app,
  174. editor: editorContainer,
  175. messages: messagesContainer,
  176. layout: layout.NewSplitPane(
  177. layout.WithLeftPanel(messagesContainer),
  178. layout.WithBottomPanel(editorContainer),
  179. ),
  180. }
  181. }