| 145 | } |
| 146 | |
| 147 | pub fn respond( |
| 148 | &mut self, |
| 149 | session_id: &str, |
| 150 | permission_id: &str, |
| 151 | response: Response, |
| 152 | ) -> Result<(), PermissionError> { |
| 153 | let session_pending = self.pending.get_mut(session_id).ok_or_else(|| { |
| 154 | PermissionError::NotFound(session_id.to_string(), permission_id.to_string()) |
| 155 | })?; |
| 156 | |
| 157 | let match_item = session_pending.remove(permission_id).ok_or_else(|| { |
| 158 | PermissionError::NotFound(session_id.to_string(), permission_id.to_string()) |
| 159 | })?; |
| 160 | |
| 161 | if response == Response::Reject { |
| 162 | return Err(PermissionError::Rejected { |
| 163 | session_id: session_id.to_string(), |
| 164 | permission_id: permission_id.to_string(), |
| 165 | tool_call_id: match_item.info.call_id.clone(), |
| 166 | }); |
| 167 | } |
| 168 | |
| 169 | if response == Response::Always { |
| 170 | let approved_session = self |
| 171 | .approved |
| 172 | .entry(session_id.to_string()) |
| 173 | .or_insert_with(HashMap::new); |
| 174 | let approve_keys = Self::to_keys( |
| 175 | match_item.info.pattern.as_ref(), |
| 176 | &match_item.info.permission_type, |
| 177 | ); |
| 178 | for k in approve_keys { |
| 179 | approved_session.insert(k, true); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | Ok(()) |
| 184 | } |
| 185 | |
| 186 | pub fn clear_session(&mut self, session_id: &str) { |
| 187 | self.pending.remove(session_id); |