| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | /** |
| 6: | * This file is part of the Nexus MCP SDK package. |
| 7: | * |
| 8: | * (c) 2025 John Paul E. Balandan, CPA <paulbalandan@gmail.com> |
| 9: | * |
| 10: | * For the full copyright and license information, please view |
| 11: | * the LICENSE file that was distributed with this source code. |
| 12: | */ |
| 13: | |
| 14: | namespace Nexus\Mcp\Schema\Sampling; |
| 15: | |
| 16: | use Nexus\Mcp\Schema\Arrayable; |
| 17: | |
| 18: | /** |
| 19: | * The server's preferences for model selection, requested of the client during sampling. |
| 20: | * |
| 21: | * Because LLMs can vary along multiple dimensions, choosing the "best" model is rarely straightforward. |
| 22: | * Different models excel in different areas—some are faster but less capable, others are more capable but |
| 23: | * more expensive, and so on. This interface allows servers to express their priorities across multiple |
| 24: | * dimensions to help clients make an appropriate selection for their use case. |
| 25: | * |
| 26: | * These preferences are always advisory. The client MAY ignore them. It is also up to the client to decide |
| 27: | * how to interpret these preferences and how to balance them against other considerations. |
| 28: | * |
| 29: | * @implements Arrayable<array{ |
| 30: | * hints?: list<template-type<ModelHint, Arrayable, 'T'>>, |
| 31: | * costPriority?: float, |
| 32: | * speedPriority?: float, |
| 33: | * intelligencePriority?: float, |
| 34: | * }> |
| 35: | */ |
| 36: | final readonly class ModelPreferences implements \JsonSerializable, Arrayable |
| 37: | { |
| 38: | /** |
| 39: | * @param null|list<ModelHint> $hints Optional hints to use for model selection. |
| 40: | * @param null|float $costPriority How much to prioritize cost when selecting a model. A value of 0 means cost is |
| 41: | * not important, while a value of 1 means cost is the most important factor. |
| 42: | * @param null|float $speedPriority How much to prioritize sampling speed (latency) when selecting a model. A value |
| 43: | * of 0 means speed is not important, while a value of 1 means speed is the most |
| 44: | * important factor. |
| 45: | * @param null|float $intelligencePriority How much to prioritize intelligence and capabilities when selecting a model. |
| 46: | * A value of 0 means intelligence is not important, while a value of 1 means |
| 47: | * intelligence is the most important factor. |
| 48: | */ |
| 49: | public function __construct( |
| 50: | public ?array $hints = null, |
| 51: | public ?float $costPriority = null, |
| 52: | public ?float $speedPriority = null, |
| 53: | public ?float $intelligencePriority = null, |
| 54: | ) {} |
| 55: | |
| 56: | #[\Override] |
| 57: | public function toArray(): array |
| 58: | { |
| 59: | return array_filter([ |
| 60: | 'hints' => null === $this->hints ? null : array_map( |
| 61: | static fn(ModelHint $hint): array => $hint->toArray(), |
| 62: | $this->hints, |
| 63: | ), |
| 64: | 'costPriority' => $this->costPriority, |
| 65: | 'speedPriority' => $this->speedPriority, |
| 66: | 'intelligencePriority' => $this->intelligencePriority, |
| 67: | ], static fn(mixed $value) => null !== $value); |
| 68: | } |
| 69: | |
| 70: | /** |
| 71: | * @return template-type<self, Arrayable, 'T'> |
| 72: | */ |
| 73: | #[\Override] |
| 74: | public function jsonSerialize(): array |
| 75: | { |
| 76: | return $this->toArray(); |
| 77: | } |
| 78: | } |
| 79: |