root.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "sync"
  7. "time"
  8. tea "github.com/charmbracelet/bubbletea/v2"
  9. "github.com/charmbracelet/crush/internal/app"
  10. "github.com/charmbracelet/crush/internal/config"
  11. "github.com/charmbracelet/crush/internal/db"
  12. "github.com/charmbracelet/crush/internal/format"
  13. "github.com/charmbracelet/crush/internal/llm/agent"
  14. "github.com/charmbracelet/crush/internal/logging"
  15. "github.com/charmbracelet/crush/internal/pubsub"
  16. "github.com/charmbracelet/crush/internal/tui"
  17. "github.com/charmbracelet/crush/internal/version"
  18. "github.com/charmbracelet/fang"
  19. "github.com/spf13/cobra"
  20. )
  21. var rootCmd = &cobra.Command{
  22. Use: "crush",
  23. Short: "Terminal-based AI assistant for software development",
  24. Long: `Crush is a powerful terminal-based AI assistant that helps with software development tasks.
  25. It provides an interactive chat interface with AI capabilities, code analysis, and LSP integration
  26. to assist developers in writing, debugging, and understanding code directly from the terminal.`,
  27. Example: `
  28. # Run in interactive mode
  29. crush
  30. # Run with debug logging
  31. crush -d
  32. # Run with debug logging in a specific directory
  33. crush -d -c /path/to/project
  34. # Print version
  35. crush -v
  36. # Run a single non-interactive prompt
  37. crush -p "Explain the use of context in Go"
  38. # Run a single non-interactive prompt with JSON output format
  39. crush -p "Explain the use of context in Go" -f json
  40. `,
  41. RunE: func(cmd *cobra.Command, args []string) error {
  42. // Load the config
  43. debug, _ := cmd.Flags().GetBool("debug")
  44. cwd, _ := cmd.Flags().GetString("cwd")
  45. prompt, _ := cmd.Flags().GetString("prompt")
  46. outputFormat, _ := cmd.Flags().GetString("output-format")
  47. quiet, _ := cmd.Flags().GetBool("quiet")
  48. // Validate format option
  49. if !format.IsValid(outputFormat) {
  50. return fmt.Errorf("invalid format option: %s\n%s", outputFormat, format.GetHelpText())
  51. }
  52. if cwd != "" {
  53. err := os.Chdir(cwd)
  54. if err != nil {
  55. return fmt.Errorf("failed to change directory: %v", err)
  56. }
  57. }
  58. if cwd == "" {
  59. c, err := os.Getwd()
  60. if err != nil {
  61. return fmt.Errorf("failed to get current working directory: %v", err)
  62. }
  63. cwd = c
  64. }
  65. _, err := config.Init(cwd, debug)
  66. if err != nil {
  67. return err
  68. }
  69. // Create main context for the application
  70. ctx, cancel := context.WithCancel(context.Background())
  71. defer cancel()
  72. // Connect DB, this will also run migrations
  73. conn, err := db.Connect(ctx)
  74. if err != nil {
  75. return err
  76. }
  77. app, err := app.New(ctx, conn)
  78. if err != nil {
  79. logging.Error("Failed to create app: %v", err)
  80. return err
  81. }
  82. // Defer shutdown here so it runs for both interactive and non-interactive modes
  83. defer app.Shutdown()
  84. // Initialize MCP tools early for both modes
  85. initMCPTools(ctx, app)
  86. // Non-interactive mode
  87. if prompt != "" {
  88. // Run non-interactive flow using the App method
  89. return app.RunNonInteractive(ctx, prompt, outputFormat, quiet)
  90. }
  91. // Set up the TUI
  92. program := tea.NewProgram(
  93. tui.New(app),
  94. tea.WithAltScreen(),
  95. tea.WithKeyReleases(),
  96. tea.WithUniformKeyLayout(),
  97. )
  98. // Setup the subscriptions, this will send services events to the TUI
  99. ch, cancelSubs := setupSubscriptions(app, ctx)
  100. // Create a context for the TUI message handler
  101. tuiCtx, tuiCancel := context.WithCancel(ctx)
  102. var tuiWg sync.WaitGroup
  103. tuiWg.Add(1)
  104. // Set up message handling for the TUI
  105. go func() {
  106. defer tuiWg.Done()
  107. defer logging.RecoverPanic("TUI-message-handler", func() {
  108. attemptTUIRecovery(program)
  109. })
  110. for {
  111. select {
  112. case <-tuiCtx.Done():
  113. logging.Info("TUI message handler shutting down")
  114. return
  115. case msg, ok := <-ch:
  116. if !ok {
  117. logging.Info("TUI message channel closed")
  118. return
  119. }
  120. program.Send(msg)
  121. }
  122. }
  123. }()
  124. // Cleanup function for when the program exits
  125. cleanup := func() {
  126. // Shutdown the app
  127. app.Shutdown()
  128. // Cancel subscriptions first
  129. cancelSubs()
  130. // Then cancel TUI message handler
  131. tuiCancel()
  132. // Wait for TUI message handler to finish
  133. tuiWg.Wait()
  134. logging.Info("All goroutines cleaned up")
  135. }
  136. // Run the TUI
  137. result, err := program.Run()
  138. cleanup()
  139. if err != nil {
  140. logging.Error("TUI error: %v", err)
  141. return fmt.Errorf("TUI error: %v", err)
  142. }
  143. logging.Info("TUI exited with result: %v", result)
  144. return nil
  145. },
  146. }
  147. // attemptTUIRecovery tries to recover the TUI after a panic
  148. func attemptTUIRecovery(program *tea.Program) {
  149. logging.Info("Attempting to recover TUI after panic")
  150. // We could try to restart the TUI or gracefully exit
  151. // For now, we'll just quit the program to avoid further issues
  152. program.Quit()
  153. }
  154. func initMCPTools(ctx context.Context, app *app.App) {
  155. go func() {
  156. defer logging.RecoverPanic("MCP-goroutine", nil)
  157. // Create a context with timeout for the initial MCP tools fetch
  158. ctxWithTimeout, cancel := context.WithTimeout(ctx, 30*time.Second)
  159. defer cancel()
  160. // Set this up once with proper error handling
  161. agent.GetMcpTools(ctxWithTimeout, app.Permissions)
  162. logging.Info("MCP message handling goroutine exiting")
  163. }()
  164. }
  165. func setupSubscriber[T any](
  166. ctx context.Context,
  167. wg *sync.WaitGroup,
  168. name string,
  169. subscriber func(context.Context) <-chan pubsub.Event[T],
  170. outputCh chan<- tea.Msg,
  171. ) {
  172. wg.Add(1)
  173. go func() {
  174. defer wg.Done()
  175. defer logging.RecoverPanic(fmt.Sprintf("subscription-%s", name), nil)
  176. subCh := subscriber(ctx)
  177. for {
  178. select {
  179. case event, ok := <-subCh:
  180. if !ok {
  181. logging.Info("subscription channel closed", "name", name)
  182. return
  183. }
  184. var msg tea.Msg = event
  185. select {
  186. case outputCh <- msg:
  187. case <-time.After(2 * time.Second):
  188. logging.Warn("message dropped due to slow consumer", "name", name)
  189. case <-ctx.Done():
  190. logging.Info("subscription cancelled", "name", name)
  191. return
  192. }
  193. case <-ctx.Done():
  194. logging.Info("subscription cancelled", "name", name)
  195. return
  196. }
  197. }
  198. }()
  199. }
  200. func setupSubscriptions(app *app.App, parentCtx context.Context) (chan tea.Msg, func()) {
  201. ch := make(chan tea.Msg, 100)
  202. wg := sync.WaitGroup{}
  203. ctx, cancel := context.WithCancel(parentCtx) // Inherit from parent context
  204. setupSubscriber(ctx, &wg, "logging", logging.Subscribe, ch)
  205. setupSubscriber(ctx, &wg, "sessions", app.Sessions.Subscribe, ch)
  206. setupSubscriber(ctx, &wg, "messages", app.Messages.Subscribe, ch)
  207. setupSubscriber(ctx, &wg, "permissions", app.Permissions.Subscribe, ch)
  208. setupSubscriber(ctx, &wg, "coderAgent", app.CoderAgent.Subscribe, ch)
  209. setupSubscriber(ctx, &wg, "history", app.History.Subscribe, ch)
  210. cleanupFunc := func() {
  211. logging.Info("Cancelling all subscriptions")
  212. cancel() // Signal all goroutines to stop
  213. waitCh := make(chan struct{})
  214. go func() {
  215. defer logging.RecoverPanic("subscription-cleanup", nil)
  216. wg.Wait()
  217. close(waitCh)
  218. }()
  219. select {
  220. case <-waitCh:
  221. logging.Info("All subscription goroutines completed successfully")
  222. close(ch) // Only close after all writers are confirmed done
  223. case <-time.After(5 * time.Second):
  224. logging.Warn("Timed out waiting for some subscription goroutines to complete")
  225. close(ch)
  226. }
  227. }
  228. return ch, cleanupFunc
  229. }
  230. func Execute() {
  231. if err := fang.Execute(
  232. context.Background(),
  233. rootCmd,
  234. fang.WithVersion(version.Version),
  235. ); err != nil {
  236. os.Exit(1)
  237. }
  238. }
  239. func init() {
  240. rootCmd.Flags().BoolP("help", "h", false, "Help")
  241. rootCmd.Flags().BoolP("debug", "d", false, "Debug")
  242. rootCmd.Flags().StringP("cwd", "c", "", "Current working directory")
  243. rootCmd.Flags().StringP("prompt", "p", "", "Prompt to run in non-interactive mode")
  244. // Add format flag with validation logic
  245. rootCmd.Flags().StringP("output-format", "f", format.Text.String(),
  246. "Output format for non-interactive mode (text, json)")
  247. // Add quiet flag to hide spinner in non-interactive mode
  248. rootCmd.Flags().BoolP("quiet", "q", false, "Hide spinner in non-interactive mode")
  249. // Register custom validation for the format flag
  250. rootCmd.RegisterFlagCompletionFunc("output-format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  251. return format.SupportedFormats, cobra.ShellCompDirectiveNoFileComp
  252. })
  253. }