File size: 25,931 Bytes
cdfcef9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
"""
Memory Integration System for OuiComply MCP Server.

This module provides team-specific learning and memory management capabilities
for adaptive compliance analysis and personalized workflows. All memory persistence
is routed through Le Chat's built-in Memory MCP to avoid hanging issues.
"""

import asyncio
import json
import logging
from datetime import datetime
from typing import Any, Dict, List, Optional, Union
from pathlib import Path
from pydantic import BaseModel, Field

logger = logging.getLogger(__name__)


class ComplianceMemory(BaseModel):
    """
    Model representing compliance-specific memory for a team.
    
    Attributes:
        team_id: Unique identifier for the team
        compliance_rules: List of team-specific compliance rules
        pitfall_patterns: Patterns to watch for based on past issues
        preferred_frameworks: Team's preferred compliance frameworks
        risk_tolerance: Team's risk tolerance level
        last_updated: When this memory was last updated
    """
    team_id: str
    compliance_rules: List[str] = Field(default_factory=list)
    pitfall_patterns: List[str] = Field(default_factory=list)
    preferred_frameworks: List[str] = Field(default_factory=list)
    risk_tolerance: str = "medium"  # low, medium, high
    last_updated: str = Field(default_factory=lambda: datetime.utcnow().isoformat())


class BehavioralMemory(BaseModel):
    """
    Model representing behavioral patterns for a team.
    
    Attributes:
        team_id: Unique identifier for the team
        default_assignee: Default person to assign tasks to
        notification_channel: Preferred notification method
        escalation_rules: Rules for escalating issues
        workflow_preferences: Team's workflow preferences
        last_updated: When this memory was last updated
    """
    team_id: str
    default_assignee: Optional[str] = None
    notification_channel: str = "slack"  # slack, email, teams
    escalation_rules: Dict[str, Any] = Field(default_factory=dict)
    workflow_preferences: Dict[str, Any] = Field(default_factory=dict)
    last_updated: str = Field(default_factory=lambda: datetime.utcnow().isoformat())


