use super::pi::command_error;
use super::{ChatCommand, ChatEvent, ChatId, Command, Event, EventQueue};
pub(super) fn message_chat(command: &Command) -> Option<ChatId> {
match command {
Command::Chat {
chat,
command: ChatCommand::Send { .. } | ChatCommand::RunCommand { .. },
} => Some(*chat),
_ => None,
}
}
pub(super) async fn report_failed(
events: &EventQueue,
chat: Option<ChatId>,
error: String,
ended: Option<ChatId>,
) {
events.push(command_error(chat, error)).await;
if let Some(chat) = ended {
events.push(Event::of(chat, ChatEvent::TurnDone)).await;
}
}
pub(super) async fn ran_nothing(events: &EventQueue, chat: ChatId, running: bool) {
if !running {
events.push(Event::of(chat, ChatEvent::TurnDone)).await;
}
}
pub(super) async fn refused(events: &EventQueue, chat: ChatId, error: String) {
report_failed(events, Some(chat), error, Some(chat)).await;
}
#[cfg(test)]
mod tests {
use super::message_chat;
use crate::{ChatCommand, Command};
#[test]
fn only_a_message_starts_or_steers_a_turn() {
let send = Command::Chat {
chat: 3,
command: ChatCommand::Send {
text: "hello".into(),
context: Vec::new(),
images: Vec::new(),
},
};
let skill = Command::Chat {
chat: 4,
command: ChatCommand::RunCommand {
name: "review".into(),
args: String::new(),
},
};
let rename = Command::Chat {
chat: 5,
command: ChatCommand::Rename("name".into()),
};
assert_eq!(message_chat(&send), Some(3));
assert_eq!(message_chat(&skill), Some(4));
assert_eq!(message_chat(&rename), None);
assert_eq!(message_chat(&Command::ListSessions), None);
}
}