cargo fmt

This commit is contained in:
murmeldin 2025-01-15 14:54:11 +01:00
parent c15188ec9d
commit 8a6c6cda9a
3 changed files with 51 additions and 37 deletions

View file

@ -1,10 +1,10 @@
use crate::config_spec::{CfgField, CfgGroup}; use crate::config_spec::{CfgField, CfgGroup};
use ollama_rs;
use ollama_rs::generation::completion::request::GenerationRequest; use ollama_rs::generation::completion::request::GenerationRequest;
use regex::Regex; use regex::Regex;
use reqwest::blocking::Client; use reqwest::blocking::Client;
use reqwest::blocking::Response; use reqwest::blocking::Response;
use std::error::Error; use std::error::Error;
use ollama_rs;
use tokio::runtime::Runtime; use tokio::runtime::Runtime;
pub const CONFIG: CfgGroup<'static> = CfgGroup { 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(); let title = captures.get(2).unwrap().as_str();
result.push(format!("{}{}", indent, title)); result.push(format!("{}{}", indent, title));
} }
}; }
result.join("\n") result.join("\n")
} }
pub fn summarize_with_ollama(pad_content: &str, ollama_pre_prompt: &str, ollama_address: &str, ollama_port: &u16) -> Result<String, Box<dyn Error>> { pub fn summarize_with_ollama(
pad_content: &str, ollama_pre_prompt: &str, ollama_address: &str, ollama_port: &u16,
) -> Result<String, Box<dyn Error>> {
let ollama = ollama_rs::Ollama::new(ollama_address, ollama_port.clone()); let ollama = ollama_rs::Ollama::new(ollama_address, ollama_port.clone());
let model = "qwen2.5:32b".to_string(); let model = "qwen2.5:32b".to_string();
let prompt = ollama_pre_prompt.to_string() + pad_content; let prompt = ollama_pre_prompt.to_string() + pad_content;
let rt = Runtime::new().unwrap(); let rt = Runtime::new().unwrap();
let result = rt.block_on(async { let result =
ollama.generate(GenerationRequest::new(model, prompt)).await rt.block_on(async { ollama.generate(GenerationRequest::new(model, prompt)).await });
});
match result { match result {
Ok(res) => {return Ok(res.response)}, Ok(res) => return Ok(res.response),
Err(err) => {return Err(err.into())} Err(err) => return Err(err.into()),
} }
} }

View file

@ -1,10 +1,5 @@
// Dies ist der Plenumsbot vom Chaos Computer Club Berlin. Mehr Infos zum aktuellen Stand des Projektss in der ReadMe. // 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::config_spec::{self, CfgField, CfgGroup, CfgSpec};
use cccron_lib::date; use cccron_lib::date;
use cccron_lib::email::{self, Email, SimpleEmail}; use cccron_lib::email::{self, Email, SimpleEmail};
@ -18,6 +13,11 @@ use cccron_lib::{trace_var, trace_var_, verboseln};
use chrono::{Local, NaiveDate}; use chrono::{Local, NaiveDate};
use clap::{Arg, Command}; use clap::{Arg, Command};
use colored::Colorize; use colored::Colorize;
use std::env;
use std::error::Error;
use std::fmt::Display;
use std::io::IsTerminal;
use std::time::Instant;
/* ***** Config Spec ***** */ /* ***** Config Spec ***** */
const CONFIG_SPEC: CfgSpec<'static> = CfgSpec { const CONFIG_SPEC: CfgSpec<'static> = CfgSpec {
@ -368,7 +368,10 @@ fn do_announcement(
&config["matrix-room-id-2"], &config["matrix-room-id-2"],
is_dry_run(), 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)?; matrix.send_message_to_two_rooms(&message)?;
Ok(()) Ok(())
} }
@ -433,7 +436,10 @@ fn do_reminder(
&config["matrix-room-id-2"], &config["matrix-room-id-2"],
is_dry_run(), 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)?; matrix.send_message_to_two_rooms(&message)?;
Ok(()) Ok(())
} }
@ -455,13 +461,20 @@ fn do_protocol(
}, },
}; };
let summary_or_toc: String = if ollama_enabled && !toc.is_empty() { let summary_or_toc: String = if ollama_enabled && !toc.is_empty() {
let ollama_port: &u16 = &config["hedgedoc-ollama-port"].parse::<u16>().expect("The ollama port wasn't given a valid u16 port, please check the config"); let ollama_port: &u16 = &config["hedgedoc-ollama-port"]
match hedgedoc::summarize_with_ollama(&pad_content, &config["hedgedoc-ollama-pre-prompt"], &config["hedgedoc-ollama-address"], ollama_port) { .parse::<u16>()
.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, Ok(ollama_summary) => ollama_summary,
Err(err) => { Err(err) => {
eprintln!("Ollama failed, continuing with standard toc. This was the error Message: {err}"); eprintln!("Ollama failed, continuing with standard toc. This was the error Message: {err}");
toc.clone() toc.clone()
} },
} }
} else { } else {
verboseln!("Ollama is disabled, just using toc"); verboseln!("Ollama is disabled, just using toc");
@ -524,7 +537,10 @@ fn do_protocol(
&config["wiki-plenum-page"], &config["wiki-plenum-page"],
&summary_or_toc &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)?; matrix.send_message_to_two_rooms(&full_message)?;
Ok(()) Ok(())
} }

