pub struct GuardrailEngine { /* private fields */ }Expand description
The per-generation compiled guardrail engine (DW-082): the
compiled rules from the ai.guardrails config block. Built at
dataplane refresh; immutable once built. Stored on the dataplane
behind an ArcSwap and swapped on reload, so a guardrail change
applies to the next request with no restart.
Implementations§
Source§impl GuardrailEngine
impl GuardrailEngine
Sourcepub fn compile(cfg: Option<&AiConfig>) -> Self
pub fn compile(cfg: Option<&AiConfig>) -> Self
Compile from the ai: config block’s guardrails section.
Absent or empty guardrails yields an empty engine (no rules ->
every prompt and response passes through uninspected).
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Whether the engine carries any rules (cheap dataplane skip — an empty engine allows everything and records nothing).
Sourcepub fn check_prompt(
&self,
consumer_policies: &[String],
route_policies: &[String],
service_policies: &[String],
listener_policies: &[String],
global_policies: &[String],
chat_req: &ChatRequest,
) -> GuardrailResult
pub fn check_prompt( &self, consumer_policies: &[String], route_policies: &[String], service_policies: &[String], listener_policies: &[String], global_policies: &[String], chat_req: &ChatRequest, ) -> GuardrailResult
Check the prompt (before the provider call). Runs all
prompt-phase rules (phase = Prompt or Both) that apply to the
consumer’s policy chain. The first block or redact result
wins (declaration order); log actions record but continue.
Sourcepub fn check_response(
&self,
consumer_policies: &[String],
route_policies: &[String],
service_policies: &[String],
listener_policies: &[String],
global_policies: &[String],
chat_resp: &ChatResponse,
) -> GuardrailResult
pub fn check_response( &self, consumer_policies: &[String], route_policies: &[String], service_policies: &[String], listener_policies: &[String], global_policies: &[String], chat_resp: &ChatResponse, ) -> GuardrailResult
Check the response (after the provider call, before returning
to the client). Runs all response-phase rules that apply to
the consumer’s policy chain. The first block result wins;
log actions record but continue. Non-streaming only (streaming
responses cannot be schema-validated on partial content).
Sourcepub fn check_stream_chunk(&self, chunk_text: &str) -> Option<String>
pub fn check_stream_chunk(&self, chunk_text: &str) -> Option<String>
Check a streaming chunk’s text against banned-content rules (response phase). Returns true if the chunk should be suppressed (a banned pattern matched). Used by AiStreamBody’s poll_frame to cut off banned content mid-stream.
Sourcepub fn redact_stream_chunk(&self, chunk_text: &str) -> String
pub fn redact_stream_chunk(&self, chunk_text: &str) -> String
AI-08 (#197): Redact PII from a streaming chunk’s text. Runs all response-phase PII rules’ redactors against the chunk text and returns the redacted string. If no PII rules are configured or no matches are found, the original text is returned unchanged. This enables mid-stream PII scrubbing for SSE responses without buffering the entire response.
Sourcepub fn check_text(&self, text: &str) -> GuardrailResult
pub fn check_text(&self, text: &str) -> GuardrailResult
AI-08 (#197): Check an arbitrary text string (e.g., an MCP
tool-call output) against response-phase guardrail rules.
Returns Block if a banned pattern matches with a block
action, Redact if a PII rule matches with a redact action
(carrying the redacted text), or Allow otherwise. Used to
inspect MCP tool outputs before they are returned to the
client.
Sourcepub fn apply_redaction(
&self,
chat_req: &ChatRequest,
redacted_prompt: &str,
) -> ChatRequest
pub fn apply_redaction( &self, chat_req: &ChatRequest, redacted_prompt: &str, ) -> ChatRequest
Apply prompt-phase redaction to a ChatRequest’s messages, returning a new ChatRequest with the redacted content. Used when a Redact result is returned: the caller replaces the original chat_req with the redacted one before the provider call.
Trait Implementations§
Source§impl Clone for GuardrailEngine
impl Clone for GuardrailEngine
Source§fn clone(&self) -> GuardrailEngine
fn clone(&self) -> GuardrailEngine
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for GuardrailEngine
impl Debug for GuardrailEngine
Source§impl Default for GuardrailEngine
impl Default for GuardrailEngine
Source§fn default() -> GuardrailEngine
fn default() -> GuardrailEngine
Auto Trait Implementations§
impl Freeze for GuardrailEngine
impl !RefUnwindSafe for GuardrailEngine
impl Send for GuardrailEngine
impl Sync for GuardrailEngine
impl Unpin for GuardrailEngine
impl !UnwindSafe for GuardrailEngine
Blanket Implementations§
§impl<T> AnyExt for T
impl<T> AnyExt for T
§fn downcast_ref<T>(this: &Self) -> Option<&T>where
T: Any,
fn downcast_ref<T>(this: &Self) -> Option<&T>where
T: Any,
T behind reference§fn downcast_mut<T>(this: &mut Self) -> Option<&mut T>where
T: Any,
fn downcast_mut<T>(this: &mut Self) -> Option<&mut T>where
T: Any,
T behind mutable reference§fn downcast_rc<T>(this: Rc<Self>) -> Result<Rc<T>, Rc<Self>>where
T: Any,
fn downcast_rc<T>(this: Rc<Self>) -> Result<Rc<T>, Rc<Self>>where
T: Any,
T behind Rc pointer§fn downcast_arc<T>(this: Arc<Self>) -> Result<Arc<T>, Arc<Self>>where
T: Any,
fn downcast_arc<T>(this: Arc<Self>) -> Result<Arc<T>, Arc<Self>>where
T: Any,
T behind Arc pointer§fn downcast_box<T>(this: Box<Self>) -> Result<Box<T>, Box<Self>>where
T: Any,
fn downcast_box<T>(this: Box<Self>) -> Result<Box<T>, Box<Self>>where
T: Any,
T behind Box pointer§fn downcast_move<T>(this: Self) -> Option<T>
fn downcast_move<T>(this: Self) -> Option<T>
Self to T,
useful only in generic context as a workaround for specializationSource§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T, X> CoerceTo<T> for Xwhere
T: CoerceFrom<X> + ?Sized,
impl<T, X> CoerceTo<T> for Xwhere
T: CoerceFrom<X> + ?Sized,
fn coerce_rc_to(self: Rc<X>) -> Rc<T>
fn coerce_box_to(self: Box<X>) -> Box<T>
fn coerce_ref_to(&self) -> &T
fn coerce_mut_to(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more