aither-agent 0.24.0

AI agent framework with planning, memory, and tool execution for aither
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
//! State: keyed conditions the model is told about only when they change.
//!
//! Memory pressure, machine load, the free space of a volume: each is a
//! condition with one current value, and only the latest value matters. A
//! [`StateSource`] reports those values; the context keeps, per key, the
//! value the model last saw, and an alert is written only as a request is
//! assembled and only when the current value differs from it. Nothing is
//! written while the agent is idle, so a condition that rises and settles
//! back between two requests reaches the model as nothing at all.
//!
//! A state alert is restated rather than kept: every reassembly, a cold
//! cache's or a compaction's, removes the earlier alerts and what the model
//! saw with them, and the next request writes the current value again only
//! when it is not the normal one, which the model assumes until told
//! otherwise.

use std::collections::BTreeMap;
use std::future::Future;
use std::hash::Hash;
use std::pin::Pin;

use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};

use super::source::Block;

/// A state whose value differs from the one the model last saw, as the
/// model is to be told it.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StateChange<'a, K, V> {
    key: &'a K,
    seen: Option<&'a V>,
    now: &'a V,
}

impl<'a, K, V> StateChange<'a, K, V> {
    /// The state that changed.
    #[must_use]
    pub const fn key(&self) -> &'a K {
        self.key
    }

    /// The value the model last saw; `None` when it was never told, or
    /// nothing it was told is still in the context, so it assumes the
    /// normal value.
    #[must_use]
    pub const fn seen(&self) -> Option<&'a V> {
        self.seen
    }

    /// The value now.
    #[must_use]
    pub const fn now(&self) -> &'a V {
        self.now
    }
}

/// Supplies state: keyed conditions with a latest value.
///
/// The agent reads the source only as a request is assembled. The context
/// keeps the value the model last saw of every key, and the agent appends
/// the alert [`announce`](Self::announce) writes only when the latest value
/// and that one differ. A return to the value the model already saw writes
/// nothing. After a reassembly the model has seen nothing, so a key is
/// restated only when its value is not the normal one.
///
/// State never starts a run of the agent.
///
/// # Example
///
/// ```
/// use aither_agent::{Block, StateChange, StateSource};
///
/// struct SleepMode(bool);
///
/// impl StateSource for SleepMode {
///     const ID: &'static str = "sleep_mode";
///     type Key = ();
///     type Value = bool;
///
///     async fn poll(&mut self) -> Vec<((), bool)> {
///         vec![((), self.0)]
///     }
///
///     fn is_normal(&self, (): &(), asleep: &bool) -> bool {
///         !asleep
///     }
///
///     fn announce(&self, change: &StateChange<'_, (), bool>) -> Block {
///         let text = if *change.now() { "The host sleeps." } else { "The host is awake." };
///         Block::new("sleep_mode", text)
///     }
/// }
/// ```
pub trait StateSource: Send + Sync + 'static {
    /// Names this source in the context, which keeps what the model saw of
    /// its states under it. It is unique among the agent's state sources
    /// and does not change between versions of the embedder, or a
    /// restored context would attach what the model saw to another source.
    const ID: &'static str;

    /// The identity of one state, such as a volume. It serializes with the
    /// context.
    type Key: Eq + Hash + Clone + Serialize + DeserializeOwned + Send + Sync + 'static;

    /// A state's value. Two values are the same state when they are equal.
    /// It serializes with the context.
    type Value: PartialEq + Clone + Serialize + DeserializeOwned + Send + Sync + 'static;

    /// The latest value of every state that may have changed since the
    /// last call; a key left out keeps the value it had.
    ///
    /// Called only as a request is assembled, never while the agent is
    /// idle, so a source may drain whatever it collected meanwhile. It must
    /// not block.
    fn poll(&mut self) -> impl Future<Output = Vec<(Self::Key, Self::Value)>> + Send;

    /// Whether `value` is the normal value of the state `key`: the one the
    /// model assumes while it has been told nothing.
    fn is_normal(&self, key: &Self::Key, value: &Self::Value) -> bool;

    /// The alert telling the model of `change`, appended to the
    /// conversation once and removed at the next reassembly.
    fn announce(&self, change: &StateChange<'_, Self::Key, Self::Value>) -> Block;
}

