hanzo-ai-format 1.1.21

Hanzo AI - Universal .ai file format for AI artifacts
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
//! Storage backends for AI artifacts
//!
//! Supports multiple storage backends:
//! - Local filesystem
//! - HuggingFace Hub (fallback/mirror)
//! - P2P swarm (BitTorrent-style)
//! - IPFS
//! - Node operator storage

use crate::{
    error::{AiFormatError, Result},
    AiArtifact, ArtifactId, ArtifactMetadata, ArtifactRef, StorageLocation,
};
use async_trait::async_trait;
use dashmap::DashMap;
use reqwest::Client;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tokio::fs;
use tracing::{debug, info, warn};

/// Storage backend trait
#[async_trait]
pub trait StorageBackend: Send + Sync {
    /// Check if an artifact exists
    async fn exists(&self, id: &ArtifactId) -> Result<bool>;

    /// Get artifact metadata
    async fn get_metadata(&self, id: &ArtifactId) -> Result<ArtifactMetadata>;

    /// Download an artifact
    async fn download(&self, id: &ArtifactId, dest: &Path) -> Result<PathBuf>;

    /// Upload an artifact
    async fn upload(&self, artifact: &AiArtifact) -> Result<StorageLocation>;

    /// List available artifacts
    async fn list(&self) -> Result<Vec<ArtifactRef>>;

    /// Delete an artifact
    async fn delete(&self, id: &ArtifactId) -> Result<()>;

    /// Get the storage location type
    fn location_type(&self) -> &'static str;
}

/// Multi-backend storage manager
pub struct Storage {
    /// Local storage (primary)
    local: LocalStorage,
    /// HuggingFace storage (fallback)
    huggingface: Option<HuggingFaceStorage>,
    /// IPFS storage
    ipfs: Option<IpfsStorage>,
    /// Node operator storage endpoints
    node_storage: Vec<NodeStorage>,
    /// Artifact cache
    cache: Arc<DashMap<ArtifactId, PathBuf>>,
}

impl Storage {
    /// Create a new storage manager with local storage only
    pub fn new(local_path: impl Into<PathBuf>) -> Self {
        Self {
            local: LocalStorage::new(local_path),
            huggingface: None,
            ipfs: None,
            node_storage: Vec::new(),
            cache: Arc::new(DashMap::new()),
        }
    }

    /// Create storage with HuggingFace fallback
    pub fn with_hf_fallback(local_path: impl Into<PathBuf>, hf_token: Option<String>) -> Self {
        Self {
            local: LocalStorage::new(local_path),
            huggingface: Some(HuggingFaceStorage::new(hf_token)),
            ipfs: None,
            node_storage: Vec::new(),
            cache: Arc::new(DashMap::new()),
        }
    }

    /// Add IPFS backend
    pub fn with_ipfs(mut self, gateway: impl Into<String>) -> Self {
        self.ipfs = Some(IpfsStorage::new(gateway));
        self
    }

    /// Add node operator storage
    pub fn with_node_storage(mut self, peer_id: impl Into<String>, endpoint: impl Into<String>) -> Self {
        self.node_storage.push(NodeStorage::new(peer_id, endpoint));
        self
    }

    /// Get artifact, trying backends in order
    pub async fn get(&self, id: &ArtifactId) -> Result<PathBuf> {
        // Check cache first
        if let Some(path) = self.cache.get(id) {
            if path.exists() {
                return Ok(path.clone());
            }
        }

        // Try local storage first
        if self.local.exists(id).await? {
            let path = self.local.get_path(id);
            self.cache.insert(id.clone(), path.clone());
            return Ok(path);
        }

        // Try node operator storage
        for node in &self.node_storage {
            if let Ok(true) = node.exists(id).await {
                let path = node.download(id, &self.local.base_path).await?;
                self.cache.insert(id.clone(), path.clone());
                return Ok(path);
            }
        }

        // Try HuggingFace fallback
        if let Some(hf) = &self.huggingface {
            if let Ok(true) = hf.exists(id).await {
                let path = hf.download(id, &self.local.base_path).await?;
                self.cache.insert(id.clone(), path.clone());
                return Ok(path);
            }
        }

        // Try IPFS
        if let Some(ipfs) = &self.ipfs {
            if let Ok(true) = ipfs.exists(id).await {
                let path = ipfs.download(id, &self.local.base_path).await?;
                self.cache.insert(id.clone(), path.clone());
                return Ok(path);
            }
        }

        Err(AiFormatError::artifact_not_found(id))
    }

