Class: Omakase::Tools::Ruby

Inherits:
RubyLLM::Tool
  • Object
show all
Defined in:
lib/omakase/tools/ruby.rb

Overview

The model's one tool. Every capability of the agent reaches the model through it, so the model composes calls in code instead of one per turn.

Constant Summary collapse

BUDGET =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent, schema, budget: BUDGET, timeout: Executor::TIMEOUT, executor: Omakase.executor) ⇒ Ruby

Returns a new instance of Ruby.



20
21
22
23
24
25
26
27
28
# File 'lib/omakase/tools/ruby.rb', line 20

def initialize(agent, schema, budget: BUDGET, timeout: Executor::TIMEOUT, executor: Omakase.executor)
  super()
  @agent = agent
  @schema = schema
  @budget = budget
  @timeout = timeout
  @executor = executor
  @calls = 0
end

Instance Attribute Details

#answerObject (readonly)

Returns the value of attribute answer.



18
19
20
# File 'lib/omakase/tools/ruby.rb', line 18

def answer
  @answer
end

Instance Method Details

#execute(code:) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/omakase/tools/ruby.rb', line 32

def execute(code:)
  # Nothing bounds the provider's tool loop, so the budget does.
  @calls += 1
  return "No tool calls left — answer with what you have." if @calls == @budget + 1
  return halt("Tool budget spent.") if @calls > @budget + 1

  outcome = @executor.call(@agent, code, timeout: @timeout)
  Omakase.emit(:ruby, agent: @agent, code:, outcome:)
  return outcome if outcome.is_a?(String)
  # The seam's contract, checked here so a wrong executor cannot reach the model.
  raise Error, "executor must return a String or Executor::Answer, got #{outcome.class}" unless outcome.is_a?(Executor::Answer)

  @answer = Executor::Answer.new(value: @schema.take(outcome.value))
  halt("Answer accepted.")
rescue ContractError => e
  # Off-contract answers are corrected inside the same loop, not by another request.
  # Anything else — a broken executor, a bad configuration — is not the model's to fix.
  "finish rejected: #{e.message}"
end

#nameObject



30
# File 'lib/omakase/tools/ruby.rb', line 30

def name = "ruby"