-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path09-subagents.rs
More file actions
70 lines (63 loc) · 1.99 KB
/
09-subagents.rs
File metadata and controls
70 lines (63 loc) · 1.99 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
// Example 9: Subagents
//
// Creates a specialized code-reviewer subagent with restricted tools.
//
// Run: cargo run --example 09-subagents
use open_agent_sdk::agent::SubagentDefinition;
use open_agent_sdk::{Agent, AgentOptions, SDKMessage};
use open_agent_sdk::types;
use std::collections::HashMap;
#[tokio::main]
async fn main() {
println!("--- Example 9: Subagents ---");
let mut agents = HashMap::new();
agents.insert(
"code-reviewer".to_string(),
SubagentDefinition {
description: "Expert code reviewer that analyzes code quality".to_string(),
instructions: Some(
"You are an expert code reviewer. Analyze code for:\n\
- Code quality and readability\n\
- Potential bugs or edge cases\n\
- Suggested improvements\n\
Be concise and constructive."
.to_string(),
),
tools: Some(vec![
"Read".to_string(),
"Glob".to_string(),
"Grep".to_string(),
]),
model: None,
},
);
let mut agent = Agent::new(AgentOptions {
max_turns: Some(15),
agents,
..Default::default()
})
.await
.unwrap();
let (mut rx, handle) = agent
.query("Use the code-reviewer agent to review src/lib.rs")
.await;
while let Some(event) = rx.recv().await {
match event {
SDKMessage::Assistant { message, .. } => {
let text = types::extract_text(&message);
if !text.is_empty() {
print!("{}", text);
}
}
SDKMessage::Result { cost_usd, .. } => {
println!("\n\n--- Done (${:.4}) ---", cost_usd);
}
SDKMessage::Error { message } => {
eprintln!("Error: {}", message);
}
_ => {}
}
}
handle.await.unwrap();
agent.close().await;
}