Repository navigation
Expand file tree
/
Copy pathcode_review.rs
More file actions
184 lines (165 loc) · 8 KB
/
Copy pathcode_review.rs
File metadata and controls
184 lines (165 loc) · 8 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
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
//! Code review agent: parallel sub-agents reviewing the same source file.
//!
//! Demonstrates:
//! - Reading a real file and storing it in SharedState
//! - 3 parallel sub-agents each analyzing a different aspect
//! - Sub-agents writing structured findings back to shared state
//! - Parent aggregating all reviews into a unified report
//!
//! Run on any source file:
//! ANTHROPIC_API_KEY=sk-... cargo run --example code_review -- path/to/file.rs
//!
//! Try it on this repo:
//! ANTHROPIC_API_KEY=sk-... cargo run --example code_review -- src/shared_state.rs
use std::sync::{Arc, Mutex};
use yoagent::provider::{AnthropicProvider, ModelConfig, StreamProvider};
use yoagent::shared_state::SharedState;
use yoagent::sub_agent::SubAgentTool;
use yoagent::*;
#[tokio::main]
async fn main() {
let api_key = std::env::var("ANTHROPIC_API_KEY").expect("Set ANTHROPIC_API_KEY");
let config = ModelConfig::anthropic("claude-sonnet-5", "Claude Sonnet 5");
let provider: Arc<dyn StreamProvider> = Arc::new(AnthropicProvider);
// --- Read the target file from CLI args ---
let file_path = std::env::args().nth(1).unwrap_or_else(|| {
eprintln!("Usage: cargo run --example code_review -- <file_path>");
std::process::exit(1);
});
let source_code = std::fs::read_to_string(&file_path).unwrap_or_else(|e| {
eprintln!("Failed to read '{}': {}", file_path, e);
std::process::exit(1);
});
println!("Reviewing: {} ({} bytes)\n", file_path, source_code.len());
// --- Store the source code once in shared state ---
let state = SharedState::new();
state
.set("source_code", source_code)
.await
.expect("store source code");
state
.set("file_path", file_path.clone())
.await
.expect("store file path");
// --- Three reviewers, each focused on a different aspect ---
let bug_reviewer =
SubAgentTool::from_provider("bug_reviewer", Arc::clone(&provider), config.clone())
.with_description("Reviews code for bugs and logic errors")
.with_system_prompt(
"You are a bug-finding specialist. Read the source code from shared state \
(key: 'source_code'), look for bugs, logic errors, off-by-one errors, \
race conditions, and potential panics. Write your findings to shared state \
under key 'bugs_review'. Format: bullet points, each with line reference \
and severity (critical/warning/info). If no bugs found, say so.",
)
.with_api_key(&api_key)
.with_shared_state(state.clone())
.with_max_turns(5);
let quality_reviewer =
SubAgentTool::from_provider("quality_reviewer", Arc::clone(&provider), config.clone())
.with_description("Reviews code quality and style")
.with_system_prompt(
"You are a code quality reviewer. Read the source code from shared state \
(key: 'source_code'), evaluate naming, structure, idiomatic usage, \
error handling patterns, and API design. Write your findings to shared state \
under key 'quality_review'. Format: bullet points with specific suggestions. \
Mention what's done well too.",
)
.with_api_key(&api_key)
.with_shared_state(state.clone())
.with_max_turns(5);
let docs_reviewer =
SubAgentTool::from_provider("docs_reviewer", Arc::clone(&provider), config.clone())
.with_description("Reviews documentation completeness")
.with_system_prompt(
"You are a documentation reviewer. Read the source code from shared state \
(key: 'source_code') and the file path (key: 'file_path'). Evaluate: \
are public items documented? Are doc comments accurate? Are edge cases \
explained? Are examples provided where helpful? Write findings to shared \
state under key 'docs_review'. Format: bullet points.",
)
.with_api_key(&api_key)
.with_shared_state(state.clone())
.with_max_turns(5);
// --- Run all three in parallel with streaming ---
println!("Dispatching 3 reviewers in parallel...\n");
let make_ctx = |label: &str| -> (ToolContext, Arc<Mutex<String>>) {
let label = label.to_string();
let buf: Arc<Mutex<String>> = Arc::new(Mutex::new(String::new()));
let ctx =
ToolContext::new(format!("tc-{}", label), label.clone()).with_on_update(Arc::new({
let label = label.clone();
let buf = buf.clone();
move |result: ToolResult| {
for content in &result.content {
if let Content::Text { text } = content {
let mut b = buf.lock().unwrap();
// Tool call events: flush buffer first, print on own line
if text.starts_with("[sub-agent calling tool:") {
if !b.is_empty() {
eprintln!("[{}] {}", label, b.drain(..).collect::<String>());
}
eprintln!("[{}] {}", label, text);
continue;
}
b.push_str(text);
while let Some(pos) = b.find('\n') {
let line: String = b.drain(..=pos).collect();
eprint!("[{}] {}", label, line);
}
}
}
}
}));
(ctx, buf)
};
let (ctx1, buf1) = make_ctx("bugs");
let (ctx2, buf2) = make_ctx("quality");
let (ctx3, buf3) = make_ctx("docs");
let (r1, r2, r3) = tokio::join!(
bug_reviewer.execute(
serde_json::json!({"task": "Review the source code for bugs and logic errors."}),
ctx1,
),
quality_reviewer.execute(
serde_json::json!({"task": "Review the source code for quality and style."}),
ctx2,
),
docs_reviewer.execute(
serde_json::json!({"task": "Review the source code for documentation completeness."}),
ctx3,
),
);
// Flush any remaining buffered text
for (label, buf) in [("bugs", buf1), ("quality", buf2), ("docs", buf3)] {
let b = buf.lock().unwrap();
if !b.is_empty() {
eprintln!("[{}] {}", label, *b);
}
}
r1.expect("bug reviewer failed");
r2.expect("quality reviewer failed");
r3.expect("docs reviewer failed");
// --- Print unified review ---
println!("═══════════════════════════════════════════════════════════");
println!(" Code Review: {}", file_path);
println!("═══════════════════════════════════════════════════════════\n");
let sections = [
("bugs_review", "Bug Analysis"),
("quality_review", "Code Quality"),
("docs_review", "Documentation"),
];
for (key, title) in sections {
println!("── {} ──\n", title);
match state.get(key).await {
Some(value) => println!("{}\n", value),
None => println!("(reviewer did not produce findings)\n"),
}
}
println!("═══════════════════════════════════════════════════════════");
println!(
" Review complete. Shared state keys: {}",
state.summary().await
);
println!("═══════════════════════════════════════════════════════════");
}