Class: Kreator::AgentLoop

Inherits:
Object
  • Object
show all
Defined in:
lib/kreator/agent_loop.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

DEFAULT_SYSTEM_PROMPT =
"You are Kreator, a concise and practical coding assistant."
DEFAULT_MAX_TOOL_ITERATIONS =
16

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ AgentLoop

Returns a new instance of AgentLoop.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/kreator/agent_loop.rb', line 10

def initialize(options = {})
  @provider = options.fetch(:provider)
  @event_bus = options.fetch(:event_bus, EventBus.new)
  @system_prompt = options.fetch(:system_prompt, DEFAULT_SYSTEM_PROMPT)
  @model = options.fetch(:model, nil)
  @tool_registry = normalize_tools(options.fetch(:tools, nil))
  @tools = @tool_registry.to_a
  @context = options.fetch(:context, ToolContext.new)
  @max_tool_iterations = options.fetch(:max_tool_iterations, DEFAULT_MAX_TOOL_ITERATIONS)
  @last_usage = nil
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



8
9
10
# File 'lib/kreator/agent_loop.rb', line 8

def context
  @context
end

#event_busObject (readonly)

Returns the value of attribute event_bus.



8
9
10
# File 'lib/kreator/agent_loop.rb', line 8

def event_bus
  @event_bus
end

#last_messagesObject (readonly)

Returns the value of attribute last_messages.



8
9
10
# File 'lib/kreator/agent_loop.rb', line 8

def last_messages
  @last_messages
end

#last_usageObject (readonly)

Returns the value of attribute last_usage.



8
9
10
# File 'lib/kreator/agent_loop.rb', line 8

def last_usage
  @last_usage
end

#modelObject (readonly)

Returns the value of attribute model.



8
9
10
# File 'lib/kreator/agent_loop.rb', line 8

def model
  @model
end

#providerObject (readonly)

Returns the value of attribute provider.



8
9
10
# File 'lib/kreator/agent_loop.rb', line 8

def provider
  @provider
end

#system_promptObject (readonly)

Returns the value of attribute system_prompt.



8
9
10
# File 'lib/kreator/agent_loop.rb', line 8

def system_prompt
  @system_prompt
end

#tool_registryObject (readonly)

Returns the value of attribute tool_registry.



8
9
10
# File 'lib/kreator/agent_loop.rb', line 8

def tool_registry
  @tool_registry
end

#toolsObject (readonly)

Returns the value of attribute tools.



8
9
10
# File 'lib/kreator/agent_loop.rb', line 8

def tools
  @tools
end

Instance Method Details

#run(prompt:, messages: [], signal: nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/kreator/agent_loop.rb', line 22

def run(prompt:, messages: [], signal: nil)
  normalized_messages = messages.map { |message| normalize_message(message) }
  normalized_messages << Message.user(prompt)

  @last_usage = nil
  publish("agent_start", model: model, provider: provider_name, capabilities: provider_capabilities)

  final_message = run_until_complete(normalized_messages, signal: signal)

  publish("turn_end", message: final_message.to_h)
  publish("agent_end", message: final_message.to_h)
  @last_messages = normalized_messages
  final_message
rescue StandardError => e
  publish("agent_end", error: error_hash(e))
  raise
end