resolveActiveInferenceModel method

Future<({String? loraPath, WebModelSource model})> resolveActiveInferenceModel()

Resolves the active inference model. Returns the source plus the installed LoRA path if any (only meaningful for MediaPipe; LiteRT-LM throws on loraPath).

Implementation

Future<({WebModelSource model, String? loraPath})>
resolveActiveInferenceModel() async {
  // A resolver built via `forActiveModel()` holds a FRESH WebModelManager
  // whose active identity is rehydrated from prefs asynchronously. Without
  // this await, `activeInferenceModel` is read before the restore completes
  // and reports null even though a model was just installed ("No active
  // inference model set" on createChat). `ensureInitialized` is idempotent.
  await _modelManager.ensureInitialized();
  final active = _modelManager.activeInferenceModel;
  if (active == null) {
    throw StateError(
      'No active inference model set. Use FlutterGemma.installModel() first.',
    );
  }
  final paths = await _modelManager.getModelFilePaths(active);
  if (paths == null || paths.isEmpty) {
    throw StateError('Model file paths not found for active model.');
  }
  final raw = paths[PreferencesKeys.installedModelFileName];
  if (raw == null) {
    throw StateError('Model path not found in file paths.');
  }
  final lora = paths[PreferencesKeys.installedLoraFileName];
  final source = await _toSource(raw);
  return (model: source, loraPath: lora);
}