Class: Flunky::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/flunky/agent.rb

Overview

Optional observe/decide/act loop. The model is injected and must respond to #call(messages:, tools:) returning { text:, tool_calls: [{ id:, name:, arguments: }] }. Keeping the contract vendor-neutral is the whole point: the gem never talks to a model vendor, the caller’s adapter does.

Instance Method Summary collapse

Constructor Details

#initialize(session, model:) ⇒ Agent

Returns a new instance of Agent.



9
10
11
12
13
# File 'lib/flunky/agent.rb', line 9

def initialize(session, model:)
  @session = session
  @model = model
  @tools = Tools.new(session)
end

Instance Method Details

#run(goal, max_steps: 10) ⇒ Object

Drive toward goal, stopping when the model stops asking for tools or the step budget runs out. Returns the transcript of messages exchanged.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/flunky/agent.rb', line 17

def run(goal, max_steps: 10)
  messages = [{ role: "user", content: seed_prompt(goal) }]

  max_steps.times do
    response = @model.call(messages: messages, tools: @tools.definitions)
    messages << { role: "assistant", content: response[:text], tool_calls: response[:tool_calls] }

    calls = response[:tool_calls] || []
    break if calls.empty?

    results = calls.map { |call| run_tool(call) }
    messages << { role: "tool", content: results }
  end

  messages
end