BlueGit

ict rusty-teams

Files

rusty-teams / crates / mcp / src / policy.rs

policy.rs

policy.rs 5.5 KiB Download Raw
1use std::collections::BTreeSet;
2
3pub const DEFAULT_PORT: u16 = 40615;
4
5#[derive(Debug, Clone, PartialEq, Eq, Default)]
6pub enum Access {
7 #[default]
8 None,
9 Selected(BTreeSet<String>),
10 All,
11}
12
13impl Access {
14 pub fn allows(&self, conversation_id: &str) -> bool {
15 match self {
16 Access::None => false,
17 Access::Selected(ids) => ids.contains(conversation_id),
18 Access::All => true,
19 }
20 }
21}
22
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24pub enum ConversationKind {
25 Chat,
26 Meeting,
27 Channel,
28}
29
30#[derive(Debug, Clone, PartialEq, Eq)]
31pub struct AgentPolicy {
32 pub enabled: bool,
33 pub port: u16,
34 pub chats: Access,
35 pub channels: Access,
36 pub propose: bool,
37 pub calendar: bool,
38 pub transcripts: bool,
39}
40
41impl Default for AgentPolicy {
42 fn default() -> Self {
43 AgentPolicy {
44 enabled: false,
45 port: DEFAULT_PORT,
46 chats: Access::None,
47 channels: Access::None,
48 propose: false,
49 calendar: false,
50 transcripts: false,
51 }
52 }
53}
54
55impl AgentPolicy {
56 pub fn access(&self, kind: ConversationKind) -> &Access {
57 match kind {
58 ConversationKind::Chat | ConversationKind::Meeting => &self.chats,
59 ConversationKind::Channel => &self.channels,
60 }
61 }
62
63 pub fn can_read(&self, kind: ConversationKind, conversation_id: &str) -> bool {
64 self.enabled && self.access(kind).allows(conversation_id)
65 }
66
67 pub fn can_propose(&self, kind: ConversationKind, conversation_id: &str) -> bool {
68 self.can_read(kind, conversation_id) && self.propose
69 }
70
71 pub fn can_read_any_kind(&self, conversation_id: &str) -> bool {
72 self.enabled
73 && (self.chats.allows(conversation_id) || self.channels.allows(conversation_id))
74 }
75
76 pub fn readable_ids<'a>(
77 &self,
78 kind: ConversationKind,
79 conversation_ids: impl IntoIterator<Item = &'a str>,
80 ) -> Vec<String> {
81 conversation_ids
82 .into_iter()
83 .filter(|conversation_id| self.can_read(kind, conversation_id))
84 .map(str::to_owned)
85 .collect()
86 }
87}
88
89#[cfg(test)]
90mod tests {
91 use super::*;
92
93 fn selected(ids: &[&str]) -> Access {
94 Access::Selected(ids.iter().map(|id| (*id).to_owned()).collect())
95 }
96
97 fn policy(chats: Access, channels: Access, propose: bool) -> AgentPolicy {
98 AgentPolicy {
99 enabled: true,
100 chats,
101 channels,
102 propose,
103 ..AgentPolicy::default()
104 }
105 }
106
107 #[test]
108 fn read_and_propose_follow_scope_and_flags() {
109 use ConversationKind::{Channel, Chat, Meeting};
110 let cases = [
111 (
112 policy(Access::None, Access::None, true),
113 Chat,
114 "a",
115 false,
116 false,
117 ),
118 (
119 policy(Access::All, Access::None, false),
120 Chat,
121 "a",
122 true,
123 false,
124 ),
125 (
126 policy(Access::All, Access::None, true),
127 Chat,
128 "a",
129 true,
130 true,
131 ),
132 (
133 policy(selected(&["a"]), Access::None, true),
134 Chat,
135 "a",
136 true,
137 true,
138 ),
139 (
140 policy(selected(&["a"]), Access::None, true),
141 Chat,
142 "b",
143 false,
144 false,
145 ),
146 (
147 policy(selected(&["a"]), Access::None, true),
148 Meeting,
149 "a",
150 true,
151 true,
152 ),
153 (
154 policy(selected(&["a"]), Access::None, true),
155 Channel,
156 "a",
157 false,
158 false,
159 ),
160 (
161 policy(Access::None, Access::All, true),
162 Channel,
163 "a",
164 true,
165 true,
166 ),
167 (
168 policy(Access::All, selected(&["c"]), false),
169 Channel,
170 "d",
171 false,
172 false,
173 ),
174 ];
175 for (policy, kind, id, read, propose) in cases {
176 assert_eq!(policy.can_read(kind, id), read, "{kind:?} {id}");
177 assert_eq!(policy.can_propose(kind, id), propose, "{kind:?} {id}");
178 }
179 }
180
181 #[test]
182 fn disabled_policy_allows_nothing() {
183 let policy = AgentPolicy {
184 enabled: false,
185 chats: Access::All,
186 channels: Access::All,
187 propose: true,
188 ..AgentPolicy::default()
189 };
190 assert!(!policy.can_read(ConversationKind::Chat, "a"));
191 assert!(!policy.can_propose(ConversationKind::Channel, "a"));
192 }
193
194 #[test]
195 fn default_is_closed() {
196 let policy = AgentPolicy::default();
197 assert_eq!(policy.port, DEFAULT_PORT);
198 assert!(!policy.enabled && !policy.propose && !policy.calendar && !policy.transcripts);
199 assert_eq!(policy.chats, Access::None);
200 assert_eq!(policy.channels, Access::None);
201 }
202
203 #[test]
204 fn readable_ids_keeps_only_allowed() {
205 let policy = policy(selected(&["a", "c"]), Access::None, false);
206 assert_eq!(
207 policy.readable_ids(ConversationKind::Chat, ["a", "b", "c"]),
208 ["a", "c"]
209 );
210 }
211}