UpdateChatSessionUseCase.php
- Pfad:
src/UseCases/Chat/UpdateChatSessionUseCase.php - Namespace: UseCases\Chat
- Zeilen: 176 | Größe: 5,544 Bytes
- Geändert: 2025-12-27 23:21:38 | Gescannt: 2025-12-31 10:22:15
Code Hygiene Score: 88
- Dependencies: 80 (25%)
- LOC: 87 (20%)
- Methods: 80 (20%)
- Secrets: 100 (15%)
- Classes: 100 (10%)
- Magic Numbers: 100 (10%)
Keine Issues gefunden.
Dependencies 14
- implements UseCases\Chat\UpdateChatSessionUseCaseInterface
- constructor Domain\Repository\ChatSessionRepositoryInterface
- constructor Domain\Repository\ContentConfigRepositoryInterface
- constructor Domain\Repository\CollectionRepositoryInterface
- constructor Infrastructure\Validation\CollectionValidator
- constructor Domain\Service\ModelRegistryInterface
- constructor PDO
- use Domain\Constants
- use Domain\Entity\ChatSession
- use Domain\Repository\ChatSessionRepositoryInterface
- use Domain\Repository\CollectionRepositoryInterface
- use Domain\Repository\ContentConfigRepositoryInterface
- use Domain\Service\ModelRegistryInterface
- use Infrastructure\Validation\CollectionValidator
Klassen 1
-
UpdateChatSessionUseCaseclass Zeile 17
Funktionen 12
-
__construct()public Zeile 19 -
updateTitle()public Zeile 29 -
updateSettings()public Zeile 43 -
updateSystemPrompt()public Zeile 70 -
settingsHaveChanged()public Zeile 82 -
validateCollectionCompatibility()public Zeile 99 -
validateCollections()private Zeile 113 -
validateContextLimit()private Zeile 126 -
validateAuthorProfileId()private Zeile 133 -
validateTemperature()private Zeile 144 -
validateMaxTokens()private Zeile 149 -
logSystemPromptChange()private Zeile 156
Verwendet von 1
Versionen 2
-
v2
2025-12-27 23:21 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v1
2025-12-27 23:21 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation
Code
<?php
declare(strict_types=1);
namespace UseCases\Chat;
// @responsibility: Updates chat session data
use Domain\Constants;
use Domain\Entity\ChatSession;
use Domain\Repository\ChatSessionRepositoryInterface;
use Domain\Repository\CollectionRepositoryInterface;
use Domain\Repository\ContentConfigRepositoryInterface;
use Domain\Service\ModelRegistryInterface;
use Infrastructure\Validation\CollectionValidator;
final class UpdateChatSessionUseCase implements UpdateChatSessionUseCaseInterface
{
public function __construct(
private ChatSessionRepositoryInterface $sessionRepo,
private ContentConfigRepositoryInterface $configRepo,
private CollectionRepositoryInterface $collectionRepo,
private CollectionValidator $collectionValidator,
private ModelRegistryInterface $modelRegistry,
private \PDO $pdoDev
) {
}
public function updateTitle(int $sessionId, string $title): string
{
$title = trim($title);
if ($title === '') {
$title = 'Neuer Chat';
}
$title = mb_substr($title, 0, Constants::NAME_MAX_LENGTH);
$this->sessionRepo->updateTitle($sessionId, $title);
return $title;
}
public function updateSettings(
int $sessionId,
string $model,
array $collections,
int $contextLimit,
int $authorProfileId,
float $temperature,
int $maxTokens
): void {
$model = $this->modelRegistry->isValid($model) ? $model : $this->modelRegistry->getDefaultChatModel();
$collections = $this->validateCollections($collections);
$contextLimit = $this->validateContextLimit($contextLimit);
$authorProfileId = $this->validateAuthorProfileId($authorProfileId);
$temperature = $this->validateTemperature($temperature);
$maxTokens = $this->validateMaxTokens($maxTokens);
$this->sessionRepo->updateSettings(
$sessionId,
$model,
$collections,
$contextLimit,
$authorProfileId > 0 ? $authorProfileId : null,
$temperature,
$maxTokens
);
}
public function updateSystemPrompt(int $sessionId, string $systemPrompt): ChatSessionResult
{
$maxLength = 2000;
$wasTruncated = mb_strlen($systemPrompt) > $maxLength;
$systemPrompt = trim(mb_substr($systemPrompt, 0, $maxLength));
$this->logSystemPromptChange($systemPrompt, $wasTruncated);
$this->sessionRepo->updateSystemPrompt($sessionId, $systemPrompt);
return ChatSessionResult::success('System-Prompt gespeichert.');
}
public function settingsHaveChanged(
ChatSession $session,
string $model,
array $collections,
int $contextLimit,
int $authorProfileId,
float $temperature,
int $maxTokens
): bool {
return $model !== $session->getModel()
|| $collections !== $session->getCollections()
|| $contextLimit !== $session->getContextLimit()
|| $authorProfileId !== ($session->getAuthorProfileId() ?? 0)
|| $temperature !== $session->getTemperature()
|| $maxTokens !== $session->getMaxTokens();
}
public function validateCollectionCompatibility(array $collectionIds): array
{
if (empty($collectionIds)) {
return ['valid' => true, 'error' => null];
}
$result = $this->collectionValidator->validateSelection($collectionIds);
return [
'valid' => $result->isValid(),
'error' => $result->getError(),
];
}
private function validateCollections(array|string $collections): array
{
$availableIds = array_column($this->collectionRepo->getSearchable(), 'collection_id');
if (is_string($collections)) {
$collections = [$collections];
}
$valid = array_filter($collections, fn ($c) => in_array($c, $availableIds, true));
return array_values($valid);
}
private function validateContextLimit(int $limit): int
{
$allowedLimits = [3, 5, 10, 15];
return in_array($limit, $allowedLimits, true) ? $limit : 5;
}
private function validateAuthorProfileId(int $profileId): int
{
if ($profileId === 0) {
return 0;
}
$profile = $this->configRepo->getAuthorProfile($profileId);
return $profile !== null ? $profileId : 0;
}
private function validateTemperature(float $temperature): float
{
return max(0.0, min(1.0, $temperature));
}
private function validateMaxTokens(int $maxTokens): int
{
$allowedValues = [1024, 2048, 4096, 8192];
return in_array($maxTokens, $allowedValues, true) ? $maxTokens : 4096;
}
private function logSystemPromptChange(string $prompt, bool $wasTruncated): void
{
$logData = [
'prompt_length' => mb_strlen($prompt),
'truncated' => $wasTruncated,
'ip' => $_SERVER['REMOTE_ADDR'] ?? 'unknown',
'timestamp' => date('c'),
];
try {
$stmt = $this->pdoDev->prepare(
"INSERT INTO mcp_log (tool, operation, parameters, result, logged_at)
VALUES ('chat', 'system_prompt_change', :params, 'logged', NOW())"
);
$stmt->execute(['params' => json_encode($logData)]);
} catch (\Exception $e) {
// Silently fail
}
}
}