Class: DhanHQ::AI::ContextBuilder

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeContextBuilder

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

#sectionsObject (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.

Yields:

  • (builder)

    The context builder

Returns:



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_fundsObject

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.

Parameters:

  • security_ids (Array<String>)

    Security IDs to fetch



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_portfolioObject

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_positionsObject

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.

Parameters:

  • limit (Integer) (defaults to: 10)

    Number of recent orders (default: 10)



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.

Parameters:

  • type (Symbol)

    Section type

  • data (Array<String>)

    Prompt strings

  • summary (String)

    Section summary



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_hHash

Serialize context to a hash.

Returns:

  • (Hash)

    Context as 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_promptString

Serialize context to a prompt string.

Returns:

  • (String)

    Formatted context 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