| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | namespace Nexus\Mcp\Server\Completion; |
| 15: | |
| 16: | use Nexus\Assert\Assert; |
| 17: | use Nexus\Mcp\Core\Schema\Prompt\PromptReference; |
| 18: | use Nexus\Mcp\Core\Schema\Resource\ResourceTemplateReference; |
| 19: | use Nexus\Mcp\Core\Schema\Result\CompleteResult; |
| 20: | use Nexus\Mcp\Server\ServerContext; |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | final readonly class CompletionStore implements CompletionStoreInterface |
| 28: | { |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | public function __construct(private array $promptCompletions = [], private array $templateCompletions = []) |
| 34: | { |
| 35: | Assert::that($this->promptCompletions) |
| 36: | ->keys() |
| 37: | ->isNonEmptyString('Completion store prompt key must be a non-empty string.') |
| 38: | ; |
| 39: | Assert::that($this->templateCompletions) |
| 40: | ->keys() |
| 41: | ->isNonEmptyString('Completion store template key must be a non-empty string.') |
| 42: | ; |
| 43: | } |
| 44: | |
| 45: | #[\Override] |
| 46: | public function complete( |
| 47: | PromptReference|ResourceTemplateReference $ref, |
| 48: | string $argumentName, |
| 49: | string $argumentValue, |
| 50: | ?array $contextArguments, |
| 51: | ServerContext $context, |
| 52: | ): CompleteResult { |
| 53: | if ($ref instanceof PromptReference) { |
| 54: | $providers = $this->promptCompletions[$ref->name] ?? null; |
| 55: | } else { |
| 56: | $providers = $this->templateCompletions[$ref->uri] ?? null; |
| 57: | } |
| 58: | |
| 59: | if (null === $providers || ! isset($providers[$argumentName])) { |
| 60: | return new CompleteResult(['values' => []]); |
| 61: | } |
| 62: | |
| 63: | return ($providers[$argumentName])($argumentValue, $contextArguments, $context); |
| 64: | } |
| 65: | } |
| 66: | |