ai_agent/
understanding.rs1use crate::types::TaskPlan;
4use crate::models::LanguageModel;
5use crate::errors::AgentError;
6
7pub struct UnderstandingEngine {
9 model: std::sync::Arc<dyn LanguageModel>,
10}
11
12impl UnderstandingEngine {
13 pub fn new(model: std::sync::Arc<dyn LanguageModel>) -> Self {
14 Self { model }
15 }
16
17 pub async fn understand(&self, request: &str) -> Result<TaskPlan, AgentError> {
19 let prompt = self.build_understanding_prompt(request);
20 let response = self.model.complete(&prompt).await?;
21
22 self.parse_task_plan(&response.content)
23 }
24
25 fn build_understanding_prompt(&self, request: &str) -> String {
26 format!(
27 "You are an intelligent coding assistant with full autonomy.
28
29TASK TO ANALYZE: {request}
30
31Please analyze this task and provide:
321. Your understanding of what the user wants
332. Your approach to solving it
343. Assessment of complexity (Simple/Moderate/Complex)
354. Any requirements or dependencies you identify
36
37You have complete freedom in how to structure your response. Be thorough but concise."
38 )
39 }
40
41 fn parse_task_plan(&self, response: &str) -> Result<TaskPlan, AgentError> {
42 let complexity = if response.to_lowercase().contains("simple") {
43 crate::types::TaskComplexity::Simple
44 } else if response.to_lowercase().contains("complex") {
45 crate::types::TaskComplexity::Complex
46 } else {
47 crate::types::TaskComplexity::Moderate
48 };
49
50 let estimated_steps = if response.to_lowercase().contains("step") {
51 Some(self.extract_step_count(response))
52 } else {
53 None
54 };
55
56 Ok(TaskPlan {
57 understanding: response.to_string(),
58 approach: "AI-determined approach".to_string(),
59 complexity,
60 estimated_steps,
61 requirements: vec![],
62 })
63 }
64
65 fn extract_step_count(&self, response: &str) -> u32 {
66 let words: Vec<&str> = response.split_whitespace().collect();
68 for (i, word) in words.iter().enumerate() {
69 if word.to_lowercase() == "step" && i > 0 {
70 if let Some(num_word) = words.get(i - 1) {
71 if let Ok(num) = num_word.parse::<u32>() {
72 return num;
73 }
74 }
75 }
76 }
77 3 }
79}
80
81#[cfg(test)]
82mod tests {
83 use super::*;
84 use crate::models::MockModel;
85
86 #[tokio::test]
87 async fn test_understanding_engine() {
88 let model = std::sync::Arc::new(MockModel::new("test".to_string()));
89 let engine = UnderstandingEngine::new(model);
90
91 let plan = engine.understand("Create a simple hello world program").await.unwrap();
92
93 assert!(!plan.understanding.is_empty());
94 assert!(!plan.approach.is_empty());
95 }
96}