Class: ActionAI::Interaction

Inherits:
Delegator
  • Object
show all
Defined in:
lib/action_ai/interaction.rb

Overview

Action AI Interaction

The ActionAI::Interaction class is used by ActionAI::Agent when creating a new agent. Interaction is a wrapper (Delegator subclass) around a lazy created RubyLLM::Message. You can get direct access to the RubyLLM::Message or schedule the job to be executed through Active Job.

Generator.code(task)         # an ActionAI::Interaction object
Generator.code(task).content # executes and returns the result
Generator.code(task).later   # enqueue execution as a job through Active Job
Generator.code(task).message # a RubyLLM::Message object

Direct Known Subclasses

Parameterized::PromptExecution

Instance Method Summary collapse

Constructor Details

#initialize(agent_class, action, *args) ⇒ Interaction

:nodoc:



20
21
22
23
24
25
26
27
# File 'lib/action_ai/interaction.rb', line 20

def initialize(agent_class, action, *args) # :nodoc:
  @agent_class, @action, @args = agent_class, action, args

  # The AI interaction is only processed if we try to call any methods on it.
  # Typical usage will leave it unloaded and call +later+.
  @processed_agent = nil
  @message = nil
end

Instance Method Details

#__getobj__Object

Method calls are delegated to the RubyLLM::Message that’s ready to execute.



31
32
33
34
35
36
37
# File 'lib/action_ai/interaction.rb', line 31

def __getobj__ # :nodoc:
  @message ||= processed_agent.handle_exceptions do
    processed_agent.run_callbacks(:execution) do
      processed_agent.message
    end
  end.presence
end

#__setobj__(message) ⇒ Object

Unused except for delegator internals (dup, marshalling).



40
41
42
# File 'lib/action_ai/interaction.rb', line 40

def __setobj__(message) # :nodoc:
  @message = message
end

#laterObject

Enqueues the action to be executed through Active Job.

Generator.code(task).later
Generator.code(task).later(wait: 1.hour)
Generator.code(task).later(wait_until: 10.hours.from_now)
Generator.code(task).later(priority: 10)

Options:

  • :wait - Enqueue the action to be executed with a delay.

  • :wait_until - Enqueue the action to be executed at (after) a specific date / time.

  • :queue - Enqueue the action on the specified queue.

  • :priority - Enqueues the action with the specified priority

By default, the action will be enqueued using ActionAI::ExecutionJob on the default queue. Agent classes can customize the queue name used for the default job by assigning a execute_later_queue_name class variable, or provide a custom job by assigning a execution_job. When a custom job is used, it controls the queue name.

class CostlyAgent < ApplicationAI
  self.execution_job = CostlyExecutionJob
end


78
# File 'lib/action_ai/interaction.rb', line 78

def later(...) = enqueue_execution(...)

#messageObject

Returns the resulting RubyLLM::Message



45
46
47
# File 'lib/action_ai/interaction.rb', line 45

def message
  __getobj__
end

#processed?Boolean

Was the delegate loaded, causing the action to be processed?

Returns:

  • (Boolean)


50
51
52
# File 'lib/action_ai/interaction.rb', line 50

def processed?
  @processed_agent || @message
end

#runObject



54
# File 'lib/action_ai/interaction.rb', line 54

def run = message