tirea-agentos 0.5.0

Agent runtime with streaming LLM integration, sub-agent orchestration, and context window management
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
use super::*;
use crate::composition::{A2aAgentBinding, RemoteSecurityConfig};
use reqwest::Method;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::sync::OnceLock;
use tokio::time::{sleep, Duration};

#[derive(Debug, Clone)]
pub(super) struct A2aStartResponse {
    pub(super) context_id: Option<String>,
    pub(super) task_id: String,
}

#[derive(Debug, Clone)]
pub(super) struct A2aTaskSnapshot {
    pub(super) status: SubAgentStatus,
    pub(super) error: Option<String>,
    pub(super) done: bool,
    pub(super) output_text: Option<String>,
    pub(super) raw_task: Value,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct A2aTaskResponse {
    #[serde(default)]
    context_id: Option<String>,
    status: String,
    #[serde(default)]
    termination_code: Option<String>,
    #[serde(default)]
    termination_detail: Option<String>,
    #[serde(default)]
    message: Option<Value>,
    #[serde(default)]
    history: Vec<Value>,
    #[serde(default)]
    artifacts: Vec<Value>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct A2aSubmissionResponse {
    #[serde(default)]
    context_id: Option<String>,
    task_id: String,
}

#[derive(Debug, thiserror::Error)]
enum A2aClientError {
    #[error("remote A2A agents require exactly one non-empty user prompt")]
    InvalidPromptCardinality,
    #[error("remote A2A agents currently support user prompts only")]
    UnsupportedMessageRole,
    #[error("failed to submit remote A2A run: {source}")]
    SubmitRequest {
        #[source]
        source: reqwest::Error,
    },
    #[error("remote A2A submission rejected: {source}")]
    SubmitRejected {
        #[source]
        source: reqwest::Error,
    },
    #[error("failed to decode remote A2A submission response: {source}")]
    SubmitDecode {
        #[source]
        source: reqwest::Error,
    },
    #[error("failed to query remote A2A task status: {source}")]
    QueryRequest {
        #[source]
        source: reqwest::Error,
    },
    #[error("remote A2A task query rejected: {source}")]
    QueryRejected {
        #[source]
        source: reqwest::Error,
    },
    #[error("failed to decode remote A2A task status: {source}")]
    QueryDecode {
        #[source]
        source: reqwest::Error,
    },
    #[error("failed to cancel remote A2A task: {source}")]
    CancelRequest {
        #[source]
        source: reqwest::Error,
    },
    #[error("remote A2A cancel rejected: {source}")]
    CancelRejected {
        #[source]
        source: reqwest::Error,
    },
}

#[derive(Debug, Clone)]
struct A2aRuntimeClient {
    http: reqwest::Client,
}

impl A2aRuntimeClient {
    fn shared() -> Self {
        static HTTP_CLIENT: OnceLock<reqwest::Client> = OnceLock::new();
        Self {
            http: HTTP_CLIENT.get_or_init(reqwest::Client::new).clone(),
        }
    }

    fn authorized_request(
        &self,
        method: Method,
        url: &str,
        auth: Option<&RemoteSecurityConfig>,
    ) -> reqwest::RequestBuilder {
        authorized(&self.http, method, url, auth)
    }

    async fn submit_run(
        &self,
        target: &A2aAgentBinding,
        messages: &[Message],
    ) -> Result<A2aStartResponse, A2aClientError> {
        let input = single_user_input(messages)?;
        let url = format!("{}/message:send", agent_path(target));
        let response = self
            .authorized_request(Method::POST, &url, target.auth.as_ref())
            .json(&json!({ "input": input }))
            .send()
            .await
            .map_err(|source| A2aClientError::SubmitRequest { source })?;
        let response = response
            .error_for_status()
            .map_err(|source| A2aClientError::SubmitRejected { source })?;
        let payload = response
            .json::<A2aSubmissionResponse>()
            .await
            .map_err(|source| A2aClientError::SubmitDecode { source })?;
        Ok(A2aStartResponse {
            context_id: payload.context_id,
            task_id: payload.task_id,
        })
    }

    async fn fetch_task_snapshot(
        &self,
        target: &A2aAgentBinding,
        task_id: &str,
    ) -> Result<A2aTaskSnapshot, A2aClientError> {
        let url = format!("{}/tasks/{}", agent_path(target), task_id.trim());
        let response = self
            .authorized_request(Method::GET, &url, target.auth.as_ref())
            .send()
            .await
            .map_err(|source| A2aClientError::QueryRequest { source })?;
        let response = response
            .error_for_status()
            .map_err(|source| A2aClientError::QueryRejected { source })?;
        let payload = response
            .json::<A2aTaskResponse>()
            .await
            .map_err(|source| A2aClientError::QueryDecode { source })?;
        let _ = payload.context_id.as_deref();
        Ok(map_task_status(payload))
    }

    async fn cancel_run(
        &self,
        target: &A2aAgentBinding,
        task_id: &str,
    ) -> Result<(), A2aClientError> {
        let url = format!("{}/tasks/{}:cancel", agent_path(target), task_id.trim());
        self.authorized_request(Method::POST, &url, target.auth.as_ref())
            .json(&json!({}))
            .send()
            .await
            .map_err(|source| A2aClientError::CancelRequest { source })?
            .error_for_status()
            .map_err(|source| A2aClientError::CancelRejected { source })?;
        Ok(())
    }
}

fn base_url(target: &A2aAgentBinding) -> &str {
    target.base_url.trim_end_matches('/')
}

fn agent_path(target: &A2aAgentBinding) -> String {
    format!(
        "{}/agents/{}",
        base_url(target),
        target.remote_agent_id.trim()
    )
}

fn authorized(
    client: &reqwest::Client,
    method: Method,
    url: &str,
    auth: Option<&RemoteSecurityConfig>,
) -> reqwest::RequestBuilder {
    let request = client.request(method, url);
    match auth {
        Some(RemoteSecurityConfig::BearerToken(token)) => request.bearer_auth(token),
        Some(RemoteSecurityConfig::Header { name, value }) => request.header(name, value),
        None => request,
    }
}

fn single_user_input(messages: &[Message]) -> Result<String, A2aClientError> {
    let mut non_empty = messages
        .iter()
        .filter(|message| !message.content.trim().is_empty())
        .collect::<Vec<_>>();
    if non_empty.len() != 1 {
        return Err(A2aClientError::InvalidPromptCardinality);
    }
    let message = non_empty.pop().expect("single message must exist");
    if message.role != Role::User {
        return Err(A2aClientError::UnsupportedMessageRole);
    }
    Ok(message.content.trim().to_string())
}

fn map_task_status(response: A2aTaskResponse) -> A2aTaskSnapshot {
    let output_text = extract_task_output_text(&response);
    let raw_task = serde_json::to_value(&response).unwrap_or(Value::Null);
    let status = response.status.trim().to_ascii_lowercase();
    match status.as_str() {
        "done" | "completed" => {
            let termination = response
                .termination_code
                .as_deref()
                .map(str::trim)
                .map(str::to_ascii_lowercase);
            match termination.as_deref() {
                Some("cancelled") | Some("cancel_requested") => A2aTaskSnapshot {
                    status: SubAgentStatus::Stopped,
                    error: response.termination_detail,
                    done: true,
                    output_text,
                    raw_task,
                },
                Some("error") => A2aTaskSnapshot {
                    status: SubAgentStatus::Failed,
                    error: response
                        .termination_detail
                        .or_else(|| Some("remote run failed".into())),
                    done: true,
                    output_text,
                    raw_task,
                },
                _ => A2aTaskSnapshot {
                    status: SubAgentStatus::Completed,
                    error: response.termination_detail,
                    done: true,
                    output_text,
                    raw_task,
                },
            }
        }
        "failed" | "error" => A2aTaskSnapshot {
            status: SubAgentStatus::Failed,
            error: response
                .termination_detail
                .or_else(|| Some("remote run failed".into())),
            done: true,
            output_text,
            raw_task,
        },
        "cancelled" | "stopped" => A2aTaskSnapshot {
            status: SubAgentStatus::Stopped,
            error: response.termination_detail,
            done: true,
            output_text,
            raw_task,
        },
        _ => A2aTaskSnapshot {
            status: SubAgentStatus::Running,
            error: None,
            done: false,
            output_text,
            raw_task,
        },
    }
}

fn push_trimmed_text(out: &mut Vec<String>, text: &str) {
    let trimmed = text.trim();
    if !trimmed.is_empty() {
        out.push(trimmed.to_string());
    }
}

fn extract_text_parts(value: &Value, out: &mut Vec<String>) {
    match value {
        Value::Null => {}
        Value::Bool(_) | Value::Number(_) => out.push(value.to_string()),
        Value::String(text) => push_trimmed_text(out, text),
        Value::Array(items) => {
            for item in items {
                extract_text_parts(item, out);
            }
        }
        Value::Object(object) => {
            if let Some(role) = object
                .get("role")
                .and_then(Value::as_str)
                .map(str::trim)
                .map(str::to_ascii_lowercase)
            {
                if role != "assistant" && role != "agent" {
                    return;
                }
            }

            if let Some(text) = object.get("text").and_then(Value::as_str) {
                push_trimmed_text(out, text);
                return;
            }
            if let Some(content) = object.get("content") {
                extract_text_parts(content, out);
                return;
            }
            if let Some(parts) = object.get("parts") {
                extract_text_parts(parts, out);
                return;
            }
            if let Some(data) = object.get("data") {
                match data {
                    Value::String(text) => push_trimmed_text(out, text),
                    Value::Null => {}
                    _ => out.push(data.to_string()),
                }
                return;
            }
            if let Some(message) = object.get("message") {
                extract_text_parts(message, out);
                return;
            }
            if let Some(artifact) = object.get("artifact") {
                extract_text_parts(artifact, out);
            }
        }
    }
}

fn extract_task_output_text(response: &A2aTaskResponse) -> Option<String> {
    let mut parts = Vec::new();
    for artifact in &response.artifacts {
        extract_text_parts(artifact, &mut parts);
    }
    if parts.is_empty() {
        if let Some(message) = response.message.as_ref() {
            extract_text_parts(message, &mut parts);
        }
    }
    if parts.is_empty() {
        for message in response.history.iter().rev() {
            extract_text_parts(message, &mut parts);
            if !parts.is_empty() {
                break;
            }
        }
    }
    if parts.is_empty() {
        None
    } else {
        Some(parts.join("\n\n"))
    }
}

pub(super) async fn submit_a2a_run(
    target: &A2aAgentBinding,
    messages: &[Message],
) -> Result<A2aStartResponse, String> {
    A2aRuntimeClient::shared()
        .submit_run(target, messages)
        .await
        .map_err(|err| err.to_string())
}

pub(super) async fn fetch_a2a_task_snapshot(
    target: &A2aAgentBinding,
    task_id: &str,
) -> Result<A2aTaskSnapshot, String> {
    A2aRuntimeClient::shared()
        .fetch_task_snapshot(target, task_id)
        .await
        .map_err(|err| err.to_string())
}

pub(super) async fn poll_a2a_run_to_completion(
    target: &A2aAgentBinding,
    task_id: &str,
) -> A2aTaskSnapshot {
    loop {
        match fetch_a2a_task_snapshot(target, task_id).await {
            Ok(snapshot) if snapshot.done => return snapshot,
            Ok(_) => {
                sleep(Duration::from_millis(target.poll_interval_ms)).await;
            }
            Err(err) => {
                return A2aTaskSnapshot {
                    status: SubAgentStatus::Failed,
                    error: Some(err),
                    done: true,
                    output_text: None,
                    raw_task: Value::Null,
                }
            }
        }
    }
}

pub(super) async fn cancel_a2a_run(target: &A2aAgentBinding, task_id: &str) -> Result<(), String> {
    A2aRuntimeClient::shared()
        .cancel_run(target, task_id)
        .await
        .map_err(|err| err.to_string())
}

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

    #[test]
    fn single_user_input_requires_exactly_one_user_message() {
        let result = single_user_input(&[Message::user("one"), Message::user("two")]);
        assert!(result.is_err());
    }

    #[test]
    fn single_user_input_rejects_non_user_role() {
        let result = single_user_input(&[Message::assistant("hello")]);
        assert!(matches!(
            result,
            Err(A2aClientError::UnsupportedMessageRole)
        ));
    }

    #[test]
    fn map_task_status_uses_done_and_termination_code() {
        let snapshot = map_task_status(A2aTaskResponse {
            context_id: Some("ctx-1".to_string()),
            status: "done".to_string(),
            termination_code: Some("cancelled".to_string()),
            termination_detail: None,
            message: None,
            history: Vec::new(),
            artifacts: Vec::new(),
        });
        assert_eq!(snapshot.status, SubAgentStatus::Stopped);
        assert!(snapshot.done);
        assert_eq!(snapshot.raw_task["contextId"], json!("ctx-1"));
    }

    #[test]
    fn map_task_status_extracts_output_from_artifacts_then_message() {
        let snapshot = map_task_status(A2aTaskResponse {
            context_id: Some("ctx-2".to_string()),
            status: "completed".to_string(),
            termination_code: None,
            termination_detail: None,
            message: Some(json!({"role":"assistant","content":"fallback"})),
            history: Vec::new(),
            artifacts: vec![json!({
                "parts": [
                    {"text": "hello"},
                    {"text": "world"}
                ]
            })],
        });
        assert_eq!(snapshot.output_text.as_deref(), Some("hello\n\nworld"));
        assert_eq!(
            snapshot.raw_task["artifacts"][0]["parts"][0]["text"],
            json!("hello")
        );
    }
}