qualityValidation; } public function withQualityValidation(array $validation): self { return new self( answer: $this->answer, sources: $this->sources, tokensInput: $this->tokensInput, tokensOutput: $this->tokensOutput, durationSeconds: $this->durationSeconds, error: $this->error, qualityValidation: $validation, ); } public function getAnswer(): string { return $this->answer; } public function getSources(): array { return $this->sources; } public function getTokensInput(): ?int { return $this->tokensInput; } public function getTokensOutput(): ?int { return $this->tokensOutput; } public function getDurationSeconds(): ?float { return $this->durationSeconds; } public function getError(): ?string { return $this->error; } public function hasError(): bool { return $this->error !== null; } /** * Create from AI service response array */ public static function fromServiceResponse(array $result, ?float $duration = null): self { if (isset($result['error'])) { return new self( answer: '', error: $result['error'], ); } return new self( answer: $result['answer'] ?? '', sources: $result['sources'] ?? [], tokensInput: $result['usage']['input_tokens'] ?? null, tokensOutput: $result['usage']['output_tokens'] ?? null, durationSeconds: $duration, ); } /** * Create error response */ public static function error(string $message): self { return new self( answer: '', error: $message, ); } /** * Convert to array for view rendering */ public function toArray(): array { return [ 'answer' => $this->answer, 'sources' => $this->sources, 'usage' => [ 'input_tokens' => $this->tokensInput, 'output_tokens' => $this->tokensOutput, ], 'duration_seconds' => $this->durationSeconds, 'error' => $this->error, ]; } }