Class: Nukitori::ResponseParser

Inherits:
Object
  • Object
show all
Defined in:
lib/nukitori/response_parser.rb

Overview

Parses LLM response content (handles both Hash and markdown-wrapped JSON)

Class Method Summary collapse

Class Method Details

.parse(content) ⇒ Hash

Returns Parsed response.

Parameters:

  • content (Hash, String) —

    Response content from LLM

Returns:

  • (Hash) —

    Parsed response



8
9
10
11
12
13
14
15
16
# File 'lib/nukitori/response_parser.rb', line 8

def self.parse(content)
  return content if content.is_a?(Hash)

  text = content.is_a?(String) ? content : content.to_s
  text = text.strip
  text = text.gsub(/\A```json\s*/, '').gsub(/\s*```\z/, '')
  text = text.gsub(/\A```\s*/, '').gsub(/\s*```\z/, '')
  JSON.parse(text)
end