llm-agent-runtime 1.74.0

Unified Tokio agent runtime -- orchestration, memory, knowledge graph, and ReAct loop in one crate
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! Core identifier types, available without feature gates.
//!
//! [`AgentId`] and [`MemoryId`] are defined here so they can be used by the
//! agent runtime regardless of whether the `memory` feature is enabled.

use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// Stable identifier for an agent instance.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct AgentId(pub String);

impl AgentId {
    /// Create a new `AgentId` from any string-like value.
    ///
    /// # Panics (debug only)
    ///
    /// Triggers a `debug_assert!` if `id` is empty.  In release builds a
    /// `tracing::warn!` is emitted instead so that the misconfiguration is
    /// surfaced in production logs without aborting the process.
    pub fn new(id: impl Into<String>) -> Self {
        let id = id.into();
        if id.is_empty() {
            debug_assert!(false, "AgentId must not be empty");
            tracing::warn!("AgentId::new called with an empty string — agent IDs should be non-empty to avoid lookup ambiguity");
        }
        Self(id)
    }

    /// Create a validated `AgentId`, returning an error if `id` is empty.
    ///
    /// Prefer this constructor in user-facing code where empty IDs must be
    /// rejected explicitly rather than silently warned about.
    pub fn try_new(id: impl Into<String>) -> Result<Self, crate::error::AgentRuntimeError> {
        let id = id.into();
        if id.is_empty() {
            return Err(crate::error::AgentRuntimeError::Memory(
                "AgentId must not be empty".into(),
            ));
        }
        Ok(Self(id))
    }

    /// Generate a random `AgentId` backed by a UUID v4.
    pub fn random() -> Self {
        Self(Uuid::new_v4().to_string())
    }

    /// Return the inner ID string as a `&str`.
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Return the byte length of the inner ID string.
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Return `true` if the inner ID string is empty.
    ///
    /// Note: `AgentId::new` warns (debug: asserts) against empty IDs.
    /// This predicate is provided for defensive checks.
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Return `true` if the inner ID string starts with `prefix`.
    pub fn starts_with(&self, prefix: &str) -> bool {
        self.0.starts_with(prefix)
    }
}

impl AsRef<str> for AgentId {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl std::fmt::Display for AgentId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<String> for AgentId {
    /// Create an `AgentId` from an owned `String`.
    ///
    /// Equivalent to [`AgentId::new`]; emits a `tracing::warn!` for empty strings.
    fn from(s: String) -> Self {
        Self::new(s)
    }
}

impl From<&str> for AgentId {
    /// Create an `AgentId` from a string slice.
    ///
    /// Equivalent to [`AgentId::new`]; emits a `tracing::warn!` for empty strings.
    fn from(s: &str) -> Self {
        Self::new(s)
    }
}

impl std::str::FromStr for AgentId {
    type Err = crate::error::AgentRuntimeError;

    /// Parse an `AgentId` from a string, returning an error if the string is empty.
    ///
    /// Unlike [`AgentId::new`] this is a validated constructor: empty strings are
    /// rejected with `AgentRuntimeError::Memory` rather than silently warned about.
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::try_new(s)
    }
}

impl std::ops::Deref for AgentId {
    type Target = str;

    /// Dereference to the inner ID string slice.
    ///
    /// Allows `&agent_id` to coerce to `&str` transparently.
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

/// Stable identifier for a memory item.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct MemoryId(pub String);

impl MemoryId {
    /// Create a new `MemoryId` from any string-like value.
    ///
    /// # Panics (debug only)
    ///
    /// Triggers a `debug_assert!` if `id` is empty.  In release builds a
    /// `tracing::warn!` is emitted instead so that the misconfiguration is
    /// surfaced in production logs without aborting the process.
    pub fn new(id: impl Into<String>) -> Self {
        let id = id.into();
        if id.is_empty() {
            debug_assert!(false, "MemoryId must not be empty");
            tracing::warn!("MemoryId::new called with an empty string — memory IDs should be non-empty to avoid lookup ambiguity");
        }
        Self(id)
    }

