Backup #714

ID714
Dateipfad/var/www/dev.campus.systemische-tools.de/src/UseCases/Chat/ExportChatSessionUseCase.php
Version1
Typ modified
Größe4.4 KB
Hashf65a0f26f33e589c5a9e578686bb06e9b56e67f10f2ea05cb46d48df069a2145
Datum2025-12-23 07:55:29
Geändert vonclaude-code-hook
GrundClaude Code Pre-Hook Backup vor Edit-Operation
Datei existiert Ja

Dateiinhalt

<?php

namespace UseCases\Chat;

use Infrastructure\Persistence\ChatMessageRepository;
use Infrastructure\Persistence\ChatSessionRepository;

final class ExportChatSessionUseCase
{
    private ChatSessionRepository $sessionRepo;
    private ChatMessageRepository $messageRepo;

    public function __construct(
        ?ChatSessionRepository $sessionRepo = null,
        ?ChatMessageRepository $messageRepo = null
    ) {
        $this->sessionRepo = $sessionRepo ?? new ChatSessionRepository();
        $this->messageRepo = $messageRepo ?? new ChatMessageRepository();
    }

    /**
     * Export session as Markdown.
     */
    public function exportAsMarkdown(string $uuid): ?string
    {
        $session = $this->sessionRepo->findByUuid($uuid);

        if ($session === null) {
            return null;
        }

        $messages = $this->messageRepo->findBySessionId((int) $session['id']);
        $title = $session['title'] ?? 'Chat';
        $date = date('d.m.Y H:i');
        $model = $session['model'] ?? 'unbekannt';

        $md = "# {$title}\n";
        $md .= "*Exportiert am {$date}*\n";
        $md .= "*Modell: {$model}*\n\n";
        $md .= "---\n\n";

        $questionNum = 0;
        foreach ($messages as $msg) {
            if ($msg['role'] === 'user') {
                $questionNum++;
                $md .= "## Frage {$questionNum}\n\n";
                $md .= $msg['content'] . "\n\n";
            } else {
                $md .= "## Antwort {$questionNum}\n\n";
                $md .= $msg['content'] . "\n\n";

                $sources = json_decode($msg['sources'] ?? '[]', true) ?: [];
                if (!empty($sources)) {
                    $md .= "**Quellen:**\n";
                    foreach ($sources as $source) {
                        $title = $source['title'] ?? 'Unbekannt';
                        $score = isset($source['score']) ? round($source['score'] * 100) . '%' : '';
                        $collection = isset($source['collection']) ? "[{$source['collection']}] " : '';
                        $md .= "- {$collection}{$title} ({$score})\n";
                    }
                    $md .= "\n";
                }

                $md .= "---\n\n";
            }
        }

        return $md;
    }

    /**
     * Export session as JSON.
     *
     * @return array<string, mixed>|null
     */
    public function exportAsJson(string $uuid): ?array
    {
        $session = $this->sessionRepo->findByUuid($uuid);

        if ($session === null) {
            return null;
        }

        $messages = $this->messageRepo->findBySessionId((int) $session['id']);

        $exportMessages = [];
        foreach ($messages as $msg) {
            $exportMsg = [
                'role' => $msg['role'],
                'content' => $msg['content'],
                'created_at' => $msg['created_at'] ?? null,
            ];

            if ($msg['role'] === 'assistant') {
                $sources = json_decode($msg['sources'] ?? '[]', true) ?: [];
                $exportMsg['sources'] = $sources;
                $exportMsg['tokens'] = [
                    'input' => (int) ($msg['tokens_input'] ?? 0),
                    'output' => (int) ($msg['tokens_output'] ?? 0),
                ];
                $exportMsg['model'] = $msg['model'] ?? null;
            }

            $exportMessages[] = $exportMsg;
        }

        return [
            'session' => [
                'uuid' => $session['uuid'],
                'title' => $session['title'] ?? 'Chat',
                'created_at' => $session['created_at'] ?? null,
                'model' => $session['model'] ?? null,
                'collections' => json_decode($session['collections'] ?? '[]', true),
            ],
            'messages' => $exportMessages,
            'exported_at' => date('c'),
        ];
    }

    /**
     * Generate filename for export.
     */
    public function generateFilename(string $uuid, string $format): string
    {
        $session = $this->sessionRepo->findByUuid($uuid);
        $title = $session['title'] ?? 'chat';

        // Sanitize title for filename
        $title = preg_replace('/[^a-zA-Z0-9äöüÄÖÜß\-_\s]/u', '', $title);
        $title = preg_replace('/\s+/', '-', trim($title));
        $title = mb_substr($title, 0, 50);

        if ($title === '') {
            $title = 'chat';
        }

        $date = date('Y-m-d');
        $ext = $format === 'json' ? 'json' : 'md';

        return "chat-{$title}-{$date}.{$ext}";
    }
}

Vollständig herunterladen

Aktionen

Herunterladen

Andere Versionen dieser Datei

ID Version Typ Größe Datum
1843 9 modified 4.7 KB 2025-12-27 23:21
1840 8 modified 4.7 KB 2025-12-27 23:21
1499 7 modified 4.7 KB 2025-12-25 17:32
1498 6 modified 4.7 KB 2025-12-25 17:32
1141 5 modified 4.7 KB 2025-12-25 09:44
1140 4 modified 4.5 KB 2025-12-25 09:43
1139 3 modified 4.3 KB 2025-12-25 09:43
843 2 modified 4.5 KB 2025-12-23 08:17
714 1 modified 4.4 KB 2025-12-23 07:55

← Zurück zur Übersicht