Skip to main content

a2a_server/task_store/
mod.rs

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