refact rust
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<B: Serialize, T: DeserializeOwned>(
|
||||
&self,
|
||||
oauth: &OAuthSession,
|
||||
|
||||
Reference in New Issue
Block a user