main.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package main
  2. import (
  3. "context"
  4. "log/slog"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "sync"
  9. "time"
  10. tea "github.com/charmbracelet/bubbletea"
  11. zone "github.com/lrstanley/bubblezone"
  12. "github.com/sst/opencode/internal/app"
  13. "github.com/sst/opencode/internal/pubsub"
  14. "github.com/sst/opencode/internal/tui"
  15. "github.com/sst/opencode/pkg/client"
  16. )
  17. var Version = "dev"
  18. func main() {
  19. url := os.Getenv("OPENCODE_SERVER")
  20. httpClient, err := client.NewClientWithResponses(url)
  21. if err != nil {
  22. slog.Error("Failed to create client", "error", err)
  23. os.Exit(1)
  24. }
  25. paths, err := httpClient.PostPathGetWithResponse(context.Background())
  26. if err != nil {
  27. panic(err)
  28. }
  29. logfile := filepath.Join(paths.JSON200.Data, "log", "tui.log")
  30. if _, err := os.Stat(filepath.Dir(logfile)); os.IsNotExist(err) {
  31. err := os.MkdirAll(filepath.Dir(logfile), 0755)
  32. if err != nil {
  33. slog.Error("Failed to create log directory", "error", err)
  34. os.Exit(1)
  35. }
  36. }
  37. file, err := os.Create(logfile)
  38. if err != nil {
  39. slog.Error("Failed to create log file", "error", err)
  40. os.Exit(1)
  41. }
  42. defer file.Close()
  43. logger := slog.New(slog.NewTextHandler(file, &slog.HandlerOptions{Level: slog.LevelDebug}))
  44. slog.SetDefault(logger)
  45. // Create main context for the application
  46. ctx, cancel := context.WithCancel(context.Background())
  47. defer cancel()
  48. version := Version
  49. if version != "dev" && !strings.HasPrefix(Version, "v") {
  50. version = "v" + Version
  51. }
  52. app_, err := app.New(ctx, version, httpClient)
  53. if err != nil {
  54. panic(err)
  55. }
  56. // Set up the TUI
  57. zone.NewGlobal()
  58. program := tea.NewProgram(
  59. tui.NewModel(app_),
  60. tea.WithAltScreen(),
  61. )
  62. eventClient, err := client.NewClient(url)
  63. if err != nil {
  64. slog.Error("Failed to create event client", "error", err)
  65. os.Exit(1)
  66. }
  67. evts, err := eventClient.Event(ctx)
  68. if err != nil {
  69. slog.Error("Failed to subscribe to events", "error", err)
  70. os.Exit(1)
  71. }
  72. go func() {
  73. for item := range evts {
  74. program.Send(item)
  75. }
  76. }()
  77. // Setup the subscriptions, this will send services events to the TUI
  78. ch, cancelSubs := setupSubscriptions(app_, ctx)
  79. // Create a context for the TUI message handler
  80. tuiCtx, tuiCancel := context.WithCancel(ctx)
  81. var tuiWg sync.WaitGroup
  82. tuiWg.Add(1)
  83. // Set up message handling for the TUI
  84. go func() {
  85. defer tuiWg.Done()
  86. // defer logging.RecoverPanic("TUI-message-handler", func() {
  87. // attemptTUIRecovery(program)
  88. // })
  89. for {
  90. select {
  91. case <-tuiCtx.Done():
  92. slog.Info("TUI message handler shutting down")
  93. return
  94. case msg, ok := <-ch:
  95. if !ok {
  96. slog.Info("TUI message channel closed")
  97. return
  98. }
  99. program.Send(msg)
  100. }
  101. }
  102. }()
  103. // Cleanup function for when the program exits
  104. cleanup := func() {
  105. // Cancel subscriptions first
  106. cancelSubs()
  107. // Then shutdown the app
  108. app_.Shutdown()
  109. // Then cancel TUI message handler
  110. tuiCancel()
  111. // Wait for TUI message handler to finish
  112. tuiWg.Wait()
  113. slog.Info("All goroutines cleaned up")
  114. }
  115. // Run the TUI
  116. result, err := program.Run()
  117. cleanup()
  118. if err != nil {
  119. slog.Error("TUI error", "error", err)
  120. // return fmt.Errorf("TUI error: %v", err)
  121. }
  122. slog.Info("TUI exited", "result", result)
  123. }
  124. func setupSubscriber[T any](
  125. ctx context.Context,
  126. wg *sync.WaitGroup,
  127. name string,
  128. subscriber func(context.Context) <-chan pubsub.Event[T],
  129. outputCh chan<- tea.Msg,
  130. ) {
  131. wg.Add(1)
  132. go func() {
  133. defer wg.Done()
  134. // defer logging.RecoverPanic(fmt.Sprintf("subscription-%s", name), nil)
  135. subCh := subscriber(ctx)
  136. if subCh == nil {
  137. slog.Warn("subscription channel is nil", "name", name)
  138. return
  139. }
  140. for {
  141. select {
  142. case event, ok := <-subCh:
  143. if !ok {
  144. slog.Info("subscription channel closed", "name", name)
  145. return
  146. }
  147. var msg tea.Msg = event
  148. select {
  149. case outputCh <- msg:
  150. case <-time.After(2 * time.Second):
  151. slog.Warn("message dropped due to slow consumer", "name", name)
  152. case <-ctx.Done():
  153. slog.Info("subscription cancelled", "name", name)
  154. return
  155. }
  156. case <-ctx.Done():
  157. slog.Info("subscription cancelled", "name", name)
  158. return
  159. }
  160. }
  161. }()
  162. }
  163. func setupSubscriptions(app *app.App, parentCtx context.Context) (chan tea.Msg, func()) {
  164. ch := make(chan tea.Msg, 100)
  165. wg := sync.WaitGroup{}
  166. ctx, cancel := context.WithCancel(parentCtx) // Inherit from parent context
  167. setupSubscriber(ctx, &wg, "status", app.Status.Subscribe, ch)
  168. cleanupFunc := func() {
  169. slog.Info("Cancelling all subscriptions")
  170. cancel() // Signal all goroutines to stop
  171. waitCh := make(chan struct{})
  172. go func() {
  173. // defer logging.RecoverPanic("subscription-cleanup", nil)
  174. wg.Wait()
  175. close(waitCh)
  176. }()
  177. select {
  178. case <-waitCh:
  179. slog.Info("All subscription goroutines completed successfully")
  180. close(ch) // Only close after all writers are confirmed done
  181. case <-time.After(5 * time.Second):
  182. slog.Warn("Timed out waiting for some subscription goroutines to complete")
  183. close(ch)
  184. }
  185. }
  186. return ch, cleanupFunc
  187. }