pub struct AgentBuilder { /* private fields */ }Expand description
Builder for configuring an Agent before connecting to the network.
The builder allows customization of the agent’s identity:
- Machine key path: Where to store/load the machine keypair
- Agent keypair: Import a portable agent identity from another machine
- User keypair: Bind a human identity to this agent
§Example
use x0x::Agent;
// Default: auto-generates both keypairs
let agent = Agent::builder()
.build()
.await?;
// Custom machine key path
let agent = Agent::builder()
.with_machine_key("/custom/path/machine.key")
.build()
.await?;
// Import agent keypair
let agent_kp = load_agent_keypair()?;
let agent = Agent::builder()
.with_agent_key(agent_kp)
.build()
.await?;
// With user identity (three-layer)
let agent = Agent::builder()
.with_user_key_path("~/.x0x/user.key")
.build()
.await?;Implementations§
Source§impl AgentBuilder
impl AgentBuilder
Sourcepub fn with_machine_key<P: AsRef<Path>>(self, path: P) -> Self
pub fn with_machine_key<P: AsRef<Path>>(self, path: P) -> Self
Sourcepub fn with_agent_key(self, keypair: AgentKeypair) -> Self
pub fn with_agent_key(self, keypair: AgentKeypair) -> Self
Import an agent keypair.
If not set, the agent keypair is loaded from storage (or generated fresh if no stored key exists).
This enables running the same agent on multiple machines by importing the same agent keypair (but with different machine keypairs).
Note: When an explicit keypair is provided via this method, it takes
precedence over with_agent_key_path().
§Arguments
keypair- The agent keypair to import.
§Returns
Self for chaining.
Sourcepub fn with_agent_key_path<P: AsRef<Path>>(self, path: P) -> Self
pub fn with_agent_key_path<P: AsRef<Path>>(self, path: P) -> Self
Set a custom path for the agent keypair.
If not set, the agent keypair is stored in ~/.x0x/agent.key.
If no stored key is found at the path, a fresh one is generated and saved.
This is ignored when with_agent_key() provides an explicit keypair.
§Arguments
path- The path to store/load the agent keypair.
§Returns
Self for chaining.
Sourcepub fn with_network_config(self, config: NetworkConfig) -> Self
pub fn with_network_config(self, config: NetworkConfig) -> Self
Sourcepub fn with_peer_cache_dir<P: AsRef<Path>>(self, path: P) -> Self
pub fn with_peer_cache_dir<P: AsRef<Path>>(self, path: P) -> Self
Set the directory for the bootstrap peer cache.
The cache persists peer quality metrics across restarts, enabling
cache-first join strategy. Defaults to ~/.x0x/peers/ if not set.
Falls back to ./.x0x/peers/ (relative to CWD) if $HOME is unset.
Sourcepub fn with_user_key(self, keypair: UserKeypair) -> Self
pub fn with_user_key(self, keypair: UserKeypair) -> Self
Import a user keypair for three-layer identity.
This binds a human identity to this agent. When provided, an
identity::AgentCertificate is automatically issued (if one
doesn’t already exist in storage) to cryptographically attest
that this agent belongs to the user.
Note: When an explicit keypair is provided via this method, it takes
precedence over with_user_key_path().
§Arguments
keypair- The user keypair to import.
§Returns
Self for chaining.
Sourcepub fn with_user_key_path<P: AsRef<Path>>(self, path: P) -> Self
pub fn with_user_key_path<P: AsRef<Path>>(self, path: P) -> Self
Set a custom path for the user keypair.
Unlike machine and agent keys, user keys are not auto-generated. If the file at this path doesn’t exist, no user identity is set (the agent operates with two-layer identity).
This is ignored when with_user_key() provides an explicit keypair.
§Arguments
path- The path to load the user keypair from.
§Returns
Self for chaining.
Sourcepub fn with_heartbeat_interval(self, secs: u64) -> Self
pub fn with_heartbeat_interval(self, secs: u64) -> Self
Set the identity heartbeat re-announcement interval.
Defaults to IDENTITY_HEARTBEAT_INTERVAL_SECS (300 seconds).
§Arguments
secs- Interval in seconds between identity re-announcements.
Sourcepub fn with_identity_ttl(self, secs: u64) -> Self
pub fn with_identity_ttl(self, secs: u64) -> Self
Set the identity cache TTL.
Cache entries with last_seen older than this threshold are filtered
from Agent::presence and Agent::discovered_agents.
Defaults to IDENTITY_TTL_SECS (900 seconds).
§Arguments
secs- Time-to-live in seconds for discovered agent entries.
Sourcepub fn with_contact_store_path<P: AsRef<Path>>(self, path: P) -> Self
pub fn with_contact_store_path<P: AsRef<Path>>(self, path: P) -> Self
Set a custom path for the contacts file.
The contacts file persists trust levels and machine records for known
agents. Defaults to ~/.x0x/contacts.json if not set.
§Arguments
path- The path for the contacts file.
Sourcepub async fn build(self) -> Result<Agent>
pub async fn build(self) -> Result<Agent>
Build and initialise the agent.
This performs the following:
- Loads or generates the machine keypair (stored in
~/.x0x/machine.keyby default) - Uses provided agent keypair or generates a fresh one
- Combines both into a unified Identity
The machine keypair is automatically persisted to storage.
§Errors
Returns an error if:
- Machine keypair generation fails
- Storage I/O fails
- Keypair deserialization fails
Trait Implementations§
Auto Trait Implementations§
impl Freeze for AgentBuilder
impl RefUnwindSafe for AgentBuilder
impl Send for AgentBuilder
impl Sync for AgentBuilder
impl Unpin for AgentBuilder
impl UnsafeUnpin for AgentBuilder
impl UnwindSafe for AgentBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more