ManageChatSessionsUseCase.php
- Pfad:
src/UseCases/Chat/ManageChatSessionsUseCase.php
- Namespace: UseCases\Chat
- Zeilen: 280 | Größe: 8,463 Bytes
- Geändert: 2025-12-27 23:21:30 | Gescannt: 2025-12-31 10:22:15
Code Hygiene Score: 57
- Dependencies: 60 (25%)
- LOC: 35 (20%)
- Methods: 0 (20%)
- Secrets: 100 (15%)
- Classes: 100 (10%)
- Magic Numbers: 100 (10%)
Issues 1
| Zeile |
Typ |
Beschreibung |
| - |
srp |
Klasse hat 24 Methoden (max: 20) |
Dependencies 16
- constructor Domain\Repository\ChatSessionRepositoryInterface
- constructor Domain\Repository\ChatMessageRepositoryInterface
- constructor Infrastructure\Persistence\ContentConfigRepository
- constructor Domain\Repository\CollectionRepositoryInterface
- constructor Infrastructure\Validation\CollectionValidator
- constructor PDO
- constructor Infrastructure\AI\ModelRegistry
- use Domain\Constants
- use Domain\Entity\ChatMessage
- use Domain\Entity\ChatSession
- use Domain\Repository\ChatMessageRepositoryInterface
- use Domain\Repository\ChatSessionRepositoryInterface
- use Domain\Repository\CollectionRepositoryInterface
- use Infrastructure\AI\ModelRegistry
- use Infrastructure\Persistence\ContentConfigRepository
- use Infrastructure\Validation\CollectionValidator
Klassen 1
-
ManageChatSessionsUseCase
class
Zeile 19
Funktionen 24
-
__construct()
public
Zeile 23
-
createSession()
public
Zeile 34
-
getSession()
public
Zeile 42
-
getAllSessions()
public
Zeile 52
-
getAllSessionsWithStats()
public
Zeile 62
-
getMessages()
public
Zeile 72
-
updateTitle()
public
Zeile 77
-
updateSettings()
public
Zeile 89
-
updateSystemPrompt()
public
Zeile 116
-
deleteSession()
public
Zeile 128
-
getAuthorProfiles()
public
Zeile 133
-
getSystemPrompts()
public
Zeile 138
-
getOutputStructures()
public
Zeile 143
-
getStructure()
public
Zeile 148
-
getAvailableCollections()
public
Zeile 153
-
validateCollectionCompatibility()
public
Zeile 168
-
getDefaultSystemPrompt()
public
Zeile 182
-
settingsHaveChanged()
public
Zeile 200
-
validateCollections()
private
Zeile 217
-
validateContextLimit()
private
Zeile 230
-
validateAuthorProfileId()
private
Zeile 237
-
validateTemperature()
private
Zeile 248
-
validateMaxTokens()
private
Zeile 253
-
logSystemPromptChange()
private
Zeile 260
Versionen 14
-
v14
2025-12-27 23:21 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation
-
v13
2025-12-27 23:20 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation
-
v12
2025-12-25 17:00 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation
-
v11
2025-12-25 13:29 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation
-
v10
2025-12-25 13:29 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation
-
v9
2025-12-25 10:41 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation
-
v8
2025-12-25 10:40 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation
-
v7
2025-12-25 09:45 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation
-
v6
2025-12-25 09:41 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation
-
v5
2025-12-25 09:41 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation
-
v4
2025-12-25 09:41 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation
-
v3
2025-12-23 08:16 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation
-
v2
2025-12-23 07:55 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation
-
v1
2025-12-23 02:37 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation
Code
<?php
declare(strict_types=1);
namespace UseCases\Chat;
// @responsibility: Verwaltet Chat-Sessions (CRUD, Einstellungen, Collections)
use Domain\Constants;
use Domain\Entity\ChatMessage;
use Domain\Entity\ChatSession;
use Domain\Repository\ChatMessageRepositoryInterface;
use Domain\Repository\ChatSessionRepositoryInterface;
use Domain\Repository\CollectionRepositoryInterface;
use Infrastructure\AI\ModelRegistry;
use Infrastructure\Persistence\ContentConfigRepository;
use Infrastructure\Validation\CollectionValidator;
final class ManageChatSessionsUseCase
{
private ?array $collectionsCache = null;
public function __construct(
private ChatSessionRepositoryInterface $sessionRepo,
private ChatMessageRepositoryInterface $messageRepo,
private ContentConfigRepository $configRepo,
private CollectionRepositoryInterface $collectionRepo,
private CollectionValidator $collectionValidator,
private \PDO $pdoDev,
private ModelRegistry $modelRegistry
) {
}
public function createSession(): string
{
$uuid = $this->sessionRepo->generateUuid();
$this->sessionRepo->create($uuid, 'claude-opus-4-5-20251101', '["documents"]', 5);
return $uuid;
}
public function getSession(string $uuid): ?ChatSession
{
return $this->sessionRepo->findByUuid($uuid);
}
/**
* Get all sessions as entities.
*
* @return array<int, ChatSession>
*/
public function getAllSessions(int $limit = 50): array
{
return $this->sessionRepo->findAll($limit);
}
/**
* Get all sessions with stats as arrays (for list views).
*
* @return array<int, array<string, mixed>>
*/
public function getAllSessionsWithStats(int $limit = 50): array
{
return $this->sessionRepo->findAllWithStats($limit);
}
/**
* Get messages for a session.
*
* @return array<int, ChatMessage>
*/
public function getMessages(int $sessionId): array
{
return $this->messageRepo->findBySessionId($sessionId);
}
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 deleteSession(int $sessionId): void
{
$this->sessionRepo->delete($sessionId);
}
public function getAuthorProfiles(): array
{
return $this->configRepo->getAuthorProfiles();
}
public function getSystemPrompts(): array
{
return $this->configRepo->getSystemPrompts();
}
public function getOutputStructures(): array
{
return $this->configRepo->getStructures();
}
public function getStructure(int $id): ?array
{
return $this->configRepo->getStructure($id);
}
public function getAvailableCollections(): array
{
if ($this->collectionsCache === null) {
$this->collectionsCache = $this->collectionRepo->getSearchable();
if ($this->collectionsCache === []) {
$this->collectionsCache = [
['collection_id' => 'documents', 'display_name' => 'Dokumente', 'points_count' => 0, 'vector_size' => 1024],
];
}
}
return $this->collectionsCache;
}
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(),
];
}
public function getDefaultSystemPrompt(): string
{
return <<<'PROMPT'
Du bist ein hilfreicher Assistent für Fragen zu systemischem Teamcoaching und Teamentwicklung.
Beantworte die Frage des Nutzers basierend auf dem bereitgestellten Kontext.
- Antworte auf Deutsch
- Sei präzise und hilfreich
- Wenn der Kontext die Frage nicht beantwortet, sage das ehrlich
- Verweise auf die Quellen wenn passend
PROMPT;
}
/**
* Check if settings have changed.
*
* @param array<string> $collections
*/
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();
}
private function validateCollections(array|string $collections): array
{
$availableIds = array_column($this->getAvailableCollections(), '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
}
}
}