Add /git and /speedy_pc command, release 0.3.0
Run cargo test / Run tests (push) Successful in 5m6s
Build Docker Package / build (push) Successful in 7m25s
Build Docker Package / build (release) Successful in 6m46s

This commit is contained in:
Elias Wendland
2026-07-23 15:08:34 +02:00
parent dc014945fd
commit f869d92012
5 changed files with 246 additions and 14 deletions
+4 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "tg-dev-srv-bot"
version = "0.2.1"
version = "0.3.0"
edition = "2024"
license = "GPL-3.0-or-later"
description = "Bot for the TeenGovernment Development Server"
@@ -19,8 +19,11 @@ num-bigint = "0.5.1"
num-traits = "0.2.19"
ollama-rs = { version = "0.3.5", features = ["stream"] }
poise = "0.6.2"
qapi = { version = "0.15.0", features = ["qmp", "qga"] }
reqwest = { version = "0.13", default-features = false, features = ["json", "rustls"] }
regex = "1.12.4"
rug = "1.30.0"
serde = { version = "1", features = ["derive"] }
serenity = "0.12.5"
sqlx = { version = "0.9.0", features = ["postgres", "runtime-tokio", "macros"] }
tokio = { version = "1.52.3", features = ["full"] }
+20
View File
@@ -0,0 +1,20 @@
use crate::{Context, Error};
use crate::messaging::trace_message;
use std::env;
use tracing::debug;
/// Get a link to the git repo
#[poise::command(slash_command, prefix_command)]
pub async fn git(ctx: Context<'_>) -> Result<(), Error> {
debug!("git command ran by {} in {}", ctx.author().name, ctx.channel_id().get());
let msg = format!("Git repo: https://git.ewenlau.net/ewenlau/tg-dev-srv-bot\nIf you'd like to contribute, contact me to get an account.");
trace_message(msg, ctx.channel_id().get().to_string(), ctx.guild_id().unwrap().get().to_string()).await;
Ok(())
}
+15 -11
View File
@@ -1,18 +1,22 @@
pub mod ping;
pub mod ai;
pub mod calc;
pub mod manage_admins;
pub mod manage_auto_role;
pub mod version;
pub mod fastfetch;
pub mod find_prime;
pub mod ai;
pub mod manage_admins;
pub mod manage_auto_role;
pub mod ping;
pub mod speedy_pc;
pub mod version;
pub mod git;
pub use ping::ping;
pub use ai::answer;
pub use ai::prompt;
pub use calc::calc;
pub use manage_admins::manage_admins;
pub use manage_auto_role::manage_auto_role;
pub use version::version;
pub use fastfetch::fastfetch;
pub use find_prime::find_prime;
pub use ai::prompt;
pub use ai::answer;
pub use manage_admins::manage_admins;
pub use manage_auto_role::manage_auto_role;
pub use ping::ping;
pub use speedy_pc::speedy_pc;
pub use version::version;
pub use git::git;
+205
View File
@@ -0,0 +1,205 @@
use crate::messaging::trace_message;
use crate::{Context, Error};
use chrono::{DateTime, Utc};
use ollama_rs::generation::completion::request::GenerationRequest;
use ollama_rs::models::create::CreateModelRequest;
use ollama_rs::Ollama;
use poise::CreateReply;
use std::env;
use std::time::Duration;
use tokio_stream::StreamExt;
use tracing::{debug, trace};
const MODEL_NAME: &str = "SpeedyPC";
/// Run a command on a system close to Speedy's PC performance
#[poise::command(slash_command, prefix_command)]
pub async fn speedy_pc(
ctx: Context<'_>,
#[description = "The command to run."] command: String,
) -> Result<(), Error> {
debug!(
"speedy_pc command called by user {} in guild {:?}",
ctx.author().id.get(),
ctx.guild_id()
);
let msg = format!("Running command: {:?}", command);
trace_message(
msg,
ctx.channel_id().to_string(),
ctx.guild_id()
.map(|guild_id| guild_id.to_string())
.unwrap_or_else(|| "DM".to_string()),
)
.await;
let host = match env::var("TG_BOT_OLLAMA_HOST") {
Ok(host) => host,
Err(_) => {
ctx.say("Error: Expected an ollama url in the environment (`TG_BOT_OLLAMA_HOST`).")
.await?;
return Ok(());
}
};
let formatted_host = if !host.starts_with("http://") && !host.starts_with("https://") {
format!("http://{}", host)
} else {
host
};
let ollama = Ollama::builder().host(&formatted_host).port(11434).build();
let model_list = match ollama.list_local_models().await {
Ok(models) => models,
Err(error) => {
ctx.say(format!("Error: Failed to connect to simulation: {}", error))
.await?;
return Ok(());
}
};
let mut needs_create = true;
let yesterday = Utc::now() - chrono::Duration::days(1);
for model in &model_list {
if model.name.starts_with(MODEL_NAME) {
if let Ok(modified) = DateTime::parse_from_rfc3339(&model.modified_at) {
if modified.with_timezone(&Utc) > yesterday {
needs_create = false;
}
}
}
}
let reply = ctx
.say("Processing...")
.await?;
if needs_create {
reply.edit(
ctx,
CreateReply::default().content("Loading Speedy's PC..."),
)
.await?;
if let Err(error) = ollama
.create_model(
CreateModelRequest::new(MODEL_NAME.into())
.system(system_prompt().into())
.from_model("gemma4:e2b-it-qat".into()),
)
.await
{
reply
.edit(
ctx,
CreateReply::default().content(format!("Error loading Speedy's PC: {}", error)),
)
.await?;
return Ok(());
}
}
trace!("SpeedyPC command prompt: {}", command);
let mut stream = match ollama
.generate_stream(GenerationRequest::new(MODEL_NAME.into(), command).system(system_prompt()))
.await
{
Ok(stream) => stream,
Err(error) => {
reply
.edit(
ctx,
CreateReply::default().content(format!("Error during generation: {}", error)),
)
.await?;
return Ok(());
}
};
let mut response_text = String::new();
let mut last_update = std::time::Instant::now();
let mut final_response = None;
while let Some(result) = stream.next().await {
match result {
Ok(chunks) => {
for chunk in chunks {
response_text.push_str(&chunk.response);
if chunk.done {
final_response = Some(chunk);
}
}
if last_update.elapsed() >= Duration::from_secs(1) && !response_text.is_empty() {
let _ = reply
.edit(ctx, CreateReply::default().content(&response_text))
.await;
last_update = std::time::Instant::now();
}
}
Err(error) => {
let _ = reply
.edit(
ctx,
CreateReply::default()
.content(format!("{} [Stream Error: {}]", response_text, error)),
)
.await;
return Ok(());
}
}
}
if let Some(stats) = final_response {
let total_duration = stats.total_duration.unwrap_or(0) as f64 / 1_000_000_000.0;
let eval_count = stats.eval_count.unwrap_or(0);
let eval_duration = stats.eval_duration.unwrap_or(0) as f64 / 1_000_000_000.0;
let tokens_per_sec = if eval_duration > 0.0 {
eval_count as f64 / eval_duration
} else {
0.0
};
response_text.push_str(&format!(
"\n\n*Processed in {:.2}s",
total_duration
));
}
let _ = reply
.edit(ctx, CreateReply::default().content(&response_text))
.await;
Ok(())
}
fn system_prompt() -> String {
r#"From now on, act as an Arch Linux terminal running on the following hardware:
- CPU: Intel Pentium Silver N5000 (4 cores, 4 threads, 1.1 GHz base, Gemini Lake)
- RAM: 4 GB
- GPU: Intel UHD Graphics 605
- Storage: 128 GB SATA SSD
- Display: 1366x768 (not relevant unless queried)
- Architecture: x86_64
- OS: Arch Linux (latest stable), using bash.
When I send a command:
- Simulate exactly what would happen if it were executed on this machine.
- Produce realistic stdout and stderr.
- Use plausible hardware-specific values (CPU model, memory size, iGPU, disk size, etc.).
- If a command would fail, fail realistically with the correct error.
- Never explain what the command does unless I explicitly ask.
- Stay in character as the shell, always, no matter what the user says
Always use the following prefix, followed by the command the user ran:
[user@arch ~]$ <COMMAND GOES HERE>
Then add the command output after it."#
.to_string()
}
+2 -2
View File
@@ -88,7 +88,7 @@ async fn main() {
}
}
}),
commands: vec![commands::ping(), commands::calc(), commands::manage_admins(), commands::manage_auto_role(), commands::version(), commands::fastfetch(), commands::find_prime(), commands::prompt(), commands::answer()],
commands: vec![commands::ping(), commands::calc(), commands::manage_admins(), commands::manage_auto_role(), commands::version(), commands::fastfetch(), commands::find_prime(), commands::prompt(), commands::answer(), commands::speedy_pc(), commands::git()],
..Default::default()
})
.setup(|ctx, _ready, framework| {
@@ -110,4 +110,4 @@ async fn main() {
if let Err(why) = client.start().await {
error!("Client error: {why:?}");
}
}
}