Class: LittleGhost::CodeMode::Catalog

Inherits:
Object
  • Object
show all
Defined in:
lib/little_ghost/code_mode/catalog.rb

Overview

Normalizes a trusted Tool catalog for a guest language and rejects names that would collide or shadow code-mode controls.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(specifications, normalize:, reserved: %w[exec wait stop])) ⇒ Catalog

Normalizes trusted Tool specifications with normalize. Names in reserved and names that collide after normalization are rejected.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/little_ghost/code_mode/catalog.rb', line 13

def initialize(specifications, normalize:, reserved: %w[exec wait stop])
  aliases = {}
  @definitions = Array(specifications).map do |specification|
    canonical_name = value(specification, :name).to_s
    name = normalize.call(canonical_name).to_s
    if reserved.include?(name)
      raise ConfigurationError, "Code-mode tool name #{canonical_name.inspect} conflicts with reserved tool #{name.inspect}"
    end
    if aliases.key?(name)
      raise ConfigurationError,
        "Code-mode tool names #{aliases.fetch(name).inspect} and #{canonical_name.inspect} both normalize to #{name.inspect}"
    end

    aliases[name] = canonical_name
    {
      "name" => name.freeze,
      "canonical_name" => canonical_name.freeze,
      "description" => value(specification, :description, "").to_s.freeze,
      "input_schema" => value(specification, :input_schema, {}).freeze,
      "specification" => specification
    }.freeze
  end.freeze
  @by_name = @definitions.to_h { |definition| [definition.fetch("name"), definition] }.freeze
end

Instance Attribute Details

#definitionsObject (readonly)

Frozen normalized Tool definitions in declaration order.



9
10
11
# File 'lib/little_ghost/code_mode/catalog.rb', line 9

def definitions
  @definitions
end

Instance Method Details

#fetch(name) ⇒ Object

Returns the normalized definition for name.



39
40
# File 'lib/little_ghost/code_mode/catalog.rb', line 39

def fetch(name) = @by_name.fetch(name.to_s)
# Indicates whether +name+ is present in the catalog.

#key?(name) ⇒ Boolean

Indicates whether name is present in the catalog.

Returns:

  • (Boolean)


41
# File 'lib/little_ghost/code_mode/catalog.rb', line 41

def key?(name) = @by_name.key?(name.to_s)