Skip to main content

openai_interface/conversations/
list.rs

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