SessionMetadata.php

Code Hygiene Score: 84

Keine Issues gefunden.

Dependencies 1

Klassen 1

Funktionen 18

Verwendet von 3

Versionen 3

Code

<?php

declare(strict_types=1);

namespace Domain\ValueObject;

// @responsibility: Immutable Value Object für Chat-Session-Metadaten (Model, Collections, Context, etc.)

use InvalidArgumentException;

final class SessionMetadata
{
    /**
     * @param array<string> $collections
     */
    private function __construct(
        private string $model,
        private array $collections,
        private int $contextLimit,
        private float $temperature,
        private int $maxTokens
    ) {
    }

    /**
     * @param array<string> $collections
     */
    public static function create(
        string $model = 'claude-opus-4-5-20251101',
        array $collections = ['documents'],
        int $contextLimit = 5,
        float $temperature = 0.7,
        int $maxTokens = 4096
    ): self {
        self::validateModel($model);
        self::validateCollections($collections);
        self::validateContextLimit($contextLimit);
        self::validateTemperature($temperature);
        self::validateMaxTokens($maxTokens);

        return new self($model, $collections, $contextLimit, $temperature, $maxTokens);
    }

    public function getModel(): string
    {
        return $this->model;
    }

    /**
     * @return array<string>
     */
    public function getCollections(): array
    {
        return $this->collections;
    }

    public function getContextLimit(): int
    {
        return $this->contextLimit;
    }

    public function getTemperature(): float
    {
        return $this->temperature;
    }

    public function getMaxTokens(): int
    {
        return $this->maxTokens;
    }

    public function withModel(string $model): self
    {
        self::validateModel($model);

        return new self($model, $this->collections, $this->contextLimit, $this->temperature, $this->maxTokens);
    }

    /**
     * @param array<string> $collections
     */
    public function withCollections(array $collections): self
    {
        self::validateCollections($collections);

        return new self($this->model, $collections, $this->contextLimit, $this->temperature, $this->maxTokens);
    }

    public function withContextLimit(int $contextLimit): self
    {
        self::validateContextLimit($contextLimit);

        return new self($this->model, $this->collections, $contextLimit, $this->temperature, $this->maxTokens);
    }

    public function withTemperature(float $temperature): self
    {
        self::validateTemperature($temperature);

        return new self($this->model, $this->collections, $this->contextLimit, $temperature, $this->maxTokens);
    }

    public function withMaxTokens(int $maxTokens): self
    {
        self::validateMaxTokens($maxTokens);

        return new self($this->model, $this->collections, $this->contextLimit, $this->temperature, $maxTokens);
    }

    private static function validateModel(string $model): void
    {
        if (trim($model) === '') {
            throw new InvalidArgumentException('Model cannot be empty');
        }
    }

    /**
     * @param array<string> $collections
     */
    private static function validateCollections(array $collections): void
    {
        // Empty collections allowed - user can chat without RAG context
        foreach ($collections as $collection) {
            if (trim($collection) === '') {
                throw new InvalidArgumentException('Each collection must be a non-empty string');
            }
        }
    }

    private static function validateContextLimit(int $contextLimit): void
    {
        if ($contextLimit < 1 || $contextLimit > 20) {
            throw new InvalidArgumentException('Context limit must be between 1 and 20');
        }
    }

    private static function validateTemperature(float $temperature): void
    {
        if ($temperature < 0.0 || $temperature > 1.0) {
            throw new InvalidArgumentException('Temperature must be between 0.0 and 1.0');
        }
    }

    private static function validateMaxTokens(int $maxTokens): void
    {
        if ($maxTokens < 1 || $maxTokens > 100000) {
            throw new InvalidArgumentException('Max tokens must be between 1 and 100000');
        }
    }

    public function equals(self $other): bool
    {
        return $this->model === $other->model
            && $this->collections === $other->collections
            && $this->contextLimit === $other->contextLimit
            && abs($this->temperature - $other->temperature) < 0.0001
            && $this->maxTokens === $other->maxTokens;
    }
}
← Übersicht Graph