bevy-agent 0.1.0

AI-powered Bevy game development assistant with GPT/Claude integration
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
//! Project management and utilities for Bevy AI

use crate::config::{ProjectConfig, ProjectMetadata, ConversationEntry, GeneratedFile, Dependency};
use crate::error::{BevyAIError, Result};
use crate::ai::{BevyAIAgent, AIResponse};
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use walkdir::WalkDir;
use std::collections::HashMap;

/// Project manager for Bevy AI projects
pub struct ProjectManager {
    project_path: PathBuf,
    config: Option<ProjectConfig>,
}

/// Represents a Bevy AI project
pub struct Project {
    manager: ProjectManager,
    agent: BevyAIAgent,
}

/// Cargo.toml management
pub struct CargoManager {
    // This struct is currently not implemented
    // TODO: Add cargo management functionality
}

/// Dependency information
#[derive(Debug, Clone)]
pub struct DependencyInfo {
    /// The name of the dependency
    pub name: String,
    /// The version requirement of the dependency
    pub version: String,
    /// List of features to enable for this dependency
    pub features: Vec<String>,
    /// Whether this dependency is optional
    pub optional: bool,
    /// Whether to enable default features for this dependency
    pub default_features: bool,
}

impl ProjectManager {
    /// Create a new project manager for the given path
    pub fn new<P: AsRef<Path>>(project_path: P) -> Self {
        Self {
            project_path: project_path.as_ref().to_path_buf(),
            config: None,
        }
    }
    
    /// Initialize a new Bevy AI project
    pub async fn init(&mut self, name: &str, description: &str) -> Result<()> {
        // Create project directory structure
        self.create_directory_structure().await?;
        
        // Initialize Cargo.toml
        self.create_cargo_toml(name).await?;
        
        // Create basic main.rs
        self.create_main_rs().await?;
        
        // Create .bevy-agent.json config
        let metadata = ProjectMetadata {
            name: name.to_string(),
            description: description.to_string(),
            version: "0.1.0".to_string(),
            created_at: chrono::Utc::now(),
            updated_at: chrono::Utc::now(),
            bevy_version: "0.12".to_string(),
            features: Vec::new(),
            tags: Vec::new(),
        };
        
        let config = ProjectConfig {
            metadata,
            conversations: Vec::new(),
            generated_files: Vec::new(),
            dependencies: Vec::new(),
            templates: Vec::new(),
        };
        
        self.save_config(&config).await?;
        self.config = Some(config);
        
        Ok(())
    }
    
    /// Load existing project configuration
    pub async fn load(&mut self) -> Result<()> {
        let config_path = self.project_path.join(".bevy-agent.json");
        
        if !config_path.exists() {
            return Err(BevyAIError::project_not_found(self.project_path.display().to_string()));
        }
        
        let content = fs::read_to_string(&config_path)?;
        let config: ProjectConfig = serde_json::from_str(&content)?;
        
        self.config = Some(config);
        Ok(())
    }
    
    /// Save project configuration
    pub async fn save_config(&self, config: &ProjectConfig) -> Result<()> {
        let config_path = self.project_path.join(".bevy-agent.json");
        let content = serde_json::to_string_pretty(config)?;
        fs::write(&config_path, content)?;
        Ok(())
    }
    
    /// Get project configuration
    pub fn config(&self) -> Option<&ProjectConfig> {
        self.config.as_ref()
    }
    
    /// Get mutable project configuration
    pub fn config_mut(&mut self) -> Option<&mut ProjectConfig> {
        self.config.as_mut()
    }
    
    /// Add a conversation entry
    pub async fn add_conversation(&mut self, entry: ConversationEntry) -> Result<()> {
        if let Some(config) = &mut self.config {
            config.conversations.push(entry);
            config.metadata.updated_at = chrono::Utc::now();
            let config_clone = config.clone();
            self.save_config(&config_clone).await?;
        }
        Ok(())
    }
    
    /// Add a generated file entry
    pub async fn add_generated_file(&mut self, file: GeneratedFile) -> Result<()> {
        if let Some(config) = &mut self.config {
            config.generated_files.push(file);
            config.metadata.updated_at = chrono::Utc::now();
            let config_clone = config.clone();
            self.save_config(&config_clone).await?;
        }
        Ok(())
    }
    
