cli: dedupe agent display-name and hook-agent-option building in setup · Entire

cli: dedupe agent display-name and hook-agent-option building in setup

b12bc0f·

Soph·2w ago·2 files·+38 added/-61 removed

The name→display-type mapping loop appeared three times (setup twice, InstalledAgentDisplayNames once) — extract agentDisplayNames. The hook-supporting/non-test-only agent selector options were built twice with only the preselection set differing — extract hookAgentOptions. Covered by the existing setup/manage-agents tests.

Co-Authored-By: Claude Fable 5 noreply@anthropic.com

Sessions

76f119a87828View transcript

Changes

2

98 unmodified lines

// InstalledAgentDisplayNames returns user-facing display names for agents with hooks installed.
func InstalledAgentDisplayNames(ctx context.Context) []string {
    installedNames := GetAgentsWithHooksInstalled(ctx)
    displayNames := make([]string, 0, len(installedNames))
    for _, name := range installedNames {
        return agentDisplayNames(GetAgentsWithHooksInstalled(ctx))
    }
}

// agentDisplayNames maps agent names to their user-facing display names,
// skipping names that aren't registered.
func agentDisplayNames(names []types.AgentName) []string {
    displayNames := make([]string, 0, len(names))
    for _, name := range names {
        if ag, err := agent.Get(name); err == nil {
            displayNames = append(displayNames, string(ag.Type()))
        }
    }
}

Mcmd/entire/cli/config.go+8/-3

412 unmodified lines

// hookAgentOptions builds selector options for every registered agent that
// supports hooks and isn't test-only (e.g. the Vogon canary), preselecting
// the names in selected.
func hookAgentOptions(selected map[types.AgentName]struct{}) []huh.Option[string] {
    agentNames := agent.List()
    options := make([]huh.Option[string], 0, len(agentNames))
    for _, name := range agentNames {
        ag, err := agent.Get(name)
        if err != nil {
            continue
        }
        if _, ok := agent.AsHookSupport(ag); !ok {
            continue
        }
        if to, ok := ag.(agent.TestOnly); ok && to.IsTestOnly() {
            continue
        }
        opt := huh.NewOption(string(ag.Type()), string(name))
        if _, ok := selected[name]; ok {
            opt = opt.Selected(true)
        }
        options = append(options, opt)
    }
    return options
}

// runManageAgents shows which agents are currently enabled and lets the user
// add or remove agents. Deselecting an installed agent removes its hooks.
func runManageAgents(ctx context.Context, w io.Writer, opts EnableOptions, selectFn func(available []string) ([]string, error)) error {
    // Show currently installed agents
    if len(installedNames) > 0 {
        displayNames := make([]string, 0, len(installedNames))
        for _, name := range installedNames {
            if ag, err := agent.Get(name); err == nil {
                displayNames = append(displayNames, string(ag.Type()))
            }
        }
        fmt.Fprintf(w, "Enabled agents: %s\n\n", strings.Join(displayNames, ", "))
        fmt.Fprintf(w, "Enabled agents: %s\n\n", strings.Join(agentDisplayNames(installedNames), ", "))
    }
    // Build pre-selection set from installed agents
    // during setup the setting doesn't exist yet.
    external.DiscoverAndRegisterAlways(ctx)
}

This cleaned content retains the main details regarding the CLI agent management functionality and relevant code snippets.