async_openai/
chatkit.rs

1use crate::{
2    config::Config,
3    error::OpenAIError,
4    types::chatkit::{
5        ChatSessionResource, CreateChatSessionBody, DeletedThreadResource, ThreadItemListResource,
6        ThreadListResource, ThreadResource,
7    },
8    Client, RequestOptions,
9};
10
11/// ChatKit API for managing sessions and threads.
12///
13/// Related guide: [ChatKit](https://platform.openai.com/docs/api-reference/chatkit)
14pub struct Chatkit<'c, C: Config> {
15    client: &'c Client<C>,
16    pub(crate) request_options: RequestOptions,
17}
18
19impl<'c, C: Config> Chatkit<'c, C> {
20    pub fn new(client: &'c Client<C>) -> Self {
21        Self {
22            client,
23            request_options: RequestOptions::new(),
24        }
25    }
26
27    /// Access sessions API.
28    pub fn sessions(&self) -> ChatkitSessions<'_, C> {
29        ChatkitSessions::new(self.client)
30    }
31
32    /// Access threads API.
33    pub fn threads(&self) -> ChatkitThreads<'_, C> {
34        ChatkitThreads::new(self.client)
35    }
36}
37
38/// ChatKit sessions API.
39pub struct ChatkitSessions<'c, C: Config> {
40    client: &'c Client<C>,
41    pub(crate) request_options: RequestOptions,
42}
43
44impl<'c, C: Config> ChatkitSessions<'c, C> {
45    pub fn new(client: &'c Client<C>) -> Self {
46        Self {
47            client,
48            request_options: RequestOptions::new(),
49        }
50    }
51
52    /// Create a ChatKit session.
53    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
54    pub async fn create(
55        &self,
56        request: CreateChatSessionBody,
57    ) -> Result<ChatSessionResource, OpenAIError> {
58        self.client
59            .post("/chatkit/sessions", request, &self.request_options)
60            .await
61    }
62
63    /// Cancel a ChatKit session.
64    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
65    pub async fn cancel(&self, session_id: &str) -> Result<ChatSessionResource, OpenAIError> {
66        self.client
67            .post(
68                &format!("/chatkit/sessions/{session_id}/cancel"),
69                serde_json::json!({}),
70                &self.request_options,
71            )
72            .await
73    }
74}
75
76/// ChatKit threads API.
77pub struct ChatkitThreads<'c, C: Config> {
78    client: &'c Client<C>,
79    pub(crate) request_options: RequestOptions,
80}
81
82impl<'c, C: Config> ChatkitThreads<'c, C> {
83    pub fn new(client: &'c Client<C>) -> Self {
84        Self {
85            client,
86            request_options: RequestOptions::new(),
87        }
88    }
89
90    /// List ChatKit threads.
91    #[crate::byot(R = serde::de::DeserializeOwned)]
92    pub async fn list(&self) -> Result<ThreadListResource, OpenAIError> {
93        self.client
94            .get("/chatkit/threads", &self.request_options)
95            .await
96    }
97
98    /// Retrieve a ChatKit thread.
99    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
100    pub async fn retrieve(&self, thread_id: &str) -> Result<ThreadResource, OpenAIError> {
101        self.client
102            .get(
103                &format!("/chatkit/threads/{thread_id}"),
104                &self.request_options,
105            )
106            .await
107    }
108
109    /// Delete a ChatKit thread.
110    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
111    pub async fn delete(&self, thread_id: &str) -> Result<DeletedThreadResource, OpenAIError> {
112        self.client
113            .delete(
114                &format!("/chatkit/threads/{thread_id}"),
115                &self.request_options,
116            )
117            .await
118    }
119
120    /// List ChatKit thread items.
121    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
122    pub async fn list_items(&self, thread_id: &str) -> Result<ThreadItemListResource, OpenAIError> {
123        self.client
124            .get(
125                &format!("/chatkit/threads/{thread_id}/items"),
126                &self.request_options,
127            )
128            .await
129    }
130}