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
//! A2A HTTP 客户端
//!
//! 用于发现和调用远程 A2A 兼容 Agent。
//!
//! 支持同步和流式两种任务模式:
//! - [`send_task`](A2AClient::send_task) — 同步等待任务完成
//! - [`send_task_streaming`](A2AClient::send_task_streaming) — SSE 流式接收实时事件
use super::types::*;
use crate::error::{ReactError, Result};
use futures::Stream;
use reqwest::Client;
use std::pin::Pin;
use tracing::{debug, info, warn};
/// A2A 客户端 — 发现和调用远程 Agent
pub struct A2AClient {
client: Client,
}
impl A2AClient {
/// Create a new A2A client instance.
pub fn new() -> Self {
Self {
client: Client::new(),
}
}
/// 发现远程 Agent:获取 Agent Card
///
/// 从 `{base_url}/.well-known/agent.json` 获取 Agent Card。
///
/// # 示例
///
/// ```rust,no_run
/// use echo_agent::a2a::A2AClient;
///
/// # #[tokio::main]
/// # async fn main() -> echo_agent::error::Result<()> {
/// let client = A2AClient::new();
/// let card = client.discover("http://localhost:8080").await?;
/// println!("发现 Agent: {} - {:?}", card.name, card.description);
/// for skill in &card.skills {
/// println!(" 技能: {} - {:?}", skill.name, skill.description);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn discover(&self, base_url: &str) -> Result<AgentCard> {
let url = format!("{}/.well-known/agent.json", base_url.trim_end_matches('/'));
info!(url = %url, "A2A: 发现远程 Agent");
let response = self
.client
.get(&url)
.send()
.await
.map_err(|e| ReactError::Other(format!("A2A 发现请求失败: {}", e)))?;
if !response.status().is_success() {
let status = response.status();
let body = response.text().await.unwrap_or_default();
return Err(ReactError::Other(format!(
"A2A 发现失败: HTTP {}: {}",
status, body
)));
}
let card: AgentCard = response
.json()
.await
.map_err(|e| ReactError::Other(format!("A2A Agent Card 解析失败: {}", e)))?;
info!(
agent = %card.name,
skills = card.skills.len(),
"A2A: 发现 Agent '{}' ({}个技能)",
card.name,
card.skills.len()
);
Ok(card)
}
/// 向远程 Agent 发送任务(同步等待完成)
///
/// # 示例
///
/// ```rust,no_run
/// use echo_agent::a2a::A2AClient;
///
/// # #[tokio::main]
/// # async fn main() -> echo_agent::error::Result<()> {
/// let client = A2AClient::new();
/// let result = client.send_task("http://localhost:8080", "请翻译'你好'为英文").await?;
/// if let Some(task) = result {
/// println!("任务状态: {}", task.status.state);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn send_task(&self, agent_url: &str, message: &str) -> Result<Option<A2ATask>> {
self.send_task_with_session(agent_url, message, None).await
}
/// 向远程 Agent 发送任务(带会话 ID)
pub async fn send_task_with_session(
&self,
agent_url: &str,
message: &str,
session_id: Option<String>,
) -> Result<Option<A2ATask>> {
let request = A2ATaskRequest {
jsonrpc: JSONRPC_VERSION.to_string(),
id: uuid::Uuid::new_v4().to_string(),
method: METHOD_SEND.to_string(),
params: A2ATaskParams {
id: None,
session_id,
message: A2AMessage::user_text(message),
},
};
info!(
url = %agent_url,
message_len = message.len(),
"A2A: 发送任务"
);
let response = self
.client
.post(agent_url)
.json(&request)
.send()
.await
.map_err(|e| ReactError::Other(format!("A2A 任务发送失败: {}", e)))?;
let task_response: A2ATaskResponse = response
.json()
.await
.map_err(|e| ReactError::Other(format!("A2A 响应解析失败: {}", e)))?;
if let Some(error) = task_response.error {
return Err(ReactError::Other(format!(
"A2A 远程错误 [{}]: {}",
error.code, error.message
)));
}
debug!(
task_id = ?task_response.result.as_ref().map(|t| &t.id),
"A2A: 任务发送完成"
);
Ok(task_response.result)
}
/// 向远程 Agent 流式发送任务(SSE)
///
/// 发送 `tasks/sendSubscribe` 请求,解析服务端返回的 SSE 事件流。
/// 返回 `A2AStreamEvent` 的异步 Stream,调用方可逐事件处理。
///
/// # 示例
///
/// ```rust,no_run
/// use echo_agent::a2a::{A2AClient, A2AStreamEvent, TaskState};
/// use futures::StreamExt;
///
/// # #[tokio::main]
/// # async fn main() -> echo_agent::error::Result<()> {
/// let client = A2AClient::new();
/// let mut stream = client
/// .send_task_streaming("http://localhost:8080", "翻译'你好'")
/// .await?;
///
/// while let Some(event) = stream.next().await {
/// match event {
/// A2AStreamEvent::StatusUpdate(e) => {
/// println!("状态: {}", e.status.state);
/// if e.is_final { break; }
/// }
/// A2AStreamEvent::ArtifactUpdate(e) => {
/// for part in &e.artifact.parts {
/// if let echo_agent::a2a::A2APart::Text { text } = part {
/// print!("{}", text);
/// }
/// }
/// }
/// }
/// }
/// # Ok(())
/// # }
/// ```
pub async fn send_task_streaming(
&self,
agent_url: &str,
message: &str,
) -> Result<Pin<Box<dyn Stream<Item = A2AStreamEvent> + Send>>> {
self.send_task_streaming_with_session(agent_url, message, None)
.await
}
/// 向远程 Agent 流式发送任务(带会话 ID)
pub async fn send_task_streaming_with_session(
&self,
agent_url: &str,
message: &str,
session_id: Option<String>,
) -> Result<Pin<Box<dyn Stream<Item = A2AStreamEvent> + Send>>> {
let request = A2ATaskRequest {
jsonrpc: JSONRPC_VERSION.to_string(),
id: uuid::Uuid::new_v4().to_string(),
method: METHOD_SEND_SUBSCRIBE.to_string(),
params: A2ATaskParams {
id: None,
session_id,
message: A2AMessage::user_text(message),
},
};
info!(url = %agent_url, "A2A: 发送流式任务");
let response = self
.client
.post(agent_url)
.header("Accept", "text/event-stream")
.json(&request)
.send()
.await
.map_err(|e| ReactError::Other(format!("A2A 流式请求失败: {}", e)))?;
if !response.status().is_success() {
let status = response.status();
let body = response.text().await.unwrap_or_default();
return Err(ReactError::Other(format!(
"A2A 流式请求失败: HTTP {}: {}",
status, body
)));
}
let byte_stream = response.bytes_stream();
let event_stream = async_stream::stream! {
use futures::StreamExt;
let mut buffer = String::new();
let mut byte_stream = Box::pin(byte_stream);
while let Some(chunk_result) = byte_stream.next().await {
let chunk = match chunk_result {
Ok(bytes) => match String::from_utf8(bytes.to_vec()) {
Ok(s) => s,
Err(e) => {
warn!("A2A SSE: UTF-8 解码失败: {}", e);
continue;
}
},
Err(e) => {
warn!("A2A SSE: 读取块失败: {}", e);
break;
}
};
buffer.push_str(&chunk);
while let Some(boundary) = buffer.find("\n\n") {
let event_block = buffer[..boundary].to_string();
buffer = buffer[boundary + 2..].to_string();
for line in event_block.lines() {
let line = line.trim();
if let Some(data) = line.strip_prefix("data:").map(|s| s.trim_start())
&& let Some(event) = Self::parse_sse_data(data)
{
yield event;
}
}
}
}
// flush remaining
if !buffer.is_empty() {
for line in buffer.lines() {
let line = line.trim();
if let Some(data) = line.strip_prefix("data:").map(|s| s.trim_start())
&& let Some(event) = Self::parse_sse_data(data) {
yield event;
}
}
}
};
Ok(Box::pin(event_stream))
}
/// 查询远程任务状态
pub async fn get_task(&self, agent_url: &str, task_id: &str) -> Result<Option<A2ATask>> {
let request = A2ATaskRequest {
jsonrpc: JSONRPC_VERSION.to_string(),
id: uuid::Uuid::new_v4().to_string(),
method: METHOD_GET.to_string(),
params: A2ATaskParams {
id: Some(task_id.to_string()),
session_id: None,
message: A2AMessage::user_text(""),
},
};
let response = self
.client
.post(agent_url)
.json(&request)
.send()
.await
.map_err(|e| ReactError::Other(format!("A2A 任务查询失败: {}", e)))?;
let task_response: A2ATaskResponse = response
.json()
.await
.map_err(|e| ReactError::Other(format!("A2A 响应解析失败: {}", e)))?;
if let Some(error) = task_response.error {
return Err(ReactError::Other(format!(
"A2A error {}: {}",
error.code, error.message
)));
}
Ok(task_response.result)
}
/// 取消远程任务
pub async fn cancel_task(&self, agent_url: &str, task_id: &str) -> Result<Option<A2ATask>> {
let request = A2ATaskRequest {
jsonrpc: JSONRPC_VERSION.to_string(),
id: uuid::Uuid::new_v4().to_string(),
method: METHOD_CANCEL.to_string(),
params: A2ATaskParams {
id: Some(task_id.to_string()),
session_id: None,
message: A2AMessage::user_text(""),
},
};
let response = self
.client
.post(agent_url)
.json(&request)
.send()
.await
.map_err(|e| ReactError::Other(format!("A2A 任务取消失败: {}", e)))?;
let task_response: A2ATaskResponse = response
.json()
.await
.map_err(|e| ReactError::Other(format!("A2A 响应解析失败: {}", e)))?;
if let Some(error) = task_response.error {
return Err(ReactError::Other(format!(
"A2A error {}: {}",
error.code, error.message
)));
}
Ok(task_response.result)
}
// ── 内部辅助 ─────────────────────────────────────────────────────────────
fn parse_sse_data(data: &str) -> Option<A2AStreamEvent> {
// SSE data 可能是 A2AStreamResponse(带 jsonrpc 包装)或直接是 A2AStreamEvent
if let Ok(stream_resp) = serde_json::from_str::<A2AStreamResponse>(data) {
return stream_resp.result;
}
serde_json::from_str::<A2AStreamEvent>(data).ok()
}
}
impl Default for A2AClient {
fn default() -> Self {
Self::new()
}
}