app.go 3.8 KB

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