    /// Add a dependency
    pub async fn add_dependency(&mut self, dependency: Dependency) -> Result<()> {
        if let Some(config) = &mut self.config {
            // Check if dependency already exists
            if !config.dependencies.iter().any(|d| d.name == dependency.name) {
                config.dependencies.push(dependency.clone());
                config.metadata.updated_at = chrono::Utc::now();
                let config_clone = config.clone();
                self.save_config(&config_clone).await?;
                
                // Update Cargo.toml
                self.update_cargo_dependencies(&[dependency]).await?;
            }
        }
        Ok(())
    }
    
    /// Create directory structure
    async fn create_directory_structure(&self) -> Result<()> {
        let dirs = [
            "src",
            "assets",
            "assets/textures",
            "assets/sounds",
            "assets/models",
            "assets/fonts",
            "examples",
            "tests",
            "benches",
        ];
        
        for dir in &dirs {
            let dir_path = self.project_path.join(dir);
            fs::create_dir_all(&dir_path)?;
        }
        
        Ok(())
    }
    
    /// Create basic Cargo.toml
    async fn create_cargo_toml(&self, name: &str) -> Result<()> {
        let cargo_content = format!(r#"[package]
name = "{}"
version = "0.1.0"
edition = "2021"
description = "A Bevy game created with Bevy AI"

[dependencies]
bevy = {{ version = "0.12", features = ["default"] }}

[dev-dependencies]
bevy = {{ version = "0.12", features = ["default", "dynamic_linking"] }}

[[example]]
name = "game"
path = "examples/game.rs"

[profile.dev]
opt-level = 1

[profile.dev.package."*"]
opt-level = 3
"#, name);
        
        let cargo_path = self.project_path.join("Cargo.toml");
        fs::write(&cargo_path, cargo_content)?;
        
        Ok(())
    }
    
    /// Create basic main.rs
    async fn create_main_rs(&self) -> Result<()> {
        let main_content = r#"use bevy::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Startup, setup)
        .add_systems(Update, (
            // Add your systems here
        ))
        .run();
}

fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
) {
    // Camera
    commands.spawn(Camera3dBundle {
        transform: Transform::from_xyz(0.0, 6.0, 12.0).looking_at(Vec3::new(0.0, 1.0, 0.0), Vec3::Y),
        ..default()
    });

    // Light
    commands.spawn(DirectionalLightBundle {
        directional_light: DirectionalLight {
            shadows_enabled: true,
            ..default()
        },
        transform: Transform {
            translation: Vec3::new(0.0, 2.0, 0.0),
            rotation: Quat::from_rotation_x(-std::f32::consts::FRAC_PI_4),
            ..default()
        },
        ..default()
    });

    // Ground plane
    commands.spawn(PbrBundle {
        mesh: meshes.add(Plane3d::default().mesh().size(8.0, 8.0)),
        material: materials.add(Color::rgb(0.3, 0.5, 0.3)),
        ..default()
    });
}
"#;
        
        let main_path = self.project_path.join("src/main.rs");
        fs::write(&main_path, main_content)?;
        