    /// Store artifact locally and optionally replicate
    pub async fn store(&self, artifact: &AiArtifact, replicate: bool) -> Result<Vec<StorageLocation>> {
        let mut locations = Vec::new();

        // Always store locally
        let local_loc = self.local.upload(artifact).await?;
        locations.push(local_loc);

        if replicate {
            // Replicate to HuggingFace if available
            if let Some(hf) = &self.huggingface {
                match hf.upload(artifact).await {
                    Ok(loc) => locations.push(loc),
                    Err(e) => warn!("Failed to replicate to HuggingFace: {}", e),
                }
            }

            // Replicate to IPFS if available
            if let Some(ipfs) = &self.ipfs {
                match ipfs.upload(artifact).await {
                    Ok(loc) => locations.push(loc),
                    Err(e) => warn!("Failed to replicate to IPFS: {}", e),
                }
            }
        }

        Ok(locations)
    }

    /// Load an artifact from storage
    pub async fn load(&self, id: &ArtifactId) -> Result<AiArtifact> {
        let path = self.get(id).await?;
        AiArtifact::load(&path).await
    }

    /// Get local storage path
    pub fn local_path(&self) -> &Path {
        &self.local.base_path
    }

    /// Clear cache
    pub fn clear_cache(&self) {
        self.cache.clear();
    }
}

/// Local filesystem storage
pub struct LocalStorage {
    base_path: PathBuf,
}

impl LocalStorage {
    pub fn new(base_path: impl Into<PathBuf>) -> Self {
        Self {
            base_path: base_path.into(),
        }
    }

    pub fn get_path(&self, id: &ArtifactId) -> PathBuf {
        self.base_path.join(format!("{}.ai", id))
    }

    /// Get default storage path
    pub fn default_path() -> PathBuf {
        dirs::data_local_dir()
            .unwrap_or_else(|| PathBuf::from("."))
            .join("hanzo")
            .join("artifacts")
    }
}

#[async_trait]
impl StorageBackend for LocalStorage {
    async fn exists(&self, id: &ArtifactId) -> Result<bool> {
        Ok(self.get_path(id).exists())
    }

    async fn get_metadata(&self, id: &ArtifactId) -> Result<ArtifactMetadata> {
        let artifact = AiArtifact::load(self.get_path(id)).await?;
        Ok(artifact.metadata)
    }

    async fn download(&self, id: &ArtifactId, _dest: &Path) -> Result<PathBuf> {
        let path = self.get_path(id);
        if path.exists() {
            Ok(path)
        } else {
            Err(AiFormatError::artifact_not_found(id))
        }
    }

    async fn upload(&self, artifact: &AiArtifact) -> Result<StorageLocation> {
        fs::create_dir_all(&self.base_path).await?;
        let path = self.get_path(&artifact.metadata.id);
        let mut artifact = artifact.clone();
        artifact.save(&path).await?;
        info!("Saved artifact {} to {:?}", artifact.metadata.id, path);
        Ok(StorageLocation::local(path.to_string_lossy()))
    }

    async fn list(&self) -> Result<Vec<ArtifactRef>> {
        let mut refs = Vec::new();

        if !self.base_path.exists() {
            return Ok(refs);
        }

        let mut entries = fs::read_dir(&self.base_path).await?;
        while let Some(entry) = entries.next_entry().await? {
            let path = entry.path();
            if path.extension().map(|e| e == "ai").unwrap_or(false) {
                if let Ok(artifact) = AiArtifact::load(&path).await {
                    refs.push(ArtifactRef::new(
                        artifact.metadata.content_hash,
                        artifact.metadata.name,
                        artifact.metadata.version,
                    ));
                }
            }
        }

        Ok(refs)
    }

    async fn delete(&self, id: &ArtifactId) -> Result<()> {
        let path = self.get_path(id);
        if path.exists() {
            fs::remove_file(path).await?;
        }
        Ok(())
    }

    fn location_type(&self) -> &'static str {
        "local"
    }
}

/// HuggingFace Hub storage (fallback)
pub struct HuggingFaceStorage {
    client: Client,
    token: Option<String>,
    api_base: String,
}

