Skip to main content

weirdnet_model/
lib.rs

1//! Weirdnet local models.
2//!
3//! - [`catalog`]: pinned Gemma 4 E2B and E4B GGUF files (Hugging Face repo, file, size, SHA-256).
4//! - [`store`] and [`mod@download`]: where model files live, and a consent-gated download that
5//!   resumes a `.part` file and checks size and SHA-256 before the file appears.
6//! - [`hf`]: read a Hugging Face repo's GGUF files, so any repo can become a [`ModelSpec`].
7//! - [`prompt`]: render chat turns with the model's own Jinja template, and the `<tool_call>`
8//!   tag format for tools (prompt text and a streaming parser).
9//! - [`worker`]: an [`Engine`] on its own thread, streaming text with cancel.
10//! - [`cpu`]: whether this CPU can run the compiled-in llama.cpp (checked before it loads).
11//! - `llama` (feature): the llama.cpp [`Engine`].
12//! - `agent` (feature): `weirdnet_agent::ModelBackend` over the worker.
13//!
14//! Nothing here knows an app: callers pass paths, consent and callbacks.
15#![forbid(unsafe_code)]
16
17pub mod catalog;
18pub mod cpu;
19pub mod download;
20pub mod hf;
21pub mod prompt;
22pub mod store;
23#[cfg(test)]
24mod testing;
25pub mod worker;
26
27#[cfg(feature = "agent")]
28pub mod agent;
29#[cfg(feature = "llama")]
30pub mod llama;
31
32pub use catalog::{Device, ModelSpec, human};
33pub use download::{Consent, Progress, download};
34pub use prompt::{ChatMessage, ChatRole};
35pub use store::ModelStore;
36pub use worker::{Chunk, Engine, GenerateRequest, Generation, Stats, Worker};
37
38/// Everything that can go wrong here.
39#[derive(Debug, thiserror::Error)]
40pub enum Error {
41    #[error("{0}")]
42    Io(#[from] std::io::Error),
43    #[error("network: {0}")]
44    Http(String),
45    #[error("{name}: checksum mismatch (got {got}); the file was deleted")]
46    Checksum { name: String, got: String },
47    #[error("download stopped at {done} of {total} (run it again to resume)")]
48    Incomplete { done: String, total: String },
49    #[error("download cancelled at {done} of {total} (run it again to resume)")]
50    Cancelled { done: String, total: String },
51    #[error("consent was given for another model")]
52    Consent,
53    #[error("{0:?} is not a Hugging Face model id (owner/name)")]
54    BadRepo(String),
55    #[error("{0}")]
56    Model(String),
57    /// This CPU lacks instructions the compiled-in llama.cpp uses; see [`cpu`].
58    #[error("local models need CPU instructions this one lacks: {}", missing.join(", "))]
59    UnsupportedCpu { missing: Vec<String> },
60    /// `cpu-dispatch` builds: no llama.cpp CPU backend library could be loaded.
61    #[error("no llama.cpp CPU backend found in {} (ship the ggml-cpu libraries there)", dir.display())]
62    NoCpuBackend { dir: std::path::PathBuf },
63}
64
65pub type Result<T> = std::result::Result<T, Error>;