use crate::error::LlmWasmError;
use crate::types::ChatRequest;
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum GuardResult {
Allow,
Block {
reason: String,
},
Modify(ChatRequest),
}
pub trait Guard: Send + Sync {
fn name(&self) -> &str;
fn check(&self, request: &ChatRequest) -> GuardResult;
}
#[derive(Debug, Clone)]
pub struct ContentGuard {
blocklist: Vec<String>,
lowered: Vec<String>,
whole_words: bool,
}
impl ContentGuard {
pub fn new(blocklist: Vec<String>) -> Self {
let lowered = blocklist.iter().map(|t| t.to_lowercase()).collect();
Self {
blocklist,
lowered,
whole_words: false,
}
}
pub fn whole_words(blocklist: Vec<String>) -> Self {
Self {
whole_words: true,
..Self::new(blocklist)
}
}
fn matches(&self, lower_text: &str, term: &str) -> bool {
if term.is_empty() {
return false;
}
if !self.whole_words {
return lower_text.contains(term);
}
lower_text.match_indices(term).any(|(i, m)| {
let before = lower_text[..i].chars().next_back();
let after = lower_text[i + m.len()..].chars().next();
!before.is_some_and(char::is_alphanumeric) && !after.is_some_and(char::is_alphanumeric)
})
}
}
impl Guard for ContentGuard {
fn name(&self) -> &str {
"content"
}
fn check(&self, request: &ChatRequest) -> GuardResult {
for message in &request.messages {
let lower = message.content.to_lowercase();
for (term, low) in self.blocklist.iter().zip(&self.lowered) {
if self.matches(&lower, low) {
return GuardResult::Block {
reason: format!("message contains blocked term '{term}'"),
};
}
}
}
GuardResult::Allow
}
}
#[derive(Debug, Clone)]
pub struct LengthGuard {
max_total_chars: usize,
}
impl LengthGuard {
pub fn new(max_total_chars: usize) -> Self {
Self { max_total_chars }
}
}
impl Guard for LengthGuard {
fn name(&self) -> &str {
"length"
}
fn check(&self, request: &ChatRequest) -> GuardResult {
let total: usize = request
.messages
.iter()
.map(|m| m.content.chars().count())
.sum();
if total > self.max_total_chars {
GuardResult::Block {
reason: format!(
"total message length {total} exceeds limit {}",
self.max_total_chars
),
}
} else {
GuardResult::Allow
}
}
}
pub struct GuardChain {
guards: Vec<Box<dyn Guard>>,
}
impl std::fmt::Debug for GuardChain {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("GuardChain")
.field(
"guards",
&self.guards.iter().map(|g| g.name()).collect::<Vec<_>>(),
)
.finish()
}
}
impl Default for GuardChain {
fn default() -> Self {
Self::new()
}
}
impl GuardChain {
pub fn new() -> Self {
Self { guards: Vec::new() }
}
#[allow(clippy::should_implement_trait)]
pub fn add(mut self, guard: impl Guard + 'static) -> Self {
self.guards.push(Box::new(guard));
self
}
pub fn check(&self, request: &ChatRequest) -> Result<Option<ChatRequest>, LlmWasmError> {
let current = request;
let mut owned: Option<ChatRequest> = None;
for guard in &self.guards {
let target = owned.as_ref().unwrap_or(current);
match guard.check(target) {
GuardResult::Allow => {}
GuardResult::Block { reason } => {
return Err(LlmWasmError::GuardBlocked {
guard: guard.name().to_string(),
reason,
});
}
GuardResult::Modify(modified) => {
owned = Some(modified);
}
}
}
Ok(owned)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::{ChatMessage, Role};
fn req(content: &str) -> ChatRequest {
ChatRequest::new("model", vec![ChatMessage::new(Role::User, content)])
}
#[test]
fn test_content_guard_blocks_blocklisted_term() {
let guard = ContentGuard::new(vec!["spam".into()]);
assert!(matches!(
guard.check(&req("buy spam now")),
GuardResult::Block { .. }
));
}
#[test]
fn test_content_guard_case_insensitive() {
let guard = ContentGuard::new(vec!["spam".into()]);
assert!(matches!(
guard.check(&req("Buy SPAM Now")),
GuardResult::Block { .. }
));
}
#[test]
fn test_content_guard_allows_clean_request() {
let guard = ContentGuard::new(vec!["spam".into()]);
assert!(matches!(
guard.check(&req("hello world")),
GuardResult::Allow
));
}
#[test]
fn test_length_guard_blocks_over_limit() {
let guard = LengthGuard::new(5);
assert!(matches!(
guard.check(&req("hello world")),
GuardResult::Block { .. }
));
}
#[test]
fn test_length_guard_allows_under_limit() {
let guard = LengthGuard::new(100);
assert!(matches!(guard.check(&req("hi")), GuardResult::Allow));
}
#[test]
fn test_length_guard_allows_at_exact_limit() {
let guard = LengthGuard::new(5);
assert!(matches!(guard.check(&req("hello")), GuardResult::Allow));
}
#[test]
fn test_guard_chain_allows_clean_request() {
let chain = GuardChain::new()
.add(ContentGuard::new(vec!["bad".into()]))
.add(LengthGuard::new(1000));
let result = chain.check(&req("good content")).unwrap();
assert!(result.is_none());
}
#[test]
fn test_guard_chain_stops_at_first_block() {
let chain = GuardChain::new()
.add(ContentGuard::new(vec!["spam".into()]))
.add(LengthGuard::new(3)); let result = chain.check(&req("spam"));
assert!(matches!(
result,
Err(LlmWasmError::GuardBlocked { guard, .. }) if guard == "content"
));
}
#[test]
fn test_guard_chain_empty_allows_everything() {
let chain = GuardChain::new();
assert!(chain.check(&req("anything")).unwrap().is_none());
}
#[test]
fn test_guard_chain_length_blocks() {
let chain = GuardChain::new().add(LengthGuard::new(2));
let result = chain.check(&req("too long"));
assert!(matches!(result, Err(LlmWasmError::GuardBlocked { .. })));
}
#[test]
fn test_guard_chain_debug_lists_names_and_result_clones() {
let chain = GuardChain::new()
.add(ContentGuard::new(vec![]))
.add(LengthGuard::new(1));
assert_eq!(
format!("{chain:?}"),
r#"GuardChain { guards: ["content", "length"] }"#
);
let r = GuardResult::Block { reason: "x".into() };
assert!(matches!(r.clone(), GuardResult::Block { reason } if reason == "x"));
}
#[test]
fn test_length_guard_counts_characters_not_bytes() {
let req = ChatRequest::new("m", vec![ChatMessage::new(Role::User, "\u{e9}".repeat(10))]);
assert!(matches!(
LengthGuard::new(10).check(&req),
GuardResult::Allow
));
assert!(matches!(
LengthGuard::new(9).check(&req),
GuardResult::Block { .. }
));
}
#[test]
fn test_whole_words_avoids_scunthorpe_problem() {
let g = ContentGuard::whole_words(vec!["ass".into(), "credit card".into(), "café".into()]);
let check = |t: &str| {
matches!(
g.check(&ChatRequest::new(
"m",
vec![ChatMessage::new(Role::User, t)]
)),
GuardResult::Block { .. }
)
};
assert!(!check("this class is about assessment"));
assert!(check("what an ASS."));
assert!(check("my Credit Card number"));
assert!(!check("creditcard"));
assert!(check("Le CAFÉ, s'il vous plaît")); assert!(!check("cafés")); let sub = ContentGuard::new(vec!["ass".into()]);
assert!(matches!(
sub.check(&ChatRequest::new(
"m",
vec![ChatMessage::new(Role::User, "class")]
)),
GuardResult::Block { .. }
));
let empty = ContentGuard::new(vec![String::new()]);
assert!(matches!(
empty.check(&ChatRequest::new(
"m",
vec![ChatMessage::new(Role::User, "x")]
)),
GuardResult::Allow
));
}
}