Class: AgentHarness::Api::SchemaResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/agent_harness/api/schema_response.rb

Overview

Parses and validates one schema-constrained provider response.

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ SchemaResponse

Returns a new instance of SchemaResponse.



10
11
12
# File 'lib/agent_harness/api/schema_response.rb', line 10

def initialize(schema)
  @validator = JSONSchemer.schema(schema)
end

Instance Method Details

#call(response) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/agent_harness/api/schema_response.rb', line 14

def call(response)
  content = response[:content].to_s
  return failure(content, :refusal) if response[:refusal]
  return failure(content, :truncated_output) if response[:finish_reason]&.to_sym == :max_tokens

  parsed = JSON.parse(content)
  return failure(content, :invalid_schema) unless @validator.valid?(parsed)

  {content: content, parsed: parsed, error: nil}
rescue JSON::ParserError
  failure(content, :invalid_json)
end