        Ok(())
    }
    
    /// Update Cargo.toml with new dependencies
    async fn update_cargo_dependencies(&self, dependencies: &[Dependency]) -> Result<()> {
        let cargo_path = self.project_path.join("Cargo.toml");
        let mut cargo_content = fs::read_to_string(&cargo_path)?;
        
        for dep in dependencies {
            let dep_line = if dep.features.is_empty() {
                format!("{} = \"{}\"", dep.name, dep.version)
            } else {
                format!("{} = {{ version = \"{}\", features = {:?} }}", 
                       dep.name, dep.version, dep.features)
            };
            
            // Simple append to dependencies section
            if let Some(deps_start) = cargo_content.find("[dependencies]") {
                let insert_pos = cargo_content[deps_start..].find('\n').unwrap() + deps_start + 1;
                cargo_content.insert_str(insert_pos, &format!("{}\n", dep_line));
            }
        }
        
        fs::write(&cargo_path, cargo_content)?;
        Ok(())
    }
    
    /// Analyze existing code for dependencies
    pub async fn analyze_dependencies(&self) -> Result<Vec<DependencyInfo>> {
        let mut dependencies = Vec::new();
        let src_path = self.project_path.join("src");
        
        if !src_path.exists() {
            return Ok(dependencies);
        }
        
        // Walk through all Rust files
        for entry in WalkDir::new(&src_path).into_iter().filter_map(|e| e.ok()) {
            if entry.path().extension().map_or(false, |ext| ext == "rs") {
                let content = fs::read_to_string(entry.path())?;
                dependencies.extend(self.extract_dependencies_from_code(&content));
            }
        }
        
        Ok(dependencies)
    }
    
    /// Extract dependencies from Rust code
    fn extract_dependencies_from_code(&self, code: &str) -> Vec<DependencyInfo> {
        let mut dependencies = HashMap::new();
        
        // Common Bevy-related dependencies based on use statements
        let dependency_patterns = [
            ("bevy_rapier2d", vec!["rapier", "physics"]),
            ("bevy_rapier3d", vec!["rapier", "physics"]),
            ("rand", vec!["rand"]),
            ("serde", vec!["serde"]),
            ("noise", vec!["noise"]),
            ("image", vec!["image"]),
            ("nalgebra", vec!["nalgebra", "na"]),
            ("glam", vec!["glam"]),
            ("winit", vec!["winit"]),
            ("wgpu", vec!["wgpu"]),
        ];
        
        for (dep_name, patterns) in &dependency_patterns {
            for pattern in patterns {
                if code.contains(pattern) {
                    dependencies.insert(dep_name.to_string(), DependencyInfo {
                        name: dep_name.to_string(),
                        version: "*".to_string(),
                        features: Vec::new(),
                        optional: false,
                        default_features: true,
                    });
                    break;
                }
            }
        }
        
        dependencies.into_values().collect()
    }
    
    /// Build the project
    pub async fn build(&self) -> Result<String> {
        let output = Command::new("cargo")
            .arg("build")
            .current_dir(&self.project_path)
            .output()?;
        
        if output.status.success() {
            Ok(String::from_utf8_lossy(&output.stdout).to_string())
        } else {
            Err(BevyAIError::build_system(
                String::from_utf8_lossy(&output.stderr).to_string()
            ))
        }
    }
    
    /// Run the project
    pub async fn run(&self) -> Result<String> {
        let output = Command::new("cargo")
            .arg("run")
            .current_dir(&self.project_path)
            .output()?;
        
        if output.status.success() {
            Ok(String::from_utf8_lossy(&output.stdout).to_string())
        } else {
            Err(BevyAIError::build_system(
                String::from_utf8_lossy(&output.stderr).to_string()
            ))
        }
    }
    
    /// Check code with clippy
    pub async fn check(&self) -> Result<String> {
        let output = Command::new("cargo")
            .args(&["clippy", "--", "-D", "warnings"])
            .current_dir(&self.project_path)
            .output()?;
        
        Ok(format!("{}\n{}", 
           String::from_utf8_lossy(&output.stdout),
           String::from_utf8_lossy(&output.stderr)))
    }
    
    /// Format code
    pub async fn format(&self) -> Result<String> {
        let output = Command::new("cargo")
            .arg("fmt")
            .current_dir(&self.project_path)
            .output()?;
        
        if output.status.success() {
            Ok("Code formatted successfully".to_string())
        } else {
            Err(BevyAIError::build_system(
                String::from_utf8_lossy(&output.stderr).to_string()
            ))
        }
    }
    
    /// Get project statistics
    pub async fn stats(&self) -> Result<ProjectStats> {
        let mut stats = ProjectStats::default();
        
        if let Some(config) = &self.config {
            stats.conversations = config.conversations.len();
            stats.generated_files = config.generated_files.len();
            stats.dependencies = config.dependencies.len();
            stats.features = config.metadata.features.len();
        }
        
        // Count lines of code
        let src_path = self.project_path.join("src");
        if src_path.exists() {
            for entry in WalkDir::new(&src_path).into_iter().filter_map(|e| e.ok()) {
                if entry.path().extension().map_or(false, |ext| ext == "rs") {
                    let content = fs::read_to_string(entry.path())?;
                    stats.lines_of_code += content.lines().count();
                    stats.rust_files += 1;
                }
            }
        }
        
        Ok(stats)
    }
}

