4 "charm.land/bubbles/v2/help"
5 "charm.land/bubbles/v2/key"
6 tea "charm.land/bubbletea/v2"
7 "charm.land/lipgloss/v2"
8 "golang.org/x/text/cases"
9 "golang.org/x/text/language"
11 "github.com/charmbracelet/crush/internal/config"
12 "github.com/charmbracelet/crush/internal/tui/components/core"
13 "github.com/charmbracelet/crush/internal/tui/components/dialogs"
14 "github.com/charmbracelet/crush/internal/tui/exp/list"
15 "github.com/charmbracelet/crush/internal/tui/styles"
16 "github.com/charmbracelet/crush/internal/tui/util"
20 ReasoningDialogID dialogs.DialogID = "reasoning"
25type listModel = list.FilterableList[list.CompletionItem[EffortOption]]
27type EffortOption struct {
32type ReasoningDialog interface {
36type reasoningDialogCmp struct {
38 wWidth int // Width of the terminal window
39 wHeight int // Height of the terminal window
42 keyMap ReasoningDialogKeyMap
46type ReasoningEffortSelectedMsg struct {
50type ReasoningDialogKeyMap struct {
57func DefaultReasoningDialogKeyMap() ReasoningDialogKeyMap {
58 return ReasoningDialogKeyMap{
60 key.WithKeys("down", "j", "ctrl+n"),
61 key.WithHelp("↓/j/ctrl+n", "next"),
63 Previous: key.NewBinding(
64 key.WithKeys("up", "k", "ctrl+p"),
65 key.WithHelp("↑/k/ctrl+p", "previous"),
67 Select: key.NewBinding(
68 key.WithKeys("enter"),
69 key.WithHelp("enter", "select"),
71 Close: key.NewBinding(
72 key.WithKeys("esc", "ctrl+c"),
73 key.WithHelp("esc/ctrl+c", "close"),
78func (k ReasoningDialogKeyMap) ShortHelp() []key.Binding {
79 return []key.Binding{k.Select, k.Close}
82func (k ReasoningDialogKeyMap) FullHelp() [][]key.Binding {
83 return [][]key.Binding{
89func NewReasoningDialog() ReasoningDialog {
90 keyMap := DefaultReasoningDialogKeyMap()
91 listKeyMap := list.DefaultKeyMap()
92 listKeyMap.Down.SetEnabled(false)
93 listKeyMap.Up.SetEnabled(false)
94 listKeyMap.DownOneItem = keyMap.Next
95 listKeyMap.UpOneItem = keyMap.Previous
97 t := styles.CurrentTheme()
98 inputStyle := t.S().Base.PaddingLeft(1).PaddingBottom(1)
99 effortList := list.NewFilterableList(
100 []list.CompletionItem[EffortOption]{},
101 list.WithFilterInputStyle(inputStyle),
102 list.WithFilterListOptions(
103 list.WithKeyMap(listKeyMap),
104 list.WithWrapNavigation(),
105 list.WithResizeByList(),
109 help.Styles = t.S().Help
111 return &reasoningDialogCmp{
112 effortList: effortList,
119func (r *reasoningDialogCmp) Init() tea.Cmd {
120 return r.populateEffortOptions()
123func (r *reasoningDialogCmp) populateEffortOptions() tea.Cmd {
125 if agentCfg, ok := cfg.Agents[config.AgentCoder]; ok {
126 selectedModel := cfg.Models[agentCfg.Model]
127 model := cfg.GetModelByType(agentCfg.Model)
129 // Get current reasoning effort
130 currentEffort := selectedModel.ReasoningEffort
131 if currentEffort == "" && model != nil {
132 currentEffort = model.DefaultReasoningEffort
135 efforts := []EffortOption{}
136 caser := cases.Title(language.Und)
137 for _, level := range model.ReasoningLevels {
138 efforts = append(efforts, EffortOption{
139 Title: caser.String(level),
144 effortItems := []list.CompletionItem[EffortOption]{}
146 for _, effort := range efforts {
147 opts := []list.CompletionItemOption{
148 list.WithCompletionID(effort.Effort),
150 if effort.Effort == currentEffort {
151 opts = append(opts, list.WithCompletionShortcut("current"))
152 selectedID = effort.Effort
154 effortItems = append(effortItems, list.NewCompletionItem(
161 cmd := r.effortList.SetItems(effortItems)
162 // Set the current effort as the selected item
163 if currentEffort != "" && selectedID != "" {
164 return tea.Sequence(cmd, r.effortList.SetSelected(selectedID))
171func (r *reasoningDialogCmp) Update(msg tea.Msg) (util.Model, tea.Cmd) {
172 switch msg := msg.(type) {
173 case tea.WindowSizeMsg:
175 r.wHeight = msg.Height
176 return r, r.effortList.SetSize(r.listWidth(), r.listHeight())
177 case tea.KeyPressMsg:
179 case key.Matches(msg, r.keyMap.Select):
180 selectedItem := r.effortList.SelectedItem()
181 if selectedItem == nil {
182 return r, nil // No item selected, do nothing
184 effort := (*selectedItem).Value()
185 return r, tea.Sequence(
186 util.CmdHandler(dialogs.CloseDialogMsg{}),
188 return ReasoningEffortSelectedMsg{
189 Effort: effort.Effort,
193 case key.Matches(msg, r.keyMap.Close):
194 return r, util.CmdHandler(dialogs.CloseDialogMsg{})
196 u, cmd := r.effortList.Update(msg)
197 r.effortList = u.(listModel)
204func (r *reasoningDialogCmp) View() string {
205 t := styles.CurrentTheme()
206 listView := r.effortList
208 header := t.S().Base.Padding(0, 1, 1, 1).Render(core.Title("Select Reasoning Effort", r.width-4))
209 content := lipgloss.JoinVertical(
214 t.S().Base.Width(r.width-2).PaddingLeft(1).AlignHorizontal(lipgloss.Left).Render(r.help.View(r.keyMap)),
216 return r.style().Render(content)
219func (r *reasoningDialogCmp) Cursor() *tea.Cursor {
220 if cursor, ok := r.effortList.(util.Cursor); ok {
221 cursor := cursor.Cursor()
223 cursor = r.moveCursor(cursor)
230func (r *reasoningDialogCmp) listWidth() int {
231 return r.width - 2 // 4 for padding
234func (r *reasoningDialogCmp) listHeight() int {
235 listHeight := len(r.effortList.Items()) + 2 + 4 // height based on items + 2 for the input + 4 for the sections
236 return min(listHeight, r.wHeight/2)
239func (r *reasoningDialogCmp) moveCursor(cursor *tea.Cursor) *tea.Cursor {
240 row, col := r.Position()
243 cursor.X = cursor.X + col + 2
247func (r *reasoningDialogCmp) style() lipgloss.Style {
248 t := styles.CurrentTheme()
251 Border(lipgloss.RoundedBorder()).
252 BorderForeground(t.BorderFocus)
255func (r *reasoningDialogCmp) Position() (int, int) {
256 row := r.wHeight/4 - 2 // just a bit above the center
262func (r *reasoningDialogCmp) ID() dialogs.DialogID {
263 return ReasoningDialogID
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list