diff --git a/src/Capability/PromptChain.php b/src/Capability/PromptChain.php index 49f8e1d..b66d11c 100644 --- a/src/Capability/PromptChain.php +++ b/src/Capability/PromptChain.php @@ -16,28 +16,33 @@ */ class PromptChain implements PromptGetterInterface, CollectionInterface { - public function __construct( - /** - * @var IdentifierInterface[] - */ - private readonly array $items, - ) { + /** @var MetadataInterface[] */ + private readonly array $items; + + /** + * @param IdentifierInterface[] $items + */ + public function __construct(array $items) + { + /** @var MetadataInterface[] $values */ + $values = array_values(array_filter($items, fn ($item) => $item instanceof MetadataInterface)); + $keys = array_map(fn ($item) => $item->getName(), $values); + $this->items = array_combine($keys, $values); } public function getMetadata(): array { - return array_filter($this->items, fn ($item) => $item instanceof MetadataInterface); + return array_values($this->items); } public function get(PromptGet $input): PromptGetResult { - foreach ($this->items as $item) { - if ($item instanceof PromptGetterInterface && $input->name === $item->getName()) { - try { - return $item->get($input); - } catch (\Throwable $e) { - throw new PromptGetException($input, $e); - } + $item = $this->items[$input->name] ?? null; + if (!empty($item) && $item instanceof PromptGetterInterface) { + try { + return $item->get($input); + } catch (\Throwable $e) { + throw new PromptGetException($input, $e); } } diff --git a/src/Capability/ResourceChain.php b/src/Capability/ResourceChain.php index e16d74c..8b1480e 100644 --- a/src/Capability/ResourceChain.php +++ b/src/Capability/ResourceChain.php @@ -16,28 +16,33 @@ */ class ResourceChain implements CollectionInterface, ResourceReaderInterface { - public function __construct( - /** - * @var IdentifierInterface[] - */ - private readonly array $items, - ) { + /** @var MetadataInterface[] */ + private readonly array $items; + + /** + * @param IdentifierInterface[] $items + */ + public function __construct(array $items) + { + /** @var MetadataInterface[] $values */ + $values = array_values(array_filter($items, fn ($item) => $item instanceof MetadataInterface)); + $keys = array_map(fn ($item) => $item->getUri(), $values); + $this->items = array_combine($keys, $values); } public function getMetadata(): array { - return array_filter($this->items, fn ($item) => $item instanceof MetadataInterface); + return array_values($this->items); } public function read(ResourceRead $input): ResourceReadResult { - foreach ($this->items as $item) { - if ($item instanceof ResourceReaderInterface && $input->uri === $item->getUri()) { - try { - return $item->read($input); - } catch (\Throwable $e) { - throw new ResourceReadException($input, $e); - } + $item = $this->items[$input->uri] ?? null; + if (!empty($item) && $item instanceof ResourceReaderInterface) { + try { + return $item->read($input); + } catch (\Throwable $e) { + throw new ResourceReadException($input, $e); } } diff --git a/src/Capability/ToolChain.php b/src/Capability/ToolChain.php index 6d3eb9e..7674ec2 100644 --- a/src/Capability/ToolChain.php +++ b/src/Capability/ToolChain.php @@ -16,28 +16,33 @@ */ class ToolChain implements ToolExecutorInterface, CollectionInterface { - public function __construct( - /** - * @var IdentifierInterface[] $items - */ - private readonly array $items, - ) { + /** @var MetadataInterface[] */ + private readonly array $items; + + /** + * @param IdentifierInterface[] $items + */ + public function __construct(array $items) + { + /** @var MetadataInterface[] $values */ + $values = array_values(array_filter($items, fn ($item) => $item instanceof MetadataInterface)); + $keys = array_map(fn ($item) => $item->getName(), $values); + $this->items = array_combine($keys, $values); } public function getMetadata(): array { - return array_filter($this->items, fn ($item) => $item instanceof MetadataInterface); + return array_values($this->items); } public function call(ToolCall $input): ToolCallResult { - foreach ($this->items as $item) { - if ($item instanceof ToolExecutorInterface && $input->name === $item->getName()) { - try { - return $item->call($input); - } catch (\Throwable $e) { - throw new ToolExecutionException($input, $e); - } + $item = $this->items[$input->name] ?? null; + if (!empty($item) && $item instanceof ToolExecutorInterface) { + try { + return $item->call($input); + } catch (\Throwable $e) { + throw new ToolExecutionException($input, $e); } }