Skip to main content

openai_interface/vector_stores/files/
list.rs

1//! List the files of a vector store via
2//! `GET /vector_stores/{vector_store_id}/files`.
3//!
4//! > ![warn] This module is untested!
5//! > If you encounter any issues, please report them on the repository.
6
7use url::Url;
8
9use crate::{
10    errors::OapiError,
11    pagination::PaginationQuery,
12    rest::get::{Get, GetNoStream},
13    vector_stores::{VectorStoreFile, VectorStoreFileStatus},
14};
15
16/// Lists the files of a vector store.
17#[derive(Debug, Clone, Default)]
18pub struct ListVectorStoreFilesRequest<'a> {
19    /// The ID of the vector store, e.g. `vs_...`.
20    pub vector_store_id: &'a str,
21    /// The standard pagination parameters (`before`, `after`, `limit`,
22    /// `order`).
23    pub pagination: PaginationQuery<'a>,
24    /// Only return files with this processing status.
25    pub status: Option<VectorStoreFileStatus>,
26    /// Additional query parameters appended verbatim to the URL.
27    pub extra_query: Option<std::collections::HashMap<String, String>>,
28}
29
30impl ListVectorStoreFilesRequest<'_> {
31    fn status_str(status: VectorStoreFileStatus) -> &'static str {
32        match status {
33            VectorStoreFileStatus::InProgress => "in_progress",
34            VectorStoreFileStatus::Completed => "completed",
35            VectorStoreFileStatus::Failed => "failed",
36            VectorStoreFileStatus::Cancelled => "cancelled",
37        }
38    }
39}
40
41impl Get for ListVectorStoreFilesRequest<'_> {
42    /// Builds the URL for the request.
43    ///
44    /// `base_url` should be like <https://api.openai.com/v1>
45    fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
46        let mut url = Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
47        url.path_segments_mut()
48            .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
49            .push("vector_stores")
50            .push(self.vector_store_id)
51            .push("files");
52
53        let mut touched = false;
54        {
55            let mut pairs = url.query_pairs_mut();
56            if self.pagination.any_set() {
57                self.pagination.append_to(&mut pairs);
58                touched = true;
59            }
60            if let Some(status) = self.status {
61                pairs.append_pair("status", Self::status_str(status));
62                touched = true;
63            }
64            if let Some(extra_query) = &self.extra_query {
65                for (key, value) in extra_query {
66                    pairs.append_pair(key, value);
67                }
68                touched = true;
69            }
70        }
71        if !touched {
72            url.set_query(None);
73        }
74
75        Ok(url.to_string())
76    }
77}
78
79impl GetNoStream for ListVectorStoreFilesRequest<'_> {
80    type Response = ListVectorStoreFilesResponse;
81}
82
83/// The response of listing a store's files.
84#[derive(Debug, Clone, serde::Deserialize)]
85pub struct ListVectorStoreFilesResponse {
86    /// The file associations on this page.
87    #[serde(default)]
88    pub data: Vec<VectorStoreFile>,
89    /// Whether more files exist after this page.
90    #[serde(default)]
91    pub has_more: Option<bool>,
92    /// The ID of the first file on the page, for cursor pagination.
93    #[serde(default)]
94    pub first_id: Option<String>,
95    /// The ID of the last file on the page, for cursor pagination.
96    #[serde(default)]
97    pub last_id: Option<String>,
98    /// The object type (`list`), if the provider sends it.
99    #[serde(default)]
100    pub object: Option<String>,
101}
102
103crate::impl_from_str!(ListVectorStoreFilesResponse);