|  | 
| 6 | 6 | use Kirschbaum\Loop\Concerns\Makeable; | 
| 7 | 7 | use Kirschbaum\Loop\Contracts\Tool; | 
| 8 | 8 | use Prism\Prism\Contracts\Schema; | 
| 9 |  | -use Prism\Prism\Schema\BooleanSchema; | 
| 10 |  | -use Prism\Prism\Schema\NumberSchema; | 
| 11 |  | -use Prism\Prism\Schema\ObjectSchema; | 
| 12 |  | -use Prism\Prism\Schema\StringSchema; | 
| 13 | 9 | use Prism\Prism\Tool as PrismTool; | 
| 14 | 10 | 
 | 
| 15 | 11 | class CustomTool implements Tool | 
| 16 | 12 | { | 
| 17 | 13 |     use Makeable; | 
| 18 | 14 | 
 | 
|  | 15 | +    private PrismTool $prismTool; | 
|  | 16 | + | 
| 19 | 17 |     public function __construct( | 
| 20 | 18 |         public readonly string $name, | 
| 21 | 19 |         public readonly string $description, | 
| 22 |  | -        /** @var array<string, array{type?: string, description?: string, required?: bool}> */ | 
| 23 |  | -        public readonly array $parameters, | 
| 24 |  | -        public readonly Closure $handler, | 
| 25 |  | -    ) {} | 
| 26 |  | - | 
| 27 |  | -    public function build(): PrismTool | 
| 28 |  | -    { | 
| 29 |  | -        $tool = app(PrismTool::class) | 
|  | 20 | +    ) { | 
|  | 21 | +        $this->prismTool = app(PrismTool::class) | 
| 30 | 22 |             ->as($this->getName()) | 
| 31 | 23 |             ->for($this->description); | 
|  | 24 | +    } | 
| 32 | 25 | 
 | 
| 33 |  | -        $this->buildParameters($tool); | 
| 34 |  | - | 
| 35 |  | -        return $tool->using($this->handler); | 
|  | 26 | +    public function build(): PrismTool | 
|  | 27 | +    { | 
|  | 28 | +        return $this->prismTool; | 
| 36 | 29 |     } | 
| 37 | 30 | 
 | 
| 38 | 31 |     public function getName(): string | 
| 39 | 32 |     { | 
| 40 | 33 |         return $this->name; | 
| 41 | 34 |     } | 
| 42 | 35 | 
 | 
| 43 |  | -    private function buildParameters(PrismTool $tool): void | 
|  | 36 | +    public function using(Closure|callable $handler): self | 
| 44 | 37 |     { | 
| 45 |  | -        foreach ($this->parameters as $name => $config) { | 
| 46 |  | -            $type = $config['type'] ?? 'string'; | 
| 47 |  | -            $description = $config['description'] ?? ''; | 
| 48 |  | -            $required = $config['required'] ?? false; | 
| 49 |  | - | 
| 50 |  | -            match ($type) { | 
| 51 |  | -                'string' => $tool->withStringParameter($name, $description, required: $required), | 
| 52 |  | -                'integer', 'number' => $tool->withNumberParameter($name, $description, required: $required), | 
| 53 |  | -                'boolean' => $tool->withBooleanParameter($name, $description, required: $required), | 
| 54 |  | -                'object' => $this->buildObjectParameter($tool, $name, $config), | 
| 55 |  | -                default => $tool->withStringParameter($name, $description, required: $required), | 
| 56 |  | -            }; | 
| 57 |  | -        } | 
|  | 38 | +        $this->prismTool->using(fn: $handler); | 
|  | 39 | + | 
|  | 40 | +        return $this; | 
| 58 | 41 |     } | 
| 59 | 42 | 
 | 
| 60 |  | -    /** | 
| 61 |  | -     * @param  array<string, mixed>  $config | 
| 62 |  | -     */ | 
| 63 |  | -    private function buildObjectParameter(PrismTool $tool, string $name, array $config): void | 
| 64 |  | -    { | 
| 65 |  | -        /** @var string $description */ | 
| 66 |  | -        $description = $config['description'] ?? ''; | 
|  | 43 | +    public function withArrayParameter( | 
|  | 44 | +        string $name, | 
|  | 45 | +        string $description, | 
|  | 46 | +        Schema $items, | 
|  | 47 | +        bool $required = true, | 
|  | 48 | +    ): self { | 
|  | 49 | +        $this->prismTool->withArrayParameter($name, $description, $items, $required); | 
| 67 | 50 | 
 | 
| 68 |  | -        /** @var bool $required */ | 
| 69 |  | -        $required = $config['required'] ?? false; | 
|  | 51 | +        return $this; | 
|  | 52 | +    } | 
| 70 | 53 | 
 | 
| 71 |  | -        /** @var array<string, array<string, mixed>> $properties */ | 
| 72 |  | -        $properties = $config['properties'] ?? []; | 
|  | 54 | +    public function withBooleanParameter(string $name, string $description, bool $required = true): self | 
|  | 55 | +    { | 
|  | 56 | +        $this->prismTool->withBooleanParameter($name, $description, $required); | 
| 73 | 57 | 
 | 
| 74 |  | -        /** @var array<int, string> $requiredFields */ | 
| 75 |  | -        $requiredFields = $config['required'] ?? []; | 
|  | 58 | +        return $this; | 
|  | 59 | +    } | 
| 76 | 60 | 
 | 
| 77 |  | -        /** @var bool $allowAdditional */ | 
| 78 |  | -        $allowAdditional = $config['allow_additional_properties'] ?? false; | 
|  | 61 | +    /** | 
|  | 62 | +     * @param  array<int, string|int|float>  $options | 
|  | 63 | +     */ | 
|  | 64 | +    public function withEnumParameter( | 
|  | 65 | +        string $name, | 
|  | 66 | +        string $description, | 
|  | 67 | +        array $options, | 
|  | 68 | +        bool $required = true, | 
|  | 69 | +    ): self { | 
|  | 70 | +        $this->prismTool->withEnumParameter($name, $description, $options, $required); | 
|  | 71 | + | 
|  | 72 | +        return $this; | 
|  | 73 | +    } | 
| 79 | 74 | 
 | 
| 80 |  | -        $schemaProperties = $this->buildSchemaArray($properties); | 
|  | 75 | +    public function withNumberParameter(string $name, string $description, bool $required = true): self | 
|  | 76 | +    { | 
|  | 77 | +        $this->prismTool->withNumberParameter($name, $description, $required); | 
| 81 | 78 | 
 | 
| 82 |  | -        $tool->withObjectParameter( | 
| 83 |  | -            name: $name, | 
| 84 |  | -            description: $description, | 
| 85 |  | -            properties: $schemaProperties, | 
| 86 |  | -            requiredFields: $requiredFields, | 
| 87 |  | -            allowAdditionalProperties: $allowAdditional, | 
| 88 |  | -            required: $required | 
| 89 |  | -        ); | 
|  | 79 | +        return $this; | 
| 90 | 80 |     } | 
| 91 | 81 | 
 | 
| 92 | 82 |     /** | 
| 93 |  | -     * @param  array<string, array<string, mixed>>  $parameters | 
| 94 |  | -     * @return array<Schema> | 
|  | 83 | +     * @param  array<int, Schema>  $properties | 
|  | 84 | +     * @param  array<int, string>  $requiredFields | 
| 95 | 85 |      */ | 
| 96 |  | -    private function buildSchemaArray(array $parameters): array | 
|  | 86 | +    public function withObjectParameter( | 
|  | 87 | +        string $name, | 
|  | 88 | +        string $description, | 
|  | 89 | +        array $properties, | 
|  | 90 | +        array $requiredFields = [], | 
|  | 91 | +        bool $allowAdditionalProperties = false, | 
|  | 92 | +        bool $required = true, | 
|  | 93 | +    ): self { | 
|  | 94 | + | 
|  | 95 | +        $this->prismTool->withObjectParameter($name, $description, $properties, $requiredFields, $allowAdditionalProperties, $required); | 
|  | 96 | + | 
|  | 97 | +        return $this; | 
|  | 98 | +    } | 
|  | 99 | + | 
|  | 100 | +    public function withParameter(Schema $parameter, bool $required = true): self | 
|  | 101 | +    { | 
|  | 102 | +        $this->prismTool->withParameter($parameter, $required); | 
|  | 103 | + | 
|  | 104 | +        return $this; | 
|  | 105 | +    } | 
|  | 106 | + | 
|  | 107 | +    public function withStringParameter(string $name, string $description, bool $required = true): self | 
| 97 | 108 |     { | 
| 98 |  | -        $schemaArray = []; | 
| 99 |  | - | 
| 100 |  | -        foreach ($parameters as $name => $config) { | 
| 101 |  | -            $type = $config['type'] ?? 'string'; | 
| 102 |  | - | 
| 103 |  | -            /** @var string $description */ | 
| 104 |  | -            $description = $config['description'] ?? ''; | 
| 105 |  | - | 
| 106 |  | -            /** @var array<string, array<string, mixed>> $properties */ | 
| 107 |  | -            $properties = $config['properties'] ?? []; | 
| 108 |  | - | 
| 109 |  | -            /** @var array<int, string> $requiredFields */ | 
| 110 |  | -            $requiredFields = $config['required_fields'] ?? []; | 
| 111 |  | - | 
| 112 |  | -            /** @var bool $allowAdditional */ | 
| 113 |  | -            $allowAdditional = $config['allow_additional_properties'] ?? false; | 
| 114 |  | - | 
| 115 |  | -            $schemaArray[] = match ($type) { | 
| 116 |  | -                'string' => new StringSchema($name, $description), | 
| 117 |  | -                'integer', 'number' => new NumberSchema($name, $description), | 
| 118 |  | -                'boolean' => new BooleanSchema($name, $description), | 
| 119 |  | -                'object' => new ObjectSchema( | 
| 120 |  | -                    $name, | 
| 121 |  | -                    $description, | 
| 122 |  | -                    $this->buildSchemaArray($properties), | 
| 123 |  | -                    $requiredFields, | 
| 124 |  | -                    $allowAdditional, | 
| 125 |  | -                ), | 
| 126 |  | -                default => new StringSchema($name, $description), | 
| 127 |  | -            }; | 
| 128 |  | -        } | 
| 129 |  | - | 
| 130 |  | -        return $schemaArray; | 
|  | 109 | +        $this->prismTool->withStringParameter($name, $description, $required); | 
|  | 110 | + | 
|  | 111 | +        return $this; | 
| 131 | 112 |     } | 
| 132 | 113 | } | 
0 commit comments