app.go 3.0 KB

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