    /// Create a validated `MemoryId`, returning an error if `id` is empty.
    pub fn try_new(id: impl Into<String>) -> Result<Self, crate::error::AgentRuntimeError> {
        let id = id.into();
        if id.is_empty() {
            return Err(crate::error::AgentRuntimeError::Memory(
                "MemoryId must not be empty".into(),
            ));
        }
        Ok(Self(id))
    }

    /// Generate a random `MemoryId` backed by a UUID v4.
    pub fn random() -> Self {
        Self(Uuid::new_v4().to_string())
    }

    /// Return the inner ID string as a `&str`.
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Return the byte length of the inner ID string.
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Return `true` if the inner ID string is empty.
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Return `true` if the inner ID string starts with `prefix`.
    pub fn starts_with(&self, prefix: &str) -> bool {
        self.0.starts_with(prefix)
    }
}

impl AsRef<str> for MemoryId {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl std::fmt::Display for MemoryId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<String> for MemoryId {
    /// Create a `MemoryId` from an owned `String`.
    ///
    /// Equivalent to [`MemoryId::new`]; emits a `tracing::warn!` for empty strings.
    fn from(s: String) -> Self {
        Self::new(s)
    }
}

impl From<&str> for MemoryId {
    /// Create a `MemoryId` from a string slice.
    ///
    /// Equivalent to [`MemoryId::new`]; emits a `tracing::warn!` for empty strings.
    fn from(s: &str) -> Self {
        Self::new(s)
    }
}

impl std::str::FromStr for MemoryId {
    type Err = crate::error::AgentRuntimeError;

    /// Parse a `MemoryId` from a string, returning an error if the string is empty.
    ///
    /// Unlike [`MemoryId::new`] this is a validated constructor: empty strings are
    /// rejected with `AgentRuntimeError::Memory` rather than silently warned about.
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::try_new(s)
    }
}

impl std::ops::Deref for MemoryId {
    type Target = str;

    /// Dereference to the inner ID string slice.
    ///
    /// Allows `&memory_id` to coerce to `&str` transparently.
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_agent_id_new_stores_value() {
        let id = AgentId::new("agent-1");
        assert_eq!(id.as_str(), "agent-1");
    }

    #[test]
    fn test_agent_id_try_new_rejects_empty() {
        assert!(AgentId::try_new("").is_err());
    }

    #[test]
    fn test_agent_id_try_new_accepts_nonempty() {
        let id = AgentId::try_new("ok").unwrap();
        assert_eq!(id.as_str(), "ok");
    }

    #[test]
    fn test_agent_id_random_generates_unique_ids() {
        let a = AgentId::random();
        let b = AgentId::random();
        assert_ne!(a, b);
    }

    #[test]
    fn test_agent_id_len_and_is_empty() {
        let id = AgentId::new("abc");
        assert_eq!(id.len(), 3);
        assert!(!id.is_empty());
    }

    #[test]
    fn test_agent_id_display() {
        let id = AgentId::new("my-agent");
        assert_eq!(id.to_string(), "my-agent");
    }

    #[test]
    fn test_memory_id_new_stores_value() {
        let id = MemoryId::new("mem-42");
        assert_eq!(id.as_str(), "mem-42");
    }

    #[test]
    fn test_memory_id_try_new_rejects_empty() {
        assert!(MemoryId::try_new("").is_err());
    }

    #[test]
    fn test_memory_id_len_and_is_empty() {
        let id = MemoryId::new("hello");
        assert_eq!(id.len(), 5);
        assert!(!id.is_empty());
    }

    #[test]
    fn test_memory_id_display() {
        let id = MemoryId::new("mem-id");
        assert_eq!(id.to_string(), "mem-id");
    }

    #[test]
    fn test_memory_id_random_generates_unique_ids() {
        let a = MemoryId::random();
        let b = MemoryId::random();
        assert_ne!(a, b);
    }

