Class: Deja::Adapters::Base
- Inherits:
-
Object
- Object
- Deja::Adapters::Base
- Defined in:
- lib/deja/adapters/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#install_block ⇒ Object
readonly
Returns the value of attribute install_block.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Instance Method Summary collapse
-
#build_mock_client ⇒ Object
The stub client object the app receives.
-
#cached_call(method, kwargs, &real_call) ⇒ Object
Wraps a single call: records it (for expect_llm_called), routes through the cache, and (de)serializes via the subclass.
- #default_real_client ⇒ Object
-
#deserialize(_method, _data) ⇒ Object
Plain Hash (from the cache) -> object shaped like the provider’s response.
-
#initialize(key:, install:, real_client: nil) ⇒ Base
constructor
key — how this registration is named (usually the provider symbol) install — block run in the example’s context to swap the app’s client for the one Deja hands it real_client — optional block building a live client; falls back to the subclass default.
-
#prompt_for(_kwargs) ⇒ Object
A human-readable prompt string stored on the cache entry (purely for auditing the YAML).
- #real_client ⇒ Object
-
#serialize(_method, _response) ⇒ Object
Provider response object -> plain Hash (must round-trip through deserialize).
Constructor Details
#initialize(key:, install:, real_client: nil) ⇒ Base
key — how this registration is named (usually the provider symbol) install — block run in the example’s context to swap the app’s client
for the one Deja hands it
real_client — optional block building a live client; falls back to the
subclass default
34 35 36 37 38 |
# File 'lib/deja/adapters/base.rb', line 34 def initialize(key:, install:, real_client: nil) @key = key @install_block = install @real_client_override = real_client end |
Instance Attribute Details
#install_block ⇒ Object (readonly)
Returns the value of attribute install_block.
27 28 29 |
# File 'lib/deja/adapters/base.rb', line 27 def install_block @install_block end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
27 28 29 |
# File 'lib/deja/adapters/base.rb', line 27 def key @key end |
Instance Method Details
#build_mock_client ⇒ Object
The stub client object the app receives. Its methods call ‘cached_call`.
58 59 60 |
# File 'lib/deja/adapters/base.rb', line 58 def build_mock_client raise NotImplementedError, "#{self.class} must implement #build_mock_client" end |
#cached_call(method, kwargs, &real_call) ⇒ Object
Wraps a single call: records it (for expect_llm_called), routes through the cache, and (de)serializes via the subclass. ‘real_call` performs the live provider call when recording.
47 48 49 50 51 52 53 |
# File 'lib/deja/adapters/base.rb', line 47 def cached_call(method, kwargs, &real_call) Deja.record_call(key, method, kwargs) data = Deja::Cache.fetch(method, kwargs, provider: key, prompt: prompt_for(kwargs)) do serialize(method, real_call.call) end deserialize(method, data) end |
#default_real_client ⇒ Object
72 73 74 |
# File 'lib/deja/adapters/base.rb', line 72 def default_real_client raise NotImplementedError, "#{self.class} must implement #default_real_client" end |
#deserialize(_method, _data) ⇒ Object
Plain Hash (from the cache) -> object shaped like the provider’s response.
68 69 70 |
# File 'lib/deja/adapters/base.rb', line 68 def deserialize(_method, _data) raise NotImplementedError, "#{self.class} must implement #deserialize" end |
#prompt_for(_kwargs) ⇒ Object
A human-readable prompt string stored on the cache entry (purely for auditing the YAML). Optional.
78 79 80 |
# File 'lib/deja/adapters/base.rb', line 78 def prompt_for(_kwargs) nil end |
#real_client ⇒ Object
40 41 42 |
# File 'lib/deja/adapters/base.rb', line 40 def real_client (@real_client_override || default_real_client).call end |
#serialize(_method, _response) ⇒ Object
Provider response object -> plain Hash (must round-trip through deserialize).
63 64 65 |
# File 'lib/deja/adapters/base.rb', line 63 def serialize(_method, _response) raise NotImplementedError, "#{self.class} must implement #serialize" end |