Class: Legate::Callbacks::CallbackContext
- Inherits:
-
Object
- Object
- Legate::Callbacks::CallbackContext
- Defined in:
- lib/legate/callbacks/callback_context.rb
Overview
Context object passed to agent lifecycle and model interaction callbacks.
Instance Attribute Summary collapse
-
#agent_name ⇒ Object
readonly
Returns the value of attribute agent_name.
-
#app_name ⇒ Object
readonly
Returns the value of attribute app_name.
-
#invocation_id ⇒ Object
readonly
Returns the value of attribute invocation_id.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#pending_state_delta ⇒ Object
readonly
Expose pending state delta for inspection but not direct modification.
-
#session_id ⇒ Object
readonly
Returns the value of attribute session_id.
-
#session_service ⇒ Object
readonly
Returns the value of attribute session_service.
-
#user_id ⇒ Object
readonly
Returns the value of attribute user_id.
Instance Method Summary collapse
-
#clear_pending_state_delta! ⇒ Object
Clears any accumulated pending state changes within this context instance.
-
#initialize(agent_name:, invocation_id:, session_id:, user_id:, app_name:, session_service:, logger: Legate.logger) ⇒ CallbackContext
constructor
A new instance of CallbackContext.
-
#state_get(key) ⇒ Object?
Retrieves a value from the session state.
-
#state_set(key, value) ⇒ Object
Sets a value in the pending state delta.
-
#state_update(hash_to_merge) ⇒ Object
Merges a hash into the pending state delta.
Constructor Details
#initialize(agent_name:, invocation_id:, session_id:, user_id:, app_name:, session_service:, logger: Legate.logger) ⇒ CallbackContext
Returns a new instance of CallbackContext.
23 24 25 26 27 28 29 30 31 |
# File 'lib/legate/callbacks/callback_context.rb', line 23 def initialize(agent_name:, invocation_id:, session_id:, user_id:, app_name:, session_service:, logger: Legate.logger) @agent_name = agent_name @invocation_id = invocation_id @session_id = session_id @user_id = user_id @app_name = app_name @session_service = session_service @pending_state_delta = {} # Internal mutable hash end |
Instance Attribute Details
#agent_name ⇒ Object (readonly)
Returns the value of attribute agent_name.
11 12 13 |
# File 'lib/legate/callbacks/callback_context.rb', line 11 def agent_name @agent_name end |
#app_name ⇒ Object (readonly)
Returns the value of attribute app_name.
11 12 13 |
# File 'lib/legate/callbacks/callback_context.rb', line 11 def app_name @app_name end |
#invocation_id ⇒ Object (readonly)
Returns the value of attribute invocation_id.
11 12 13 |
# File 'lib/legate/callbacks/callback_context.rb', line 11 def invocation_id @invocation_id end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
11 12 13 |
# File 'lib/legate/callbacks/callback_context.rb', line 11 def logger @logger end |
#pending_state_delta ⇒ Object (readonly)
Expose pending state delta for inspection but not direct modification
14 15 16 |
# File 'lib/legate/callbacks/callback_context.rb', line 14 def pending_state_delta @pending_state_delta end |
#session_id ⇒ Object (readonly)
Returns the value of attribute session_id.
11 12 13 |
# File 'lib/legate/callbacks/callback_context.rb', line 11 def session_id @session_id end |
#session_service ⇒ Object (readonly)
Returns the value of attribute session_service.
11 12 13 |
# File 'lib/legate/callbacks/callback_context.rb', line 11 def session_service @session_service end |
#user_id ⇒ Object (readonly)
Returns the value of attribute user_id.
11 12 13 |
# File 'lib/legate/callbacks/callback_context.rb', line 11 def user_id @user_id end |
Instance Method Details
#clear_pending_state_delta! ⇒ Object
Clears any accumulated pending state changes within this context instance.
66 67 68 |
# File 'lib/legate/callbacks/callback_context.rb', line 66 def clear_pending_state_delta! @pending_state_delta = {} end |
#state_get(key) ⇒ Object?
Retrieves a value from the session state.
36 37 38 39 40 41 42 |
# File 'lib/legate/callbacks/callback_context.rb', line 36 def state_get(key) Legate.logger.debug { "[CallbackContext] state_get for key: #{key} in session: #{@session_id}" } @session_service.get_state(session_id: @session_id, key: key) rescue StandardError => e Legate.logger.error { "[CallbackContext] Error in state_get for key '#{key}': #{e.}" } nil end |
#state_set(key, value) ⇒ Object
Sets a value in the pending state delta. This change will be applied to the session state by the Legate framework after the callback completes.
48 49 50 51 |
# File 'lib/legate/callbacks/callback_context.rb', line 48 def state_set(key, value) Legate.logger.debug { "[CallbackContext] state_set for key: #{key} to value: #{value.inspect} (pending)" } @pending_state_delta[key.to_sym] = value end |
#state_update(hash_to_merge) ⇒ Object
Merges a hash into the pending state delta.
55 56 57 58 59 60 61 62 63 |
# File 'lib/legate/callbacks/callback_context.rb', line 55 def state_update(hash_to_merge) unless hash_to_merge.is_a?(Hash) Legate.logger.warn { "[CallbackContext] state_update called with non-hash: #{hash_to_merge.class}" } return end Legate.logger.debug { "[CallbackContext] state_update with hash: #{hash_to_merge.inspect} (pending)" } @pending_state_delta.merge!(hash_to_merge.transform_keys(&:to_sym)) end |