providers-kit 0.0.2

Portable enum-dispatch Kit over multiple model provider APIs.
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
//! Provider-owned file upload, metadata, pagination, and bounded downloads.
//!
//! [`FileUploadRequest`] owns validated bytes. Submit it through
//! [`crate::ProviderBackend::upload_file`], retain the returned lifecycle id, and
//! explicitly delete only caller-owned files. [`FileObject::input_reference`](crate::files::FileObject::input_reference) may
//! differ from the lifecycle id. Processing readiness and operation support are
//! provider-authored facts rather than assumptions following upload.
//!
//! # Retain ownership through deletion
//!
//! This requires an OpenAI key and upload/read/delete access. The exact upload id
//! is the cleanup authority; neither a filename nor a list-difference establishes
//! ownership. If upload fails without a returned id, retain the error's evidence of
//! any uncertain remote outcome rather than deleting a guessed resource.
//!
//! ```no_run
//! use std::num::NonZeroUsize;
//! use providers_kit::{Provider, ProviderBackend, ProviderConfig};
//! use providers_kit::config::{ApiKey, OpenAiConfig, ProviderKind, TransportConfig};
//! use providers_kit::files::FileUploadRequest;
//! # #[tokio::main(flavor = "current_thread")]
//! # async fn main() -> Result<(), providers_kit::KitError> {
//! let provider = Provider::from_config(ProviderConfig::new(
//!     ProviderKind::OpenAi(OpenAiConfig::new(ApiKey::new("replace-with-your-key")?)),
//!     TransportConfig::default(),
//! ))?;
//! let uploaded = provider
//!     .upload_file(FileUploadRequest::new(
//!         "note.txt",
//!         "text/plain",
//!         b"owned note".to_vec(),
//!     )?)
//!     .await;
//! let work = async {
//!     let file = uploaded.as_ref().map_err(Clone::clone)?;
//!     let info = provider.file_info(file.id.clone()).await?;
//!     assert_eq!(info.id, file.id);
//!     let bytes = provider.download_file(file.id.clone(), NonZeroUsize::new(1024).unwrap()).await?;
//!     assert_eq!(bytes, b"owned note");
//!     Ok::<(), providers_kit::KitError>(())
//! }.await;
//! let deletion = match uploaded {
//!     Ok(file) => provider.delete_file(file.id).await,
//!     Err(_) => Ok(()),
//! };
//! let shutdown = provider.shutdown().await;
//! // Retain cleanup errors as supplementary evidence on an earlier failure.
//! let mut outcome = work;
//! for cleanup in [deletion, shutdown] {
//!     if let Err(error) = cleanup {
//!         outcome = Err(match outcome { Err(primary) => primary.with_supplementary(error), Ok(()) => error });
//!     }
//! }
//! outcome?;
//! # Ok(())
//! # }
//! ```
//!
//! Provider shutdown settles local resources only. Failed deletion remains a remote
//! cleanup obligation; it is not converted into success by shutdown.

use std::num::NonZeroUsize;

use reqwest::multipart::Part;
use serde::{Deserialize, Deserializer, Serialize};

use crate::KitError;
use crate::core::content::base64_bytes;
use crate::error::ValidationViolation;
use crate::generation::FileReference;

/// Validated provider-file upload request.
///
/// This owns bytes without uploading them. Submit it through [`crate::ProviderBackend::upload_file`]
/// and retain the returned lifecycle id for explicit owned-resource cleanup.
///
/// # Examples
///
/// ```
/// use providers_kit::files::FileUploadRequest;
/// let request = FileUploadRequest::new("notes.txt", "text/plain", b"notes".to_vec())?;
/// assert_eq!(request.file_name(), "notes.txt");
/// assert_eq!(request.media_type(), "text/plain");
/// assert_eq!(request.data(), b"notes");
///
/// # Ok::<(), providers_kit::KitError>(())
/// ```
///
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct FileUploadRequest {
    file_name: String,
    media_type: String,
    #[serde(with = "base64_bytes")]
    data: Vec<u8>,
}

