diff --git a/.config/ai/scpt b/.config/ai/scpt deleted file mode 160000 index 7a4d642..0000000 --- a/.config/ai/scpt +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 7a4d642e418bbe43d1b5cfaa8cf7ead45363e5dd diff --git a/.config/keep b/.config/keep deleted file mode 100644 index e69de29..0000000 diff --git a/.gitignore b/.gitignore index 5a33697..ac28386 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ pnpm-lock.yaml **Cargo.lock */target/ */**/*.rs.bk +.claude diff --git a/compose.yml b/compose.yml index 2d6c159..43fba25 100644 --- a/compose.yml +++ b/compose.yml @@ -1,7 +1,7 @@ services: aios: #image: syui/aios - #command: ai bot -a syui.syu.is + #command: aibot bot -a syui.syu.is build: context: . restart: always diff --git a/src/bot.rs b/src/bot.rs index 9d2d9e5..1026256 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -167,7 +167,9 @@ pub fn c_bot(c: &Context) { || com.contains("うらない") == true || com.contains("うらなって") == true { - let _output = Command::new(data_scpt(&"ai")) + let script_path = data_scpt(&"ai"); + println!("[fortune] script: {}", script_path); + let output = Command::new(&script_path) .arg(&"atproto").arg(&"fortune") .arg(&handle) .arg(&did) @@ -178,11 +180,25 @@ pub fn c_bot(c: &Context) { .arg(&host) .arg(&prompt) .arg(&prompt_sub) - .output() - .expect("zsh"); + .output(); + match output { + Ok(out) => { + let stdout = String::from_utf8_lossy(&out.stdout); + let stderr = String::from_utf8_lossy(&out.stderr); + println!("[fortune] stdout: {}", stdout); + if !stderr.is_empty() { + println!("[fortune] stderr: {}", stderr); + } + } + Err(e) => { + println!("[fortune] error: {}", e); + } + } w_cid(cid.to_string(), log_file(&"n1"), true); } else if com == "card" || com == "/card" { - let output = Command::new(data_scpt(&"ai")) + let script_path = data_scpt(&"ai"); + println!("[card] script: {}", script_path); + let output = Command::new(&script_path) .arg(&"atproto").arg(&"card") .arg(&handle) .arg(&did) @@ -193,29 +209,48 @@ pub fn c_bot(c: &Context) { .arg(&host) .arg(&prompt) .arg(&prompt_sub) - .output() - .expect("zsh"); - let d = String::from_utf8_lossy(&output.stdout); - let dd = "\n".to_owned() + &d.to_string(); - let text_limit = c_char(dd); - if text_limit.len() > 3 { - //handlev = handle.replace(".", "-").to_string(); - handlev = d.lines().collect::>()[0].to_string(); - link = "https://card.syui.ai/".to_owned() + &handlev; - e = link.chars().count(); - let str_rep = reply_link::post_request( - text_limit.to_string(), - link.to_string(), - s, - e.try_into().unwrap(), - cid.to_string(), - uri.to_string(), - cid_root.to_string(), - uri_root.to_string(), - ) - .await; - println!("{}", str_rep); - w_cid(cid.to_string(), log_file(&"n1"), true); + .output(); + + match output { + Ok(out) => { + let d = String::from_utf8_lossy(&out.stdout); + let stderr = String::from_utf8_lossy(&out.stderr); + println!("[card] stdout: {}", d); + if !stderr.is_empty() { + println!("[card] stderr: {}", stderr); + } + let dd = "\n".to_owned() + &d.to_string(); + let text_limit = c_char(dd); + if text_limit.len() > 3 { + let lines: Vec<&str> = d.lines().collect(); + handlev = lines.get(0).unwrap_or(&"").trim().to_string(); + // 空白を含む場合や空の場合はhandleから生成 + if handlev.is_empty() || handlev.contains(' ') { + handlev = handle.split('.').next().unwrap_or("").to_string(); + } + if !handlev.is_empty() { + link = "https://card.syui.ai/".to_owned() + &handlev; + e = link.chars().count(); + let str_rep = reply_link::post_request( + text_limit.to_string(), + link.to_string(), + s, + e.try_into().unwrap(), + cid.to_string(), + uri.to_string(), + cid_root.to_string(), + uri_root.to_string(), + ) + .await; + println!("{}", str_rep); + w_cid(cid.to_string(), log_file(&"n1"), true); + } + } + } + Err(e) => { + println!("[card] script error: {}", e); + w_cid(cid.to_string(), log_file(&"n1"), true); + } } } else if com == "fav" || com == "/fav" { let output = Command::new(data_scpt(&"ai")) @@ -814,7 +849,9 @@ pub fn c_bot_feed(c: &Context) { } w_cid(cid.to_string(), log_file(&"n1"), true); } else if com == "card" || com == "/card" { - let output = Command::new(data_scpt(&"ai")) + let script_path = data_scpt(&"ai"); + println!("[card] script: {}", script_path); + let output = Command::new(&script_path) .arg(&"atproto").arg(&"card") .arg(&handle) .arg(&did) @@ -825,29 +862,48 @@ pub fn c_bot_feed(c: &Context) { .arg(&host) .arg(&prompt) .arg(&prompt_sub) - .output() - .expect("zsh"); - let d = String::from_utf8_lossy(&output.stdout); - let dd = "\n".to_owned() + &d.to_string(); - let text_limit = c_char(dd); - if text_limit.len() > 3 { - //handlev = handle.replace(".", "-").to_string(); - handlev = d.lines().collect::>()[0].to_string(); - link = "https://card.syui.ai/".to_owned() + &handlev; - e = link.chars().count(); - let str_rep = reply_link::post_request( - text_limit.to_string(), - link.to_string(), - s, - e.try_into().unwrap(), - cid.to_string(), - uri.to_string(), - cid_root.to_string(), - uri_root.to_string(), - ) - .await; - println!("{}", str_rep); - w_cid(cid.to_string(), log_file(&"n1"), true); + .output(); + + match output { + Ok(out) => { + let d = String::from_utf8_lossy(&out.stdout); + let stderr = String::from_utf8_lossy(&out.stderr); + println!("[card] stdout: {}", d); + if !stderr.is_empty() { + println!("[card] stderr: {}", stderr); + } + let dd = "\n".to_owned() + &d.to_string(); + let text_limit = c_char(dd); + if text_limit.len() > 3 { + let lines: Vec<&str> = d.lines().collect(); + handlev = lines.get(0).unwrap_or(&"").trim().to_string(); + // 空白を含む場合や空の場合はhandleから生成 + if handlev.is_empty() || handlev.contains(' ') { + handlev = handle.split('.').next().unwrap_or("").to_string(); + } + if !handlev.is_empty() { + link = "https://card.syui.ai/".to_owned() + &handlev; + e = link.chars().count(); + let str_rep = reply_link::post_request( + text_limit.to_string(), + link.to_string(), + s, + e.try_into().unwrap(), + cid.to_string(), + uri.to_string(), + cid_root.to_string(), + uri_root.to_string(), + ) + .await; + println!("{}", str_rep); + w_cid(cid.to_string(), log_file(&"n1"), true); + } + } + } + Err(e) => { + println!("[card] script error: {}", e); + w_cid(cid.to_string(), log_file(&"n1"), true); + } } } else if com == "fav" || com == "/fav" { let output = Command::new(data_scpt(&"ai")) diff --git a/src/data.rs b/src/data.rs index f7835b4..2437b74 100644 --- a/src/data.rs +++ b/src/data.rs @@ -8,7 +8,7 @@ use std::io::Write; use std::path::Path; pub fn data_file(s: &str) -> String { - let file = "/.config/ai/"; + let file = "/.config/syui/ai/bot/"; let mut f = shellexpand::tilde("~").to_string(); f.push_str(&file); let path = Path::new(&f); @@ -24,7 +24,7 @@ pub fn data_file(s: &str) -> String { } pub fn log_file(s: &str) -> String { - let file = "/.config/ai/txt/"; + let file = "/.config/syui/ai/bot/txt/"; let mut f = shellexpand::tilde("~").to_string(); f.push_str(&file); let path = Path::new(&f); @@ -263,7 +263,7 @@ pub fn data_refresh(s: &str) -> String { pub fn data_scpt(s: &str) -> String { let s = String::from(s); - let file = "/.config/ai/scpt/".to_owned() + &s + &".zsh"; + let file = "/.config/syui/ai/bot/scpt/".to_owned() + &s + &".zsh"; let mut f = shellexpand::tilde("~").to_string(); f.push_str(&file); return f; @@ -603,7 +603,7 @@ pub fn w_cid(cid: String, file: String, t: bool) -> bool { } pub fn c_follow_all() { - let file = "/.config/ai/scpt/follow_all.zsh"; + let file = "/.config/syui/ai/bot/scpt/follow_all.zsh"; let mut f = shellexpand::tilde("~").to_string(); f.push_str(&file); use std::process::Command; @@ -617,7 +617,7 @@ pub fn c_openai_key(c: &Context) { let api = c.args[0].to_string(); let o = "api='".to_owned() + &api.to_string() + &"'".to_owned(); let o = o.to_string(); - let l = shellexpand::tilde("~") + "/.config/ai/openai.toml"; + let l = shellexpand::tilde("~") + "/.config/syui/ai/bot/openai.toml"; let l = l.to_string(); let mut l = fs::File::create(l).unwrap(); if o != "" { @@ -628,7 +628,7 @@ pub fn c_openai_key(c: &Context) { impl Open { pub fn new() -> Result { - let d = shellexpand::tilde("~") + "/.config/ai/openai.toml"; + let d = shellexpand::tilde("~") + "/.config/syui/ai/bot/openai.toml"; let s = Config::builder() .add_source(File::with_name(&d)) .add_source(config::Environment::with_prefix("APP")) diff --git a/src/main.rs b/src/main.rs index 102cc45..d380c74 100644 --- a/src/main.rs +++ b/src/main.rs @@ -480,6 +480,8 @@ fn bot(c: &Context) { loop { c_bot(c); c_bot_feed(c); + // 10秒待機してCPU使用率を抑制 + std::thread::sleep(std::time::Duration::from_secs(10)); } } @@ -487,6 +489,7 @@ fn feed_watch(c: &Context) { refresh(c); loop { c_feed_watch(c); + std::thread::sleep(std::time::Duration::from_secs(10)); } } diff --git a/test/entrypoint.sh b/test/entrypoint.sh index edeb8a4..ad0a7aa 100644 --- a/test/entrypoint.sh +++ b/test/entrypoint.sh @@ -1,4 +1,4 @@ #!/bin/zsh -#ai l $HANDLE -p $PASSWORD -s $HOST -ai bot -a $ADMIN +#aibot l $HANDLE -p $PASSWORD -s $HOST +aibot bot -a $ADMIN