impl Project {
    /// Create a new project with AI agent
    pub async fn new(project_path: PathBuf, agent: BevyAIAgent) -> Result<Self> {
        let mut manager = ProjectManager::new(project_path);
        manager.load().await?;
        
        Ok(Self { manager, agent })
    }
    
    /// Initialize a new project
    pub async fn init(
        project_path: PathBuf, 
        name: &str, 
        description: &str, 
        agent: BevyAIAgent
    ) -> Result<Self> {
        let mut manager = ProjectManager::new(&project_path);
        manager.init(name, description).await?;
        
        Ok(Self { manager, agent })
    }
    
    /// Generate game code with AI
    pub async fn generate_game(&mut self, description: &str) -> Result<AIResponse> {
        let response = self.agent
            .generate_game(description)
            .execute()
            .await?;
        
        // Save conversation
        let conversation = ConversationEntry {
            id: response.conversation_id,
            request: description.to_string(),
            response: response.content.clone(),
            model_used: response.model.clone(),
            timestamp: chrono::Utc::now(),
            tokens_used: response.tokens_used,
            cost: None, // TODO: Calculate cost based on model and tokens
            files_modified: vec!["src/main.rs".to_string()],
        };
        
        self.manager.add_conversation(conversation).await?;
        
        // Write generated code
        let code = self.agent.extract_code(&response.content);
        let main_path = self.manager.project_path.join("src/main.rs");
        fs::write(&main_path, &code)?;
        
        // Track generated file
        let generated_file = GeneratedFile {
            path: "src/main.rs".to_string(),
            generator: "generate_game".to_string(),
            model: response.model.clone(),
            created_at: chrono::Utc::now(),
            checksum: format!("{:x}", md5::compute(&code)),
        };
        
        self.manager.add_generated_file(generated_file).await?;
        
        Ok(response)
    }
    
    /// Add feature with AI
    pub async fn add_feature(&mut self, feature_description: &str) -> Result<AIResponse> {
        let main_path = self.manager.project_path.join("src/main.rs");
        let existing_code = fs::read_to_string(&main_path)?;
        
        let response = self.agent
            .add_feature(feature_description, &existing_code)
            .execute()
            .await?;
        
        // Save conversation
        let conversation = ConversationEntry {
            id: response.conversation_id,
            request: format!("Add feature: {}", feature_description),
            response: response.content.clone(),
            model_used: response.model.clone(),
            timestamp: chrono::Utc::now(),
            tokens_used: response.tokens_used,
            cost: None,
            files_modified: vec!["src/main.rs".to_string()],
        };
        
        self.manager.add_conversation(conversation).await?;
        
        // Write updated code
        let code = self.agent.extract_code(&response.content);
        fs::write(&main_path, &code)?;
        
        // Update project features
        if let Some(config) = self.manager.config_mut() {
            config.metadata.features.push(feature_description.to_string());
            config.metadata.updated_at = chrono::Utc::now();
            let config_clone = config.clone();
            self.manager.save_config(&config_clone).await?;
        }
        
        Ok(response)
    }
    
    /// Get project manager
    pub fn manager(&self) -> &ProjectManager {
        &self.manager
    }
    
    /// Get mutable project manager
    pub fn manager_mut(&mut self) -> &mut ProjectManager {
        &mut self.manager
    }
    
    /// Get AI agent
    pub fn agent(&self) -> &BevyAIAgent {
        &self.agent
    }
}

/// Project statistics
#[derive(Debug, Default)]
pub struct ProjectStats {
    /// Number of AI conversations in the project
    pub conversations: usize,
    /// Number of files generated by AI
    pub generated_files: usize,
    /// Number of dependencies in the project
    pub dependencies: usize,
    /// Number of features added to the project
    pub features: usize,
    /// Total lines of code in the project
    pub lines_of_code: usize,
    /// Number of Rust source files in the project
    pub rust_files: usize,
}