openai-interface 0.10.0

A low-level Rust interface for the OpenAI API
Documentation
//! List the input items of a stored response via
//! `GET /responses/{response_id}/input_items`.
//!
//! > ![warn] This module is untested!
//! > If you encounter any issues, please report them on the repository.
//!
//! Only responses created with `store: true` have their input items
//! recorded. Provider support varies: Qwen's Responses API documents this
//! endpoint, while DeepSeek's Responses API is stateless and stores
//! nothing.

use url::Url;

use crate::{
    errors::OapiError,
    rest::get::{Get, GetNoStream},
};

/// Lists the input items of a previously created response by its ID.
#[derive(Debug, Clone, Default)]
pub struct ListInputItemsRequest<'a> {
    /// The ID of the response whose input items to list, e.g. `resp_...`.
    pub response_id: &'a str,
    /// An item ID to list items after, used in pagination.
    pub after: Option<&'a str>,
    /// An item ID to list items before, used in pagination.
    pub before: Option<&'a str>,
    /// A limit on the number of items to be returned (1-100, default 20).
    pub limit: Option<u32>,
    /// The order to return the input items in (`asc` or `desc`).
    pub order: Option<InputItemsOrder>,
    /// Additional query parameters appended verbatim to the URL.
    pub extra_query: Option<std::collections::HashMap<String, String>>,
}

/// The order to return the input items in.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InputItemsOrder {
    /// Ascending order (oldest first).
    Asc,
    /// Descending order (newest first, the default).
    Desc,
}

impl InputItemsOrder {
    fn as_str(self) -> &'static str {
        match self {
            InputItemsOrder::Asc => "asc",
            InputItemsOrder::Desc => "desc",
        }
    }
}

impl Get for ListInputItemsRequest<'_> {
    /// Builds the URL for the request.
    ///
    /// `base_url` should be like <https://api.openai.com/v1>
    fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
        let mut url = Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
        url.path_segments_mut()
            .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
            .push("responses")
            .push(self.response_id)
            .push("input_items");

        let mut query_pairs: Vec<(&str, &str)> = Vec::new();
        if let Some(after) = self.after {
            query_pairs.push(("after", after));
        }
        if let Some(before) = self.before {
            query_pairs.push(("before", before));
        }
        if let Some(limit) = self.limit {
            query_pairs.push(("limit", Box::leak(limit.to_string().into_boxed_str())));
        }
        if let Some(order) = self.order {
            query_pairs.push(("order", order.as_str()));
        }

        if !query_pairs.is_empty() {
            {
                let mut pairs = url.query_pairs_mut();
                for (key, value) in query_pairs {
                    pairs.append_pair(key, value);
                }
            }
        }
        if let Some(extra_query) = &self.extra_query {
            let mut pairs = url.query_pairs_mut();
            for (key, value) in extra_query {
                pairs.append_pair(key, value);
            }
        }

        Ok(url.to_string())
    }
}

impl GetNoStream for ListInputItemsRequest<'_> {
    type Response = ListResponseInputItems;
}

/// The response of listing a response's input items.
#[derive(Debug, Clone, serde::Deserialize)]
pub struct ListResponseInputItems {
    /// The list of input items. Item types unknown to this crate are
    /// preserved as
    /// [`ResponseOutputItem::Other`](crate::responses::ResponseOutputItem).
    #[serde(default)]
    pub data: Vec<crate::responses::ResponseOutputItem>,
    /// Whether there are more items available.
    #[serde(default)]
    pub has_more: Option<bool>,
    /// The ID of the first item in this list, when more items exist.
    #[serde(default)]
    pub first_id: Option<String>,
    /// The object type (`list`), if the provider sends it.
    #[serde(default)]
    pub object: Option<String>,
}

crate::impl_from_str!(ListResponseInputItems);