View file

@ -1,5 +1,5 @@
use std::error::Error;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use std::error::Error;
use colored::Colorize; use colored::Colorize;
use regex::Regex; use regex::Regex;
@ -83,8 +83,8 @@ impl std::fmt::Debug for MatrixClient {
impl MatrixClient { impl MatrixClient {
pub fn new( pub fn new(
homeserver_url: &str, user_id: &str, access_token: &str, room_id_1: &str, homeserver_url: &str, user_id: &str, access_token: &str, room_id_1: &str, room_id_2: &str,
room_id_2: &str, is_dry_run: bool, is_dry_run: bool,
) -> Self { ) -> Self {
Self { Self {
homeserver_url: homeserver_url.to_string(), homeserver_url: homeserver_url.to_string(),
@ -198,7 +198,7 @@ impl MatrixClient {
Ok(html) Ok(html)
} }
pub fn pandoc_convert_text_to_md(markdown: String) -> Result<String, Box<dyn Error>> { pub fn pandoc_convert_text_to_md(markdown: String) -> Result<String, Box<dyn Error>> {
let (output, errors, status) = crate::pipe( let (output, errors, status) = crate::pipe(
"pandoc", "pandoc",
&mut ["--from", "markdown-auto_identifiers", "--to", "html5"], &mut ["--from", "markdown-auto_identifiers", "--to", "html5"],
@ -223,7 +223,7 @@ impl MatrixClient {
("format", "org.matrix.custom.html"), ("format", "org.matrix.custom.html"),
("formatted_body", &formatted_text), ("formatted_body", &formatted_text),
// ("m.mentions", "{}"), // ("m.mentions", "{}"),
]); ]);
self.send_room_event(&room_id, "m.room.message", &content) self.send_room_event(&room_id, "m.room.message", &content)
} }
@ -234,12 +234,9 @@ impl MatrixClient {
verboseln!("room event:{}", &endpoint.green()); verboseln!("room event:{}", &endpoint.green());
self.put(&endpoint, None, Some(content), false) self.put(&endpoint, None, Some(content), false)
} }
pub fn send_message_to_two_rooms( pub fn send_message_to_two_rooms(&mut self, message: &str) -> Result<(), Box<dyn Error>> {
&mut self, message: &str self.send_room_message(&self.room_id_2.clone(), &message)?;
) -> Result<(), Box<dyn Error>> { self.send_room_message(&self.room_id_1.clone(), &message)?;
self.send_room_message( &self.room_id_2.clone(), &message,)?;
self.send_room_message( &self.room_id_1.clone(), &message,)?;
Ok(()) Ok(())
} }
} }