Class: RubynCode::LLM::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyn_code/llm/client.rb

Overview

Thin facade over provider-specific adapters.

All consumers (Agent::Loop, REPL, DaemonRunner) talk to Client. Client delegates to the resolved adapter, which can be swapped at runtime via switch_provider! or the /model command.

Defined Under Namespace

Classes: AuthExpiredError, PromptTooLongError, RequestError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model: nil, provider: nil, adapter: nil) ⇒ Client

Returns a new instance of Client.



20
21
22
23
24
25
26
# File 'lib/rubyn_code/llm/client.rb', line 20

def initialize(model: nil, provider: nil, adapter: nil)
  settings = Config::Settings.new
  @model = model || settings.model
  @provider = provider || settings.provider
  @adapter = adapter || resolve_adapter(@provider)
  @thinking_budget_tokens = 0
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



17
18
19
# File 'lib/rubyn_code/llm/client.rb', line 17

def adapter
  @adapter
end

#effortObject

Returns the value of attribute effort.



18
19
20
# File 'lib/rubyn_code/llm/client.rb', line 18

def effort
  @effort
end

#modelObject

Returns the value of attribute model.



18
19
20
# File 'lib/rubyn_code/llm/client.rb', line 18

def model
  @model
end

#thinking_budget_tokensObject

Returns the value of attribute thinking_budget_tokens.



18
19
20
# File 'lib/rubyn_code/llm/client.rb', line 18

def thinking_budget_tokens
  @thinking_budget_tokens
end

Instance Method Details

#chat(messages:, tools: nil, system: nil, model: nil, **opts) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rubyn_code/llm/client.rb', line 28

def chat(messages:, tools: nil, system: nil, model: nil, **opts)
  effective_model = model || @model
  max_tokens = opts[:max_tokens] || Config::Defaults::CAPPED_MAX_OUTPUT_TOKENS
  effective_thinking = opts[:thinking] || current_thinking_hash

  kwargs = {
    messages: messages,
    tools: tools,
    system: system,
    model: effective_model,
    max_tokens: max_tokens,
    on_text: opts[:on_text],
    task_budget: opts[:task_budget]
  }
  kwargs[:thinking] = effective_thinking if effective_thinking
  kwargs[:effort] = @effort if @effort

  @adapter.chat(**kwargs)
end

#current_thinking_hashObject



48
49
50
51
52
53
# File 'lib/rubyn_code/llm/client.rb', line 48

def current_thinking_hash
  budget = @thinking_budget_tokens.to_i
  return nil unless budget.positive?

  { budget_tokens: budget }
end

#modelsObject



65
66
67
# File 'lib/rubyn_code/llm/client.rb', line 65

def models
  @adapter.models
end

#provider_nameObject



61
62
63
# File 'lib/rubyn_code/llm/client.rb', line 61

def provider_name
  @adapter.provider_name
end

#stream(messages:, tools: nil, system: nil, model: nil, max_tokens: Config::Defaults::CAPPED_MAX_OUTPUT_TOKENS, &block) ⇒ Object



55
56
57
58
59
# File 'lib/rubyn_code/llm/client.rb', line 55

def stream(messages:, tools: nil, system: nil, model: nil,
           max_tokens: Config::Defaults::CAPPED_MAX_OUTPUT_TOKENS, &block)
  chat(messages: messages, tools: tools, system: system,
       model: model, max_tokens: max_tokens, on_text: block)
end

#switch_provider!(provider, model: nil) ⇒ Object

Switch the active provider (and optionally model) at runtime. Called by the REPL when /model provider:model is used.

Parameters:

  • provider (String)

    provider name ('anthropic', 'openai', etc.)

  • model (String, nil) (defaults to: nil)

    optional model to set



74
75
76
77
78
# File 'lib/rubyn_code/llm/client.rb', line 74

def switch_provider!(provider, model: nil)
  @provider = provider
  @adapter = resolve_adapter(provider)
  @model = model if model
end