Backup #1184
| ID | 1184 |
| Dateipfad | /var/www/dev.campus.systemische-tools.de/src/Infrastructure/Persistence/ChatMessageRepository.php |
| Version | 3 |
| Typ |
modified |
| Größe | 4.5 KB |
| Hash | cbb9544a7e7049636c3bb035f56ae51832c032ffe2f5edb6e2baf7802bbf6187 |
| Datum | 2025-12-25 10:32:37 |
| 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\Persistence;
// @responsibility: Persistenz für Chat-Nachrichten (CRUD, Session-Filter)
use Domain\Entity\ChatMessage;
use Domain\Repository\ChatMessageRepositoryInterface;
use Infrastructure\Config\DatabaseFactory;
class ChatMessageRepository implements ChatMessageRepositoryInterface
{
private \PDO $pdo;
public function __construct(?\PDO $pdo = null)
{
$this->pdo = $pdo ?? DatabaseFactory::content();
}
public function findBySessionId(int $sessionId): array
{
$stmt = $this->pdo->prepare(
'SELECT * FROM chat_messages WHERE session_id = ? ORDER BY created_at ASC'
);
$stmt->execute([$sessionId]);
$messages = [];
foreach ($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) {
$messages[] = ChatMessage::fromArray($row);
}
return $messages;
}
public function saveEntity(ChatMessage $message): int
{
$data = $message->toArray();
unset($data['id']);
$stmt = $this->pdo->prepare(
'INSERT INTO chat_messages
(session_id, role, content, model, tokens_input, tokens_output, sources,
start_microtime, end_microtime, author_profile_id, system_prompt_id, collections, context_limit)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
);
$stmt->execute([
$message->getSessionId(),
$message->getRole()->value,
$message->getContent(),
$message->getModel(),
$message->getTokensInput(),
$message->getTokensOutput(),
$message->getSources() !== null ? json_encode($message->getSources()) : null,
$message->getStartMicrotime(),
$message->getEndMicrotime(),
$message->getAuthorProfileId(),
$message->getSystemPromptId(),
$message->getCollections() !== null ? json_encode($message->getCollections()) : null,
$message->getContextLimit(),
]);
// Update session timestamp
$this->pdo->prepare('UPDATE chat_sessions SET updated_at = NOW() WHERE id = ?')
->execute([$message->getSessionId()]);
return (int) $this->pdo->lastInsertId();
}
public function save(
int $sessionId,
string $role,
string $content,
string $model,
?int $tokensInput = null,
?int $tokensOutput = null,
?array $sources = null,
?float $startMicrotime = null,
?float $endMicrotime = null,
?int $authorProfileId = null,
?int $systemPromptId = null,
?string $collectionsJson = null,
?int $contextLimit = null
): int {
$stmt = $this->pdo->prepare(
'INSERT INTO chat_messages
(session_id, role, content, model, tokens_input, tokens_output, sources,
start_microtime, end_microtime, author_profile_id, system_prompt_id, collections, context_limit)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
);
$stmt->execute([
$sessionId,
$role,
$content,
$model,
$tokensInput,
$tokensOutput,
$sources !== null ? json_encode($sources) : null,
$startMicrotime,
$endMicrotime,
$authorProfileId,
$systemPromptId,
$collectionsJson,
$contextLimit,
]);
// Update session timestamp
$this->pdo->prepare('UPDATE chat_sessions SET updated_at = NOW() WHERE id = ?')
->execute([$sessionId]);
return (int) $this->pdo->lastInsertId();
}
public function delete(int $id): void
{
$stmt = $this->pdo->prepare('DELETE FROM chat_messages WHERE id = ?');
$stmt->execute([$id]);
}
public function deleteBySessionId(int $sessionId): void
{
$stmt = $this->pdo->prepare('DELETE FROM chat_messages WHERE session_id = ?');
$stmt->execute([$sessionId]);
}
public function getSessionStats(int $sessionId): array
{
$stmt = $this->pdo->prepare(
'SELECT
COUNT(*) as message_count,
COALESCE(SUM(tokens_input), 0) as total_input_tokens,
COALESCE(SUM(tokens_output), 0) as total_output_tokens,
COALESCE(SUM(end_microtime - start_microtime), 0) as total_duration
FROM chat_messages
WHERE session_id = ?'
);
$stmt->execute([$sessionId]);
return $stmt->fetch(\PDO::FETCH_ASSOC);
}
}
Vollständig herunterladen
Aktionen
Andere Versionen dieser Datei
| ID |
Version |
Typ |
Größe |
Datum |
| 2099 |
8 |
modified |
4.4 KB |
2025-12-29 08:44 |
| 1391 |
7 |
modified |
4.5 KB |
2025-12-25 16:57 |
| 1389 |
6 |
modified |
4.5 KB |
2025-12-25 16:57 |
| 1387 |
5 |
modified |
4.5 KB |
2025-12-25 16:57 |
| 1201 |
4 |
modified |
4.5 KB |
2025-12-25 10:34 |
| 1184 |
3 |
modified |
4.5 KB |
2025-12-25 10:32 |
| 1123 |
2 |
modified |
3.0 KB |
2025-12-25 09:37 |
| 772 |
1 |
modified |
3.0 KB |
2025-12-23 08:04 |
← Zurück zur Übersicht