1use std::fmt;
2use std::path::{Path, PathBuf};
3
4use bytes::Bytes;
5use tokio::io::AsyncRead;
6
7use crate::models::{Document, VersionRef};
8
9pub struct FileInput {
27 pub(crate) source: Source,
28 pub(crate) filename: Option<String>,
29 pub(crate) content_type: Option<String>,
30 pub(crate) sha256: Option<String>,
31}
32
33pub(crate) enum Source {
34 Path(PathBuf),
35 Bytes(Bytes),
36 File {
37 file: tokio::fs::File,
38 size: Option<u64>,
39 },
40 Reader {
41 reader: Box<dyn AsyncRead + Send + Unpin>,
42 size: u64,
43 },
44 Document(VersionRef),
45}
46
47impl fmt::Debug for FileInput {
48 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49 let source = match &self.source {
50 Source::Path(path) => format!("Path({})", path.display()),
51 Source::Bytes(bytes) => format!("Bytes({} bytes)", bytes.len()),
52 Source::File { size, .. } => format!("File({size:?} bytes)"),
53 Source::Reader { size, .. } => format!("Reader({size} bytes)"),
54 Source::Document(reference) => format!("Document({})", reference.document_id),
55 };
56 f.debug_struct("FileInput")
57 .field("source", &source)
58 .field("filename", &self.filename)
59 .field("content_type", &self.content_type)
60 .finish()
61 }
62}
63
64impl FileInput {
65 fn new(source: Source, filename: Option<String>) -> Self {
66 FileInput {
67 source,
68 filename,
69 content_type: None,
70 sha256: None,
71 }
72 }
73
74 pub fn path(path: impl AsRef<Path>) -> Self {
76 Self::new(Source::Path(path.as_ref().to_path_buf()), None)
77 }
78
79 pub fn bytes(data: impl Into<Bytes>, filename: impl Into<String>) -> Self {
81 Self::new(Source::Bytes(data.into()), Some(filename.into()))
82 }
83
84 pub fn file(file: tokio::fs::File, filename: impl Into<String>) -> Self {
86 Self::new(Source::File { file, size: None }, Some(filename.into()))
87 }
88
89 pub fn reader(
93 reader: impl AsyncRead + Send + Unpin + 'static,
94 size: u64,
95 filename: impl Into<String>,
96 ) -> Self {
97 Self::new(
98 Source::Reader {
99 reader: Box::new(reader),
100 size,
101 },
102 Some(filename.into()),
103 )
104 }
105
106 pub fn document(document_id: impl Into<String>) -> Self {
108 Self::new(
109 Source::Document(VersionRef {
110 document_id: document_id.into(),
111 version_id: None,
112 }),
113 None,
114 )
115 }
116
117 pub fn version(document_id: impl Into<String>, version_id: impl Into<String>) -> Self {
119 Self::new(
120 Source::Document(VersionRef {
121 document_id: document_id.into(),
122 version_id: Some(version_id.into()),
123 }),
124 None,
125 )
126 }
127
128 pub fn with_filename(mut self, filename: impl Into<String>) -> Self {
130 self.filename = Some(filename.into());
131 self
132 }
133
134 pub fn with_content_type(mut self, content_type: impl Into<String>) -> Self {
136 self.content_type = Some(content_type.into());
137 self
138 }
139
140 pub fn with_sha256(mut self, sha256: impl Into<String>) -> Self {
143 self.sha256 = Some(sha256.into());
144 self
145 }
146
147 pub fn is_document(&self) -> bool {
149 matches!(self.source, Source::Document(_))
150 }
151
152 pub(crate) fn resolved_filename(&self) -> String {
154 if let Some(name) = &self.filename {
155 return name.clone();
156 }
157 match &self.source {
158 Source::Path(path) => path
159 .file_name()
160 .map(|name| name.to_string_lossy().into_owned())
161 .unwrap_or_else(|| "upload".into()),
162 _ => "upload".into(),
163 }
164 }
165
166 pub(crate) fn resolved_content_type(&self) -> String {
167 self.content_type
168 .clone()
169 .unwrap_or_else(|| crate::util::content_type_for(&self.resolved_filename()).to_owned())
170 }
171
172 pub(crate) fn stem(&self) -> Option<String> {
174 match &self.source {
175 Source::Document(_) => None,
176 _ => {
177 let name = self.resolved_filename();
178 Some(
179 name.rsplit_once('.')
180 .map(|(stem, _)| stem.to_owned())
181 .unwrap_or(name),
182 )
183 }
184 }
185 }
186}
187
188impl From<&str> for FileInput {
189 fn from(path: &str) -> Self {
190 FileInput::path(path)
191 }
192}
193
194impl From<String> for FileInput {
195 fn from(path: String) -> Self {
196 FileInput::path(path)
197 }
198}
199
200impl From<&String> for FileInput {
201 fn from(path: &String) -> Self {
202 FileInput::path(path)
203 }
204}
205
206impl From<&Path> for FileInput {
207 fn from(path: &Path) -> Self {
208 FileInput::path(path)
209 }
210}
211
212impl From<PathBuf> for FileInput {
213 fn from(path: PathBuf) -> Self {
214 FileInput::path(path)
215 }
216}
217
218impl From<&PathBuf> for FileInput {
219 fn from(path: &PathBuf) -> Self {
220 FileInput::path(path)
221 }
222}
223
224impl From<&Document> for FileInput {
225 fn from(document: &Document) -> Self {
226 match document.current_version_id() {
227 Some(version) => FileInput::version(document.id.clone(), version),
228 None => FileInput::document(document.id.clone()),
229 }
230 }
231}
232
233impl From<Document> for FileInput {
234 fn from(document: Document) -> Self {
235 FileInput::from(&document)
236 }
237}
238
239impl From<VersionRef> for FileInput {
240 fn from(reference: VersionRef) -> Self {
241 FileInput::new(Source::Document(reference), None)
242 }
243}
244
245#[derive(Debug, Default)]
248pub struct ToolInput {
249 pub(crate) files: Vec<FileInput>,
250}
251
252impl ToolInput {
253 pub fn many<I, F>(files: I) -> Self
255 where
256 I: IntoIterator<Item = F>,
257 F: Into<FileInput>,
258 {
259 ToolInput {
260 files: files.into_iter().map(Into::into).collect(),
261 }
262 }
263
264 pub fn len(&self) -> usize {
266 self.files.len()
267 }
268
269 pub fn is_empty(&self) -> bool {
271 self.files.is_empty()
272 }
273}
274
275macro_rules! single_input {
276 ($($ty:ty),*) => {
277 $(impl From<$ty> for ToolInput {
278 fn from(value: $ty) -> Self {
279 ToolInput { files: vec![FileInput::from(value)] }
280 }
281 })*
282 };
283}
284
285single_input!(&str, String, &String, &Path, PathBuf, &PathBuf, &Document, Document, VersionRef);
286
287impl From<FileInput> for ToolInput {
288 fn from(file: FileInput) -> Self {
289 ToolInput { files: vec![file] }
290 }
291}
292
293impl<F: Into<FileInput>> From<Vec<F>> for ToolInput {
294 fn from(files: Vec<F>) -> Self {
295 ToolInput::many(files)
296 }
297}
298
299impl<F: Into<FileInput>, const N: usize> From<[F; N]> for ToolInput {
300 fn from(files: [F; N]) -> Self {
301 ToolInput::many(files)
302 }
303}