Class: Hitch::MCP::Internal::ResultNormalizer

Inherits:
Object
  • Object
show all
Defined in:
app/models/hitch/mcp/internal/result_normalizer.rb

Overview

Converts the closed host Result into the one exact SDK response that was independently schema-validated and measured before SDK validation.

Constant Summary collapse

JSON_SCHEMA_2020_12 =
"https://json-schema.org/draft/2020-12/schema"
ROOT_SCHEMA_KEYWORDS =
%w[type $ref oneOf anyOf allOf not if const enum].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result, output_schema, max_bytes) ⇒ ResultNormalizer

Returns a new instance of ResultNormalizer.



60
61
62
63
64
# File 'app/models/hitch/mcp/internal/result_normalizer.rb', line 60

def initialize(result, output_schema, max_bytes)
  @result = result
  @output_schema = output_schema
  @max_bytes = max_bytes
end

Class Method Details

.call(result:, output_schema:, max_bytes:) ⇒ Object



47
48
49
# File 'app/models/hitch/mcp/internal/result_normalizer.rb', line 47

def call(result:, output_schema:, max_bytes:)
  new(result, output_schema, max_bytes).call
end

.explicit_error_text(response) ⇒ Object



55
56
57
# File 'app/models/hitch/mcp/internal/result_normalizer.rb', line 55

def explicit_error_text(response)
  response.explicit_error_text if response.instance_of?(SDKResponse)
end

.failure_category(error) ⇒ Object



51
52
53
# File 'app/models/hitch/mcp/internal/result_normalizer.rb', line 51

def failure_category(error)
  error.category if error.instance_of?(Failure)
end

Instance Method Details

#callObject



66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/models/hitch/mcp/internal/result_normalizer.rb', line 66

def call
  invalid!(:invalid_result_type) unless result.instance_of?(Result)
  invalid!(:invalid_result_limit) unless max_bytes.instance_of?(Integer) && max_bytes.positive?

  canonical, content_provided, explicit_error_text = canonical_result
  serialized = generate(canonical)
  invalid!(:result_too_large) if serialized.bytesize > max_bytes

  SDKResponse.new(canonical, content_provided:, explicit_error_text:)
rescue SystemStackError
  invalid!(:serialization_failure)
end