Skip to main content

outfox_openai/responses/
conversations.rs

1use crate::config::Config;
2use crate::error::OpenAIError;
3use crate::spec::responses::{
4    ConversationResource, CreateConversationRequest, DeleteConversationResponse,
5    UpdateConversationRequest,
6};
7use crate::{Client, ConversationItems, RequestOptions};
8
9pub struct Conversations<'c, C: Config> {
10    client: &'c Client<C>,
11    pub(crate) request_options: RequestOptions,
12}
13
14impl<'c, C: Config> Conversations<'c, C> {
15    pub fn new(client: &'c Client<C>) -> Self {
16        Self {
17            client,
18            request_options: RequestOptions::new(),
19        }
20    }
21
22    /// [ConversationItems] API group
23    pub fn items(&self, conversation_id: &str) -> ConversationItems<'_, C> {
24        ConversationItems::new(self.client, conversation_id)
25    }
26
27    /// Create a conversation.
28    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
29    pub async fn create(
30        &self,
31        request: CreateConversationRequest,
32    ) -> Result<ConversationResource, OpenAIError> {
33        self.client
34            .post("/conversations", request, &self.request_options)
35            .await
36    }
37
38    /// Retrieves a conversation.
39    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
40    pub async fn retrieve(
41        &self,
42        conversation_id: &str,
43    ) -> Result<ConversationResource, OpenAIError> {
44        self.client
45            .get(
46                &format!("/conversations/{conversation_id}"),
47                &self.request_options,
48            )
49            .await
50    }
51
52    /// Delete a conversation. Items in the conversation will not be deleted.
53    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
54    pub async fn delete(
55        &self,
56        conversation_id: &str,
57    ) -> Result<DeleteConversationResponse, OpenAIError> {
58        self.client
59            .delete(
60                &format!("/conversations/{conversation_id}"),
61                &self.request_options,
62            )
63            .await
64    }
65
66    /// Update a conversation.
67    #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
68    pub async fn update(
69        &self,
70        conversation_id: &str,
71        request: UpdateConversationRequest,
72    ) -> Result<ConversationResource, OpenAIError> {
73        self.client
74            .post(
75                &format!("/conversations/{conversation_id}"),
76                request,
77                &self.request_options,
78            )
79            .await
80    }
81}