Class: Phronomy::Agent::JournalProjection

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

Constant Summary collapse

MESSAGE_KINDS =
%i[
  external_message
  assistant_message
  tool_message
].freeze
KNOWLEDGE_RESET_KINDS =
%i[knowledge_cleared context_reset].freeze

Instance Method Summary collapse

Constructor Details

#initialize(agent_root:, persistence: nil, records: nil) ⇒ JournalProjection

Returns a new instance of JournalProjection.



13
14
15
16
17
18
19
20
# File 'lib/phronomy/agent/journal_projection.rb', line 13

def initialize(agent_root:, persistence: nil, records: nil)
  @persistence = persistence
  @agent_root = agent_root
  @records = records&.dup&.freeze
  if @records.nil? && @persistence.nil?
    raise ArgumentError, "JournalProjection requires persistence: or records:"
  end
end

Instance Method Details

#context_recordsObject

Returns all persistent records eligible for Context selection. Knowledge has an independent logical lifetime from the transcript: clear_transcript! does not remove Knowledge, while knowledge_cleared and context_reset invalidate earlier Knowledge records by Journal position.



35
36
37
38
39
# File 'lib/phronomy/agent/journal_projection.rb', line 35

def context_records
  (transcript_records + active_knowledge_records)
    .sort_by { |record| [record.sequence || 0, record.record_id] }
    .freeze
end

#llm_call_recordsObject



41
42
43
# File 'lib/phronomy/agent/journal_projection.rb', line 41

def llm_call_records
  records.select { |record| record.kind == :llm_call_recorded }
end

#recordsObject



45
46
47
# File 'lib/phronomy/agent/journal_projection.rb', line 45

def records
  @records ||= @persistence.journals.read(@agent_root.agent_id)
end

#transcript_recordsObject



22
23
24
25
26
27
28
29
# File 'lib/phronomy/agent/journal_projection.rb', line 22

def transcript_records
  generation = @agent_root.transcript_generation
  records.select do |record|
    record.context_candidate &&
      record.context_generation == generation &&
      MESSAGE_KINDS.include?(record.kind)
  end
end