Class: Prescient::Tool::Base
- Inherits:
-
Object
- Object
- Prescient::Tool::Base
- Defined in:
- lib/prescient/tool.rb
Overview
Base contract for explicit external tool invocation.
Constant Summary collapse
- DEFAULT_TIMEOUT =
Returns Default request timeout in seconds.
5- DEFAULT_MAX_RESULTS =
Returns Default maximum number of returned results.
5- MAX_QUERY_LENGTH =
Returns Maximum accepted search query length.
2_000- DEFAULT_MAX_RESPONSE_BYTES =
Returns Maximum accepted tool response size in bytes.
1_048_576
Instance Attribute Summary collapse
-
#options ⇒ Hash<Symbol, untyped>
readonly
Tool configuration options.
Instance Method Summary collapse
-
#initialize(**options) ⇒ Base
constructor
A new instance of Base.
-
#max_response_bytes ⇒ Integer
protected
Maximum accepted response size.
-
#request_timeout(value) ⇒ Numeric
protected
Validated timeout.
-
#result_limit(value) ⇒ Integer
protected
Validated result limit.
-
#search(query, **options) ⇒ Hash
Execute a search using the tool.
-
#validate_configuration! ⇒ void
protected
Validate adapter configuration.
-
#validate_query(query) ⇒ String
protected
Validated query.
Constructor Details
#initialize(**options) ⇒ Base
Returns a new instance of Base.
23 24 25 26 |
# File 'lib/prescient/tool.rb', line 23 def initialize(**) @options = validate_configuration! end |
Instance Attribute Details
#options ⇒ Hash<Symbol, untyped> (readonly)
Returns Tool configuration options.
20 21 22 |
# File 'lib/prescient/tool.rb', line 20 def @options end |
Instance Method Details
#max_response_bytes ⇒ Integer (protected)
Returns Maximum accepted response size.
84 85 86 87 88 89 90 91 92 |
# File 'lib/prescient/tool.rb', line 84 def max_response_bytes value = @options.fetch(:max_response_bytes, DEFAULT_MAX_RESPONSE_BYTES) unless value.is_a?(Integer) && value.positive? raise Prescient::ToolConfigurationError, 'max_response_bytes must be a positive integer' end value end |
#request_timeout(value) ⇒ Numeric (protected)
Returns Validated timeout.
74 75 76 77 78 79 80 81 |
# File 'lib/prescient/tool.rb', line 74 def request_timeout(value) timeout = value.nil? ? @options.fetch(:timeout, DEFAULT_TIMEOUT) : value unless timeout.is_a?(Numeric) && timeout.positive? raise Prescient::ToolConfigurationError, 'timeout must be a positive number' end timeout end |
#result_limit(value) ⇒ Integer (protected)
Returns Validated result limit.
63 64 65 66 67 68 69 70 |
# File 'lib/prescient/tool.rb', line 63 def result_limit(value) limit = value.nil? ? @options.fetch(:max_results, DEFAULT_MAX_RESULTS) : value unless limit.is_a?(Integer) && limit.positive? raise Prescient::ToolConfigurationError, 'max_results must be a positive integer' end [limit, 20].min end |
#search(query, **options) ⇒ Hash
Execute a search using the tool.
33 34 35 |
# File 'lib/prescient/tool.rb', line 33 def search(query, **) raise NotImplementedError, "#{self.class} must implement #search" end |
#validate_configuration! ⇒ void (protected)
This method returns an undefined value.
Validate adapter configuration.
41 42 43 |
# File 'lib/prescient/tool.rb', line 41 def validate_configuration! # Override in subclasses. end |
#validate_query(query) ⇒ String (protected)
Returns Validated query.
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/prescient/tool.rb', line 47 def validate_query(query) unless query.is_a?(String) && !query.strip.empty? raise Prescient::ToolConfigurationError, 'search query must be a non-empty string' end cleaned_query = query.strip if cleaned_query.length > MAX_QUERY_LENGTH raise Prescient::ToolConfigurationError, "search query cannot contain more than #{MAX_QUERY_LENGTH} characters" end cleaned_query end |