From d5c18077304f0fcfe95e44b9e54d41219766dfff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=90=BD=E6=9C=88?= <1569097443@qq.com> Date: Sun, 27 Apr 2025 19:54:34 +0800 Subject: [PATCH 1/2] fix: There may be no PHP_EOL in the Windows --- src/Server/Transport/Stdio/SymfonyConsoleTransport.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Server/Transport/Stdio/SymfonyConsoleTransport.php b/src/Server/Transport/Stdio/SymfonyConsoleTransport.php index 55c4034..2c61cad 100644 --- a/src/Server/Transport/Stdio/SymfonyConsoleTransport.php +++ b/src/Server/Transport/Stdio/SymfonyConsoleTransport.php @@ -33,14 +33,12 @@ public function isConnected(): bool public function receive(): \Generator { - $stream = $this->input instanceof StreamableInputInterface ? $this->input->getStream() : STDIN; - $line = fgets($stream ?? STDIN); - + $stream = $this->input instanceof StreamableInputInterface ? $this->input->getStream() ?? STDIN : STDIN; + $line = fgets($stream); if (false === $line) { return; } - - $this->buffer .= $line; + $this->buffer .= STDIN === $stream ? rtrim($line).PHP_EOL : $line; if (str_contains($this->buffer, PHP_EOL)) { $lines = explode(PHP_EOL, $this->buffer); $this->buffer = array_pop($lines); From c5e45cb1464de03f1e1b728124df2e4ac6786d03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=90=BD=E6=9C=88?= <1569097443@qq.com> Date: Tue, 27 May 2025 10:52:58 +0800 Subject: [PATCH 2/2] refactor: Improve code execution efficiency --- src/Capability/PromptChain.php | 33 ++++++++++++++++++-------------- src/Capability/ResourceChain.php | 33 ++++++++++++++++++-------------- src/Capability/ToolChain.php | 33 ++++++++++++++++++-------------- 3 files changed, 57 insertions(+), 42 deletions(-) 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); } }