smart_message 0.0.1

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.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/.envrc +3 -0
  3. data/.gitignore +8 -0
  4. data/.travis.yml +7 -0
  5. data/CHANGELOG.md +100 -0
  6. data/COMMITS.md +196 -0
  7. data/Gemfile +6 -0
  8. data/Gemfile.lock +71 -0
  9. data/README.md +303 -0
  10. data/Rakefile +10 -0
  11. data/bin/console +14 -0
  12. data/bin/setup +8 -0
  13. data/docs/README.md +52 -0
  14. data/docs/architecture.md +370 -0
  15. data/docs/dispatcher.md +593 -0
  16. data/docs/examples.md +808 -0
  17. data/docs/getting-started.md +235 -0
  18. data/docs/ideas_to_think_about.md +329 -0
  19. data/docs/serializers.md +575 -0
  20. data/docs/transports.md +501 -0
  21. data/docs/troubleshooting.md +582 -0
  22. data/examples/01_point_to_point_orders.rb +200 -0
  23. data/examples/02_publish_subscribe_events.rb +364 -0
  24. data/examples/03_many_to_many_chat.rb +608 -0
  25. data/examples/README.md +335 -0
  26. data/examples/tmux_chat/README.md +283 -0
  27. data/examples/tmux_chat/bot_agent.rb +272 -0
  28. data/examples/tmux_chat/human_agent.rb +197 -0
  29. data/examples/tmux_chat/room_monitor.rb +158 -0
  30. data/examples/tmux_chat/shared_chat_system.rb +295 -0
  31. data/examples/tmux_chat/start_chat_demo.sh +190 -0
  32. data/examples/tmux_chat/stop_chat_demo.sh +22 -0
  33. data/lib/simple_stats.rb +57 -0
  34. data/lib/smart_message/base.rb +284 -0
  35. data/lib/smart_message/dispatcher/.keep +0 -0
  36. data/lib/smart_message/dispatcher.rb +146 -0
  37. data/lib/smart_message/errors.rb +29 -0
  38. data/lib/smart_message/header.rb +20 -0
  39. data/lib/smart_message/logger/base.rb +8 -0
  40. data/lib/smart_message/logger.rb +7 -0
  41. data/lib/smart_message/serializer/base.rb +23 -0
  42. data/lib/smart_message/serializer/json.rb +22 -0
  43. data/lib/smart_message/serializer.rb +10 -0
  44. data/lib/smart_message/transport/base.rb +85 -0
  45. data/lib/smart_message/transport/memory_transport.rb +69 -0
  46. data/lib/smart_message/transport/registry.rb +59 -0
  47. data/lib/smart_message/transport/stdout_transport.rb +62 -0
  48. data/lib/smart_message/transport.rb +41 -0
  49. data/lib/smart_message/version.rb +7 -0
  50. data/lib/smart_message/wrapper.rb +43 -0
  51. data/lib/smart_message.rb +54 -0
  52. data/smart_message.gemspec +53 -0
  53. metadata +252 -0
