bevy_debugger_mcp 0.1.8

AI-assisted debugging for Bevy games through Claude Code using Model Context Protocol
Documentation
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
use crate::brp_messages::{BrpRequest, QueryFilter};
use crate::error::{Error, Result};
use crate::semantic_analyzer::{SemanticAnalyzer, SemanticQueryResult};
/// Query parsing for natural language to BRP conversion
use regex::Regex;
use std::collections::HashMap;

/// Trait for parsing natural language queries into BRP requests
pub trait QueryParser {
    /// Parse a natural language query into a BRP request
    ///
    /// # Errors
    /// Returns error if query cannot be parsed or is invalid
    fn parse(&self, query: &str) -> Result<BrpRequest>;

    /// Parse a semantic query and return detailed results
    ///
    /// # Errors
    /// Returns error if query cannot be parsed or analyzed
    fn parse_semantic(&self, query: &str) -> Result<SemanticQueryResult>;

    /// Get help text for supported query patterns
    fn help(&self) -> &str;
}

/// Basic pattern-based query parser using regex
pub struct RegexQueryParser {
    patterns: Vec<QueryPattern>,
    semantic_analyzer: SemanticAnalyzer,
}

struct QueryPattern {
    pattern: Regex,
    builder: fn(&regex::Captures) -> Result<BrpRequest>,
    description: &'static str,
}

impl RegexQueryParser {
    /// Create a new regex-based query parser
    #[must_use]
    pub fn new() -> Self {
        let patterns = vec![
            // List all entities
            QueryPattern {
                pattern: Regex::new(r"^(?i)list\s+all\s+entities?$").unwrap(),
                builder: |_| Ok(BrpRequest::ListEntities { filter: None }),
                description: "list all entities - List all entities in the game",
            },

            // Show specific entity
            QueryPattern {
                pattern: Regex::new(r"^(?i)show\s+entity\s+(\d+)$").unwrap(),
                builder: |caps| {
                    let entity_id = caps[1].parse::<u64>()
                        .map_err(|_| Error::Brp("Invalid entity ID".to_string()))?;
                    Ok(BrpRequest::Get {
                        entity: entity_id,
                        components: None,
                    })
                },
                description: "show entity X - Show details for entity with ID X",
            },

            // Find entities with component
            QueryPattern {
                pattern: Regex::new(r"^(?i)find\s+entities\s+with\s+component\s+([a-zA-Z_:]+)$").unwrap(),
                builder: |caps| {
                    let component_type = caps[1].to_string();
                    Ok(BrpRequest::Query {
                        filter: Some(QueryFilter {
                            with: Some(vec![component_type]),
                            without: None,
                            where_clause: None,
                        }),
                        limit: None,
                    })
                },
                description: "find entities with component Y - Find all entities that have component Y",
            },

            // Find entities without component
            QueryPattern {
                pattern: Regex::new(r"^(?i)find\s+entities\s+without\s+component\s+([a-zA-Z_:]+)$").unwrap(),
                builder: |caps| {
                    let component_type = caps[1].to_string();
                    Ok(BrpRequest::Query {
                        filter: Some(QueryFilter {
                            with: None,
                            without: Some(vec![component_type]),
                            where_clause: None,
                        }),
                        limit: None,
                    })
                },
                description: "find entities without component Y - Find all entities that don't have component Y",
            },

            // List component types
            QueryPattern {
                pattern: Regex::new(r"^(?i)list\s+components?$").unwrap(),
                builder: |_| Ok(BrpRequest::ListComponents),
                description: "list components - List all available component types",
            },

            // Find entities with multiple components
            QueryPattern {
                pattern: Regex::new(r"^(?i)find\s+entities\s+with\s+(?:components?\s+)?([a-zA-Z_:,\s]+)$").unwrap(),
                builder: |caps| {
                    let components_str = &caps[1];
                    let components: Vec<String> = components_str
                        .split(',')
                        .map(|s| s.trim().to_string())
                        .filter(|s| !s.is_empty())
                        .collect();

                    if components.is_empty() {
                        return Err(Error::Brp("No components specified".to_string()));
                    }

                    Ok(BrpRequest::Query {
                        filter: Some(QueryFilter {
                            with: Some(components),
                            without: None,
                            where_clause: None,
                        }),
                        limit: None,
                    })
                },
                description: "find entities with A, B, C - Find entities that have all specified components",
            },

            // Get entity with specific components only
            QueryPattern {
                pattern: Regex::new(r"^(?i)show\s+entity\s+(\d+)\s+components?\s+([a-zA-Z_:,\s]+)$").unwrap(),
                builder: |caps| {
                    let entity_id = caps[1].parse::<u64>()
                        .map_err(|_| Error::Brp("Invalid entity ID".to_string()))?;
                    let components_str = &caps[2];
                    let components: Vec<String> = components_str
                        .split(',')
                        .map(|s| s.trim().to_string())
                        .filter(|s| !s.is_empty())
                        .collect();

                    Ok(BrpRequest::Get {
                        entity: entity_id,
                        components: if components.is_empty() { None } else { Some(components) },
                    })
                },
                description: "show entity X components A, B - Show specific components of entity X",
            },

            // Query with limit
            QueryPattern {
                pattern: Regex::new(r"^(?i)find\s+(\d+)\s+entities\s+with\s+component\s+([a-zA-Z_:]+)$").unwrap(),
                builder: |caps| {
                    let limit = caps[1].parse::<usize>()
                        .map_err(|_| Error::Brp("Invalid limit".to_string()))?;
                    let component_type = caps[2].to_string();
                    Ok(BrpRequest::Query {
                        filter: Some(QueryFilter {
                            with: Some(vec![component_type]),
                            without: None,
                            where_clause: None,
                        }),
                        limit: Some(limit),
                    })
                },
                description: "find N entities with component Y - Find up to N entities with component Y",
            },
        ];

        Self {
            patterns,
            semantic_analyzer: SemanticAnalyzer::new(),
        }
    }
}

