Module: OllamaAgent::ToolRuntime::JsonExtractor

Defined in:
lib/ollama_agent/tool_runtime/json_extractor.rb

Overview

Pulls the first top-level JSON object from text using brace matching (strings respected).

Class Method Summary collapse

Class Method Details

.extract_object(text) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ollama_agent/tool_runtime/json_extractor.rb', line 10

def extract_object(text)
  raise ArgumentError, "text must be a String" unless text.is_a?(String)

  start_idx = text.index("{")
  raise JsonParseError, "no JSON object found" if start_idx.nil?

  slice = extract_balanced_object(text, start_idx)
  JSON.parse(slice)
rescue JSON::ParserError => e
  raise JsonParseError, "invalid JSON: #{e.message}"
end