Class: SwarmSDK::ContextManagement::Context
- Inherits:
-
Object
- Object
- SwarmSDK::ContextManagement::Context
- Defined in:
- lib/swarm_sdk/context_management/context.rb
Overview
Rich context wrapper for context management handlers
Provides a clean, developer-friendly API for manipulating the conversation context when warning thresholds are triggered. Wraps the lower-level Hooks::Context with message manipulation helpers.
Instance Method Summary collapse
-
#agent_name ⇒ Symbol
Agent name.
-
#compress_tool_results(keep_recent: 10, truncate_to: 200) ⇒ Integer
Compress tool result messages to save context space.
-
#compression_applied? ⇒ Boolean
Check if compression has already been applied.
-
#context_limit ⇒ Integer
Total context window size.
-
#initialize(hooks_context) ⇒ Context
constructor
Create a new context wrapper.
-
#log_action(action, details = {}) ⇒ void
Log a context management action.
-
#mark_compression_applied ⇒ void
Mark compression as applied in ContextManager.
-
#message_count ⇒ Integer
Number of messages.
-
#messages ⇒ Array<RubyLLM::Message>
Get all messages (copy for manipulation).
-
#prune_old_messages(keep_recent: 20) ⇒ Integer
Remove old messages from history.
-
#replace_messages(new_messages) ⇒ void
Replace all messages with new array.
-
#summarize_old_exchanges(older_than: 10) ⇒ Integer
Summarize old message exchanges.
-
#threshold ⇒ Integer
Threshold that triggered this handler.
-
#tokens_remaining ⇒ Integer
Tokens remaining in context window.
-
#tokens_used ⇒ Integer
Total tokens used so far.
-
#transform_messages {|Array<RubyLLM::Message>| ... } ⇒ void
Custom message transformation.
-
#usage_percentage ⇒ Float
Current context usage percentage.
Constructor Details
#initialize(hooks_context) ⇒ Context
Create a new context wrapper
29 30 31 32 |
# File 'lib/swarm_sdk/context_management/context.rb', line 29 def initialize(hooks_context) @hooks_context = hooks_context @chat = hooks_context.[:chat] end |
Instance Method Details
#agent_name ⇒ Symbol
Agent name
96 97 98 |
# File 'lib/swarm_sdk/context_management/context.rb', line 96 def agent_name @hooks_context.agent_name end |
#compress_tool_results(keep_recent: 10, truncate_to: 200) ⇒ Integer
Compress tool result messages to save context space
Creates NEW message objects with truncated content (follows RubyLLM patterns). Truncates old tool results while keeping recent ones intact. Automatically marks compression as applied to prevent double compression.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/swarm_sdk/context_management/context.rb', line 155 def compress_tool_results(keep_recent: 10, truncate_to: 200) msgs = .dup compressed_count = 0 # Find tool result messages (skip recent ones) tool_indices = [] msgs.each_with_index do |msg, idx| tool_indices << idx if msg.role == :tool end # Keep recent tool results, compress older ones indices_to_compress = tool_indices[0...-keep_recent] || [] indices_to_compress.each do |idx| msg = msgs[idx] content = msg.content.to_s next if content.length <= truncate_to # Create NEW message with truncated content (NO instance_variable_set!) truncated_content = "#{content[0...truncate_to]}... [truncated for context management]" # Create new message object following RubyLLM patterns msgs[idx] = RubyLLM::Message.new( role: :tool, content: truncated_content, tool_call_id: msg.tool_call_id, ) compressed_count += 1 end (msgs) # Mark compression as applied to coordinate with ContextManager mark_compression_applied compressed_count end |
#compression_applied? ⇒ Boolean
Check if compression has already been applied
218 219 220 221 222 |
# File 'lib/swarm_sdk/context_management/context.rb', line 218 def compression_applied? return false unless @chat.respond_to?(:context_manager) !!@chat.context_manager.compression_applied end |
#context_limit ⇒ Integer
Total context window size
86 87 88 |
# File 'lib/swarm_sdk/context_management/context.rb', line 86 def context_limit @hooks_context.[:context_limit] end |
#log_action(action, details = {}) ⇒ void
This method returns an undefined value.
Log a context management action
Emits a log event for tracking what actions were taken. Useful for debugging and monitoring context management strategies.
316 317 318 319 320 321 322 323 324 325 |
# File 'lib/swarm_sdk/context_management/context.rb', line 316 def log_action(action, details = {}) LogStream.emit( type: "context_management_action", agent: agent_name, threshold: threshold, action: action, usage_percentage: usage_percentage, **details, ) end |
#mark_compression_applied ⇒ void
This method returns an undefined value.
Mark compression as applied in ContextManager
Call this when your handler performs compression to prevent double compression from auto-compression logic.
204 205 206 207 208 |
# File 'lib/swarm_sdk/context_management/context.rb', line 204 def mark_compression_applied return unless @chat.respond_to?(:context_manager) @chat.context_manager.compression_applied = true end |
#message_count ⇒ Integer
Number of messages
122 123 124 |
# File 'lib/swarm_sdk/context_management/context.rb', line 122 def @chat. end |
#messages ⇒ Array<RubyLLM::Message>
Get all messages (copy for manipulation)
110 111 112 |
# File 'lib/swarm_sdk/context_management/context.rb', line 110 def @chat. end |
#prune_old_messages(keep_recent: 20) ⇒ Integer
Remove old messages from history
Keeps system message (if any) and recent exchanges. This is more aggressive than compression and loses context.
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/swarm_sdk/context_management/context.rb', line 237 def (keep_recent: 20) msgs = .dup original_count = msgs.size # Always keep system message if present system_msg = msgs.first if msgs.first&.role == :system non_system = system_msg ? msgs[1..] : msgs # Keep only recent messages if non_system.size > keep_recent kept = non_system.last(keep_recent) new_msgs = system_msg ? [system_msg] + kept : kept (new_msgs) original_count - new_msgs.size else 0 end end |
#replace_messages(new_messages) ⇒ void
This method returns an undefined value.
Replace all messages with new array
136 137 138 |
# File 'lib/swarm_sdk/context_management/context.rb', line 136 def () @chat.() end |
#summarize_old_exchanges(older_than: 10) ⇒ Integer
Summarize old message exchanges
Groups old user/assistant pairs and replaces with summary. This is a placeholder - actual implementation would use LLM.
266 267 268 269 270 271 |
# File 'lib/swarm_sdk/context_management/context.rb', line 266 def summarize_old_exchanges(older_than: 10) # For now, this is a marker - full implementation would call LLM # to summarize exchanges. We provide the API for developers to # implement their own summarization logic. 0 end |
#threshold ⇒ Integer
Threshold that triggered this handler
54 55 56 |
# File 'lib/swarm_sdk/context_management/context.rb', line 54 def threshold @hooks_context.[:threshold] end |
#tokens_remaining ⇒ Integer
Tokens remaining in context window
76 77 78 |
# File 'lib/swarm_sdk/context_management/context.rb', line 76 def tokens_remaining @hooks_context.[:tokens_remaining] end |
#tokens_used ⇒ Integer
Total tokens used so far
64 65 66 |
# File 'lib/swarm_sdk/context_management/context.rb', line 64 def tokens_used @hooks_context.[:tokens_used] end |
#transform_messages {|Array<RubyLLM::Message>| ... } ⇒ void
This method returns an undefined value.
Custom message transformation
Apply a block to transform messages. This gives full control over message manipulation for custom strategies.
297 298 299 300 |
# File 'lib/swarm_sdk/context_management/context.rb', line 297 def new_msgs = yield(.dup) (new_msgs) end |
#usage_percentage ⇒ Float
Current context usage percentage
44 45 46 |
# File 'lib/swarm_sdk/context_management/context.rb', line 44 def usage_percentage @hooks_context.[:percentage] end |