create static method

Loads config.modelPath, a URL the page can fetch (blob:, https:), on WebGPU (Backend.gpu; a page without WebGPU is refused). The vision tower, when config.visionBackend is set, runs on the text tower's backend; the audio tower on config.audioBackend.

Throws UnsupportedError for what does not run here: the CPU text tower, the NPU, a CPU thread count, a cacheDir or requireWeightCache (the web runtime keeps no weight cache); EmbedderLoadException for a bundle or a page that cannot run as asked.

Implementation

static Future<EmbeddingEngine> create(EmbeddingEngineConfig config) async {
  if (config.cacheDir != null || config.requireWeightCache) {
    throw UnsupportedError(
      'The web runtime ($liteRtLmWebRuntime) keeps no weight cache: leave '
      'cacheDir unset and requireWeightCache false on the web.',
    );
  }
  if (config.backend is CpuBackend) {
    throw UnsupportedError(
      'EmbeddingGemma 2 runs on WebGPU only on the web: the CPU '
      '(WebAssembly) path of $liteRtLmWebRuntime cannot read the bundles\' '
      'external weights ("Failed to get external weight", measured '
      '2026-11-23). Pass backend: Backend.gpu().',
    );
  }
  final backend = _webBackend(config.backend, 'backend');
  final audio = config.audioBackend;
  final budget =
      config.visionTokensPerImage ?? liteRtLmDefaultVisionTokensPerImage;
  if (!liteRtLmVisionTokenBudgets.contains(budget)) {
    throw ArgumentError.value(
      config.visionTokensPerImage,
      'visionTokensPerImage',
      'must be one of $liteRtLmVisionTokenBudgets',
    );
  }
  if (config.visionBackend case final vision?
      when _webBackend(vision, 'visionBackend') != backend) {
    throw UnsupportedError(
      'On the web the vision tower runs on the text tower\'s backend '
      '($backend); visionBackend $vision differs.',
    );
  }
  final file = config.modelPath.split('/').last;
  final embedder = LiteRtLmWebEmbedder(
    LiteRtLmWebEmbedderConfig(
      modelPath: config.modelPath,
      fileName: file.isEmpty ? config.modelPath : file,
      runtime: const WindowLiteRtLmWebRuntime(),
      resolveUrl: (path) async => path,
      revokeUrl: (_) {},
      declaredMedia: {
        if (config.visionBackend != null) EmbedderModality.image,
        if (audio != null) EmbedderModality.audio,
      },
      visionTokensPerImage: budget,
      backend: backend,
      audioBackend: audio == null
          ? liteRtLmWebAudioBackend
          : _webBackend(audio, 'audioBackend'),
    ),
  );
  final loaded = await embedder.load();
  final limit = loaded.maxInputTokens;
  if (limit == null) {
    try {
      await embedder.close();
    } catch (e) {
      // The refusal below is the answer; a failed close only goes to the
      // log.
      runtimeLog('[litertlm_dart/web] closing a refused embedder: $e');
    }
    throw EmbedderLoadException(
      'LiteRT-LM could not pin the input limit of ${config.modelPath} on '
      'the web ($liteRtLmWebRuntime): the engine neither truncated nor '
      'split its over-long probe as expected.',
    );
  }
  return EmbeddingEngine._(
    embedder,
    EmbedderInfo(
      dimension: loaded.dimension,
      maxInputTokens: limit,
      modalities: loaded.modalities,
      visionTokensPerImage: loaded.visionTokensPerImage,
      activeBackend: switch (backend) {
        LiteRtLmWebBackend.cpu => BackendKind.cpu,
        LiteRtLmWebBackend.gpu => BackendKind.gpu,
      },
    ),
  );
}