ChatResponse.php

Code Hygiene Score: 94

Keine Issues gefunden.

Klassen 1

Funktionen 13

Versionen 3

Code

<?php

declare(strict_types=1);

namespace UseCases\Chat;

// @responsibility: Ergebnisobjekt für Chat-Nachrichten mit Metadaten

final class ChatResponse
{
    public function __construct(
        private string $answer,
        private array $sources = [],
        private ?int $tokensInput = null,
        private ?int $tokensOutput = null,
        private ?float $durationSeconds = null,
        private ?string $error = null,
        private ?array $qualityValidation = null,
    ) {
    }

    public function getQualityValidation(): ?array
    {
        return $this->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,
            'quality_validation' => $this->qualityValidation,
        ];
    }
}
← Übersicht Graph