Skip to main content

openai_interface/responses/
input_items.rs

1//! List the input items of a stored response via
2//! `GET /responses/{response_id}/input_items`.
3//!
4//! > ![warn] This module is untested!
5//! > If you encounter any issues, please report them on the repository.
6//!
7//! Only responses created with `store: true` have their input items
8//! recorded. Provider support varies: Qwen's Responses API documents this
9//! endpoint, while DeepSeek's Responses API is stateless and stores
10//! nothing.
11
12use url::Url;
13
14use crate::{
15    errors::OapiError,
16    rest::get::{Get, GetNoStream},
17};
18
19/// Lists the input items of a previously created response by its ID.
20#[derive(Debug, Clone, Default)]
21pub struct ListInputItemsRequest<'a> {
22    /// The ID of the response whose input items to list, e.g. `resp_...`.
23    pub response_id: &'a str,
24    /// An item ID to list items after, used in pagination.
25    pub after: Option<&'a str>,
26    /// An item ID to list items before, used in pagination.
27    pub before: Option<&'a str>,
28    /// A limit on the number of items to be returned (1-100, default 20).
29    pub limit: Option<u32>,
30    /// The order to return the input items in (`asc` or `desc`).
31    pub order: Option<InputItemsOrder>,
32    /// Additional query parameters appended verbatim to the URL.
33    pub extra_query: Option<std::collections::HashMap<String, String>>,
34}
35
36/// The order to return the input items in.
37#[derive(Debug, Clone, Copy, PartialEq, Eq)]
38pub enum InputItemsOrder {
39    /// Ascending order (oldest first).
40    Asc,
41    /// Descending order (newest first, the default).
42    Desc,
43}
44
45impl InputItemsOrder {
46    fn as_str(self) -> &'static str {
47        match self {
48            InputItemsOrder::Asc => "asc",
49            InputItemsOrder::Desc => "desc",
50        }
51    }
52}
53
54impl Get for ListInputItemsRequest<'_> {
55    /// Builds the URL for the request.
56    ///
57    /// `base_url` should be like <https://api.openai.com/v1>
58    fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
59        let mut url = Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
60        url.path_segments_mut()
61            .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
62            .push("responses")
63            .push(self.response_id)
64            .push("input_items");
65
66        let mut query_pairs: Vec<(&str, &str)> = Vec::new();
67        if let Some(after) = self.after {
68            query_pairs.push(("after", after));
69        }
70        if let Some(before) = self.before {
71            query_pairs.push(("before", before));
72        }
73        if let Some(limit) = self.limit {
74            query_pairs.push(("limit", Box::leak(limit.to_string().into_boxed_str())));
75        }
76        if let Some(order) = self.order {
77            query_pairs.push(("order", order.as_str()));
78        }
79
80        if !query_pairs.is_empty() {
81            {
82                let mut pairs = url.query_pairs_mut();
83                for (key, value) in query_pairs {
84                    pairs.append_pair(key, value);
85                }
86            }
87        }
88        if let Some(extra_query) = &self.extra_query {
89            let mut pairs = url.query_pairs_mut();
90            for (key, value) in extra_query {
91                pairs.append_pair(key, value);
92            }
93        }
94
95        Ok(url.to_string())
96    }
97}
98
99impl GetNoStream for ListInputItemsRequest<'_> {
100    type Response = ListResponseInputItems;
101}
102
103/// The response of listing a response's input items.
104#[derive(Debug, Clone, serde::Deserialize)]
105pub struct ListResponseInputItems {
106    /// The list of input items. Item types unknown to this crate are
107    /// preserved as
108    /// [`ResponseOutputItem::Other`](crate::responses::ResponseOutputItem).
109    #[serde(default)]
110    pub data: Vec<crate::responses::ResponseOutputItem>,
111    /// Whether there are more items available.
112    #[serde(default)]
113    pub has_more: Option<bool>,
114    /// The ID of the first item in this list, when more items exist.
115    #[serde(default)]
116    pub first_id: Option<String>,
117    /// The object type (`list`), if the provider sends it.
118    #[serde(default)]
119    pub object: Option<String>,
120}
121
122crate::impl_from_str!(ListResponseInputItems);