Class: Legion::Extensions::Llm::Canonical::ThinkingConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/llm/canonical/thinking.rb

Overview

Normalized config for thinking across providers. Mirrors lex-llm Thinking::Config.

Constant Summary collapse

INCLUDES =
Thinking
EFFORT_BUDGET =

SSOT for the effort<->budget conversion. A client dialect supplies only ONE axis (Anthropic = budget_tokens only; OpenAI = effort only), but a provider translator may need the OTHER. This single map lets every provider ask for whichever axis it needs and always get a usable value, so thinking survives any client x provider pair (best-effort, never silently dropped). effort -> budget is exact; budget -> effort uses the band boundaries below.

{ 'low' => 1024, 'medium' => 8192, 'high' => 16_384 }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(effort: nil, budget: nil) ⇒ ThinkingConfig

Returns a new instance of ThinkingConfig.



65
66
67
68
# File 'lib/legion/extensions/llm/canonical/thinking.rb', line 65

def initialize(effort: nil, budget: nil)
  @effort = effort.is_a?(Symbol) ? effort.to_s : effort
  @budget = budget
end

Instance Attribute Details

#budgetObject (readonly)

Returns the value of attribute budget.



63
64
65
# File 'lib/legion/extensions/llm/canonical/thinking.rb', line 63

def budget
  @budget
end

#effortObject (readonly)

Returns the value of attribute effort.



63
64
65
# File 'lib/legion/extensions/llm/canonical/thinking.rb', line 63

def effort
  @effort
end

Class Method Details

.build(effort: nil, budget: nil) ⇒ Object

Build from keyword args.



71
72
73
# File 'lib/legion/extensions/llm/canonical/thinking.rb', line 71

def self.build(effort: nil, budget: nil)
  new(effort: effort, budget: budget)
end

.from_hash(source) ⇒ Object

Build from a Hash.



76
77
78
79
80
81
# File 'lib/legion/extensions/llm/canonical/thinking.rb', line 76

def self.from_hash(source)
  return nil if source.nil? || source.empty?

  h = source.transform_keys(&:to_sym)
  build(effort: h[:effort], budget: h[:budget])
end

Instance Method Details

#enabled?Boolean

Whether thinking is configured.

Returns:

  • (Boolean)


90
91
92
# File 'lib/legion/extensions/llm/canonical/thinking.rb', line 90

def enabled?
  !effort.nil? || !budget.nil?
end

#resolved_budgetObject

Budget for a provider that needs a token budget (e.g. Anthropic), derived from effort when budget was not explicitly set. nil only when neither axis is configured.



97
98
99
100
101
102
# File 'lib/legion/extensions/llm/canonical/thinking.rb', line 97

def resolved_budget
  return budget unless budget.nil?
  return nil if effort.nil?

  EFFORT_BUDGET[effort.to_s.downcase] || EFFORT_BUDGET['medium']
end

#resolved_effortObject

Effort for a provider that needs an effort level (e.g. OpenAI), derived from budget when effort was not explicitly set. nil only when neither axis is configured.



107
108
109
110
111
112
113
114
115
116
# File 'lib/legion/extensions/llm/canonical/thinking.rb', line 107

def resolved_effort
  return effort unless effort.nil?
  return nil if budget.nil?

  b = budget.to_i
  if b < EFFORT_BUDGET['medium'] then 'low'
  elsif b < EFFORT_BUDGET['high'] then 'medium'
  else 'high'
  end
end

#to_hObject

Serialize to a Hash for AMQP/fleet/wire transport. Faithful to what was SET — never fabricates the missing axis (use resolved_* for that).



85
86
87
# File 'lib/legion/extensions/llm/canonical/thinking.rb', line 85

def to_h
  { effort: effort, budget: budget }.compact
end