app.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package app
  2. import (
  3. "context"
  4. "database/sql"
  5. "maps"
  6. "sync"
  7. "time"
  8. "log/slog"
  9. "github.com/sst/opencode/internal/config"
  10. "github.com/sst/opencode/internal/fileutil"
  11. "github.com/sst/opencode/internal/history"
  12. "github.com/sst/opencode/internal/llm/agent"
  13. "github.com/sst/opencode/internal/logging"
  14. "github.com/sst/opencode/internal/lsp"
  15. "github.com/sst/opencode/internal/message"
  16. "github.com/sst/opencode/internal/permission"
  17. "github.com/sst/opencode/internal/session"
  18. "github.com/sst/opencode/internal/status"
  19. "github.com/sst/opencode/internal/tui/theme"
  20. )
  21. type App struct {
  22. CurrentSession *session.Session
  23. Logs logging.Service
  24. Sessions session.Service
  25. Messages message.Service
  26. History history.Service
  27. Permissions permission.Service
  28. Status status.Service
  29. PrimaryAgent agent.Service
  30. LSPClients map[string]*lsp.Client
  31. clientsMutex sync.RWMutex
  32. watcherCancelFuncs []context.CancelFunc
  33. cancelFuncsMutex sync.Mutex
  34. watcherWG sync.WaitGroup
  35. // UI state
  36. filepickerOpen bool
  37. completionDialogOpen bool
  38. }
  39. func New(ctx context.Context, conn *sql.DB) (*App, error) {
  40. err := logging.InitService(conn)
  41. if err != nil {
  42. slog.Error("Failed to initialize logging service", "error", err)
  43. return nil, err
  44. }
  45. err = session.InitService(conn)
  46. if err != nil {
  47. slog.Error("Failed to initialize session service", "error", err)
  48. return nil, err
  49. }
  50. err = message.InitService(conn)
  51. if err != nil {
  52. slog.Error("Failed to initialize message service", "error", err)
  53. return nil, err
  54. }
  55. err = history.InitService(conn)
  56. if err != nil {
  57. slog.Error("Failed to initialize history service", "error", err)
  58. return nil, err
  59. }
  60. err = permission.InitService()
  61. if err != nil {
  62. slog.Error("Failed to initialize permission service", "error", err)
  63. return nil, err
  64. }
  65. err = status.InitService()
  66. if err != nil {
  67. slog.Error("Failed to initialize status service", "error", err)
  68. return nil, err
  69. }
  70. fileutil.Init()
  71. app := &App{
  72. CurrentSession: &session.Session{},
  73. Logs: logging.GetService(),
  74. Sessions: session.GetService(),
  75. Messages: message.GetService(),
  76. History: history.GetService(),
  77. Permissions: permission.GetService(),
  78. Status: status.GetService(),
  79. LSPClients: make(map[string]*lsp.Client),
  80. }
  81. // Initialize theme based on configuration
  82. app.initTheme()
  83. // Initialize LSP clients in the background
  84. go app.initLSPClients(ctx)
  85. app.PrimaryAgent, err = agent.NewAgent(
  86. config.AgentPrimary,
  87. app.Sessions,
  88. app.Messages,
  89. agent.PrimaryAgentTools(
  90. app.Permissions,
  91. app.Sessions,
  92. app.Messages,
  93. app.History,
  94. app.LSPClients,
  95. ),
  96. )
  97. if err != nil {
  98. slog.Error("Failed to create primary agent", "error", err)
  99. return nil, err
  100. }
  101. return app, nil
  102. }
  103. // initTheme sets the application theme based on the configuration
  104. func (app *App) initTheme() {
  105. cfg := config.Get()
  106. if cfg == nil || cfg.TUI.Theme == "" {
  107. return // Use default theme
  108. }
  109. // Try to set the theme from config
  110. err := theme.SetTheme(cfg.TUI.Theme)
  111. if err != nil {
  112. slog.Warn("Failed to set theme from config, using default theme", "theme", cfg.TUI.Theme, "error", err)
  113. } else {
  114. slog.Debug("Set theme from config", "theme", cfg.TUI.Theme)
  115. }
  116. }
  117. // IsFilepickerOpen returns whether the filepicker is currently open
  118. func (app *App) IsFilepickerOpen() bool {
  119. return app.filepickerOpen
  120. }
  121. // SetFilepickerOpen sets the state of the filepicker
  122. func (app *App) SetFilepickerOpen(open bool) {
  123. app.filepickerOpen = open
  124. }
  125. // IsCompletionDialogOpen returns whether the completion dialog is currently open
  126. func (app *App) IsCompletionDialogOpen() bool {
  127. return app.completionDialogOpen
  128. }
  129. // SetCompletionDialogOpen sets the state of the completion dialog
  130. func (app *App) SetCompletionDialogOpen(open bool) {
  131. app.completionDialogOpen = open
  132. }
  133. // Shutdown performs a clean shutdown of the application
  134. func (app *App) Shutdown() {
  135. // Cancel all watcher goroutines
  136. app.cancelFuncsMutex.Lock()
  137. for _, cancel := range app.watcherCancelFuncs {
  138. cancel()
  139. }
  140. app.cancelFuncsMutex.Unlock()
  141. app.watcherWG.Wait()
  142. // Perform additional cleanup for LSP clients
  143. app.clientsMutex.RLock()
  144. clients := make(map[string]*lsp.Client, len(app.LSPClients))
  145. maps.Copy(clients, app.LSPClients)
  146. app.clientsMutex.RUnlock()
  147. for name, client := range clients {
  148. shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  149. if err := client.Shutdown(shutdownCtx); err != nil {
  150. slog.Error("Failed to shutdown LSP client", "name", name, "error", err)
  151. }
  152. cancel()
  153. }
  154. }