test pds oauth did
This commit is contained in:
14
src/lib.rs
Normal file
14
src/lib.rs
Normal file
@ -0,0 +1,14 @@
|
||||
// Export modules for testing
|
||||
pub mod ai;
|
||||
pub mod analyzer;
|
||||
pub mod atproto;
|
||||
pub mod commands;
|
||||
pub mod config;
|
||||
pub mod doc_generator;
|
||||
pub mod generator;
|
||||
pub mod markdown;
|
||||
pub mod mcp;
|
||||
pub mod oauth;
|
||||
// pub mod ollama_proxy; // Temporarily disabled - uses actix-web instead of axum
|
||||
pub mod template;
|
||||
pub mod translator;
|
@ -2,6 +2,7 @@ use anyhow::Result;
|
||||
use regex::Regex;
|
||||
use super::MarkdownSection;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct MarkdownParser {
|
||||
_code_block_regex: Regex,
|
||||
header_regex: Regex,
|
||||
|
@ -42,9 +42,9 @@ pub enum MarkdownSection {
|
||||
|
||||
pub trait Translator {
|
||||
#[allow(dead_code)]
|
||||
async fn translate(&self, content: &str, config: &TranslationConfig) -> Result<String>;
|
||||
async fn translate_markdown(&self, content: &str, config: &TranslationConfig) -> Result<String>;
|
||||
async fn translate_sections(&self, sections: Vec<MarkdownSection>, config: &TranslationConfig) -> Result<Vec<MarkdownSection>>;
|
||||
fn translate(&self, content: &str, config: &TranslationConfig) -> impl std::future::Future<Output = Result<String>> + Send;
|
||||
fn translate_markdown(&self, content: &str, config: &TranslationConfig) -> impl std::future::Future<Output = Result<String>> + Send;
|
||||
fn translate_sections(&self, sections: Vec<MarkdownSection>, config: &TranslationConfig) -> impl std::future::Future<Output = Result<Vec<MarkdownSection>>> + Send;
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
@ -67,6 +67,7 @@ pub struct TranslationMetrics {
|
||||
pub sections_preserved: usize,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct LanguageMapping {
|
||||
pub mappings: HashMap<String, LanguageInfo>,
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ use std::time::Instant;
|
||||
use super::*;
|
||||
use crate::translator::markdown_parser::MarkdownParser;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct OllamaTranslator {
|
||||
client: Client,
|
||||
language_mapping: LanguageMapping,
|
||||
@ -129,86 +130,103 @@ Translation:"#,
|
||||
}
|
||||
|
||||
impl Translator for OllamaTranslator {
|
||||
async fn translate(&self, content: &str, config: &TranslationConfig) -> Result<String> {
|
||||
let prompt = self.build_translation_prompt(content, config)?;
|
||||
self.call_ollama(&prompt, config).await
|
||||
fn translate(&self, content: &str, config: &TranslationConfig) -> impl std::future::Future<Output = Result<String>> + Send {
|
||||
async move {
|
||||
let prompt = self.build_translation_prompt(content, config)?;
|
||||
self.call_ollama(&prompt, config).await
|
||||
}
|
||||
}
|
||||
|
||||
async fn translate_markdown(&self, content: &str, config: &TranslationConfig) -> Result<String> {
|
||||
println!("🔄 Parsing markdown content...");
|
||||
let sections = self.parser.parse_markdown(content)?;
|
||||
|
||||
println!("📝 Found {} sections to process", sections.len());
|
||||
let translated_sections = self.translate_sections(sections, config).await?;
|
||||
|
||||
println!("✅ Rebuilding markdown from translated sections...");
|
||||
let result = self.parser.rebuild_markdown(translated_sections);
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
async fn translate_sections(&self, sections: Vec<MarkdownSection>, config: &TranslationConfig) -> Result<Vec<MarkdownSection>> {
|
||||
let mut translated_sections = Vec::new();
|
||||
let start_time = Instant::now();
|
||||
|
||||
for (index, section) in sections.into_iter().enumerate() {
|
||||
println!(" 🔤 Processing section {}", index + 1);
|
||||
fn translate_markdown(&self, content: &str, config: &TranslationConfig) -> impl std::future::Future<Output = Result<String>> + Send {
|
||||
async move {
|
||||
println!("🔄 Parsing markdown content...");
|
||||
let sections = self.parser.parse_markdown(content)?;
|
||||
|
||||
let translated_section = match §ion {
|
||||
MarkdownSection::Code(_content, _lang) => {
|
||||
if config.preserve_code {
|
||||
println!(" ⏭️ Preserving code block");
|
||||
section // Preserve code blocks
|
||||
} else {
|
||||
section // Still preserve for now
|
||||
}
|
||||
}
|
||||
MarkdownSection::Link(text, url) => {
|
||||
if config.preserve_links {
|
||||
println!(" ⏭️ Preserving link");
|
||||
section // Preserve links
|
||||
} else {
|
||||
// Translate link text only
|
||||
let prompt = self.build_section_translation_prompt(&MarkdownSection::Text(text.clone()), config)?;
|
||||
let translated_text = self.call_ollama(&prompt, config).await?;
|
||||
MarkdownSection::Link(translated_text.trim().to_string(), url.clone())
|
||||
}
|
||||
}
|
||||
MarkdownSection::Image(_alt, _url) => {
|
||||
println!(" 🖼️ Preserving image");
|
||||
section // Preserve images
|
||||
}
|
||||
MarkdownSection::Table(content) => {
|
||||
println!(" 📊 Translating table content");
|
||||
let prompt = self.build_section_translation_prompt(&MarkdownSection::Text(content.clone()), config)?;
|
||||
let translated_content = self.call_ollama(&prompt, config).await?;
|
||||
MarkdownSection::Table(translated_content.trim().to_string())
|
||||
}
|
||||
_ => {
|
||||
// Translate text sections
|
||||
println!(" 🔤 Translating text");
|
||||
let prompt = self.build_section_translation_prompt(§ion, config)?;
|
||||
let translated_text = self.call_ollama(&prompt, config).await?;
|
||||
|
||||
match section {
|
||||
MarkdownSection::Text(_) => MarkdownSection::Text(translated_text.trim().to_string()),
|
||||
MarkdownSection::Header(_, level) => MarkdownSection::Header(translated_text.trim().to_string(), level),
|
||||
MarkdownSection::Quote(_) => MarkdownSection::Quote(translated_text.trim().to_string()),
|
||||
MarkdownSection::List(_) => MarkdownSection::List(translated_text.trim().to_string()),
|
||||
_ => section,
|
||||
}
|
||||
}
|
||||
println!("📝 Found {} sections to process", sections.len());
|
||||
let translated_sections = self.translate_sections(sections, config).await?;
|
||||
|
||||
println!("✅ Rebuilding markdown from translated sections...");
|
||||
let result = self.parser.rebuild_markdown(translated_sections);
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
fn translate_sections(&self, sections: Vec<MarkdownSection>, config: &TranslationConfig) -> impl std::future::Future<Output = Result<Vec<MarkdownSection>>> + Send {
|
||||
let config = config.clone();
|
||||
let client = self.client.clone();
|
||||
let parser = self.parser.clone();
|
||||
let language_mapping = self.language_mapping.clone();
|
||||
|
||||
async move {
|
||||
let translator = OllamaTranslator {
|
||||
client,
|
||||
language_mapping,
|
||||
parser,
|
||||
};
|
||||
|
||||
translated_sections.push(translated_section);
|
||||
|
||||
// Add small delay to avoid overwhelming Ollama
|
||||
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
|
||||
let mut translated_sections = Vec::new();
|
||||
let start_time = Instant::now();
|
||||
|
||||
for (index, section) in sections.into_iter().enumerate() {
|
||||
println!(" 🔤 Processing section {}", index + 1);
|
||||
|
||||
let translated_section = match §ion {
|
||||
MarkdownSection::Code(_content, _lang) => {
|
||||
if config.preserve_code {
|
||||
println!(" ⏭️ Preserving code block");
|
||||
section // Preserve code blocks
|
||||
} else {
|
||||
section // Still preserve for now
|
||||
}
|
||||
}
|
||||
MarkdownSection::Link(text, url) => {
|
||||
if config.preserve_links {
|
||||
println!(" ⏭️ Preserving link");
|
||||
section // Preserve links
|
||||
} else {
|
||||
// Translate link text only
|
||||
let prompt = translator.build_section_translation_prompt(&MarkdownSection::Text(text.clone()), &config)?;
|
||||
let translated_text = translator.call_ollama(&prompt, &config).await?;
|
||||
MarkdownSection::Link(translated_text.trim().to_string(), url.clone())
|
||||
}
|
||||
}
|
||||
MarkdownSection::Image(_alt, _url) => {
|
||||
println!(" 🖼️ Preserving image");
|
||||
section // Preserve images
|
||||
}
|
||||
MarkdownSection::Table(content) => {
|
||||
println!(" 📊 Translating table content");
|
||||
let prompt = translator.build_section_translation_prompt(&MarkdownSection::Text(content.clone()), &config)?;
|
||||
let translated_content = translator.call_ollama(&prompt, &config).await?;
|
||||
MarkdownSection::Table(translated_content.trim().to_string())
|
||||
}
|
||||
_ => {
|
||||
// Translate text sections
|
||||
println!(" 🔤 Translating text");
|
||||
let prompt = translator.build_section_translation_prompt(§ion, &config)?;
|
||||
let translated_text = translator.call_ollama(&prompt, &config).await?;
|
||||
|
||||
match section {
|
||||
MarkdownSection::Text(_) => MarkdownSection::Text(translated_text.trim().to_string()),
|
||||
MarkdownSection::Header(_, level) => MarkdownSection::Header(translated_text.trim().to_string(), level),
|
||||
MarkdownSection::Quote(_) => MarkdownSection::Quote(translated_text.trim().to_string()),
|
||||
MarkdownSection::List(_) => MarkdownSection::List(translated_text.trim().to_string()),
|
||||
_ => section,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
translated_sections.push(translated_section);
|
||||
|
||||
// Add small delay to avoid overwhelming Ollama
|
||||
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
|
||||
}
|
||||
|
||||
let elapsed = start_time.elapsed();
|
||||
println!("⏱️ Translation completed in {:.2}s", elapsed.as_secs_f64());
|
||||
|
||||
Ok(translated_sections)
|
||||
}
|
||||
|
||||
let elapsed = start_time.elapsed();
|
||||
println!("⏱️ Translation completed in {:.2}s", elapsed.as_secs_f64());
|
||||
|
||||
Ok(translated_sections)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user