run.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "log/slog"
  6. "os"
  7. "os/signal"
  8. "strings"
  9. "time"
  10. "charm.land/lipgloss/v2"
  11. "charm.land/log/v2"
  12. "github.com/charmbracelet/crush/internal/client"
  13. "github.com/charmbracelet/crush/internal/config"
  14. "github.com/charmbracelet/crush/internal/event"
  15. "github.com/charmbracelet/crush/internal/format"
  16. "github.com/charmbracelet/crush/internal/proto"
  17. "github.com/charmbracelet/crush/internal/pubsub"
  18. "github.com/charmbracelet/crush/internal/session"
  19. "github.com/charmbracelet/crush/internal/ui/anim"
  20. "github.com/charmbracelet/crush/internal/ui/styles"
  21. "github.com/charmbracelet/crush/internal/workspace"
  22. "github.com/charmbracelet/x/ansi"
  23. "github.com/charmbracelet/x/exp/charmtone"
  24. "github.com/charmbracelet/x/term"
  25. "github.com/spf13/cobra"
  26. )
  27. var runCmd = &cobra.Command{
  28. Aliases: []string{"r"},
  29. Use: "run [prompt...]",
  30. Short: "Run a single non-interactive prompt",
  31. Long: `Run a single prompt in non-interactive mode and exit.
  32. The prompt can be provided as arguments or piped from stdin.`,
  33. Example: `
  34. # Run a simple prompt
  35. crush run "Guess my 5 favorite Pokémon"
  36. # Pipe input from stdin
  37. curl https://charm.land | crush run "Summarize this website"
  38. # Read from a file
  39. crush run "What is this code doing?" <<< prrr.go
  40. # Redirect output to a file
  41. crush run "Generate a hot README for this project" > MY_HOT_README.md
  42. # Run in quiet mode (hide the spinner)
  43. crush run --quiet "Generate a README for this project"
  44. # Run in verbose mode (show logs)
  45. crush run --verbose "Generate a README for this project"
  46. # Continue a previous session
  47. crush run --session {session-id} "Follow up on your last response"
  48. # Continue the most recent session
  49. crush run --continue "Follow up on your last response"
  50. `,
  51. RunE: func(cmd *cobra.Command, args []string) error {
  52. var (
  53. quiet, _ = cmd.Flags().GetBool("quiet")
  54. verbose, _ = cmd.Flags().GetBool("verbose")
  55. largeModel, _ = cmd.Flags().GetString("model")
  56. smallModel, _ = cmd.Flags().GetString("small-model")
  57. sessionID, _ = cmd.Flags().GetString("session")
  58. useLast, _ = cmd.Flags().GetBool("continue")
  59. )
  60. // Cancel on SIGINT or SIGTERM.
  61. ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
  62. defer cancel()
  63. prompt := strings.Join(args, " ")
  64. prompt, err := MaybePrependStdin(prompt)
  65. if err != nil {
  66. slog.Error("Failed to read from stdin", "error", err)
  67. return err
  68. }
  69. if prompt == "" {
  70. return fmt.Errorf("no prompt provided")
  71. }
  72. event.SetNonInteractive(true)
  73. switch {
  74. case sessionID != "":
  75. event.SetContinueBySessionID(true)
  76. case useLast:
  77. event.SetContinueLastSession(true)
  78. }
  79. if useClientServer() {
  80. c, ws, cleanup, err := connectToServer(cmd)
  81. if err != nil {
  82. return err
  83. }
  84. defer cleanup()
  85. event.AppInitialized()
  86. if sessionID != "" {
  87. sess, err := resolveSessionByID(ctx, c, ws.ID, sessionID)
  88. if err != nil {
  89. return err
  90. }
  91. sessionID = sess.ID
  92. }
  93. if !ws.Config.IsConfigured() {
  94. return fmt.Errorf("no providers configured - please run 'crush' to set up a provider interactively")
  95. }
  96. if verbose {
  97. slog.SetDefault(slog.New(log.New(os.Stderr)))
  98. }
  99. return runNonInteractive(ctx, c, ws, prompt, largeModel, smallModel, quiet || verbose, sessionID, useLast)
  100. }
  101. ws, cleanup, err := setupLocalWorkspace(cmd)
  102. if err != nil {
  103. return err
  104. }
  105. defer cleanup()
  106. event.AppInitialized()
  107. if !ws.Config().IsConfigured() {
  108. return fmt.Errorf("no providers configured - please run 'crush' to set up a provider interactively")
  109. }
  110. if verbose {
  111. slog.SetDefault(slog.New(log.New(os.Stderr)))
  112. }
  113. appWs := ws.(*workspace.AppWorkspace)
  114. return appWs.App().RunNonInteractive(ctx, os.Stdout, prompt, largeModel, smallModel, quiet || verbose, sessionID, useLast)
  115. },
  116. }
  117. func init() {
  118. runCmd.Flags().BoolP("quiet", "q", false, "Hide spinner")
  119. runCmd.Flags().BoolP("verbose", "v", false, "Show logs")
  120. runCmd.Flags().StringP("model", "m", "", "Model to use. Accepts 'model' or 'provider/model' to disambiguate models with the same name across providers")
  121. runCmd.Flags().String("small-model", "", "Small model to use. If not provided, uses the default small model for the provider")
  122. runCmd.Flags().StringP("session", "s", "", "Continue a previous session by ID")
  123. runCmd.Flags().BoolP("continue", "C", false, "Continue the most recent session")
  124. runCmd.MarkFlagsMutuallyExclusive("session", "continue")
  125. }
  126. // runNonInteractive executes the agent via the server and streams output
  127. // to stdout.
  128. func runNonInteractive(
  129. ctx context.Context,
  130. c *client.Client,
  131. ws *proto.Workspace,
  132. prompt, largeModel, smallModel string,
  133. hideSpinner bool,
  134. continueSessionID string,
  135. useLast bool,
  136. ) error {
  137. slog.Info("Running in non-interactive mode")
  138. ctx, cancel := context.WithCancel(ctx)
  139. defer cancel()
  140. if largeModel != "" || smallModel != "" {
  141. if err := overrideModels(ctx, c, ws, largeModel, smallModel); err != nil {
  142. return fmt.Errorf("failed to override models: %w", err)
  143. }
  144. }
  145. var (
  146. spinner *format.Spinner
  147. stdoutTTY bool
  148. stderrTTY bool
  149. stdinTTY bool
  150. progress bool
  151. )
  152. stdoutTTY = term.IsTerminal(os.Stdout.Fd())
  153. stderrTTY = term.IsTerminal(os.Stderr.Fd())
  154. stdinTTY = term.IsTerminal(os.Stdin.Fd())
  155. progress = ws.Config.Options.Progress == nil || *ws.Config.Options.Progress
  156. if !hideSpinner && stderrTTY {
  157. t := styles.DefaultStyles()
  158. hasDarkBG := true
  159. if stdinTTY && stdoutTTY {
  160. hasDarkBG = lipgloss.HasDarkBackground(os.Stdin, os.Stdout)
  161. }
  162. defaultFG := lipgloss.LightDark(hasDarkBG)(charmtone.Pepper, t.FgBase)
  163. spinner = format.NewSpinner(ctx, cancel, anim.Settings{
  164. Size: 10,
  165. Label: "Generating",
  166. LabelColor: defaultFG,
  167. GradColorA: t.Primary,
  168. GradColorB: t.Secondary,
  169. CycleColors: true,
  170. })
  171. spinner.Start()
  172. }
  173. stopSpinner := func() {
  174. if !hideSpinner && spinner != nil {
  175. spinner.Stop()
  176. spinner = nil
  177. }
  178. }
  179. // Wait for the agent to become ready (MCP init, etc).
  180. if err := waitForAgent(ctx, c, ws.ID); err != nil {
  181. stopSpinner()
  182. return fmt.Errorf("agent not ready: %w", err)
  183. }
  184. // Force-update agent models so MCP tools are loaded.
  185. if err := c.UpdateAgent(ctx, ws.ID); err != nil {
  186. slog.Warn("Failed to update agent", "error", err)
  187. }
  188. defer stopSpinner()
  189. sess, err := resolveSession(ctx, c, ws.ID, continueSessionID, useLast)
  190. if err != nil {
  191. return fmt.Errorf("failed to resolve session: %w", err)
  192. }
  193. if continueSessionID != "" || useLast {
  194. slog.Info("Continuing session for non-interactive run", "session_id", sess.ID)
  195. } else {
  196. slog.Info("Created session for non-interactive run", "session_id", sess.ID)
  197. }
  198. events, err := c.SubscribeEvents(ctx, ws.ID)
  199. if err != nil {
  200. return fmt.Errorf("failed to subscribe to events: %w", err)
  201. }
  202. if err := c.SendMessage(ctx, ws.ID, sess.ID, prompt); err != nil {
  203. return fmt.Errorf("failed to send message: %w", err)
  204. }
  205. messageReadBytes := make(map[string]int)
  206. var printed bool
  207. defer func() {
  208. if progress && stderrTTY {
  209. _, _ = fmt.Fprintf(os.Stderr, ansi.ResetProgressBar)
  210. }
  211. _, _ = fmt.Fprintln(os.Stdout)
  212. }()
  213. for {
  214. if progress && stderrTTY {
  215. _, _ = fmt.Fprintf(os.Stderr, ansi.SetIndeterminateProgressBar)
  216. }
  217. select {
  218. case ev, ok := <-events:
  219. if !ok {
  220. stopSpinner()
  221. return nil
  222. }
  223. switch e := ev.(type) {
  224. case pubsub.Event[proto.Message]:
  225. msg := e.Payload
  226. if msg.SessionID != sess.ID || msg.Role != proto.Assistant || len(msg.Parts) == 0 {
  227. continue
  228. }
  229. stopSpinner()
  230. content := msg.Content().String()
  231. readBytes := messageReadBytes[msg.ID]
  232. if len(content) < readBytes {
  233. slog.Error("Non-interactive: message content shorter than read bytes",
  234. "message_length", len(content), "read_bytes", readBytes)
  235. return fmt.Errorf("message content is shorter than read bytes: %d < %d", len(content), readBytes)
  236. }
  237. part := content[readBytes:]
  238. if readBytes == 0 {
  239. part = strings.TrimLeft(part, " \t")
  240. }
  241. if printed || strings.TrimSpace(part) != "" {
  242. printed = true
  243. fmt.Fprint(os.Stdout, part)
  244. }
  245. messageReadBytes[msg.ID] = len(content)
  246. if msg.IsFinished() {
  247. return nil
  248. }
  249. case pubsub.Event[proto.AgentEvent]:
  250. if e.Payload.Error != nil {
  251. stopSpinner()
  252. return fmt.Errorf("agent error: %w", e.Payload.Error)
  253. }
  254. }
  255. case <-ctx.Done():
  256. stopSpinner()
  257. return ctx.Err()
  258. }
  259. }
  260. }
  261. // waitForAgent polls GetAgentInfo until the agent is ready, with a
  262. // timeout.
  263. func waitForAgent(ctx context.Context, c *client.Client, wsID string) error {
  264. timeout := time.After(30 * time.Second)
  265. for {
  266. info, err := c.GetAgentInfo(ctx, wsID)
  267. if err == nil && info.IsReady {
  268. return nil
  269. }
  270. select {
  271. case <-timeout:
  272. if err != nil {
  273. return fmt.Errorf("timeout waiting for agent: %w", err)
  274. }
  275. return fmt.Errorf("timeout waiting for agent readiness")
  276. case <-ctx.Done():
  277. return ctx.Err()
  278. case <-time.After(200 * time.Millisecond):
  279. }
  280. }
  281. }
  282. // overrideModels resolves model strings and updates the workspace
  283. // configuration via the server.
  284. func overrideModels(
  285. ctx context.Context,
  286. c *client.Client,
  287. ws *proto.Workspace,
  288. largeModel, smallModel string,
  289. ) error {
  290. cfg, err := c.GetConfig(ctx, ws.ID)
  291. if err != nil {
  292. return fmt.Errorf("failed to get config: %w", err)
  293. }
  294. providers := cfg.Providers.Copy()
  295. largeMatches, smallMatches := findModelMatches(providers, largeModel, smallModel)
  296. var largeProviderID string
  297. if largeModel != "" {
  298. found, err := validateModelMatches(largeMatches, largeModel, "large")
  299. if err != nil {
  300. return err
  301. }
  302. largeProviderID = found.provider
  303. slog.Info("Overriding large model", "provider", found.provider, "model", found.modelID)
  304. if err := c.UpdatePreferredModel(ctx, ws.ID, config.ScopeWorkspace, config.SelectedModelTypeLarge, config.SelectedModel{
  305. Provider: found.provider,
  306. Model: found.modelID,
  307. }); err != nil {
  308. return fmt.Errorf("failed to set large model: %w", err)
  309. }
  310. }
  311. switch {
  312. case smallModel != "":
  313. found, err := validateModelMatches(smallMatches, smallModel, "small")
  314. if err != nil {
  315. return err
  316. }
  317. slog.Info("Overriding small model", "provider", found.provider, "model", found.modelID)
  318. if err := c.UpdatePreferredModel(ctx, ws.ID, config.ScopeWorkspace, config.SelectedModelTypeSmall, config.SelectedModel{
  319. Provider: found.provider,
  320. Model: found.modelID,
  321. }); err != nil {
  322. return fmt.Errorf("failed to set small model: %w", err)
  323. }
  324. case largeModel != "":
  325. sm, err := c.GetDefaultSmallModel(ctx, ws.ID, largeProviderID)
  326. if err != nil {
  327. slog.Warn("Failed to get default small model", "error", err)
  328. } else if sm != nil {
  329. if err := c.UpdatePreferredModel(ctx, ws.ID, config.ScopeWorkspace, config.SelectedModelTypeSmall, *sm); err != nil {
  330. return fmt.Errorf("failed to set small model: %w", err)
  331. }
  332. }
  333. }
  334. return c.UpdateAgent(ctx, ws.ID)
  335. }
  336. type modelMatch struct {
  337. provider string
  338. modelID string
  339. }
  340. // findModelMatches searches providers for matching large/small model
  341. // strings.
  342. func findModelMatches(providers map[string]config.ProviderConfig, largeModel, smallModel string) ([]modelMatch, []modelMatch) {
  343. largeFilter, largeID := parseModelString(largeModel)
  344. smallFilter, smallID := parseModelString(smallModel)
  345. var largeMatches, smallMatches []modelMatch
  346. for name, provider := range providers {
  347. if provider.Disable {
  348. continue
  349. }
  350. for _, m := range provider.Models {
  351. if matchesModel(largeID, largeFilter, m.ID, name) {
  352. largeMatches = append(largeMatches, modelMatch{provider: name, modelID: m.ID})
  353. }
  354. if matchesModel(smallID, smallFilter, m.ID, name) {
  355. smallMatches = append(smallMatches, modelMatch{provider: name, modelID: m.ID})
  356. }
  357. }
  358. }
  359. return largeMatches, smallMatches
  360. }
  361. // parseModelString splits "provider/model" into (provider, model) or
  362. // ("", model).
  363. func parseModelString(s string) (string, string) {
  364. if s == "" {
  365. return "", ""
  366. }
  367. if idx := strings.Index(s, "/"); idx >= 0 {
  368. return s[:idx], s[idx+1:]
  369. }
  370. return "", s
  371. }
  372. // matchesModel returns true if the model ID matches the filter
  373. // criteria.
  374. func matchesModel(wantID, wantProvider, modelID, providerName string) bool {
  375. if wantID == "" {
  376. return false
  377. }
  378. if wantProvider != "" && wantProvider != providerName {
  379. return false
  380. }
  381. return strings.EqualFold(modelID, wantID)
  382. }
  383. // validateModelMatches ensures exactly one match exists.
  384. func validateModelMatches(matches []modelMatch, modelID, label string) (modelMatch, error) {
  385. switch {
  386. case len(matches) == 0:
  387. return modelMatch{}, fmt.Errorf("%s model %q not found", label, modelID)
  388. case len(matches) > 1:
  389. names := make([]string, len(matches))
  390. for i, m := range matches {
  391. names[i] = m.provider
  392. }
  393. return modelMatch{}, fmt.Errorf(
  394. "%s model: model %q found in multiple providers: %s. Please specify provider using 'provider/model' format",
  395. label, modelID, strings.Join(names, ", "),
  396. )
  397. }
  398. return matches[0], nil
  399. }
  400. // resolveSession returns the session to use for a non-interactive run.
  401. // If continueSessionID is set it fetches that session; if useLast is set it
  402. // returns the most recently updated top-level session; otherwise it creates a
  403. // new one.
  404. func resolveSession(ctx context.Context, c *client.Client, wsID, continueSessionID string, useLast bool) (*proto.Session, error) {
  405. switch {
  406. case continueSessionID != "":
  407. sess, err := c.GetSession(ctx, wsID, continueSessionID)
  408. if err != nil {
  409. return nil, fmt.Errorf("session not found: %s", continueSessionID)
  410. }
  411. if sess.ParentSessionID != "" {
  412. return nil, fmt.Errorf("cannot continue a child session: %s", continueSessionID)
  413. }
  414. return sess, nil
  415. case useLast:
  416. sessions, err := c.ListSessions(ctx, wsID)
  417. if err != nil || len(sessions) == 0 {
  418. return nil, fmt.Errorf("no sessions found to continue")
  419. }
  420. last := sessions[0]
  421. for _, s := range sessions[1:] {
  422. if s.UpdatedAt > last.UpdatedAt && s.ParentSessionID == "" {
  423. last = s
  424. }
  425. }
  426. return &last, nil
  427. default:
  428. return c.CreateSession(ctx, wsID, "non-interactive")
  429. }
  430. }
  431. // resolveSessionByID resolves a session ID that may be a full UUID or a hash
  432. // prefix returned by crush session list.
  433. func resolveSessionByID(ctx context.Context, c *client.Client, wsID, id string) (*proto.Session, error) {
  434. if sess, err := c.GetSession(ctx, wsID, id); err == nil {
  435. return sess, nil
  436. }
  437. sessions, err := c.ListSessions(ctx, wsID)
  438. if err != nil {
  439. return nil, err
  440. }
  441. var matches []proto.Session
  442. for _, s := range sessions {
  443. hash := session.HashID(s.ID)
  444. if hash == id || strings.HasPrefix(hash, id) {
  445. matches = append(matches, s)
  446. }
  447. }
  448. switch len(matches) {
  449. case 0:
  450. return nil, fmt.Errorf("session %q not found", id)
  451. case 1:
  452. return &matches[0], nil
  453. default:
  454. return nil, fmt.Errorf("session ID %q is ambiguous (%d matches)", id, len(matches))
  455. }
  456. }