main.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 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. }