1use adk_core::{Content, Result};
6
7#[cfg(feature = "guardrails")]
8use adk_core::AdkError;
9
10#[cfg(feature = "guardrails")]
11pub use adk_guardrail::{
12 ContentFilter, ContentFilterConfig, Guardrail, GuardrailExecutor, GuardrailResult,
13 GuardrailSet, PiiRedactor, PiiType, Severity,
14};
15
16#[cfg(feature = "guardrails")]
17pub use adk_guardrail::SchemaValidator;
18
19#[cfg(not(feature = "guardrails"))]
21pub struct GuardrailSet;
22
23#[cfg(not(feature = "guardrails"))]
24impl GuardrailSet {
25 pub fn new() -> Self {
27 Self
28 }
29 pub fn is_empty(&self) -> bool {
31 true
32 }
33}
34
35#[cfg(not(feature = "guardrails"))]
36impl Default for GuardrailSet {
37 fn default() -> Self {
38 Self::new()
39 }
40}
41
42#[cfg(feature = "guardrails")]
43pub(crate) async fn enforce_guardrails(
44 guardrails: &GuardrailSet,
45 content: &Content,
46 phase: &str,
47) -> Result<Content> {
48 let result = GuardrailExecutor::run(guardrails, content)
49 .await
50 .map_err(|err| AdkError::agent(format!("{phase} guardrail failed: {err}")))?;
51
52 if !result.passed {
53 let failures = result
54 .failures
55 .iter()
56 .map(|(name, reason, severity)| format!("{name} ({severity:?}): {reason}"))
57 .collect::<Vec<_>>()
58 .join("; ");
59 return Err(AdkError::agent(format!("{phase} guardrails blocked content: {failures}")));
60 }
61
62 Ok(result.transformed_content.unwrap_or_else(|| content.clone()))
63}
64
65#[cfg(not(feature = "guardrails"))]
66pub(crate) async fn enforce_guardrails(
67 _guardrails: &GuardrailSet,
68 content: &Content,
69 _phase: &str,
70) -> Result<Content> {
71 Ok(content.clone())
72}