impl Default for RegexQueryParser {
    fn default() -> Self {
        Self::new()
    }
}

impl QueryParser for RegexQueryParser {
    fn parse(&self, query: &str) -> Result<BrpRequest> {
        let query = query.trim();

        if query.is_empty() {
            return Err(Error::Brp("Empty query".to_string()));
        }

        // Try semantic analysis first
        if let Ok(semantic_result) = self.semantic_analyzer.analyze(query) {
            return Ok(semantic_result.request);
        }

        // Fall back to basic pattern matching
        for pattern in &self.patterns {
            if let Some(captures) = pattern.pattern.captures(query) {
                return (pattern.builder)(&captures);
            }
        }

        Err(Error::Brp(format!(
            "Unrecognized query pattern: '{}'. {}",
            query,
            self.help()
        )))
    }

    fn parse_semantic(&self, query: &str) -> Result<SemanticQueryResult> {
        self.semantic_analyzer.analyze(query)
    }

    fn help(&self) -> &str {
        "Supported query patterns:\n\
        Basic queries:\n\
        - list all entities\n\
        - show entity X\n\
        - find entities with component Y\n\
        - find entities without component Y\n\
        - find entities with A, B, C\n\
        - show entity X components A, B\n\
        - find N entities with component Y\n\
        - list components\n\
        \n\
        Semantic queries:\n\
        - find stuck entities\n\
        - show fast moving objects\n\
        - find overlapping colliders\n\
        - find memory leaks\n\
        - find inconsistent state\n\
        - find physics violations\n\
        - find stuck and fast entities (compound)"
    }
}

/// Query execution metrics
#[derive(Debug, Clone)]
pub struct QueryMetrics {
    pub query: String,
    pub execution_time_ms: u64,
    pub entity_count: usize,
    pub cache_hit: bool,
    pub timestamp: chrono::DateTime<chrono::Utc>,
}

/// Query result with metadata
#[derive(Debug, Clone)]
pub struct QueryResult {
    pub result: serde_json::Value,
    pub metrics: QueryMetrics,
}

/// Cached query result
#[derive(Debug, Clone)]
struct CachedResult {
    result: serde_json::Value,
    timestamp: std::time::Instant,
    entity_count: usize,
}

