use alloc::boxed::Box;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use core::fmt::{self, Display};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct ToolCallId(String);
impl ToolCallId {
#[must_use]
pub fn new(id: impl Into<String>) -> Self {
Self(id.into())
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl Display for ToolCallId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SubagentIdentity {
name: String,
started_by: Option<Box<CallIdentity>>,
}
impl SubagentIdentity {
#[must_use]
pub fn new(name: impl Into<String>, started_by: Option<CallIdentity>) -> Self {
Self {
name: name.into(),
started_by: started_by.map(Box::new),
}
}
#[must_use]
pub fn name(&self) -> &str {
&self.name
}
#[must_use]
pub fn started_by(&self) -> Option<&CallIdentity> {
self.started_by.as_deref()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum AgentIdentity {
Main,
Subagent(SubagentIdentity),
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CallIdentity {
call: ToolCallId,
agent: AgentIdentity,
}
impl CallIdentity {
#[must_use]
pub const fn new(call: ToolCallId, agent: AgentIdentity) -> Self {
Self { call, agent }
}
#[must_use]
pub const fn call(&self) -> &ToolCallId {
&self.call
}
#[must_use]
pub const fn agent(&self) -> &AgentIdentity {
&self.agent
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct AgentPath(Vec<String>);
impl AgentPath {
#[must_use]
pub const fn main() -> Self {
Self(Vec::new())
}
#[must_use]
pub fn of(identity: &AgentIdentity) -> Self {
match identity {
AgentIdentity::Main => Self::main(),
AgentIdentity::Subagent(subagent) => {
let mut path = subagent
.started_by()
.map_or_else(Self::main, |call| Self::of(call.agent()));
path.0.push(subagent.name().to_string());
path
}
}
}
#[must_use]
pub fn child(&self, name: impl Into<String>) -> Self {
let mut path = self.clone();
path.0.push(name.into());
path
}
#[must_use]
pub fn segments(&self) -> &[String] {
&self.0
}
#[must_use]
pub const fn is_main(&self) -> bool {
self.0.is_empty()
}
}
impl Display for AgentPath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.0.is_empty() {
return f.write_str("main");
}
f.write_str(&self.0.join("/"))
}
}