app.go 4.7 KB

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