pub fn inject_memory_prompt(system_instructions: &str, memory_summary: Option<&str>) -> String {
let Some(memory) = memory_summary else {
return system_instructions.to_string();
};
if memory.trim().is_empty() {
return system_instructions.to_string();
}
format!("{}\n\nMemory Context:\n{}", system_instructions, memory.trim())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn inject_memory_prompt_with_memory() {
let base = "You are a helpful assistant.";
let memory = "Previous conversation about Rust programming.";
let result = inject_memory_prompt(base, Some(memory));
assert!(result.contains(base));
assert!(result.contains("Memory Context:"));
assert!(result.contains(memory));
}
#[test]
fn inject_memory_prompt_without_memory() {
let base = "You are a helpful assistant.";
let result = inject_memory_prompt(base, None);
assert_eq!(result, base);
}
#[test]
fn inject_memory_prompt_with_empty_memory() {
let base = "You are a helpful assistant.";
let result = inject_memory_prompt(base, Some(""));
assert_eq!(result, base);
}
#[test]
fn inject_memory_prompt_with_whitespace_memory() {
let base = "You are a helpful assistant.";
let result = inject_memory_prompt(base, Some(" \n\t "));
assert_eq!(result, base);
}
#[test]
fn inject_memory_prompt_preserves_formatting() {
let base = "You are a helpful assistant.\nThink step by step.";
let memory = "User prefers detailed explanations.";
let result = inject_memory_prompt(base, Some(memory));
assert!(result.starts_with(base));
assert!(result.contains("\n\nMemory Context:\n"));
assert!(result.ends_with(memory));
}
}