Class: RubyLLM::Toolbox::Tools::YamlQuery

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby_llm/toolbox/tools/yaml_query.rb

Overview

SAFE. Parses YAML (from a file in fs_root or an inline string) and either pretty-prints it (as JSON, for readability) or extracts a value with a dot/bracket path. Uses safe_load — no arbitrary Ruby objects are instantiated from the document.

Path syntax matches json_query: users.name, users[].email, config.port

Constant Summary collapse

MAX_BYTES =
5 * 1024 * 1024
PERMITTED =
[Date, Time, Symbol].freeze

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#call, exec_tool!, exec_tool?, #initialize, #name

Constructor Details

This class inherits a constructor from RubyLLM::Toolbox::Base

Instance Method Details

#execute(path: nil, yaml: nil, query: nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ruby_llm/toolbox/tools/yaml_query.rb', line 37

def execute(path: nil, yaml: nil, query: nil)
  source = load_source(path, yaml)
  return source if source.is_a?(Hash) # error

  data = YAML.safe_load(source, permitted_classes: PERMITTED, aliases: true)
  result = query.to_s.strip.empty? ? data : DataPath.query(data, query)
  truncate(JSON.pretty_generate(result))
rescue Safety::PathJail::Jailbreak => e
  error(e.message, code: :path_denied)
rescue Psych::DisallowedClass => e
  error("YAML contains a disallowed type: #{e.message}", code: :unsafe_yaml)
rescue Psych::SyntaxError => e
  error("invalid YAML: #{e.message}", code: :bad_yaml)
rescue DataPath::Error => e
  error(e.message, code: :bad_query)
end