Skip to main content

openai_interface/conversations/items/
create.rs

1//! Add items to a conversation via
2//! `POST /conversations/{conversation_id}/items`.
3//!
4//! > ![warn] This module is untested!
5//! > If you encounter any issues, please report them on the repository.
6
7use serde::Serialize;
8use url::Url;
9
10use crate::{
11    conversations::items::ListConversationItemsResponse,
12    errors::OapiError,
13    rest::post::{Post, PostNoStream},
14};
15
16/// Adds items to a conversation.
17#[derive(Debug, Clone, Serialize)]
18pub struct CreateConversationItemsRequest<'a> {
19    /// The ID of the conversation, e.g. `conv_...`.
20    #[serde(skip)]
21    pub conversation_id: &'a str,
22    /// The items to add, as raw JSON (Responses-API input item
23    /// shapes).
24    pub items: Vec<serde_json::Value>,
25    /// Additional JSON properties flattened into the request body, for
26    /// fields not covered by the typed struct.
27    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
28    pub extra_body_map: Option<serde_json::Map<String, serde_json::Value>>,
29}
30
31impl Post for CreateConversationItemsRequest<'_> {
32    fn is_streaming(&self) -> bool {
33        false
34    }
35
36    /// Builds the URL for the request.
37    ///
38    /// `base_url` should be like <https://api.openai.com/v1>
39    fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
40        let mut url = Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
41        url.path_segments_mut()
42            .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
43            .push("conversations")
44            .push(self.conversation_id)
45            .push("items");
46        Ok(url.to_string())
47    }
48}
49
50impl PostNoStream for CreateConversationItemsRequest<'_> {
51    type Response = ListConversationItemsResponse;
52}