Class: LLM::Repl::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/repl/command.rb

Overview

The LLM::Repl::Command class is the superclass of all read-eval-print loop commands. A command has a name, and a description. This basic version does not implement parameters. A command is accessible via the / prefix: eg /exit.

Direct Known Subclasses

Help

Defined Under Namespace

Classes: Exit, Parameter, Quit

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repl) ⇒ LLM::Repl::Command

Parameters:



148
149
150
# File 'lib/llm/repl/command.rb', line 148

def initialize(repl)
  @repl = repl
end

Class Method Details

.description(description = UNDEFINED) ⇒ String

Set or get a command description.

Parameters:

  • description (String) (defaults to: UNDEFINED)

    The command description.

Returns:

  • (String)


106
107
108
109
# File 'lib/llm/repl/command.rb', line 106

def self.description(description = UNDEFINED)
  return @description if description == UNDEFINED
  @description = description
end

.find_by(input: UNDEFINED, name: UNDEFINED) ⇒ LLM::Repl::Command?

Note:

The input string must be prefixed with "/" or it won't be matched. The match is made against the string before the first space - so "/exit foo" will match the "exit" command but "/exitnow" will not.

Find a command by a name, or by an input string.

Examples:

find by name

LLM::Repl::Command.find_by(name: "exit")

find by input string

LLM::Repl::Command.find_by(input: "/exit")

Parameters:

  • input (String) (defaults to: UNDEFINED)
  • name (String) (defaults to: UNDEFINED)

Returns:



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/llm/repl/command.rb', line 61

def self.find_by(input: UNDEFINED, name: UNDEFINED)
  if input != UNDEFINED
    return nil unless input[0] == "/"
    n, = input.split(" ")
    registry.find { n[1..] == _1.name }
  elsif name != UNDEFINED
    registry.find { name == _1.name }
  else
    raise ArgumentError, "provide either an input or a name"
  end
end

.helpvoid

This method returns an undefined value.

Display a formatted help message for this command. Uses a single #write call to output the command name, description, and parameter details.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/llm/repl/command.rb', line 166

def self.help
  lines = []
  lines << "Command: #{name}"
  lines << "Description: #{description}"
  unless parameters.empty?
    lines << ""
    lines << "Parameters:"
    parameters.each_value do |param|
      tag = param.required? ? "(required)" : "(optional)"
      lines << "  #{param.name} [#{param.type}] - #{param.description} #{tag}"
    end
  end
  lines.join("\n")
end

.inherited(command) ⇒ void

This method returns an undefined value.

Parameters:



77
78
79
80
81
82
83
# File 'lib/llm/repl/command.rb', line 77

def self.inherited(command)
  LLM.lock(:inherited) do
    registry << command
    command.instance_variable_set(:@parameters, {})
    command.define_singleton_method(:inherited) { |command| SINGLETON.inherited(command) }
  end
end

.name(name = UNDEFINED) ⇒ String

Set or get a command name.

Parameters:

  • name (String) (defaults to: UNDEFINED)

    The command name.

Returns:

  • (String)


96
97
98
99
# File 'lib/llm/repl/command.rb', line 96

def self.name(name = UNDEFINED)
  return @name if name == UNDEFINED
  @name = name
end

.parameter(name, type, description, options = {}) ⇒ void

This method returns an undefined value.

Parameters:

  • name (Symbol)
  • type (Class)
  • description (String)
  • options (Hash) (defaults to: {})


117
118
119
120
121
122
123
# File 'lib/llm/repl/command.rb', line 117

def self.parameter(name, type, description, options = {})
  @parameters[name] = Parameter.new(
    name, type,
    description, options,
    @parameters.size, nil
  )
end

.parametersHash

Returns:

  • (Hash)


127
128
129
# File 'lib/llm/repl/command.rb', line 127

def self.parameters
  @parameters
end

.registryArray<LLM::Repl::Command]

Returns Array<LLM::Repl::Command].

Returns:



87
88
89
# File 'lib/llm/repl/command.rb', line 87

def self.registry
  @registry ||= []
end

.required(names) ⇒ void

This method returns an undefined value.

Parameters:

  • names (Array<Symbol>)

    One or more required names



135
136
137
138
139
140
141
142
143
# File 'lib/llm/repl/command.rb', line 135

def self.required(names)
  names.each do |name|
    if @parameters.key?(name)
      @parameters[name].required!
    else
      raise LLM::Error, "'#{name}' is not a known parameter"
    end
  end
end

Instance Method Details

#callObject

This method should be implemented by subclasses.

Raises:

  • (NotImplementedError)


184
185
186
# File 'lib/llm/repl/command.rb', line 184

def call(...)
  raise NotImplementedError, "#{self.class}#call is not implemented"
end

#parametersHash<Symbol, Parameter>

Returns:



190
191
192
# File 'lib/llm/repl/command.rb', line 190

def parameters
  self.class.parameters
end

#write(str, who: "command(#{self.class.name}): ") ⇒ void

This method returns an undefined value.

Write a string to the transcript

Parameters:

  • str (String)


156
157
158
159
# File 'lib/llm/repl/command.rb', line 156

def write(str, who: "command(#{self.class.name}): ")
  @repl.write(who, Curses::A_BOLD)
  @repl.write(str)
end