siumai 0.10.3

A unified LLM interface library for Rust
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
//! 💻 Code Assistant - AI-powered coding helper

#![allow(clippy::double_ended_iterator_last)]
//!
//! This example demonstrates how to build a comprehensive code assistant with:
//! - Code explanation and documentation generation
//! - Bug detection and fixing suggestions
//! - Code refactoring recommendations
//! - Multi-language support
//! - Interactive code review
//! - Code optimization suggestions
//!
//! Before running, set your API key:
//! ```bash
//! export OPENAI_API_KEY="your-key"
//! export GROQ_API_KEY="your-key"
//! ```
//!
//! Usage:
//! ```bash
//! cargo run --example code_assistant
//! ```

use siumai::models;
use siumai::prelude::*;
use std::fs;
use std::io::{self, Write};
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("💻 Code Assistant - AI-powered coding helper\n");

    // Initialize the code assistant
    let assistant = CodeAssistant::new().await?;

    println!("🎉 Code Assistant initialized! Available commands:");
    println!("  1. explain <file_path>     - Explain code in a file");
    println!("  2. review <file_path>      - Review code for issues");
    println!("  3. optimize <file_path>    - Suggest optimizations");
    println!("  4. document <file_path>    - Generate documentation");
    println!("  5. fix <file_path>         - Suggest bug fixes");
    println!("  6. refactor <file_path>    - Suggest refactoring");
    println!("  7. help                    - Show this help");
    println!("  8. quit                    - Exit the assistant\n");

    // Interactive command loop
    loop {
        print!("🤖 Code Assistant> ");
        io::stdout().flush()?;

        let mut input = String::new();
        io::stdin().read_line(&mut input)?;
        let input = input.trim();

        if input.is_empty() {
            continue;
        }

        let parts: Vec<&str> = input.split_whitespace().collect();
        if parts.is_empty() {
            continue;
        }

        match parts[0] {
            "explain" => {
                if parts.len() < 2 {
                    println!("❌ Usage: explain <file_path>");
                    continue;
                }
                assistant.explain_code(parts[1]).await?;
            }
            "review" => {
                if parts.len() < 2 {
                    println!("❌ Usage: review <file_path>");
                    continue;
                }
                assistant.review_code(parts[1]).await?;
            }
            "optimize" => {
                if parts.len() < 2 {
                    println!("❌ Usage: optimize <file_path>");
                    continue;
                }
                assistant.optimize_code(parts[1]).await?;
            }
            "document" => {
                if parts.len() < 2 {
                    println!("❌ Usage: document <file_path>");
                    continue;
                }
                assistant.generate_documentation(parts[1]).await?;
            }
            "fix" => {
                if parts.len() < 2 {
                    println!("❌ Usage: fix <file_path>");
                    continue;
                }
                assistant.suggest_fixes(parts[1]).await?;
            }
            "refactor" => {
                if parts.len() < 2 {
                    println!("❌ Usage: refactor <file_path>");
                    continue;
                }
                assistant.suggest_refactoring(parts[1]).await?;
            }
            "help" => {
                println!("📖 Available commands:");
                println!("  explain <file>   - Explain what the code does");
                println!("  review <file>    - Review code for potential issues");
                println!("  optimize <file>  - Suggest performance optimizations");
                println!("  document <file>  - Generate documentation");
                println!("  fix <file>       - Suggest bug fixes");
                println!("  refactor <file>  - Suggest code refactoring");
                println!("  help            - Show this help");
                println!("  quit            - Exit the assistant");
            }
            "quit" => {
                println!("👋 Goodbye! Happy coding!");
                break;
            }
            _ => {
                println!(
                    "❌ Unknown command: {}. Type 'help' for available commands.",
                    parts[0]
                );
            }
        }
        println!();
    }

    Ok(())
}

/// Code Assistant implementation
struct CodeAssistant {
    ai: Arc<dyn ChatCapability + Send + Sync>,
}

