async_openai/
chatkit.rs

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