/// One state: its latest value.
struct Entry<K, V> {
    key: K,
    now: V,
}

/// A [`StateSource`] with the latest value of each of its states.
struct Tracked<S: StateSource> {
    source: S,
    /// In the order the keys first appeared, so alerts come in a stable
    /// order.
    entries: Vec<Entry<S::Key, S::Value>>,
}

impl<S: StateSource> Tracked<S> {
    fn record(&mut self, key: S::Key, value: S::Value) {
        match self.entries.iter_mut().find(|entry| entry.key == key) {
            Some(entry) => entry.now = value,
            None => self.entries.push(Entry { key, now: value }),
        }
    }

    /// The alerts for every state the model does not know as it stands,
    /// and what it has seen of the source's states after them.
    ///
    /// `stored` is what the context held of this source: the keys and the
    /// values the model last saw for them.
    fn alerts(&self, stored: Option<serde_json::Value>) -> (Vec<Block>, serde_json::Value) {
        let mut seen: Vec<(S::Key, S::Value)> = stored.map_or_else(Vec::new, |stored| {
            serde_json::from_value(stored).unwrap_or_else(|error| {
                panic!(
                    "the context holds what the model saw of `{}` in another form: {error}",
                    S::ID
                )
            })
        });
        let mut alerts = Vec::new();
        for entry in &self.entries {
            let position = seen.iter().position(|(key, _)| *key == entry.key);
            let known = position.map_or_else(
                || self.source.is_normal(&entry.key, &entry.now),
                |position| seen[position].1 == entry.now,
            );
            if known {
                continue;
            }
            alerts.push(self.source.announce(&StateChange {
                key: &entry.key,
                seen: position.map(|position| &seen[position].1),
                now: &entry.now,
            }));
            match position {
                Some(position) => seen[position].1 = entry.now.clone(),
                None => seen.push((entry.key.clone(), entry.now.clone())),
            }
        }
        let seen = serde_json::to_value(&seen).expect("the keys and values of a state serialize");
        (alerts, seen)
    }
}

/// What a state source answers a request with: its alerts, and what the
/// model has seen of its states after them.
type Answer = (Vec<Block>, serde_json::Value);

/// Object-safe twin of [`StateSource`], with the agent's bookkeeping.
trait StateSourceImpl: Send + Sync {
    fn id(&self) -> &'static str;

    fn on_request(
        &mut self,
        stored: Option<serde_json::Value>,
    ) -> Pin<Box<dyn Future<Output = Answer> + Send + '_>>;
}

impl<S: StateSource> StateSourceImpl for Tracked<S> {
    fn id(&self) -> &'static str {
        S::ID
    }

    fn on_request(
        &mut self,
        stored: Option<serde_json::Value>,
    ) -> Pin<Box<dyn Future<Output = Answer> + Send + '_>> {
        Box::pin(async move {
            for (key, value) in self.source.poll().await {
                self.record(key, value);
            }
            self.alerts(stored)
        })
    }
}

/// What the model has seen of every state source's states, under the
/// source's [`ID`](StateSource::ID).
///
/// It is part of the [`Context`](super::Context), so it serializes, forks
/// and is replaced with it. A source's part is its keys and the values the
/// model last saw for them, in the serde form of the source's own types.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct SeenStates(BTreeMap<String, serde_json::Value>);

impl SeenStates {
    /// Whether the model has seen nothing of any state.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// What the model saw of the source `id`.
    pub(crate) fn get(&self, id: &str) -> Option<&serde_json::Value> {
        self.0.get(id)
    }

    /// Records what the model has now seen of the source `id`.
    pub(crate) fn set(&mut self, id: &str, seen: serde_json::Value) {
        self.0.insert(id.to_owned(), seen);
    }

    /// Forgets everything the model saw: the alerts that told it are gone.
    pub(crate) fn clear(&mut self) {
        self.0.clear();
    }
}

/// A type-erased [`StateSource`], so an agent can hold a heterogeneous
/// set. What the model has seen of its states lives in the context.
pub struct AnyStateSource {
    inner: Box<dyn StateSourceImpl>,
}

impl std::fmt::Debug for AnyStateSource {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("AnyStateSource")
            .finish_non_exhaustive()
    }
}

