mirror of
https://github.com/marcogll/talia_bot.git
synced 2026-01-13 13:25:19 +00:00
refactor: Modularize conversational flows into individual files
This commit refactors the conversational flow architecture by splitting the monolithic `flows.json` file into individual JSON files for each flow, located in the new `talia_bot/data/flows/` directory. Key changes: - **Modular Flow Files:** Each conversational flow is now an independent JSON file (e.g., `admin_check_agenda.json`), improving maintainability and making it easier to modify or add new flows. - **Updated Flow Engine:** The `FlowEngine` in `talia_bot/modules/flow_engine.py` has been updated to dynamically load all `.json` files from the `talia_bot/data/flows/` directory at startup. - **Removed Monolithic File:** The old `talia_bot/data/flows.json` file has been deleted, completing the transition to the new modular structure. This change enhances the organization of the project and aligns with the user's request for easier modification of individual flows.
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
# talia_bot/modules/flow_engine.py
|
# talia_bot/modules/flow_engine.py
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
from talia_bot.db import get_db_connection
|
from talia_bot.db import get_db_connection
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -10,23 +11,38 @@ class FlowEngine:
|
|||||||
self.flows = self._load_flows()
|
self.flows = self._load_flows()
|
||||||
|
|
||||||
def _load_flows(self):
|
def _load_flows(self):
|
||||||
"""Loads and flattens flow definitions from the JSON file."""
|
"""Loads all individual flow JSON files from the flows directory."""
|
||||||
|
flows_dir = 'talia_bot/data/flows'
|
||||||
|
loaded_flows = []
|
||||||
try:
|
try:
|
||||||
with open('talia_bot/data/flows.json', 'r', encoding='utf-8') as f:
|
if not os.path.exists(flows_dir):
|
||||||
all_flows_by_role = json.load(f)
|
logger.error(f"Flows directory not found at '{flows_dir}'")
|
||||||
|
return []
|
||||||
|
|
||||||
flattened_flows = []
|
for filename in os.listdir(flows_dir):
|
||||||
for role, data in all_flows_by_role.items():
|
if filename.endswith('.json'):
|
||||||
if 'flows' in data:
|
file_path = os.path.join(flows_dir, filename)
|
||||||
for flow in data['flows']:
|
try:
|
||||||
flow['role'] = role
|
with open(file_path, 'r', encoding='utf-8') as f:
|
||||||
flattened_flows.append(flow)
|
flow_data = json.load(f)
|
||||||
return flattened_flows
|
# Asignar un rol basado en el prefijo del nombre del archivo, si existe
|
||||||
except FileNotFoundError:
|
if filename.startswith('admin_'):
|
||||||
logger.error("flows.json not found.")
|
flow_data['role'] = 'admin'
|
||||||
return []
|
elif filename.startswith('crew_'):
|
||||||
except json.JSONDecodeError:
|
flow_data['role'] = 'crew'
|
||||||
logger.error("Error decoding flows.json.")
|
elif filename.startswith('client_'):
|
||||||
|
flow_data['role'] = 'client'
|
||||||
|
loaded_flows.append(flow_data)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
logger.error(f"Error decoding JSON from {filename}.")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error loading flow from {filename}: {e}")
|
||||||
|
|
||||||
|
logger.info(f"Successfully loaded {len(loaded_flows)} flows.")
|
||||||
|
return loaded_flows
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Failed to load flows from directory {flows_dir}: {e}")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
def get_flow(self, flow_id):
|
def get_flow(self, flow_id):
|
||||||
|
|||||||
Reference in New Issue
Block a user