llama_cpp_v3_agent_sdk/
permission.rs1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
5pub enum PermissionDecision {
6 AllowOnce,
8 AllowAlways,
10 DenyOnce,
12 DenyAlways,
14}
15
16impl PermissionDecision {
17 pub fn is_allowed(self) -> bool {
18 matches!(self, Self::AllowOnce | Self::AllowAlways)
19 }
20}
21
22#[derive(Debug, Clone, Serialize, Deserialize)]
24pub struct PermissionRequest {
25 pub tool_name: String,
27 pub description: String,
29 pub dangerous: bool,
31 pub arguments: serde_json::Value,
33}
34
35pub enum PermissionMode {
37 AutoApprove,
39 Callback(Box<dyn Fn(&PermissionRequest) -> PermissionDecision + Send + Sync>),
41}
42
43impl std::fmt::Debug for PermissionMode {
44 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45 match self {
46 PermissionMode::AutoApprove => write!(f, "AutoApprove"),
47 PermissionMode::Callback(_) => write!(f, "Callback(...)"),
48 }
49 }
50}
51
52pub struct PermissionTracker {
54 mode: PermissionMode,
55 always_allowed: std::collections::HashSet<String>,
57 always_denied: std::collections::HashSet<String>,
59}
60
61impl PermissionTracker {
62 pub fn new(mode: PermissionMode) -> Self {
63 Self {
64 mode,
65 always_allowed: std::collections::HashSet::new(),
66 always_denied: std::collections::HashSet::new(),
67 }
68 }
69
70 pub fn check(&mut self, request: &PermissionRequest) -> bool {
74 if self.always_allowed.contains(&request.tool_name) {
76 return true;
77 }
78 if self.always_denied.contains(&request.tool_name) {
79 return false;
80 }
81
82 match &self.mode {
83 PermissionMode::AutoApprove => true,
84 PermissionMode::Callback(cb) => {
85 let decision = cb(request);
86 match decision {
87 PermissionDecision::AllowAlways => {
88 self.always_allowed.insert(request.tool_name.clone());
89 true
90 }
91 PermissionDecision::DenyAlways => {
92 self.always_denied.insert(request.tool_name.clone());
93 false
94 }
95 PermissionDecision::AllowOnce => true,
96 PermissionDecision::DenyOnce => false,
97 }
98 }
99 }
100 }
101}