Class: OllamaAgent::Resilience::RetryMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama_agent/resilience/retry_middleware.rb

Overview

Wraps an Ollama::Client with exponential backoff retry for transient errors.

Constant Summary collapse

DEFAULT_MAX_ATTEMPTS =
RetryPolicy::DEFAULT_MAX_ATTEMPTS
DEFAULT_BASE_DELAY =
RetryPolicy::DEFAULT_BASE_DELAY

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, max_attempts: DEFAULT_MAX_ATTEMPTS, hooks: nil, base_delay: DEFAULT_BASE_DELAY) ⇒ RetryMiddleware

Returns a new instance of RetryMiddleware.



15
16
17
18
19
# File 'lib/ollama_agent/resilience/retry_middleware.rb', line 15

def initialize(client:, max_attempts: DEFAULT_MAX_ATTEMPTS, hooks: nil, base_delay: DEFAULT_BASE_DELAY)
  @client  = client
  @hooks   = hooks
  @policy  = RetryPolicy.new(max_attempts: max_attempts, base_delay: base_delay)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



13
14
15
# File 'lib/ollama_agent/resilience/retry_middleware.rb', line 13

def client
  @client
end

#policyObject (readonly)

Returns the value of attribute policy.



13
14
15
# File 'lib/ollama_agent/resilience/retry_middleware.rb', line 13

def policy
  @policy
end

Instance Method Details

#chat(**args) ⇒ Object

rubocop:disable Metrics/MethodLength – single rescue/retry loop



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ollama_agent/resilience/retry_middleware.rb', line 22

def chat(**args)
  attempt = 0
  begin
    @client.chat(**args)
  rescue *RetryPolicy::RETRYABLE => e
    raise if http_error_non_retryable?(e)

    attempt += 1
    raise if attempt >= @policy.max_attempts

    delay = @policy.backoff(attempt)
    @hooks&.emit(:on_retry, { error: e, attempt: attempt, delay_ms: (delay * 1000).round })
    sleep delay
    retry
  end
end

#list_model_namesObject

Delegates to the inner Ollama client (/api/tags).



41
42
43
# File 'lib/ollama_agent/resilience/retry_middleware.rb', line 41

def list_model_names
  @client.list_model_names
end