impl CodeAssistant {
    /// Create a new code assistant
    async fn new() -> Result<Self, Box<dyn std::error::Error>> {
        // Try to get API key from environment
        let api_key = std::env::var("GROQ_API_KEY")
            .or_else(|_| std::env::var("OPENAI_API_KEY"))
            .unwrap_or_else(|_| "demo-key".to_string());

        // Initialize AI provider with coding-optimized settings
        let ai = Siumai::builder()
            .openai()
            .api_key(&api_key)
            .model(models::openai::GPT_4O_MINI)
            .temperature(0.1) // Low temperature for more consistent code analysis
            .max_tokens(2000)
            .build()
            .await?;

        Ok(Self { ai: Arc::new(ai) })
    }

    /// Explain code in a file
    async fn explain_code(&self, file_path: &str) -> Result<(), Box<dyn std::error::Error>> {
        println!("🔍 Analyzing code in: {file_path}");

        let code = self.read_file(file_path)?;
        let language = self.detect_language(file_path);

        let system_prompt = format!(
            "You are an expert {language} programmer and code educator. \
            Explain the provided code in a clear, educational manner. \
            Break down complex concepts and explain the purpose, \
            structure, and key components."
        );

        let user_prompt = format!(
            "Please explain this {language} code:\n\n```{language}\n{code}\n```\n\n\
            Provide:\n\
            1. Overall purpose and functionality\n\
            2. Key components and their roles\n\
            3. Important algorithms or patterns used\n\
            4. Any notable design decisions"
        );

        let messages = vec![
            ChatMessage::system(&system_prompt).build(),
            ChatMessage::user(&user_prompt).build(),
        ];

        println!("💭 Generating explanation...\n");

        let response = self.ai.chat(messages).await?;
        if let Some(text) = response.text() {
            println!("📝 Code Explanation:\n{text}");
        }

        Ok(())
    }

    /// Review code for potential issues
    async fn review_code(&self, file_path: &str) -> Result<(), Box<dyn std::error::Error>> {
        println!("🔍 Reviewing code in: {file_path}");

        let code = self.read_file(file_path)?;
        let language = self.detect_language(file_path);

        let system_prompt = format!(
            "You are an expert {language} code reviewer with years of experience. \
            Review the provided code for potential issues, bugs, security vulnerabilities, \
            performance problems, and code quality issues. \
            Provide constructive feedback and specific suggestions."
        );

        let user_prompt = format!(
            "Please review this {language} code for issues:\n\n```{language}\n{code}\n```\n\n\
            Focus on:\n\
            1. Potential bugs and logic errors\n\
            2. Security vulnerabilities\n\
            3. Performance issues\n\
            4. Code quality and best practices\n\
            5. Error handling\n\
            6. Memory management (if applicable)\n\n\
            For each issue found, provide:\n\
            - Description of the problem\n\
            - Severity level (Critical/High/Medium/Low)\n\
            - Specific line numbers if possible\n\
            - Suggested fix"
        );

        let messages = vec![
            ChatMessage::system(&system_prompt).build(),
            ChatMessage::user(&user_prompt).build(),
        ];

        println!("🔍 Performing code review...\n");

        let response = self.ai.chat(messages).await?;
        if let Some(text) = response.text() {
            println!("📋 Code Review Results:\n{text}");
        }

        Ok(())
    }

    /// Suggest code optimizations
    async fn optimize_code(&self, file_path: &str) -> Result<(), Box<dyn std::error::Error>> {
        println!("⚡ Analyzing code for optimizations: {file_path}");

        let code = self.read_file(file_path)?;
        let language = self.detect_language(file_path);

        let system_prompt = format!(
            "You are a performance optimization expert for {language}. \
            Analyze the provided code and suggest specific optimizations \
            for better performance, memory usage, and efficiency. \
            Focus on practical, measurable improvements."
        );

        let user_prompt = format!(
            "Please analyze this {language} code for optimization opportunities:\n\n```{language}\n{code}\n```\n\n\
            Suggest optimizations for:\n\
            1. Performance improvements\n\
            2. Memory usage reduction\n\
            3. Algorithm efficiency\n\
            4. Resource utilization\n\
            5. Concurrency opportunities\n\n\
            For each optimization:\n\
            - Explain the current inefficiency\n\
            - Provide the optimized code\n\
            - Estimate the performance impact\n\
            - Mention any trade-offs"
        );

        let messages = vec![
            ChatMessage::system(&system_prompt).build(),
            ChatMessage::user(&user_prompt).build(),
        ];

        println!("⚡ Generating optimization suggestions...\n");

        let response = self.ai.chat(messages).await?;
        if let Some(text) = response.text() {
            println!("🚀 Optimization Suggestions:\n{text}");
        }

        Ok(())
    }

