Class: Legate::Callbacks::CallbackContext

Inherits:
Object
  • Object
show all
Defined in:
lib/legate/callbacks/callback_context.rb

Overview

Context object passed to agent lifecycle and model interaction callbacks.

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • agent_name (Symbol)
  • invocation_id (String)
  • session_id (String)
  • user_id (String)
  • app_name (String)
  • session_service (Legate::SessionService::Base)
  • logger (Logger) (defaults to: Legate.logger)


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_nameObject (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_nameObject (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_idObject (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

#loggerObject (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_deltaObject (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_idObject (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_serviceObject (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_idObject (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.

Parameters:

  • key (Symbol, String)

    The key to retrieve

Returns:

  • (Object, nil)

    The value or nil if not found



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.message}" }
  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.

Parameters:

  • key (Symbol, String)

    The key to set

  • value (Object)

    The value to store (should be serializable)



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.

Parameters:

  • hash_to_merge (Hash)

    The hash to merge 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