Class: Phronomy::Context::Builder
- Inherits:
-
Object
- Object
- Phronomy::Context::Builder
- Defined in:
- lib/phronomy/context/builder.rb
Overview
Assembles ordered context sections (system prompt, knowledge, conversation history) within a given token budget.
Usage: builder = Phronomy::Context::Builder.new(budget: budget) builder.add_system(instructions_text) builder.add_knowledge(knowledge_text) builder.add_messages(messages) messages_to_send = builder.build
Sections are added in priority order. When the budget is exceeded the lower-priority tail of each section is truncated.
Instance Method Summary collapse
-
#add_knowledge(text) ⇒ Object
Append knowledge/RAG text (medium priority).
-
#add_messages(messages) ⇒ Object
Set conversation messages (lowest priority — oldest are dropped first).
-
#add_system(text) ⇒ Object
Set the system instructions text (highest priority).
-
#build ⇒ Hash
Assemble the context respecting the token budget.
-
#initialize(budget:) ⇒ Builder
constructor
A new instance of Builder.
Constructor Details
#initialize(budget:) ⇒ Builder
Returns a new instance of Builder.
19 20 21 22 23 24 |
# File 'lib/phronomy/context/builder.rb', line 19 def initialize(budget:) @budget = budget @system = nil @knowledge = [] @messages = [] end |
Instance Method Details
#add_knowledge(text) ⇒ Object
Append knowledge/RAG text (medium priority).
35 36 37 38 |
# File 'lib/phronomy/context/builder.rb', line 35 def add_knowledge(text) @knowledge << text.to_s self end |
#add_messages(messages) ⇒ Object
Set conversation messages (lowest priority — oldest are dropped first).
42 43 44 45 |
# File 'lib/phronomy/context/builder.rb', line 42 def () @messages = Array() self end |
#add_system(text) ⇒ Object
Set the system instructions text (highest priority).
28 29 30 31 |
# File 'lib/phronomy/context/builder.rb', line 28 def add_system(text) @system = text.to_s self end |
#build ⇒ Hash
Assemble the context respecting the token budget.
Returns a hash with: :system [String, nil] system prompt (instructions + knowledge) :messages [Array] conversation messages that fit within the budget
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/phronomy/context/builder.rb', line 54 def build used = 0 # System prompt is always included (budget enforcement is informational only). system_text = [@system, *@knowledge].compact.join("\n\n") used += TokenEstimator.estimate(system_text) # Conversation messages — keep as many recent messages as fit. remaining = @budget.available(used: used) kept = (@messages, remaining) { system: system_text.empty? ? nil : system_text, messages: kept } end |