Class: SkillBench::Clients::ResponseParser
- Inherits:
-
Object
- Object
- SkillBench::Clients::ResponseParser
- 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
-
.extract_content(message) ⇒ String?
Extracts the content from a message.
-
.extract_openai_message(body) ⇒ Hash?
Extracts the message from an OpenAI-compatible response body.
-
.extract_openai_usage(body) ⇒ Hash
Extracts usage data from an OpenAI-compatible response.
-
.extract_tool_calls(message) ⇒ Array?
Extracts tool calls from a message.
-
.parse_body(response) ⇒ Hash
Parses the response body into a Hash.
-
.strip_markdown_fences(text) ⇒ String
Strips markdown code fences from a string if present.
-
.valid_message?(message) ⇒ Boolean
Checks if a message is valid (has content or tool calls).
Class Method Details
.extract_content(message) ⇒ String?
Extracts the content from a message.
57 58 59 60 61 |
# File 'lib/skill_bench/clients/response_parser.rb', line 57 def self.extract_content() return unless .is_a?(Hash) [:content] || ['content'] end |
.extract_openai_message(body) ⇒ Hash?
Extracts the message from an OpenAI-compatible response body.
77 78 79 80 81 82 |
# File 'lib/skill_bench/clients/response_parser.rb', line 77 def self.(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.
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.
67 68 69 70 71 |
# File 'lib/skill_bench/clients/response_parser.rb', line 67 def self.extract_tool_calls() return nil unless .is_a?(Hash) [:tool_calls] || ['tool_calls'] end |
.parse_body(response) ⇒ Hash
Parses the response body into a Hash.
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.
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).
44 45 46 47 48 49 50 51 |
# File 'lib/skill_bench/clients/response_parser.rb', line 44 def self.() return false if .nil? content = extract_content() tool_calls = extract_tool_calls() !content.nil? || !Array(tool_calls).empty? end |