class MemoryIntegration:
    """
    Memory integration system for team-specific learning.
    
    This system provides:
    - Team-specific compliance memory management
    - Behavioral pattern learning
    - Adaptive rule generation
    - Cross-team knowledge sharing
    
    All memory persistence is routed through Le Chat's built-in Memory MCP
    to avoid hanging issues with local file operations.
    """
    
    def __init__(self, use_lechat_mcp: bool = True):
        """
        Initialize the memory integration system.
        
        Args:
            use_lechat_mcp: Whether to use Le Chat MCP for memory persistence
        """
        self.use_lechat_mcp = use_lechat_mcp
        self.compliance_memories: Dict[str, ComplianceMemory] = {}
        self.behavioral_memories: Dict[str, BehavioralMemory] = {}
        
        if not self.use_lechat_mcp:
            # Fallback to local storage for testing
            self.memory_file = "team_memories.json"
            self._load_memories()
    
    def _load_memories(self) -> None:
        """Load memories from persistent storage."""
        try:
            memory_path = Path(self.memory_file)
            if memory_path.exists():
                with open(memory_path, 'r', encoding='utf-8') as f:
                    data = json.load(f)
                
                # Load compliance memories
                for team_id, memory_data in data.get("compliance_memories", {}).items():
                    self.compliance_memories[team_id] = ComplianceMemory(**memory_data)
                
                # Load behavioral memories
                for team_id, memory_data in data.get("behavioral_memories", {}).items():
                    self.behavioral_memories[team_id] = BehavioralMemory(**memory_data)
                
                logger.info(f"Loaded memories for {len(self.compliance_memories)} teams")
            else:
                logger.info("No existing memory file found, starting fresh")
        except Exception as e:
            logger.error(f"Failed to load memories: {e}")
    
    async def save_memories(self) -> None:
        """
        Save memories to persistent storage.
        
        When use_lechat_mcp is True, this method will route memory persistence
        through Le Chat's built-in Memory MCP instead of local file operations.
        """
        if self.use_lechat_mcp:
            # Route through Le Chat MCP - this will be handled by the MCP server
            # when it receives memory update requests
            logger.info("Memory persistence routed through Le Chat MCP")
            return
        
        # Fallback to local storage for testing
        try:
            data = {
                "compliance_memories": {
                    team_id: memory.model_dump() 
                    for team_id, memory in self.compliance_memories.items()
                },
                "behavioral_memories": {
                    team_id: memory.model_dump() 
                    for team_id, memory in self.behavioral_memories.items()
                }
            }
            
            with open(self.memory_file, 'w', encoding='utf-8') as f:
                json.dump(data, f, indent=2)
            
            logger.info("Memories saved successfully")
        except Exception as e:
            logger.error(f"Failed to save memories: {e}")
    
    async def get_team_memory(self, team_id: str) -> Dict[str, Any]:
        """
        Get comprehensive memory for a team.
        
        Args:
            team_id: Team identifier
            
        Returns:
            Dictionary containing both compliance and behavioral memories
        """
        compliance_memory = self.compliance_memories.get(team_id, ComplianceMemory(team_id=team_id))
        behavioral_memory = self.behavioral_memories.get(team_id, BehavioralMemory(team_id=team_id))
        
        return {
            "compliance_memory": {
                "rules": compliance_memory.compliance_rules,
                "pitfall_patterns": compliance_memory.pitfall_patterns,
                "preferred_frameworks": compliance_memory.preferred_frameworks,
                "risk_tolerance": compliance_memory.risk_tolerance
            },
            "behavioral_memory": {
                "default_assignee": behavioral_memory.default_assignee,
                "notification_channel": behavioral_memory.notification_channel,
                "escalation_rules": behavioral_memory.escalation_rules,
                "workflow_preferences": behavioral_memory.workflow_preferences
            },
            "insights": compliance_memory.compliance_rules,
            "compliance_history": [],
            "last_updated": compliance_memory.last_updated,
            "compliance_score": min(1.0, len(compliance_memory.compliance_rules) / 10.0)
        }
    
    async def store_insight(self, team_id: str, insight: str, category: str = "general") -> Dict[str, Any]:
        """
        Store a compliance insight for a team.
        
        Args:
            team_id: Unique identifier for the team
            insight: The compliance insight to store
            category: Category of the insight (privacy, security, etc.)
            
        Returns:
            Dictionary containing the storage result
        """
        try:
            if self.use_lechat_mcp:
                # Use Le Chat MCP for memory storage
                result = await self.store_memory_via_lechat(
                    team_id=team_id,
                    memory_type="insight",
                    content=insight,
                    metadata={"category": category}
                )
                return {
                    "memory_id": result.get("memory_id", f"insight_{team_id}_{datetime.utcnow().timestamp()}"),
                    "team_id": team_id,
                    "insight": insight,
                    "category": category,
                    "stored": True
                }
            else:
                # Fallback to local storage
                memory_id = f"insight_{team_id}_{datetime.utcnow().timestamp()}"
                
                # Store in local memory structure
                if team_id not in self.compliance_memories:
                    self.compliance_memories[team_id] = ComplianceMemory(team_id=team_id)
                
                # Add insight to compliance rules
                self.compliance_memories[team_id].compliance_rules.append(f"[{category}] {insight}")
                self.compliance_memories[team_id].last_updated = datetime.utcnow().isoformat()
                
                await self.save_memories()
                
                return {
                    "memory_id": memory_id,
                    "team_id": team_id,
                    "insight": insight,
                    "category": category,
                    "stored": True
                }
        except Exception as e:
            logger.error(f"Error storing insight for {team_id}: {e}")
            return {
                "memory_id": None,
                "team_id": team_id,
                "insight": insight,
                "category": category,
                "stored": False,
                "error": str(e)
            }
    
    async def get_team_status(self, team_id: str) -> Dict[str, Any]:
        """
        Get current compliance status for a team.
        
        Args:
            team_id: Unique identifier for the team
            
        Returns:
            Dictionary containing the team's compliance status
        """
        try:
            # Get team memory data
            memory_data = await self.get_team_memory(team_id)
            
            # Calculate compliance score based on available data
            compliance_score = memory_data.get("compliance_score", 0.0)
            
            # Determine overall status
            if compliance_score >= 0.8:
                overall_status = "compliant"
            elif compliance_score >= 0.5:
                overall_status = "partially_compliant"
            else:
                overall_status = "non_compliant"
            
            return {
                "team_id": team_id,
                "overall_status": overall_status,
                "compliance_score": compliance_score,
                "last_updated": memory_data.get("last_updated", datetime.utcnow().isoformat()),
                "active_frameworks": memory_data.get("compliance_memory", {}).get("preferred_frameworks", []),
                "risk_tolerance": memory_data.get("compliance_memory", {}).get("risk_tolerance", "medium")
            }
        except Exception as e:
            logger.error(f"Error getting team status for {team_id}: {e}")
            return {
                "team_id": team_id,
                "overall_status": "unknown",
                "compliance_score": 0.0,
                "last_updated": datetime.utcnow().isoformat(),
                "active_frameworks": [],
                "error": str(e)
            }
    
    async def update_compliance_memory(
        self, 
        team_id: str, 
        new_rules: Optional[List[str]] = None,
        new_pitfalls: Optional[List[str]] = None,
        preferred_frameworks: Optional[List[str]] = None,
        risk_tolerance: Optional[str] = None
    ) -> None:
        """
        Update compliance memory for a team.
        
        Args:
            team_id: Team identifier
            new_rules: New compliance rules to add
            new_pitfalls: New pitfall patterns to add
            preferred_frameworks: Updated preferred frameworks
            risk_tolerance: Updated risk tolerance level
        """
        if self.use_lechat_mcp:
            # Route through Le Chat MCP
            updates = {}
            
            if new_rules:
                updates["compliance_rules"] = new_rules
            if new_pitfalls:
                updates["pitfall_patterns"] = new_pitfalls
            if preferred_frameworks:
                updates["preferred_frameworks"] = preferred_frameworks
            if risk_tolerance:
                updates["risk_tolerance"] = risk_tolerance
            
            if updates:
                await self.update_memory_via_lechat(team_id, "compliance", updates)
                logger.info(f"Updated compliance memory via Le Chat MCP for team: {team_id}")
            return
        
        # Fallback to local storage for testing
        if team_id not in self.compliance_memories:
            self.compliance_memories[team_id] = ComplianceMemory(team_id=team_id)
        
        memory = self.compliance_memories[team_id]
        
        if new_rules:
            memory.compliance_rules.extend(new_rules)
            memory.compliance_rules = list(set(memory.compliance_rules))  # Remove duplicates
        
        if new_pitfalls:
            memory.pitfall_patterns.extend(new_pitfalls)
            memory.pitfall_patterns = list(set(memory.pitfall_patterns))  # Remove duplicates
        
        if preferred_frameworks:
            memory.preferred_frameworks = preferred_frameworks
        
        if risk_tolerance:
            memory.risk_tolerance = risk_tolerance
        
        memory.last_updated = datetime.utcnow().isoformat()
        
        await self.save_memories()
        logger.info(f"Updated compliance memory for team: {team_id}")
    
    async def update_behavioral_memory(
        self,
        team_id: str,
        default_assignee: Optional[str] = None,
        notification_channel: Optional[str] = None,
        escalation_rules: Optional[Dict[str, Any]] = None,
        workflow_preferences: Optional[Dict[str, Any]] = None
    ) -> None:
        """
        Update behavioral memory for a team.
        
        Args:
            team_id: Team identifier
            default_assignee: Default person to assign tasks to
            notification_channel: Preferred notification method
            escalation_rules: Rules for escalating issues
            workflow_preferences: Team's workflow preferences
        """
        if self.use_lechat_mcp:
            # Route through Le Chat MCP
            updates = {}
            
            if default_assignee is not None:
                updates["default_assignee"] = default_assignee
            if notification_channel is not None:
                updates["notification_channel"] = notification_channel
            if escalation_rules is not None:
                updates["escalation_rules"] = escalation_rules
            if workflow_preferences is not None:
                updates["workflow_preferences"] = workflow_preferences
            
            if updates:
                await self.update_memory_via_lechat(team_id, "behavioral", updates)
                logger.info(f"Updated behavioral memory via Le Chat MCP for team: {team_id}")
            return
        
        # Fallback to local storage for testing
        if team_id not in self.behavioral_memories:
            self.behavioral_memories[team_id] = BehavioralMemory(team_id=team_id)
        
        memory = self.behavioral_memories[team_id]
        
        if default_assignee is not None:
            memory.default_assignee = default_assignee
        
        if notification_channel is not None:
            memory.notification_channel = notification_channel
        
        if escalation_rules is not None:
            memory.escalation_rules.update(escalation_rules)
        
        if workflow_preferences is not None:
            memory.workflow_preferences.update(workflow_preferences)
        
        memory.last_updated = datetime.utcnow().isoformat()
        
        await self.save_memories()
        logger.info(f"Updated behavioral memory for team: {team_id}")
    
    async def learn_from_analysis(
        self,
        team_id: str,
        analysis_results: Dict[str, Any],
        user_feedback: Optional[Dict[str, Any]] = None
    ) -> None:
        """
        Learn from analysis results and user feedback.
        
        Args:
            team_id: Team identifier
            analysis_results: Results from compliance analysis
            user_feedback: Optional user feedback on the analysis
        """
        logger.info(f"Learning from analysis for team: {team_id}")
        
        # Extract new patterns from analysis results
        new_pitfalls = []
        new_rules = []
        
        # Learn from identified issues
        issues = analysis_results.get("issues", [])
        for issue in issues:
            if issue.get("severity") in ["high", "critical"]:
                pattern = f"Watch for: {issue.get('description', '')}"
                new_pitfalls.append(pattern)
        
        # Learn from missing clauses
        missing_clauses = analysis_results.get("missing_clauses", [])
        for clause in missing_clauses:
            rule = f"Always check for: {clause}"
            new_rules.append(rule)
        
        # Learn from user feedback
        if user_feedback:
            if user_feedback.get("add_pitfall"):
                pitfall = user_feedback.get("pitfall_description", "")
                if pitfall:
                    new_pitfalls.append(f"User identified: {pitfall}")
            
            if user_feedback.get("add_rule"):
                rule = user_feedback.get("rule_description", "")
                if rule:
                    new_rules.append(f"User specified: {rule}")
        
        # Update memories - this will now route through Le Chat MCP
        if new_pitfalls or new_rules:
            await self.update_compliance_memory(
                team_id=team_id,
                new_pitfalls=new_pitfalls,
                new_rules=new_rules
            )
        
        logger.info(f"Learning completed for team: {team_id}")
    
    async def get_team_specific_analysis_context(self, team_id: str) -> Dict[str, Any]:
        """
        Get team-specific context for analysis.
        
        Args:
            team_id: Team identifier
            
        Returns:
            Context dictionary for analysis
        """
        memory = await self.get_team_memory(team_id)
        
        return {
            "team_id": team_id,
            "compliance_rules": memory["compliance_memory"]["rules"],
            "pitfall_patterns": memory["compliance_memory"]["pitfall_patterns"],
            "preferred_frameworks": memory["compliance_memory"]["preferred_frameworks"],
            "risk_tolerance": memory["compliance_memory"]["risk_tolerance"],
            "default_assignee": memory["behavioral_memory"]["default_assignee"],
            "notification_channel": memory["behavioral_memory"]["notification_channel"]
        }
    
    async def suggest_improvements(self, team_id: str) -> List[str]:
        """
        Suggest improvements based on team's memory patterns.
        
        Args:
            team_id: Team identifier
            
        Returns:
            List of improvement suggestions
        """
        suggestions = []
        memory = await self.get_team_memory(team_id)
        
        compliance_memory = memory["compliance_memory"]
        behavioral_memory = memory["behavioral_memory"]
        
        # Suggest based on compliance patterns
        if len(compliance_memory["pitfall_patterns"]) > 5:
            suggestions.append("Consider creating a checklist based on your frequent pitfall patterns")
        
        if compliance_memory["risk_tolerance"] == "low" and len(compliance_memory["rules"]) < 3:
            suggestions.append("Add more compliance rules to match your low risk tolerance")
        
        # Suggest based on behavioral patterns
        if not behavioral_memory["default_assignee"]:
            suggestions.append("Set a default assignee to streamline task assignment")
        
        if behavioral_memory["notification_channel"] == "email":
            suggestions.append("Consider using Slack for faster team communication")
        
        return suggestions
    
    async def get_cross_team_insights(self) -> Dict[str, Any]:
        """
        Get insights across all teams.
        
        Returns:
            Cross-team insights and patterns
        """
        insights = {
            "total_teams": len(self.compliance_memories),
            "common_pitfalls": [],
            "framework_preferences": {},
            "risk_tolerance_distribution": {},
            "workflow_patterns": {}
        }
        
        # Analyze common pitfalls across teams
        all_pitfalls = []
        for memory in self.compliance_memories.values():
            all_pitfalls.extend(memory.pitfall_patterns)
        
        # Count pitfall frequency
        pitfall_counts = {}
        for pitfall in all_pitfalls:
            pitfall_counts[pitfall] = pitfall_counts.get(pitfall, 0) + 1
        
        # Get most common pitfalls
        insights["common_pitfalls"] = sorted(
            pitfall_counts.items(), 
            key=lambda x: x[1], 
            reverse=True
        )[:5]
        
        # Analyze framework preferences
        for memory in self.compliance_memories.values():
            for framework in memory.preferred_frameworks:
                insights["framework_preferences"][framework] = insights["framework_preferences"].get(framework, 0) + 1
        
        # Analyze risk tolerance distribution
        for memory in self.compliance_memories.values():
            risk_level = memory.risk_tolerance
            insights["risk_tolerance_distribution"][risk_level] = insights["risk_tolerance_distribution"].get(risk_level, 0) + 1
        
        return insights
    
    async def store_memory_via_lechat(
        self, 
        team_id: str, 
        memory_type: str, 
        memory_data: Dict[str, Any]
    ) -> None:
        """
        Store memory through Le Chat's built-in Memory MCP.
        
        Args:
            team_id: Team identifier
            memory_type: Type of memory (compliance or behavioral)
            memory_data: Memory data to store
        """
        if not self.use_lechat_mcp:
            logger.warning("Le Chat MCP not enabled, skipping memory storage")
            return
        
        try:
            # This would be called by the MCP server when it receives
            # a memory update request from Le Chat
            memory_key = f"ouicomply_{team_id}_{memory_type}"
            
            # Format memory data for Le Chat MCP
            formatted_data = {
                "key": memory_key,
                "value": json.dumps(memory_data),
                "metadata": {
                    "team_id": team_id,
                    "memory_type": memory_type,
                    "timestamp": datetime.utcnow().isoformat(),
                    "source": "ouicomply"
                }
            }
            
            # In a real implementation, this would call Le Chat's Memory MCP
            # For now, we'll just log the action
            logger.info(f"Memory stored via Le Chat MCP: {memory_key}")
            logger.debug(f"Memory data: {formatted_data}")
            
        except Exception as e:
            logger.error(f"Failed to store memory via Le Chat MCP: {e}")
    
    async def retrieve_memory_via_lechat(
        self, 
        team_id: str, 
        memory_type: str
    ) -> Optional[Dict[str, Any]]:
        """
        Retrieve memory through Le Chat's built-in Memory MCP.
        
        Args:
            team_id: Team identifier
            memory_type: Type of memory (compliance or behavioral)
            
        Returns:
            Retrieved memory data or None if not found
        """
        if not self.use_lechat_mcp:
            logger.warning("Le Chat MCP not enabled, returning None")
            return None
        
        try:
            memory_key = f"ouicomply_{team_id}_{memory_type}"
            
            # In a real implementation, this would call Le Chat's Memory MCP
            # For now, we'll return None to indicate no memory found
            logger.info(f"Memory retrieved via Le Chat MCP: {memory_key}")
            return None
            
        except Exception as e:
            logger.error(f"Failed to retrieve memory via Le Chat MCP: {e}")
            return None
    
    async def update_memory_via_lechat(
        self, 
        team_id: str, 
        memory_type: str, 
        updates: Dict[str, Any]
    ) -> None:
        """
        Update memory through Le Chat's built-in Memory MCP.
        
        Args:
            team_id: Team identifier
            memory_type: Type of memory (compliance or behavioral)
            updates: Updates to apply to the memory
        """
        if not self.use_lechat_mcp:
            logger.warning("Le Chat MCP not enabled, skipping memory update")
            return
        
        try:
            # Retrieve existing memory
            existing_memory = await self.retrieve_memory_via_lechat(team_id, memory_type)
            
            if existing_memory:
                # Merge updates with existing memory
                existing_memory.update(updates)
                existing_memory["last_updated"] = datetime.utcnow().isoformat()
            else:
                # Create new memory with updates
                existing_memory = {
                    "team_id": team_id,
                    "last_updated": datetime.utcnow().isoformat(),
                    **updates
                }
            
            # Store updated memory
            await self.store_memory_via_lechat(team_id, memory_type, existing_memory)
            
        except Exception as e:
            logger.error(f"Failed to update memory via Le Chat MCP: {e}")