tui.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. package tui
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/charmbracelet/bubbles/key"
  6. tea "github.com/charmbracelet/bubbletea"
  7. "github.com/charmbracelet/lipgloss"
  8. "github.com/opencode-ai/opencode/internal/app"
  9. "github.com/opencode-ai/opencode/internal/config"
  10. "github.com/opencode-ai/opencode/internal/logging"
  11. "github.com/opencode-ai/opencode/internal/permission"
  12. "github.com/opencode-ai/opencode/internal/pubsub"
  13. "github.com/opencode-ai/opencode/internal/tui/components/chat"
  14. "github.com/opencode-ai/opencode/internal/tui/components/core"
  15. "github.com/opencode-ai/opencode/internal/tui/components/dialog"
  16. "github.com/opencode-ai/opencode/internal/tui/layout"
  17. "github.com/opencode-ai/opencode/internal/tui/page"
  18. "github.com/opencode-ai/opencode/internal/tui/util"
  19. )
  20. type keyMap struct {
  21. Logs key.Binding
  22. Quit key.Binding
  23. Help key.Binding
  24. SwitchSession key.Binding
  25. Commands key.Binding
  26. Models key.Binding
  27. SwitchTheme key.Binding
  28. }
  29. var keys = keyMap{
  30. Logs: key.NewBinding(
  31. key.WithKeys("ctrl+l"),
  32. key.WithHelp("ctrl+l", "logs"),
  33. ),
  34. Quit: key.NewBinding(
  35. key.WithKeys("ctrl+c"),
  36. key.WithHelp("ctrl+c", "quit"),
  37. ),
  38. Help: key.NewBinding(
  39. key.WithKeys("ctrl+_"),
  40. key.WithHelp("ctrl+?", "toggle help"),
  41. ),
  42. SwitchSession: key.NewBinding(
  43. key.WithKeys("ctrl+a"),
  44. key.WithHelp("ctrl+a", "switch session"),
  45. ),
  46. Commands: key.NewBinding(
  47. key.WithKeys("ctrl+k"),
  48. key.WithHelp("ctrl+k", "commands"),
  49. ),
  50. Models: key.NewBinding(
  51. key.WithKeys("ctrl+o"),
  52. key.WithHelp("ctrl+o", "model selection"),
  53. ),
  54. SwitchTheme: key.NewBinding(
  55. key.WithKeys("ctrl+t"),
  56. key.WithHelp("ctrl+t", "switch theme"),
  57. ),
  58. }
  59. var helpEsc = key.NewBinding(
  60. key.WithKeys("?"),
  61. key.WithHelp("?", "toggle help"),
  62. )
  63. var returnKey = key.NewBinding(
  64. key.WithKeys("esc"),
  65. key.WithHelp("esc", "close"),
  66. )
  67. var logsKeyReturnKey = key.NewBinding(
  68. key.WithKeys("backspace", "q"),
  69. key.WithHelp("backspace/q", "go back"),
  70. )
  71. type appModel struct {
  72. width, height int
  73. currentPage page.PageID
  74. previousPage page.PageID
  75. pages map[page.PageID]tea.Model
  76. loadedPages map[page.PageID]bool
  77. status core.StatusCmp
  78. app *app.App
  79. showPermissions bool
  80. permissions dialog.PermissionDialogCmp
  81. showHelp bool
  82. help dialog.HelpCmp
  83. showQuit bool
  84. quit dialog.QuitDialog
  85. showSessionDialog bool
  86. sessionDialog dialog.SessionDialog
  87. showCommandDialog bool
  88. commandDialog dialog.CommandDialog
  89. commands []dialog.Command
  90. showModelDialog bool
  91. modelDialog dialog.ModelDialog
  92. showInitDialog bool
  93. initDialog dialog.InitDialogCmp
  94. showThemeDialog bool
  95. themeDialog dialog.ThemeDialog
  96. }
  97. func (a appModel) Init() tea.Cmd {
  98. var cmds []tea.Cmd
  99. cmd := a.pages[a.currentPage].Init()
  100. a.loadedPages[a.currentPage] = true
  101. cmds = append(cmds, cmd)
  102. cmd = a.status.Init()
  103. cmds = append(cmds, cmd)
  104. cmd = a.quit.Init()
  105. cmds = append(cmds, cmd)
  106. cmd = a.help.Init()
  107. cmds = append(cmds, cmd)
  108. cmd = a.sessionDialog.Init()
  109. cmds = append(cmds, cmd)
  110. cmd = a.commandDialog.Init()
  111. cmds = append(cmds, cmd)
  112. cmd = a.modelDialog.Init()
  113. cmds = append(cmds, cmd)
  114. cmd = a.initDialog.Init()
  115. cmds = append(cmds, cmd)
  116. cmd = a.themeDialog.Init()
  117. cmds = append(cmds, cmd)
  118. // Check if we should show the init dialog
  119. cmds = append(cmds, func() tea.Msg {
  120. shouldShow, err := config.ShouldShowInitDialog()
  121. if err != nil {
  122. return util.InfoMsg{
  123. Type: util.InfoTypeError,
  124. Msg: "Failed to check init status: " + err.Error(),
  125. }
  126. }
  127. return dialog.ShowInitDialogMsg{Show: shouldShow}
  128. })
  129. return tea.Batch(cmds...)
  130. }
  131. func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  132. var cmds []tea.Cmd
  133. var cmd tea.Cmd
  134. switch msg := msg.(type) {
  135. case tea.WindowSizeMsg:
  136. msg.Height -= 1 // Make space for the status bar
  137. a.width, a.height = msg.Width, msg.Height
  138. s, _ := a.status.Update(msg)
  139. a.status = s.(core.StatusCmp)
  140. a.pages[a.currentPage], cmd = a.pages[a.currentPage].Update(msg)
  141. cmds = append(cmds, cmd)
  142. prm, permCmd := a.permissions.Update(msg)
  143. a.permissions = prm.(dialog.PermissionDialogCmp)
  144. cmds = append(cmds, permCmd)
  145. help, helpCmd := a.help.Update(msg)
  146. a.help = help.(dialog.HelpCmp)
  147. cmds = append(cmds, helpCmd)
  148. session, sessionCmd := a.sessionDialog.Update(msg)
  149. a.sessionDialog = session.(dialog.SessionDialog)
  150. cmds = append(cmds, sessionCmd)
  151. command, commandCmd := a.commandDialog.Update(msg)
  152. a.commandDialog = command.(dialog.CommandDialog)
  153. cmds = append(cmds, commandCmd)
  154. a.initDialog.SetSize(msg.Width, msg.Height)
  155. return a, tea.Batch(cmds...)
  156. // Status
  157. case util.InfoMsg:
  158. s, cmd := a.status.Update(msg)
  159. a.status = s.(core.StatusCmp)
  160. cmds = append(cmds, cmd)
  161. return a, tea.Batch(cmds...)
  162. case pubsub.Event[logging.LogMessage]:
  163. if msg.Payload.Persist {
  164. switch msg.Payload.Level {
  165. case "error":
  166. s, cmd := a.status.Update(util.InfoMsg{
  167. Type: util.InfoTypeError,
  168. Msg: msg.Payload.Message,
  169. TTL: msg.Payload.PersistTime,
  170. })
  171. a.status = s.(core.StatusCmp)
  172. cmds = append(cmds, cmd)
  173. case "info":
  174. s, cmd := a.status.Update(util.InfoMsg{
  175. Type: util.InfoTypeInfo,
  176. Msg: msg.Payload.Message,
  177. TTL: msg.Payload.PersistTime,
  178. })
  179. a.status = s.(core.StatusCmp)
  180. cmds = append(cmds, cmd)
  181. case "warn":
  182. s, cmd := a.status.Update(util.InfoMsg{
  183. Type: util.InfoTypeWarn,
  184. Msg: msg.Payload.Message,
  185. TTL: msg.Payload.PersistTime,
  186. })
  187. a.status = s.(core.StatusCmp)
  188. cmds = append(cmds, cmd)
  189. default:
  190. s, cmd := a.status.Update(util.InfoMsg{
  191. Type: util.InfoTypeInfo,
  192. Msg: msg.Payload.Message,
  193. TTL: msg.Payload.PersistTime,
  194. })
  195. a.status = s.(core.StatusCmp)
  196. cmds = append(cmds, cmd)
  197. }
  198. }
  199. case util.ClearStatusMsg:
  200. s, _ := a.status.Update(msg)
  201. a.status = s.(core.StatusCmp)
  202. // Permission
  203. case pubsub.Event[permission.PermissionRequest]:
  204. a.showPermissions = true
  205. return a, a.permissions.SetPermissions(msg.Payload)
  206. case dialog.PermissionResponseMsg:
  207. var cmd tea.Cmd
  208. switch msg.Action {
  209. case dialog.PermissionAllow:
  210. a.app.Permissions.Grant(msg.Permission)
  211. case dialog.PermissionAllowForSession:
  212. a.app.Permissions.GrantPersistant(msg.Permission)
  213. case dialog.PermissionDeny:
  214. a.app.Permissions.Deny(msg.Permission)
  215. }
  216. a.showPermissions = false
  217. return a, cmd
  218. case page.PageChangeMsg:
  219. return a, a.moveToPage(msg.ID)
  220. case dialog.CloseQuitMsg:
  221. a.showQuit = false
  222. return a, nil
  223. case dialog.CloseSessionDialogMsg:
  224. a.showSessionDialog = false
  225. return a, nil
  226. case dialog.CloseCommandDialogMsg:
  227. a.showCommandDialog = false
  228. return a, nil
  229. case dialog.CloseThemeDialogMsg:
  230. a.showThemeDialog = false
  231. return a, nil
  232. case dialog.ThemeChangedMsg:
  233. a.pages[a.currentPage], cmd = a.pages[a.currentPage].Update(msg)
  234. a.showThemeDialog = false
  235. return a, tea.Batch(cmd, util.ReportInfo("Theme changed to: "+msg.ThemeName))
  236. case dialog.CloseModelDialogMsg:
  237. a.showModelDialog = false
  238. return a, nil
  239. case dialog.ModelSelectedMsg:
  240. a.showModelDialog = false
  241. model, err := a.app.CoderAgent.Update(config.AgentCoder, msg.Model.ID)
  242. if err != nil {
  243. return a, util.ReportError(err)
  244. }
  245. return a, util.ReportInfo(fmt.Sprintf("Model changed to %s", model.Name))
  246. case dialog.ShowInitDialogMsg:
  247. a.showInitDialog = msg.Show
  248. return a, nil
  249. case dialog.CloseInitDialogMsg:
  250. a.showInitDialog = false
  251. if msg.Initialize {
  252. // Run the initialization command
  253. for _, cmd := range a.commands {
  254. if cmd.ID == "init" {
  255. // Mark the project as initialized
  256. if err := config.MarkProjectInitialized(); err != nil {
  257. return a, util.ReportError(err)
  258. }
  259. return a, cmd.Handler(cmd)
  260. }
  261. }
  262. } else {
  263. // Mark the project as initialized without running the command
  264. if err := config.MarkProjectInitialized(); err != nil {
  265. return a, util.ReportError(err)
  266. }
  267. }
  268. return a, nil
  269. case chat.SessionSelectedMsg:
  270. a.sessionDialog.SetSelectedSession(msg.ID)
  271. case dialog.SessionSelectedMsg:
  272. a.showSessionDialog = false
  273. if a.currentPage == page.ChatPage {
  274. return a, util.CmdHandler(chat.SessionSelectedMsg(msg.Session))
  275. }
  276. return a, nil
  277. case dialog.CommandSelectedMsg:
  278. a.showCommandDialog = false
  279. // Execute the command handler if available
  280. if msg.Command.Handler != nil {
  281. return a, msg.Command.Handler(msg.Command)
  282. }
  283. return a, util.ReportInfo("Command selected: " + msg.Command.Title)
  284. case tea.KeyMsg:
  285. switch {
  286. case key.Matches(msg, keys.Quit):
  287. a.showQuit = !a.showQuit
  288. if a.showHelp {
  289. a.showHelp = false
  290. }
  291. if a.showSessionDialog {
  292. a.showSessionDialog = false
  293. }
  294. if a.showCommandDialog {
  295. a.showCommandDialog = false
  296. }
  297. if a.showModelDialog {
  298. a.showModelDialog = false
  299. }
  300. return a, nil
  301. case key.Matches(msg, keys.SwitchSession):
  302. if a.currentPage == page.ChatPage && !a.showQuit && !a.showPermissions && !a.showCommandDialog {
  303. // Load sessions and show the dialog
  304. sessions, err := a.app.Sessions.List(context.Background())
  305. if err != nil {
  306. return a, util.ReportError(err)
  307. }
  308. if len(sessions) == 0 {
  309. return a, util.ReportWarn("No sessions available")
  310. }
  311. a.sessionDialog.SetSessions(sessions)
  312. a.showSessionDialog = true
  313. return a, nil
  314. }
  315. return a, nil
  316. case key.Matches(msg, keys.Commands):
  317. if a.currentPage == page.ChatPage && !a.showQuit && !a.showPermissions && !a.showSessionDialog && !a.showThemeDialog {
  318. // Show commands dialog
  319. if len(a.commands) == 0 {
  320. return a, util.ReportWarn("No commands available")
  321. }
  322. a.commandDialog.SetCommands(a.commands)
  323. a.showCommandDialog = true
  324. return a, nil
  325. }
  326. return a, nil
  327. case key.Matches(msg, keys.Models):
  328. if a.showModelDialog {
  329. a.showModelDialog = false
  330. return a, nil
  331. }
  332. if a.currentPage == page.ChatPage && !a.showQuit && !a.showPermissions && !a.showSessionDialog && !a.showCommandDialog {
  333. a.showModelDialog = true
  334. return a, nil
  335. }
  336. return a, nil
  337. case key.Matches(msg, keys.SwitchTheme):
  338. if !a.showQuit && !a.showPermissions && !a.showSessionDialog && !a.showCommandDialog {
  339. // Show theme switcher dialog
  340. a.showThemeDialog = true
  341. // Theme list is dynamically loaded by the dialog component
  342. return a, a.themeDialog.Init()
  343. }
  344. return a, nil
  345. case key.Matches(msg, logsKeyReturnKey):
  346. if a.currentPage == page.LogsPage {
  347. return a, a.moveToPage(page.ChatPage)
  348. }
  349. case key.Matches(msg, returnKey):
  350. if a.showQuit {
  351. a.showQuit = !a.showQuit
  352. return a, nil
  353. }
  354. if a.showHelp {
  355. a.showHelp = !a.showHelp
  356. return a, nil
  357. }
  358. if a.showInitDialog {
  359. a.showInitDialog = false
  360. // Mark the project as initialized without running the command
  361. if err := config.MarkProjectInitialized(); err != nil {
  362. return a, util.ReportError(err)
  363. }
  364. return a, nil
  365. }
  366. case key.Matches(msg, keys.Logs):
  367. return a, a.moveToPage(page.LogsPage)
  368. case key.Matches(msg, keys.Help):
  369. if a.showQuit {
  370. return a, nil
  371. }
  372. a.showHelp = !a.showHelp
  373. return a, nil
  374. case key.Matches(msg, helpEsc):
  375. if a.app.CoderAgent.IsBusy() {
  376. if a.showQuit {
  377. return a, nil
  378. }
  379. a.showHelp = !a.showHelp
  380. return a, nil
  381. }
  382. }
  383. }
  384. if a.showQuit {
  385. q, quitCmd := a.quit.Update(msg)
  386. a.quit = q.(dialog.QuitDialog)
  387. cmds = append(cmds, quitCmd)
  388. // Only block key messages send all other messages down
  389. if _, ok := msg.(tea.KeyMsg); ok {
  390. return a, tea.Batch(cmds...)
  391. }
  392. }
  393. if a.showPermissions {
  394. d, permissionsCmd := a.permissions.Update(msg)
  395. a.permissions = d.(dialog.PermissionDialogCmp)
  396. cmds = append(cmds, permissionsCmd)
  397. // Only block key messages send all other messages down
  398. if _, ok := msg.(tea.KeyMsg); ok {
  399. return a, tea.Batch(cmds...)
  400. }
  401. }
  402. if a.showSessionDialog {
  403. d, sessionCmd := a.sessionDialog.Update(msg)
  404. a.sessionDialog = d.(dialog.SessionDialog)
  405. cmds = append(cmds, sessionCmd)
  406. // Only block key messages send all other messages down
  407. if _, ok := msg.(tea.KeyMsg); ok {
  408. return a, tea.Batch(cmds...)
  409. }
  410. }
  411. if a.showCommandDialog {
  412. d, commandCmd := a.commandDialog.Update(msg)
  413. a.commandDialog = d.(dialog.CommandDialog)
  414. cmds = append(cmds, commandCmd)
  415. // Only block key messages send all other messages down
  416. if _, ok := msg.(tea.KeyMsg); ok {
  417. return a, tea.Batch(cmds...)
  418. }
  419. }
  420. if a.showModelDialog {
  421. d, modelCmd := a.modelDialog.Update(msg)
  422. a.modelDialog = d.(dialog.ModelDialog)
  423. cmds = append(cmds, modelCmd)
  424. // Only block key messages send all other messages down
  425. if _, ok := msg.(tea.KeyMsg); ok {
  426. return a, tea.Batch(cmds...)
  427. }
  428. }
  429. if a.showInitDialog {
  430. d, initCmd := a.initDialog.Update(msg)
  431. a.initDialog = d.(dialog.InitDialogCmp)
  432. cmds = append(cmds, initCmd)
  433. // Only block key messages send all other messages down
  434. if _, ok := msg.(tea.KeyMsg); ok {
  435. return a, tea.Batch(cmds...)
  436. }
  437. }
  438. if a.showThemeDialog {
  439. d, themeCmd := a.themeDialog.Update(msg)
  440. a.themeDialog = d.(dialog.ThemeDialog)
  441. cmds = append(cmds, themeCmd)
  442. // Only block key messages send all other messages down
  443. if _, ok := msg.(tea.KeyMsg); ok {
  444. return a, tea.Batch(cmds...)
  445. }
  446. }
  447. s, _ := a.status.Update(msg)
  448. a.status = s.(core.StatusCmp)
  449. a.pages[a.currentPage], cmd = a.pages[a.currentPage].Update(msg)
  450. cmds = append(cmds, cmd)
  451. return a, tea.Batch(cmds...)
  452. }
  453. // RegisterCommand adds a command to the command dialog
  454. func (a *appModel) RegisterCommand(cmd dialog.Command) {
  455. a.commands = append(a.commands, cmd)
  456. }
  457. func (a *appModel) moveToPage(pageID page.PageID) tea.Cmd {
  458. if a.app.CoderAgent.IsBusy() {
  459. // For now we don't move to any page if the agent is busy
  460. return util.ReportWarn("Agent is busy, please wait...")
  461. }
  462. var cmds []tea.Cmd
  463. if _, ok := a.loadedPages[pageID]; !ok {
  464. cmd := a.pages[pageID].Init()
  465. cmds = append(cmds, cmd)
  466. a.loadedPages[pageID] = true
  467. }
  468. a.previousPage = a.currentPage
  469. a.currentPage = pageID
  470. if sizable, ok := a.pages[a.currentPage].(layout.Sizeable); ok {
  471. cmd := sizable.SetSize(a.width, a.height)
  472. cmds = append(cmds, cmd)
  473. }
  474. return tea.Batch(cmds...)
  475. }
  476. func (a appModel) View() string {
  477. components := []string{
  478. a.pages[a.currentPage].View(),
  479. }
  480. components = append(components, a.status.View())
  481. appView := lipgloss.JoinVertical(lipgloss.Top, components...)
  482. if a.showPermissions {
  483. overlay := a.permissions.View()
  484. row := lipgloss.Height(appView) / 2
  485. row -= lipgloss.Height(overlay) / 2
  486. col := lipgloss.Width(appView) / 2
  487. col -= lipgloss.Width(overlay) / 2
  488. appView = layout.PlaceOverlay(
  489. col,
  490. row,
  491. overlay,
  492. appView,
  493. true,
  494. )
  495. }
  496. if !a.app.CoderAgent.IsBusy() {
  497. a.status.SetHelpMsg("ctrl+? help")
  498. } else {
  499. a.status.SetHelpMsg("? help")
  500. }
  501. if a.showHelp {
  502. bindings := layout.KeyMapToSlice(keys)
  503. if p, ok := a.pages[a.currentPage].(layout.Bindings); ok {
  504. bindings = append(bindings, p.BindingKeys()...)
  505. }
  506. if a.showPermissions {
  507. bindings = append(bindings, a.permissions.BindingKeys()...)
  508. }
  509. if a.currentPage == page.LogsPage {
  510. bindings = append(bindings, logsKeyReturnKey)
  511. }
  512. if !a.app.CoderAgent.IsBusy() {
  513. bindings = append(bindings, helpEsc)
  514. }
  515. a.help.SetBindings(bindings)
  516. overlay := a.help.View()
  517. row := lipgloss.Height(appView) / 2
  518. row -= lipgloss.Height(overlay) / 2
  519. col := lipgloss.Width(appView) / 2
  520. col -= lipgloss.Width(overlay) / 2
  521. appView = layout.PlaceOverlay(
  522. col,
  523. row,
  524. overlay,
  525. appView,
  526. true,
  527. )
  528. }
  529. if a.showQuit {
  530. overlay := a.quit.View()
  531. row := lipgloss.Height(appView) / 2
  532. row -= lipgloss.Height(overlay) / 2
  533. col := lipgloss.Width(appView) / 2
  534. col -= lipgloss.Width(overlay) / 2
  535. appView = layout.PlaceOverlay(
  536. col,
  537. row,
  538. overlay,
  539. appView,
  540. true,
  541. )
  542. }
  543. if a.showSessionDialog {
  544. overlay := a.sessionDialog.View()
  545. row := lipgloss.Height(appView) / 2
  546. row -= lipgloss.Height(overlay) / 2
  547. col := lipgloss.Width(appView) / 2
  548. col -= lipgloss.Width(overlay) / 2
  549. appView = layout.PlaceOverlay(
  550. col,
  551. row,
  552. overlay,
  553. appView,
  554. true,
  555. )
  556. }
  557. if a.showModelDialog {
  558. overlay := a.modelDialog.View()
  559. row := lipgloss.Height(appView) / 2
  560. row -= lipgloss.Height(overlay) / 2
  561. col := lipgloss.Width(appView) / 2
  562. col -= lipgloss.Width(overlay) / 2
  563. appView = layout.PlaceOverlay(
  564. col,
  565. row,
  566. overlay,
  567. appView,
  568. true,
  569. )
  570. }
  571. if a.showCommandDialog {
  572. overlay := a.commandDialog.View()
  573. row := lipgloss.Height(appView) / 2
  574. row -= lipgloss.Height(overlay) / 2
  575. col := lipgloss.Width(appView) / 2
  576. col -= lipgloss.Width(overlay) / 2
  577. appView = layout.PlaceOverlay(
  578. col,
  579. row,
  580. overlay,
  581. appView,
  582. true,
  583. )
  584. }
  585. if a.showInitDialog {
  586. overlay := a.initDialog.View()
  587. appView = layout.PlaceOverlay(
  588. a.width/2-lipgloss.Width(overlay)/2,
  589. a.height/2-lipgloss.Height(overlay)/2,
  590. overlay,
  591. appView,
  592. true,
  593. )
  594. }
  595. if a.showThemeDialog {
  596. overlay := a.themeDialog.View()
  597. row := lipgloss.Height(appView) / 2
  598. row -= lipgloss.Height(overlay) / 2
  599. col := lipgloss.Width(appView) / 2
  600. col -= lipgloss.Width(overlay) / 2
  601. appView = layout.PlaceOverlay(
  602. col,
  603. row,
  604. overlay,
  605. appView,
  606. true,
  607. )
  608. }
  609. return appView
  610. }
  611. func New(app *app.App) tea.Model {
  612. startPage := page.ChatPage
  613. model := &appModel{
  614. currentPage: startPage,
  615. loadedPages: make(map[page.PageID]bool),
  616. status: core.NewStatusCmp(app.LSPClients),
  617. help: dialog.NewHelpCmp(),
  618. quit: dialog.NewQuitCmp(),
  619. sessionDialog: dialog.NewSessionDialogCmp(),
  620. commandDialog: dialog.NewCommandDialogCmp(),
  621. modelDialog: dialog.NewModelDialogCmp(),
  622. permissions: dialog.NewPermissionDialogCmp(),
  623. initDialog: dialog.NewInitDialogCmp(),
  624. themeDialog: dialog.NewThemeDialogCmp(),
  625. app: app,
  626. commands: []dialog.Command{},
  627. pages: map[page.PageID]tea.Model{
  628. page.ChatPage: page.NewChatPage(app),
  629. page.LogsPage: page.NewLogsPage(),
  630. },
  631. }
  632. model.RegisterCommand(dialog.Command{
  633. ID: "init",
  634. Title: "Initialize Project",
  635. Description: "Create/Update the OpenCode.md memory file",
  636. Handler: func(cmd dialog.Command) tea.Cmd {
  637. prompt := `Please analyze this codebase and create a OpenCode.md file containing:
  638. 1. Build/lint/test commands - especially for running a single test
  639. 2. Code style guidelines including imports, formatting, types, naming conventions, error handling, etc.
  640. The file you create will be given to agentic coding agents (such as yourself) that operate in this repository. Make it about 20 lines long.
  641. If there's already a opencode.md, improve it.
  642. If there are Cursor rules (in .cursor/rules/ or .cursorrules) or Copilot rules (in .github/copilot-instructions.md), make sure to include them.`
  643. return tea.Batch(
  644. util.CmdHandler(chat.SendMsg{
  645. Text: prompt,
  646. }),
  647. )
  648. },
  649. })
  650. return model
  651. }