KAI System-Kompendium

Vollständige technische Referenz aller Module, Endpunkte und Logiken des KAI Backends.

Status: Vollständig integriert | System: Flask / Celery / Redis / Tesseract / SpaCy

🧩 Parsing-Profile

GET /parsing/profiles Profil-Manager
Funktion: Listet alle vorhandenen Parsing-Profile auf.
Logik: Liest die JSON-Dateien aus dem Ordner parsings/ (früher Redis Set).
POST /parsing/analyze-sources Profile Parsing (KI-Analyse)
Funktion: Analysiert E-Mail-Konten auf Geschäftskontakte.
Logik: Startet Celery-Task. Scannt IMAP/Gmail/Outlook nach Keywords ("Rechnung"). Extrahiert Namen/E-Mails via SpaCy (NLP) und schlägt diese für die Einnahmen/Ausgaben-Listen vor.

🧠 Rechnungsverarbeitung

POST /ai-invoices/batch/run-ai-workflow KI-Workflow: Rechnungen holen
Funktion: Phase 1 des Hauptprozesses.
Logik: Iteriert über konfigurierte Quellen. Lädt Anhänge/E-Mails. Führt OCR aus. Validiert "Ist es eine Rechnung?". Speichert standardisiertes PDF mit Meta-Header in uploads/. Nutzt Profil-Patterns.
POST /workspace/run-cleanup-workflow Workflow: Aufräumen & Sortieren
Funktion: Phase 2 - Organisation.
Logik: Analysiert Dateien in uploads/. Gruppiert zusammengehörige Seiten. Löscht Duplikate. Sortiert nach Jahr/Monat/Kategorie. Benennt Dateien um (Prefix: einnahme_/ausgabe_).
POST /lexoffice-analyzer/batch/analyze-folder-for-lexoffice-csv KI-Workflow: Ordner analysieren
Funktion: Phase 3 - Finaler Export.
Logik: Geht durch einen sortierten Monatsordner. Extrahiert finale Buchungsdaten. Erstellt eine CSV für Lexoffice. Nutzt KI-Fallback für fehlende Adressdaten.
POST /lexoffice/batch/run-full-workflow rechnungen holen workflow guided
Funktion: Der "Big Red Button".
Logik: Kombiniert Phase 1, 2 und 3 in einer State-Machine, um Deadlocks zu vermeiden. Führt den kompletten Prozess von E-Mail bis CSV durch.

🏦 Kontoauszüge

POST /statement/batch/process-bank-statement extrakt kontoauszug datei hochladen
Funktion: Einzelverarbeitung PDF.
Logik: Zerlegt PDF-Kontoauszug via Regex in CSV-Buchungszeilen (Datum, Betrag, Verwendungszweck).
POST /statement/batch/process-folder extrakt kontoauszüge ordner verarbeiten
Funktion: Massenverarbeitung.
Logik: Fügt alle PDFs eines Ordners zusammen, analysiert den Stream und archiviert die Originale.

⚙️ System & Core

DELETE /api/jobs/cancel/{job_id} cancel job
Funktion: Bricht Task ab.
Logik: Sendet SIGKILL an den Worker-Prozess und setzt Redis-Flag.
DELETE /api/jobs/clear clear all jobs
Funktion: Bereinigt Job-Historie.
Logik: Löscht alle job:* Keys aus Redis.
GET /api/message-sources get available email accounts
Funktion: Listet Quellen.
Logik: Aggregiert Google-Tokens, IMAP-Configs und Microsoft-Tokens aus Redis.
GET /api/jobs/result/{job_id} get job status
Funktion: Polling für Frontend.
Logik: Liest JSON-Status aus Redis. Gibt Fortschritt (Progress/Total) zurück.
GET /api/system/stats get system stats
Funktion: Dashboard-Daten.
Logik: Zählt Dateien in Ordnern, prüft Auth-Status, prüft Worker-Verfügbarkeit.
GET /api/models list ai models
Funktion: Zeigt KI-Modelle.
Logik: Liest config.json.
GET /api/jobs/list list all jobs
Funktion: Job-Übersicht.
Logik: Iteriert über alle Redis-Job-Keys.

🔍 Diagnose & Entwicklung