impl FileUploadRequest {
    /// Creates a non-empty upload with a file name and media type.
    ///
    /// # Errors
    ///
    /// Returns an input error for an empty, padded, or control-containing file name; malformed or padded MIME type/parameters; or empty file bytes.
    ///
    /// See [`crate::files::FileUploadRequest`] for a complete example.
    ///
    pub fn new(
        file_name: impl Into<String>,
        media_type: impl Into<String>,
        data: Vec<u8>,
    ) -> Result<Self, KitError> {
        let file_name = file_name.into();
        let media_type = media_type.into();
        if file_name.is_empty()
            || file_name.trim() != file_name
            || file_name.chars().any(char::is_control)
        {
            return Err(KitError::invalid_input(
                ValidationViolation::InvalidFormat {
                    field: "file.name",
                    rule: "nonblank, unpadded, without control characters",
                },
                "upload file name must be nonblank, unpadded, and contain no control characters",
            ));
        }
        let parameters_valid = media_type.split(';').skip(1).all(|parameter| {
            parameter
                .split_once('=')
                .is_some_and(|(name, value)| !name.trim().is_empty() && !value.trim().is_empty())
        });
        if media_type.is_empty()
            || media_type.trim() != media_type
            || media_type.chars().any(char::is_control)
            || !parameters_valid
            || Part::bytes(Vec::new()).mime_str(&media_type).is_err()
        {
            return Err(KitError::invalid_input(
                ValidationViolation::InvalidFormat {
                    field: "file.media_type",
                    rule: "valid type/subtype",
                },
                "upload media type must be a valid type/subtype",
            ));
        }
        if data.is_empty() {
            return Err(KitError::invalid_input(
                ValidationViolation::Empty { field: "file.data" },
                "upload data must not be empty",
            ));
        }
        Ok(Self {
            file_name,
            media_type,
            data,
        })
    }

    #[must_use]
    /// Returns the file name presented to the provider.
    ///
    /// See [`crate::files::FileUploadRequest`] for a complete example.
    ///
    pub fn file_name(&self) -> &str {
        &self.file_name
    }

    #[must_use]
    /// Returns the declared media type.
    ///
    /// See [`crate::files::FileUploadRequest`] for a complete example.
    ///
    pub fn media_type(&self) -> &str {
        &self.media_type
    }

    #[must_use]
    /// Returns the raw file bytes.
    ///
    /// See [`crate::files::FileUploadRequest`] for a complete example.
    ///
    pub fn data(&self) -> &[u8] {
        &self.data
    }

    #[must_use]
    /// Consumes the request into its file name, media type, and bytes.
    ///
    /// # Examples
    ///
    /// ```
    /// use providers_kit::files::FileUploadRequest;
    /// let request = FileUploadRequest::new("notes.txt", "text/plain", b"notes".to_vec())?;
    /// let (name, media_type, bytes) = request.into_parts();
    /// assert_eq!(name, "notes.txt");
    /// assert_eq!(media_type, "text/plain");
    /// assert_eq!(bytes, b"notes");
    ///
    /// # Ok::<(), providers_kit::KitError>(())
    /// ```
    ///
    pub fn into_parts(self) -> (String, String, Vec<u8>) {
        (self.file_name, self.media_type, self.data)
    }
}

impl<'de> Deserialize<'de> for FileUploadRequest {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Deserialize)]
        #[serde(deny_unknown_fields)]
        struct Wire {
            file_name: String,
            media_type: String,
            #[serde(with = "base64_bytes")]
            data: Vec<u8>,
        }
        let wire = Wire::deserialize(deserializer)?;
        Self::new(wire.file_name, wire.media_type, wire.data).map_err(serde::de::Error::custom)
    }
}

/// Validated file-list pagination request.
///
/// Pagination cursors are opaque provider-owned values; a requested limit is not a
/// guarantee that every provider supports the same pagination controls.
///
/// # Examples
///
/// ```
/// use providers_kit::files::FileListRequest;
/// let request = FileListRequest::default()
///     .with_limit(std::num::NonZeroUsize::new(10).unwrap())
///     .with_cursor("opaque-next")?;
/// assert_eq!(request.limit().unwrap().get(), 10);
/// assert_eq!(request.cursor(), Some("opaque-next"));
///
/// # Ok::<(), providers_kit::KitError>(())
/// ```
///
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize)]
pub struct FileListRequest {
    limit: Option<NonZeroUsize>,
    cursor: Option<String>,
}

