Class: Phronomy::Agent::AgentRoot

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

Constant Summary collapse

LIFECYCLE_STATUSES =
%i[idle active suspended closed invalidated].freeze
ATTRIBUTES =
%i[
  agent_id agent_definition_id definition_version agent_revision
  context_revision journal_position lifecycle_status transcript_generation
  created_at updated_at metadata
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**attributes) ⇒ AgentRoot

Returns a new instance of AgentRoot.

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/phronomy/agent/agent_root.rb', line 35

def initialize(**attributes)
  ATTRIBUTES.each do |name|
    value = attributes.fetch(name)
    value = value.to_sym if name == :lifecycle_status
    instance_variable_set("@#{name}", Immutable.copy(value))
  end
  unless LIFECYCLE_STATUSES.include?(lifecycle_status)
    raise ArgumentError, "unknown Agent lifecycle status: #{lifecycle_status.inspect}"
  end
  raise ArgumentError, "agent_revision must be non-negative" if agent_revision.negative?
  raise ArgumentError, "context_revision must be non-negative" if context_revision.negative?
  raise ArgumentError, "journal_position must be non-negative" if journal_position.negative?
  Immutable.validate_canonical_json!(, label: "Agent metadata")
  freeze
end

Class Method Details

.create(agent_id:, agent_definition_id:, definition_version:, metadata: {}) ⇒ Object



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

def self.create(agent_id:, agent_definition_id:, definition_version:, metadata: {})
  now = Time.now.utc.iso8601(6)
  new(
    agent_id: agent_id,
    agent_definition_id: agent_definition_id,
    definition_version: definition_version,
    agent_revision: 0,
    context_revision: 0,
    journal_position: 0,
    lifecycle_status: :idle,
    transcript_generation: 0,
    created_at: now,
    updated_at: now,
    metadata: 
  )
end

.from_h(hash) ⇒ Object



61
62
63
# File 'lib/phronomy/agent/agent_root.rb', line 61

def self.from_h(hash)
  new(**ATTRIBUTES.to_h { |name| [name, hash.fetch(name.to_s)] })
end

Instance Method Details

#to_hObject



57
58
59
# File 'lib/phronomy/agent/agent_root.rb', line 57

def to_h
  ATTRIBUTES.to_h { |name| [name.to_s, public_send(name)] }
end

#with(**changes) ⇒ Object



51
52
53
54
55
# File 'lib/phronomy/agent/agent_root.rb', line 51

def with(**changes)
  values = to_h.transform_keys(&:to_sym).merge(changes)
  values[:updated_at] = Time.now.utc.iso8601(6) unless changes.key?(:updated_at)
  self.class.new(**values)
end