| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | namespace Nexus\Mcp\Extension\Skills\Server; |
| 15: | |
| 16: | use Nexus\Assert\Assert; |
| 17: | use Nexus\Mcp\Core\Schema\Resource\BlobResourceContents; |
| 18: | use Nexus\Mcp\Core\Schema\Resource\TextResourceContents; |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | final readonly class SkillFile |
| 24: | { |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | public function __construct( |
| 31: | public string $uri, |
| 32: | public string $contents, |
| 33: | public ?string $mimeType = null, |
| 34: | ) { |
| 35: | Assert::that($uri)->isNonEmptyString('skill file "uri" must be a non-empty string.'); |
| 36: | Assert::that($mimeType)->nullOr()->isNonEmptyString('skill file "mimeType" must be a non-empty string or null.'); |
| 37: | } |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | public function toResourceContents(): BlobResourceContents|TextResourceContents |
| 43: | { |
| 44: | if (preg_match('//u', $this->contents) === 1 && ! str_contains($this->contents, "\0")) { |
| 45: | return new TextResourceContents($this->uri, $this->contents, $this->mimeType); |
| 46: | } |
| 47: | |
| 48: | return new BlobResourceContents($this->uri, base64_encode($this->contents), $this->mimeType); |
| 49: | } |
| 50: | } |
| 51: | |