pub struct PermissionHandler { /* private fields */ }Expand description
A user-supplied async callback invoked when the CLI requests permission to use a tool.
Construct with Self::new, passing an async fn or
async-block closure. Cheap to clone (Arc under the hood).
The handler runs inline on the duplex session’s task. The CLI is
blocked on the response while the handler runs, and the session reads
no stdout and accepts no other requests until it returns, so keep it
short: an async policy check (DB lookup, remote call) is fine. If the
decision needs human input, return PermissionDecision::Defer
promptly and answer via DuplexSession::respond_to_permission when
ready.
With DuplexOptions::turn_timeout set, the turn deadline includes
time spent in the handler and any deferred wait. On expiry the
handler’s future is dropped and the session closes. A handler invoked
while no deadline-bound operation is pending is not bounded.
Implementations§
Source§impl PermissionHandler
impl PermissionHandler
Sourcepub fn new<F, Fut>(f: F) -> Selfwhere
F: Fn(PermissionRequest) -> Fut + Send + Sync + 'static,
Fut: Future<Output = PermissionDecision> + Send + 'static,
pub fn new<F, Fut>(f: F) -> Selfwhere
F: Fn(PermissionRequest) -> Fut + Send + Sync + 'static,
Fut: Future<Output = PermissionDecision> + Send + 'static,
Wrap an async closure as a permission handler.
§Example
use claude_wrapper::duplex::{PermissionDecision, PermissionHandler};
let _handler = PermissionHandler::new(|req| async move {
if req.tool_name == "Bash" {
PermissionDecision::Deny { message: "no bash".into() }
} else {
PermissionDecision::Allow { updated_input: None }
}
});