Class: Phronomy::Agent::ContextAssembler

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

Constant Summary collapse

ASSEMBLY_POLICY_VERSION =
6
SEGMENT_ORIGIN_METADATA_KEY =
"phronomy_origin"
BEFORE_LLM_INPUT_ORIGIN =
"before_llm_input"
EARLY_CONTEXT_CATEGORIES =
%i[
  instruction structured_state knowledge memory summary
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(agent:, persistence:, policy: ContextPolicies::Default.new, candidate_resolver: nil) ⇒ ContextAssembler

Returns a new instance of ContextAssembler.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/phronomy/agent/context_assembler.rb', line 13

def initialize(
  agent:,
  persistence:,
  policy: ContextPolicies::Default.new,
  candidate_resolver: nil
)
  @agent = agent
  @persistence = persistence
  @policy = policy
  @candidate_resolver = candidate_resolver || ContextCandidateResolver.new(
    content_loader: method(:fetch_content)
  )
end

Instance Method Details

#build_followup(base_manifest:, agent_root:, execution:, config: {}, patch: LLMInputPatch.empty) ⇒ Object



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
# File 'lib/phronomy/agent/context_assembler.rb', line 61

def build_followup(
  base_manifest:,
  agent_root:,
  execution:,
  config: {},
  patch: LLMInputPatch.empty
)
  projection = JournalProjection.new(persistence: @persistence, agent_root: agent_root)
  model_cfg = effective_model_config(config, patch)
  hook_candidates = normalize_candidates(patch.segment_candidates)
  system_segments = base_manifest.segments.select do |segment|
    segment.role == :system && !before_llm_input_segment?(segment)
  end
  tool_definitions = base_manifest.tool_definitions_ref ?
    fetch_json(base_manifest.tool_definitions_ref) : []

  assemble(
    agent_root: agent_root,
    execution: execution,
    call_sequence: execution.llm_calls.length + 1,
    call_mode: :complete,
    previous_manifest: base_manifest,
    model_config: model_cfg,
    tool_definitions: tool_definitions,
    tool_definitions_ref: base_manifest.tool_definitions_ref,
    prior_records: projection.context_records,
    working_records: execution.working_records,
    excluded_record_ids: [],
    base_segments: system_segments.map { |existing| segment_hash(existing) },
    hook_candidates: hook_candidates,
    current_input_segment: nil,
    mandatory_values: system_segments.map { |segment| fetch_content(segment.content_ref) } +
      [tool_definitions]
  )
end

#build_initial(input:, agent_root:, execution:, config: {}, patch: LLMInputPatch.empty) ⇒ Object



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
# File 'lib/phronomy/agent/context_assembler.rb', line 27

def build_initial(input:, agent_root:, execution:, config: {}, patch: LLMInputPatch.empty)
  projection = JournalProjection.new(persistence: @persistence, agent_root: agent_root)
  model_cfg = effective_model_config(config, patch)
  tool_set = ToolDefinitionSet.build(@agent)
  system_text = build_system_text(input)
  hook_candidates = normalize_candidates(patch.segment_candidates)
  input_ref = execution..fetch("current_input_ref")
  current_input = @persistence.contents.fetch_text(input_ref)
  excluded = [execution.["current_input_record_id"]].compact

  base_segments = []
  unless system_text.to_s.empty?
    base_segments << text_segment(:instruction, :system, system_text)
  end

  assemble(
    agent_root: agent_root,
    execution: execution,
    call_sequence: 1,
    call_mode: :ask,
    previous_manifest: nil,
    model_config: model_cfg,
    tool_definitions: tool_set.definitions,
    tool_definitions_ref: nil,
    prior_records: projection.context_records,
    working_records: execution.working_records,
    excluded_record_ids: excluded,
    base_segments: base_segments,
    hook_candidates: hook_candidates,
    current_input_segment: segment(:current_input, :user, input_ref, :ask_argument),
    mandatory_values: [system_text, current_input, tool_set.definitions]
  )
end