Class: SkillBench::Clients::ResponseParser

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

Overview

Parses LLM provider responses and extracts messages and usage data. Handles JSON parsing, message extraction, and validation.

Class Method Summary collapse

Class Method Details

.extract_content(message) ⇒ String?

Extracts the content from a message.

Parameters:

  • message (Hash, String)

    The message

Returns:

  • (String, nil)

    The content or nil



57
58
59
60
61
# File 'lib/skill_bench/clients/response_parser.rb', line 57

def self.extract_content(message)
  return message unless message.is_a?(Hash)

  message[:content] || message['content']
end

.extract_openai_message(body) ⇒ Hash?

Extracts the message from an OpenAI-compatible response body.

Parameters:

  • body (Hash)

    The parsed response body

Returns:

  • (Hash, nil)

    The message or nil



77
78
79
80
81
82
# File 'lib/skill_bench/clients/response_parser.rb', line 77

def self.extract_openai_message(body)
  choices = body[:choices] || body['choices']
  return nil unless choices&.any?

  choices.first[:message] || choices.first['message']
end

.extract_openai_usage(body) ⇒ Hash

Extracts usage data from an OpenAI-compatible response.

Parameters:

  • body (Hash)

    The parsed response body

Returns:

  • (Hash)

    Usage data



88
89
90
# File 'lib/skill_bench/clients/response_parser.rb', line 88

def self.extract_openai_usage(body)
  body[:usage] || body['usage'] || {}
end

.extract_tool_calls(message) ⇒ Array?

Extracts tool calls from a message.

Parameters:

  • message (Hash)

    The message

Returns:

  • (Array, nil)

    The tool calls or nil



67
68
69
70
71
# File 'lib/skill_bench/clients/response_parser.rb', line 67

def self.extract_tool_calls(message)
  return nil unless message.is_a?(Hash)

  message[:tool_calls] || message['tool_calls']
end

.parse_body(response) ⇒ Hash

Parses the response body into a Hash.

Parameters:

  • response (Faraday::Response)

    The HTTP response

Returns:

  • (Hash)

    Parsed response body



14
15
16
17
18
19
20
21
# File 'lib/skill_bench/clients/response_parser.rb', line 14

def self.parse_body(response)
  return response.body if response.body.is_a?(Hash)
  return { error: { message: response.body.to_s } } if response.body.is_a?(Array)

  JSON.parse(response.body, symbolize_names: true)
rescue JSON::ParserError
  { error: { message: response.body.to_s } }
end

.strip_markdown_fences(text) ⇒ String

Strips markdown code fences from a string if present.

Parameters:

  • text (String)

    The text to clean

Returns:

  • (String)

    Cleaned text



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/skill_bench/clients/response_parser.rb', line 27

def self.strip_markdown_fences(text)
  return text unless text.is_a?(String)

  if text.start_with?('```')
    lines = text.each_line.to_a
    lines.shift if lines.first&.strip&.start_with?('```')
    lines.pop if lines.last&.strip == '```'
    lines.join.strip
  else
    text
  end
end

.valid_message?(message) ⇒ Boolean

Checks if a message is valid (has content or tool calls).

Parameters:

  • message (Hash, String, nil)

    The message to validate

Returns:

  • (Boolean)

    True if the message is valid



44
45
46
47
48
49
50
51
# File 'lib/skill_bench/clients/response_parser.rb', line 44

def self.valid_message?(message)
  return false if message.nil?

  content = extract_content(message)
  tool_calls = extract_tool_calls(message)

  !content.nil? || !Array(tool_calls).empty?
end