POST /api/debug/extract-raw-text extract raw text for patterns
Funktion: Regex-Tester.
Logik: Lädt E-Mails basierend auf Suchquery. Führt OCR durch. Gibt rohen Text zurück (ohne Parsing). Ideal um Patterns zu testen.

🛒 Shopware Features

POST /shopware/accounts add/list/delete shopware account
Funktion: Verwaltung DB-Zugang.
Logik: Speichert MySQL-Credentials sicher in Redis.
POST /shopware-combine/batch/combine-csv-workflow Workflow: CSVs kombinieren
Funktion: Merge-Tool.
Logik: Liest Shopware-Export und manuell erstellte Monats-CSV. Vergleicht Rechnungsnummern. Entfernt Duplikate. Erstellt Master-CSV.
POST /shopware/batch/extract-and-export-csv shopware export orders to csv
Funktion: SQL-Direktexport.
Logik: Verbindet direkt mit MySQL. Führt komplexe Joins über Order/Customer/Address aus. Filtert nach Status-IDs. Exportiert CSV für Lexoffice.
POST /shopware/batch/upload-csv-to-lexoffice shopware upload csv to lexoffice
Funktion: Import-Bridge.
Logik: Nimmt eine lokale CSV und speist sie in den Lexoffice-Voucher-Import ein.

💻 WHMCS Features

POST /whmcs/accounts add/delete/list whmcs accounts
Funktion: Account-Verwaltung.
FE API Explorer
Funktion: Generischer API Client.
Logik: Frontend baut dynamische Formulare. Backend signiert Requests an die WHMCS API (z.B. `GetClients`, `GetInvoices`, `CreateTicket`).

📧 IMAP Features

POST /imap/accounts add/delete/list imap accounts
Funktion: Konfiguration.
POST /imap/account/{name}/folder/clear empty imap folder
Funktion: Ordner leeren.
Logik: Löscht alle E-Mails in einem spezifischen Ordner (Batch-Delete).
POST /imap/batch/deep-scan-invoices imap deep scan invoices
Funktion: Tiefensuche.
Logik: Sucht nach Keywords im Body und Betreff, auch wenn keine Anhänge markiert sind.
POST /imap/batch/extract-invoices imap extract and save invoices
Funktion: Standard-Extraktion.
Logik: Speichert Anhänge (PDF/Img) von E-Mails, die Kriterien erfüllen.
POST /imap/batch/find-and-move imap find and move emails
Funktion: Organisator.
Logik: Sucht E-Mails anhand Query und verschiebt sie serverseitig in einen anderen Ordner.
POST /imap/batch/intelligent-extract imap intelligent extract invoices
Funktion: KI-Extraktion.
Logik: Nutzt LLM, um zu entscheiden, ob eine E-Mail relevant ist, auch wenn Keywords fehlen.
GET /imap/account/{name}/folders list imap folders
Funktion: Ordnerstruktur.
POST /imap/account/{name}/message/flags modify imap flags
Funktion: Status ändern.
Logik: Setzt Flags wie `\Seen`, `\Flagged` oder Custom Labels.
POST /imap/batch/search-messages search emails imap
Funktion: Suche.
Logik: Führt server-seitige Suche (SEARCH command) aus. Unterstützt UTF-8.

🇬 Google Features

POST /google/accounts/authorize add google account
Funktion: OAuth2 Flow.
POST /google/calendar/create create calendar event
Funktion: Termin.
Logik: Erstellt Event, optional mit Google Meet Link.
DELETE /google/calendar/event/{id} delete calendar event
Funktion: Termin löschen.
DELETE /google/drive/file/{id} delete drive file
Funktion: Drive Cleanup.
POST /google/batch/extract-invoices google gmail extract invoices
Funktion: Gmail Extraktion.
Logik: Nutzt Gmail API (schneller als IMAP). Filtert nach `has:attachment`.
GET /google/calendar/events list calendar events
Funktion: Kalender lesen.
GET /google/contacts/list list google contacts
Funktion: Kontakte lesen.
Logik: Nutzt People API.
GET /google/drive/files search drive files
Funktion: Drive Suche.
Logik: Sucht Dateien in Google Drive per Query.
POST /google/gmail/search search emails google
Funktion: Gmail Suche.
POST /google/gmail/send send gmail email
Funktion: Versand.
Logik: Sendet E-Mail via Gmail API (auch mit Anhängen aus Drive).

