Class: SwarmCLI::V3::CommandCompleter

Inherits:
Object
  • Object
show all
Defined in:
lib/swarm_cli/v3/command_completer.rb

Overview

Command completion logic for slash commands.

CommandCompleter provides methods to extract command targets from text buffers and find matching slash commands. It has no knowledge of the UI or display.

Examples:

Extracting command word

buffer = "/mem"
cursor = 4  # After "mem"
result = CommandCompleter.extract_command_word(buffer, cursor)
# => ["", "/mem", ""]

Finding matches

matches = CommandCompleter.find_matches("/mem")
# => ["/memory"]

Constant Summary collapse

COMMANDS =

Available slash commands

[
  "/help",
  "/clear",
  "/memory",
  "/defrag",
  "/queue",
  "/refine",
  "/reboot",
  "/exit",
  "/quit",
].freeze

Class Method Summary collapse

Class Method Details

.extract_command_word(buffer, cursor) ⇒ Array<String>?

Extract the command at cursor position.

Searches backward from cursor to find a ‘/` at the start of the line, then extracts from that `/` to the cursor position.

Examples:

Cursor at end of command

extract_command_word("/mem", 4)
# => ["", "/mem", ""]

Cursor in middle of command

extract_command_word("/memory", 4)
# => ["", "/mem", "ory"]

No / at start of line

extract_command_word("check /mem", 10)
# => nil (slash not at start)

Parameters:

  • buffer (String)

    the full text buffer

  • cursor (Integer)

    the cursor position (0-based index)

Returns:

  • (Array<String>, nil)
    pre, target, post

    or nil if no / found

    • pre: text before the /

    • target: the / and characters up to cursor

    • post: text after cursor



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/swarm_cli/v3/command_completer.rb', line 58

def extract_command_word(buffer, cursor)
  # Command must be at start of line
  return unless buffer.start_with?("/")

  # Find end of command after cursor (next space or end of buffer)
  target_end = cursor
  target_end += 1 while target_end < buffer.length && buffer[target_end] !~ /\s/

  pre = ""
  target = buffer[0...cursor]
  post = buffer[cursor...target_end] || ""

  [pre, target, post]
end

.find_matches(target, max: 5, skills: []) ⇒ Array<String>

Find matching commands for a query (with / prefix).

Examples:

Exact match

find_matches("/clear")
# => ["/clear"]

Prefix match

find_matches("/me")
# => ["/memory"]

With skills

find_matches("/an", skills: ["analyze", "annotate"])
# => ["/analyze", "/annotate"]

Multiple matches

find_matches("/")
# => ["/clear", "/memory", "/defrag", "/queue", "/reboot"]

Parameters:

  • target (String)

    the search target (must start with /)

  • max (Integer) (defaults to: 5)

    maximum number of results to return

  • skills (Array<String>) (defaults to: [])

    additional skill names to include

Returns:

  • (Array<String>)

    array of matching commands (with / prefix)



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/swarm_cli/v3/command_completer.rb', line 96

def find_matches(target, max: 5, skills: [])
  return [] unless target.start_with?("/")

  query = target.downcase

  # Combine built-in commands with skill names
  skill_commands = skills.map { |name| "/#{name}" }
  all_commands = COMMANDS + skill_commands

  # Find commands that start with the query
  matches = all_commands.select { |cmd| cmd.downcase.start_with?(query) }

  # Sort by length (shorter = more specific)
  matches.sort_by(&:length).first(max)
end