async_openai/responses/
conversation_items.rs

1use crate::{
2    config::Config,
3    error::OpenAIError,
4    types::responses::{
5        ConversationItem, ConversationItemList, ConversationResource,
6        CreateConversationItemsRequest,
7    },
8    Client, RequestOptions,
9};
10
11/// Conversation items represent items within a conversation.
12pub struct ConversationItems<'c, C: Config> {
13    client: &'c Client<C>,
14    pub conversation_id: String,
15    pub(crate) request_options: RequestOptions,
16}
17
18impl<'c, C: Config> ConversationItems<'c, C> {
19    pub fn new(client: &'c Client<C>, conversation_id: &str) -> Self {
20        Self {
21            client,
22            conversation_id: conversation_id.into(),
23            request_options: RequestOptions::new(),
24        }
25    }
26
27    /// Create items in a conversation.
28    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
29    pub async fn create(
30        &self,
31        request: CreateConversationItemsRequest,
32    ) -> Result<ConversationItemList, OpenAIError> {
33        self.client
34            .post(
35                &format!("/conversations/{}/items", &self.conversation_id),
36                request,
37                &self.request_options,
38            )
39            .await
40    }
41
42    /// List all items for a conversation.
43    #[crate::byot(R = serde::de::DeserializeOwned)]
44    pub async fn list(&self) -> Result<ConversationItemList, OpenAIError> {
45        self.client
46            .get(
47                &format!("/conversations/{}/items", &self.conversation_id),
48                &self.request_options,
49            )
50            .await
51    }
52
53    /// Retrieve an item from a conversation.
54    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
55    pub async fn retrieve(&self, item_id: &str) -> Result<ConversationItem, OpenAIError> {
56        self.client
57            .get(
58                &format!("/conversations/{}/items/{item_id}", &self.conversation_id),
59                &self.request_options,
60            )
61            .await
62    }
63
64    /// Delete an item from a conversation.
65    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
66    pub async fn delete(&self, item_id: &str) -> Result<ConversationResource, OpenAIError> {
67        self.client
68            .delete(
69                &format!("/conversations/{}/items/{item_id}", &self.conversation_id),
70                &self.request_options,
71            )
72            .await
73    }
74}