From e8aa5d12a366a1fe14192e56321d39a7c9e5d87f Mon Sep 17 00:00:00 2001 From: sigoden Date: Mon, 3 Feb 2025 22:40:13 +0800 Subject: refactor: improve call_chat_completions function (#1143) --- src/utils/mod.rs | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) (limited to 'src/utils') diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 049f361..38617a6 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -61,10 +61,6 @@ pub fn parse_bool(value: &str) -> Option { } } -pub fn strip_think_tag(text: &str) -> Cow { - THINK_TAG_RE.replace_all(text, "") -} - pub fn estimate_token_length(text: &str) -> usize { let words: Vec<&str> = text.unicode_words().collect(); let mut output: f32 = 0.0; @@ -101,20 +97,16 @@ pub fn light_theme_from_colorfgbg(colorfgbg: &str) -> Option { Some(light) } -pub fn extract_block(input: &str) -> String { - let output: String = CODE_BLOCK_RE - .captures_iter(input) - .filter_map(|m| { - m.ok() - .and_then(|cap| cap.get(1)) - .map(|m| String::from(m.as_str())) - }) - .collect(); - if output.is_empty() { - input.trim().to_string() - } else { - output.trim().to_string() - } +pub fn strip_think_tag(text: &str) -> Cow { + THINK_TAG_RE.replace_all(text, "") +} + +pub fn extract_code_block(text: &str) -> &str { + CODE_BLOCK_RE + .captures(text) + .ok() + .and_then(|v| v?.get(1).map(|v| v.as_str().trim())) + .unwrap_or(text) } pub fn convert_option_string(value: &str) -> Option { -- cgit v1.2.3