Skip to content

feat: add auto-switch Claude Code mode before sending prompts - #5

Merged
MartianGreed merged 1 commit into
mainfrom
feat/auto-switch-mode
Jan 22, 2026
Merged

MartianGreed merged 1 commit into
mainfrom
feat/auto-switch-mode

Conversation

@MartianGreed

Copy link
Copy Markdown
Owner

Summary

  • Adds default_mode config option to UIConfig (plan/code/auto/edit or empty to disable)
  • Detects current Claude mode from terminal output
  • Automatically cycles Shift+Tab to switch to target mode before sending prompts

Config Example

ui:
  default_mode: plan

Test plan

  • Build passes (make build)
  • Tests pass (make test)
  • Lint passes (make lint)
  • Manual test: set default_mode: plan, send prompt from session in edit mode, verify mode switches

@greptile-apps

greptile-apps Bot commented Jan 22, 2026

Copy link
Copy Markdown

Greptile Summary

Adds automatic Claude Code mode switching before sending prompts. When default_mode is configured in the UI settings, the tool detects the current mode from terminal output and cycles Shift+Tab until reaching the target mode (plan/code/auto/edit).

Key Changes:

  • Added mode detection regex pattern to identify current Claude mode from terminal output
  • Implemented switchToMode() that cycles through modes using Shift+Tab up to 3 times
  • Enhanced multi-line paste handling with double Enter for 3+ line content
  • Improved Claude session ID persistence across restarts via database storage
  • Added global usage tracking overlay (press u to view aggregated stats)
  • Changed header to display total usage across all sessions instead of selected session only

Issues Found:

  • Line 1145 in internal/tui/model.go returns true even when mode switch fails after exhausting retry attempts, which could mislead activity logs

Confidence Score: 4/5

  • Safe to merge after fixing the return value bug in switchToMode
  • Implementation is well-tested and follows Go conventions. One logical bug exists where switchToMode returns true instead of false on failure, which could cause misleading activity logs but won't break functionality. The mode switching logic includes proper guards and retry limits. All other changes (usage tracking, multi-line paste handling, UI enhancements) are safe.
  • Fix the return value in internal/tui/model.go:1145

Important Files Changed

Filename Overview
internal/claude/detector.go Added DetectMode function and modePattern regex to detect current Claude mode from terminal output, expanded thinking patterns for better state detection
internal/tui/model.go Implemented switchToMode function that cycles Shift+Tab to switch modes before sending prompts, added usage overlay tracking
internal/daemon/monitor.go Added Store dependency to persist Claude session IDs across restarts, improving usage tracking continuity
internal/tui/view.go Added usage overlay view showing global usage summary, changed header to display total usage across all sessions instead of selected session only

Sequence Diagram

sequenceDiagram
    participant User
    participant TUI as TUI (model.go)
    participant Detector as Claude Detector
    participant Tmux as Tmux Client
    participant Claude as Claude Code Session
    
    User->>TUI: Press Enter to send prompt
    TUI->>TUI: Check config.UI.DefaultMode
    
    alt DefaultMode is set (e.g., "plan")
        TUI->>Detector: DetectMode(previewCache content)
        Detector-->>TUI: currentMode (e.g., "code")
        
        alt currentMode != targetMode
            loop maxCycles (up to 3 times)
                TUI->>Tmux: SendKeysToPaneRaw("BTab")
                Tmux->>Claude: Send Shift+Tab keystroke
                Claude-->>Claude: Cycle to next mode
                TUI->>TUI: Sleep 50ms
                TUI->>Tmux: CapturePaneDefault()
                Tmux-->>TUI: newContent
                TUI->>Detector: DetectMode(newContent)
                Detector-->>TUI: detectedMode
                
                alt detectedMode == targetMode
                    TUI->>TUI: Sleep 50ms (stabilize)
                    TUI->>TUI: addActivity("Switched to X mode")
                    Note over TUI: Exit loop, proceed to send
                end
            end
        end
    end
    
    TUI->>Tmux: SendKeysToPane(promptText)
    Tmux->>Claude: Send prompt with Enter(s)
    TUI->>TUI: addActivity("Sent: X")
Loading

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

11 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Comment thread internal/tui/model.go Outdated
return true
}
}
return true

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: returns true even when mode switch fails after maxCycles

Suggested change
return true
return false
Prompt To Fix With AI
This is a comment left during a code review.
Path: internal/tui/model.go
Line: 1145:1145

Comment:
**logic:** returns `true` even when mode switch fails after maxCycles

```suggestion
	return false
```

How can I resolve this? If you propose a fix, please make it concise.

Adds default_mode config option to automatically switch Claude Code
to specified mode (plan/code/auto) before sending prompts via ccmanager.
@MartianGreed
MartianGreed force-pushed the feat/auto-switch-mode branch from 15d28d8 to 093ffc2 Compare January 22, 2026 19:46
@MartianGreed
MartianGreed merged commit cf74d8e into main Jan 22, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant