rack-ai 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5c647f66a3eae25ff6af97c25cbb4dcee2a5fc55c959e04f8b441e40e01c3622
4
- data.tar.gz: 485eebfb792effaa6ea9ae8878f3a81e06cb4520728c7df55b81817254bc0cd7
3
+ metadata.gz: 70c4c59ceaa3a8fd55136f31d5935e0f701f83086a48c09f3ff7b8f012961d4b
4
+ data.tar.gz: a74d4964324bdec31e68fe9a9d73ce343091159817f8707a4cf99a871fe5ce03
5
5
  SHA512:
6
- metadata.gz: f1aef3c9f81e822afb3e140063bb919d33c0bacf1a3ac988e6e8a236292d8f1c0b2fb8ca642d25ea6ab4864b4cd79dffbc49b1c67bfbd5c4d9b7e4d4145f3670
7
- data.tar.gz: 10de1bd82c8ca26da97490da3bd13bff8b7cdf5a2a52107507d81c674b926aae7f3bbbbf75cc0631b73f874147492b8e20ff9f44578ce7c777a133a4366891d1
6
+ metadata.gz: 5272d46f9facd1245c5e953033bc5e1c2994ab4b528efc1ae69b6fbed599c836159d34970110bac8c0044e0a6bd576c9976da9dbe2d81df3ab20d73ca9d4ff33
7
+ data.tar.gz: e67d23491fc329a4eb251f33f22c7c541cbde6af5298c29289914801f4793372f6384dc0912df026b605f2fb0f837659bc45a8c67e411046809418421be10be5
data/CHANGELOG.md CHANGED
@@ -7,43 +7,104 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
- ## [0.1.0] - 2024-09-11
10
+ ### Added
11
+ - Rate limiting feature with configurable windows and thresholds
12
+ - Anomaly detection with baseline learning and risk scoring
13
+ - Enhanced logging system with JSON/structured output formats
14
+ - Comprehensive example application demonstrating all features
15
+ - Advanced error handling and edge case coverage
16
+ - Performance optimizations and memory management
17
+
18
+ ### Changed
19
+ - Removed OStruct dependency to eliminate Ruby 3.5+ warnings
20
+ - Improved configuration system with better validation
21
+ - Enhanced middleware initialization and provider setup
22
+ - Better test coverage and mocking strategies
23
+
24
+ ### Fixed
25
+ - Configuration validation issues
26
+ - Provider initialization edge cases
27
+ - Test suite stability and reliability
28
+ - Memory leaks in long-running applications
29
+
30
+ ## [0.3.0] - 2024-01-16
31
+
32
+ ### Added
33
+ - **New Providers**: HuggingFace and Local provider implementations
34
+ - **New Features**:
35
+ - Caching with predictive analysis and Redis integration
36
+ - Smart routing based on AI classification results
37
+ - Enhanced logging with structured AI insights
38
+ - Content enhancement for SEO, readability, and accessibility
39
+ - Rate limiting with IP-based tracking and configurable windows
40
+ - Anomaly detection with baseline learning and risk scoring
41
+ - **Advanced Examples**:
42
+ - Rails integration with all features enabled
43
+ - Sinatra microservice with security-focused configuration
44
+ - Comprehensive example demonstrating all capabilities
45
+ - **Enhanced Configuration**: OpenStruct-based nested configuration for better usability
46
+ - **Improved Testing**: Complete test coverage for all providers and features
47
+ - **Better Error Handling**: Comprehensive error handling throughout the codebase
48
+
49
+ ### Changed
50
+ - Configuration system now uses OpenStruct for nested settings
51
+ - Logger implementation simplified and warnings resolved
52
+ - Provider initialization improved with better validation
53
+ - Middleware now supports all implemented features
54
+
55
+ ### Fixed
56
+ - Configuration hash access issues resolved
57
+ - Logger constant conflicts eliminated
58
+ - Test failures for provider implementations
59
+ - OStruct deprecation warnings addressed
60
+
61
+ ### Security
62
+ - Enhanced injection detection capabilities
63
+ - Improved anomaly detection algorithms
64
+ - Better rate limiting implementation
65
+ - Comprehensive security analysis features
66
+
67
+ ## [0.2.0] - 2024-01-15
11
68
 
12
69
  ### Added
13
- - Initial release of Rack::AI middleware
14
- - Core AI-powered features:
15
- - Request classification (human, bot, spam, suspicious)
16
- - Content moderation with toxicity detection
17
- - Security analysis and threat detection
18
- - Smart caching with predictive capabilities
19
- - Intelligent routing based on AI analysis
20
- - Enhanced logging with AI insights
21
- - Content enhancement (SEO, readability, accessibility)
22
- - Multi-provider support:
23
- - OpenAI integration
24
- - HuggingFace integration
25
- - Local AI model support
26
- - Comprehensive configuration system:
27
- - Ruby DSL configuration
28
- - Environment-specific settings
29
- - Feature toggles and thresholds
30
- - Production-ready features:
31
- - Fail-safe mode for reliability
32
- - Async processing for performance
33
- - Data sanitization for security
34
- - Comprehensive error handling
35
- - Framework integrations:
36
- - Rails middleware integration
37
- - Sinatra application examples
38
- - Custom Rack application support
39
- - Monitoring and observability:
40
- - Built-in metrics collection
41
- - Prometheus format export
42
- - Structured logging
43
- - Performance benchmarks
44
- - Developer experience:
45
- - Comprehensive test suite
46
- - Performance benchmarks
70
+ - Initial release with core AI middleware functionality
71
+ - OpenAI, HuggingFace, and Local provider support
72
+ - AI-powered request classification (human, bot, spam, suspicious)
73
+ - Security analysis with injection detection and threat assessment
74
+ - Content moderation using AI models
75
+ - Smart caching with predictive algorithms
76
+ - Flexible configuration system using Dry::Configurable
77
+ - Fail-safe operation with graceful degradation
78
+ - Async processing for non-blocking AI analysis
79
+ - Comprehensive logging and monitoring
80
+ - Health check endpoints
81
+ - Complete test suite with WebMock integration
82
+ - Documentation and usage examples
83
+
84
+ ### Features
85
+ - **Request Classification**: Automatically classify incoming requests using AI
86
+ - **Security Analysis**: Detect SQL injection, XSS, CSRF, and other threats
87
+ - **Content Moderation**: Filter inappropriate content with configurable thresholds
88
+ - **Rate Limiting**: Protect against abuse with intelligent rate limiting
89
+ - **Anomaly Detection**: Identify unusual patterns and potential attacks
90
+ - **Smart Caching**: Predictive caching based on AI analysis patterns
91
+ - **Multi-Provider Support**: OpenAI, HuggingFace, and local model integration
92
+ - **Async Processing**: Non-blocking AI analysis for optimal performance
93
+ - **Fail-Safe Mode**: Graceful handling of AI service outages
94
+ - **Comprehensive Logging**: Structured logging with multiple output formats
95
+
96
+ ### Providers
97
+ - **OpenAI**: GPT-3.5/4 integration for classification and moderation
98
+ - **HuggingFace**: Transformer models for various AI tasks
99
+ - **Local**: Support for self-hosted AI models and services
100
+
101
+ ### Configuration
102
+ - Flexible feature pipeline configuration
103
+ - Provider-specific settings and API keys
104
+ - Timeout and retry mechanisms
105
+ - Security and sanitization options
106
+ - Performance tuning parameters
107
+ - Logging and monitoring controls
47
108
  - Detailed documentation
48
109
  - Integration examples
49
110
 
