diff --git a/network-poc/native-node/src/inference.rs b/network-poc/native-node/src/inference.rs index 57fe516..fac39b1 100644 --- a/network-poc/native-node/src/inference.rs +++ b/network-poc/native-node/src/inference.rs @@ -225,7 +225,7 @@ impl LlmEngine { } else { 0.0 }; Ok(GenerateResult { - text: generated_text, + text: strip_markdown_wrapper(&generated_text), tokens_generated, duration_ms: gen_time.as_millis() as f64, tokens_per_sec, @@ -233,6 +233,31 @@ impl LlmEngine { } } +/// Poistaa mallin tuottaman markdown-wrapperin ja johdantotekstin. +fn strip_markdown_wrapper(text: &str) -> String { + let text = text.trim(); + if let Some(start) = text.find("```") { + let after = &text[start + 3..]; + let code_start = after.find('\n').map(|i| i + 1).unwrap_or(0); + let code = &after[code_start..]; + if let Some(end) = code.find("```") { + return code[..end].trim().to_string(); + } + return code.trim().to_string(); + } + 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(nl) = result.find('\n') { + result = result[nl + 1..].to_string(); + } + break; + } + } + result.trim().to_string() +} + pub struct GenerateResult { pub text: String, pub tokens_generated: usize, diff --git a/network-poc/node/src/qwen_coder.rs b/network-poc/node/src/qwen_coder.rs index 3b0ee5b..ca1007d 100644 --- a/network-poc/node/src/qwen_coder.rs +++ b/network-poc/node/src/qwen_coder.rs @@ -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>>> = RefCell::new(std::collections::HashMap::new()); static MODEL_CACHE: RefCell> = RefCell::new(None); @@ -295,7 +326,11 @@ pub async fn run_coder_inference(prompt: String, ws: Rc>, 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 }; diff --git a/network-poc/static/index.html b/network-poc/static/index.html index 23ae9fe..eba6a79 100644 --- a/network-poc/static/index.html +++ b/network-poc/static/index.html @@ -1090,7 +1090,7 @@ -
+
Hub: Yhdistetään... @@ -1640,8 +1640,10 @@ // Päivitetään agents-näkymän hub-status const hubDot = document.getElementById('agent-hub-dot'); const hubLabel = document.getElementById('agent-hub-label'); + const hubStatus = document.getElementById('agent-hub-status'); if (hubDot) hubDot.style.background = '#3fb950'; if (hubLabel) { hubLabel.textContent = 'Yhdistetty'; hubLabel.style.color = '#3fb950'; } + if (hubStatus) hubStatus.title = 'Yhdistetty Kipinä Hubiin — tehtävien jakelu ja solmujen koordinointi aktiivinen'; // Päivitetään molemmat statukset const el = document.getElementById('node-status'); @@ -1692,8 +1694,10 @@ uiSocket.onclose = () => { const hubDot = document.getElementById('agent-hub-dot'); const hubLabel = document.getElementById('agent-hub-label'); + const hubStatus2 = document.getElementById('agent-hub-status'); if (hubDot) hubDot.style.background = '#f85149'; if (hubLabel) { hubLabel.textContent = 'Yhteys katkennut'; hubLabel.style.color = '#f85149'; } + if (hubStatus2) hubStatus2.title = 'WebSocket-yhteys hubiin katkesi — tarkista verkkoyhteytesi tai hubin tila. Lataa sivu uudelleen yhdistääksesi.'; const el = document.getElementById('node-status'); el.textContent = 'Disconnected';