impl AnyStateSource {
    /// Erases the concrete type of `source`.
    #[must_use]
    pub fn new<S: StateSource>(source: S) -> Self {
        Self {
            inner: Box::new(Tracked {
                source,
                entries: Vec::new(),
            }),
        }
    }

    /// The source's [`ID`](StateSource::ID).
    pub(crate) fn id(&self) -> &'static str {
        self.inner.id()
    }

    /// Reads the source and returns the alerts for the states whose value
    /// differs from what the model last saw, and what it has seen of the
    /// source's states then.
    ///
    /// `stored` is what the context held of this source: `None` when the
    /// model has seen nothing of it, as after a reassembly.
    pub(crate) fn on_request(
        &mut self,
        stored: Option<serde_json::Value>,
    ) -> impl Future<Output = Answer> + Send + '_ {
        self.inner.on_request(stored)
    }
}

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

    /// A disk whose free-space level a test sets; `0` is normal.
    struct Disk {
        levels: async_channel::Receiver<u8>,
    }

    impl StateSource for Disk {
        const ID: &'static str = "disk";
        type Key = String;
        type Value = u8;

        fn poll(&mut self) -> impl Future<Output = Vec<(String, u8)>> + Send {
            std::future::ready(
                std::iter::from_fn(|| self.levels.try_recv().ok())
                    .map(|level| ("/work".to_owned(), level))
                    .collect(),
            )
        }

        fn is_normal(&self, _: &String, level: &u8) -> bool {
            *level == 0
        }

        fn announce(&self, change: &StateChange<'_, String, u8>) -> Block {
            Block::new(
                "disk",
                format!("{} {:?} -> {}", change.key(), change.seen(), change.now()),
            )
        }
    }

    /// A disk source and the seen states of the context it is read for.
    struct Harness {
        levels: async_channel::Sender<u8>,
        source: AnyStateSource,
        seen: SeenStates,
    }

    impl Harness {
        fn new() -> Self {
            let (levels, receiver) = async_channel::unbounded();
            Self {
                levels,
                source: AnyStateSource::new(Disk { levels: receiver }),
                seen: SeenStates::default(),
            }
        }

        async fn send(&self, level: u8) {
            self.levels.send(level).await.expect("open");
        }

        /// A request: the alerts' texts. A reassembly before it empties
        /// what the model saw.
        async fn request(&mut self, reassembled: bool) -> Vec<String> {
            if reassembled {
                self.seen.clear();
            }
            let id = self.source.id();
            let (alerts, seen) = self.source.on_request(self.seen.get(id).cloned()).await;
            self.seen.set(id, seen);
            alerts
                .iter()
                .map(|block| block.content().to_owned())
                .collect()
        }
    }

    const NONE: [&str; 0] = [];

    /// The normal value is what the model assumes: nothing is said of it,
    /// a change from it is said once, and only the latest value counts.
    #[tokio::test]
    async fn only_a_value_the_model_does_not_know_is_announced() {
        let mut disk = Harness::new();
        disk.send(0).await;
        assert_eq!(disk.request(false).await, NONE);

        disk.send(1).await;
        disk.send(2).await;
        assert_eq!(disk.request(false).await, ["/work None -> 2"]);
        assert_eq!(disk.request(false).await, NONE, "said once");

        disk.send(0).await;
        assert_eq!(disk.request(false).await, ["/work Some(2) -> 0"]);
    }

    /// A value that changes and comes back between two requests is no
    /// news.
    #[tokio::test]
    async fn a_return_to_the_value_the_model_saw_says_nothing() {
        let mut disk = Harness::new();
        disk.send(2).await;
        assert_eq!(disk.request(false).await.len(), 1);
        disk.send(0).await;
        disk.send(2).await;
        assert_eq!(disk.request(false).await, NONE);
    }

    /// After a reassembly the model knows nothing: a value that is not
    /// normal is restated, a normal one is not.
    #[tokio::test]
    async fn a_reassembly_restates_only_a_value_that_is_not_normal() {
        let mut disk = Harness::new();
        disk.send(2).await;
        assert_eq!(disk.request(false).await.len(), 1);
        assert_eq!(disk.request(true).await, ["/work None -> 2"]);

        disk.send(0).await;
        assert_eq!(disk.request(false).await.len(), 1);
        assert_eq!(disk.request(true).await, NONE);
    }
}