basic_expertise/
basic_expertise.rs

1//! Basic expertise creation and usage example
2
3use llm_toolkit_expertise::{
4    Anchor, ContextProfile, Expertise, KnowledgeFragment, Priority, TaskHealth, WeightedFragment,
5};
6
7fn main() {
8    println!("=== Basic Expertise Example ===\n");
9
10    // Create a code review expertise
11    let expertise = Expertise::new("rust-code-reviewer", "1.0.0")
12        .with_tag("lang:rust")
13        .with_tag("role:reviewer")
14        .with_tag("style:thorough")
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 SQL injection vulnerabilities".to_string(),
28                    "Verify input validation and sanitization".to_string(),
29                    "Review error handling for information leakage".to_string(),
30                    "Check for unsafe code blocks and justify their usage".to_string(),
31                ],
32            })
33            .with_priority(Priority::High)
34            .with_context(ContextProfile::Conditional {
35                task_types: vec!["security-review".to_string()],
36                user_states: vec![],
37                task_health: Some(TaskHealth::AtRisk),
38            }),
39        )
40        // Normal: Code quality guidelines
41        .with_fragment(
42            WeightedFragment::new(KnowledgeFragment::Guideline {
43                rule: "Prefer explicit error handling over unwrap/expect".to_string(),
44                anchors: vec![Anchor {
45                    context: "Parsing user input".to_string(),
46                    positive: "let value = parse_input(s).map_err(|e| Error::InvalidInput(e))?;".to_string(),
47                    negative: "let value = parse_input(s).unwrap();".to_string(),
48                    reason: "Unwrap can panic. Use proper error handling for user input.".to_string(),
49                }],
50            })
51            .with_priority(Priority::Normal),
52        )
53        // Low: Documentation suggestions
54        .with_fragment(
55            WeightedFragment::new(KnowledgeFragment::Text(
56                "Consider adding doc comments for public APIs. Use `cargo doc` to preview.".to_string(),
57            ))
58            .with_priority(Priority::Low),
59        )
60        // Quality standards
61        .with_fragment(
62            WeightedFragment::new(KnowledgeFragment::QualityStandard {
63                criteria: vec![
64                    "Code compiles without warnings".to_string(),
65                    "All tests pass".to_string(),
66                    "No clippy warnings".to_string(),
67                    "Public APIs have documentation".to_string(),
68                ],
69                passing_grade: "All criteria must be met before approval".to_string(),
70            })
71            .with_priority(Priority::High),
72        );
73
74    // Display expertise in various formats
75    println!("--- Tree Visualization ---\n");
76    println!("{}", expertise.to_tree());
77
78    println!("\n--- Generated Prompt (All Contexts) ---\n");
79    println!("{}", expertise.to_prompt());
80
81    println!("\n--- Generated Prompt (Security Review, At Risk) ---\n");
82    use llm_toolkit_expertise::ContextMatcher;
83    let security_context = ContextMatcher::new()
84        .with_task_type("security-review")
85        .with_task_health(TaskHealth::AtRisk);
86    println!("{}", expertise.to_prompt_with_context(&security_context));
87
88    println!("\n--- Mermaid Graph ---\n");
89    println!("{}", expertise.to_mermaid());
90
91    println!("\n--- JSON Serialization ---\n");
92    let json = serde_json::to_string_pretty(&expertise).expect("Failed to serialize");
93    println!("{}", json);
94}