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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fbdfc24
f37bf1d
 
 
fbdfc24
f37bf1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fbdfc24
f37bf1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fbdfc24
f37bf1d
 
fbdfc24
 
 
f37bf1d
 
 
 
 
 
 
 
 
fbdfc24
 
 
 
f37bf1d
fbdfc24
f37bf1d
 
 
 
 
 
 
fbdfc24
 
 
 
f37bf1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Multi-Country Legal RAG System - Interactive Testing Mode with Human-in-the-Loop Support
"""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent))

import asyncio
import logging
import time
from datetime import datetime
from typing import Optional

from core.system_initializer import setup_system

# Setup comprehensive logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.StreamHandler(sys.stdout),
        logging.FileHandler('legal_rag_system.log', mode='a')
    ]
)
logger = logging.getLogger(__name__)

class LegalRAGTester:
    """
    Interactive tester for the Legal RAG system with support for:
    - Human-in-the-loop interrupts
    - Session management
    - Statistics tracking
    - Assistance workflow testing
    """
    
    def __init__(self):
        self.system = None
        self.chat_manager = None
        self.session_id = f"test_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
        self.message_count = 0
    
    async def initialize(self):
        """Initialize the Legal RAG system"""
        print("🚀 Initializing Legal RAG System...")
        try:
            self.system = await setup_system()
            self.chat_manager = self.system["chat_manager"]
            
            print("✅ System initialized successfully!")
            
            # Print system info
            info = self.chat_manager.get_checkpointer_info()
            stats = self.chat_manager.get_global_stats()
            
            print(f"📊 Checkpointer: {info['type']} ({info['description']})")
            print(f"💾 Persistent: {info['persistent']}")
            print(f"🎯 Session ID: {self.session_id}")
            print(f"🌍 Available countries: Bénin, Madagascar")
            
            return True
            
        except Exception as e:
            logger.exception("Failed to initialize system")
            print(f"❌ Initialization failed: {e}")
            return False
    
    def _handle_interrupt(self, interrupt_value: dict) -> str:
        """
        Handle human-in-the-loop interrupts synchronously.
        This is called when the graph needs human approval.
        
        Args:
            interrupt_value: Interrupt data containing message and context
            
        Returns:
            Moderator's decision (approve/reject)
        """
        print("\n" + "="*70)
        print("🔒 HUMAN APPROVAL REQUIRED")
        print("="*70)
        
        # Extract interrupt information
        message = interrupt_value.get("message", "")
        email = interrupt_value.get("user_email", "N/A")
        country = interrupt_value.get("country", "N/A")
        description = interrupt_value.get("description", "N/A")
        
        # Display formatted approval request
        print(message)
        print()
        
        # Get moderator decision with validation
        while True:
            moderator_input = input("🔐 Moderator Decision: ").strip()
            
            if not moderator_input:
                print("⚠️  Please provide a decision (approve/reject)")
                continue
            
            # Validate input
            input_lower = moderator_input.lower()
            if any(keyword in input_lower for keyword in ["approve", "approuver", "accept"]):
                print("✅ Request APPROVED")
                break
            elif any(keyword in input_lower for keyword in ["reject", "rejeter", "refuse"]):
                print("❌ Request REJECTED")
                break
            else:
                print("⚠️  Invalid decision. Use 'approve [reason]' or 'reject [reason]'")
                continue
        
        print("="*70 + "\n")
        return moderator_input
    
    async def chat(self, message: str) -> Optional[str]:
        """
        Send a chat message and get response with interrupt handling.
        
        Args:
            message: User message
            
        Returns:
            Assistant response or None on error
        """
        if not self.chat_manager:
            print("❌ System not initialized. Please restart.")
            return None
        
        try:
            start_time = time.time()
            self.message_count += 1
            
            # Send message with interrupt handler
            response = await self.chat_manager.chat(
                message=message,
                session_id=self.session_id,
                interrupt_handler=self._handle_interrupt  # Enable synchronous interrupt handling
            )
            
            response_time = time.time() - start_time
            
            # Display response
            print(f"\n🤖 Assistant ({response_time:.2f}s):")
            
            # Format multi-line responses
            for line in response.split('\n'):
                if line.strip():
                    print(f"   {line}")
                else:
                    print()
            
            # Check for pending interrupts (async mode fallback)
            if self.chat_manager.has_pending_interrupt(self.session_id):
                print("\n⏸️  💡 System paused - waiting for moderator decision")
                print("   Your next message will be treated as approval/rejection")
            
            return response
            
        except KeyboardInterrupt:
            raise  # Re-raise to allow graceful shutdown
        except Exception as e:
            logger.exception(f"Error processing message: {message}")
            print(f"❌ Error: {str(e)}")
            return None
    
    def display_stats(self):
        """Display current system statistics"""
        if not self.chat_manager:
            print("❌ System not initialized")
            return
        
        stats = self.chat_manager.get_global_stats()
        routing = stats.get('routing_stats', {})
        
        print("\n" + "="*70)
        print("📊 SYSTEM STATISTICS")
        print("="*70)
        print(f"💬 Session Messages: {self.message_count}")
        print(f"🔢 Total System Queries: {stats.get('total_queries', 0)}")
        print(f"👥 Active Sessions: {stats.get('active_sessions', 0)}")
        print(f"⏸️  Pending Interrupts: {stats.get('pending_interrupts', 0)}")
        print()
        print("🔀 Routing Statistics:")
        print(f"   📍 Bénin queries: {routing.get('benin', 0)}")
        print(f"   📍 Madagascar queries: {routing.get('madagascar', 0)}")
        print(f"   ❓ Unclear queries: {routing.get('unclear', 0)}")
        print()
        print(f"💾 Storage: {stats.get('type', 'unknown')} - {stats.get('description', '')}")
        print("="*70 + "\n")
    
    def display_help(self):
        """Display help information"""
        print("\n" + "="*70)
        print("📋 AVAILABLE COMMANDS")
        print("="*70)
        print("  quit, exit, q       - Exit the program")
        print("  stats, s            - Show system statistics")
        print("  clear, cls          - Clear the screen")
        print("  help, h, ?          - Show this help message")
        print("  history             - Show conversation history")
        print("  reset               - Reset current session")
        print()
        print("💡 TESTING TIPS:")
        print("  - Legal queries: Ask about laws in Bénin or Madagascar")
        print("  - Assistance: Say 'je veux parler à un avocat'")
        print("  - Follow the prompts for email and description")
        print("  - You'll be prompted for approval when needed")
        print("="*70 + "\n")
    
    async def display_history(self):
        """Display conversation history"""
        if not self.chat_manager:
            print("❌ System not initialized")
            return
        
        try:
            history = await self.chat_manager.get_conversation_history(self.session_id)
            
            if not history:
                print("\n💬 No conversation history yet\n")
                return
            
            print("\n" + "="*70)
            print("💬 CONVERSATION HISTORY")
            print("="*70)
            
            for i, msg in enumerate(history, 1):
                role = "👤 User" if msg.type == "human" else "🤖 Assistant"
                content = msg.content[:100] + "..." if len(msg.content) > 100 else msg.content
                print(f"{i}. {role}: {content}")
            
            print("="*70 + "\n")
            
        except Exception as e:
            logger.exception("Error displaying history")
            print(f"❌ Error: {e}\n")
    
    def reset_session(self):
        """Reset the current session"""
        old_session = self.session_id
        self.session_id = f"test_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
        self.message_count = 0
        print(f"\n🔄 Session reset")
        print(f"   Old: {old_session}")
        print(f"   New: {self.session_id}\n")
    
    def clear_screen(self):
        """Clear the terminal screen"""
        print("\033[H\033[J", end="")

async def interactive_mode():
    """Run the system in interactive mode with full interrupt support"""
    tester = LegalRAGTester()
    
    # Initialize system
    if not await tester.initialize():
        print("❌ Failed to initialize. Exiting.")
        return
    
    # Display welcome message
    print("\n" + "="*70)
    print("🎯 LEGAL RAG INTERACTIVE TESTING MODE")
    print("="*70)
    print("Type 'help' for available commands")
    print("="*70)
    
    # Quick test questions
    test_questions = [
        "Bonjour, comment ça va?",
        "Quelle est la procédure de divorce au Bénin?",
        "Je veux parler à un avocat spécialisé",
        "Quels sont les droits des enfants à Madagascar?",
        "Quelles sont les conditions pour se marier au Bénin?",
    ]
    
    print("\n💡 Quick test questions:")
    for i, question in enumerate(test_questions, 1):
        print(f"  {i}. {question}")
    print()
    
    # Main interaction loop
    while True:
        try:
            # Get user input
            user_input = input("👤 You: ").strip()
            
            if not user_input:
                continue
            
            # Handle commands
            cmd_lower = user_input.lower()
            
            # Exit commands
            if cmd_lower in ['quit', 'exit', 'q']:
                print("\n👋 Goodbye! Thank you for testing.")
                break
            
            # Stats command
            elif cmd_lower in ['stats', 's']:
                tester.display_stats()
                continue
            
            # Clear screen
            elif cmd_lower in ['clear', 'cls']:
                tester.clear_screen()
                continue
            
            # Help command
            elif cmd_lower in ['help', 'h', '?']:
                tester.display_help()
                continue
            
            # History command
            elif cmd_lower == 'history':
                await tester.display_history()
                continue
            
            # Reset session
            elif cmd_lower == 'reset':
                tester.reset_session()
                continue
            
            # Regular chat message
            else:
                await tester.chat(user_input)
            
        except KeyboardInterrupt:
            print("\n\n⚠️  Interrupted by user")
            print("Type 'quit' to exit or continue chatting\n")
            continue
        
        except Exception as e:
            logger.exception("Unexpected error in main loop")
            print(f"\n❌ Unexpected error: {e}\n")

async def demo_mode():
    """Run automated demo of the system capabilities"""
    tester = LegalRAGTester()
    
    print("🚀 Running Automated Demo...")
    
    if not await tester.initialize():
        print("❌ Failed to initialize. Exiting demo.")
        return
    
    demo_scenarios = [
        {
            "name": "Greeting",
            "messages": ["Bonjour, je m'appelle Test"]
        },
        {
            "name": "Legal Query - Bénin",
            "messages": ["Quelle est la procédure de divorce au Bénin?"]
        },
        {
            "name": "Legal Query - Madagascar",
            "messages": ["Quels sont les droits de succession à Madagascar?"]
        },
        {
            "name": "Conversation Repair",
            "messages": ["Peux-tu répéter plus simplement?"]
        },
        {
            "name": "Summary Request",
            "messages": ["Résume notre conversation"]
        }
    ]
    
    print("\n" + "="*70)
    print("🧪 DEMO SCENARIOS")
    print("="*70 + "\n")
    
    for i, scenario in enumerate(demo_scenarios, 1):
        print(f"\n{'='*70}")
        print(f"📋 Scenario {i}/{len(demo_scenarios)}: {scenario['name']}")
        print('='*70)
        
        for message in scenario['messages']:
            print(f"\n🧪 Testing: '{message}'")
            await tester.chat(message)
            await asyncio.sleep(1.5)  # Pause between messages
    
    print("\n" + "="*70)
    print("✅ DEMO COMPLETED")
    print("="*70)
    tester.display_stats()

async def test_assistance_workflow():
    """Test the complete assistance workflow with interrupts"""
    tester = LegalRAGTester()
    
    print("🧪 Testing Assistance Workflow with Human Approval...")
    
    if not await tester.initialize():
        print("❌ Failed to initialize. Exiting test.")
        return
    
    print("\n" + "="*70)
    print("🔬 ASSISTANCE WORKFLOW TEST")
    print("="*70 + "\n")
    
    # Simulated assistance workflow
    workflow_steps = [
        "Je veux parler à un avocat",
        "test@example.com",
        "J'ai besoin d'aide pour un problème de divorce au Bénin",
        "oui"
    ]
    
    step_names = [
        "1. Initiate assistance request",
        "2. Provide email",
        "3. Describe situation",
        "4. Confirm request"
    ]
    
    for step_name, message in zip(step_names, workflow_steps):
        print(f"\n📍 {step_name}")
        print(f"   Message: '{message}'")
        await tester.chat(message)
        await asyncio.sleep(1)
    
    print("\n" + "="*70)
    print("✅ WORKFLOW TEST COMPLETED")
    print("="*70)
    tester.display_stats()

def main():
    """Main entry point with argument parsing"""
    import argparse
    
    parser = argparse.ArgumentParser(
        description="Legal RAG System - Interactive Testing with Human-in-the-Loop Support",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
Examples:
  python main.py                    # Interactive mode (default)
  python main.py --mode demo        # Automated demo
  python main.py --mode test        # Test assistance workflow
  python main.py --debug            # Enable debug logging
        """
    )
    
    parser.add_argument(
        "--mode",
        choices=["interactive", "demo", "test"],
        default="interactive",
        help="Run mode: interactive (default), demo, or test"
    )
    
    parser.add_argument(
        "--debug",
        action="store_true",
        help="Enable debug logging"
    )
    
    args = parser.parse_args()
    
    # Adjust logging level if debug
    if args.debug:
        logging.getLogger().setLevel(logging.DEBUG)
        print("🐛 Debug logging enabled\n")
    
    # Run selected mode
    try:
        if args.mode == "interactive":
            asyncio.run(interactive_mode())
        elif args.mode == "demo":
            asyncio.run(demo_mode())
        elif args.mode == "test":
            asyncio.run(test_assistance_workflow())
    
    except KeyboardInterrupt:
        print("\n\n👋 Program interrupted by user. Goodbye!")
    
    except Exception as e:
        logger.exception("Fatal error occurred")
        print(f"\n❌ Fatal error: {e}")
        sys.exit(1)

if __name__ == "__main__":
    main()