Class: RubyLLM::Toolbox::Tools::JsonQuery

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

Overview

SAFE. Parses JSON (from a file in fs_root or an inline string) and either pretty-prints it or extracts a value with a dot/bracket path. In-process, read-only.

Path syntax: dot-separated keys, [n] for array indices, and [] to map a field across an array. Examples:

users[0].name        users[].email        config.server.port

Constant Summary collapse

MAX_BYTES =
5 * 1024 * 1024

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, json: nil, query: nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ruby_llm/toolbox/tools/json_query.rb', line 35

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

  data = JSON.parse(source)
  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 JSON::ParserError => e
  error("invalid JSON: #{e.message}", code: :bad_json)
rescue DataPath::Error => e
  error(e.message, code: :bad_query)
end