2
0

refact rust

This commit is contained in:
2026-03-25 06:34:44 +09:00
parent 3103090c33
commit 1d8224c4bf
5 changed files with 30 additions and 29 deletions

View File

@@ -151,7 +151,7 @@ pub async fn list_memory() -> Result<()> {
} }
println!("Found {} memory records", all_records.len()); println!("Found {} memory records", all_records.len());
println!("{:<50} {:>8} {}", "URI", "VERSION", "CREATED"); println!("{:<50} {:>8} CREATED", "URI", "VERSION");
println!("{}", "-".repeat(80)); println!("{}", "-".repeat(80));
for record in &all_records { for record in &all_records {

View File

@@ -83,7 +83,7 @@ pub async fn check_versions(networks_path: &str) -> Result<()> {
println!("latest: {}", latest); println!("latest: {}", latest);
println!(); println!();
for (name, _network) in &networks { for name in networks.keys() {
// Check PDS using network name as domain // Check PDS using network name as domain
let url = format!("https://{}/xrpc/_health", name); let url = format!("https://{}/xrpc/_health", name);
let version = match client.get(&url).send().await { let version = match client.get(&url).send().await {

View File

@@ -47,21 +47,13 @@ struct ChatRecord {
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Default)]
struct ChatSession { struct ChatSession {
root_uri: Option<String>, root_uri: Option<String>,
last_uri: Option<String>, last_uri: Option<String>,
messages: Vec<ChatMessage>, messages: Vec<ChatMessage>,
} }
impl Default for ChatSession {
fn default() -> Self {
Self {
root_uri: None,
last_uri: None,
messages: Vec::new(),
}
}
}
/// Get system prompt from environment or file /// Get system prompt from environment or file
fn get_system_prompt() -> String { fn get_system_prompt() -> String {
@@ -238,17 +230,25 @@ fn save_chat_local(
Ok(uri) 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 /// Process a single message and get response
async fn process_message( async fn process_message(
client: &reqwest::Client, ctx: &ChatContext,
llm_url: &str,
model: &str,
output_dir: &str,
user_did: &str,
bot_did: &str,
session: &mut ChatSession, session: &mut ChatSession,
input: &str, input: &str,
) -> Result<String> { ) -> Result<String> {
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 // Add user message to history
session.messages.push(ChatMessage { session.messages.push(ChatMessage {
role: "user".to_string(), 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()) load_session().unwrap_or_else(|_| new_session_with_prompt())
}; };
let client = reqwest::Client::new(); let ctx = ChatContext {
let llm_url = format!("{}/chat/completions", chat_url); client: reqwest::Client::new(),
llm_url: format!("{}/chat/completions", chat_url),
model,
output_dir,
user_did,
bot_did,
};
// Single message mode // Single message mode
if let Some(msg) = input { if let Some(msg) = input {
let response = process_message( let response = process_message(&ctx, &mut session, msg).await?;
&client, &llm_url, &model, &output_dir,
&user_did, &bot_did, &mut session, msg,
).await?;
println!("{}", response); println!("{}", response);
use std::io::Write; use std::io::Write;
std::io::stdout().flush()?; std::io::stdout().flush()?;
@@ -347,7 +350,7 @@ pub async fn run(input: Option<&str>, new_session: bool) -> Result<()> {
// Interactive mode // Interactive mode
println!("ailog chat (type 'exit' to quit, Ctrl+C to cancel)"); println!("ailog chat (type 'exit' to quit, Ctrl+C to cancel)");
println!("model: {}", model); println!("model: {}", ctx.model);
println!("---"); println!("---");
let mut rl = DefaultEditor::new()?; 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); let _ = rl.add_history_entry(input);
match process_message( match process_message(&ctx, &mut session, input).await {
&client, &llm_url, &model, &output_dir,
&user_did, &bot_did, &mut session, input,
).await {
Ok(response) => println!("\n{}\n", response), Ok(response) => println!("\n{}\n", response),
Err(e) => { Err(e) => {
eprintln!("Error: {}", e); eprintln!("Error: {}", e);

View File

@@ -202,7 +202,7 @@ async fn translate_folder(dir: &Path, from: &str, to: &str) -> Result<()> {
match translate_file(path, from, to).await { match translate_file(path, from, to).await {
Ok(_) => { Ok(_) => {
// Check if it was actually translated or skipped // 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 record: serde_json::Value = serde_json::from_str(&content)?;
let value = record.get("value").unwrap_or(&record); let value = record.get("value").unwrap_or(&record);
if value if value

View File

@@ -199,6 +199,7 @@ impl XrpcClient {
} }
/// DPoP-authenticated request with optional proxy header /// DPoP-authenticated request with optional proxy header
#[allow(clippy::too_many_arguments)]
async fn dpop_request_with_retry_proxy<B: Serialize, T: DeserializeOwned>( async fn dpop_request_with_retry_proxy<B: Serialize, T: DeserializeOwned>(
&self, &self,
oauth: &OAuthSession, oauth: &OAuthSession,