smart_prompt 0.5.0 → 0.5.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.
@@ -0,0 +1,533 @@
1
+ # History Management Examples
2
+
3
+ This directory contains comprehensive examples demonstrating the new intelligent history management features in SmartPrompt.
4
+
5
+ ## Overview
6
+
7
+ The history management system provides:
8
+
9
+ - **Session Isolation**: Each conversation has its own isolated history
10
+ - **Intelligent Context Management**: Multiple strategies for selecting relevant messages
11
+ - **Automatic Compression**: Summarize old messages to save tokens
12
+ - **Persistence**: Save and restore conversations from disk
13
+ - **Monitoring**: Track usage statistics and metrics
14
+ - **Backward Compatibility**: Works seamlessly with existing code
15
+
16
+ ## Quick Start
17
+
18
+ ### Running the Examples
19
+
20
+ ```bash
21
+ # Run all examples
22
+ ruby examples/history_management_examples.rb
23
+
24
+ # Or run specific sections by modifying the file
25
+ ```
26
+
27
+ ### Basic Usage
28
+
29
+ ```ruby
30
+ require 'smart_prompt'
31
+
32
+ # Simple chat with automatic session management
33
+ engine.call_worker(:basic_chat, {
34
+ message: "Hello! How are you?"
35
+ })
36
+
37
+ # Chat with explicit session ID
38
+ engine.call_worker(:explicit_session_chat, {
39
+ session_id: "user_123",
40
+ message: "Hello! How are you?"
41
+ })
42
+ ```
43
+
44
+ ## Examples Included
45
+
46
+ ### 1. Explicit Session Management
47
+
48
+ Demonstrates how to create and manage isolated conversation sessions.
49
+
50
+ **Key Features:**
51
+ - Unique session IDs for each conversation
52
+ - Session isolation (no cross-contamination)
53
+ - Multi-user support
54
+
55
+ **Requirements Validated:** 1.1, 1.2, 1.3, 1.4, 5.2
56
+
57
+ **Example:**
58
+ ```ruby
59
+ # User 1's conversation
60
+ SmartPrompt.run_worker(:explicit_session_chat, {
61
+ session_id: "user_alice",
62
+ message: "My name is Alice."
63
+ })
64
+
65
+ # User 2's conversation (completely isolated)
66
+ SmartPrompt.run_worker(:explicit_session_chat, {
67
+ session_id: "user_bob",
68
+ message: "My name is Bob."
69
+ })
70
+
71
+ # Continue User 1's conversation
72
+ SmartPrompt.run_worker(:explicit_session_chat, {
73
+ session_id: "user_alice",
74
+ message: "What's my name?" # Will remember "Alice"
75
+ })
76
+ ```
77
+
78
+ ### 2. Different Context Strategies
79
+
80
+ Demonstrates the four available context strategies for managing conversation history.
81
+
82
+ #### 2.1 Sliding Window Strategy
83
+
84
+ Keeps the most recent N messages. Best for real-time chat and customer support.
85
+
86
+ **Requirements Validated:** 8.2
87
+
88
+ **Example:**
89
+ ```ruby
90
+ SmartPrompt.run_worker(:sliding_window_chat, {
91
+ session_id: "support_001",
92
+ message: "I need help with my account."
93
+ })
94
+ ```
95
+
96
+ **Configuration:**
97
+ - `max_messages: 20` - Keep last 20 messages
98
+ - `max_tokens: 2000` - Maximum 2000 tokens
99
+ - `preserve_system_messages: true` - Always keep system messages
100
+
101
+ #### 2.2 Relevance-Based Strategy
102
+
103
+ Selects semantically relevant messages based on importance scoring. Best for Q&A systems and knowledge bases.
104
+
105
+ **Requirements Validated:** 8.1, 8.3
106
+
107
+ **Example:**
108
+ ```ruby
109
+ SmartPrompt.run_worker(:relevance_chat, {
110
+ session_id: "qa_001",
111
+ message: "Tell me about Ruby programming."
112
+ })
113
+
114
+ # Later in the conversation...
115
+ SmartPrompt.run_worker(:relevance_chat, {
116
+ session_id: "qa_001",
117
+ message: "What were the Ruby features we discussed?"
118
+ # Will include relevant earlier messages even if not recent
119
+ })
120
+ ```
121
+
122
+ **Configuration:**
123
+ - `max_messages: 100` - Consider up to 100 messages
124
+ - `max_tokens: 4000` - Maximum 4000 tokens
125
+ - Importance scoring based on recency and relevance
126
+
127
+ #### 2.3 Summary-Based Strategy
128
+
129
+ Automatically compresses old messages into summaries. Best for long conversations and extended dialogues.
130
+
131
+ **Requirements Validated:** 9.1, 9.2, 9.3
132
+
133
+ **Example:**
134
+ ```ruby
135
+ SmartPrompt.run_worker(:summary_chat, {
136
+ session_id: "long_conv_001",
137
+ message: "Let's discuss the history of computing."
138
+ })
139
+
140
+ # After many messages, old ones are automatically summarized
141
+ ```
142
+
143
+ **Configuration:**
144
+ - `max_messages: 200` - Can handle up to 200 messages
145
+ - `max_tokens: 8000` - Maximum 8000 tokens
146
+ - Automatic summarization when threshold is reached
147
+
148
+ #### 2.4 Hybrid Strategy
149
+
150
+ Adaptively combines multiple strategies based on conversation characteristics. Best for general-purpose applications.
151
+
152
+ **Requirements Validated:** 6.1
153
+
154
+ **Example:**
155
+ ```ruby
156
+ SmartPrompt.run_worker(:hybrid_chat, {
157
+ session_id: "general_001",
158
+ message: "Let's talk about various topics."
159
+ })
160
+ ```
161
+
162
+ **Configuration:**
163
+ - `max_messages: 150` - Balanced message limit
164
+ - `max_tokens: 6000` - Balanced token limit
165
+ - Automatically switches between strategies based on conversation state
166
+
167
+ ### 3. Session Operations
168
+
169
+ Demonstrates all available session management operations.
170
+
171
+ **Requirements Validated:** 4.1, 4.2, 4.3, 4.4, 4.5
172
+
173
+ #### Get Statistics
174
+
175
+ ```ruby
176
+ stats = SmartPrompt.run_worker(:session_operations, {
177
+ session_id: "user_123",
178
+ operation: :stats
179
+ })
180
+
181
+ # Returns:
182
+ # {
183
+ # session_id: "user_123",
184
+ # message_count: 15,
185
+ # total_tokens: 1234,
186
+ # created_at: <timestamp>,
187
+ # updated_at: <timestamp>,
188
+ # config: {...}
189
+ # }
190
+ ```
191
+
192
+ #### Search Messages
193
+
194
+ ```ruby
195
+ results = SmartPrompt.run_worker(:session_operations, {
196
+ session_id: "user_123",
197
+ operation: :search,
198
+ query: "Ruby programming"
199
+ })
200
+
201
+ # Returns: "Found 3 messages matching 'Ruby programming'"
202
+ ```
203
+
204
+ #### Export Session
205
+
206
+ ```ruby
207
+ data = SmartPrompt.run_worker(:session_operations, {
208
+ session_id: "user_123",
209
+ operation: :export
210
+ })
211
+
212
+ # Returns JSON with all session data
213
+ ```
214
+
215
+ #### Clear Session
216
+
217
+ ```ruby
218
+ SmartPrompt.run_worker(:session_operations, {
219
+ session_id: "user_123",
220
+ operation: :clear
221
+ })
222
+
223
+ # Removes all messages except system messages
224
+ ```
225
+
226
+ #### Delete Session
227
+
228
+ ```ruby
229
+ SmartPrompt.run_worker(:session_operations, {
230
+ session_id: "user_123",
231
+ operation: :delete
232
+ })
233
+
234
+ # Completely removes session from memory and disk
235
+ ```
236
+
237
+ ### 4. Backward Compatibility
238
+
239
+ Demonstrates that existing code continues to work without modifications.
240
+
241
+ **Requirements Validated:** 5.1, 5.2, 5.3, 5.4
242
+
243
+ **Example:**
244
+ ```ruby
245
+ # Old API - still works!
246
+ SmartPrompt.run_worker(:legacy_chat, {
247
+ message: "Hello!"
248
+ })
249
+
250
+ # Automatically creates a default session
251
+ # Benefits from new features without code changes
252
+ ```
253
+
254
+ ### 5. Advanced Use Cases
255
+
256
+ #### Multi-User Chat Application
257
+
258
+ ```ruby
259
+ # User 1
260
+ SmartPrompt.run_worker(:multi_user_chat, {
261
+ user_id: "001",
262
+ user_name: "Alice",
263
+ message: "Hi! I'm working on a Ruby project."
264
+ })
265
+
266
+ # User 2 (completely isolated)
267
+ SmartPrompt.run_worker(:multi_user_chat, {
268
+ user_id: "002",
269
+ user_name: "Bob",
270
+ message: "Hello! I need help with Python."
271
+ })
272
+ ```
273
+
274
+ #### Streaming with History
275
+
276
+ ```ruby
277
+ SmartPrompt.run_worker(:streaming_history_chat, {
278
+ session_id: "stream_001",
279
+ message: "Tell me a long story."
280
+ })
281
+
282
+ # Streams response while maintaining history
283
+ ```
284
+
285
+ #### Custom Configuration
286
+
287
+ ```ruby
288
+ SmartPrompt.run_worker(:custom_config_chat, {
289
+ session_id: "code_review_001",
290
+ message: "Please review this code: def hello; puts 'world'; end"
291
+ })
292
+
293
+ # Uses fine-tuned configuration for code review
294
+ ```
295
+
296
+ #### Persistent Sessions
297
+
298
+ ```ruby
299
+ SmartPrompt.run_worker(:persistent_chat, {
300
+ session_id: "persistent_001",
301
+ message: "This conversation will be saved to disk."
302
+ })
303
+
304
+ # Session is automatically saved and can be restored later
305
+ ```
306
+
307
+ ### 6. Monitoring and Debugging
308
+
309
+ #### System-Wide Statistics
310
+
311
+ ```ruby
312
+ stats = SmartPrompt.run_worker(:system_stats, {})
313
+
314
+ # Returns:
315
+ # {
316
+ # active_sessions: 5,
317
+ # total_messages: 123,
318
+ # total_tokens: 12345,
319
+ # cache_hit_rate: 0.85,
320
+ # messages_added: 150,
321
+ # context_retrievals: 75
322
+ # }
323
+ ```
324
+
325
+ #### Export Prometheus Metrics
326
+
327
+ ```ruby
328
+ metrics = SmartPrompt.run_worker(:export_metrics, {})
329
+
330
+ # Returns Prometheus-formatted metrics:
331
+ # smart_prompt_active_sessions 5
332
+ # smart_prompt_total_messages 123
333
+ # smart_prompt_cache_hit_rate 0.85
334
+ # ...
335
+ ```
336
+
337
+ ## Configuration Options
338
+
339
+ ### Session Configuration
340
+
341
+ ```ruby
342
+ session_config = {
343
+ max_messages: 100, # Maximum number of messages to keep
344
+ max_tokens: 4000, # Maximum token count
345
+ context_strategy: :sliding_window, # Strategy: :sliding_window, :relevance_based, :summary_based, :hybrid
346
+ preserve_system_messages: true # Always keep system messages
347
+ }
348
+ ```
349
+
350
+ ### Global Configuration
351
+
352
+ Configure the history manager in your `config/anthropic_config.yml`:
353
+
354
+ ```yaml
355
+ history:
356
+ cache_size: 100 # Maximum sessions in cache
357
+
358
+ session_defaults:
359
+ max_messages: 100
360
+ max_tokens: 4000
361
+ context_strategy: sliding_window
362
+ preserve_system_messages: true
363
+
364
+ persistence:
365
+ enabled: true
366
+ backend: filesystem
367
+ storage_path: "./history_data"
368
+ async: true
369
+
370
+ cleanup:
371
+ auto_cleanup: true
372
+ cleanup_interval: 3600 # 1 hour
373
+ session_ttl: 86400 # 24 hours
374
+
375
+ monitoring:
376
+ enabled: true
377
+ log_level: info
378
+ ```
379
+
380
+ ## Best Practices
381
+
382
+ ### 1. Choose the Right Strategy
383
+
384
+ - **Sliding Window**: Use for real-time chat, customer support, or when you only need recent context
385
+ - **Relevance-Based**: Use for Q&A systems, knowledge bases, or when semantic relevance matters
386
+ - **Summary-Based**: Use for long conversations, documentation, or when you need to preserve history
387
+ - **Hybrid**: Use for general-purpose applications or when conversation patterns vary
388
+
389
+ ### 2. Set Appropriate Limits
390
+
391
+ ```ruby
392
+ # For short conversations
393
+ max_messages: 20
394
+ max_tokens: 2000
395
+
396
+ # For medium conversations
397
+ max_messages: 50
398
+ max_tokens: 4000
399
+
400
+ # For long conversations
401
+ max_messages: 100
402
+ max_tokens: 8000
403
+ ```
404
+
405
+ ### 3. Use Meaningful Session IDs
406
+
407
+ ```ruby
408
+ # Good: Descriptive and unique
409
+ session_id: "user_#{user_id}"
410
+ session_id: "conversation_#{conversation_id}"
411
+ session_id: "support_ticket_#{ticket_id}"
412
+
413
+ # Bad: Generic or non-unique
414
+ session_id: "session"
415
+ session_id: "default"
416
+ ```
417
+
418
+ ### 4. Clean Up Old Sessions
419
+
420
+ ```ruby
421
+ # Manually trigger cleanup
422
+ engine.history_manager.cleanup_expired_sessions
423
+
424
+ # Or enable automatic cleanup
425
+ cleanup: {
426
+ auto_cleanup: true,
427
+ cleanup_interval: 3600, # Check every hour
428
+ session_ttl: 86400 # Delete after 24 hours
429
+ }
430
+ ```
431
+
432
+ ### 5. Monitor Performance
433
+
434
+ ```ruby
435
+ # Regularly check statistics
436
+ stats = engine.history_manager.get_stats
437
+
438
+ # Monitor cache hit rate (should be > 0.7)
439
+ puts "Cache hit rate: #{stats[:cache_hit_rate]}"
440
+
441
+ # Monitor token usage
442
+ puts "Average tokens per session: #{stats[:tokens_per_session_avg]}"
443
+ ```
444
+
445
+ ## Troubleshooting
446
+
447
+ ### Session Not Found
448
+
449
+ If a session is not found, it may have been evicted from cache or deleted:
450
+
451
+ ```ruby
452
+ # Check if session exists
453
+ if engine.history_manager.session_exists?(session_id)
454
+ # Session exists
455
+ else
456
+ # Session was deleted or never created
457
+ end
458
+ ```
459
+
460
+ ### High Memory Usage
461
+
462
+ If memory usage is high:
463
+
464
+ 1. Reduce `cache_size` in configuration
465
+ 2. Enable automatic cleanup
466
+ 3. Reduce `max_messages` and `max_tokens` per session
467
+ 4. Use summary-based strategy for long conversations
468
+
469
+ ### Slow Performance
470
+
471
+ If performance is slow:
472
+
473
+ 1. Check cache hit rate (should be > 0.7)
474
+ 2. Reduce `max_messages` to speed up context retrieval
475
+ 3. Use sliding window strategy for faster selection
476
+ 4. Enable async persistence
477
+
478
+ ## Migration Guide
479
+
480
+ ### From Old History API
481
+
482
+ The new system is backward compatible. No changes required!
483
+
484
+ ```ruby
485
+ # Old code - still works
486
+ SmartPrompt.define_worker :old_worker do
487
+ prompt(params[:message], with_history: true)
488
+ send_msg(with_history: true)
489
+ end
490
+
491
+ # New code - with explicit session management
492
+ SmartPrompt.define_worker :new_worker do
493
+ session_id = params[:session_id]
494
+ prompt(params[:message], with_history: true, session_id: session_id)
495
+ send_msg
496
+ end
497
+ ```
498
+
499
+ ### Gradual Migration
500
+
501
+ 1. Start using explicit session IDs for new features
502
+ 2. Keep existing workers unchanged
503
+ 3. Gradually migrate to new API as needed
504
+ 4. Both APIs work simultaneously
505
+
506
+ ## Requirements Coverage
507
+
508
+ This example set validates the following requirements:
509
+
510
+ - **Requirement 1**: Session Isolation (1.1, 1.2, 1.3, 1.4)
511
+ - **Requirement 2**: Message and Token Limits (2.1, 2.2, 2.3, 2.4, 2.5)
512
+ - **Requirement 3**: Persistence (3.1, 3.2, 3.3)
513
+ - **Requirement 4**: Session Operations (4.1, 4.2, 4.3, 4.4, 4.5)
514
+ - **Requirement 5**: Backward Compatibility (5.1, 5.2, 5.3, 5.4, 5.5)
515
+ - **Requirement 6**: Configuration (6.1)
516
+ - **Requirement 8**: Intelligent Context Management (8.1, 8.2, 8.3)
517
+ - **Requirement 9**: Automatic Compression (9.1, 9.2, 9.3)
518
+ - **Requirement 11**: Monitoring (11.3, 11.5)
519
+
520
+ ## Additional Resources
521
+
522
+ - [Design Document](.kiro/specs/history-optimization/design.md)
523
+ - [Requirements Document](.kiro/specs/history-optimization/requirements.md)
524
+ - [Implementation Tasks](.kiro/specs/history-optimization/tasks.md)
525
+ - [Main README](../README.md)
526
+
527
+ ## Support
528
+
529
+ For questions or issues:
530
+ 1. Check the design document for detailed architecture
531
+ 2. Review the requirements document for expected behavior
532
+ 3. Examine the worker examples in `workers/history_management_examples.rb`
533
+ 4. Run the comprehensive examples in this directory