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

@@ -47,21 +47,13 @@ struct ChatRecord {
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Default)]
struct ChatSession {
root_uri: Option<String>,
last_uri: Option<String>,
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
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<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
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);

View File

@@ -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