    /// Generate documentation for code
    async fn generate_documentation(
        &self,
        file_path: &str,
    ) -> Result<(), Box<dyn std::error::Error>> {
        println!("📚 Generating documentation for: {file_path}");

        let code = self.read_file(file_path)?;
        let language = self.detect_language(file_path);

        let system_prompt = format!(
            "You are a technical documentation expert for {language}. \
            Generate comprehensive, clear, and useful documentation \
            for the provided code. Follow the language's documentation \
            conventions and best practices."
        );

        let user_prompt = format!(
            "Please generate documentation for this {language} code:\n\n```{language}\n{code}\n```\n\n\
            Include:\n\
            1. Module/file overview\n\
            2. Function/method documentation with parameters and return values\n\
            3. Usage examples\n\
            4. Important notes and warnings\n\
            5. Related functions or dependencies\n\n\
            Use proper {language} documentation format and conventions."
        );

        let messages = vec![
            ChatMessage::system(&system_prompt).build(),
            ChatMessage::user(&user_prompt).build(),
        ];

        println!("📝 Generating documentation...\n");

        let response = self.ai.chat(messages).await?;
        if let Some(text) = response.text() {
            println!("📖 Generated Documentation:\n{text}");
        }

        Ok(())
    }

    /// Suggest bug fixes
    async fn suggest_fixes(&self, file_path: &str) -> Result<(), Box<dyn std::error::Error>> {
        println!("🐛 Analyzing code for potential bugs: {file_path}");

        let code = self.read_file(file_path)?;
        let language = self.detect_language(file_path);

        let system_prompt = format!(
            "You are an expert {language} debugger and bug hunter. \
            Analyze the provided code to identify potential bugs, \
            logic errors, and provide specific fixes. \
            Focus on common bug patterns and edge cases."
        );

        let user_prompt = format!(
            "Please analyze this {language} code for bugs and suggest fixes:\n\n```{language}\n{code}\n```\n\n\
            Look for:\n\
            1. Logic errors and edge cases\n\
            2. Null pointer/reference issues\n\
            3. Off-by-one errors\n\
            4. Race conditions (if applicable)\n\
            5. Resource leaks\n\
            6. Input validation issues\n\n\
            For each bug found:\n\
            - Describe the bug and its impact\n\
            - Show the problematic code\n\
            - Provide the corrected code\n\
            - Explain why the fix works"
        );

        let messages = vec![
            ChatMessage::system(&system_prompt).build(),
            ChatMessage::user(&user_prompt).build(),
        ];

        println!("🔧 Analyzing for bugs and generating fixes...\n");

        let response = self.ai.chat(messages).await?;
        if let Some(text) = response.text() {
            println!("🐛 Bug Analysis and Fixes:\n{text}");
        }

        Ok(())
    }

    /// Suggest code refactoring
    async fn suggest_refactoring(&self, file_path: &str) -> Result<(), Box<dyn std::error::Error>> {
        println!("🔄 Analyzing code for refactoring opportunities: {file_path}");

        let code = self.read_file(file_path)?;
        let language = self.detect_language(file_path);

        let system_prompt = format!(
            "You are an expert {language} software architect and refactoring specialist. \
            Analyze the provided code and suggest refactoring improvements \
            for better maintainability, readability, and design. \
            Follow SOLID principles and best practices."
        );

        let user_prompt = format!(
            "Please analyze this {language} code for refactoring opportunities:\n\n```{language}\n{code}\n```\n\n\
            Suggest refactoring for:\n\
            1. Code structure and organization\n\
            2. Function/method decomposition\n\
            3. Design patterns application\n\
            4. Naming improvements\n\
            5. Duplicate code elimination\n\
            6. Separation of concerns\n\n\
            For each refactoring suggestion:\n\
            - Explain the current issue\n\
            - Show the refactored code\n\
            - Explain the benefits\n\
            - Mention any considerations"
        );

        let messages = vec![
            ChatMessage::system(&system_prompt).build(),
            ChatMessage::user(&user_prompt).build(),
        ];

        println!("🔄 Generating refactoring suggestions...\n");

        let response = self.ai.chat(messages).await?;
        if let Some(text) = response.text() {
            println!("♻️ Refactoring Suggestions:\n{text}");
        }

        Ok(())
    }

