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
11pub 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 pub fn sessions(&self) -> ChatkitSessions<'_, C> {
29 ChatkitSessions::new(self.client)
30 }
31
32 pub fn threads(&self) -> ChatkitThreads<'_, C> {
34 ChatkitThreads::new(self.client)
35 }
36}
37
38pub 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 #[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 #[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
76pub 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 #[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 #[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 #[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 #[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}