use crate::{
config::Config,
error::OpenAIError,
types::chatkit::{
ChatSessionResource, CreateChatSessionBody, DeletedThreadResource, ThreadItemListResource,
ThreadListResource, ThreadResource,
},
Client, RequestOptions,
};
pub struct Chatkit<'c, C: Config> {
client: &'c Client<C>,
pub(crate) request_options: RequestOptions,
}
impl<'c, C: Config> Chatkit<'c, C> {
pub fn new(client: &'c Client<C>) -> Self {
Self {
client,
request_options: RequestOptions::new(),
}
}
pub fn sessions(&self) -> ChatkitSessions<'_, C> {
ChatkitSessions::new(self.client)
}
pub fn threads(&self) -> ChatkitThreads<'_, C> {
ChatkitThreads::new(self.client)
}
}
pub struct ChatkitSessions<'c, C: Config> {
client: &'c Client<C>,
pub(crate) request_options: RequestOptions,
}
impl<'c, C: Config> ChatkitSessions<'c, C> {
pub fn new(client: &'c Client<C>) -> Self {
Self {
client,
request_options: RequestOptions::new(),
}
}
#[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
pub async fn create(
&self,
request: CreateChatSessionBody,
) -> Result<ChatSessionResource, OpenAIError> {
self.client
.post("/chatkit/sessions", request, &self.request_options)
.await
}
#[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
pub async fn cancel(&self, session_id: &str) -> Result<ChatSessionResource, OpenAIError> {
self.client
.post(
&format!("/chatkit/sessions/{session_id}/cancel"),
serde_json::json!({}),
&self.request_options,
)
.await
}
}
pub struct ChatkitThreads<'c, C: Config> {
client: &'c Client<C>,
pub(crate) request_options: RequestOptions,
}
impl<'c, C: Config> ChatkitThreads<'c, C> {
pub fn new(client: &'c Client<C>) -> Self {
Self {
client,
request_options: RequestOptions::new(),
}
}
#[crate::byot(R = serde::de::DeserializeOwned)]
pub async fn list(&self) -> Result<ThreadListResource, OpenAIError> {
self.client
.get("/chatkit/threads", &self.request_options)
.await
}
#[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
pub async fn retrieve(&self, thread_id: &str) -> Result<ThreadResource, OpenAIError> {
self.client
.get(
&format!("/chatkit/threads/{thread_id}"),
&self.request_options,
)
.await
}
#[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
pub async fn delete(&self, thread_id: &str) -> Result<DeletedThreadResource, OpenAIError> {
self.client
.delete(
&format!("/chatkit/threads/{thread_id}"),
&self.request_options,
)
.await
}
#[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
pub async fn list_items(&self, thread_id: &str) -> Result<ThreadItemListResource, OpenAIError> {
self.client
.get(
&format!("/chatkit/threads/{thread_id}/items"),
&self.request_options,
)
.await
}
}