Skip to content

Commit

Permalink
naming refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Charlie Bacon committed Jun 12, 2024
1 parent 936d155 commit 3fae959
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
12 changes: 6 additions & 6 deletions src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub enum Command {

pub async fn handle_commands(
bot: Bot,
messages: types::Messages,
buffer_store: types::BufferStore,
msg: Message,
cmd: Command,
) -> ResponseResult<()> {
Expand All @@ -43,7 +43,7 @@ pub async fn handle_commands(
bot.send_message(msg.chat.id, answer).await?;
}
Command::Mediate => {
let answer = match gpt::mediate(messages, msg.chat.id).await {
let answer = match gpt::mediate(buffer_store, msg.chat.id).await {
Ok(response) => response,
Err(err) => format!("Error getting an answer from OpenAI: {err}"),
};
Expand All @@ -55,9 +55,9 @@ pub async fn handle_commands(
Ok(())
}

pub async fn handle_messages(messages: types::Messages, msg: Message) -> ResponseResult<()> {
let mut messages_lock = messages.write().await;
match messages_lock.get_mut(&msg.chat.id) {
pub async fn handle_messages(buffer_store: types::BufferStore, msg: Message) -> ResponseResult<()> {
let mut buffer_store_lock = buffer_store.write().await;
match buffer_store_lock.get_mut(&msg.chat.id) {
Some(buffer) => {
if buffer.len() == types::BUFFER_CAPACITY {
buffer.pop_front();
Expand All @@ -67,7 +67,7 @@ pub async fn handle_messages(messages: types::Messages, msg: Message) -> Respons
None => {
let mut buffer = VecDeque::new();
buffer.push_back(msg.clone());
messages_lock.insert(msg.chat.id, buffer);
buffer_store_lock.insert(msg.chat.id, buffer);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/gpt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ pub async fn ask(question: String) -> Result<String, Box<dyn Error>> {
}

// TODO: Better user and message handling
pub async fn mediate(messages: types::Messages, chat_id: ChatId) -> Result<String, Box<dyn Error>> {
let messages_lock = messages.read().await;
pub async fn mediate(buffer: types::Buffer, chat_id: ChatId) -> Result<String, Box<dyn Error>> {
let buffer_lock = buffer.read().await;

let mut conversation = Builder::default();

let buffer = match messages_lock.get(&chat_id) {
let buffer = match buffer_lock.get(&chat_id) {
Some(b) => b,
None => return Err("Buffer with selected ChatId does not exist".into()),
};
Expand All @@ -55,7 +55,7 @@ pub async fn mediate(messages: types::Messages, chat_id: ChatId) -> Result<Strin
conversation.append("\n");
}

drop(messages_lock);
drop(buffer_lock);

let client = init_gpt_client()?;
let request = CreateChatCompletionRequestArgs::default()
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct TelitairoBot {}
impl TelitairoBot {
pub async fn dispatch() {
let bot = Bot::from_env();
let buffer: types::Buffer = Arc::new(RwLock::new(HashMap::new()));
let buffer_store: types::BufferStore = Arc::new(RwLock::new(HashMap::new()));

let handler = dptree::entry()
.branch(
Expand All @@ -24,7 +24,7 @@ impl TelitairoBot {
.branch(Update::filter_message().endpoint(bot::handle_messages));

Dispatcher::builder(bot, handler)
.dependencies(dptree::deps![buffer])
.dependencies(dptree::deps![buffer_store])
.default_handler(|update| async move {
log::warn!("Unhandled update: {:#?}", update);
})
Expand Down
2 changes: 1 addition & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ pub const PERSONALITY: &str= "Eres un asistente andaluz con jerga informal y alg
pub const MEDIATE_QUERY: &str= "A partir de los siguientes mensajes, analiza una posible discusión y da la razón a alguno de los implicados, con una pequeña argumentación.";
pub const BUFFER_CAPACITY: usize = 200;

pub type Buffer = Arc<RwLock<HashMap<ChatId, VecDeque<Message>>>>;
pub type BufferStore = Arc<RwLock<HashMap<ChatId, VecDeque<Message>>>>;

0 comments on commit 3fae959

Please sign in to comment.