|
| 1 | +use crate::types::PermissionRule; |
| 2 | +use crate::{AgentConfig, AgentMode, AgentToolSettings}; |
| 3 | +use ahash::AHashMap; |
| 4 | +use indexmap::IndexMap; |
| 5 | +use reloaded_code_core::permissions::PermissionAction; |
| 6 | +use reloaded_code_core::tool_metadata::task as task_meta; |
| 7 | + |
| 8 | +/// Build an [`AgentConfig`] for tests. |
| 9 | +/// |
| 10 | +/// # Parameters |
| 11 | +/// - `name` - agent name |
| 12 | +/// - `mode` - agent operating mode |
| 13 | +/// - `description` - agent description |
| 14 | +/// - `permission` - initial permission rules |
| 15 | +/// |
| 16 | +/// # Returns |
| 17 | +/// A populated [`AgentConfig`] with all other fields set to defaults. |
| 18 | +pub(crate) fn agent( |
| 19 | + name: &str, |
| 20 | + mode: AgentMode, |
| 21 | + description: &str, |
| 22 | + permission: IndexMap<String, PermissionRule>, |
| 23 | +) -> AgentConfig { |
| 24 | + AgentConfig { |
| 25 | + name: name.into(), |
| 26 | + mode, |
| 27 | + description: description.into(), |
| 28 | + model: None, |
| 29 | + hidden: false, |
| 30 | + temperature: None, |
| 31 | + top_p: None, |
| 32 | + permission, |
| 33 | + options: AHashMap::new(), |
| 34 | + tool_settings: AgentToolSettings::default(), |
| 35 | + prompt: Default::default(), |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/// Map tool names to `Allow` permission rules. |
| 40 | +/// |
| 41 | +/// # Parameters |
| 42 | +/// - `names` - tool names that should be allowed. |
| 43 | +/// |
| 44 | +/// # Returns |
| 45 | +/// An ordered map of name → `PermissionRule::Action(Allow)`. |
| 46 | +pub(crate) fn allow_tools(names: &[&str]) -> IndexMap<String, PermissionRule> { |
| 47 | + names |
| 48 | + .iter() |
| 49 | + .map(|n| ((*n).into(), PermissionRule::Action(PermissionAction::Allow))) |
| 50 | + .collect() |
| 51 | +} |
| 52 | + |
| 53 | +/// Build task-scoped pattern permissions. |
| 54 | +/// |
| 55 | +/// Patterns are wrapped under the task metadata name so they apply to task execution. |
| 56 | +/// |
| 57 | +/// # Parameters |
| 58 | +/// - `patterns` - ordered pattern/action pairs. |
| 59 | +/// |
| 60 | +/// # Returns |
| 61 | +/// A single-entry map keyed by [`task_meta::NAME`] containing [`PermissionRule::Pattern`]. |
| 62 | +pub(crate) fn pattern_task( |
| 63 | + patterns: &[(&str, PermissionAction)], |
| 64 | +) -> IndexMap<String, PermissionRule> { |
| 65 | + let mut map = IndexMap::new(); |
| 66 | + for (pattern, action) in patterns { |
| 67 | + map.insert(pattern.to_string(), *action); |
| 68 | + } |
| 69 | + IndexMap::from([(task_meta::NAME.into(), PermissionRule::Pattern(map))]) |
| 70 | +} |
| 71 | + |
| 72 | +/// Return a deny-all rule for task execution. |
| 73 | +/// |
| 74 | +/// # Returns |
| 75 | +/// A single-entry map keyed by [`task_meta::NAME`] with `PermissionRule::Action(Deny)`. |
| 76 | +pub(crate) fn deny_task() -> IndexMap<String, PermissionRule> { |
| 77 | + IndexMap::from([( |
| 78 | + task_meta::NAME.into(), |
| 79 | + PermissionRule::Action(PermissionAction::Deny), |
| 80 | + )]) |
| 81 | +} |
0 commit comments