feat: Enhance document synchronization by integrating CAIKnowledge handling and improving error logging
This commit is contained in:
@@ -242,78 +242,67 @@ class DocumentSync(BaseSyncUtils):
|
||||
entity_type: str = 'Document'
|
||||
) -> List[str]:
|
||||
"""
|
||||
Determine all xAI collection IDs of entities linked to this document.
|
||||
Determine all xAI collection IDs of CAIKnowledge entities linked to this document.
|
||||
|
||||
EspoCRM Many-to-Many: Document can be linked to arbitrary entities
|
||||
(CBeteiligte, Account, CVmhErstgespraech, etc.)
|
||||
Checks CAIKnowledgeCDokumente junction table:
|
||||
- Status 'active' + datenbankId: Returns collection ID
|
||||
- Status 'new': Returns "NEW:{knowledge_id}" marker (collection must be created first)
|
||||
- Other statuses (paused, deactivated): Skips
|
||||
|
||||
Args:
|
||||
document_id: Document ID
|
||||
entity_type: Entity type (e.g., 'CDokumente')
|
||||
|
||||
Returns:
|
||||
List of xAI collection IDs (deduplicated)
|
||||
List of collection IDs or markers:
|
||||
- Normal IDs: "abc123..." (existing collections)
|
||||
- New markers: "NEW:kb-id..." (collection needs to be created via knowledge sync)
|
||||
"""
|
||||
collections = set()
|
||||
|
||||
self._log(f"🔍 Checking relations of {entity_type} {document_id}...")
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════
|
||||
# SPECIAL HANDLING: CAIKnowledge via Junction Table
|
||||
# ═══════════════════════════════════════════════════════════════
|
||||
try:
|
||||
entity_def = await self.espocrm.get_entity_def(entity_type)
|
||||
links = entity_def.get('links', {}) if isinstance(entity_def, dict) else {}
|
||||
except Exception as e:
|
||||
self._log(f"⚠️ Konnte Metadata fuer {entity_type} nicht laden: {e}", level='warn')
|
||||
links = {}
|
||||
|
||||
link_types = {'hasMany', 'hasChildren', 'manyMany', 'hasManyThrough'}
|
||||
|
||||
for link_name, link_def in links.items():
|
||||
try:
|
||||
if not isinstance(link_def, dict):
|
||||
continue
|
||||
if link_def.get('type') not in link_types:
|
||||
continue
|
||||
|
||||
related_entity = link_def.get('entity')
|
||||
if not related_entity:
|
||||
continue
|
||||
|
||||
related_def = await self.espocrm.get_entity_def(related_entity)
|
||||
related_fields = related_def.get('fields', {}) if isinstance(related_def, dict) else {}
|
||||
|
||||
select_fields = ['id']
|
||||
if 'xaiCollectionId' in related_fields:
|
||||
select_fields.append('xaiCollectionId')
|
||||
|
||||
offset = 0
|
||||
page_size = 100
|
||||
|
||||
while True:
|
||||
result = await self.espocrm.list_related(
|
||||
entity_type,
|
||||
document_id,
|
||||
link_name,
|
||||
select=','.join(select_fields),
|
||||
offset=offset,
|
||||
max_size=page_size
|
||||
)
|
||||
|
||||
entities = result.get('list', [])
|
||||
if not entities:
|
||||
break
|
||||
|
||||
for entity in entities:
|
||||
collection_id = entity.get('xaiCollectionId')
|
||||
if collection_id:
|
||||
junction_entries = await self.espocrm.get_junction_entries(
|
||||
'CAIKnowledgeCDokumente',
|
||||
'cDokumenteId',
|
||||
document_id
|
||||
)
|
||||
|
||||
if junction_entries:
|
||||
self._log(f" 📋 Found {len(junction_entries)} CAIKnowledge link(s)")
|
||||
|
||||
for junction in junction_entries:
|
||||
knowledge_id = junction.get('cAIKnowledgeId')
|
||||
if not knowledge_id:
|
||||
continue
|
||||
|
||||
try:
|
||||
knowledge = await self.espocrm.get_entity('CAIKnowledge', knowledge_id)
|
||||
activation_status = knowledge.get('activationStatus')
|
||||
collection_id = knowledge.get('datenbankId')
|
||||
|
||||
if activation_status == 'active' and collection_id:
|
||||
# Existing collection - use it
|
||||
collections.add(collection_id)
|
||||
|
||||
if len(entities) < page_size:
|
||||
break
|
||||
offset += page_size
|
||||
|
||||
except Exception as e:
|
||||
self._log(f" ⚠️ Fehler beim Prüfen von Link {link_name}: {e}", level='warn')
|
||||
continue
|
||||
self._log(f" ✅ CAIKnowledge {knowledge_id}: {collection_id} (active)")
|
||||
elif activation_status == 'new':
|
||||
# Collection doesn't exist yet - return special marker
|
||||
# Format: "NEW:{knowledge_id}" signals to caller: trigger knowledge sync first
|
||||
collections.add(f"NEW:{knowledge_id}")
|
||||
self._log(f" 🆕 CAIKnowledge {knowledge_id}: status='new' → collection must be created first")
|
||||
else:
|
||||
self._log(f" ⏭️ CAIKnowledge {knowledge_id}: status={activation_status}, datenbankId={collection_id or 'N/A'}")
|
||||
|
||||
except Exception as e:
|
||||
self._log(f" ⚠️ Failed to load CAIKnowledge {knowledge_id}: {e}", level='warn')
|
||||
|
||||
except Exception as e:
|
||||
self._log(f" ⚠️ Failed to check CAIKnowledge junction: {e}", level='warn')
|
||||
|
||||
result = list(collections)
|
||||
self._log(f"📊 Gesamt: {len(result)} eindeutige Collection(s) gefunden")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user