Class: DhanHQ::AI::ContextBuilder
- Inherits:
-
Object
- Object
- DhanHQ::AI::ContextBuilder
- Defined in:
- lib/DhanHQ/ai/context_builder.rb
Overview
Build context for AI agents from current account state.
Provides methods to serialize portfolio, positions, orders, and market data into prompt-friendly formats.
Instance Attribute Summary collapse
-
#sections ⇒ Object
readonly
Returns the value of attribute sections.
Class Method Summary collapse
-
.build {|builder| ... } ⇒ DhanHQ::AI::ContextBuilder
Build context using a block.
Instance Method Summary collapse
-
#add_funds ⇒ Object
Add fund information to context.
-
#add_market_snapshot(security_ids:) ⇒ Object
Add market snapshot to context.
-
#add_portfolio ⇒ Object
Add portfolio holdings to context.
-
#add_positions ⇒ Object
Add current positions to context.
-
#add_recent_orders(limit: 10) ⇒ Object
Add recent orders to context.
-
#add_section(type:, data:, summary:) ⇒ Object
Add custom section to context.
-
#initialize ⇒ ContextBuilder
constructor
A new instance of ContextBuilder.
-
#to_h ⇒ Hash
Serialize context to a hash.
-
#to_prompt ⇒ String
Serialize context to a prompt string.
Constructor Details
#initialize ⇒ ContextBuilder
Returns a new instance of ContextBuilder.
25 26 27 |
# File 'lib/DhanHQ/ai/context_builder.rb', line 25 def initialize @sections = [] end |
Instance Attribute Details
#sections ⇒ Object (readonly)
Returns the value of attribute sections.
23 24 25 |
# File 'lib/DhanHQ/ai/context_builder.rb', line 23 def sections @sections end |
Class Method Details
.build {|builder| ... } ⇒ DhanHQ::AI::ContextBuilder
Build context using a block.
33 34 35 36 37 |
# File 'lib/DhanHQ/ai/context_builder.rb', line 33 def self.build builder = new yield builder builder end |
Instance Method Details
#add_funds ⇒ Object
Add fund information to context.
77 78 79 80 81 82 83 84 85 |
# File 'lib/DhanHQ/ai/context_builder.rb', line 77 def add_funds funds = DhanHQ::Models::Funds.fetch @sections << { type: :funds, data: [funds.to_prompt], summary: "Funds: Available ₹#{funds.available_balance}" } self end |
#add_market_snapshot(security_ids:) ⇒ Object
Add market snapshot to context.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/DhanHQ/ai/context_builder.rb', line 90 def add_market_snapshot(security_ids:) instruments = security_ids.each_with_object({}) do |sec_id, hash| hash[DhanHQ::Constants::ExchangeSegment::NSE_EQ] ||= [] hash[DhanHQ::Constants::ExchangeSegment::NSE_EQ] << sec_id end response = DhanHQ::Models::MarketFeed.ltp(instruments) snapshot = DhanHQ::MarketData::MarketSnapshot.from_response(response) @sections << { type: :market_data, data: [snapshot.empty? ? "No data" : "Snapshot with #{snapshot.size} instruments"], summary: "Market: #{snapshot.size} instruments" } self end |
#add_portfolio ⇒ Object
Add portfolio holdings to context.
40 41 42 43 44 45 46 47 48 |
# File 'lib/DhanHQ/ai/context_builder.rb', line 40 def add_portfolio holdings = DhanHQ::Models::Holding.all @sections << { type: :portfolio, data: holdings.map(&:to_prompt), summary: "Portfolio: #{holdings.size} holdings" } self end |
#add_positions ⇒ Object
Add current positions to context.
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/DhanHQ/ai/context_builder.rb', line 51 def add_positions positions = DhanHQ::Models::Position.all active = positions.select(&:open?) @sections << { type: :positions, data: active.map(&:to_prompt), summary: "Positions: #{active.size} open" } self end |
#add_recent_orders(limit: 10) ⇒ Object
Add recent orders to context.
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/DhanHQ/ai/context_builder.rb', line 65 def add_recent_orders(limit: 10) orders = DhanHQ::Models::Order.all recent = orders.last(limit) @sections << { type: :orders, data: recent.map(&:to_prompt), summary: "Orders: #{orders.size} total, #{recent.size} recent" } self end |
#add_section(type:, data:, summary:) ⇒ Object
Add custom section to context.
112 113 114 115 |
# File 'lib/DhanHQ/ai/context_builder.rb', line 112 def add_section(type:, data:, summary:) @sections << { type: type, data: data, summary: summary } self end |
#to_h ⇒ Hash
Serialize context to a hash.
137 138 139 140 141 142 |
# File 'lib/DhanHQ/ai/context_builder.rb', line 137 def to_h { generated_at: Time.now, sections: @sections } end |
#to_prompt ⇒ String
Serialize context to a prompt string.
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/DhanHQ/ai/context_builder.rb', line 120 def to_prompt lines = ["=== DhanHQ Trading Context ==="] lines << "Generated at: #{Time.now}" lines << "" @sections.each do |section| lines << "--- #{section[:summary]} ---" section[:data].each { |item| lines << " #{item}" } lines << "" end lines.join("\n") end |