@@ -0,0 +1,235 @@
1
+ # Getting Started with SmartMessage
2
+
3
+ This guide will help you get up and running with SmartMessage quickly.
4
+
5
+ ## Installation
6
+
7
+ Add SmartMessage to your Gemfile:
8
+
9
+ ```ruby
10
+ gem 'smart_message'
11
+ ```
12
+
13
+ Then run:
14
+
15
+ ```bash
16
+ bundle install
17
+ ```
18
+
19
+ Or install directly:
20
+
21
+ ```bash
22
+ gem install smart_message
23
+ ```
24
+
25
+ ## Requirements
26
+
27
+ - Ruby >= 3.0.0
28
+ - No additional system dependencies
29
+
30
+ ## Your First Message
31
+
32
+ Let's create a simple message class and see it in action:
33
+
34
+ ### 1. Define a Message Class
35
+
36
+ ```ruby
37
+ require 'smart_message'
38
+
39
+ class WelcomeMessage < SmartMessage::Base
40
+ # Define message properties
41
+ property :user_name
42
+ property :email
43
+ property :signup_date
44
+
45
+ # Configure the transport (where messages go)
46
+ config do
47
+ transport SmartMessage::Transport.create(:stdout, loopback: true)
48
+ serializer SmartMessage::Serializer::JSON.new
49
+ end
50
+
51
+ # Define how to process received messages
52
+ def self.process(message_header, message_payload)
53
+ # Decode the message
54
+ data = JSON.parse(message_payload)
55
+ welcome = new(data)
56
+
57
+ # Process the welcome message
58
+ puts "🎉 Welcome #{welcome.user_name}!"
59
+ puts "📧 Email: #{welcome.email}"
60
+ puts "📅 Signed up: #{welcome.signup_date}"
61
+ end
62
+ end
63
+ ```
64
+
65
+ ### 2. Subscribe to Messages
66
+
67
+ Before publishing, set up a subscription to receive messages:
68
+
69
+ ```ruby
70
+ # Subscribe to process incoming WelcomeMessage instances
71
+ WelcomeMessage.subscribe
72
+ ```
73
+
74
+ ### 3. Create and Publish a Message
75
+
76
+ ```ruby
77
+ # Create a new welcome message
78
+ welcome = WelcomeMessage.new(
79
+ user_name: "Alice Johnson",
80
+ email: "alice@example.com",
81
+ signup_date: Date.today.to_s
82
+ )
83
+
84
+ # Publish the message
85
+ welcome.publish
86
+ ```
87
+
88
+ ### 4. See It in Action
89
+
90
+ Run your script and you should see:
91
+
92
+ ```
93
+ ===================================================
94
+ == SmartMessage Published via STDOUT Transport
95
+ == Header: #<SmartMessage::Header:0x... @uuid="...", @message_class="WelcomeMessage", ...>
96
+ == Payload: {"user_name":"Alice Johnson","email":"alice@example.com","signup_date":"2025-08-17"}
97
+ ===================================================
98
+
99
+ 🎉 Welcome Alice Johnson!
100
+ 📧 Email: alice@example.com
101
+ 📅 Signed up: 2025-08-17
102
+ ```
103
+
104
+ ## Understanding What Happened
105
+
106
+ 1. **Message Definition**: We created a `WelcomeMessage` class with three properties
107
+ 2. **Configuration**: We configured it to use STDOUT transport with loopback enabled
108
+ 3. **Subscription**: We subscribed to process incoming messages of this type
109
+ 4. **Publishing**: We created an instance and published it
110
+ 5. **Processing**: Because loopback is enabled, the message was immediately routed back and processed
111
+
112
+ ## Key Concepts
113
+
114
+ ### Properties
115
+ Messages use Hashie::Dash properties for type-safe attributes:
116
+
117
+ ```ruby
118
+ class OrderMessage < SmartMessage::Base
119
+ property :order_id, required: true
120
+ property :amount, transform_with: ->(v) { BigDecimal(v.to_s) }
121
+ property :items, default: []
122
+ property :created_at, default: -> { Time.now }
123
+ end
124
+ ```
125
+
126
+ ### Headers
127
+ Every message automatically gets a header with metadata:
128
+
129
+ ```ruby
130
+ message = WelcomeMessage.new(user_name: "Bob")
131
+ puts message._sm_header.uuid # Unique identifier
132
+ puts message._sm_header.message_class # "WelcomeMessage"
133
+ puts message._sm_header.published_at # Timestamp when published
134
+ puts message._sm_header.publisher_pid # Process ID of publisher
135
+ ```
136
+
137
+ ### Transports
138
+ Transports handle where messages go. Built-in options include:
139
+
140
+ ```ruby
141
+ # STDOUT - for development/debugging
142
+ transport SmartMessage::Transport.create(:stdout, loopback: true)
143
+
144
+ # Memory - for testing
145
+ transport SmartMessage::Transport.create(:memory, auto_process: true)
146
+ ```
147
+
148
+ ### Serializers
149
+ Serializers handle message encoding/decoding:
150
+
151
+ ```ruby
152
+ # JSON serialization (built-in)
153
+ serializer SmartMessage::Serializer::JSON.new
154
+ ```
155
+
156
+ ## Next Steps
157
+
158
+ Now that you have the basics working, explore:
159
+
160
+ - [Architecture Overview](architecture.md) - Understand how SmartMessage works
161
+ - [Examples](examples.md) - See more practical use cases
162
+ - [Transports](transports.md) - Learn about different transport options
163
+ - [Custom Transports](custom-transports.md) - Build your own transport
164
+
165
+ ## Common Patterns
166
+
167
+ ### Simple Notification System
168
+
169
+ ```ruby
170
+ class NotificationMessage < SmartMessage::Base
171
+ property :recipient
172
+ property :subject
173
+ property :body
174
+ property :priority, default: 'normal'
175
+
176
+ config do
177
+ transport SmartMessage::Transport.create(:memory, auto_process: true)
178
+ serializer SmartMessage::Serializer::JSON.new
179
+ end
180
+
181
+ def self.process(message_header, message_payload)
182
+ data = JSON.parse(message_payload)
183
+ notification = new(data)
184
+
185
+ # Send email, SMS, push notification, etc.
186
+ send_notification(notification)
187
+ end
188
+
189
+ private
190
+
191
+ def self.send_notification(notification)
192
+ puts "Sending #{notification.priority} notification to #{notification.recipient}"
193
+ puts "Subject: #{notification.subject}"
194
+ end
195
+ end
196
+
197
+ # Subscribe and send
198
+ NotificationMessage.subscribe
199
+
200
+ NotificationMessage.new(
201
+ recipient: "user@example.com",
202
+ subject: "Welcome!",
203
+ body: "Thanks for signing up!",
204
+ priority: "high"
205
+ ).publish
206
+ ```
207
+
208
+ ### Event Logging
209
+
210
+ ```ruby
211
+ class EventMessage < SmartMessage::Base
212
+ property :event_type
213
+ property :user_id
214
+ property :data
215
+ property :timestamp, default: -> { Time.now.iso8601 }
216
+
217
+ config do
218
+ transport SmartMessage::Transport.create(:stdout, output: 'events.log')
219
+ serializer SmartMessage::Serializer::JSON.new
220
+ end
221
+ end
222
+
223
+ # Log events
224
+ EventMessage.new(
225
+ event_type: 'user_login',
226
+ user_id: 123,
227
+ data: { ip: '192.168.1.1', user_agent: 'Chrome' }
228
+ ).publish
229
+ ```
230
+
231
+ ## Need Help?
232
+
233
+ - Check the [Troubleshooting Guide](troubleshooting.md)
234
+ - Review the [API Reference](api-reference.md)
235
+ - Look at more [Examples](examples.md)
@@ -0,0 +1,329 @@
1
+ # Ideas to Think About
2
+
3
+ This document analyzes potential enhancements to SmartMessage based on a comparison between the current implementation and an idealized vision of intelligent messaging.
4
+
5
+ ## Current State vs. Vision
6
+
7
+ A description was provided that envisioned SmartMessage as an advanced, intelligent messaging system. While the current implementation is solid and well-designed, there are significant opportunities for enhancement to move toward that vision.
8
+
9
+ ### What Currently Matches:
10
+
11
+ ✅ **Message abstraction from transport mechanisms** - The gem does abstract messages from their delivery systems
12
+
13
+ ✅ **Pluggable architecture** - Supports different transports and serializers
14
+
15
+ ✅ **Multiple messaging patterns** - Can handle 1-to-1, 1-to-many scenarios through subscription patterns
16
+
17
+ ✅ **Error handling** - Basic error isolation in the dispatcher
18
+
19
+ ### Major Enhancement Opportunities:
20
+
21
+ The following features were described in the vision but are not currently implemented:
22
+
23
+ #### 1. Autonomous Self-Publishing
24
+ **Vision:** Messages autonomously publish themselves based on predefined triggers
25
+ **Current:** Messages require explicit `.publish` calls
26
+ **Potential Implementation:**
27
+ ```ruby
28
+ class SmartMessage::Base
29
+ # Add trigger-based publishing
30
+ def self.publish_on(event_type, condition = nil, &block)
31
+ @auto_publish_triggers ||= []
32
+ @auto_publish_triggers << {
33
+ event: event_type,
34
+ condition: condition || block,
35
+ target: self
36
+ }
37
+ end
38
+
39
+ # Monitor system events and auto-publish when triggered
40
+ def self.check_triggers(event_data)
41
+ @auto_publish_triggers&.each do |trigger|
42
+ if trigger[:condition].call(event_data)
43
+ new(event_data).publish
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ # Usage example:
50
+ class OrderStatusUpdate < SmartMessage::Base
51
+ property :order_id
52
+ property :status
53
+
54
+ # Automatically publish when order status changes
55
+ publish_on(:order_status_changed) do |event|
56
+ event[:status] == 'shipped'
57
+ end
58
+ end
59
+ ```
60
+
61
+ #### 2. Dynamic Content Transformation
62
+ **Vision:** On-the-fly format conversion for different subscribers
63
+ **Current:** One serializer per message class
64
+ **Potential Implementation:**
65
+ ```ruby
66
+ class SmartMessage::Dispatcher
67
+ # Route with format transformation
68
+ def route_with_transformation(message_header, message_payload)
69
+ @subscribers[message_header.message_class].each do |processor|
70
+ # Get subscriber's preferred format
71
+ preferred_format = get_subscriber_format(processor)
72
+
73
+ # Transform if needed
74
+ if preferred_format != message_header.format
75
+ transformed_payload = transform_format(
76
+ message_payload,
77
+ from: message_header.format,
78
+ to: preferred_format
79
+ )
80
+ route_to_processor(processor, message_header, transformed_payload)
81
+ else
82
+ route_to_processor(processor, message_header, message_payload)
83
+ end
84
+ end
85
+ end
86
+
87
+ private
88
+
89
+ def transform_format(payload, from:, to:)
90
+ # Convert between formats (JSON -> XML, XML -> MessagePack, etc.)
91
+ case [from, to]
92
+ when ['json', 'xml']
93
+ json_to_xml(payload)
94
+ when ['xml', 'json']
95
+ xml_to_json(payload)
96
+ # Add more transformations as needed
97
+ end
98
+ end
99
+ end
100
+ ```
101
+
102
+ #### 3. Contextual Awareness and Routing Intelligence
103
+ **Vision:** Intelligent routing based on context and subscriber relevance
104
+ **Current:** Simple class-name-based dispatch
105
+ **Potential Implementation:**
106
+ ```ruby
107
+ class SmartMessage::IntelligentDispatcher < SmartMessage::Dispatcher
108
+ def initialize
109
+ super
110
+ @subscriber_profiles = {}
111
+ @routing_intelligence = RoutingIntelligence.new
112
+ end
113
+
114
+ def subscribe_with_profile(message_class, processor, profile: {})
115
+ super(message_class, processor)
116
+ @subscriber_profiles[processor] = profile
117
+ end
118
+
119
+ def route(message_header, message_payload)
120
+ # Analyze message content
121
+ message_context = analyze_message_context(message_payload)
122
+
123
+ # Find relevant subscribers based on context
124
+ relevant_subscribers = find_relevant_subscribers(
125
+ message_header.message_class,
126
+ message_context
127
+ )
128
+
129
+ # Route only to relevant subscribers
130
+ relevant_subscribers.each do |processor|
131
+ route_to_processor(processor, message_header, message_payload)
132
+ end
133
+ end
134
+
135
+ private
136
+
137
+ def find_relevant_subscribers(message_class, context)
138
+ candidates = @subscribers[message_class] || []
139
+
140
+ candidates.select do |processor|
141
+ profile = @subscriber_profiles[processor]
142
+ @routing_intelligence.is_relevant?(context, profile)
143
+ end
144
+ end
145
+ end
146
+
147
+ # Usage:
148
+ dispatcher.subscribe_with_profile(
149
+ "OrderMessage",
150
+ "FulfillmentService.process",
151
+ profile: {
152
+ interests: ['physical_goods'],
153
+ regions: ['US', 'CA'],
154
+ order_value_min: 100
155
+ }
156
+ )
157
+ ```
158
+
159
+ #### 4. Feedback Mechanisms
160
+ **Vision:** Collect feedback from subscribers for continuous improvement
161
+ **Current:** No feedback system
162
+ **Potential Implementation:**
163
+ ```ruby
164
+ class SmartMessage::Base
165
+ def self.process_with_feedback(message_header, message_payload)
166
+ start_time = Time.now
167
+
168
+ begin
169
+ result = process_without_feedback(message_header, message_payload)
170
+
171
+ # Collect success feedback
172
+ SmartMessage::FeedbackCollector.record_success(
173
+ processor: "#{self}.process",
174
+ message_class: message_header.message_class,
175
+ processing_time: Time.now - start_time,
176
+ result_quality: evaluate_result_quality(result)
177
+ )
178
+
179
+ result
180
+ rescue => e
181
+ # Collect failure feedback
182
+ SmartMessage::FeedbackCollector.record_failure(
183
+ processor: "#{self}.process",
184
+ message_class: message_header.message_class,
185
+ error: e,
186
+ processing_time: Time.now - start_time
187
+ )
188
+ raise
189
+ end
190
+ end
191
+
192
+ alias_method :process_without_feedback, :process
193
+ alias_method :process, :process_with_feedback
194
+ end
195
+
196
+ module SmartMessage::FeedbackCollector
197
+ def self.record_success(processor:, message_class:, processing_time:, result_quality:)
198
+ # Store feedback for analysis
199
+ feedback_store.record({
200
+ type: 'success',
201
+ processor: processor,
202
+ message_class: message_class,
203
+ processing_time: processing_time,
204
+ result_quality: result_quality,
205
+ timestamp: Time.now
206
+ })
207
+ end
208
+
209
+ def self.analyze_processor_performance(processor)
210
+ # Analyze collected feedback to provide insights
211
+ feedback_store.analyze(processor)
212
+ end
213
+ end
214
+ ```
215
+
216
+ #### 5. Security Features
217
+ **Vision:** Built-in encryption and authentication
218
+ **Current:** No security features
219
+ **Potential Implementation:**
220
+ ```ruby
221
+ class SmartMessage::SecureTransport < SmartMessage::Transport::Base
222
+ def initialize(options = {})
223
+ super
224
+ @encryption_key = options[:encryption_key]
225
+ @auth_provider = options[:auth_provider]
226
+ end
227
+
228
+ def publish(message_header, message_payload)
229
+ # Authenticate sender
230
+ unless @auth_provider.authenticate(message_header.publisher_pid)
231
+ raise SmartMessage::Errors::AuthenticationFailed
232
+ end
233
+
234
+ # Encrypt payload
235
+ encrypted_payload = encrypt_payload(message_payload)
236
+
237
+ # Add security metadata to header
238
+ secure_header = message_header.dup
239
+ secure_header.encrypted = true
240
+ secure_header.encryption_algorithm = 'AES-256-GCM'
241
+
242
+ super(secure_header, encrypted_payload)
243
+ end
244
+
245
+ protected
246
+
247
+ def receive(message_header, message_payload)
248
+ # Decrypt if needed
249
+ if message_header.encrypted
250
+ decrypted_payload = decrypt_payload(message_payload)
251
+ super(message_header, decrypted_payload)
252
+ else
253
+ super
254
+ end
255
+ end
256
+
257
+ private
258
+
259
+ def encrypt_payload(payload)
260
+ # Implement encryption logic
261
+ end
262
+
263
+ def decrypt_payload(encrypted_payload)
264
+ # Implement decryption logic
265
+ end
266
+ end
267
+ ```
268
+
269
+ ## Implementation Roadmap
270
+
271
+ ### Phase 1: Foundation Enhancements
272
+ 1. **Enhanced Statistics System** - More detailed metrics collection
273
+ 2. **Message Tracing** - Correlation IDs and message flow tracking
274
+ 3. **Improved Error Handling** - Retry mechanisms and dead letter queues
275
+ 4. **Configuration Management** - More sophisticated configuration options
276
+
277
+ ### Phase 2: Intelligence Features
278
+ 1. **Content Analysis** - Message content classification and tagging
279
+ 2. **Routing Intelligence** - Context-aware subscriber matching
280
+ 3. **Performance Optimization** - Adaptive processing based on load
281
+ 4. **Feedback Collection** - Basic subscriber feedback mechanisms
282
+
283
+ ### Phase 3: Advanced Features
284
+ 1. **Dynamic Transformation** - Format conversion between subscribers
285
+ 2. **Autonomous Publishing** - Event-driven message creation
286
+ 3. **Security Layer** - Encryption and authentication
287
+ 4. **Machine Learning Integration** - Predictive routing and optimization
288
+
289
+ ### Phase 4: Enterprise Features
290
+ 1. **Distributed Processing** - Multi-node message handling
291
+ 2. **Advanced Analytics** - Message flow analysis and optimization
292
+ 3. **Integration APIs** - Easy integration with existing systems
293
+ 4. **Management Console** - Web-based monitoring and configuration
294
+
295
+ ## Technical Considerations
296
+
297
+ ### Architecture Changes Needed
298
+ 1. **Plugin System Enhancement** - More sophisticated plugin architecture
299
+ 2. **Event System** - System-wide event broadcasting for triggers
300
+ 3. **Metadata Framework** - Rich message and subscriber metadata
301
+ 4. **Processing Pipeline** - Configurable message processing stages
302
+
303
+ ### Performance Implications
304
+ 1. **Intelligence Overhead** - Balance between smart features and performance
305
+ 2. **Memory Usage** - Feedback collection and context storage
306
+ 3. **Processing Complexity** - Multiple transformation and routing steps
307
+ 4. **Scalability** - Ensure enhancements don't impact horizontal scaling
308
+
309
+ ### Backward Compatibility
310
+ 1. **Gradual Migration** - All enhancements should be optional
311
+ 2. **API Stability** - Maintain existing API while adding new features
312
+ 3. **Configuration Migration** - Smooth upgrade path for existing installations
313
+ 4. **Testing Strategy** - Comprehensive testing for both old and new features
314
+
315
+ ## Current Strengths to Preserve
316
+
317
+ The existing SmartMessage implementation has several strengths that should be maintained:
318
+
319
+ 1. **Simplicity** - Easy to understand and use
320
+ 2. **Solid Architecture** - Well-designed plugin system and separation of concerns
321
+ 3. **Thread Safety** - Proper concurrent processing
322
+ 4. **Flexibility** - Support for custom transports and serializers
323
+ 5. **Testing** - Good test coverage and patterns
324
+
325
+ ## Conclusion
326
+
327
+ While the current SmartMessage implementation is solid and functional, there's significant potential to evolve it toward a more intelligent messaging system. The key is to implement these enhancements gradually while preserving the simplicity and reliability that make the current system valuable.
328
+
329
+ The vision of autonomous, context-aware messaging is achievable, but it requires careful planning to ensure that added intelligence doesn't compromise the system's core strengths of simplicity and reliability.