#[cfg(feature = "agent")]
fn main() {
use llm_toolkit::agent::expertise::{Expertise, KnowledgeFragment, WeightedFragment};
use llm_toolkit::agent::{Agent, ExpertiseAgent, Payload};
use llm_toolkit::context::{ContextProfile, Priority, TaskHealth};
println!("=== Context-Aware Expertise Example ===\n");
println!("📚 Part 1: Defining Expertise with Conditional Fragments\n");
let code_reviewer_expertise = Expertise::new("code-reviewer", "1.0")
.with_description("AI Code Reviewer with context-aware guidance")
.with_fragment(
WeightedFragment::new(KnowledgeFragment::Text(
r#"# Code Review Principles
- Verify code compiles and all tests pass
- Check for proper error handling
- Ensure code follows project conventions
"#
.to_string(),
))
.with_priority(Priority::Critical)
.with_context(ContextProfile::Always),
)
.with_fragment(
WeightedFragment::new(KnowledgeFragment::Text(
r#"# Security Review Checklist
- Verify all inputs are validated and sanitized
- Check for SQL injection vulnerabilities
- Ensure proper authentication and authorization
- Look for XSS attack vectors
- Verify secure password storage (bcrypt, Argon2)
"#
.to_string(),
))
.with_priority(Priority::Critical)
.with_context(ContextProfile::Conditional {
task_types: vec!["security-review".to_string()],
user_states: vec![],
task_health: None,
}),
)
.with_fragment(
WeightedFragment::new(KnowledgeFragment::Text(
r#"⚠️ SLOW DOWN - EXTRA VIGILANCE REQUIRED ⚠️
This task is marked as AT RISK. Apply maximum scrutiny:
- Double-check all logic carefully
- Verify edge cases are handled
- Ensure no regression in existing functionality
- Consider requesting additional review
"#
.to_string(),
))
.with_priority(Priority::Critical)
.with_context(ContextProfile::Conditional {
task_types: vec![],
user_states: vec![],
task_health: Some(TaskHealth::AtRisk),
}),
)
.with_fragment(
WeightedFragment::new(KnowledgeFragment::Text(
r#"# Beginner-Friendly Guidance
- Explain technical terms clearly
- Provide step-by-step reasoning
- Include code examples where helpful
- Be patient and encouraging
"#
.to_string(),
))
.with_priority(Priority::High)
.with_context(ContextProfile::Conditional {
task_types: vec![],
user_states: vec!["beginner".to_string()],
task_health: None,
}),
);
println!("✅ Defined expertise with 4 conditional fragments:");
println!(" 1. Always visible: Base code review principles");
println!(" 2. task_type='security-review': Security checklist");
println!(" 3. task_health=AtRisk: Extra vigilance warning");
println!(" 4. user_state='beginner': Simplified explanations");
println!();
println!("📦 Part 2: Creating Payloads with RenderContext\n");
let _regular_review =
Payload::text("Review this authentication middleware").with_task_type("code-review");
println!("Scenario 1: Regular code review");
println!(" Task Type: code-review");
println!(" Expected Fragments: Base principles only");
println!();
let _security_review_at_risk = Payload::text("Review this password hashing implementation")
.with_task_type("security-review")
.with_task_health(TaskHealth::AtRisk);
println!("Scenario 2: Security review (at-risk)");
println!(" Task Type: security-review");
println!(" Task Health: AtRisk");
println!(" Expected Fragments: Base + Security + Extra vigilance");
println!();
let _beginner_review = Payload::text("Can you review my first API endpoint?")
.with_user_state("beginner")
.with_task_type("code-review");
println!("Scenario 3: Beginner code review");
println!(" User State: beginner");
println!(" Task Type: code-review");
println!(" Expected Fragments: Base + Beginner guidance");
println!();
println!("🤖 Part 3: ExpertiseAgent - Automatic Context Application\n");
struct DemoAgent;
#[async_trait::async_trait]
impl Agent for DemoAgent {
type Output = String;
type Expertise = &'static str;
fn expertise(&self) -> &&'static str {
&"Demo Agent"
}
async fn execute(
&self,
intent: Payload,
) -> Result<Self::Output, llm_toolkit::agent::AgentError> {
let messages = intent.to_messages();
let text = intent.to_text();
let mut response = String::from("=== Agent Received ===\n\n");
if !messages.is_empty() {
response.push_str("System Messages:\n");
for msg in messages {
response.push_str(&format!(" [{:?}] {}\n", msg.speaker, msg.content));
}
response.push('\n');
}
response.push_str(&format!("User Question:\n {}\n", text));
Ok(response)
}
fn name(&self) -> String {
"DemoAgent".to_string()
}
async fn is_available(&self) -> Result<(), llm_toolkit::agent::AgentError> {
Ok(())
}
}
let _expertise_agent = ExpertiseAgent::new(DemoAgent, code_reviewer_expertise);
println!("✅ Created ExpertiseAgent wrapper");
println!(" Inner Agent: DemoAgent");
println!(" Expertise: code-reviewer v1.0");
println!();
println!("🚀 Part 4: Execute and See Context-Aware Results\n");
println!("Note: This is a demonstration. In practice, use tokio::runtime.\n");
println!("Scenario 1 Execution (Regular Review):");
println!(" Payload: 'Review this authentication middleware'");
println!(" Context: task_type='code-review'");
println!(" Expected: Base code review principles only\n");
println!("Scenario 2 Execution (Security Review - At Risk):");
println!(" Payload: 'Review this password hashing implementation'");
println!(" Context: task_type='security-review', task_health=AtRisk");
println!(" Expected: Base + Security checklist + Extra vigilance warning\n");
println!("Scenario 3 Execution (Beginner):");
println!(" Payload: 'Can you review my first API endpoint?'");
println!(" Context: user_state='beginner', task_type='code-review'");
println!(" Expected: Base + Beginner-friendly explanations\n");
println!("🛠️ Part 5: Builder Pattern API Examples\n");
println!("Example 1: Chaining context methods");
println!(
r#" let payload = Payload::text("Review this code")
.with_task_type("security-review")
.with_user_state("beginner")
.with_task_health(TaskHealth::AtRisk);"#
);
println!();
println!("Example 2: Multiple user states");
println!(
r#" let payload = Payload::text("Explain this error")
.with_user_state("beginner")
.with_user_state("confused");"#
);
println!();
println!("Example 3: Direct RenderContext");
println!(
r#" use llm_toolkit::agent::expertise::RenderContext;
let context = RenderContext::new()
.with_task_type("performance-review")
.with_task_health(TaskHealth::OnTrack);
let payload = Payload::text("Optimize this query")
.with_render_context(context);"#
);
println!();
println!("🔄 Part 6: Context Separation - LLM Context vs RenderContext\n");
println!("RenderContext (structured metadata for expertise rendering):");
println!(r#" payload.with_task_type("security-review") // Invisible to LLM"#);
println!();
println!("LLM Context (natural language visible to LLM):");
println!(
r#" payload.with_context("This code handles payment processing") // Visible to LLM"#
);
println!();
println!("Both can coexist independently:");
println!(
r#" let payload = Payload::text("Review this code")
.with_context("This handles user authentication") // LLM sees this
.with_task_type("security-review"); // Controls expertise rendering"#
);
println!();
println!("{}", "=".repeat(60));
println!("\n✨ Summary: Benefits of Context-Aware Expertise\n");
println!("1. Zero Manual Context Management");
println!(" - Context flows automatically from Payload → ExpertiseAgent");
println!(" - No need to manually extract or apply context\n");
println!("2. Precise Expertise Rendering");
println!(" - Only relevant knowledge fragments are included");
println!(" - Reduces prompt bloat and improves focus\n");
println!("3. Flexible Context Dimensions");
println!(" - task_type: What kind of task (security-review, etc.)");
println!(" - user_states: User characteristics (beginner, expert, etc.)");
println!(" - task_health: Task status (AtRisk, OnTrack, etc.)\n");
println!("4. Backward Compatible");
println!(" - Payloads without render_context work normally");
println!(" - Expertise without conditional fragments still work\n");
println!("5. Type-Safe and Composable");
println!(" - Builder pattern with fluent API");
println!(" - ExpertiseAgent composable with any Agent implementation\n");
println!("{}", "=".repeat(60));
}
#[cfg(not(feature = "agent"))]
fn main() {
println!("This example requires the 'agent' feature.");
println!("Run with: cargo run --example context_aware_expertise --features agent");
}