Skip to main content

weirdnet_agent/
identity.rs

1//! Who an agent (a "companion" in Morph) is.
2
3use std::fmt;
4
5use serde::{Deserialize, Serialize};
6
7/// Stable agent id, unique per user.
8#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
9#[serde(transparent)]
10pub struct AgentId(pub String);
11
12impl AgentId {
13    pub fn new(id: impl Into<String>) -> Self {
14        Self(id.into())
15    }
16
17    pub fn as_str(&self) -> &str {
18        &self.0
19    }
20}
21
22impl fmt::Display for AgentId {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        f.write_str(&self.0)
25    }
26}
27
28/// Where an agent runs relative to the app hosting the session.
29#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
30#[serde(rename_all = "snake_case")]
31pub enum AgentKind {
32    /// Runs on this machine.
33    Local,
34    /// Runs on another of the user's linked nodes.
35    Node,
36    /// An outside agent driving the app (for example over MCP).
37    External,
38}
39
40/// A named agent: every step, approval and citation is attributed to one.
41#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
42pub struct AgentIdentity {
43    pub id: AgentId,
44    pub name: String,
45    pub kind: AgentKind,
46    /// Node the agent runs on, when known (for example a host name).
47    #[serde(default, skip_serializing_if = "Option::is_none")]
48    pub node: Option<String>,
49}
50
51impl AgentIdentity {
52    pub fn new(id: impl Into<String>, name: impl Into<String>, kind: AgentKind) -> Self {
53        Self { id: AgentId::new(id), name: name.into(), kind, node: None }
54    }
55
56    pub fn with_node(mut self, node: impl Into<String>) -> Self {
57        self.node = Some(node.into());
58        self
59    }
60}