100 lines
3.2 KiB
Go
100 lines
3.2 KiB
Go
package main
|
|
|
|
import "github.com/chzyer/readline"
|
|
|
|
// To store list of agents for tab completion
|
|
var agents []string
|
|
|
|
// Empty when in main context
|
|
var currentAgentContext string
|
|
|
|
// Build tab completer
|
|
func getCompleter() *readline.PrefixCompleter {
|
|
var items []readline.PrefixCompleterInterface
|
|
|
|
if currentAgentContext != "" {
|
|
// Agent context: include context commands, exit, and interact
|
|
for _, cmd := range availableContextCommands {
|
|
if cmd.subcommands != nil {
|
|
var subItems []readline.PrefixCompleterInterface
|
|
for sub, subsub := range cmd.subcommands {
|
|
if sub == "" {
|
|
for _, s := range subsub {
|
|
subItems = append(subItems, readline.PcItem(s))
|
|
}
|
|
} else {
|
|
nestedItems := make([]readline.PrefixCompleterInterface, len(subsub))
|
|
for i, subsubCmd := range subsub {
|
|
nestedItems[i] = readline.PcItem(subsubCmd)
|
|
}
|
|
subItems = append(subItems, readline.PcItem(sub, nestedItems...))
|
|
}
|
|
}
|
|
items = append(items, readline.PcItem(cmd.command, subItems...))
|
|
} else {
|
|
items = append(items, readline.PcItem(cmd.command))
|
|
}
|
|
}
|
|
// Add exit and interact explicitly
|
|
items = append(items, readline.PcItem("exit"))
|
|
var interactSubItems []readline.PrefixCompleterInterface
|
|
for _, agent := range agents {
|
|
interactSubItems = append(interactSubItems, readline.PcItem(agent))
|
|
}
|
|
items = append(items, readline.PcItem("interact", interactSubItems...))
|
|
} else {
|
|
// Main context: include global commands and agent IDs with context commands
|
|
// Add global commands
|
|
for _, cmd := range availableGeneralCommands {
|
|
if cmd.subcommands != nil {
|
|
var subItems []readline.PrefixCompleterInterface
|
|
for sub, subsub := range cmd.subcommands {
|
|
if sub == "" {
|
|
for _, s := range subsub {
|
|
subItems = append(subItems, readline.PcItem(s))
|
|
}
|
|
} else {
|
|
nestedItems := make([]readline.PrefixCompleterInterface, len(subsub))
|
|
for i, subsubCmd := range subsub {
|
|
nestedItems[i] = readline.PcItem(subsubCmd)
|
|
}
|
|
subItems = append(subItems, readline.PcItem(sub, nestedItems...))
|
|
}
|
|
}
|
|
items = append(items, readline.PcItem(cmd.command, subItems...))
|
|
} else {
|
|
items = append(items, readline.PcItem(cmd.command))
|
|
}
|
|
}
|
|
|
|
// Add agent IDs with context commands as subcommands
|
|
for _, agent := range agents {
|
|
var agentSubItems []readline.PrefixCompleterInterface
|
|
for _, cmd := range availableContextCommands {
|
|
if cmd.subcommands != nil {
|
|
var subItems []readline.PrefixCompleterInterface
|
|
for sub, subsub := range cmd.subcommands {
|
|
if sub == "" {
|
|
for _, s := range subsub {
|
|
subItems = append(subItems, readline.PcItem(s))
|
|
}
|
|
} else {
|
|
nestedItems := make([]readline.PrefixCompleterInterface, len(subsub))
|
|
for i, subsubCmd := range subsub {
|
|
nestedItems[i] = readline.PcItem(subsubCmd)
|
|
}
|
|
subItems = append(subItems, readline.PcItem(sub, nestedItems...))
|
|
}
|
|
}
|
|
agentSubItems = append(agentSubItems, readline.PcItem(cmd.command, subItems...))
|
|
} else {
|
|
agentSubItems = append(agentSubItems, readline.PcItem(cmd.command))
|
|
}
|
|
}
|
|
items = append(items, readline.PcItem(agent, agentSubItems...))
|
|
}
|
|
}
|
|
|
|
return readline.NewPrefixCompleter(items...)
|
|
}
|