pub struct PromptInjectionHook { /* private fields */ }Expand description
Detects and sanitizes prompt injections in tool output.
Scans tool results in the on_after_tool_call phase; when any pattern in
DEFAULT_INJECTION_PATTERNS (or a custom pattern) matches, replaces the whole
result with a safe placeholder so malicious instructions never reach the next
plan() prompt (blocking cross-turn pollution).
§Example
ⓘ
use lc_agents::hooks::PromptInjectionHook;
let executor = AgentExecutor::new(agent, tools)
.hook(PromptInjectionHook::new());Implementations§
Source§impl PromptInjectionHook
impl PromptInjectionHook
Sourcepub fn with_patterns(self, patterns: Vec<String>) -> Self
pub fn with_patterns(self, patterns: Vec<String>) -> Self
Replaces the default patterns with a custom list.
Sourcepub fn with_marker(self, marker: impl Into<String>) -> Self
pub fn with_marker(self, marker: impl Into<String>) -> Self
Custom replacement placeholder; when it contains {}, it is filled with the matched pattern text.
Sourcepub fn detect(&self, text: &str) -> Option<&str>
pub fn detect(&self, text: &str) -> Option<&str>
Detects whether the text contains an injection pattern; returns the matched pattern (None if no match).
Sourcepub fn detected_count(&self) -> usize
pub fn detected_count(&self) -> usize
Cumulative number of injections detected.
Trait Implementations§
Source§impl AgentHook for PromptInjectionHook
impl AgentHook for PromptInjectionHook
Source§fn on_after_tool_call(
&self,
ctx: &mut ToolResultContext,
) -> Result<(), HookError>
fn on_after_tool_call( &self, ctx: &mut ToolResultContext, ) -> Result<(), HookError>
Called after a tool call. Can modify the result.
Source§fn on_before_completion(&self, _ctx: &mut CompletionContext) -> CompletionAction
fn on_before_completion(&self, _ctx: &mut CompletionContext) -> CompletionAction
Called before an LLM completion. Can modify messages or reject the call.
Source§fn on_after_completion(
&self,
_ctx: &mut CompletionResult,
) -> Result<(), HookError>
fn on_after_completion( &self, _ctx: &mut CompletionResult, ) -> Result<(), HookError>
Called after an LLM completion. Can modify the response.
Source§fn on_before_tool_call(&self, _ctx: &mut ToolCallContext) -> ToolCallAction
fn on_before_tool_call(&self, _ctx: &mut ToolCallContext) -> ToolCallAction
Called before a tool call. Can approve, reject, modify, or skip.
Source§fn on_stream_chunk(&self, chunk: &str) -> StreamAction
fn on_stream_chunk(&self, chunk: &str) -> StreamAction
Called for each streaming token. Can filter, replace, or forward.
Source§fn on_agent_start(&self, _input: &str) -> Result<(), HookError>
fn on_agent_start(&self, _input: &str) -> Result<(), HookError>
Called when the agent starts execution.
Source§fn on_agent_end(&self, _output: &str) -> Result<(), HookError>
fn on_agent_end(&self, _output: &str) -> Result<(), HookError>
Called when the agent finishes execution.
Source§fn on_error(&self, _error: &HookError) -> ErrorAction
fn on_error(&self, _error: &HookError) -> ErrorAction
Called when an error occurs. Can retry, ignore, or propagate.
Auto Trait Implementations§
impl !Freeze for PromptInjectionHook
impl RefUnwindSafe for PromptInjectionHook
impl Send for PromptInjectionHook
impl Sync for PromptInjectionHook
impl Unpin for PromptInjectionHook
impl UnsafeUnpin for PromptInjectionHook
impl UnwindSafe for PromptInjectionHook
Blanket Implementations§
Source§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
Mutably borrows from an owned value. Read more