/// Query cache with TTL support
pub struct QueryCache {
    cache: dashmap::DashMap<String, CachedResult>,
    ttl_seconds: u64,
}

impl QueryCache {
    /// Create a new query cache with specified TTL
    #[must_use]
    pub fn new(ttl_seconds: u64) -> Self {
        Self {
            cache: dashmap::DashMap::new(),
            ttl_seconds,
        }
    }

    /// Get cached result if available and not expired
    pub fn get(&self, query: &str) -> Option<(serde_json::Value, usize)> {
        let entry = self.cache.get(query)?;

        if entry.timestamp.elapsed().as_secs() > self.ttl_seconds {
            drop(entry);
            self.cache.remove(query);
            return None;
        }

        Some((entry.result.clone(), entry.entity_count))
    }

    /// Cache a query result
    pub fn set(&self, query: String, result: serde_json::Value, entity_count: usize) {
        self.cache.insert(
            query,
            CachedResult {
                result,
                timestamp: std::time::Instant::now(),
                entity_count,
            },
        );
    }

    /// Clear expired entries from cache
    pub fn cleanup(&self) {
        let cutoff = std::time::Instant::now() - std::time::Duration::from_secs(self.ttl_seconds);
        self.cache.retain(|_, entry| entry.timestamp > cutoff);
    }

    /// Get cache statistics
    pub fn stats(&self) -> HashMap<String, usize> {
        let mut stats = HashMap::new();
        stats.insert("total_entries".to_string(), self.cache.len());

        let expired_count = self
            .cache
            .iter()
            .filter(|entry| entry.timestamp.elapsed().as_secs() > self.ttl_seconds)
            .count();
        stats.insert("expired_entries".to_string(), expired_count);

        stats
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_basic_query_parsing() {
        let parser = RegexQueryParser::new();

        // Test list all entities
        let result = parser.parse("list all entities").unwrap();
        matches!(result, BrpRequest::ListEntities { .. });

        // Test show entity
        let result = parser.parse("show entity 123").unwrap();
        if let BrpRequest::Get { entity, .. } = result {
            assert_eq!(entity, 123);
        } else {
            panic!("Expected Get request");
        }

        // Test find with component
        let result = parser
            .parse("find entities with component Transform")
            .unwrap();
        if let BrpRequest::Query {
            filter: Some(filter),
            ..
        } = result
        {
            assert_eq!(filter.with, Some(vec!["Transform".to_string()]));
        } else {
            panic!("Expected Query request");
        }
    }

    #[test]
    fn test_case_insensitive_parsing() {
        let parser = RegexQueryParser::new();

        let result1 = parser.parse("LIST ALL ENTITIES").unwrap();
        let result2 = parser.parse("list all entities").unwrap();

        matches!(result1, BrpRequest::ListEntities { .. });
        matches!(result2, BrpRequest::ListEntities { .. });
    }

    #[test]
    fn test_invalid_queries() {
        let parser = RegexQueryParser::new();

        assert!(parser.parse("").is_err());
        assert!(parser.parse("invalid query").is_err());
        assert!(parser.parse("show entity abc").is_err()); // Invalid entity ID
    }

    #[test]
    fn test_query_cache() {
        let cache = QueryCache::new(300); // 5 minute TTL

        let query = "list all entities";
        let result = serde_json::json!({"entities": []});

        // Cache miss
        assert!(cache.get(query).is_none());

        // Cache set and hit
        cache.set(query.to_string(), result.clone(), 0);
        let cached = cache.get(query).unwrap();
        assert_eq!(cached.0, result);
        assert_eq!(cached.1, 0);
    }

    #[test]
    fn test_multiple_components_query() {
        let parser = RegexQueryParser::new();

        let result = parser
            .parse("find entities with Transform, Velocity, Health")
            .unwrap();
        if let BrpRequest::Query {
            filter: Some(filter),
            ..
        } = result
        {
            let expected = vec![
                "Transform".to_string(),
                "Velocity".to_string(),
                "Health".to_string(),
            ];
            assert_eq!(filter.with, Some(expected));
        } else {
            panic!("Expected Query request");
        }
    }
}