use std::sync::Arc;
#[cfg(test)]
use mockall::automock;
use swiftide::chat_completion;
use uuid::Uuid;
#[derive(Debug, Clone)]
pub enum CommandResponse {
Chat(Uuid, chat_completion::ChatMessage),
Activity(Uuid, String),
RenameChat(Uuid, String),
BackendMessage(Uuid, String),
Completed(Uuid),
}
impl CommandResponse {
#[must_use]
pub fn with_uuid(self, uuid: Uuid) -> Self {
match self {
CommandResponse::Chat(uuid, msg) => CommandResponse::Chat(uuid, msg),
CommandResponse::Activity(_, state) => CommandResponse::Activity(uuid, state),
CommandResponse::RenameChat(_, name) => CommandResponse::RenameChat(uuid, name),
CommandResponse::BackendMessage(_, msg) => CommandResponse::BackendMessage(uuid, msg),
CommandResponse::Completed(_) => CommandResponse::Completed(uuid),
}
}
}
#[cfg_attr(test, automock)]
pub trait Responder: std::fmt::Debug + Send + Sync {
fn send(&self, response: CommandResponse);
fn agent_message(&self, message: chat_completion::ChatMessage);
fn system_message(&self, message: &str);
fn update(&self, state: &str);
fn rename(&self, name: &str);
}
impl Responder for tokio::sync::mpsc::UnboundedSender<CommandResponse> {
fn send(&self, response: CommandResponse) {
let _ = self.send(response);
}
fn agent_message(&self, message: chat_completion::ChatMessage) {
let _ = self.send(CommandResponse::Chat(Uuid::default(), message));
}
fn system_message(&self, message: &str) {
let _ = self.send(CommandResponse::BackendMessage(
Uuid::default(),
message.to_string(),
));
}
fn update(&self, state: &str) {
let _ = self.send(CommandResponse::Activity(
Uuid::default(),
state.to_string(),
));
}
fn rename(&self, name: &str) {
let _ = self.send(CommandResponse::RenameChat(
Uuid::default(),
name.to_string(),
));
}
}
impl Responder for Arc<dyn Responder> {
fn send(&self, response: CommandResponse) {
self.as_ref().send(response);
}
fn agent_message(&self, message: chat_completion::ChatMessage) {
self.as_ref().agent_message(message);
}
fn system_message(&self, message: &str) {
self.as_ref().system_message(message);
}
fn update(&self, state: &str) {
self.as_ref().update(state);
}
fn rename(&self, name: &str) {
self.as_ref().rename(name);
}
}
impl Responder for () {
fn send(&self, _response: CommandResponse) {}
fn agent_message(&self, _message: chat_completion::ChatMessage) {}
fn system_message(&self, _message: &str) {}
fn update(&self, _state: &str) {}
fn rename(&self, _name: &str) {}
}