createdAt = new \DateTimeImmutable(); } // Getters public function getId(): ?int { return $this->id; } public function getTaskId(): int { return $this->taskId; } public function getAssignmentId(): ?int { return $this->assignmentId; } public function getExecutor(): string { return $this->executor; } public function getExecutorType(): AssigneeType { return $this->executorType; } public function getModelName(): ?string { return $this->modelName; } public function getRequest(): ?string { return $this->request; } public function getResponse(): ?string { return $this->response; } public function getRequestTimestamp(): \DateTimeImmutable { return $this->requestTimestamp; } public function getResponseTimestamp(): ?\DateTimeImmutable { return $this->responseTimestamp; } public function getDurationMs(): ?int { return $this->durationMs; } public function getTokensInput(): ?int { return $this->tokensInput; } public function getTokensOutput(): ?int { return $this->tokensOutput; } public function getTokensTotal(): ?int { return $this->tokensTotal; } public function getCostUsd(): ?float { return $this->costUsd; } public function getStatus(): string { return $this->status; } public function getErrorMessage(): ?string { return $this->errorMessage; } public function getCreatedAt(): \DateTimeImmutable { return $this->createdAt; } // Setters public function setId(int $id): self { $this->id = $id; return $this; } public function setTaskId(int $taskId): self { $this->taskId = $taskId; return $this; } public function setAssignmentId(?int $assignmentId): self { $this->assignmentId = $assignmentId; return $this; } public function setExecutor(string $executor): self { $this->executor = $executor; return $this; } public function setExecutorType(AssigneeType $executorType): self { $this->executorType = $executorType; return $this; } public function setModelName(?string $modelName): self { $this->modelName = $modelName; return $this; } public function setRequest(?string $request): self { $this->request = $request; return $this; } public function setResponse(?string $response): self { $this->response = $response; $this->responseTimestamp = new \DateTimeImmutable(); $this->calculateDuration(); return $this; } public function setRequestTimestamp(\DateTimeImmutable $requestTimestamp): self { $this->requestTimestamp = $requestTimestamp; return $this; } public function setResponseTimestamp(?\DateTimeImmutable $responseTimestamp): self { $this->responseTimestamp = $responseTimestamp; $this->calculateDuration(); return $this; } public function setDurationMs(?int $durationMs): self { $this->durationMs = $durationMs; return $this; } public function setTokensInput(?int $tokensInput): self { $this->tokensInput = $tokensInput; $this->calculateTotalTokens(); return $this; } public function setTokensOutput(?int $tokensOutput): self { $this->tokensOutput = $tokensOutput; $this->calculateTotalTokens(); return $this; } public function setTokensTotal(?int $tokensTotal): self { $this->tokensTotal = $tokensTotal; return $this; } public function setCostUsd(?float $costUsd): self { $this->costUsd = $costUsd; return $this; } public function setStatus(string $status): self { $this->status = $status; return $this; } public function setErrorMessage(?string $errorMessage): self { $this->errorMessage = $errorMessage; if ($errorMessage !== null) { $this->status = 'error'; } return $this; } public function setCreatedAt(\DateTimeImmutable $createdAt): self { $this->createdAt = $createdAt; return $this; } private function calculateDuration(): void { if ($this->responseTimestamp !== null) { $diff = $this->responseTimestamp->getTimestamp() - $this->requestTimestamp->getTimestamp(); $this->durationMs = $diff * 1000; } } private function calculateTotalTokens(): void { if ($this->tokensInput !== null && $this->tokensOutput !== null) { $this->tokensTotal = $this->tokensInput + $this->tokensOutput; } } public function estimateTokens(): void { if ($this->request !== null) { $this->tokensInput = max(1, (int) (strlen($this->request) / 4)); } if ($this->response !== null) { $this->tokensOutput = max(1, (int) (strlen($this->response) / 4)); } $this->calculateTotalTokens(); } public function toArray(): array { return [ 'id' => $this->id, 'task_id' => $this->taskId, 'assignment_id' => $this->assignmentId, 'executor' => $this->executor, 'executor_type' => $this->executorType->value, 'model_name' => $this->modelName, 'request' => $this->request, 'response' => $this->response, 'request_timestamp' => $this->requestTimestamp->format('Y-m-d H:i:s.u'), 'response_timestamp' => $this->responseTimestamp?->format('Y-m-d H:i:s.u'), 'duration_ms' => $this->durationMs, 'tokens_input' => $this->tokensInput, 'tokens_output' => $this->tokensOutput, 'tokens_total' => $this->tokensTotal, 'cost_usd' => $this->costUsd, 'status' => $this->status, 'error_message' => $this->errorMessage, 'created_at' => $this->createdAt->format('Y-m-d H:i:s.u'), ]; } public static function fromArray(array $data): self { $result = new self(); if (isset($data['id'])) { $result->setId((int) $data['id']); } if (isset($data['task_id'])) { $result->setTaskId((int) $data['task_id']); } if (isset($data['assignment_id'])) { $result->setAssignmentId((int) $data['assignment_id']); } if (isset($data['executor'])) { $result->setExecutor($data['executor']); } if (isset($data['executor_type'])) { $result->executorType = AssigneeType::from($data['executor_type']); } if (isset($data['model_name'])) { $result->setModelName($data['model_name']); } if (isset($data['request'])) { $result->request = $data['request']; } if (isset($data['response'])) { $result->response = $data['response']; } if (isset($data['request_timestamp'])) { $result->setRequestTimestamp(new \DateTimeImmutable($data['request_timestamp'])); } if (isset($data['response_timestamp'])) { $result->setResponseTimestamp(new \DateTimeImmutable($data['response_timestamp'])); } if (isset($data['duration_ms'])) { $result->setDurationMs((int) $data['duration_ms']); } if (isset($data['tokens_input'])) { $result->tokensInput = (int) $data['tokens_input']; } if (isset($data['tokens_output'])) { $result->tokensOutput = (int) $data['tokens_output']; } if (isset($data['tokens_total'])) { $result->tokensTotal = (int) $data['tokens_total']; } if (isset($data['cost_usd'])) { $result->setCostUsd((float) $data['cost_usd']); } if (isset($data['status'])) { $result->status = $data['status']; } if (isset($data['error_message'])) { $result->errorMessage = $data['error_message']; } if (isset($data['created_at'])) { $result->setCreatedAt(new \DateTimeImmutable($data['created_at'])); } return $result; } }