pub struct SessionApprovalCache { /* private fields */ }Available on crate feature
human-loop only.Expand description
会话级审批缓存
线程安全,使用 std::sync::RwLock 保护内部数据。
Key 的生成方式:
Sessionscope:tool_name + args_key(默认完整 JSON;启用 hash 模式时使用 SHA256)SessionAllToolsscope:tool_name(所有参数均匹配)
支持可选 TTL:缓存条目超过 TTL 后自动失效。
Implementations§
Source§impl SessionApprovalCache
impl SessionApprovalCache
Sourcepub fn new() -> SessionApprovalCache
pub fn new() -> SessionApprovalCache
创建空的审批缓存(永不过期,向后兼容)
Sourcepub fn with_ttl(ttl: Duration) -> SessionApprovalCache
pub fn with_ttl(ttl: Duration) -> SessionApprovalCache
创建带 TTL 的审批缓存
缓存条目超过 ttl 后自动失效。
参考:Claude Code 使用 1h(Max)/ 5min(Pro)TTL。
Sourcepub fn with_hash_args() -> SessionApprovalCache
pub fn with_hash_args() -> SessionApprovalCache
创建使用 SHA256 哈希模式缓存参数 key 的实例
当工具参数很大时,使用 SHA256 哈希值作为缓存 key 而非完整 JSON 字符串,显著减少内存占用。
Sourcepub fn set_hash_args(&mut self, enabled: bool)
pub fn set_hash_args(&mut self, enabled: bool)
启用/禁用 SHA256 哈希参数 key 模式
Sourcepub fn with_max_entries(self, max: usize) -> SessionApprovalCache
pub fn with_max_entries(self, max: usize) -> SessionApprovalCache
设置最大条目数
Sourcepub fn is_approved(&self, tool_name: &str, args: &Value) -> bool
pub fn is_approved(&self, tool_name: &str, args: &Value) -> bool
检查工具调用是否已被缓存审批
Sourcepub fn record_approval(
&self,
tool_name: &str,
args: &Value,
scope: ApprovalScope,
)
pub fn record_approval( &self, tool_name: &str, args: &Value, scope: ApprovalScope, )
记录一个审批决策
Sourcepub fn cleanup_expired(&self) -> usize
pub fn cleanup_expired(&self) -> usize
清理过期条目
扫描并移除所有超过 TTL 的缓存条目。 如果未设置 TTL,此方法无操作。
返回清理的条目数。
Trait Implementations§
Source§impl Clone for SessionApprovalCache
impl Clone for SessionApprovalCache
Source§fn clone(&self) -> SessionApprovalCache
fn clone(&self) -> SessionApprovalCache
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for SessionApprovalCache
impl Debug for SessionApprovalCache
Source§impl Default for SessionApprovalCache
impl Default for SessionApprovalCache
Source§fn default() -> SessionApprovalCache
fn default() -> SessionApprovalCache
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for SessionApprovalCache
impl RefUnwindSafe for SessionApprovalCache
impl Send for SessionApprovalCache
impl Sync for SessionApprovalCache
impl Unpin for SessionApprovalCache
impl UnsafeUnpin for SessionApprovalCache
impl UnwindSafe for SessionApprovalCache
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§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>
Converts
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>
Converts
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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::Request