opencrates 0.1.0

AI-powered Rust crate generator with enterprise-grade observability and comprehensive testing automation
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
# OpenCrates

[![Build Status](https://github.com/nikjois/opencrates/workflows/CI/badge.svg)](https://github.com/nikjois/opencrates/actions)
[![Crates.io](https://img.shields.io/crates/v/opencrates.svg)](https://crates.io/crates/opencrates)
[![Documentation](https://docs.rs/opencrates/badge.svg)](https://docs.rs/opencrates)
[![License: MIT OR Apache-2.0](https://img.shields.io/badge/License-MIT%20OR%20Apache--2.0-blue.svg)](https://opensource.org/licenses/MIT)

**OpenCrates** is a powerful AI-driven Rust crate generator that leverages OpenAI's latest models to create production-ready Rust projects with comprehensive testing, documentation, and enterprise-grade observability features.

## Features

### Core Capabilities
- **AI-Powered Generation**: Uses GPT-4, GPT-4 Turbo, and other OpenAI models
- **Web Search Integration**: Enhanced context through real-time web search
- **Interactive Wizard**: Step-by-step crate creation process
- **Comprehensive Testing**: Automated unit, integration, and benchmark tests
- **Documentation**: Auto-generated docs with examples and API references
- **Containerization**: Docker and Docker Compose support
- **Template System**: Extensible template engine for different project types

### Enterprise Features
- **Observability**: Prometheus metrics, distributed tracing, health checks
- **Security**: Built-in security scanning and vulnerability detection
- **Performance**: Benchmarking and performance regression detection
- **Web API**: FastAPI integration for web-based interactions
- **Caching**: Multi-layer caching with Redis support
- **CI/CD**: GitHub Actions workflows and automated deployment

### Supported Project Types
- CLI Tools and Applications
- Libraries and APIs
- Web Services (Axum, Warp, Actix)
- Async Libraries
- Data Processing Pipelines
- Game Development
- Embedded Systems
- Custom Templates

## Quick Start

### Prerequisites

- **Rust** 1.70.0 or later
- **OpenAI API Key** (required for AI features)
- **Docker** (optional, for containerization)
- **Redis** (optional, for caching)

### Installation

#### From Crates.io
```bash
cargo install opencrates
```

#### From Source
```bash
git clone https://github.com/nikjois/opencrates.git
cd opencrates
cargo install --path .
```

#### Using Docker
```bash
docker pull nikjois/opencrates:latest
docker run -it --rm nikjois/opencrates:latest --help
```

### Configuration

1. **Set up your OpenAI API key**:
```bash
export OPENAI_API_KEY="your-api-key-here"
```

2. **Configure OpenCrates** (optional):
```bash
opencrates config
```

3. **Verify installation**:
```bash
opencrates --help
```

## Usage

### Interactive Mode
```bash
opencrates
```
This launches the interactive wizard that guides you through creating a new crate.

### Command Line Mode
```bash
# Create a CLI tool
opencrates create "A command-line tool for managing Docker containers"

# Create with specific options
opencrates create "Web API for user management" \
  --template web-api \
  --features "postgres,redis,metrics" \
  --model gpt-4 \
  --search-web

# Create interactively
opencrates create --interactive
```

### Web Search Integration
```bash
# Search for Rust patterns and examples
opencrates search "async Rust patterns with tokio"

# Search with custom limit
opencrates search "Rust web frameworks comparison" --limit 20
```

### Testing and Quality Assurance
```bash
# Run the built-in test suite
opencrates test

# Run specific test scenarios
opencrates test --scenario basic
opencrates test --all

# List available templates
opencrates templates
```

## Architecture

OpenCrates is built with a modular architecture:

```
┌─────────────────┐    ┌─────────────────┐    ┌─────────────────┐
│   CLI Interface │    │   Web Interface │    │   API Interface │
└─────────┬───────┘    └─────────┬───────┘    └─────────┬───────┘
          │                      │                      │
          └──────────────────────┼──────────────────────┘
                    ┌─────────────▼─────────────┐
                    │      Core Engine          │
                    │  ┌─────────────────────┐  │
                    │  │  Stage Orchestrator │  │
                    │  └─────────────────────┘  │
                    └─────────────┬─────────────┘
        ┌─────────────────────────┼─────────────────────────┐
        │                         │                         │
┌───────▼────────┐    ┌───────────▼──────────┐    ┌────────▼────────┐
│   Providers    │    │      Utilities       │    │    Templates    │
│                │    │                      │    │                 │
│ • OpenAI       │    │ • Config Management  │    │ • CLI Templates │
│ • Web Search   │    │ • Caching           │    │ • Web Templates │
│ • Templates    │    │ • Metrics           │    │ • Lib Templates │
│                │    │ • Health Checks     │    │ • Custom Types  │
└────────────────┘    │ • Error Handling    │    └─────────────────┘
                      │ • FastAPI Integration│
                      └─────────────────────┘
```

### Core Components

1. **Stage Orchestrator**: Manages the crate generation pipeline
2. **Providers**: External service integrations (OpenAI, web search)
3. **Templates**: Extensible template system for different project types
4. **Utilities**: Cross-cutting concerns (config, caching, metrics)

## Configuration

OpenCrates can be configured through:

1. **Environment Variables**:
```bash
export OPENAI_API_KEY="your-key"
export OPENCRATES_MODEL="gpt-4"
export OPENCRATES_CACHE_TTL="3600"
export REDIS_URL="redis://localhost:6379"
```

2. **Configuration File** (`~/.config/opencrates/config.toml`):
```toml
[openai]
api_key = "your-key"
default_model = "gpt-4"
max_tokens = 4000
temperature = 0.7

[cache]
enabled = true
ttl = 3600
redis_url = "redis://localhost:6379"

[web_search]
enabled = true
max_results = 10

[templates]
custom_path = "~/.config/opencrates/templates"

[metrics]
enabled = true
prometheus_port = 9090
```

3. **Interactive Configuration**:
```bash
opencrates config
```

## Docker Support

### Using Pre-built Images
```bash
# Run with environment variables
docker run -e OPENAI_API_KEY="your-key" nikjois/opencrates:latest create "my awesome crate"

# Run interactively
docker run -it -e OPENAI_API_KEY="your-key" nikjois/opencrates:latest
```

### Building from Source
```bash
# Build the image
docker build -t opencrates:local .

# Run with Docker Compose
docker-compose up -d
```

### Docker Compose Setup
```yaml
version: '3.8'
services:
  opencrates:
    build: .
    environment:
      - OPENAI_API_KEY=${OPENAI_API_KEY}
      - REDIS_URL=redis://redis:6379
    depends_on:
      - redis
    ports:
      - "8000:8000"
      - "9090:9090"
  
  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"
  
  prometheus:
    image: prom/prometheus:latest
    ports:
      - "9091:9090"
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
```

## Testing

### Running Tests
```bash
# Run all tests
cargo test

# Run specific test suites
cargo test --lib                    # Unit tests
cargo test --test integration       # Integration tests
cargo test --doc                    # Documentation tests

# Run benchmarks
cargo bench

# Run the comprehensive test suite
./test_all.sh
```

### Test Coverage
```bash
# Install tarpaulin
cargo install cargo-tarpaulin

# Generate coverage report
cargo tarpaulin --out Html
```

### Performance Testing
```bash
# Run benchmarks
cargo bench

# Profile with flamegraph
cargo install flamegraph
cargo flamegraph --bin opencrates -- create "test crate"
```

## Monitoring and Observability

### Metrics
OpenCrates exposes Prometheus metrics on port 9090:

- **Request metrics**: HTTP request duration, count, errors
- **Generation metrics**: Crate generation time, success rate
- **Cache metrics**: Hit rate, miss rate, eviction count
- **Health metrics**: Component health status

### Health Checks
Health check endpoint available at `/health`:

```bash
curl http://localhost:8000/health
```

### Distributed Tracing
OpenCrates supports distributed tracing with Jaeger:

```bash
# Start Jaeger
docker run -d --name jaeger \
  -p 16686:16686 \
  -p 14268:14268 \
  jaegertracing/all-in-one:latest

# Configure OpenCrates
export JAEGER_ENDPOINT="http://localhost:14268/api/traces"
```

## API Integration

### FastAPI Web Interface
OpenCrates includes a FastAPI web interface:

```bash
# Start the web server
opencrates serve --port 8000

# API endpoints
curl -X POST http://localhost:8000/api/v1/generate \
  -H "Content-Type: application/json" \
  -d '{"description": "CLI tool for file management", "template": "cli"}'
```

### OpenAI Agents SDK Integration
```rust
use opencrates::agents::CrateAssistant;

#[tokio::main]
async fn main() -> Result<()> {
    let assistant = CrateAssistant::new("your-api-key").await?;
    
    let result = assistant.generate_crate(
        "A web scraper for e-commerce sites",
        GenerationOptions::default()
    ).await?;
    
    println!("Generated crate: {}", result.name);
    Ok(())
}
```

## Development

### Building from Source
```bash
git clone https://github.com/nikjois/opencrates.git
cd opencrates

# Install dependencies
cargo build

# Run in development mode
cargo run -- --help

# Run with all features
cargo run --all-features -- create "test crate"
```

### Contributing
1. Fork the repository
2. Create a feature branch: `git checkout -b feature/amazing-feature`
3. Make your changes and add tests
4. Run the test suite: `./test_all.sh`
5. Commit your changes: `git commit -m 'Add amazing feature'`
6. Push to the branch: `git push origin feature/amazing-feature`
7. Open a Pull Request

### Development Tools
```bash
# Install development tools
cargo install cargo-watch cargo-audit cargo-deny

# Watch for changes
cargo watch -x check -x test

# Security audit
cargo audit

# License and dependency checking
cargo deny check
```

## Examples

### CLI Tool Example
```bash
opencrates create "A CLI tool for managing Kubernetes deployments" \
  --template cli \
  --features "clap,serde,tokio" \
  --model gpt-4
```

### Web API Example
```bash
opencrates create "REST API for a blog platform" \
  --template web-api \
  --features "axum,sqlx,redis" \
  --search-web
```

### Library Example
```bash
opencrates create "A high-performance JSON parser" \
  --template library \
  --features "serde,benchmarks" \
  --no-docs false
```

## Troubleshooting

### Common Issues

1. **OpenAI API Key Issues**:
```bash
# Verify your API key
echo $OPENAI_API_KEY

# Test API connectivity
curl -H "Authorization: Bearer $OPENAI_API_KEY" \
  https://api.openai.com/v1/models
```

2. **Build Issues**:
```bash
# Clean and rebuild
cargo clean
cargo build

# Update dependencies
cargo update
```

3. **Docker Issues**:
```bash
# Check Docker daemon
docker info

# Rebuild image
docker build --no-cache -t opencrates:local .
```

### Debug Mode
```bash
# Enable debug logging
RUST_LOG=debug opencrates create "test crate"

# Enable trace logging
RUST_LOG=trace opencrates create "test crate"
```

### Performance Issues
```bash
# Profile the application
cargo install flamegraph
cargo flamegraph --bin opencrates -- create "test crate"

# Check system resources
htop
```

## License

This project is licensed under either of

- Apache License, Version 2.0, ([LICENSE-APACHE]LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license ([LICENSE-MIT]LICENSE-MIT or http://opensource.org/licenses/MIT)

at your option.

## Contributing

Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for details.

## Support

- **Documentation**: [https://docs.rs/opencrates]https://docs.rs/opencrates
- **Issues**: [GitHub Issues]https://github.com/nikjois/opencrates/issues
- **Repository**: [GitHub Repository]https://github.com/nikjois/opencrates

## Author

**Nik Jois** - [nik@nikjois.com](mailto:nik@nikjois.com)

For questions, suggestions, or contributions, please open an issue on GitHub or contact the author directly.