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
//! File attachment services (PDFs and other model-input files).
//!
//! Mirrors [`crate::image_services`] for non-image file attachments that are
//! sent to the model (e.g. PDFs via each provider's native file/document
//! input). Files are uploaded via the `/files` API, stored by ID, and
//! resolved to base64 data URLs at prompt-build time.
use async_trait::async_trait;
use everruns_provider::error::Result;
use std::collections::HashMap;
use uuid::Uuid;
/// Stored file info from the files table.
#[derive(Debug, Clone)]
pub struct StoredFileInfo {
/// Original filename supplied at upload, if any.
pub filename: Option<String>,
/// MIME type of the stored bytes.
pub content_type: String,
/// Size of the stored bytes.
pub size_bytes: i64,
}
/// Stored file with data.
#[derive(Debug, Clone)]
pub struct StoredFile {
/// Metadata describing the stored file.
pub info: StoredFileInfo,
/// Raw stored bytes.
pub data: Vec<u8>,
}
/// Input for creating a stored file.
#[derive(Debug, Clone)]
pub struct CreateStoredFile {
/// Original filename, if known.
pub filename: Option<String>,
/// MIME type of `data`.
pub content_type: String,
/// Raw bytes to store.
pub data: Vec<u8>,
}
/// Storage backend for file attachments.
#[async_trait]
pub trait FileArtifactStore: Send + Sync {
/// Persist a new file and return its id.
async fn save_file(&self, input: CreateStoredFile) -> Result<Uuid>;
/// Load a stored file by id, or `None` when absent.
async fn get_file(&self, id: Uuid) -> Result<Option<StoredFile>>;
/// Delete a stored file; reports whether one existed.
async fn delete_file(&self, id: Uuid) -> Result<bool>;
}
/// A file resolved to base64 for inclusion in an LLM request.
#[derive(Debug, Clone)]
pub struct ResolvedFile {
/// Base64-encoded file bytes.
pub base64: String,
/// MIME type, e.g. `application/pdf`.
pub media_type: String,
/// Original filename, when known.
pub filename: Option<String>,
}
impl ResolvedFile {
/// Render the resolved bytes as a `data:` URL.
pub fn to_data_url(&self) -> String {
format!("data:{};base64,{}", self.media_type, self.base64)
}
}
/// Abstraction for resolving file IDs to [`ResolvedFile`]s.
#[async_trait]
pub trait FileResolver: Send + Sync {
/// Resolve stored-file ids to inlineable payloads.
async fn resolve_files(&self, ids: &[Uuid]) -> Result<HashMap<Uuid, ResolvedFile>>;
}