openai-interface 0.10.0

A low-level Rust interface for the OpenAI API
Documentation
//! Manage the items of a conversation via
//! `/conversations/{conversation_id}/items`.
//!
//! > ![warn] This module is untested!
//! > If you encounter any issues, please report them on the repository.
//!
//! Items use the same shapes as Responses API input/output items, so
//! the list response reuses
//! [`ResponseOutputItem`](crate::responses::ResponseOutputItem).
//! Submodules: [`create`], [`retrieve`] and [`delete`]; the list
//! endpoint lives directly in this module.

pub mod create;
pub mod delete;
pub mod retrieve;

use url::Url;

use crate::{
    errors::OapiError,
    pagination::PaginationQuery,
    responses::ResponseOutputItem,
    rest::get::{Get, GetNoStream},
};

/// Lists the items of a conversation.
#[derive(Debug, Clone, Default)]
pub struct ListConversationItemsRequest<'a> {
    /// The ID of the conversation, e.g. `conv_...`.
    pub conversation_id: &'a str,
    /// The standard pagination parameters (`before`, `after`, `limit`,
    /// `order`).
    pub pagination: PaginationQuery<'a>,
    /// Additional query parameters appended verbatim to the URL.
    pub extra_query: Option<std::collections::HashMap<String, String>>,
}

impl Get for ListConversationItemsRequest<'_> {
    /// 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("conversations")
            .push(self.conversation_id)
            .push("items");

        let mut touched = false;
        {
            let mut pairs = url.query_pairs_mut();
            if self.pagination.any_set() {
                self.pagination.append_to(&mut pairs);
                touched = true;
            }
            if let Some(extra_query) = &self.extra_query {
                for (key, value) in extra_query {
                    pairs.append_pair(key, value);
                }
                touched = true;
            }
        }
        if !touched {
            url.set_query(None);
        }

        Ok(url.to_string())
    }
}

impl GetNoStream for ListConversationItemsRequest<'_> {
    type Response = ListConversationItemsResponse;
}

/// A single conversation item.
///
/// A thin wrapper over
/// [`ResponseOutputItem`](crate::responses::ResponseOutputItem) so the
/// retrieve endpoint can deserialize any item type (unknown types are
/// preserved as `ResponseOutputItem::Other`).
#[derive(Debug, Clone, serde::Deserialize)]
pub struct ConversationItem(pub ResponseOutputItem);

crate::impl_from_str!(ConversationItem);

/// The response of listing a conversation's items.
#[derive(Debug, Clone, serde::Deserialize)]
pub struct ListConversationItemsResponse {
    /// The items on this page. Item types unknown to this crate are
    /// preserved as [`ResponseOutputItem::Other`].
    #[serde(default)]
    pub data: Vec<ResponseOutputItem>,
    /// Whether more items exist after this page.
    #[serde(default)]
    pub has_more: Option<bool>,
    /// The ID of the first item on the page, for cursor pagination.
    #[serde(default)]
    pub first_id: Option<String>,
    /// The ID of the last item on the page, for cursor pagination.
    #[serde(default)]
    pub last_id: Option<String>,
    /// The object type (`list`), if the provider sends it.
    #[serde(default)]
    pub object: Option<String>,
}

crate::impl_from_str!(ListConversationItemsResponse);