-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_client.rs
More file actions
148 lines (132 loc) Β· 4.73 KB
/
Copy pathsimple_client.rs
File metadata and controls
148 lines (132 loc) Β· 4.73 KB
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
use std::time::Duration;
use tower_a2a::{
prelude::*,
protocol::{message::FileContent, AgentCapabilities, TaskError},
};
// Configuration - update these to match your agent
const AGENT_URL: &str = "https://your-agent-url";
const AUTH_TOKEN: &str = "your-auth-token";
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize tracing for logging
tracing_subscriber::fmt::init();
println!("π Tower-A2A Simple Client Example\n");
// Build the A2A client with HTTP transport and bearer authentication
let url = AGENT_URL.parse().unwrap();
let mut client = A2AClientBuilder::new_http(url)
.with_bearer_auth(AUTH_TOKEN.to_string())
.with_timeout(Duration::from_secs(30))
.build()?;
println!("β Client configured for: {AGENT_URL}\n");
// Step 1: Discover agent capabilities
println!("π Discovering agent capabilities...");
match client.discover().await {
Ok(AgentCard {
name,
description,
capabilities:
AgentCapabilities {
streaming,
task_management,
multi_turn,
..
},
..
}) => {
println!("β Connected to: {name}");
println!(" Description: {description}");
println!(" Capabilities:");
println!(" - Streaming: {streaming}");
println!(" - Task Management: {task_management}");
println!(" - Multi-turn: {multi_turn}");
println!();
}
Err(e) => {
eprintln!(
r#"β Failed to discover agent: {e}
Note: Make sure AGENT_URL points to a running A2A agent"#
);
return Ok(());
}
}
// Step 2: Send a message to the agent
println!("π¬ Sending message to agent...");
let message = Message::user("What is the weather like in San Francisco?");
let (id, artifacts) = match client.send_message(message).await {
Ok(Task {
id,
status,
artifacts,
..
}) => {
println!("β Task created: {id}");
println!(" Status: {status:?}");
(id, artifacts)
}
Err(e) => {
eprintln!("β Failed to send message: {e}");
return Ok(());
}
};
// Step 3: Poll for task completion
println!("\nβ³ Polling for task completion...");
match client.poll_until_complete(id, 1000, 30).await {
Ok(Task { status, error, .. }) => {
println!("β Task completed!");
println!(" Status: {status:?}");
if !artifacts.is_empty() {
println!("\nπ Agent artifacts:");
for Artifact {
artifact_id, parts, ..
} in &artifacts
{
println!(" Artifact: {artifact_id}");
for part in parts {
match part {
MessagePart::Text { text } => {
println!(" {text}");
}
MessagePart::File {
file:
FileContent {
name,
file_with_uri,
..
},
} => {
println!(
" [File: {name} - {}]",
file_with_uri.as_ref().unwrap_or(&"inline".to_string())
);
}
MessagePart::Data { .. } => {
println!(" [Structured data]");
}
}
}
}
}
if let Some(TaskError { message, .. }) = error {
println!("\nβ οΈ Task error: {message}");
}
}
Err(e) => {
eprintln!("β Failed to poll task: {e}");
}
}
// Step 4: List all tasks
println!("\nπ Listing all tasks...");
match client.list_all_tasks().await {
Ok(tasks) => {
println!("β Found {} tasks", tasks.len());
for (i, Task { id, status, .. }) in tasks.iter().take(5).enumerate() {
println!(" {}. {id} - {status:?}", i + 1);
}
}
Err(e) => {
eprintln!("β Failed to list tasks: {e}");
}
}
println!("\nβ
Example completed successfully!");
Ok(())
}