Skip to main content

klieo_core/
ids.rs

1//! Strongly-typed IDs used throughout the framework.
2
3use serde::{Deserialize, Serialize};
4use std::fmt;
5
6/// Unique identifier for a single agent run.
7///
8/// Backed by ULID — sortable by creation time.
9///
10/// ```
11/// use klieo_core::ids::RunId;
12/// let a = RunId::new();
13/// let b = RunId::new();
14/// assert_ne!(a, b);
15/// assert_eq!(a.to_string().len(), 26);
16/// ```
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
18pub struct RunId(pub ulid::Ulid);
19
20impl RunId {
21    /// Generate a fresh ID.
22    pub fn new() -> Self {
23        Self(ulid::Ulid::new())
24    }
25}
26
27impl Default for RunId {
28    fn default() -> Self {
29        Self::new()
30    }
31}
32
33impl fmt::Display for RunId {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        write!(f, "{}", self.0)
36    }
37}
38
39/// Identifies a conversation thread for short-term memory scoping.
40#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
41pub struct ThreadId(pub String);
42
43impl ThreadId {
44    /// Wrap a string as a thread id.
45    pub fn new(s: impl Into<String>) -> Self {
46        Self(s.into())
47    }
48}
49
50impl fmt::Display for ThreadId {
51    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52        f.write_str(&self.0)
53    }
54}
55
56/// Identifier for an enqueued job in `JobQueue`.
57#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
58pub struct JobId(pub String);
59
60impl JobId {
61    /// Wrap a string as a job id.
62    pub fn new(s: impl Into<String>) -> Self {
63        Self(s.into())
64    }
65}
66
67impl fmt::Display for JobId {
68    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69        f.write_str(&self.0)
70    }
71}
72
73/// Identifier for a stored fact in `LongTermMemory`.
74#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
75pub struct FactId(pub String);
76
77impl FactId {
78    /// Wrap a string as a fact id.
79    pub fn new(s: impl Into<String>) -> Self {
80        Self(s.into())
81    }
82}
83
84impl fmt::Display for FactId {
85    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86        f.write_str(&self.0)
87    }
88}
89
90/// Durable consumer name for a JetStream-style subscription.
91#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
92pub struct DurableName(pub String);
93
94impl DurableName {
95    /// Wrap a string as a durable consumer name.
96    pub fn new(s: impl Into<String>) -> Self {
97        Self(s.into())
98    }
99}