aither_core/llm/tool/
identity.rs1use alloc::boxed::Box;
8use alloc::string::{String, ToString};
9use alloc::vec::Vec;
10use core::fmt::{self, Display};
11
12#[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 #[must_use]
21 pub fn new(id: impl Into<String>) -> Self {
22 Self(id.into())
23 }
24
25 #[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#[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 #[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 #[must_use]
62 pub fn name(&self) -> &str {
63 &self.name
64 }
65
66 #[must_use]
68 pub fn started_by(&self) -> Option<&CallIdentity> {
69 self.started_by.as_deref()
70 }
71}
72
73#[derive(Debug, Clone, PartialEq, Eq, Hash)]
75#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
76pub enum AgentIdentity {
77 Main,
79 Subagent(SubagentIdentity),
81}
82
83#[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 #[must_use]
94 pub const fn new(call: ToolCallId, agent: AgentIdentity) -> Self {
95 Self { call, agent }
96 }
97
98 #[must_use]
100 pub const fn call(&self) -> &ToolCallId {
101 &self.call
102 }
103
104 #[must_use]
106 pub const fn agent(&self) -> &AgentIdentity {
107 &self.agent
108 }
109}
110
111#[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 #[must_use]
121 pub const fn main() -> Self {
122 Self(Vec::new())
123 }
124
125 #[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 #[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 #[must_use]
151 pub fn segments(&self) -> &[String] {
152 &self.0
153 }
154
155 #[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}