# 🔄 Refonte : DĂ©tection de Workflows Complets ## 🎯 Objectif Transformer le systĂšme d'un dĂ©tecteur de **micro-patterns** (3 clics identiques) en un dĂ©tecteur de **macro-workflows** (sĂ©quences d'actions rĂ©pĂ©tĂ©es). ## ❌ ProblĂšme Actuel ### Ce qui est implĂ©mentĂ© ```python # EventCapture dĂ©tecte : Clic A, Clic A, Clic A → Pattern dĂ©tectĂ© ✅ ``` **ProblĂšme** : Personne ne clique 3 fois sur le mĂȘme bouton dans la vraie vie ! ### Ce qu'il faut ```python # DĂ©tecter des workflows : Jour 1: [Ouvre App → Clic Menu → Type Texte → Sauvegarde] Jour 2: [Ouvre App → Clic Menu → Type Texte → Sauvegarde] Jour 3: [Ouvre App → Clic Menu → Type Texte → Sauvegarde] → Workflow dĂ©tectĂ© ✅ ``` ## ✅ Solution : Workflow Pattern Detector ### 1. Nouvelle Architecture ``` EventCapture (capture toutes les actions) ↓ WorkflowBuffer (garde les N derniĂšres actions) ↓ WorkflowDetector (dĂ©tecte les sĂ©quences rĂ©pĂ©tĂ©es) ↓ WorkflowManager (crĂ©e et gĂšre les workflows) ↓ SuggestionManager (suggĂšre le workflow suivant) ``` ### 2. Algorithme de DĂ©tection #### Étape 1 : Segmentation Temporelle ```python # Diviser les actions en "sessions" # Une session = actions dans une fenĂȘtre de temps (ex: 5 minutes) Session 1: [Action1, Action2, Action3, Action4] # 10h00-10h05 Session 2: [Action5, Action6, Action7] # 10h30-10h35 Session 3: [Action8, Action9, Action10, Action11] # 11h00-11h05 ``` #### Étape 2 : Comparaison de SĂ©quences ```python # Comparer les sessions pour trouver des similaritĂ©s def compare_sessions(session1, session2): # Utiliser la distance de Levenshtein sur les embeddings # Ou DTW (Dynamic Time Warping) # Ou similaritĂ© cosinus entre sĂ©quences similarity = calculate_sequence_similarity(session1, session2) return similarity > threshold # ex: 0.75 ``` #### Étape 3 : DĂ©tection de RĂ©pĂ©tition ```python # Si 3 sessions similaires → Workflow dĂ©tectĂ© if count_similar_sessions() >= 3: workflow = create_workflow(sessions) save_workflow(workflow) notify_user("Workflow dĂ©tectĂ© !") ``` ### 3. Structure de DonnĂ©es #### Workflow ```python @dataclass class Workflow: workflow_id: str name: str # "Ouvrir OnlyOffice et crĂ©er document" steps: List[WorkflowStep] repetitions: int # Nombre de fois observĂ© confidence: float last_seen: datetime @dataclass class WorkflowStep: step_id: int action_type: str # "click", "type", "scroll" target_description: str # "Bouton Nouveau Document" screenshot: np.ndarray embedding: np.ndarray position: Tuple[int, int] window: str ``` ### 4. DĂ©tection en Temps RĂ©el ```python class WorkflowDetector: def __init__(self): self.action_buffer = deque(maxlen=100) # 100 derniĂšres actions self.sessions = [] self.known_workflows = [] def on_action(self, action): # Ajouter l'action au buffer self.action_buffer.append(action) # VĂ©rifier si on doit crĂ©er une nouvelle session if self.should_start_new_session(action): self.finalize_current_session() self.start_new_session() # Ajouter l'action Ă  la session courante self.current_session.append(action) # VĂ©rifier si on dĂ©tecte un workflow if len(self.sessions) >= 3: workflow = self.detect_workflow() if workflow: self.on_workflow_detected(workflow) def should_start_new_session(self, action): # Nouvelle session si : # - Plus de 5 minutes depuis la derniĂšre action # - Changement de fenĂȘtre majeur # - Action de "fin" (fermeture, sauvegarde) if not self.current_session: return True last_action = self.current_session[-1] time_gap = action.timestamp - last_action.timestamp return ( time_gap > timedelta(minutes=5) or action.window != last_action.window or action.action_type in ["close", "save"] ) def detect_workflow(self): # Comparer les 3 derniĂšres sessions recent_sessions = self.sessions[-3:] # Calculer la similaritĂ© entre elles sim_01 = self.compare_sessions(recent_sessions[0], recent_sessions[1]) sim_12 = self.compare_sessions(recent_sessions[1], recent_sessions[2]) sim_02 = self.compare_sessions(recent_sessions[0], recent_sessions[2]) avg_similarity = (sim_01 + sim_12 + sim_02) / 3 if avg_similarity > 0.75: # CrĂ©er le workflow return self.create_workflow_from_sessions(recent_sessions) return None def compare_sessions(self, session1, session2): # Utiliser DTW (Dynamic Time Warping) pour comparer # les sĂ©quences d'embeddings embeddings1 = [action.embedding for action in session1] embeddings2 = [action.embedding for action in session2] distance = dtw_distance(embeddings1, embeddings2) similarity = 1 / (1 + distance) return similarity ``` ### 5. Suggestion de Workflow ```python class WorkflowSuggestionManager: def check_for_workflow_suggestion(self, current_actions): # VĂ©rifier si les actions actuelles correspondent # au dĂ©but d'un workflow connu for workflow in self.known_workflows: match_score = self.match_workflow_start( current_actions, workflow.steps ) if match_score > 0.75: # On est probablement au dĂ©but du workflow ! return { "workflow": workflow, "confidence": match_score, "next_steps": workflow.steps[len(current_actions):] } return None def match_workflow_start(self, current_actions, workflow_steps): # Comparer les N premiĂšres actions avec le dĂ©but du workflow if len(current_actions) > len(workflow_steps): return 0.0 similarities = [] for i, action in enumerate(current_actions): step = workflow_steps[i] sim = cosine_similarity(action.embedding, step.embedding) similarities.append(sim) return np.mean(similarities) ``` ## 📊 Exemple Concret ### ScĂ©nario : OnlyOffice ```python # Jour 1 - 10h00 Session 1: 1. Clic sur lanceur (embedding_1) 2. Type "office" (embedding_2) 3. Clic sur OnlyOffice (embedding_3) 4. Clic "Nouveau document" (embedding_4) 5. Type "Bonjour" (embedding_5) 6. Clic "Fermer" (embedding_6) 7. Clic "Non" (embedding_7) # Jour 2 - 14h30 Session 2: 1. Clic sur lanceur (embedding_1') # Similaire Ă  embedding_1 2. Type "office" (embedding_2') 3. Clic sur OnlyOffice (embedding_3') 4. Clic "Nouveau document" (embedding_4') 5. Type "Hello" (embedding_5') # DiffĂ©rent mais mĂȘme type d'action 6. Clic "Fermer" (embedding_6') 7. Clic "Non" (embedding_7') # Jour 3 - 16h00 Session 3: 1. Clic sur lanceur (embedding_1'') 2. Type "office" (embedding_2'') 3. Clic sur OnlyOffice (embedding_3'') 4. Clic "Nouveau document" (embedding_4'') 5. Type "Test" (embedding_5'') 6. Clic "Fermer" (embedding_6'') 7. Clic "Non" (embedding_7'') # DĂ©tection compare_sessions(Session1, Session2) = 0.89 compare_sessions(Session2, Session3) = 0.91 compare_sessions(Session1, Session3) = 0.87 avg_similarity = 0.89 > 0.75 ✅ → Workflow "Ouvrir OnlyOffice et crĂ©er document" dĂ©tectĂ© ! ``` ### Jour 4 - Suggestion ```python # L'utilisateur commence Action 1: Clic sur lanceur Action 2: Type "office" # Le systĂšme dĂ©tecte match_score = match_workflow_start([Action1, Action2], Workflow.steps) match_score = 0.92 > 0.75 ✅ # Suggestion ! "Je peux continuer ce workflow pour toi ?" [Oui] [Non] [Corriger] ``` ## 🔧 ImplĂ©mentation ### Fichiers Ă  CrĂ©er 1. `geniusia2/core/workflow_detector.py` - DĂ©tection de workflows 2. `geniusia2/core/workflow_manager.py` - Gestion des workflows 3. `geniusia2/core/session_manager.py` - Segmentation en sessions ### Fichiers Ă  Modifier 1. `geniusia2/core/event_capture.py` - IntĂ©grer WorkflowDetector 2. `geniusia2/core/orchestrator.py` - Utiliser workflows au lieu de patterns 3. `geniusia2/core/suggestion_manager.py` - SuggĂ©rer workflows complets ## 📈 Avantages ### Avant (Micro-Patterns) - ❌ DĂ©tecte seulement les clics rĂ©pĂ©tĂ©s - ❌ Inutilisable en pratique - ❌ Ne correspond pas Ă  l'usage rĂ©el ### AprĂšs (Macro-Workflows) - ✅ DĂ©tecte les workflows complets - ✅ Utilisable en pratique - ✅ Correspond Ă  l'usage rĂ©el - ✅ Vrai RPA Vision ! ## 🎯 Prochaines Étapes 1. **ImplĂ©menter WorkflowDetector** (3-4h) 2. **ImplĂ©menter SessionManager** (2h) 3. **Modifier EventCapture** (1h) 4. **Tester avec ton workflow OnlyOffice** (1h) 5. **Ajuster les seuils** (1h) **Total** : ~8-10 heures de dĂ©veloppement ## 💡 Note Importante C'est un **changement majeur** mais **nĂ©cessaire** pour que le systĂšme soit vraiment utilisable ! Le systĂšme actuel est un MVP qui ne fonctionne que pour des cas d'usage artificiels. Avec cette refonte, on aura un **vrai RPA Vision** qui apprend des workflows rĂ©els ! 🚀 --- **Veux-tu qu'on commence l'implĂ©mentation ?** đŸ’Ș