impl FileListRequest {
    /// Sets a non-zero result limit.
    ///
    /// See [`crate::files::FileListRequest`] for a complete example.
    ///
    #[must_use]
    pub fn with_limit(mut self, limit: NonZeroUsize) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Sets a non-empty provider-native continuation cursor.
    ///
    /// # Errors
    ///
    /// Returns an input error when the cursor is empty or whitespace-only. Nonblank cursor bytes are retained exactly; use only a cursor returned by the same provider operation.
    ///
    /// See [`crate::files::FileListRequest`] for a complete example.
    ///
    pub fn with_cursor(mut self, cursor: impl Into<String>) -> Result<Self, KitError> {
        let cursor = cursor.into();
        if cursor.trim().is_empty() {
            return Err(KitError::invalid_input(
                ValidationViolation::Empty {
                    field: "files.cursor",
                },
                "file cursor must not be empty",
            ));
        }
        self.cursor = Some(cursor);
        Ok(self)
    }

    /// Returns the requested result limit.
    ///
    /// See [`crate::files::FileListRequest`] for a complete example.
    ///
    #[must_use]
    pub const fn limit(&self) -> Option<NonZeroUsize> {
        self.limit
    }

    /// Returns the provider-native continuation cursor.
    ///
    /// See [`crate::files::FileListRequest`] for a complete example.
    ///
    #[must_use]
    pub fn cursor(&self) -> Option<&str> {
        self.cursor.as_deref()
    }

    /// Consumes the request into its limit and continuation cursor.
    ///
    /// # Examples
    ///
    /// ```
    /// use providers_kit::files::FileListRequest;
    /// let request = FileListRequest::default()
    ///     .with_limit(std::num::NonZeroUsize::new(10).unwrap())
    ///     .with_cursor("opaque-next")?;
    /// let (limit, cursor) = request.into_parts();
    /// assert_eq!(limit.unwrap().get(), 10);
    /// assert_eq!(cursor.as_deref(), Some("opaque-next"));
    ///
    /// # Ok::<(), providers_kit::KitError>(())
    /// ```
    ///
    #[must_use]
    pub fn into_parts(self) -> (Option<NonZeroUsize>, Option<String>) {
        (self.limit, self.cursor)
    }
}

impl<'de> Deserialize<'de> for FileListRequest {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Deserialize)]
        #[serde(deny_unknown_fields)]
        struct Wire {
            limit: Option<NonZeroUsize>,
            cursor: Option<String>,
        }
        let wire = Wire::deserialize(deserializer)?;
        let mut request = Self {
            limit: wire.limit,
            cursor: None,
        };
        if let Some(cursor) = wire.cursor {
            request = request
                .with_cursor(cursor)
                .map_err(serde::de::Error::custom)?;
        }
        Ok(request)
    }
}

/// Provider-managed file metadata.
///
/// The lifecycle `id` can differ from `input_reference`; use the returned generation
/// reference where supplied. Upload success does not establish processing readiness.
///
/// # Examples
///
/// ```
/// use providers_kit::files::{FileObject, FileState};
/// let file = FileObject {
///     id: "owned-file".into(),
///     file_name: Some("notes.txt".into()),
///     size_bytes: Some(5),
///     media_type: Some("text/plain".into()),
///     created_at: None,
///     state: Some(FileState::Processing),
///     input_reference: None,
/// };
/// assert!(matches!(file.state, Some(FileState::Processing)));
/// ```
///
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct FileObject {
    /// Exact provider-native lifecycle identifier.
    pub id: String,
    /// Provider-reported file name, when available.
    pub file_name: Option<String>,
    /// Provider-reported size in bytes, when available.
    pub size_bytes: Option<u64>,
    /// Provider-reported media type, when available.
    pub media_type: Option<String>,
    /// Provider-reported creation timestamp, when available.
    pub created_at: Option<String>,
    /// Current provider-authored lifecycle state, when available.
    pub state: Option<FileState>,
    /// Exact reference usable as generation input, when distinct from lifecycle `id`.
    pub input_reference: Option<FileReference>,
}

/// Provider-managed file lifecycle state.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "state", rename_all = "snake_case")]
pub enum FileState {
    /// The provider is processing the uploaded file.
    Processing,
    /// The file is ready for supported provider operations.
    Active,
    /// The provider failed to process the file.
    Failed {
        /// Provider error code, when available.
        code: Option<String>,
        /// Provider error message, when available.
        message: Option<String>,
    },
}

/// One page of provider-managed files.
///
/// # Examples
///
/// ```
/// let page = providers_kit::files::FilePage { files: Vec::new(), next_cursor: None };
/// assert!(page.files.is_empty());
/// assert!(page.next_cursor.is_none());
/// ```
///
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct FilePage {
    /// Files returned in this page.
    pub files: Vec<FileObject>,
    /// Provider-native cursor for a subsequent page.
    pub next_cursor: Option<String>,
}