app.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package app
  2. import (
  3. "context"
  4. "database/sql"
  5. "maps"
  6. "sync"
  7. "time"
  8. "github.com/opencode-ai/opencode/internal/config"
  9. "github.com/opencode-ai/opencode/internal/db"
  10. "github.com/opencode-ai/opencode/internal/history"
  11. "github.com/opencode-ai/opencode/internal/llm/agent"
  12. "github.com/opencode-ai/opencode/internal/logging"
  13. "github.com/opencode-ai/opencode/internal/lsp"
  14. "github.com/opencode-ai/opencode/internal/message"
  15. "github.com/opencode-ai/opencode/internal/permission"
  16. "github.com/opencode-ai/opencode/internal/session"
  17. "github.com/opencode-ai/opencode/internal/status"
  18. "github.com/opencode-ai/opencode/internal/tui/theme"
  19. )
  20. type App struct {
  21. Sessions session.Service
  22. Messages message.Service
  23. History history.Service
  24. Permissions permission.Service
  25. Status status.Service
  26. CoderAgent agent.Service
  27. LSPClients map[string]*lsp.Client
  28. clientsMutex sync.RWMutex
  29. watcherCancelFuncs []context.CancelFunc
  30. cancelFuncsMutex sync.Mutex
  31. watcherWG sync.WaitGroup
  32. }
  33. func New(ctx context.Context, conn *sql.DB) (*App, error) {
  34. q := db.New(conn)
  35. sessionService := session.NewService(q)
  36. messageService := message.NewService(q)
  37. historyService := history.NewService(q, conn)
  38. permissionService := permission.NewPermissionService()
  39. statusService := status.NewService()
  40. // Initialize session manager
  41. session.InitManager(sessionService)
  42. // Initialize status service
  43. status.InitManager(statusService)
  44. app := &App{
  45. Sessions: sessionService,
  46. Messages: messageService,
  47. History: historyService,
  48. Permissions: permissionService,
  49. Status: statusService,
  50. LSPClients: make(map[string]*lsp.Client),
  51. }
  52. // Initialize theme based on configuration
  53. app.initTheme()
  54. // Initialize LSP clients in the background
  55. go app.initLSPClients(ctx)
  56. var err error
  57. app.CoderAgent, err = agent.NewAgent(
  58. config.AgentCoder,
  59. app.Sessions,
  60. app.Messages,
  61. agent.CoderAgentTools(
  62. app.Permissions,
  63. app.Sessions,
  64. app.Messages,
  65. app.History,
  66. app.LSPClients,
  67. ),
  68. )
  69. if err != nil {
  70. logging.Error("Failed to create coder agent", err)
  71. return nil, err
  72. }
  73. return app, nil
  74. }
  75. // initTheme sets the application theme based on the configuration
  76. func (app *App) initTheme() {
  77. cfg := config.Get()
  78. if cfg == nil || cfg.TUI.Theme == "" {
  79. return // Use default theme
  80. }
  81. // Try to set the theme from config
  82. err := theme.SetTheme(cfg.TUI.Theme)
  83. if err != nil {
  84. logging.Warn("Failed to set theme from config, using default theme", "theme", cfg.TUI.Theme, "error", err)
  85. } else {
  86. logging.Debug("Set theme from config", "theme", cfg.TUI.Theme)
  87. }
  88. }
  89. // Shutdown performs a clean shutdown of the application
  90. func (app *App) Shutdown() {
  91. // Cancel all watcher goroutines
  92. app.cancelFuncsMutex.Lock()
  93. for _, cancel := range app.watcherCancelFuncs {
  94. cancel()
  95. }
  96. app.cancelFuncsMutex.Unlock()
  97. app.watcherWG.Wait()
  98. // Perform additional cleanup for LSP clients
  99. app.clientsMutex.RLock()
  100. clients := make(map[string]*lsp.Client, len(app.LSPClients))
  101. maps.Copy(clients, app.LSPClients)
  102. app.clientsMutex.RUnlock()
  103. for name, client := range clients {
  104. shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  105. if err := client.Shutdown(shutdownCtx); err != nil {
  106. logging.Error("Failed to shutdown LSP client", "name", name, "error", err)
  107. }
  108. cancel()
  109. }
  110. }