Skip to main content

floopy/types/
batches.rs

1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4
5/// Per-batch progress summary.
6#[derive(Debug, Clone, Deserialize)]
7pub struct BatchRequestCounts {
8    /// Total requests in the batch.
9    #[serde(default)]
10    pub total: Option<i64>,
11    /// Requests that completed successfully.
12    #[serde(default)]
13    pub completed: Option<i64>,
14    /// Requests that failed.
15    #[serde(default)]
16    pub failed: Option<i64>,
17}
18
19/// An asynchronous batch job. Mirrors the OpenAI batch object; the gateway
20/// forwards batch traffic verbatim.
21#[derive(Debug, Clone, Deserialize)]
22pub struct Batch {
23    /// Provider-issued batch id.
24    pub id: String,
25    /// Object type (usually `"batch"`).
26    #[serde(default)]
27    pub object: Option<String>,
28    /// The endpoint the batch targets.
29    #[serde(default)]
30    pub endpoint: Option<String>,
31    /// Lifecycle status.
32    #[serde(default)]
33    pub status: Option<String>,
34    /// Input file id.
35    #[serde(default)]
36    pub input_file_id: Option<String>,
37    /// Output file id (set when completed).
38    #[serde(default)]
39    pub output_file_id: Option<String>,
40    /// Error file id (set when there were failures).
41    #[serde(default)]
42    pub error_file_id: Option<String>,
43    /// Unix creation timestamp.
44    #[serde(default)]
45    pub created_at: Option<i64>,
46    /// Unix completion timestamp.
47    #[serde(default)]
48    pub completed_at: Option<i64>,
49    /// Progress counts.
50    #[serde(default)]
51    pub request_counts: Option<BatchRequestCounts>,
52}
53
54/// Response of [`crate::resources::Batches::list`].
55#[derive(Debug, Clone, Deserialize)]
56pub struct BatchList {
57    /// Object type (usually `"list"`).
58    #[serde(default)]
59    pub object: Option<String>,
60    /// The batches.
61    #[serde(default)]
62    pub data: Vec<Batch>,
63    /// Whether more pages are available.
64    #[serde(default)]
65    pub has_more: Option<bool>,
66}
67
68/// Arguments for [`crate::resources::Batches::create`].
69#[derive(Debug, Clone, Serialize)]
70pub struct BatchCreateParams {
71    /// Id of a previously uploaded input file.
72    pub input_file_id: String,
73    /// The endpoint each line targets (e.g. `/v1/chat/completions`).
74    pub endpoint: String,
75    /// Completion window (e.g. `"24h"`).
76    pub completion_window: String,
77    /// Optional free-form metadata.
78    #[serde(skip_serializing_if = "Option::is_none")]
79    pub metadata: Option<HashMap<String, String>>,
80}
81
82/// Filters for [`crate::resources::Batches::list`].
83#[derive(Debug, Clone, Default)]
84pub struct BatchListParams {
85    /// Page size.
86    pub limit: Option<u32>,
87    /// Cursor: return batches after this id.
88    pub after: Option<String>,
89}
90
91impl BatchListParams {
92    pub(crate) fn query(&self) -> Vec<(String, String)> {
93        let mut q = Vec::new();
94        if let Some(l) = self.limit {
95            q.push(("limit".to_owned(), l.to_string()));
96        }
97        if let Some(a) = &self.after {
98            q.push(("after".to_owned(), a.clone()));
99        }
100        q
101    }
102}