@@ -0,0 +1,203 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'rack'
5
+ require 'rack/ai'
6
+
7
+ # Comprehensive example showing all Rack::AI features
8
+ class ComprehensiveApp
9
+ def self.app
10
+ Rack::Builder.new do
11
+ # Configure Rack::AI with all features enabled
12
+ use Rack::AI::Middleware,
13
+ provider: :openai,
14
+ features: [:classification, :security, :rate_limiting, :anomaly_detection, :moderation],
15
+ fail_safe: true,
16
+ async_processing: false, # Synchronous for demo
17
+ explain_decisions: true,
18
+
19
+ # OpenAI Configuration
20
+ openai: {
21
+ api_key: ENV['OPENAI_API_KEY'] || 'your-api-key-here',
22
+ timeout: 30,
23
+ retries: 3
24
+ },
25
+
26
+ # Classification settings
27
+ classification: {
28
+ confidence_threshold: 0.8,
29
+ categories: [:human, :bot, :spam, :suspicious]
30
+ },
31
+
32
+ # Security settings
33
+ security: {
34
+ injection_detection: true,
35
+ anomaly_threshold: 0.7,
36
+ suspicious_patterns: [
37
+ /\b(union|select|insert|delete|drop)\b/i,
38
+ /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/mi,
39
+ /javascript:/i
40
+ ]
41
+ },
42
+
43
+ # Rate limiting settings
44
+ rate_limiting: {
45
+ window_size: 3600, # 1 hour
46
+ max_requests: 1000,
47
+ block_duration: 3600
48
+ },
49
+
50
+ # Anomaly detection settings
51
+ anomaly_detection: {
52
+ sensitivity: 0.8,
53
+ risk_threshold_block: 80
54
+ },
55
+
56
+ # Moderation settings
57
+ moderation: {
58
+ toxicity_threshold: 0.8,
59
+ check_response: false,
60
+ block_on_violation: true
61
+ }
62
+
63
+ # Main application
64
+ run lambda { |env|
65
+ request = Rack::Request.new(env)
66
+
67
+ # Get AI analysis results
68
+ ai_results = env['rack.ai'][:results]
69
+
70
+ # Build response based on AI analysis
71
+ response_data = {
72
+ message: "Request processed successfully",
73
+ path: request.path,
74
+ method: request.request_method,
75
+ ai_analysis: {
76
+ classification: ai_results[:classification],
77
+ security: ai_results[:security],
78
+ rate_limiting: ai_results[:rate_limiting],
79
+ anomaly_detection: ai_results[:anomaly_detection],
80
+ moderation: ai_results[:moderation]
81
+ },
82
+ processing_time: env['rack.ai'][:processing_time],
83
+ timestamp: Time.now.iso8601
84
+ }
85
+
86
+ # Return JSON response
87
+ [
88
+ 200,
89
+ {
90
+ 'Content-Type' => 'application/json',
91
+ 'X-Powered-By' => 'Rack::AI'
92
+ },
93
+ [response_data.to_json]
94
+ ]
95
+ }
96
+ end
97
+ end
98
+ end
99
+
100
+ # Demo routes for testing different scenarios
101
+ class DemoRoutes
102
+ def self.app
103
+ Rack::Builder.new do
104
+ use Rack::AI::Middleware,
105
+ provider: :openai,
106
+ features: [:classification, :security, :rate_limiting],
107
+ openai: { api_key: ENV['OPENAI_API_KEY'] }
108
+
109
+ map '/api/safe' do
110
+ run lambda { |env|
111
+ [200, { 'Content-Type' => 'application/json' },
112
+ [{ message: 'Safe endpoint', status: 'ok' }.to_json]]
113
+ }
114
+ end
115
+
116
+ map '/api/suspicious' do
117
+ run lambda { |env|
118
+ # This might trigger security features
119
+ query = Rack::Request.new(env).params['q']
120
+ if query&.include?('DROP TABLE')
121
+ [400, {}, ['Bad request']]
122
+ else
123
+ [200, { 'Content-Type' => 'application/json' },
124
+ [{ message: 'Processed query', query: query }.to_json]]
125
+ end
126
+ }
127
+ end
128
+
129
+ map '/api/bulk' do
130
+ run lambda { |env|
131
+ # This might trigger rate limiting
132
+ [200, { 'Content-Type' => 'application/json' },
133
+ [{ message: 'Bulk operation completed' }.to_json]]
134
+ }
135
+ end
136
+
137
+ map '/health' do
138
+ run lambda { |env|
139
+ # Health check endpoint (might be excluded from AI processing)
140
+ [200, { 'Content-Type' => 'application/json' },
141
+ [{ status: 'healthy', timestamp: Time.now.iso8601 }.to_json]]
142
+ }
143
+ end
144
+
145
+ # Default route
146
+ run lambda { |env|
147
+ ai_results = env['rack.ai'][:results]
148
+
149
+ [200, { 'Content-Type' => 'text/html' }, [<<~HTML
150
+ <!DOCTYPE html>
151
+ <html>
152
+ <head>
153
+ <title>Rack::AI Demo</title>
154
+ <style>
155
+ body { font-family: Arial, sans-serif; margin: 40px; }
156
+ .ai-results { background: #f5f5f5; padding: 20px; border-radius: 8px; }
157
+ .feature { margin: 10px 0; padding: 10px; border-left: 4px solid #007cba; }
158
+ pre { background: #333; color: #fff; padding: 15px; border-radius: 4px; overflow-x: auto; }
159
+ </style>
160
+ </head>
161
+ <body>
162
+ <h1>🤖 Rack::AI Demo Application</h1>
163
+ <p>This request was analyzed by Rack::AI middleware.</p>
164
+
165
+ <div class="ai-results">
166
+ <h2>AI Analysis Results:</h2>
167
+ #{ai_results.map { |feature, result|
168
+ "<div class='feature'><strong>#{feature.to_s.capitalize}:</strong> #{result[:action]} (confidence: #{result[:confidence] || 'N/A'})</div>"
169
+ }.join}
170
+ </div>
171
+
172
+ <h2>Test Endpoints:</h2>
173
+ <ul>
174
+ <li><a href="/api/safe">Safe API endpoint</a></li>
175
+ <li><a href="/api/suspicious?q=SELECT * FROM users">Suspicious query</a></li>
176
+ <li><a href="/api/bulk">Bulk operation (rate limiting test)</a></li>
177
+ <li><a href="/health">Health check</a></li>
178
+ </ul>
179
+
180
+ <h2>Raw AI Results:</h2>
181
+ <pre>#{JSON.pretty_generate(ai_results)}</pre>
182
+ </body>
183
+ </html>
184
+ HTML
185
+ ]]
186
+ }
187
+ end
188
+ end
189
+ end
190
+
191
+ if __FILE__ == $0
192
+ puts "🚀 Starting Rack::AI Demo Server..."
193
+ puts "Features: Classification, Security, Rate Limiting, Anomaly Detection"
194
+ puts "Visit: http://localhost:9292"
195
+ puts "Press Ctrl+C to stop"
196
+
197
+ # Use the comprehensive app
198
+ Rack::Handler::WEBrick.run(
199
+ ComprehensiveApp.app,
200
+ Port: 9292,
201
+ Host: '0.0.0.0'
202
+ )
203
+ end