impl HuggingFaceStorage {
    pub fn new(token: Option<String>) -> Self {
        Self {
            client: Client::new(),
            token,
            api_base: "https://huggingface.co/api".to_string(),
        }
    }

    pub fn with_endpoint(mut self, endpoint: impl Into<String>) -> Self {
        self.api_base = endpoint.into();
        self
    }

    /// Parse artifact ID to repo_id format
    fn parse_repo_id(id: &ArtifactId) -> String {
        // Format: org/model or model -> hanzo-ai/model
        if id.contains('/') {
            id.clone()
        } else {
            format!("hanzo-ai/{}", id)
        }
    }
}

#[async_trait]
impl StorageBackend for HuggingFaceStorage {
    async fn exists(&self, id: &ArtifactId) -> Result<bool> {
        let repo_id = Self::parse_repo_id(id);
        let url = format!("{}/models/{}", self.api_base, repo_id);

        let mut req = self.client.head(&url);
        if let Some(token) = &self.token {
            req = req.bearer_auth(token);
        }

        match req.send().await {
            Ok(resp) => Ok(resp.status().is_success()),
            Err(_) => Ok(false),
        }
    }

    async fn get_metadata(&self, id: &ArtifactId) -> Result<ArtifactMetadata> {
        let repo_id = Self::parse_repo_id(id);
        let url = format!("{}/models/{}", self.api_base, repo_id);

        let mut req = self.client.get(&url);
        if let Some(token) = &self.token {
            req = req.bearer_auth(token);
        }

        let resp = req.send().await?;
        if !resp.status().is_success() {
            return Err(AiFormatError::artifact_not_found(id));
        }

        let data: serde_json::Value = resp.json().await?;

        // Convert HF metadata to our format
        let name = data["modelId"].as_str().unwrap_or(id).to_string();
        let metadata = ArtifactMetadata::new(&name, crate::ArtifactType::Model);

        Ok(metadata)
    }

    async fn download(&self, id: &ArtifactId, dest: &Path) -> Result<PathBuf> {
        let repo_id = Self::parse_repo_id(id);
        debug!("Downloading from HuggingFace: {}", repo_id);

        // Download the .ai file if it exists, or construct from repo
        let url = format!(
            "https://huggingface.co/{}/resolve/main/artifact.ai",
            repo_id
        );

        let mut req = self.client.get(&url);
        if let Some(token) = &self.token {
            req = req.bearer_auth(token);
        }

        let resp = req.send().await?;
        if !resp.status().is_success() {
            return Err(AiFormatError::HuggingFace(format!(
                "Failed to download {}: {}",
                repo_id,
                resp.status()
            )));
        }

        let bytes = resp.bytes().await?;
        let dest_path = dest.join(format!("{}.ai", id.replace('/', "_")));

        fs::create_dir_all(dest).await?;
        fs::write(&dest_path, bytes).await?;

        info!("Downloaded {} to {:?}", repo_id, dest_path);
        Ok(dest_path)
    }

    async fn upload(&self, artifact: &AiArtifact) -> Result<StorageLocation> {
        let _token = self.token.as_ref().ok_or_else(|| {
            AiFormatError::HuggingFace("HuggingFace token required for upload".to_string())
        })?;

        let repo_id = format!("hanzo-ai/{}", artifact.metadata.name);

        // Create repo if needed (simplified - real impl would use HF API properly)
        debug!("Would upload to HuggingFace repo: {}", repo_id);

        // For now, just return the location hint
        Ok(StorageLocation::huggingface(&repo_id))
    }

    async fn list(&self) -> Result<Vec<ArtifactRef>> {
        // List artifacts from hanzo-ai organization
        let url = format!("{}/models?author=hanzo-ai", self.api_base);

        let mut req = self.client.get(&url);
        if let Some(token) = &self.token {
            req = req.bearer_auth(token);
        }

        let resp = req.send().await?;
        if !resp.status().is_success() {
            return Ok(Vec::new());
        }

        let data: Vec<serde_json::Value> = resp.json().await?;

        let refs = data
            .iter()
            .filter_map(|model| {
                let id = model["modelId"].as_str()?;
                Some(ArtifactRef::new(
                    String::new(), // No hash from HF
                    id.to_string(),
                    "latest".to_string(),
                ))
            })
            .collect();

        Ok(refs)
    }

