Skip to main content

eidetic/models/
library.rs

1// Copyright 2026 Mark AB (markik)
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! Model library — API for resolving model artifacts.
5
6use crate::manifest::{BlobFetcher, BlobSource, load_manifest, resolve_blob};
7use crate::schema::ManifestId;
8use crate::schema::{PrivacyClass, ProvenanceRecord, Timestamp, TrustEnvelope};
9use crate::typed::{load_typed, save_typed};
10use crate::{Error, Result, Store};
11
12use super::{ModelManifest, OpaqueBlob};
13
14/// Resolved artifacts and manifest for a model.
15pub struct ResolvedModel {
16    pub manifest: ModelManifest,
17    pub components: ModelComponents,
18}
19
20/// Resolved byte buffers for a model's components.
21pub struct ModelComponents {
22    pub config_bytes: Vec<u8>,
23    pub tokenizer_bytes: Vec<u8>,
24    pub weight_bytes: Vec<u8>,
25}
26
27/// Library API for managing model engrams.
28pub struct ModelLibrary;
29
30impl ModelLibrary {
31    /// Save a model manifest as a typed engram.
32    pub async fn save_model(store: &mut dyn Store, manifest: ModelManifest) -> Result<ManifestId> {
33        save_typed(
34            store,
35            &manifest,
36            Vec::new(),
37            PrivacyClass::LocalOnly,
38            ProvenanceRecord::self_imported("local"),
39            TrustEnvelope::self_asserted(),
40            Timestamp::ZERO,
41        )
42        .await
43    }
44
45    /// Save a model and its component blobs (weights, tokenizer) in one shot.
46    pub async fn save_model_with_components(
47        store: &mut dyn Store,
48        model_id: &str,
49        architecture: &str,
50        license: &str,
51        config: serde_json::Value,
52        weights: &[u8],
53        tokenizer: &[u8],
54        weight_sources: Vec<BlobSource>,
55        tokenizer_sources: Vec<BlobSource>,
56        privacy: PrivacyClass,
57        provenance: ProvenanceRecord,
58        trust: TrustEnvelope,
59        created_at: Timestamp,
60    ) -> Result<ManifestId> {
61        let weight_blob = save_typed(
62            store,
63            &OpaqueBlob(weights.to_vec()),
64            weight_sources,
65            privacy.clone(),
66            provenance.clone(),
67            trust.clone(),
68            created_at,
69        )
70        .await?;
71
72        let tokenizer_blob = save_typed(
73            store,
74            &OpaqueBlob(tokenizer.to_vec()),
75            tokenizer_sources,
76            privacy.clone(),
77            provenance.clone(),
78            trust.clone(),
79            created_at,
80        )
81        .await?;
82
83        let manifest = ModelManifest {
84            model_id: model_id.to_string(),
85            architecture: architecture.to_string(),
86            license: Some(license.to_string()),
87            config,
88            weight_blob,
89            tokenizer_blob,
90        };
91
92        save_typed(
93            store,
94            &manifest,
95            Vec::new(),
96            privacy,
97            provenance,
98            trust,
99            created_at,
100        )
101        .await
102    }
103
104    /// Load a model manifest by its manifest ID.
105    pub async fn load_model(
106        store: &mut dyn Store,
107        id: ManifestId,
108    ) -> Result<Option<ModelManifest>> {
109        load_typed::<ModelManifest>(store, &mut crate::manifest::NoFetcher, id).await
110    }
111
112    /// Resolve all components of a model (config, tokenizer, weights)
113    /// into byte buffers.
114    pub async fn resolve_components(
115        store: &mut dyn Store,
116        fetcher: &mut dyn BlobFetcher,
117        id: ManifestId,
118    ) -> Result<Option<ResolvedModel>> {
119        let Some(manifest) = load_typed::<ModelManifest>(store, fetcher, id).await? else {
120            return Ok(None);
121        };
122
123        let config_bytes = serde_json::to_vec(&manifest.config)
124            .map_err(|e| Error::new(format!("config serialization failed: {e}")))?;
125
126        // To resolve the weight/tokenizer blobs, we first need to load their manifests.
127        let weight_manifest = load_manifest(store, manifest.weight_blob)
128            .await?
129            .ok_or_else(|| Error::new("weight manifest missing"))?;
130        let weight_bytes = resolve_blob(store, fetcher, &weight_manifest).await?;
131
132        let tokenizer_manifest = load_manifest(store, manifest.tokenizer_blob)
133            .await?
134            .ok_or_else(|| Error::new("tokenizer manifest missing"))?;
135        let tokenizer_bytes = resolve_blob(store, fetcher, &tokenizer_manifest).await?;
136
137        Ok(Some(ResolvedModel {
138            manifest,
139            components: ModelComponents {
140                config_bytes,
141                tokenizer_bytes,
142                weight_bytes,
143            },
144        }))
145    }
146}