use std::sync::Arc;
use crate::{
error::{LlmrixError, Result},
model::{ChatRequest, HitlDecideRequest, HitlDecision},
streaming::event::StreamEvent,
transport::*,
};
pub struct ChatResource {
pub(crate) t: Arc<Transport>,
pub(crate) conv_id: String,
}
impl ChatResource {
pub async fn send<F>(&self, message: &str, handler: F) -> Result<()>
where
F: FnMut(&StreamEvent) -> Result<()>,
{
self.send_request(ChatRequest { message: message.to_string(), ..Default::default() }, handler)
.await
}
pub async fn send_request<F>(&self, req: ChatRequest, handler: F) -> Result<()>
where
F: FnMut(&StreamEvent) -> Result<()>,
{
self.t.stream(&path_chat(&self.conv_id), &req, handler).await
}
pub async fn stop(&self) -> Result<()> {
self.t.post_no_body(&path_chat_stop(&self.conv_id)).await
}
pub async fn decide(&self, decisions: Vec<HitlDecision>) -> Result<()> {
if decisions.is_empty() {
return Err(LlmrixError::Other("decisions must not be empty".into()));
}
self.t
.post_void(&path_chat_decide(&self.conv_id), &HitlDecideRequest { decisions })
.await
}
}