From 1d8224c4bf0c2d730bb44475b555c1ae87bcd077 Mon Sep 17 00:00:00 2001 From: syui Date: Wed, 25 Mar 2026 06:34:44 +0900 Subject: [PATCH] refact rust --- src/commands/gpt.rs | 2 +- src/commands/pds.rs | 2 +- src/lms/chat.rs | 52 ++++++++++++++++++++++---------------------- src/lms/translate.rs | 2 +- src/xrpc.rs | 1 + 5 files changed, 30 insertions(+), 29 deletions(-) diff --git a/src/commands/gpt.rs b/src/commands/gpt.rs index 2b39c46..b7357e2 100644 --- a/src/commands/gpt.rs +++ b/src/commands/gpt.rs @@ -151,7 +151,7 @@ pub async fn list_memory() -> Result<()> { } println!("Found {} memory records", all_records.len()); - println!("{:<50} {:>8} {}", "URI", "VERSION", "CREATED"); + println!("{:<50} {:>8} CREATED", "URI", "VERSION"); println!("{}", "-".repeat(80)); for record in &all_records { diff --git a/src/commands/pds.rs b/src/commands/pds.rs index 09d0a65..9e35165 100644 --- a/src/commands/pds.rs +++ b/src/commands/pds.rs @@ -83,7 +83,7 @@ pub async fn check_versions(networks_path: &str) -> Result<()> { println!("latest: {}", latest); println!(); - for (name, _network) in &networks { + for name in networks.keys() { // Check PDS using network name as domain let url = format!("https://{}/xrpc/_health", name); let version = match client.get(&url).send().await { diff --git a/src/lms/chat.rs b/src/lms/chat.rs index 734c95e..e985ef6 100644 --- a/src/lms/chat.rs +++ b/src/lms/chat.rs @@ -47,21 +47,13 @@ struct ChatRecord { } #[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Default)] struct ChatSession { root_uri: Option, last_uri: Option, messages: Vec, } -impl Default for ChatSession { - fn default() -> Self { - Self { - root_uri: None, - last_uri: None, - messages: Vec::new(), - } - } -} /// Get system prompt from environment or file fn get_system_prompt() -> String { @@ -238,17 +230,25 @@ fn save_chat_local( Ok(uri) } +/// Context for LLM chat processing +struct ChatContext { + client: reqwest::Client, + llm_url: String, + model: String, + output_dir: String, + user_did: String, + bot_did: String, +} + /// Process a single message and get response async fn process_message( - client: &reqwest::Client, - llm_url: &str, - model: &str, - output_dir: &str, - user_did: &str, - bot_did: &str, + ctx: &ChatContext, session: &mut ChatSession, input: &str, ) -> Result { + let (client, llm_url, model, output_dir, user_did, bot_did) = + (&ctx.client, ctx.llm_url.as_str(), ctx.model.as_str(), + ctx.output_dir.as_str(), ctx.user_did.as_str(), ctx.bot_did.as_str()); // Add user message to history session.messages.push(ChatMessage { role: "user".to_string(), @@ -330,15 +330,18 @@ pub async fn run(input: Option<&str>, new_session: bool) -> Result<()> { load_session().unwrap_or_else(|_| new_session_with_prompt()) }; - let client = reqwest::Client::new(); - let llm_url = format!("{}/chat/completions", chat_url); + let ctx = ChatContext { + client: reqwest::Client::new(), + llm_url: format!("{}/chat/completions", chat_url), + model, + output_dir, + user_did, + bot_did, + }; // Single message mode if let Some(msg) = input { - let response = process_message( - &client, &llm_url, &model, &output_dir, - &user_did, &bot_did, &mut session, msg, - ).await?; + let response = process_message(&ctx, &mut session, msg).await?; println!("{}", response); use std::io::Write; std::io::stdout().flush()?; @@ -347,7 +350,7 @@ pub async fn run(input: Option<&str>, new_session: bool) -> Result<()> { // Interactive mode println!("ailog chat (type 'exit' to quit, Ctrl+C to cancel)"); - println!("model: {}", model); + println!("model: {}", ctx.model); println!("---"); let mut rl = DefaultEditor::new()?; @@ -365,10 +368,7 @@ pub async fn run(input: Option<&str>, new_session: bool) -> Result<()> { let _ = rl.add_history_entry(input); - match process_message( - &client, &llm_url, &model, &output_dir, - &user_did, &bot_did, &mut session, input, - ).await { + match process_message(&ctx, &mut session, input).await { Ok(response) => println!("\n{}\n", response), Err(e) => { eprintln!("Error: {}", e); diff --git a/src/lms/translate.rs b/src/lms/translate.rs index a255ec7..b845f9f 100644 --- a/src/lms/translate.rs +++ b/src/lms/translate.rs @@ -202,7 +202,7 @@ async fn translate_folder(dir: &Path, from: &str, to: &str) -> Result<()> { match translate_file(path, from, to).await { Ok(_) => { // Check if it was actually translated or skipped - let content = fs::read_to_string(&path)?; + let content = fs::read_to_string(path)?; let record: serde_json::Value = serde_json::from_str(&content)?; let value = record.get("value").unwrap_or(&record); if value diff --git a/src/xrpc.rs b/src/xrpc.rs index 5651597..5382de6 100644 --- a/src/xrpc.rs +++ b/src/xrpc.rs @@ -199,6 +199,7 @@ impl XrpcClient { } /// DPoP-authenticated request with optional proxy header + #[allow(clippy::too_many_arguments)] async fn dpop_request_with_retry_proxy( &self, oauth: &OAuthSession,