Skip to main content

aither_core/llm/tool/
identity.rs

1//! Who made a tool call: the call's id and the agent behind it.
2//!
3//! The agent loop attaches a [`CallIdentity`] to the [`ToolContext`](super::ToolContext)
4//! of every call it dispatches, so a tool, and any command the tool runs on its
5//! behalf, can attribute what it did to the exact call and agent.
6
7use alloc::boxed::Box;
8use alloc::string::{String, ToString};
9use alloc::vec::Vec;
10use core::fmt::{self, Display};
11
12/// The id a model provider gave one tool call.
13#[derive(Debug, Clone, PartialEq, Eq, Hash)]
14#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
15#[cfg_attr(feature = "serde", serde(transparent))]
16pub struct ToolCallId(String);
17
18impl ToolCallId {
19    /// Wraps a provider-assigned tool call id.
20    #[must_use]
21    pub fn new(id: impl Into<String>) -> Self {
22        Self(id.into())
23    }
24
25    /// The id as the provider spelled it.
26    #[must_use]
27    pub fn as_str(&self) -> &str {
28        &self.0
29    }
30}
31
32impl Display for ToolCallId {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        f.write_str(&self.0)
35    }
36}
37
38/// A subagent: its type and the call that started it.
39///
40/// The starting call's identity makes the subagent unique across nested
41/// subagents and ties it to the call that launched it.
42#[derive(Debug, Clone, PartialEq, Eq, Hash)]
43#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
44pub struct SubagentIdentity {
45    name: String,
46    started_by: Option<Box<CallIdentity>>,
47}
48
49impl SubagentIdentity {
50    /// A subagent of type `name` started by `started_by`; `None` when the
51    /// dispatcher of the starting call attached no identity.
52    #[must_use]
53    pub fn new(name: impl Into<String>, started_by: Option<CallIdentity>) -> Self {
54        Self {
55            name: name.into(),
56            started_by: started_by.map(Box::new),
57        }
58    }
59
60    /// The subagent type it runs as.
61    #[must_use]
62    pub fn name(&self) -> &str {
63        &self.name
64    }
65
66    /// The call that started this subagent, if its dispatcher identified it.
67    #[must_use]
68    pub fn started_by(&self) -> Option<&CallIdentity> {
69        self.started_by.as_deref()
70    }
71}
72
73/// The agent making a tool call.
74#[derive(Debug, Clone, PartialEq, Eq, Hash)]
75#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
76pub enum AgentIdentity {
77    /// The main agent.
78    Main,
79    /// A subagent, at any nesting depth.
80    Subagent(SubagentIdentity),
81}
82
83/// What identifies one tool call: its id and the agent that made it.
84#[derive(Debug, Clone, PartialEq, Eq, Hash)]
85#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
86pub struct CallIdentity {
87    call: ToolCallId,
88    agent: AgentIdentity,
89}
90
91impl CallIdentity {
92    /// The identity of call `call` made by `agent`.
93    #[must_use]
94    pub const fn new(call: ToolCallId, agent: AgentIdentity) -> Self {
95        Self { call, agent }
96    }
97
98    /// The call's id.
99    #[must_use]
100    pub const fn call(&self) -> &ToolCallId {
101        &self.call
102    }
103
104    /// The agent that made the call.
105    #[must_use]
106    pub const fn agent(&self) -> &AgentIdentity {
107        &self.agent
108    }
109}
110
111/// Where an agent stands in the tree of agents a run started: empty for the
112/// main agent, the subagent's name beneath its parent's path otherwise.
113#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
114#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
115#[cfg_attr(feature = "serde", serde(transparent))]
116pub struct AgentPath(Vec<String>);
117
118impl AgentPath {
119    /// The main agent.
120    #[must_use]
121    pub const fn main() -> Self {
122        Self(Vec::new())
123    }
124
125    /// The path of the agent `identity` names, beneath the agent that
126    /// started it.
127    #[must_use]
128    pub fn of(identity: &AgentIdentity) -> Self {
129        match identity {
130            AgentIdentity::Main => Self::main(),
131            AgentIdentity::Subagent(subagent) => {
132                let mut path = subagent
133                    .started_by()
134                    .map_or_else(Self::main, |call| Self::of(call.agent()));
135                path.0.push(subagent.name().to_string());
136                path
137            }
138        }
139    }
140
141    /// The path of a child named `name` beneath this agent.
142    #[must_use]
143    pub fn child(&self, name: impl Into<String>) -> Self {
144        let mut path = self.clone();
145        path.0.push(name.into());
146        path
147    }
148
149    /// The names from the main agent down to this one.
150    #[must_use]
151    pub fn segments(&self) -> &[String] {
152        &self.0
153    }
154
155    /// Whether this is the main agent.
156    #[must_use]
157    pub const fn is_main(&self) -> bool {
158        self.0.is_empty()
159    }
160}
161
162impl Display for AgentPath {
163    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
164        if self.0.is_empty() {
165            return f.write_str("main");
166        }
167        f.write_str(&self.0.join("/"))
168    }
169}