Module: ComposableAgents::Mixins::Resumable

Defined in:
lib/composable_agents/mixins/resumable.rb

Overview

Mixin adding resumable step capabilities to agents. An agent prepending this mixin can use the following:

  • A new constructor named parameter run_id that identifies the run that can be resumable.
  • A step re-entrant method that defines a part of the agent's processing whose input/output is persisted and that can be skipped if it was previously executed.
  • An agent_step method that calls a sub-agent with the artifacts and also tracks the state of this agent.
    • Any agent that implements the methods export_state and import_state will benefit from its state's serialization automatically.
  • An instance variable @artifacts that stores artifacts (initialized with input ones) that are JSON serialized by steps. Artifacts used with this mixin, and states returned by used agents should be JSON-serializable. This mixin uses the following methods from the agent:
  • #export_state -> Object Optional method returning the current JSON-serializable state of the agent.
  • #import_state(state) Optional method that sets the agent state from a JSON-serializable object.

Public API collapse

Internal collapse

Instance Method Details

#initialize(*args, run_id: nil, **kwargs) ⇒ Object

Constructor

Parameters:

  • run_id (String, nil) (defaults to: nil)

    ID identifying this run to reuse previously executed steps, or nil if there is no resumability needed



24
25
26
27
# File 'lib/composable_agents/mixins/resumable.rb', line 24

def initialize(*args, run_id: nil, **kwargs)
  super(*args, **kwargs)
  @run_id = run_id
end

#run(**input_artifacts) ⇒ Hash{Symbol => Object}

Execute the agent to generate some output artifacts based on some input artifacts.

Parameters:

  • input_artifacts (Hash{Symbol => Object})

    The input artifacts content, per artifact name

Returns:

  • (Hash{Symbol => Object})

    The output artifacts returned by the Proc



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
# File 'lib/composable_agents/mixins/resumable.rb', line 35

def run(**input_artifacts)
  # The artifacts store, JSON serializable
  @artifacts = input_artifacts.dup
  # List of the levels' next step index, following the hierarchy of recusive step calls.
  # This is only used if there is a persistent run ID.
  # For example here are the values of this variable if we have this code:
  # # @steps_idx == [0]
  # step(:a) do
  #   # @steps_idx == [0, 0]
  #   step(:a1) do
  #     # @steps_idx == [0, 0, 0]
  #   end
  #   # @steps_idx == [0, 1]
  #   step(:a2) do
  #     # @steps_idx == [0, 1, 0]
  #     step(:a21) do
  #       # @steps_idx == [0, 1, 0, 0]
  #     end
  #     # @steps_idx == [0, 1, 1]
  #     step(:a22) do
  #       # @steps_idx == [0, 1, 1, 0]
  #     end
  #     # @steps_idx == [0, 1, 2]
  #   end
  #   # @steps_idx == [0, 2]
  # end
  # # @steps_idx == [1]
  # step(:b) do
  #   # @steps_idx == [1, 0]
  # end
  @steps_idx = [0] unless @run_id.nil?
  super
end