Class: Collavre::Orchestration::AgentContextBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/services/collavre/orchestration/agent_context_builder.rb

Overview

Builds context for AI agents including:

  • Agent identity

  • Available collaborators from Creative permissions

  • Organization hierarchy derived from permission levels

  • Sender context for A2A communication

Permission → Role mapping:

  • admin: escalation targets (supervisors)

  • write: peers (collaborators)

  • feedback: reviewers

  • read: information sources only

Constant Summary collapse

PERMISSION_ROLES =
{
  "admin" => "escalation",    # Can escalate issues to
  "write" => "collaborator",  # Can collaborate with
  "feedback" => "reviewer",   # Can request reviews from
  "read" => "reference"       # Can request information from
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(agent:, creative:, sender: nil, policy_resolver: nil) ⇒ AgentContextBuilder

Returns a new instance of AgentContextBuilder.



22
23
24
25
26
27
# File 'app/services/collavre/orchestration/agent_context_builder.rb', line 22

def initialize(agent:, creative:, sender: nil, policy_resolver: nil)
  @agent = agent
  @creative = creative
  @sender = sender
  @policy_resolver = policy_resolver
end

Instance Method Details

#a2a_request?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'app/services/collavre/orchestration/agent_context_builder.rb', line 29

def a2a_request?
  @sender && @sender["is_ai"] == true
end

#buildObject

Returns hash suitable for Liquid template rendering



34
35
36
37
38
39
40
41
42
43
44
# File 'app/services/collavre/orchestration/agent_context_builder.rb', line 34

def build
  result = {
    "agent" => build_agent_identity,
    "collaborators" => build_collaborators,
    "collaboration_guide" => build_collaboration_guide,
    "is_a2a" => a2a_request?
  }

  result["sender"] = @sender if @sender
  result
end

#to_collaboration_promptObject

Generates markdown-formatted collaboration section for system prompt



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
113
114
115
116
117
118
119
120
# File 'app/services/collavre/orchestration/agent_context_builder.rb', line 47

def to_collaboration_prompt
  sections = []
  collab = collaboration_policy_config

  # Add A2A request context if this is an agent-to-agent call
  if a2a_request?
    sections << I18n.t("collavre.ai_agent.a2a.request_header")
    sections << ""
    sections << I18n.t("collavre.ai_agent.a2a.request_description",
                       sender_name: @sender["name"], sender_type: @sender["type"])
    sections << (collab["a2a_focus_instruction"] || I18n.t("collavre.ai_agent.a2a.focus_instruction"))
    sections << (collab["a2a_completion_instruction"] ||
                 I18n.t("collavre.ai_agent.a2a.completion_instruction",
                        sender_name: @sender["name"]))
    sections << (collab["a2a_followup_instruction"] || I18n.t("collavre.ai_agent.a2a.followup_instruction"))
    sections << ""
  elsif @sender
    # Human-triggered request: instruct agent to report back
    sections << I18n.t("collavre.ai_agent.a2a.human_completion_instruction",
                       sender_name: @sender["name"])
    sections << ""
  end

  collaborators = build_collaborators
  return sections.join("\n") if collaborators.empty?

  sections << I18n.t("collavre.ai_agent.collaboration.header")
  sections << ""

  # Group by role
  by_role = collaborators.group_by { |c| c["role"] }

  if by_role["escalation"]&.any?
    sections << I18n.t("collavre.ai_agent.collaboration.escalation_header")
    by_role["escalation"].each do |c|
      sections << "- @#{c['name']}: #{c['description']}"
    end
    sections << ""
  end

  if by_role["collaborator"]&.any?
    sections << I18n.t("collavre.ai_agent.collaboration.collaborator_header")
    by_role["collaborator"].each do |c|
      sections << "- @#{c['name']}: #{c['description']}"
    end
    sections << ""
  end

  if by_role["reviewer"]&.any?
    sections << I18n.t("collavre.ai_agent.collaboration.reviewer_header")
    by_role["reviewer"].each do |c|
      sections << "- @#{c['name']}: #{c['description']}"
    end
    sections << ""
  end

  if by_role["reference"]&.any?
    sections << I18n.t("collavre.ai_agent.collaboration.reference_header")
    by_role["reference"].each do |c|
      sections << "- @#{c['name']}: #{c['description']}"
    end
    sections << ""
  end

  sections << I18n.t("collavre.ai_agent.collaboration.rules_header")
  sections << (collab["mention_rule"] || I18n.t("collavre.ai_agent.collaboration.mention_rule"))
  sections << (collab["confidence_rule"] || I18n.t("collavre.ai_agent.collaboration.confidence_rule"))
  sections << I18n.t("collavre.ai_agent.collaboration.confidence_format_instruction")
  sections << (collab["escalation_rule"] || I18n.t("collavre.ai_agent.collaboration.escalation_rule"))
  sections << (collab["review_rule"] || I18n.t("collavre.ai_agent.collaboration.review_rule"))
  sections << ""

  sections.join("\n")
end