processFile method

Future<ProcessingResult> processFile(
  1. String filePath
)

Process a single file

Implementation

Future<ProcessingResult> processFile(String filePath) async {
  _processedCount = 0;
  _failedCount = 0;
  _totalEmbeddings = 0;
  _errors.clear();

  final file = File(filePath);
  if (!await file.exists()) {
    throw Exception('File not found: $filePath');
  }

  // Set targetDirectory as the parent directory of the file
  final actualTargetDirectory = path.dirname(filePath);

  _logger.info('📝 Processing file: $filePath');
  if (verbose) {
    _logger
      ..fine('🔍 [VERBOSE] Absolute path: ${file.absolute.path}')
      ..fine('🔍 [VERBOSE] Parent directory: $actualTargetDirectory');
  }

  // Process file
  try {
    await _processFile(file, actualTargetDirectory);
    _processedCount++;
  } catch (e) {
    _failedCount++;
    // Check if it's a ModelNotFoundException to provide better error message
    if (e is ModelNotFoundException) {
      final error = 'Error processing ${file.path}: ${e.toString()}';
      _errors.add(error);
      _logger.warning('❌ $error');
      // Re-throw to be handled at higher level with command instruction
      rethrow;
    }
    final error = 'Error processing ${file.path}: $e';
    _errors.add(error);
    _logger.warning('❌ $error');
  }

  return ProcessingResult(
    processedCount: _processedCount,
    failedCount: _failedCount,
    totalEmbeddings: _totalEmbeddings,
    errors: List.unmodifiable(_errors),
  );
}