Skip to main content

a2a_server/task_store/
mod.rs

1// Copyright AGNTCY Contributors (https://github.com/agntcy)
2// SPDX-License-Identifier: Apache-2.0
3mod inmemory;
4mod store;
5
6pub use inmemory::InMemoryTaskStore;
7pub use store::{StoredTask, TaskStore, TaskVersion};
8
9use a2a::Task;
10
11/// Truncate `task.history` to the last `history_length` messages.
12///
13/// `None` leaves history unchanged. `Some(n)` with `n <= 0` clears it.
14/// Negative values must not be cast to `usize` (that wraps and returns the
15/// full history).
16pub(crate) fn apply_history_length(task: &mut Task, history_length: Option<i32>) {
17    let Some(requested) = history_length else {
18        return;
19    };
20    let keep = if requested <= 0 {
21        0
22    } else {
23        requested as usize
24    };
25    let Some(history) = task.history.as_mut() else {
26        return;
27    };
28    if keep == 0 {
29        history.clear();
30        return;
31    }
32    if history.len() > keep {
33        history.drain(..history.len() - keep);
34    }
35}