Backup #1154
| ID | 1154 |
| Dateipfad | /var/www/dev.campus.systemische-tools.de/src/Domain/Entity/ChatMessage.php |
| Version | 3 |
| Typ |
modified |
| Größe | 9.5 KB |
| Hash | d5ed7d86aaea9cbc7cbbf4ce72d3ad95c5a3055285b128d4b512abbfa3ef7a05 |
| Datum | 2025-12-25 09:54:17 |
| 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 Domain\Entity;
// @responsibility: Chat-Message-Entitaet mit Token-Tracking
use Domain\ValueObject\MessageRole;
use Domain\ValueObject\TokenCount;
class ChatMessage
{
private ?int $id = null;
private int $sessionId;
private MessageRole $role;
private string $content;
private ?string $model = null;
private TokenCount $tokens;
/** @var array<mixed>|null */
private ?array $sources = null;
private ?float $startMicrotime = null;
private ?float $endMicrotime = null;
private ?int $authorProfileId = null;
private ?int $systemPromptId = null;
/** @var array<string>|null */
private ?array $collections = null;
private ?int $contextLimit = null;
/** @var array<mixed>|null */
private ?array $chunksUsed = null;
private ?int $llmRequestId = null;
private \DateTimeImmutable $createdAt;
public function __construct(int $sessionId, MessageRole $role, string $content)
{
$this->sessionId = $sessionId;
$this->role = $role;
$this->content = $content;
$this->tokens = TokenCount::zero();
$this->createdAt = new \DateTimeImmutable();
}
// Factory methods
public static function userMessage(int $sessionId, string $content): self
{
return new self($sessionId, MessageRole::USER, $content);
}
public static function assistantMessage(int $sessionId, string $content, string $model): self
{
$message = new self($sessionId, MessageRole::ASSISTANT, $content);
$message->model = $model;
return $message;
}
public static function systemMessage(int $sessionId, string $content): self
{
return new self($sessionId, MessageRole::SYSTEM, $content);
}
// Getters
public function getId(): ?int
{
return $this->id;
}
public function getSessionId(): int
{
return $this->sessionId;
}
public function getRole(): MessageRole
{
return $this->role;
}
public function getContent(): string
{
return $this->content;
}
public function getModel(): ?string
{
return $this->model;
}
public function getTokensInput(): ?int
{
return $this->tokensInput;
}
public function getTokensOutput(): ?int
{
return $this->tokensOutput;
}
public function getTotalTokens(): int
{
return ($this->tokensInput ?? 0) + ($this->tokensOutput ?? 0);
}
/**
* @return array<mixed>|null
*/
public function getSources(): ?array
{
return $this->sources;
}
public function getStartMicrotime(): ?float
{
return $this->startMicrotime;
}
public function getEndMicrotime(): ?float
{
return $this->endMicrotime;
}
public function getDurationMs(): ?float
{
if ($this->startMicrotime === null || $this->endMicrotime === null) {
return null;
}
return ($this->endMicrotime - $this->startMicrotime) * 1000;
}
public function getAuthorProfileId(): ?int
{
return $this->authorProfileId;
}
public function getSystemPromptId(): ?int
{
return $this->systemPromptId;
}
/**
* @return array<string>|null
*/
public function getCollections(): ?array
{
return $this->collections;
}
public function getContextLimit(): ?int
{
return $this->contextLimit;
}
/**
* @return array<mixed>|null
*/
public function getChunksUsed(): ?array
{
return $this->chunksUsed;
}
public function getLlmRequestId(): ?int
{
return $this->llmRequestId;
}
public function getCreatedAt(): \DateTimeImmutable
{
return $this->createdAt;
}
// Setters
public function setId(int $id): self
{
$this->id = $id;
return $this;
}
public function setModel(?string $model): self
{
$this->model = $model;
return $this;
}
public function setTokens(?int $input, ?int $output): self
{
$this->tokensInput = $input;
$this->tokensOutput = $output;
return $this;
}
/**
* @param array<mixed>|null $sources
*/
public function setSources(?array $sources): self
{
$this->sources = $sources;
return $this;
}
public function setTiming(?float $start, ?float $end): self
{
$this->startMicrotime = $start;
$this->endMicrotime = $end;
return $this;
}
public function setAuthorProfileId(?int $authorProfileId): self
{
$this->authorProfileId = $authorProfileId;
return $this;
}
public function setSystemPromptId(?int $systemPromptId): self
{
$this->systemPromptId = $systemPromptId;
return $this;
}
/**
* @param array<string>|null $collections
*/
public function setCollections(?array $collections): self
{
$this->collections = $collections;
return $this;
}
public function setContextLimit(?int $contextLimit): self
{
$this->contextLimit = $contextLimit;
return $this;
}
/**
* @param array<mixed>|null $chunksUsed
*/
public function setChunksUsed(?array $chunksUsed): self
{
$this->chunksUsed = $chunksUsed;
return $this;
}
public function setLlmRequestId(?int $llmRequestId): self
{
$this->llmRequestId = $llmRequestId;
return $this;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Check if message is from user.
*/
public function isUser(): bool
{
return $this->role->isUser();
}
/**
* Check if message is from assistant.
*/
public function isAssistant(): bool
{
return $this->role->isAssistant();
}
/**
* Convert to array for persistence.
*
* @return array<string, mixed>
*/
public function toArray(): array
{
return [
'id' => $this->id,
'session_id' => $this->sessionId,
'role' => $this->role->value,
'content' => $this->content,
'model' => $this->model,
'tokens_input' => $this->tokensInput,
'tokens_output' => $this->tokensOutput,
'sources' => $this->sources !== null ? json_encode($this->sources) : null,
'start_microtime' => $this->startMicrotime,
'end_microtime' => $this->endMicrotime,
'author_profile_id' => $this->authorProfileId,
'system_prompt_id' => $this->systemPromptId,
'collections' => $this->collections !== null ? json_encode($this->collections) : null,
'context_limit' => $this->contextLimit,
'chunks_used' => $this->chunksUsed !== null ? json_encode($this->chunksUsed) : null,
'llm_request_id' => $this->llmRequestId,
'created_at' => $this->createdAt->format('Y-m-d H:i:s'),
];
}
/**
* Create from database row.
*
* @param array<string, mixed> $data
*/
public static function fromArray(array $data): self
{
$message = new self(
(int) $data['session_id'],
MessageRole::from($data['role']),
$data['content']
);
if (isset($data['id'])) {
$message->setId((int) $data['id']);
}
if (isset($data['model'])) {
$message->setModel($data['model']);
}
if (isset($data['tokens_input']) || isset($data['tokens_output'])) {
$message->setTokens(
isset($data['tokens_input']) ? (int) $data['tokens_input'] : null,
isset($data['tokens_output']) ? (int) $data['tokens_output'] : null
);
}
if (isset($data['sources'])) {
$sources = is_string($data['sources']) ? json_decode($data['sources'], true) : $data['sources'];
$message->setSources(is_array($sources) ? $sources : null);
}
if (isset($data['start_microtime']) || isset($data['end_microtime'])) {
$message->setTiming(
isset($data['start_microtime']) ? (float) $data['start_microtime'] : null,
isset($data['end_microtime']) ? (float) $data['end_microtime'] : null
);
}
if (isset($data['author_profile_id'])) {
$message->setAuthorProfileId((int) $data['author_profile_id']);
}
if (isset($data['system_prompt_id'])) {
$message->setSystemPromptId((int) $data['system_prompt_id']);
}
if (isset($data['collections'])) {
$collections = is_string($data['collections']) ? json_decode($data['collections'], true) : $data['collections'];
$message->setCollections(is_array($collections) ? $collections : null);
}
if (isset($data['context_limit'])) {
$message->setContextLimit((int) $data['context_limit']);
}
if (isset($data['chunks_used'])) {
$chunks = is_string($data['chunks_used']) ? json_decode($data['chunks_used'], true) : $data['chunks_used'];
$message->setChunksUsed(is_array($chunks) ? $chunks : null);
}
if (isset($data['llm_request_id'])) {
$message->setLlmRequestId((int) $data['llm_request_id']);
}
if (isset($data['created_at'])) {
$message->setCreatedAt(new \DateTimeImmutable($data['created_at']));
}
return $message;
}
}
Vollständig herunterladen
Aktionen
Andere Versionen dieser Datei
| ID |
Version |
Typ |
Größe |
Datum |
| 1936 |
9 |
modified |
7.6 KB |
2025-12-28 01:28 |
| 1935 |
8 |
modified |
7.6 KB |
2025-12-28 01:28 |
| 1934 |
7 |
modified |
7.6 KB |
2025-12-28 01:27 |
| 1382 |
6 |
modified |
10.0 KB |
2025-12-25 16:57 |
| 1156 |
5 |
modified |
9.9 KB |
2025-12-25 09:54 |
| 1155 |
4 |
modified |
9.8 KB |
2025-12-25 09:54 |
| 1154 |
3 |
modified |
9.5 KB |
2025-12-25 09:54 |
| 1153 |
2 |
modified |
9.5 KB |
2025-12-25 09:54 |
| 1152 |
1 |
modified |
9.5 KB |
2025-12-25 09:54 |
← Zurück zur Übersicht