    /// Read file content
    fn read_file(&self, file_path: &str) -> Result<String, Box<dyn std::error::Error>> {
        match fs::read_to_string(file_path) {
            Ok(content) => {
                if content.len() > 10000 {
                    // Truncate very large files
                    Ok(format!(
                        "{}...\n\n[File truncated - showing first 10000 characters]",
                        &content[..10000]
                    ))
                } else {
                    Ok(content)
                }
            }
            Err(e) => {
                println!("❌ Error reading file '{file_path}': {e}");
                Err(Box::new(e))
            }
        }
    }

    /// Detect programming language from file extension
    fn detect_language(&self, file_path: &str) -> &str {
        let extension = file_path.split('.').last().unwrap_or("");
        match extension {
            "rs" => "Rust",
            "py" => "Python",
            "js" | "jsx" => "JavaScript",
            "ts" | "tsx" => "TypeScript",
            "java" => "Java",
            "cpp" | "cc" | "cxx" => "C++",
            "c" => "C",
            "cs" => "C#",
            "go" => "Go",
            "php" => "PHP",
            "rb" => "Ruby",
            "swift" => "Swift",
            "kt" => "Kotlin",
            "dart" => "Dart",
            "scala" => "Scala",
            "clj" => "Clojure",
            "hs" => "Haskell",
            "ml" => "OCaml",
            "fs" => "F#",
            "elm" => "Elm",
            "ex" | "exs" => "Elixir",
            "erl" => "Erlang",
            "lua" => "Lua",
            "r" => "R",
            "m" => "MATLAB",
            "jl" => "Julia",
            "nim" => "Nim",
            "zig" => "Zig",
            "v" => "V",
            "d" => "D",
            "cr" => "Crystal",
            _ => "Unknown",
        }
    }
}

/// 🎯 Key Code Assistant Features Summary:
///
/// Core Features:
/// - Multi-language code analysis and explanation
/// - Comprehensive code review with issue detection
/// - Performance optimization suggestions
/// - Automated documentation generation
/// - Bug detection and fix recommendations
/// - Code refactoring suggestions
///
/// Interactive Features:
/// - Command-line interface with multiple commands
/// - File-based code analysis
/// - Language detection from file extensions
/// - Detailed explanations and suggestions
///
/// AI-Powered Analysis:
/// - Context-aware code understanding
/// - Best practices enforcement
/// - Security vulnerability detection
/// - Performance bottleneck identification
/// - Code quality assessment
///
/// Supported Languages:
/// - Rust, Python, JavaScript/TypeScript
/// - Java, C/C++, C#, Go, PHP
/// - Ruby, Swift, Kotlin, Dart
/// - And many more programming languages
///
/// Usage Examples:
/// ```bash
/// # Explain code functionality
/// explain src/main.rs
///
/// # Review code for issues
/// review src/lib.rs
///
/// # Suggest optimizations
/// optimize src/performance.rs
///
/// # Generate documentation
/// document src/api.rs
///
/// # Find and fix bugs
/// fix src/buggy_code.rs
///
/// # Suggest refactoring
/// refactor src/legacy.rs
/// ```
///
/// Production Considerations:
/// - File size limits (10KB truncation)
/// - Language-specific analysis
/// - Configurable AI parameters
/// - Error handling and recovery
/// - Extensible command system
///
/// Next Steps:
/// - Add support for project-wide analysis
/// - Implement code diff analysis
/// - Add integration with version control
/// - Create IDE plugins and extensions
/// - Add collaborative code review features
/// - Implement automated fix application
const fn _documentation() {}