agent_with_expertise/
agent_with_expertise.rs

1//! Example: Using Expertise with Agent macro
2//!
3//! Demonstrates how to use llm-toolkit-expertise's Expertise type
4//! with the #[agent(expertise = ...)] macro attribute.
5
6use llm_toolkit_expertise::{
7    Anchor, ContextProfile, Expertise, KnowledgeFragment, Priority, TaskHealth, WeightedFragment,
8};
9
10// Create an Expertise dynamically
11fn create_code_reviewer() -> Expertise {
12    Expertise::new("rust-code-reviewer", "1.0.0")
13        .with_tag("lang:rust")
14        .with_tag("role:reviewer")
15        // Critical: Always verify compilation
16        .with_fragment(
17            WeightedFragment::new(KnowledgeFragment::Text(
18                "ALWAYS run `cargo check` before reviewing code. Never review code that doesn't compile.".to_string(),
19            ))
20            .with_priority(Priority::Critical),
21        )
22        // High: Security checks for at-risk tasks
23        .with_fragment(
24            WeightedFragment::new(KnowledgeFragment::Logic {
25                instruction: "Perform security vulnerability scan".to_string(),
26                steps: vec![
27                    "Check for unsafe code blocks".to_string(),
28                    "Verify input validation and sanitization".to_string(),
29                    "Review error handling for information leakage".to_string(),
30                ],
31            })
32            .with_priority(Priority::High)
33            .with_context(ContextProfile::Conditional {
34                task_types: vec!["security-review".to_string()],
35                user_states: vec![],
36                task_health: Some(TaskHealth::AtRisk),
37            }),
38        )
39        // Normal: Code quality guidelines
40        .with_fragment(
41            WeightedFragment::new(KnowledgeFragment::Guideline {
42                rule: "Prefer explicit error handling over unwrap/expect".to_string(),
43                anchors: vec![Anchor {
44                    context: "Parsing user input".to_string(),
45                    positive: "let value = parse_input(s).map_err(|e| Error::InvalidInput(e))?;".to_string(),
46                    negative: "let value = parse_input(s).unwrap();".to_string(),
47                    reason: "Unwrap can panic. Use proper error handling for user input.".to_string(),
48                }],
49            })
50            .with_priority(Priority::Normal),
51        )
52}
53
54fn main() {
55    println!("=== Expertise-Based Agent Example ===\n");
56
57    // Create expertise
58    let expertise = create_code_reviewer();
59
60    // Display the expertise
61    println!("--- Generated Expertise Prompt ---\n");
62    println!("{}", expertise.to_prompt());
63
64    println!("\n--- Tree Visualization ---\n");
65    println!("{}", expertise.to_tree());
66
67    println!("\n=== Usage with Agent Macro ===\n");
68    println!("With the ToPrompt integration, you can now use:");
69    println!();
70    println!("```rust");
71    println!("const EXPERTISE: Expertise = create_code_reviewer();");
72    println!();
73    println!("#[derive(Agent)]");
74    println!("#[agent(expertise = EXPERTISE, output = \"String\")]");
75    println!("struct CodeReviewerAgent;");
76    println!("```");
77    println!();
78    println!("The expertise() method will automatically call EXPERTISE.to_prompt()");
79    println!("and cache the result for efficiency!");
80}