Class: Deja::Adapters::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/deja/adapters/base.rb

Direct Known Subclasses

Anthropic

Instance Attribute Summary collapse

Instance Method Summary collapse

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_blockObject (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

#keyObject (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_clientObject

The stub client object the app receives. Its methods call ‘cached_call`.

Raises:

  • (NotImplementedError)


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_clientObject

Raises:

  • (NotImplementedError)


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.

Raises:

  • (NotImplementedError)


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_clientObject



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).

Raises:

  • (NotImplementedError)


63
64
65
# File 'lib/deja/adapters/base.rb', line 63

def serialize(_method, _response)
  raise NotImplementedError, "#{self.class} must implement #serialize"
end