on main help sign in
feat: add support for `--session` and `--continue` for the tui (#2422)

These flags were previously only available for `crush run`.

commit630ff8c5dfeaabc96b8f05917d28455ad7e1075e authorAndrey Nering <andreynering@users.noreply.github.com> dateThu 19 Mar 2026, 11:06 (7 months ago) parent724102d
2 files changed+55−2
internal/cmd/root.go+22−1 internal/ui/model/ui.go+33−1
internal/cmd/root.go+22−1
@@ -37,6 +37,9 @@ func init() {
3737 rootCmd.PersistentFlags().BoolP("debug", "d", false, "Debug")
3838 rootCmd.Flags().BoolP("help", "h", false, "Help")
3939 rootCmd.Flags().BoolP("yolo", "y", false, "Automatically accept all permissions (dangerous mode)")
40+ rootCmd.Flags().StringP("session", "s", "", "Continue a previous session by ID")
41+ rootCmd.Flags().BoolP("continue", "C", false, "Continue the most recent session")
42+ rootCmd.MarkFlagsMutuallyExclusive("session", "continue")
4043
4144 rootCmd.AddCommand(
4245 runCmd,
@@ -73,21 +76,39 @@ crush --yolo
7376
7477# Run with custom data directory
7578crush --data-dir /path/to/custom/.crush
79+
80+# Continue a previous session
81+crush --session {session-id}
82+
83+# Continue the most recent session
84+crush --continue
7685 `,
7786 RunE: func(cmd *cobra.Command, args []string) error {
87+ sessionID, _ := cmd.Flags().GetString("session")
88+ continueLast, _ := cmd.Flags().GetBool("continue")
89+
7890 app, err := setupAppWithProgressBar(cmd)
7991 if err != nil {
8092 return err
8193 }
8294 defer app.Shutdown()
8395
96+ // Resolve session ID if provided
97+ if sessionID != "" {
98+ sess, err := resolveSessionID(cmd.Context(), app.Sessions, sessionID)
99+ if err != nil {
100+ return err
101+ }
102+ sessionID = sess.ID
103+ }
104+
84105 event.AppInitialized()
85106
86107 // Set up the TUI.
87108 var env uv.Environ = os.Environ()
88109
89110 com := common.DefaultCommon(app)
90− model := ui.New(com)
111+ model := ui.New(com, sessionID, continueLast)
91112
92113 program := tea.NewProgram(
93114 model,
internal/ui/model/ui.go+33−1
@@ -139,6 +139,11 @@ type UI struct {
139139 // keeps track of read files while we don't have a session id
140140 sessionFileReads []string
141141
142+ // initialSessionID is set when loading a specific session on startup.
143+ initialSessionID string
144+ // continueLastSession is set to continue the most recent session on startup.
145+ continueLastSession bool
146+
142147 lastUserMessageTime int64
143148
144149 // The width and height of the terminal in cells.
@@ -242,7 +247,7 @@ type UI struct {
242247}
243248
244249// New creates a new instance of the [UI] model.
245−func New(com *common.Common) *UI {
250+func New(com *common.Common, initialSessionID string, continueLast bool) *UI {
246251 // Editor components
247252 ta := textarea.New()
248253 ta.SetStyles(com.Styles.TextArea)
@@ -298,6 +303,8 @@ func New(com *common.Common) *UI {
298303 mcpStates: make(map[string]mcp.ClientInfo),
299304 notifyBackend: notification.NoopBackend{},
300305 notifyWindowFocused: true,
306+ initialSessionID: initialSessionID,
307+ continueLastSession: continueLast,
301308 }
302309
303310 status := NewStatus(com, ui)
@@ -346,9 +353,34 @@ func (m *UI) Init() tea.Cmd {
346353 cmds = append(cmds, m.loadCustomCommands())
347354 // load prompt history async
348355 cmds = append(cmds, m.loadPromptHistory())
356+ // load initial session if specified
357+ if cmd := m.loadInitialSession(); cmd != nil {
358+ cmds = append(cmds, cmd)
359+ }
349360 return tea.Batch(cmds...)
350361}
351362
363+// loadInitialSession loads the initial session if one was specified on startup.
364+func (m *UI) loadInitialSession() tea.Cmd {
365+ switch {
366+ case m.state != uiLanding:
367+ // Only load if we're in landing state (i.e., fully configured)
368+ return nil
369+ case m.initialSessionID != "":
370+ return m.loadSession(m.initialSessionID)
371+ case m.continueLastSession:
372+ return func() tea.Msg {
373+ sess, err := m.com.App.Sessions.GetLast(context.Background())
374+ if err != nil {
375+ return nil
376+ }
377+ return m.loadSession(sess.ID)()
378+ }
379+ default:
380+ return nil
381+ }
382+}
383+
352384// sendNotification returns a command that sends a notification if allowed by policy.
353385func (m *UI) sendNotification(n notification.Notification) tea.Cmd {
354386 if !m.shouldSendNotification() {