install method

  1. @override
Future<void> install(
  1. ModelSource source, {
  2. CancelToken? cancelToken,
  3. String? targetFilename,
})
override

Installs the model from the given source

This method performs the actual installation:

  • NetworkSource: downloads from URL
  • AssetSource: copies from Flutter assets
  • BundledSource: accesses native resources
  • FileSource: registers external file path

Parameters:

  • source: The model source to install from
  • cancelToken: Optional token for cancelling the installation
  • targetFilename: Optional override for the installed file's identity (write-target basename, ModelInfo.id, and — on web — the registration/cache key). Defaults to null, which reproduces today's behavior: the basename is derived from source (URL path segment / asset path / bundled resource name / file path). Passed by the four *InstallationBuilders so a companion file's on-disk identity can be namespaced by its owning model (see docs/superpowers/specs/2026-08-02-install-identity-namespacing-design.md).

Throws:

Implementation

@override
Future<void> install(
  ModelSource source, {
  CancelToken? cancelToken,
  String? targetFilename,
}) async {
  if (source is! AssetSource) {
    throw ArgumentError('AssetSourceHandler only supports AssetSource');
  }

  final filename = targetFilename ?? path.basename(source.path);
  // ignore: deprecated_member_use_from_same_package
  final targetPath = await fileSystem.getWriteTargetPath(filename);

  // LargeFileHandler's `targetName` parameter is *just* a filename — the
  // plugin prepends app docs dir itself. We keep the bare filename here.
  // On platforms where large_file_handler doesn't ship a plugin (desktop:
  // macOS/Windows/Linux, web stub) the channel call throws
  // MissingPluginException — fall back to in-memory loadAsset → writeFile.
  //
  // Lookup keys differ between paths:
  // - `pathForLookupKey` (no `assets/` prefix) for the native channel call
  // - `normalizedPath` (with `assets/` prefix) for the Flutter rootBundle
  //   fallback (#250 Mode 2)
  if (assetLoader is FlutterAssetLoader) {
    try {
      await (assetLoader as FlutterAssetLoader).copyAssetToFile(
        source.pathForLookupKey,
        filename,
      );
    } on MissingPluginException {
      final assetData = await assetLoader.loadAsset(source.normalizedPath);
      await fileSystem.writeFile(targetPath, assetData);
    }
  } else {
    final assetData = await assetLoader.loadAsset(source.normalizedPath);
    await fileSystem.writeFile(targetPath, assetData);
  }

  final sizeBytes = await fileSystem.getFileSize(targetPath);
  assertInstalledFilePresent(sizeBytes, targetPath);

  final modelInfo = ModelInfo(
    id: filename,
    source: source,
    installedAt: DateTime.now(),
    sizeBytes: sizeBytes,
    type: ModelType.inference,
    hasLoraWeights: false,
  );

  await repository.saveModel(modelInfo);
}