a2a_protocol_server/handler/lifecycle/
list_tasks.rs1use std::collections::HashMap;
9use std::time::Instant;
10
11use a2a_protocol_types::params::ListTasksParams;
12use a2a_protocol_types::responses::TaskListResponse;
13use a2a_protocol_types::task::TaskState;
14
15use crate::error::{ServerError, ServerResult};
16
17use super::super::RequestHandler;
18use super::super::helpers::{build_call_context, truncate_history, unset_if_empty};
19
20impl RequestHandler {
21 #[allow(clippy::too_many_lines)]
27 pub async fn on_list_tasks(
28 &self,
29 params: ListTasksParams,
30 headers: Option<&HashMap<String, String>>,
31 ) -> ServerResult<TaskListResponse> {
32 let start = Instant::now();
33 trace_debug!(method = "ListTasks", "handling list tasks");
34 self.metrics.on_request("ListTasks");
35
36 let tenant = self
37 .resolve_tenant_counted(start, "ListTasks", headers, params.tenant.as_deref())
38 .await?;
39 let mut params = params;
40 unset_if_empty(&mut params.context_id);
46 unset_if_empty(&mut params.page_token);
47 if params.status == Some(TaskState::Unspecified) {
48 params.status = None;
49 }
50 let history_length = params.history_length;
51 let include_artifacts = params.include_artifacts;
52 let result: ServerResult<_> =
53 crate::store::tenant::TenantContext::scope_request(tenant, async {
54 let call_ctx = build_call_context("ListTasks", headers, self.inbound_trace_policy);
55 let mut call = self.interceptors.begin(&call_ctx);
56 let result = async {
57 self.admit(&mut call, &call_ctx).await?;
58 self.ensure_required_extensions(&call_ctx)?;
60 refuse_unlistable(¶ms)?;
61 super::super::ids::refuse_control_chars_in_filter(¶ms)?;
62 let mut result = self.task_store.list(¶ms).await?;
63
64 if let Some(hl) = history_length {
67 for task in &mut result.tasks {
68 task.history = truncate_history(task.history.take(), hl);
69 }
70 }
71
72 if !include_artifacts.unwrap_or(false) {
75 for task in &mut result.tasks {
76 task.artifacts = None;
77 }
78 }
79
80 Ok(result)
81 }
82 .await;
83 call.finish(result).await
84 })
85 .await;
86
87 let result = result.and_then(|mut page| {
89 self.outbound.sign_page(&mut page)?;
90 Ok(page)
91 });
92
93 let elapsed = start.elapsed();
94 match &result {
95 Ok(_) => {
96 self.metrics.on_response("ListTasks");
97 self.metrics.on_latency("ListTasks", elapsed);
98 }
99 Err(e) => {
100 self.metrics.on_error("ListTasks", e.metric_label());
101 self.metrics.on_latency("ListTasks", elapsed);
102 }
103 }
104 result
105 }
106}
107
108fn refuse_unlistable(params: &ListTasksParams) -> ServerResult<()> {
116 if let Some(ps) = params.page_size
117 && !(1..=100).contains(&ps)
118 {
119 return Err(ServerError::InvalidField {
120 field: "pageSize".into(),
121 message: format!("pageSize must be between 1 and 100 inclusive, got {ps}"),
122 });
123 }
124 match params.status_timestamp_after.as_deref() {
125 Some(after) if a2a_protocol_types::parse_iso8601_to_unix_millis(after).is_none() => {
126 Err(ServerError::InvalidField {
127 field: "statusTimestampAfter".into(),
128 message: format!(
129 "statusTimestampAfter is not a valid ISO 8601 timestamp: {after:?}"
130 ),
131 })
132 }
133 _ => Ok(()),
134 }
135}
136
137#[cfg(test)]
138#[path = "list_tasks_tests.rs"]
139mod tests;