    async fn delete(&self, _id: &ArtifactId) -> Result<()> {
        Err(AiFormatError::HuggingFace(
            "Deletion not supported via API".to_string(),
        ))
    }

    fn location_type(&self) -> &'static str {
        "huggingface"
    }
}

/// IPFS storage backend
pub struct IpfsStorage {
    client: Client,
    gateway: String,
}

impl IpfsStorage {
    pub fn new(gateway: impl Into<String>) -> Self {
        Self {
            client: Client::new(),
            gateway: gateway.into(),
        }
    }

    pub fn default_gateway() -> Self {
        Self::new("https://ipfs.io")
    }
}

#[async_trait]
impl StorageBackend for IpfsStorage {
    async fn exists(&self, id: &ArtifactId) -> Result<bool> {
        // For IPFS, the id should be a CID
        let url = format!("{}/ipfs/{}", self.gateway, id);

        match self.client.head(&url).send().await {
            Ok(resp) => Ok(resp.status().is_success()),
            Err(_) => Ok(false),
        }
    }

    async fn get_metadata(&self, id: &ArtifactId) -> Result<ArtifactMetadata> {
        // Download and parse the artifact to get metadata
        let artifact = self.download_artifact(id).await?;
        Ok(artifact.metadata)
    }

    async fn download(&self, id: &ArtifactId, dest: &Path) -> Result<PathBuf> {
        let url = format!("{}/ipfs/{}", self.gateway, id);
        debug!("Downloading from IPFS: {}", url);

        let resp = self.client.get(&url).send().await?;
        if !resp.status().is_success() {
            return Err(AiFormatError::Network(format!(
                "IPFS download failed: {}",
                resp.status()
            )));
        }

        let bytes = resp.bytes().await?;
        let dest_path = dest.join(format!("{}.ai", id));

        fs::create_dir_all(dest).await?;
        fs::write(&dest_path, bytes).await?;

        info!("Downloaded IPFS {} to {:?}", id, dest_path);
        Ok(dest_path)
    }

    async fn upload(&self, _artifact: &AiArtifact) -> Result<StorageLocation> {
        // IPFS upload would require pinning service
        Err(AiFormatError::Network(
            "IPFS upload requires pinning service configuration".to_string(),
        ))
    }

    async fn list(&self) -> Result<Vec<ArtifactRef>> {
        // IPFS doesn't have a list operation
        Ok(Vec::new())
    }

    async fn delete(&self, _id: &ArtifactId) -> Result<()> {
        // IPFS content can't be deleted
        Err(AiFormatError::Network(
            "IPFS content is immutable".to_string(),
        ))
    }

    fn location_type(&self) -> &'static str {
        "ipfs"
    }
}

impl IpfsStorage {
    async fn download_artifact(&self, id: &ArtifactId) -> Result<AiArtifact> {
        let url = format!("{}/ipfs/{}", self.gateway, id);
        let resp = self.client.get(&url).send().await?;

        if !resp.status().is_success() {
            return Err(AiFormatError::artifact_not_found(id));
        }

        let bytes = resp.bytes().await?;
        let temp_path = std::env::temp_dir().join(format!("{}.ai", id));
        fs::write(&temp_path, &bytes).await?;

        let artifact = AiArtifact::load(&temp_path).await?;
        let _ = fs::remove_file(&temp_path).await;

        Ok(artifact)
    }
}

/// Node operator storage (peer-to-peer)
pub struct NodeStorage {
    peer_id: String,
    endpoint: String,
    client: Client,
}

impl NodeStorage {
    pub fn new(peer_id: impl Into<String>, endpoint: impl Into<String>) -> Self {
        Self {
            peer_id: peer_id.into(),
            endpoint: endpoint.into(),
            client: Client::new(),
        }
    }
}

#[async_trait]
impl StorageBackend for NodeStorage {
    async fn exists(&self, id: &ArtifactId) -> Result<bool> {
        let url = format!("{}/artifacts/{}/exists", self.endpoint, id);

        match self.client.get(&url).send().await {
            Ok(resp) => Ok(resp.status().is_success()),
            Err(_) => Ok(false),
        }
    }

