Backup #2117
| ID | 2117 |
| Dateipfad | /var/www/dev.campus.systemische-tools.de/src/Infrastructure/Logging/KiProtokollService.php |
| Version | 1 |
| Typ |
modified |
| Größe | 2.4 KB |
| Hash | 0a86ef143f7fa6e14aca6786a39130650518bdf8a50278fb3cbe82df8a133c1f |
| Datum | 2025-12-29 09:09:28 |
| Geändert von | claude-code-hook |
| Grund | Claude Code Pre-Hook Backup vor Edit-Operation |
| Datei existiert |
Ja
|
Dateiinhalt
<?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());
}
}
/**
* 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;
}
}
}
Vollständig herunterladen
Aktionen
← Zurück zur Übersicht