Markdown-wrapper strippaus LLM-vastauksista + hub-status tooltip

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jaakko Vanhala
2026-04-05 08:41:23 +03:00
parent 88fd31ca8c
commit 949f34833f
3 changed files with 67 additions and 3 deletions

View File

@@ -27,6 +27,37 @@ struct CachedModel {
is_3b: bool,
}
/// Poistaa mallin tuottaman markdown-wrapperin ja johdantotekstin.
/// "Sure! Here is...\n```python\nprint('hi')\n```" → "print('hi')"
fn strip_markdown_wrapper(text: &str) -> String {
let text = text.trim();
// Jos vastaus sisältää ```-koodiblokin, ota vain sen sisältö
if let Some(start) = text.find("```") {
let after_backticks = &text[start + 3..];
// Ohita mahdollinen kielitunniste (```python, ```rust jne.)
let code_start = after_backticks.find('\n').map(|i| i + 1).unwrap_or(0);
let code = &after_backticks[code_start..];
// Etsi sulkeva ```
if let Some(end) = code.find("```") {
return code[..end].trim().to_string();
}
// Ei sulkevaa ``` — ota kaikki loput
return code.trim().to_string();
}
// Ei koodiblokkia — poista yleiset johdantolauseet alusta
let mut result = text.to_string();
let lower = result.to_lowercase();
for prefix in &["sure!", "here is", "here's", "certainly!", "below is"] {
if lower.starts_with(prefix) {
if let Some(newline) = result.find('\n') {
result = result[newline + 1..].to_string();
}
break;
}
}
result.trim().to_string()
}
thread_local! {
static RAM_CACHE: RefCell<std::collections::HashMap<String, Rc<Vec<u8>>>> = RefCell::new(std::collections::HashMap::new());
static MODEL_CACHE: RefCell<Option<CachedModel>> = RefCell::new(None);
@@ -295,7 +326,11 @@ pub async fn run_coder_inference(prompt: String, ws: Rc<RefCell<WebSocket>>, use
}
let gen_time = perf.now() - start_gen;
(generated_text, tokens_generated, gen_time)
// Siivotaan vastaus: poista markdown-koodiblokit ja johdantotekstit
let cleaned = strip_markdown_wrapper(&generated_text);
(cleaned, tokens_generated, gen_time)
});
let tokens_per_sec = if gen_time > 0.0 { (tokens_generated as f64 / gen_time) * 1000.0 } else { 0.0 };