Backup #1935
| ID | 1935 |
| Dateipfad | /var/www/dev.campus.systemische-tools.de/src/Domain/Entity/ChatMessage.php |
| Version | 8 |
| Typ |
modified |
| Größe | 7.6 KB |
| Hash | e43530b099bbf9cbe3072a2391a133a683190d025c7922838042fafd5034e3c6 |
| Datum | 2025-12-28 01:28:00 |
| 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: Rich domain model for chat messages with business logic
use Domain\ValueObject\MessageContent;
use Domain\ValueObject\MessageMetadata;
use Domain\ValueObject\MessageRole;
use Domain\ValueObject\MessageTiming;
use Domain\ValueObject\TokenCount;
class ChatMessage
{
private ?int $id;
private int $sessionId;
private MessageRole $role;
private MessageContent $content;
private ?string $model;
private TokenCount $tokens;
private MessageTiming $timing;
private MessageMetadata $metadata;
private ?int $authorProfileId;
private ?int $systemPromptId;
private ?int $llmRequestId;
private \DateTimeImmutable $createdAt;
private function __construct(
?int $id,
int $sessionId,
MessageRole $role,
MessageContent $content,
?string $model,
TokenCount $tokens,
MessageTiming $timing,
MessageMetadata $metadata,
?int $authorProfileId,
?int $systemPromptId,
?int $llmRequestId,
\DateTimeImmutable $createdAt
) {
$this->id = $id;
$this->sessionId = $sessionId;
$this->role = $role;
$this->content = $content;
$this->model = $model;
$this->tokens = $tokens;
$this->timing = $timing;
$this->metadata = $metadata;
$this->authorProfileId = $authorProfileId;
$this->systemPromptId = $systemPromptId;
$this->llmRequestId = $llmRequestId;
$this->createdAt = $createdAt;
}
// Factory methods for domain logic
public static function createUserMessage(int $sessionId, string $content, ?int $authorProfileId = null): self
{
return new self(
null,
$sessionId,
MessageRole::USER,
MessageContent::fromString($content),
null,
TokenCount::zero(),
MessageTiming::none(),
MessageMetadata::empty(),
$authorProfileId,
null,
null,
new \DateTimeImmutable()
);
}
public static function createAssistantMessage(
int $sessionId,
string $content,
string $model,
TokenCount $tokens,
MessageTiming $timing
): self {
return new self(
null,
$sessionId,
MessageRole::ASSISTANT,
MessageContent::fromStringOrEmpty($content),
$model,
$tokens,
$timing,
MessageMetadata::empty(),
null,
null,
null,
new \DateTimeImmutable()
);
}
public static function createSystemMessage(int $sessionId, string $content, ?int $systemPromptId = null): self
{
return new self(
null,
$sessionId,
MessageRole::SYSTEM,
MessageContent::fromStringOrEmpty($content),
null,
TokenCount::zero(),
MessageTiming::none(),
MessageMetadata::empty(),
null,
$systemPromptId,
null,
new \DateTimeImmutable()
);
}
// Reconstitution from persistence (used by Factory)
public static function reconstituteFromPersistence(
?int $id,
int $sessionId,
MessageRole $role,
MessageContent $content,
?string $model,
TokenCount $tokens,
MessageTiming $timing,
MessageMetadata $metadata,
?int $authorProfileId,
?int $systemPromptId,
?int $llmRequestId,
\DateTimeImmutable $createdAt
): self {
return new self(
$id,
$sessionId,
$role,
$content,
$model,
$tokens,
$timing,
$metadata,
$authorProfileId,
$systemPromptId,
$llmRequestId,
$createdAt
);
}
// Essential getters
public function id(): ?int
{
return $this->id;
}
public function sessionId(): int
{
return $this->sessionId;
}
public function role(): MessageRole
{
return $this->role;
}
public function content(): MessageContent
{
return $this->content;
}
public function model(): ?string
{
return $this->model;
}
public function tokens(): TokenCount
{
return $this->tokens;
}
public function timing(): MessageTiming
{
return $this->timing;
}
public function metadata(): MessageMetadata
{
return $this->metadata;
}
public function authorProfileId(): ?int
{
return $this->authorProfileId;
}
public function systemPromptId(): ?int
{
return $this->systemPromptId;
}
public function llmRequestId(): ?int
{
return $this->llmRequestId;
}
public function createdAt(): \DateTimeImmutable
{
return $this->createdAt;
}
// Business logic methods
public function isUser(): bool
{
return $this->role->isUser();
}
public function isAssistant(): bool
{
return $this->role->isAssistant();
}
public function isSystem(): bool
{
return $this->role->isSystem();
}
public function hasTokens(): bool
{
return $this->tokens->hasTokens();
}
public function estimatedCostUsd(): float
{
return $this->tokens->estimatedCostUsd();
}
public function durationMs(): ?float
{
return $this->timing->durationMs();
}
// Mutation methods (return new instance for immutability)
public function withId(int $id): self
{
$clone = clone $this;
$clone->id = $id;
return $clone;
}
public function withMetadata(MessageMetadata $metadata): self
{
$clone = clone $this;
$clone->metadata = $metadata;
return $clone;
}
public function withLlmRequestId(int $llmRequestId): self
{
$clone = clone $this;
$clone->llmRequestId = $llmRequestId;
return $clone;
}
public function withTiming(MessageTiming $timing): self
{
$clone = clone $this;
$clone->timing = $timing;
return $clone;
}
/**
* 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->value(),
'model' => $this->model,
'tokens_input' => $this->tokens->input() > 0 ? $this->tokens->input() : null,
'tokens_output' => $this->tokens->output() > 0 ? $this->tokens->output() : null,
'sources' => $this->metadata->sources() !== null ? json_encode($this->metadata->sources()) : null,
'start_microtime' => $this->timing->startMicrotime(),
'end_microtime' => $this->timing->endMicrotime(),
'author_profile_id' => $this->authorProfileId,
'system_prompt_id' => $this->systemPromptId,
'collections' => $this->metadata->collections() !== null ? json_encode($this->metadata->collections()) : null,
'context_limit' => $this->metadata->contextLimit(),
'chunks_used' => $this->metadata->chunksUsed() !== null ? json_encode($this->metadata->chunksUsed()) : null,
'llm_request_id' => $this->llmRequestId,
'created_at' => $this->createdAt->format('Y-m-d H:i:s'),
];
}
}
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