    // ── Round 11: starts_with ─────────────────────────────────────────────────

    #[test]
    fn test_agent_id_starts_with_matching_prefix() {
        let id = AgentId::new("agent-001");
        assert!(id.starts_with("agent-"));
        assert!(!id.starts_with("user-"));
    }

    #[test]
    fn test_agent_id_starts_with_empty_prefix_always_true() {
        let id = AgentId::new("anything");
        assert!(id.starts_with(""));
    }

    #[test]
    fn test_memory_id_starts_with_matching_prefix() {
        let id = MemoryId::new("mem-42");
        assert!(id.starts_with("mem-"));
        assert!(!id.starts_with("ep-"));
    }

    // ── Round 40: PartialOrd/Ord, From, FromStr, Deref ────────────────────────

    #[test]
    fn test_agent_id_ord_allows_sorting() {
        let mut ids = vec![AgentId::new("c"), AgentId::new("a"), AgentId::new("b")];
        ids.sort();
        assert_eq!(ids[0].as_str(), "a");
        assert_eq!(ids[2].as_str(), "c");
    }

    #[test]
    fn test_agent_id_ord_allows_btreemap_key() {
        use std::collections::BTreeMap;
        let mut map: BTreeMap<AgentId, u32> = BTreeMap::new();
        map.insert(AgentId::new("agent-2"), 2);
        map.insert(AgentId::new("agent-1"), 1);
        let keys: Vec<_> = map.keys().map(|k| k.as_str()).collect();
        assert_eq!(keys, vec!["agent-1", "agent-2"]);
    }

    #[test]
    fn test_agent_id_from_string() {
        let id = AgentId::from("my-agent".to_owned());
        assert_eq!(id.as_str(), "my-agent");
    }

    #[test]
    fn test_agent_id_from_str_ref() {
        let id = AgentId::from("my-agent");
        assert_eq!(id.as_str(), "my-agent");
    }

    #[test]
    fn test_agent_id_from_str_parse_rejects_empty() {
        let result: Result<AgentId, _> = "".parse();
        assert!(result.is_err());
    }

    #[test]
    fn test_agent_id_from_str_parse_accepts_nonempty() {
        let id: AgentId = "worker-1".parse().unwrap();
        assert_eq!(id.as_str(), "worker-1");
    }

    #[test]
    fn test_agent_id_deref_to_str() {
        let id = AgentId::new("deref-test");
        let s: &str = &id;
        assert_eq!(s, "deref-test");
    }

    #[test]
    fn test_agent_id_deref_enables_str_methods() {
        let id = AgentId::new("hello-world");
        // Deref lets us call &str methods directly on &AgentId
        assert!(id.contains('-'));
        assert_eq!(id.len(), 11);
    }

    #[test]
    fn test_memory_id_ord_allows_sorting() {
        let mut ids = vec![MemoryId::new("z"), MemoryId::new("a"), MemoryId::new("m")];
        ids.sort();
        assert_eq!(ids[0].as_str(), "a");
        assert_eq!(ids[2].as_str(), "z");
    }

    #[test]
    fn test_memory_id_from_string() {
        let id = MemoryId::from("mem-x".to_owned());
        assert_eq!(id.as_str(), "mem-x");
    }

    #[test]
    fn test_memory_id_from_str_ref() {
        let id = MemoryId::from("mem-y");
        assert_eq!(id.as_str(), "mem-y");
    }

    #[test]
    fn test_memory_id_from_str_parse_rejects_empty() {
        let result: Result<MemoryId, _> = "".parse();
        assert!(result.is_err());
    }

    #[test]
    fn test_memory_id_from_str_parse_accepts_nonempty() {
        let id: MemoryId = "ep-001".parse().unwrap();
        assert_eq!(id.as_str(), "ep-001");
    }

    #[test]
    fn test_memory_id_deref_to_str() {
        let id = MemoryId::new("deref-mem");
        let s: &str = &id;
        assert_eq!(s, "deref-mem");
    }
}