Class: Phronomy::Agent::PhaseMachineBuilder Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Compiles AgentInvocation phase topology.

Async completion is represented by explicit FSM events. This builder does not await Tasks or register Task callbacks.

Constant Summary collapse

TOOL_EVENTS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

%i[
  tool_authorized
  tool_approval_required
  tool_completed
  tool_failed
  tool_rejected
  tool_cancelled
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(entry_actions: {}) ⇒ PhaseMachineBuilder

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of PhaseMachineBuilder.



23
24
25
# File 'lib/phronomy/agent/phase_machine_builder.rb', line 23

def initialize(entry_actions: {})
  @entry_actions = entry_actions
end

Instance Method Details

#buildObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/phronomy/agent/phase_machine_builder.rb', line 27

def build
  entry_actions = @entry_actions
  callback_builder = method(:build_entry_callback)

  Class.new do
    attr_accessor :context, :current_event

    state_machine :phase, initial: :idle do
      state :idle
      state :filtering_input
      state :building_context
      state :calling_llm
      state :starting_tools
      state :evaluating_tools
      state :waiting_for_tools
      state :dispatching_tools
      state :recording_tool_results
      state :suspended
      state :output_filtering
      state :completed
      state :blocked
      state :failed

      event :state_completed do
        transition idle: :filtering_input

        transition filtering_input: :building_context,
          if: ->(machine) { machine.context&.input_passed? }
        transition filtering_input: :blocked,
          if: ->(machine) { machine.context&.input_blocked? }

        transition building_context: :calling_llm
        transition starting_tools: :evaluating_tools

        transition evaluating_tools: :failed,
          if: ->(machine) { machine.context&.tool_batch_failed? }
        transition evaluating_tools: :blocked,
          if: ->(machine) { machine.context&.tool_batch_rejected? }
        transition evaluating_tools: :recording_tool_results,
          if: ->(machine) { machine.context&.tool_batch_completed? }
        transition evaluating_tools: :suspended,
          if: ->(machine) { machine.context&.approval_required? }
        transition evaluating_tools: :dispatching_tools,
          if: ->(machine) { machine.context&.ready_to_dispatch? }
        transition evaluating_tools: :waiting_for_tools

        transition dispatching_tools: :evaluating_tools
        transition recording_tool_results: :calling_llm

        transition output_filtering: :completed,
          if: ->(machine) { machine.context&.output_passed? }
        transition output_filtering: :blocked,
          if: ->(machine) { machine.context&.output_blocked? }
      end

      event :llm_completed do
        transition calling_llm: :starting_tools,
          if: ->(machine) { machine.context&.tool_call_pending? }
        transition calling_llm: :output_filtering
      end

      event :llm_failed do
        transition calling_llm: :failed
      end

      TOOL_EVENTS.each do |event_name|
        event event_name do
          transition waiting_for_tools: :evaluating_tools
        end
      end

      event :resume do
        transition suspended: :waiting_for_tools
      end

      entry_actions.each do |state_name, callables|
        callables.each do |callable|
          after_transition(
            to: state_name,
            do: callback_builder.call(callable, state_name)
          )
        end
      end
    end
  end
end