    async fn get_metadata(&self, id: &ArtifactId) -> Result<ArtifactMetadata> {
        let url = format!("{}/artifacts/{}/metadata", self.endpoint, id);

        let resp = self.client.get(&url).send().await?;
        if !resp.status().is_success() {
            return Err(AiFormatError::artifact_not_found(id));
        }

        let metadata: ArtifactMetadata = resp.json().await?;
        Ok(metadata)
    }

    async fn download(&self, id: &ArtifactId, dest: &Path) -> Result<PathBuf> {
        let url = format!("{}/artifacts/{}/download", self.endpoint, id);
        debug!("Downloading from node {}: {}", self.peer_id, id);

        let resp = self.client.get(&url).send().await?;
        if !resp.status().is_success() {
            return Err(AiFormatError::PeerNotFound(self.peer_id.clone()));
        }

        let bytes = resp.bytes().await?;
        let dest_path = dest.join(format!("{}.ai", id));

        fs::create_dir_all(dest).await?;
        fs::write(&dest_path, bytes).await?;

        info!("Downloaded {} from node {}", id, self.peer_id);
        Ok(dest_path)
    }

    async fn upload(&self, artifact: &AiArtifact) -> Result<StorageLocation> {
        let url = format!("{}/artifacts/upload", self.endpoint);

        // Serialize artifact to bytes
        let temp_path = std::env::temp_dir().join(format!("{}.ai", artifact.metadata.id));
        let mut artifact = artifact.clone();
        artifact.save(&temp_path).await?;
        let bytes = fs::read(&temp_path).await?;
        let _ = fs::remove_file(&temp_path).await;

        let resp = self.client
            .post(&url)
            .body(bytes)
            .send()
            .await?;

        if !resp.status().is_success() {
            return Err(AiFormatError::storage("Node upload failed"));
        }

        Ok(StorageLocation::NodeStorage {
            peer_id: self.peer_id.clone(),
            path: artifact.metadata.id.clone(),
        })
    }

    async fn list(&self) -> Result<Vec<ArtifactRef>> {
        let url = format!("{}/artifacts", self.endpoint);

        let resp = self.client.get(&url).send().await?;
        if !resp.status().is_success() {
            return Ok(Vec::new());
        }

        let refs: Vec<ArtifactRef> = resp.json().await?;
        Ok(refs)
    }

    async fn delete(&self, id: &ArtifactId) -> Result<()> {
        let url = format!("{}/artifacts/{}", self.endpoint, id);

        let resp = self.client.delete(&url).send().await?;
        if !resp.status().is_success() {
            return Err(AiFormatError::storage("Node delete failed"));
        }

        Ok(())
    }

    fn location_type(&self) -> &'static str {
        "node"
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::tempdir;

    #[tokio::test]
    async fn test_local_storage() {
        let dir = tempdir().unwrap();
        let storage = LocalStorage::new(dir.path());

        // Create and save artifact
        let artifact = crate::AiArtifact::builder()
            .name("test-model")
            .artifact_type(crate::ArtifactType::Model)
            .add_config("config.json", b"{}".to_vec())
            .build()
            .unwrap();

        let id = artifact.metadata.id.clone();
        storage.upload(&artifact).await.unwrap();

        // Verify it exists
        assert!(storage.exists(&id).await.unwrap());

        // List should include it
        let list = storage.list().await.unwrap();
        assert!(!list.is_empty());
    }

    #[tokio::test]
    async fn test_storage_manager() {
        let dir = tempdir().unwrap();
        let storage = Storage::new(dir.path());

        let artifact = crate::AiArtifact::builder()
            .name("test-artifact")
            .artifact_type(crate::ArtifactType::Weights)
            .add_file("weights.bin", vec![1, 2, 3, 4])
            .build()
            .unwrap();

        let id = artifact.metadata.id.clone();

        // Store artifact
        let locations = storage.store(&artifact, false).await.unwrap();
        assert_eq!(locations.len(), 1);
        assert!(matches!(locations[0], StorageLocation::Local(_)));

        // Retrieve artifact
        let path = storage.get(&id).await.unwrap();
        assert!(path.exists());
    }

    #[test]
    fn test_hf_repo_id_parsing() {
        assert_eq!(
            HuggingFaceStorage::parse_repo_id(&"meta-llama/Llama-2-7b".to_string()),
            "meta-llama/Llama-2-7b"
        );
        assert_eq!(
            HuggingFaceStorage::parse_repo_id(&"my-model".to_string()),
            "hanzo-ai/my-model"
        );
    }
}