Class: RailsAiBridge::Registry::ContextToolSpec

Inherits:
Data
  • Object
show all
Defined in:
lib/rails_ai_bridge/registry/context_tool_spec.rb

Overview

Immutable value object describing a single tool requested from a context provider.

Mirrors the untagged ContextToolSpec enum in the Rust runtime: a spec is either a plain tool name (simple) or a mapping that routes the tool output into a named context field, optionally with arguments (mapped).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argumentsHash? (readonly)

Returns optional arguments passed when executing the tool.

Returns:

  • (Hash, nil)

    optional arguments passed when executing the tool



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rails_ai_bridge/registry/context_tool_spec.rb', line 17

ContextToolSpec = Data.define(:name, :field, :arguments) do
  # Builds a {ContextToolSpec} from a parsed JSON value.
  #
  # @param value [String, Hash] plain tool name or mapped tool object
  # @return [ContextToolSpec]
  # @raise [ArgumentError] when the value is neither a String nor a Hash,
  #   or a mapped tool is missing a required field
  def self.from_json(value)
    return new(name: value, field: nil, arguments: nil) if value.is_a?(String)
    return from_mapped_json(value) if value.is_a?(Hash)

    raise ArgumentError, "Context tool spec must be a String or an object, got #{value.class.name}"
  end

  # @return [Boolean] true when the spec is a plain tool name
  # :reek:NilCheck -- value-object predicate over an optional attribute
  def simple? = field.nil?

  # @return [Boolean] true when the spec maps tool output into a context field
  # :reek:NilCheck -- value-object predicate over an optional attribute
  def mapped? = !field.nil?

  # @api private
  def self.from_mapped_json(hash)
    new(
      name: hash.fetch('name'),
      field: hash.fetch('field'),
      arguments: hash['arguments']
    )
  rescue KeyError => error
    raise ArgumentError, "Context tool spec missing required field: #{error.key}"
  end

  private_class_method :from_mapped_json
end

#fieldString? (readonly)

Returns target context field for mapped tools; nil for simple tools.

Returns:

  • (String, nil)

    target context field for mapped tools; nil for simple tools



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rails_ai_bridge/registry/context_tool_spec.rb', line 17

ContextToolSpec = Data.define(:name, :field, :arguments) do
  # Builds a {ContextToolSpec} from a parsed JSON value.
  #
  # @param value [String, Hash] plain tool name or mapped tool object
  # @return [ContextToolSpec]
  # @raise [ArgumentError] when the value is neither a String nor a Hash,
  #   or a mapped tool is missing a required field
  def self.from_json(value)
    return new(name: value, field: nil, arguments: nil) if value.is_a?(String)
    return from_mapped_json(value) if value.is_a?(Hash)

    raise ArgumentError, "Context tool spec must be a String or an object, got #{value.class.name}"
  end

  # @return [Boolean] true when the spec is a plain tool name
  # :reek:NilCheck -- value-object predicate over an optional attribute
  def simple? = field.nil?

  # @return [Boolean] true when the spec maps tool output into a context field
  # :reek:NilCheck -- value-object predicate over an optional attribute
  def mapped? = !field.nil?

  # @api private
  def self.from_mapped_json(hash)
    new(
      name: hash.fetch('name'),
      field: hash.fetch('field'),
      arguments: hash['arguments']
    )
  rescue KeyError => error
    raise ArgumentError, "Context tool spec missing required field: #{error.key}"
  end

  private_class_method :from_mapped_json
end

#nameString (readonly)

Returns name of the remote tool to execute.

Returns:

  • (String)

    name of the remote tool to execute



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rails_ai_bridge/registry/context_tool_spec.rb', line 17

ContextToolSpec = Data.define(:name, :field, :arguments) do
  # Builds a {ContextToolSpec} from a parsed JSON value.
  #
  # @param value [String, Hash] plain tool name or mapped tool object
  # @return [ContextToolSpec]
  # @raise [ArgumentError] when the value is neither a String nor a Hash,
  #   or a mapped tool is missing a required field
  def self.from_json(value)
    return new(name: value, field: nil, arguments: nil) if value.is_a?(String)
    return from_mapped_json(value) if value.is_a?(Hash)

    raise ArgumentError, "Context tool spec must be a String or an object, got #{value.class.name}"
  end

  # @return [Boolean] true when the spec is a plain tool name
  # :reek:NilCheck -- value-object predicate over an optional attribute
  def simple? = field.nil?

  # @return [Boolean] true when the spec maps tool output into a context field
  # :reek:NilCheck -- value-object predicate over an optional attribute
  def mapped? = !field.nil?

  # @api private
  def self.from_mapped_json(hash)
    new(
      name: hash.fetch('name'),
      field: hash.fetch('field'),
      arguments: hash['arguments']
    )
  rescue KeyError => error
    raise ArgumentError, "Context tool spec missing required field: #{error.key}"
  end

  private_class_method :from_mapped_json
end

Class Method Details

.from_json(value) ⇒ ContextToolSpec

Builds a RailsAiBridge::Registry::ContextToolSpec from a parsed JSON value.

Parameters:

  • value (String, Hash)

    plain tool name or mapped tool object

Returns:

Raises:

  • (ArgumentError)

    when the value is neither a String nor a Hash, or a mapped tool is missing a required field



24
25
26
27
28
29
# File 'lib/rails_ai_bridge/registry/context_tool_spec.rb', line 24

def self.from_json(value)
  return new(name: value, field: nil, arguments: nil) if value.is_a?(String)
  return from_mapped_json(value) if value.is_a?(Hash)

  raise ArgumentError, "Context tool spec must be a String or an object, got #{value.class.name}"
end

Instance Method Details

#mapped?Boolean

:reek:NilCheck -- value-object predicate over an optional attribute

Returns:

  • (Boolean)

    true when the spec maps tool output into a context field



37
# File 'lib/rails_ai_bridge/registry/context_tool_spec.rb', line 37

def mapped? = !field.nil?

#simple?Boolean

:reek:NilCheck -- value-object predicate over an optional attribute

Returns:

  • (Boolean)

    true when the spec is a plain tool name



33
# File 'lib/rails_ai_bridge/registry/context_tool_spec.rb', line 33

def simple? = field.nil?