🇲 Microsoft Features

POST /microsoft/accounts/authorize add microsoft account
Funktion: OAuth2 Flow (Graph API).
POST /microsoft/calendar/create create microsoft calendar event
Funktion: Termin erstellen.
POST /microsoft/meet/create create teams meeting
Funktion: Teams Meeting.
Logik: Erstellt ein Online-Meeting in Teams und gibt den Link zurück.
DELETE /microsoft/drive/file/{id} delete onedrive file
Funktion: OneDrive Löschen.
POST /microsoft/batch/extract-invoices microsoft outlook extract invoices
Funktion: Outlook Extraktion.
Logik: Nutzt Microsoft Graph API für Outlook Nachrichten und Anhänge.
POST /microsoft/outlook/search search emails microsoft
Funktion: Outlook Suche.
GET /microsoft/drive/files search onedrive files
Funktion: OneDrive Suche.
POST /microsoft/outlook/send send outlook email
Funktion: Versand via Outlook.

✉️ E-Mail & SMTP

POST /smtp/accounts add/delete/list smtp accounts
Funktion: Verwaltung.
POST /smtp/send send email unified
Funktion: Zentraler Versand.
Logik: Router. Sendet via SMTP (smtplib) oder API (Gmail), je nach Konto-Typ.

📱 WhatsApp Features

POST /whatsapp/accounts add/delete/list whatsapp accounts
Funktion: Konfiguration API Token.
POST /whatsapp/batch/extract-invoices whatsapp extract invoices
Funktion: Workflow-Integration.
Logik: Simulierter Task, der bestätigt, dass WhatsApp Webhooks aktiv sind (Realtime-Empfang).

⇄ FTP Features

POST /ftp/accounts add/list/delete ftp account
Funktion: Verwaltung.
POST /ftp/batch/analyze-invoice ftp analyze invoice
Funktion: Remote Analyse.
Logik: Lädt Datei in RAM, analysiert sie und gibt JSON zurück, ohne sie dauerhaft zu speichern.
POST /ftp/batch/extract-invoices ftp extract and save invoices
Funktion: Download.
Logik: Lädt alle passenden Dateien vom FTP in den lokalen Workspace.
POST /ftp/download-to-workspace download ftp file to workspace
Funktion: Einzeldatei Download.
POST /ftp/upload-from-workspace upload workspace file to ftp
Funktion: Upload.

🧾 Lexoffice Features

POST /lexoffice/accounts add/delete/list lexoffice accounts
Funktion: API Keys verwalten.
POST /lexoffice/account/{name}/vouchers create voucher
Funktion: Beleg erstellen.
POST /lexoffice/export/csv export invoices to lexoffice csv
Funktion: Export.
Logik: Konvertiert interne JSON-Rechnungsdaten in das Lexoffice CSV-Format.
POST /lexoffice/account/{name}/get-or-create-contact get or create contact
Funktion: Kontaktmanagement.
Logik: Sucht Kontakt nach Namen. Wenn nicht gefunden, erstellt neu.
POST /lexoffice/batch/import-csv import csv and book
Funktion: Massenbuchung.
Logik: Liest CSV, iteriert Zeilen, erstellt für jede Zeile einen Beleg in Lexoffice.
POST /lexoffice/batch/process-and-book-invoice process and book invoice
Funktion: Einzelbuchung.
Logik: Upload Datei -> Erstelle Beleg -> Verknüpfe Datei.

📁 Workspace Features

POST /workspace/batch/analyze-invoice workspace analyze invoice
Funktion: Lokale Analyse.
Logik: Analysiert eine Datei im Dateisystem ohne Cloud-Upload.
POST /workspace/batch/extract-invoices workspace extract invoices
Funktion: Lokale Extraktion.
Logik: Durchsucht Ordner nach Dokumenten, prüft Rechnungsmerkmale, sortiert in Zielordner.
POST /workspace/move move workspace file
Funktion: Dateioperation.
POST /workspace/upload/{folder} upload workspace file
Funktion: Upload.