diff --git a/src/hedgedoc.rs b/src/hedgedoc.rs index 37b4057..b28ae7b 100644 --- a/src/hedgedoc.rs +++ b/src/hedgedoc.rs @@ -1,10 +1,10 @@ use crate::config_spec::{CfgField, CfgGroup}; +use ollama_rs; use ollama_rs::generation::completion::request::GenerationRequest; use regex::Regex; use reqwest::blocking::Client; use reqwest::blocking::Response; use std::error::Error; -use ollama_rs; use tokio::runtime::Runtime; pub const CONFIG: CfgGroup<'static> = CfgGroup { @@ -179,22 +179,23 @@ pub fn summarize(pad_content: String) -> String { let title = captures.get(2).unwrap().as_str(); result.push(format!("{}{}", indent, title)); } - }; + } result.join("\n") } -pub fn summarize_with_ollama(pad_content: &str, ollama_pre_prompt: &str, ollama_address: &str, ollama_port: &u16) -> Result> { +pub fn summarize_with_ollama( + pad_content: &str, ollama_pre_prompt: &str, ollama_address: &str, ollama_port: &u16, +) -> Result> { let ollama = ollama_rs::Ollama::new(ollama_address, ollama_port.clone()); let model = "qwen2.5:32b".to_string(); let prompt = ollama_pre_prompt.to_string() + pad_content; let rt = Runtime::new().unwrap(); - let result = rt.block_on(async { - ollama.generate(GenerationRequest::new(model, prompt)).await - }); + let result = + rt.block_on(async { ollama.generate(GenerationRequest::new(model, prompt)).await }); match result { - Ok(res) => {return Ok(res.response)}, - Err(err) => {return Err(err.into())} + Ok(res) => return Ok(res.response), + Err(err) => return Err(err.into()), } } diff --git a/src/main.rs b/src/main.rs index f70f466..ab30b7d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,5 @@ // Dies ist der Plenumsbot vom Chaos Computer Club Berlin. Mehr Infos zum aktuellen Stand des Projektss in der ReadMe. -use std::env; -use std::error::Error; -use std::fmt::Display; -use std::io::IsTerminal; -use std::time::Instant; use cccron_lib::config_spec::{self, CfgField, CfgGroup, CfgSpec}; use cccron_lib::date; use cccron_lib::email::{self, Email, SimpleEmail}; @@ -18,6 +13,11 @@ use cccron_lib::{trace_var, trace_var_, verboseln}; use chrono::{Local, NaiveDate}; use clap::{Arg, Command}; use colored::Colorize; +use std::env; +use std::error::Error; +use std::fmt::Display; +use std::io::IsTerminal; +use std::time::Instant; /* ***** Config Spec ***** */ const CONFIG_SPEC: CfgSpec<'static> = CfgSpec { @@ -368,7 +368,10 @@ fn do_announcement( &config["matrix-room-id-2"], is_dry_run(), ); - let message = format!("{}\n\n{}{}",&config["text-email-greeting"], &body, &config["text-email-signature"]); + let message = format!( + "{}\n\n{}{}", + &config["text-email-greeting"], &body, &config["text-email-signature"] + ); matrix.send_message_to_two_rooms(&message)?; Ok(()) } @@ -422,7 +425,7 @@ fn do_reminder( ); // TODO: ADD SOMETHING TO WIKI } - + config.set("state-name", &ProgramState::Reminded.to_string()).ok(); config.set("email-state-toc", &toc).ok(); let mut matrix = MatrixClient::new( @@ -433,7 +436,10 @@ fn do_reminder( &config["matrix-room-id-2"], is_dry_run(), ); - let message = format!("{}\n\n{}{}",&config["text-email-greeting"], &body, &config["text-email-signature"]); + let message = format!( + "{}\n\n{}{}", + &config["text-email-greeting"], &body, &config["text-email-signature"] + ); matrix.send_message_to_two_rooms(&message)?; Ok(()) } @@ -455,13 +461,20 @@ fn do_protocol( }, }; let summary_or_toc: String = if ollama_enabled && !toc.is_empty() { - let ollama_port: &u16 = &config["hedgedoc-ollama-port"].parse::().expect("The ollama port wasn't given a valid u16 port, please check the config"); - match hedgedoc::summarize_with_ollama(&pad_content, &config["hedgedoc-ollama-pre-prompt"], &config["hedgedoc-ollama-address"], ollama_port) { + let ollama_port: &u16 = &config["hedgedoc-ollama-port"] + .parse::() + .expect("The ollama port wasn't given a valid u16 port, please check the config"); + match hedgedoc::summarize_with_ollama( + &pad_content, + &config["hedgedoc-ollama-pre-prompt"], + &config["hedgedoc-ollama-address"], + ollama_port, + ) { Ok(ollama_summary) => ollama_summary, Err(err) => { eprintln!("Ollama failed, continuing with standard toc. This was the error Message: {err}"); toc.clone() - } + }, } } else { verboseln!("Ollama is disabled, just using toc"); @@ -524,7 +537,10 @@ fn do_protocol( &config["wiki-plenum-page"], &summary_or_toc ); - let full_message = format!("{}\n\n{}{}",&config["text-email-greeting"], &message, &config["text-email-signature"]); + let full_message = format!( + "{}\n\n{}{}", + &config["text-email-greeting"], &message, &config["text-email-signature"] + ); matrix.send_message_to_two_rooms(&full_message)?; Ok(()) } @@ -697,4 +713,4 @@ fn send_email( &config["text-email-greeting"], body, &config["text-email-signature"] ); email.send_email(full_subject, full_body) -} \ No newline at end of file +} diff --git a/src/matrix.rs b/src/matrix.rs index 874829e..28ddb23 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -1,5 +1,5 @@ -use std::error::Error; use lazy_static::lazy_static; +use std::error::Error; use colored::Colorize; use regex::Regex; @@ -83,8 +83,8 @@ impl std::fmt::Debug for MatrixClient { impl MatrixClient { pub fn new( - homeserver_url: &str, user_id: &str, access_token: &str, room_id_1: &str, - room_id_2: &str, is_dry_run: bool, + homeserver_url: &str, user_id: &str, access_token: &str, room_id_1: &str, room_id_2: &str, + is_dry_run: bool, ) -> Self { Self { homeserver_url: homeserver_url.to_string(), @@ -183,22 +183,22 @@ impl MatrixClient { static ref MATRIX_ITALIC: Regex = Regex::new(r"\*(.+?)\*").unwrap(); static ref MATRIX_URL: Regex = Regex::new(r"(https?://\S+?)(?:[.,])?(?:\s|$)").unwrap(); } - + let mut html = markdown; - + // Handle URLs first html = MATRIX_URL.replace_all(&html, r"$1").to_string(); - + // Basic formatting html = MATRIX_BOLD.replace_all(&html, r"$1").to_string(); html = MATRIX_ITALIC.replace_all(&html, r"$1").to_string(); - + // Convert newlines to
html = html.replace("\n", "
"); - + Ok(html) } - pub fn pandoc_convert_text_to_md(markdown: String) -> Result> { + pub fn pandoc_convert_text_to_md(markdown: String) -> Result> { let (output, errors, status) = crate::pipe( "pandoc", &mut ["--from", "markdown-auto_identifiers", "--to", "html5"], @@ -223,7 +223,7 @@ impl MatrixClient { ("format", "org.matrix.custom.html"), ("formatted_body", &formatted_text), // ("m.mentions", "{}"), - ]); + ]); self.send_room_event(&room_id, "m.room.message", &content) } @@ -234,12 +234,9 @@ impl MatrixClient { verboseln!("room event:{}", &endpoint.green()); self.put(&endpoint, None, Some(content), false) } - pub fn send_message_to_two_rooms( - &mut self, message: &str - ) -> Result<(), Box> { - self.send_room_message( &self.room_id_2.clone(), &message,)?; - self.send_room_message( &self.room_id_1.clone(), &message,)?; + pub fn send_message_to_two_rooms(&mut self, message: &str) -> Result<(), Box> { + self.send_room_message(&self.room_id_2.clone(), &message)?; + self.send_room_message(&self.room_id_1.clone(), &message)?; Ok(()) } - }