use serde_json::Value;
use crate::SecretaryError;
pub fn cleanup_thinking_blocks(content: String) -> String {
let mut is_thinking: bool = false;
let mut result: String = String::new();
let mut first_line = true;
for line in content.lines() {
if line.trim() == "<think>" {
is_thinking = true;
continue;
}
if line.trim() == "</think>" {
is_thinking = false;
continue;
}
if !is_thinking {
if !first_line {
result.push('\n');
}
result.push_str(line);
first_line = false;
}
}
result
}
pub fn extract_result_content(content: &str) -> String {
if let Some(start) = content.find("<result>") {
if let Some(end) = content.find("</result>") {
if start < end {
return content[start + 8..end].trim().to_string();
}
}
}
content.trim().to_string()
}
pub fn format_additional_instructions(additional_instructions: &Vec<String>) -> String {
let mut prompt: String = String::new();
if !additional_instructions.is_empty() {
prompt.push_str("\nAdditional instructions:\n");
for instruction in additional_instructions {
prompt.push_str(&format!("- {}\n", instruction));
}
}
prompt
}
pub fn extract_text_content_from_llm_response(
api_response: &str,
) -> Result<String, Box<dyn std::error::Error + Send + Sync + 'static>> {
let value: Value = serde_json::from_str(&api_response)?;
match value["choices"][0]["message"]["content"].as_str() {
Some(result) => Ok(result.to_string()),
None => return Err(SecretaryError::NoLLMResponse.into()),
}
}