Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nextchanges/cli/aitools-install-esc-cancels.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* `databricks aitools install` now cancels the agent picker when you press Esc, as Ctrl+C does, and exits with the same "user aborted" error. Its help line now says how to cancel. ([#7721](https://github.com/databricks/cli/pull/7721))
79 changes: 72 additions & 7 deletions cmd/aitools/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"runtime"
"strings"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh"
"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/aitools/agents"
Expand Down Expand Up @@ -518,13 +519,9 @@ func defaultPromptAgentSelection(_ context.Context, scope string, choices []agen
}

var selected []string
err := huh.NewMultiSelect[string]().
Title(agentPickerTitle(scope)).
Description("space to toggle, enter to install").
Options(options...).
Value(&selected).
Run()
if err != nil {
field := agentPickerField(scope, options, &selected)
form := huh.NewForm(huh.NewGroup(field)).WithShowHelp(false)
if err := runPicker(form, field.GetFiltering); err != nil {
return nil, promptError(err)
}
if len(selected) == 0 {
Expand All @@ -538,6 +535,74 @@ func defaultPromptAgentSelection(_ context.Context, scope string, choices []agen
return result, nil
}

// agentPickerHelp is the help line of the agent picker. Enter installs the
// selected agents: install runs the plan straight after the picker.
const agentPickerHelp = "space to toggle, enter to install, esc to cancel"

func agentPickerField(scope string, options []huh.Option[string], selected *[]string) *huh.MultiSelect[string] {
return huh.NewMultiSelect[string]().
Title(agentPickerTitle(scope)).
Description(agentPickerHelp).
Options(options...).
Value(selected)
}

// pickerModel runs a form as huh's Run does, except that Esc cancels it too.
type pickerModel struct {
form *huh.Form
// searching reports whether a multi-select in the form is searching. Esc
// then belongs to the form, which leaves the search.
searching func() bool
cancelled bool
}

func (p *pickerModel) Init() tea.Cmd { return p.form.Init() }

func (p *pickerModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if key, ok := msg.(tea.KeyMsg); ok && key.Type == tea.KeyEsc && !p.searching() {
p.cancelled = true
return p, tea.Quit
}
next, cmd := p.form.Update(msg)
p.form = next.(*huh.Form)
return p, cmd
}

// View draws nothing once Esc cancelled the picker, so Esc leaves no picker on screen. Ctrl+C interrupts the program,
// which keeps the last frame, as huh's Run does.
func (p *pickerModel) View() string {
if p.cancelled {
return ""
}
return p.form.View()
}

// newPicker returns the program of form. Run sets the commands that submit and
// cancel a form, and newPicker sets them too, since it runs the form without Run.
func newPicker(form *huh.Form, searching func() bool) *pickerModel {
form.SubmitCmd = tea.Quit
form.CancelCmd = tea.Interrupt
return &pickerModel{form: form, searching: searching}
}

// runPicker runs form and returns huh.ErrUserAborted when the user cancels it,
// with Esc or Ctrl+C. It takes the program options huh's Run sets: the picker
// draws on stderr, so a redirected stdout gets none of it, and reports focus.
func runPicker(form *huh.Form, searching func() bool) error {
p := newPicker(form, searching)
_, err := tea.NewProgram(p, tea.WithOutput(os.Stderr), tea.WithReportFocus()).Run()
return pickerError(p.cancelled, err)
}

// pickerError is the result of a picker's program: a cancel, by Esc or Ctrl+C,
// is huh.ErrUserAborted, as huh's Run returns it.
func pickerError(cancelled bool, err error) error {
if cancelled || errors.Is(err, tea.ErrInterrupted) {
return huh.ErrUserAborted
}
return err
}

// buildPlan resolves the per-agent delivery and scope. Plugin-first: an agent
// with a headless plugin install gets the plugin; --skills-only forces skills
// everywhere; agents with no plugin always get skills.
Expand Down
67 changes: 67 additions & 0 deletions cmd/aitools/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"runtime"
"testing"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh"
"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/aitools/agents"
Expand Down Expand Up @@ -1820,3 +1821,69 @@ func TestNoAgentsMessageMatchesTheDetection(t *testing.T) {
printNoAgentsMessage(ctx, installInvocation{scope: installer.ScopeGlobal})
assert.Contains(t, stderr.String(), "No supported coding agents found on PATH.")
}

// Esc cancels the agent picker as Ctrl+C does, and both end the run with
// huh.ErrUserAborted.
func TestAgentPickerEscCancelsLikeCtrlC(t *testing.T) {
options := []huh.Option[string]{huh.NewOption("Cursor", "cursor")}

var selected []string
field := agentPickerField(installer.ScopeGlobal, options, &selected)
p := newPicker(huh.NewForm(huh.NewGroup(field)).WithShowHelp(false), field.GetFiltering)
p.Init()
p.Update(tea.WindowSizeMsg{Width: 80, Height: 24})
assert.Contains(t, p.View(), agentPickerHelp)

_, cmd := p.Update(tea.KeyMsg{Type: tea.KeyEsc})
assert.True(t, p.cancelled)
assert.Equal(t, tea.QuitMsg{}, cmd())
assert.Empty(t, p.View(), "a cancelled picker leaves nothing on screen")
assert.ErrorIs(t, pickerError(p.cancelled, nil), huh.ErrUserAborted)

field = agentPickerField(installer.ScopeGlobal, options, &selected)
p = newPicker(huh.NewForm(huh.NewGroup(field)).WithShowHelp(false), field.GetFiltering)
p.Init()
_, cmd = p.Update(tea.KeyMsg{Type: tea.KeyCtrlC})
assert.Equal(t, tea.InterruptMsg{}, cmd())
assert.ErrorIs(t, pickerError(false, tea.ErrInterrupted), huh.ErrUserAborted)
}

// While the agent picker searches, the first Esc only leaves the search.
func TestAgentPickerEscLeavesSearchBeforeCancelling(t *testing.T) {
options := []huh.Option[string]{huh.NewOption("Cursor", "cursor"), huh.NewOption("Claude Code", "claude")}

var selected []string
field := agentPickerField(installer.ScopeGlobal, options, &selected)
p := newPicker(huh.NewForm(huh.NewGroup(field)).WithShowHelp(false), field.GetFiltering)
p.Init()
p.Update(tea.WindowSizeMsg{Width: 80, Height: 24})
p.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("/")})
require.True(t, field.GetFiltering())

p.Update(tea.KeyMsg{Type: tea.KeyEsc})
assert.False(t, p.cancelled, "the first Esc leaves the search")
assert.False(t, field.GetFiltering())

p.Update(tea.KeyMsg{Type: tea.KeyEsc})
assert.True(t, p.cancelled, "Esc outside the search cancels the picker")
}

func TestPickerError(t *testing.T) {
boom := errors.New("boom")
tests := []struct {
name string
cancelled bool
err error
want error
}{
{"completed", false, nil, nil},
{"esc", true, nil, huh.ErrUserAborted},
{"ctrl c", false, tea.ErrInterrupted, huh.ErrUserAborted},
{"program error", false, boom, boom},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.want, pickerError(tc.cancelled, tc.err))
})
}
}
Loading