feat(sync): Implement orphan cleanup for xAI documents without EspoCRM equivalents

This commit is contained in:
bsiggel
2026-03-26 14:20:33 +00:00
parent 1e202a6233
commit c9bdd021e4
3 changed files with 142 additions and 52 deletions

View File

@@ -438,6 +438,22 @@ async def _run_xai_sync(
ctx.logger.info(f" Documents to check: {len(docs)}")
# ── Orphan-Cleanup: xAI-Docs löschen die kein EspoCRM-Äquivalent haben ──
known_xai_file_ids = {doc.get('aiFileId') for doc in docs if doc.get('aiFileId')}
try:
xai_docs = await xai.list_collection_documents(collection_id)
orphans = [d for d in xai_docs if d.get('file_id') not in known_xai_file_ids]
if orphans:
ctx.logger.info(f" 🗑️ Orphan-Cleanup: {len(orphans)} Doc(s) in xAI ohne EspoCRM-Eintrag")
for orphan in orphans:
try:
await xai.remove_from_collection(collection_id, orphan['file_id'])
ctx.logger.info(f" Gelöscht: {orphan.get('filename', orphan['file_id'])}")
except Exception as e:
ctx.logger.warn(f" Orphan-Delete fehlgeschlagen: {e}")
except Exception as e:
ctx.logger.warn(f" ⚠️ Orphan-Cleanup fehlgeschlagen (non-fatal): {e}")
synced = 0
skipped = 0
failed = 0