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.

Defined Under Namespace

Classes: Exit

Constant Summary collapse

UNDEFINED =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Object.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.description(description = UNDEFINED) ⇒ String

Set or get a command description.

Parameters:

  • description (String) (defaults to: UNDEFINED)

    The command description.

Returns:

  • (String)


72
73
74
75
# File 'lib/llm/repl/command.rb', line 72

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

.find_by(input: nil, name: nil) ⇒ 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: nil)
  • name (String) (defaults to: nil)

Returns:



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/llm/repl/command.rb', line 29

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

.inherited(command) ⇒ void

This method returns an undefined value.

Parameters:



45
46
47
48
49
# File 'lib/llm/repl/command.rb', line 45

def self.inherited(command)
  LLM.lock(:inherited) do
    registry << command
  end
end

.name(name = UNDEFINED) ⇒ String

Set or get a command name.

Parameters:

  • name (String) (defaults to: UNDEFINED)

    The command name.

Returns:

  • (String)


62
63
64
65
# File 'lib/llm/repl/command.rb', line 62

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

.registryArray<LLM::Repl::Command]

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

Returns:



53
54
55
# File 'lib/llm/repl/command.rb', line 53

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

Instance Method Details

#callObject

This method should be implemented by subclasses.

Raises:

  • (NotImplementedError)


80
81
82
# File 'lib/llm/repl/command.rb', line 80

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