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
FENCE =

Only a fence around the whole code: one inside it is part of a string.

/\A```(?:ruby|rb)?[ \t]*\r?\n(.*?)\r?\n?```\z/m

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.



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

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

#answer ⇒ Object (readonly)

Returns the value of attribute answer.



20
21
22
# File 'lib/omakase/tools/ruby.rb', line 20

def answer
  @answer
end

Instance Method Details

#done? ⇒ Boolean

RubyLLM 2 tools cannot end the loop, so the strategy asks this between steps.

Returns:

  • (Boolean)


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

def done? = !@answer.nil? || @calls > @budget + 1

#execute(code:) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/omakase/tools/ruby.rb', line 37

def execute(code:)
  code = code.strip[FENCE, 1] || 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 "Tool budget spent." if @calls > @budget + 1
  # A later call in the same round must not act on an agent that has answered.
  return "Answer already accepted." if @answer

  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), printed: outcome.printed)
  "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

#name ⇒ Object



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

def name = "ruby"