Skip to main content

AgentId

Struct AgentId 

Source
pub struct AgentId { /* private fields */ }
Expand description

A validated agent identifier in TypeID format.

The agent ID uniquely identifies an agent and encodes:

  • Semantic classification (the prefix)
  • Unique identity (the UUIDv7 suffix)
  • Creation timestamp (encoded in UUIDv7)

§Format

<prefix>_<suffix> where:

  • prefix: 1-63 lowercase letters and underscores
  • suffix: 26 character base32-encoded UUIDv7

§Examples

use agent_uri::AgentId;

let id = AgentId::parse("llm_01h455vb4pex5vsknk084sn02q").unwrap();
assert_eq!(id.prefix().as_str(), "llm");
assert_eq!(id.suffix(), "01h455vb4pex5vsknk084sn02q");

// Create a new agent ID
let id = AgentId::new("llm_chat");
assert_eq!(id.suffix().len(), 26);

§Constructing one

AgentId::parse reads an existing ID. The other two mint a fresh one and differ only in what they do with a prefix the grammar rejects: AgentId::new rewrites it, AgentId::try_new reports it. Prefer try_new for any prefix that did not come from your own source.

Implementations§

Source§

impl AgentId

Source

pub fn new(prefix: &str) -> Self

Creates a new agent ID with the given prefix and a fresh UUIDv7, sanitizing the prefix if the grammar does not already accept it.

This is the split mti draws, and this crate mints TypeIDs with mti: create_type_id sanitizes and try_create_type_id refuses. Sanitizing is described on AgentId::try_new’s counterpart below; in short, anything outside [a-z_] is dropped, uppercase is folded, and a prefix left with nothing becomes agent.

§Choosing between this and AgentId::try_new

Sanitizing means you get an agent ID with a prefix, not necessarily the one you named. That is what you want when the prefix is a label you wrote: minting an ID should not be fallible over a string literal. It is not what you want when the prefix arrived from somewhere — a peer, a config file, a URI — because a rewritten prefix is a different agent class than the one that was asked for, and silence about that is how a caller ends up registering under a name it never chose. Untrusted input belongs in AgentId::try_new or AgentId::parse, which refuse.

§Examples
use agent_uri::AgentId;

// A prefix the grammar already accepts is kept verbatim.
let id = AgentId::new("llm_chat");
assert_eq!(id.prefix().as_str(), "llm_chat");

// Anything else is rewritten rather than refused.
assert_eq!(AgentId::new("LLM Chat!").prefix().as_str(), "llmchat");
assert_eq!(AgentId::new("__llm__chat__").prefix().as_str(), "llm_chat");
assert_eq!(AgentId::new("123").prefix().as_str(), "agent");

// Every one of them is a parsable agent ID.
assert!(AgentId::parse(&AgentId::new("123").to_string()).is_ok());
Source

pub fn try_new(prefix: &str) -> Result<Self, AgentIdError>

Creates a new agent ID with the given prefix and a fresh UUIDv7, refusing a prefix the grammar does not accept.

Use this whenever the prefix came from outside your own source: it reports what AgentId::new would have quietly rewritten.

§Errors

Returns AgentIdError if the prefix is invalid.

§Examples
use agent_uri::AgentId;

assert!(AgentId::try_new("llm_chat").is_ok());
assert!(AgentId::try_new("LLM Chat!").is_err());
Source

pub fn parse(input: &str) -> Result<Self, AgentIdError>

Parses an agent ID from a string.

§Errors

Returns AgentIdError if:

  • The input is empty
  • The input exceeds 90 characters
  • The prefix is invalid
  • The suffix is invalid (not valid base32 or wrong length)
  • The separator is missing
Source

pub const fn prefix(&self) -> &AgentPrefix

Returns the prefix (semantic classification).

Source

pub fn suffix(&self) -> &str

Returns the suffix (base32-encoded UUIDv7).

Source

pub const fn inner(&self) -> &MagicTypeId

Returns the underlying MagicTypeId.

Source

pub fn uuid(&self) -> Result<Uuid, AgentIdError>

Returns the UUID from the suffix.

§Errors

Returns an error if UUID extraction fails.

Trait Implementations§

Source§

impl AsRef<MagicTypeId> for AgentId

Source§

fn as_ref(&self) -> &MagicTypeId

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for AgentId

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for AgentId

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for AgentId

Available on crate feature serde only.
Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for AgentId

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for AgentId

Source§

impl FromStr for AgentId

Source§

type Err = AgentIdError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for AgentId

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for AgentId

Orders by the rendered prefix_suffix, which is what Display writes.

The comparison is the one this always made; what is gone is the pair of Strings it used to build to make it. MagicTypeId keeps its own rendering and hands out a borrow of it, so nothing has to be rendered again here. MagicTypeId’s own Ord is not that comparison — it sorts by suffix first, which would order agents by creation time rather than by class.

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for AgentId

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for AgentId

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for AgentId

Available on crate feature serde only.
Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for AgentId

Source§

impl TryFrom<&str> for AgentId

Source§

type Error = AgentIdError

The type returned in the event of a conversion error.
Source§

fn try_from(s: &str) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.