app.go 2.9 KB

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