model_cache library
Locate the pinned local embedding model offline, and download it only when a host asks.
EmbeddingModelCache.locate reads files and never touches the network.
EmbeddingModelCache.provision downloads localEmbeddingModel into the
cache directory, by default models/<id> in the Wayfinder data directory,
verifies its size and SHA-256, and only then renames it into place. A
host calls it after the user agrees to the download its
EmbeddingModelCache.target describes.
The entry also exports the package:wayfinder/project.dart types its
API uses: WayfinderException for failures, WayfinderCancellation and
WayfinderCancellationController to stop a download, and
WayfinderDataDirectory for the default data directory. A host needs no
direct wayfinder dependency to use it.
import 'package:wayfinder_embeddings/model_cache.dart';
Future<void> main() async {
final cache = EmbeddingModelCache();
var model = await cache.locate();
if (model == null) {
await for (final step in cache.provision()) {
switch (step) {
case ModelDownloading(:final fraction):
print('${(fraction * 100).round()}%');
case ModelVerifying():
print('Verifying');
case ModelProvisioned(:final file):
model = file;
}
}
}
print('Model: ${model!.path}');
}
Classes
- EmbeddingModelCache
- The local copy of a pinned embedding model, in one cache directory.
- EmbeddingModelSpec
- Pinned artifact and preprocessing contract for a local embedding model.
- EmbeddingModelTarget
- The pinned model an EmbeddingModelCache provides, and where it goes.
- ModelDownloading
- The bytes received so far: reported for the first bytes, about once per percent after them, and for the last.
- ModelProvisioned
- The last event: the verified model is in place.
- ModelProvisionProgress
- One step of EmbeddingModelCache.provision.
- ModelVerifying
- The download is complete, and its size and SHA-256 are being checked.
- WayfinderCancellation
- Asks a long operation to stop. An operation checks isCancelled between steps and stops a running child process when whenCancelled completes; it then throws WayfinderException.cancelled. Cancelling is not a rollback: what an operation already finished stays finished.
- WayfinderCancellationController
- A WayfinderCancellation its owner cancels by calling cancel.
- WayfinderDataDirectory
-
Where Wayfinder keeps per-user state: Profile sources and their locks
under
profiles/, project locks underprojects/, and the retrieval library's indexes. Everything there is derived and can be rebuilt.
Enums
- EmbeddingModelRemedy
- What fixes a model EmbeddingModelSpec.verify refuses, as data for a host to word: the library's messages name no command (ADR-0018).
Constants
- localEmbeddingModel → const EmbeddingModelSpec
- Arctic Embed XS Q8_0, using CLS pooling and normalized retrieval vectors.
Exceptions / Errors
- EmbeddingModelMismatchException
- EmbeddingModelSpec.verify found a model file at path whose size or SHA-256 is not the pinned model's. It is a FormatException, so code that catches one still does.
- EmbeddingModelMissingException
- EmbeddingModelSpec.verify found no model file at path. It is a FileSystemException, so code that catches one still does.
- WayfinderException
- A failure a host can act on: code is a stable kebab-case identifier, and message a sentence that says what happened and names no command (ADR-0018). The command line prints it and adds what to run; a host words its own remedy from the code and, where one is typed, the exception's data.