-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity_cleaners.py
93 lines (82 loc) · 3.63 KB
/
entity_cleaners.py
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
# entity_cleaners.py
from typing import Dict
import pydantic
import logging
from entity_registry import EntityRegistry
logger = logging.getLogger(__name__)
def clean_agent_data(agent: Dict) -> Dict:
"""Clean and validate agent data."""
cleaned = agent.copy()
# Ensure required fields
if 'name' not in cleaned:
logger.warning(f"Agent missing name: {cleaned}")
cleaned['name'] = 'Unknown Agent'
if 'traits' not in cleaned:
cleaned['traits'] = []
return cleaned
def clean_object_data(obj: Dict, entity_registry: EntityRegistry) -> Dict:
cleaned = obj.copy()
if cleaned.get('original_owner'):
original_owner = cleaned['original_owner']
# If original_owner is a pydantic model (e.g., an Agent),
# extract its .uuid and store that instead of the raw model.
if isinstance(original_owner, pydantic.BaseModel):
# Some BAML calls return a typed model, e.g. Agent(...)
if hasattr(original_owner, "uuid"):
owner_id = original_owner.uuid
cleaned['original_owner'] = entity_registry.normalizer.normalize_owner_reference(str(owner_id))
else:
# If there's no `.uuid` field, we have no valid reference
cleaned['original_owner'] = None
# Optionally check if the agent exists in our registry:
if not entity_registry.get_entity_details('agents', cleaned['original_owner']):
cleaned['original_owner'] = None
elif isinstance(original_owner, (str, dict)):
# Existing logic for a plain string or dict-based reference
owner_id = original_owner.get('uuid') if isinstance(original_owner, dict) else original_owner
if owner_id and owner_id != 'agent-':
cleaned['original_owner'] = entity_registry.normalizer.normalize_owner_reference(str(owner_id))
if not entity_registry.get_entity_details('agents', cleaned['original_owner']):
cleaned['original_owner'] = None
else:
cleaned['original_owner'] = None
else:
# If it's not a pydantic model, str, or dict, just set to None
cleaned['original_owner'] = None
return cleaned
def clean_location_data(loc: Dict) -> Dict:
"""Clean and validate location data."""
cleaned = loc.copy()
# Ensure required fields
if 'name' not in cleaned:
logger.warning(f"Location missing name: {cleaned}")
cleaned['name'] = 'Unknown Location'
if 'type' not in cleaned:
cleaned['type'] = 'Unspecified'
return cleaned
def clean_organization_data(org: Dict) -> Dict:
"""Clean and validate organization data."""
cleaned = org.copy()
# Ensure required fields
if 'name' not in cleaned:
logger.warning(f"Organization missing name: {cleaned}")
cleaned['name'] = 'Unknown Organization'
if 'members' not in cleaned:
cleaned['members'] = []
return cleaned
def clean_event_data(event: Dict, entity_registry: EntityRegistry) -> Dict:
"""Clean and validate event data."""
cleaned = event.copy()
# Validate agent participations
if 'agent_participations' in cleaned:
cleaned['agent_participations'] = [
ap for ap in cleaned['agent_participations']
if entity_registry.get_entity_details('agents', ap) is not None
]
# Validate object involvements
if 'object_involvements' in cleaned:
cleaned['object_involvements'] = [
oi for oi in cleaned['object_involvements']
if entity_registry.get_entity_details('objects', oi) is not None
]
return cleaned