registerSessionFileAccessHooks function

void registerSessionFileAccessHooks({
  1. required RegisterHookCallbacksFn registerHookCallbacks,
  2. void logEvent(
    1. String event,
    2. Map<String, dynamic>? props
    )?,
})

Register session file access tracking hooks.

Implementation

void registerSessionFileAccessHooks({
  required RegisterHookCallbacksFn registerHookCallbacks,
  void Function(String event, Map<String, dynamic>? props)? logEvent,
}) {
  void handleSessionFileAccess(
    String toolName,
    Map<String, dynamic>? toolInput,
  ) {
    final fileType = getSessionFileTypeFromInput(toolName, toolInput);

    if (fileType == 'session_memory') {
      logEvent?.call('session_memory_accessed', null);
    } else if (fileType == 'session_transcript') {
      logEvent?.call('transcript_accessed', null);
    }

    final filePath = getFilePathFromInput(toolName, toolInput);
    if (filePath != null && isAutoMemFile(filePath)) {
      logEvent?.call('memdir_accessed', {'tool': toolName});
    }
  }

  // The actual hook registration is delegated to the caller.
  // In the Dart port, this is a callback-based pattern.
  final hook = <String, dynamic>{
    'type': 'callback',
    'callback': handleSessionFileAccess,
    'timeout': 1,
    'internal': true,
  };

  registerHookCallbacks({
    'PostToolUse': [
      {
        'matcher': 'FileRead',
        'hooks': [hook],
      },
      {
        'matcher': 'Grep',
        'hooks': [hook],
      },
      {
        'matcher': 'Glob',
        'hooks': [hook],
      },
      {
        'matcher': 'FileEdit',
        'hooks': [hook],
      },
      {
        'matcher': 'FileWrite',
        'hooks': [hook],
      },
    ],
  });
}