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.
24 25 26 27 |
# File 'lib/prescient/tool.rb', line 24 def initialize(**) @options = validate_configuration! end |
Instance Attribute Details
#options ⇒ Hash<Symbol, untyped> (readonly)
Returns Tool configuration options.
21 22 23 |
# File 'lib/prescient/tool.rb', line 21 def @options end |
Instance Method Details
#max_response_bytes ⇒ Integer (protected)
Returns Maximum accepted response size.
85 86 87 88 89 90 91 92 93 |
# File 'lib/prescient/tool.rb', line 85 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.
75 76 77 78 79 80 81 82 |
# File 'lib/prescient/tool.rb', line 75 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.
64 65 66 67 68 69 70 71 |
# File 'lib/prescient/tool.rb', line 64 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.
34 35 36 |
# File 'lib/prescient/tool.rb', line 34 def search(query, **) raise NotImplementedError, "#{self.class} must implement #search" end |
#validate_configuration! ⇒ void (protected)
This method returns an undefined value.
Validate adapter configuration.
42 43 44 |
# File 'lib/prescient/tool.rb', line 42 def validate_configuration! # Override in subclasses. end |
#validate_query(query) ⇒ String (protected)
Returns Validated query.
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/prescient/tool.rb', line 48 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 |