KiProtokollService.php

Code Hygiene Score: 100

Keine Issues gefunden.

Dependencies 2

Klassen 1

Funktionen 6

Verwendet von 4

Versionen 1

Code

<?php

declare(strict_types=1);

namespace Infrastructure\Logging;

// @responsibility: Crash-safe wrapper for KI-Protokoll logging (SRP extraction)

use Domain\Repository\KiProtokollRepositoryInterface;

/**
 * KiProtokollService provides crash-safe logging to ki_dev.protokoll.
 *
 * ALL methods are wrapped in try-catch to ensure the Chat continues
 * even if protokoll logging fails. This is critical for production stability.
 */
final class KiProtokollService
{
    public function __construct(
        private KiProtokollRepositoryInterface $repository
    ) {
    }

    /**
     * Log a new LLM request (status: pending).
     *
     * @return int|null The protokoll ID, or null if logging failed
     */
    public function logRequest(
        string $clientName,
        string $request,
        string $model,
        string $requestIp
    ): ?int {
        try {
            return $this->repository->insert($clientName, $request, $model, $requestIp);
        } catch (\Throwable $e) {
            error_log('KiProtokoll logRequest failed: ' . $e->getMessage());

            return null;
        }
    }

    /**
     * Complete a pending protokoll entry with response data.
     * Silent fail - Chat continues regardless.
     */
    public function logSuccess(
        int $id,
        string $response,
        int $durationMs,
        ?int $tokensInput,
        ?int $tokensOutput
    ): void {
        try {
            $this->repository->complete($id, $response, $durationMs, $tokensInput, $tokensOutput);
        } catch (\Throwable $e) {
            error_log("KiProtokoll logSuccess failed for ID {$id}: " . $e->getMessage());
        }
    }

    /**
     * Mark a protokoll entry as failed.
     * Silent fail - Chat continues regardless.
     */
    public function logFailure(int $id, string $errorMessage): void
    {
        try {
            $this->repository->fail($id, $errorMessage);
        } catch (\Throwable $e) {
            error_log("KiProtokoll logFailure failed for ID {$id}: " . $e->getMessage());
        }
    }

    /**
     * Update request with full LLM prompt (after prompt construction).
     * Silent fail - Chat continues regardless.
     */
    public function updateFullPrompt(int $id, string $fullPrompt): void
    {
        try {
            $this->repository->updateRequest($id, $fullPrompt);
        } catch (\Throwable $e) {
            error_log("KiProtokoll updateFullPrompt failed for ID {$id}: " . $e->getMessage());
        }
    }

    /**
     * Clean up stale pending entries (cron job).
     *
     * @return int Number of cleaned up entries, or 0 on failure
     */
    public function cleanupStale(int $minutesOld = 10): int
    {
        try {
            return $this->repository->cleanupStale($minutesOld);
        } catch (\Throwable $e) {
            error_log('KiProtokoll cleanupStale failed: ' . $e->getMessage());

            return 0;
        }
    }
}
← Übersicht Graph