main.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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/v2"
  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.WithMouseCellMotion(),
  61. tea.WithKeyboardEnhancements(),
  62. tea.WithAltScreen(),
  63. )
  64. eventClient, err := client.NewClient(url)
  65. if err != nil {
  66. slog.Error("Failed to create event client", "error", err)
  67. os.Exit(1)
  68. }
  69. evts, err := eventClient.Event(ctx)
  70. if err != nil {
  71. slog.Error("Failed to subscribe to events", "error", err)
  72. os.Exit(1)
  73. }
  74. go func() {
  75. for item := range evts {
  76. program.Send(item)
  77. }
  78. }()
  79. // Setup the subscriptions, this will send services events to the TUI
  80. ch, cancelSubs := setupSubscriptions(app_, ctx)
  81. // Create a context for the TUI message handler
  82. tuiCtx, tuiCancel := context.WithCancel(ctx)
  83. var tuiWg sync.WaitGroup
  84. tuiWg.Add(1)
  85. // Set up message handling for the TUI
  86. go func() {
  87. defer tuiWg.Done()
  88. // defer logging.RecoverPanic("TUI-message-handler", func() {
  89. // attemptTUIRecovery(program)
  90. // })
  91. for {
  92. select {
  93. case <-tuiCtx.Done():
  94. slog.Info("TUI message handler shutting down")
  95. return
  96. case msg, ok := <-ch:
  97. if !ok {
  98. slog.Info("TUI message channel closed")
  99. return
  100. }
  101. program.Send(msg)
  102. }
  103. }
  104. }()
  105. // Cleanup function for when the program exits
  106. cleanup := func() {
  107. // Cancel subscriptions first
  108. cancelSubs()
  109. // Then shutdown the app
  110. app_.Shutdown()
  111. // Then cancel TUI message handler
  112. tuiCancel()
  113. // Wait for TUI message handler to finish
  114. tuiWg.Wait()
  115. slog.Info("All goroutines cleaned up")
  116. }
  117. // Run the TUI
  118. result, err := program.Run()
  119. cleanup()
  120. if err != nil {
  121. slog.Error("TUI error", "error", err)
  122. // return fmt.Errorf("TUI error: %v", err)
  123. }
  124. slog.Info("TUI exited", "result", result)
  125. }
  126. func setupSubscriber[T any](
  127. ctx context.Context,
  128. wg *sync.WaitGroup,
  129. name string,
  130. subscriber func(context.Context) <-chan pubsub.Event[T],
  131. outputCh chan<- tea.Msg,
  132. ) {
  133. wg.Add(1)
  134. go func() {
  135. defer wg.Done()
  136. // defer logging.RecoverPanic(fmt.Sprintf("subscription-%s", name), nil)
  137. subCh := subscriber(ctx)
  138. if subCh == nil {
  139. slog.Warn("subscription channel is nil", "name", name)
  140. return
  141. }
  142. for {
  143. select {
  144. case event, ok := <-subCh:
  145. if !ok {
  146. slog.Info("subscription channel closed", "name", name)
  147. return
  148. }
  149. var msg tea.Msg = event
  150. select {
  151. case outputCh <- msg:
  152. case <-time.After(2 * time.Second):
  153. slog.Warn("message dropped due to slow consumer", "name", name)
  154. case <-ctx.Done():
  155. slog.Info("subscription cancelled", "name", name)
  156. return
  157. }
  158. case <-ctx.Done():
  159. slog.Info("subscription cancelled", "name", name)
  160. return
  161. }
  162. }
  163. }()
  164. }
  165. func setupSubscriptions(app *app.App, parentCtx context.Context) (chan tea.Msg, func()) {
  166. ch := make(chan tea.Msg, 100)
  167. wg := sync.WaitGroup{}
  168. ctx, cancel := context.WithCancel(parentCtx) // Inherit from parent context
  169. setupSubscriber(ctx, &wg, "status", app.Status.Subscribe, ch)
  170. cleanupFunc := func() {
  171. slog.Info("Cancelling all subscriptions")
  172. cancel() // Signal all goroutines to stop
  173. waitCh := make(chan struct{})
  174. go func() {
  175. // defer logging.RecoverPanic("subscription-cleanup", nil)
  176. wg.Wait()
  177. close(waitCh)
  178. }()
  179. select {
  180. case <-waitCh:
  181. slog.Info("All subscription goroutines completed successfully")
  182. close(ch) // Only close after all writers are confirmed done
  183. case <-time.After(5 * time.Second):
  184. slog.Warn("Timed out waiting for some subscription goroutines to complete")
  185. close(ch)
  186. }
  187. }
  188. return ch, cleanupFunc
  189. }