use crate::{
config::Config,
error::OpenAIError,
types::assistants::{
CreateThreadAndRunRequest, CreateThreadRequest, DeleteThreadResponse, ModifyThreadRequest,
RunObject, ThreadObject,
},
Client, Messages, RequestOptions, Runs,
};
#[cfg(not(target_family = "wasm"))]
use crate::types::assistants::AssistantEventStream;
#[deprecated(
note = "Assistants API is deprecated and will be removed in August 2026. Use the Responses API."
)]
pub struct Threads<'c, C: Config> {
client: &'c Client<C>,
pub(crate) request_options: RequestOptions,
}
impl<'c, C: Config> Threads<'c, C> {
pub fn new(client: &'c Client<C>) -> Self {
Self {
client,
request_options: RequestOptions::new(),
}
}
pub fn messages(&self, thread_id: &str) -> Messages<'_, C> {
Messages::new(self.client, thread_id)
}
pub fn runs(&self, thread_id: &str) -> Runs<'_, C> {
Runs::new(self.client, thread_id)
}
#[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
pub async fn create_and_run(
&self,
request: CreateThreadAndRunRequest,
) -> Result<RunObject, OpenAIError> {
self.client
.post("/threads/runs", request, &self.request_options)
.await
}
#[cfg(not(target_family = "wasm"))]
#[crate::byot(
T0 = serde::Serialize,
R = serde::de::DeserializeOwned,
stream = "true",
where_clause = "R: std::marker::Send + 'static + TryFrom<eventsource_stream::Event, Error = OpenAIError>"
)]
#[allow(unused_mut)]
pub async fn create_and_run_stream(
&self,
mut request: CreateThreadAndRunRequest,
) -> Result<AssistantEventStream, OpenAIError> {
#[cfg(not(feature = "byot"))]
{
if request.stream.is_some() && !request.stream.unwrap() {
return Err(OpenAIError::InvalidArgument(
"When stream is false, use Threads::create_and_run".into(),
));
}
request.stream = Some(true);
}
Ok(self
.client
.post_stream_mapped_raw_events(
"/threads/runs",
request,
&self.request_options,
TryFrom::try_from,
)
.await)
}
#[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
pub async fn create(&self, request: CreateThreadRequest) -> Result<ThreadObject, OpenAIError> {
self.client
.post("/threads", request, &self.request_options)
.await
}
#[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
pub async fn retrieve(&self, thread_id: &str) -> Result<ThreadObject, OpenAIError> {
self.client
.get(&format!("/threads/{thread_id}"), &self.request_options)
.await
}
#[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
pub async fn update(
&self,
thread_id: &str,
request: ModifyThreadRequest,
) -> Result<ThreadObject, OpenAIError> {
self.client
.post(
&format!("/threads/{thread_id}"),
request,
&self.request_options,
)
.await
}
#[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
pub async fn delete(&self, thread_id: &str) -> Result<DeleteThreadResponse, OpenAIError> {
self.client
.delete(&format!("/threads/{thread_id}"), &self.request_options)
.await
}
}