Skip to main content

base/
agent_type.rs

1//! Agent types: the named sub-agent definitions a session can spawn.
2//!
3//! The data only. Built-in types, loading `agents/*.md`, merging tiers and
4//! spawning live in the agents module; the type is here because plugin hosts
5//! declare agent types too, and they sit below that module.
6
7/// Where an [`AgentTypeDefinition`] came from, which decides whether it may
8/// hand its sub-agent more than the parent session had.
9///
10/// `permission_mode` and `max_turns` may loosen what the parent had only for
11/// a definition the user wrote or accepted: one of their own, or one in a
12/// project they trust. A plugin's, or an untrusted project's, is held to the
13/// parent's — and an untrusted project's does not replace a built-in, plugin
14/// or user type of the same name.
15#[derive(Debug, Clone, Default, PartialEq, Eq)]
16#[non_exhaustive]
17pub enum AgentTypeSource {
18    /// Shipped with Agix.
19    #[default]
20    Builtin,
21    /// The global or scene `agents/` directory.
22    User,
23    /// The project's `.agix/agents/`, and whether the project is trusted.
24    Project { trusted: bool },
25    /// Declared by the plugin package `plugin`.
26    Plugin { plugin: String },
27}
28
29impl AgentTypeSource {
30    /// May a definition from this source hand its sub-agent *more* than the
31    /// session it was spawned from already had?
32    pub fn may_loosen(&self) -> bool {
33        match self {
34            Self::Builtin | Self::User => true,
35            Self::Project { trusted } => *trusted,
36            Self::Plugin { .. } => false,
37        }
38    }
39
40    /// Does a definition from this source replace one of the same name from
41    /// the built-ins, a plugin or another tier?
42    pub fn may_shadow(&self) -> bool {
43        matches!(self, Self::User | Self::Project { trusted: true })
44    }
45}
46
47/// A named agent type definition with associated system prompt and tool set.
48#[derive(Debug, Clone, Default)]
49pub struct AgentTypeDefinition {
50    /// Unique name (e.g. "explore", "plan", "code-reviewer").
51    pub name: String,
52    /// Provenance, for the override clamp — see [`AgentTypeSource`].
53    pub source: AgentTypeSource,
54    /// Short description of the agent type's purpose.
55    pub description: String,
56    /// Tool names the agent type is allowed to use (empty = all tools).
57    pub allowed_tools: Vec<String>,
58    /// Tool names removed from the pool, applied before `allowed_tools`
59    /// (deny first, then allow).
60    pub disallowed_tools: Vec<String>,
61    /// The model its sub-agents use: `strong`, `balanced` or `fast` (also
62    /// `opus`, `sonnet`, `haiku`, as Claude Code writes them), `inherit` for
63    /// the parent's, or a model id.
64    pub model: Option<String>,
65    /// The provider its sub-agents' calls go to first, the rest following
66    /// `model.order`. One the session's pair does not define is ignored.
67    pub provider: Option<String>,
68    /// Optional permission mode for spawned subagents of this type. Whether
69    /// it may be looser than the parent session's depends on `source`.
70    pub permission_mode: Option<crate::permission::PermissionMode>,
71    /// Optional `ModelSettings::effort` override for spawned subagents, by
72    /// name (`low` … `max`).
73    pub effort: Option<String>,
74    /// Maximum API calls a spawned subagent of this type may make, applied
75    /// as this subagent's own `turn.max_model_calls` instead of
76    /// inheriting the parent's cap wholesale. `None` = inherit parent's cap.
77    pub max_turns: Option<u32>,
78    /// Skill names to preload in full at subagent startup (not just their
79    /// descriptions) — see `AgentTool`'s spawn path. A name that doesn't
80    /// resolve, or resolves to a `disable_model_invocation: true` skill, is
81    /// skipped with a `tracing::warn!` rather than failing the spawn.
82    pub skills: Vec<String>,
83    /// MCP server names to grant this subagent, referencing servers already
84    /// connected in the parent session (`settings.mcp_servers` keys) — the
85    /// parent's live connections are reused, not reconnected. A subagent
86    /// gets **zero** MCP tools by default — MCP access is opt-in per
87    /// subagent, not inherited; this is the opt-in list. Only name
88    /// references to an already-configured server are supported, not inline
89    /// server config.
90    pub mcp_servers: Vec<String>,
91    /// Scene id (`code`/`harnix`/`mini`) this agent type forces
92    /// its sub-agents into, overriding the scene inherited from the parent.
93    /// `None` (the default) inherits — see `Delegator::scene_for_subagent`. An
94    /// unknown id is ignored with a `tracing::warn!` and the inherited scene
95    /// is kept, same non-fatal handling as an unrecognized `effort`.
96    pub scene: Option<String>,
97    /// System prompt injected into the sub-agent's context.
98    pub system_prompt: String,
99}
100
101/// Parse the textual `permission_mode` an agent-type declaration carries —
102/// agent `.md` frontmatter and plugin manifests both spell it as a string.
103/// `bubble` is program-only and deliberately unparseable here.
104pub fn parse_permission_mode(value: &str) -> Option<crate::permission::PermissionMode> {
105    use crate::permission::PermissionMode;
106    match value {
107        "default" => Some(PermissionMode::Default),
108        "acceptEdits" | "accept_edits" | "accept-edits" => Some(PermissionMode::AcceptEdits),
109        "bypassPermissions" | "bypass_permissions" | "bypass-permissions" => {
110            Some(PermissionMode::BypassPermissions)
111        }
112        "plan" => Some(PermissionMode::Plan),
113        "dontAsk" | "dont_ask" | "dont-ask" => Some(PermissionMode::DontAsk),
114        _ => None,
115    }
116}