chat.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package page
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/charmbracelet/bubbles/key"
  6. tea "github.com/charmbracelet/bubbletea"
  7. "github.com/opencode-ai/opencode/internal/app"
  8. "github.com/opencode-ai/opencode/internal/message"
  9. "github.com/opencode-ai/opencode/internal/session"
  10. "github.com/opencode-ai/opencode/internal/status"
  11. "github.com/opencode-ai/opencode/internal/tui/components/chat"
  12. "github.com/opencode-ai/opencode/internal/tui/components/dialog"
  13. "github.com/opencode-ai/opencode/internal/tui/layout"
  14. "github.com/opencode-ai/opencode/internal/tui/util"
  15. )
  16. var ChatPage PageID = "chat"
  17. type chatPage struct {
  18. app *app.App
  19. editor layout.Container
  20. messages layout.Container
  21. layout layout.SplitPaneLayout
  22. session session.Session
  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.CoderAgent.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 chat.SessionSelectedMsg:
  72. if p.session.ID == "" {
  73. cmd := p.setSidebar()
  74. if cmd != nil {
  75. cmds = append(cmds, cmd)
  76. }
  77. }
  78. p.session = msg
  79. case chat.CompactSessionMsg:
  80. if p.session.ID == "" {
  81. status.Warn("No active session to compact.")
  82. return p, nil
  83. }
  84. // Run compaction in background
  85. go func(sessionID string) {
  86. err := p.app.CoderAgent.CompactSession(context.Background(), sessionID)
  87. if err != nil {
  88. status.Error(fmt.Sprintf("Compaction failed: %v", err))
  89. } else {
  90. status.Info("Conversation compacted successfully.")
  91. }
  92. }(p.session.ID)
  93. return p, nil
  94. case tea.KeyMsg:
  95. switch {
  96. case key.Matches(msg, keyMap.NewSession):
  97. p.session = session.Session{}
  98. return p, tea.Batch(
  99. p.clearSidebar(),
  100. util.CmdHandler(chat.SessionClearedMsg{}),
  101. )
  102. case key.Matches(msg, keyMap.Cancel):
  103. if p.session.ID != "" {
  104. // Cancel the current session's generation process
  105. // This allows users to interrupt long-running operations
  106. p.app.CoderAgent.Cancel(p.session.ID)
  107. return p, nil
  108. }
  109. case key.Matches(msg, keyMap.ToggleTools):
  110. return p, util.CmdHandler(chat.ToggleToolMessagesMsg{})
  111. }
  112. }
  113. u, cmd := p.layout.Update(msg)
  114. cmds = append(cmds, cmd)
  115. p.layout = u.(layout.SplitPaneLayout)
  116. return p, tea.Batch(cmds...)
  117. }
  118. func (p *chatPage) setSidebar() tea.Cmd {
  119. sidebarContainer := layout.NewContainer(
  120. chat.NewSidebarCmp(p.session, p.app.History),
  121. layout.WithPadding(1, 1, 1, 1),
  122. )
  123. return tea.Batch(p.layout.SetRightPanel(sidebarContainer), sidebarContainer.Init())
  124. }
  125. func (p *chatPage) clearSidebar() tea.Cmd {
  126. return p.layout.ClearRightPanel()
  127. }
  128. func (p *chatPage) sendMessage(text string, attachments []message.Attachment) tea.Cmd {
  129. var cmds []tea.Cmd
  130. if p.session.ID == "" {
  131. newSession, err := p.app.Sessions.Create(context.Background(), "New Session")
  132. if err != nil {
  133. status.Error(err.Error())
  134. return nil
  135. }
  136. p.session = newSession
  137. // Update the current session in the session manager
  138. // session.SetCurrentSession(newSession.ID)
  139. cmd := p.setSidebar()
  140. if cmd != nil {
  141. cmds = append(cmds, cmd)
  142. }
  143. cmds = append(cmds, util.CmdHandler(chat.SessionSelectedMsg(newSession)))
  144. }
  145. _, err := p.app.CoderAgent.Run(context.Background(), p.session.ID, text, attachments...)
  146. if err != nil {
  147. status.Error(err.Error())
  148. return nil
  149. }
  150. return tea.Batch(cmds...)
  151. }
  152. func (p *chatPage) SetSize(width, height int) tea.Cmd {
  153. return p.layout.SetSize(width, height)
  154. }
  155. func (p *chatPage) GetSize() (int, int) {
  156. return p.layout.GetSize()
  157. }
  158. func (p *chatPage) View() string {
  159. return p.layout.View()
  160. }
  161. func (p *chatPage) BindingKeys() []key.Binding {
  162. bindings := layout.KeyMapToSlice(keyMap)
  163. bindings = append(bindings, p.messages.BindingKeys()...)
  164. bindings = append(bindings, p.editor.BindingKeys()...)
  165. return bindings
  166. }
  167. func NewChatPage(app *app.App) tea.Model {
  168. messagesContainer := layout.NewContainer(
  169. chat.NewMessagesCmp(app),
  170. layout.WithPadding(1, 1, 0, 1),
  171. )
  172. editorContainer := layout.NewContainer(
  173. chat.NewEditorCmp(app),
  174. layout.WithBorder(true, false, false, false),
  175. )
  176. return &chatPage{
  177. app: app,
  178. editor: editorContainer,
  179. messages: messagesContainer,
  180. layout: layout.NewSplitPane(
  181. layout.WithLeftPanel(messagesContainer),
  182. layout.WithBottomPanel(editorContainer),
  183. ),
  184. }
  185. }