saveRulesSessionVariables method

Future<void> saveRulesSessionVariables(
  1. AppContext context
)

Save session and custom scope variables to persistent storage

Implementation

Future<void> saveRulesSessionVariables(AppContext context) async {
  try {
    final currentSessionId = IdManager.instance.getSessionId();

    if (currentSessionId == 'unknown') {
      ObslyLogger.debug('Cannot save variables - no valid session ID');
      return;
    }

    // Save session variables
    if (context.session.isNotEmpty) {
      await ObslyStorage.instance.storeInternalState(
        '${_sessionVariablesKey}_$currentSessionId',
        context.session,
      );
    }

    // Save custom scopes (excluding session and execution)
    final customScopes = Map<String, dynamic>.from(context.scopes);
    customScopes.remove('session');
    customScopes.remove('execution');

    if (customScopes.isNotEmpty) {
      await ObslyStorage.instance.storeInternalState(
        '${_customScopesKey}_$currentSessionId',
        customScopes,
      );
    }

    ObslyLogger.debug(
      'Saved rules session variables for session: $currentSessionId '
      '(session: ${context.session.length}, custom: ${customScopes.length})',
    );
  } catch (e) {
    ObslyLogger.error('Error saving rules session variables: $e');
  }
}