Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.

Commit 4b42682

Browse files
committed
refactor(Capability): Improve code execution efficiency
1 parent c1903a3 commit 4b42682

File tree

3 files changed

+57
-42
lines changed

3 files changed

+57
-42
lines changed

src/Capability/PromptChain.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,33 @@
1616
*/
1717
class PromptChain implements PromptGetterInterface, CollectionInterface
1818
{
19-
public function __construct(
20-
/**
21-
* @var IdentifierInterface[]
22-
*/
23-
private readonly array $items,
24-
) {
19+
/** @var IdentifierInterface[] */
20+
private readonly array $items;
21+
22+
/**
23+
* @param IdentifierInterface[] $items
24+
*/
25+
public function __construct(array $items)
26+
{
27+
/** @var IdentifierInterface[] $values */
28+
$values = array_values(array_filter($items, fn ($item) => $item instanceof MetadataInterface));
29+
$keys = array_map(fn ($item) => $item->getName(), $values);
30+
$this->items = array_combine($keys, $values);
2531
}
2632

2733
public function getMetadata(): array
2834
{
29-
return array_filter($this->items, fn ($item) => $item instanceof MetadataInterface);
35+
return array_values($this->items);
3036
}
3137

3238
public function get(PromptGet $input): PromptGetResult
3339
{
34-
foreach ($this->items as $item) {
35-
if ($item instanceof PromptGetterInterface && $input->name === $item->getName()) {
36-
try {
37-
return $item->get($input);
38-
} catch (\Throwable $e) {
39-
throw new PromptGetException($input, $e);
40-
}
40+
$item = $this->items[$input->name] ?? null;
41+
if (!empty($item) && $item instanceof PromptGetterInterface) {
42+
try {
43+
return $item->get($input);
44+
} catch (\Throwable $e) {
45+
throw new PromptGetException($input, $e);
4146
}
4247
}
4348

src/Capability/ResourceChain.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,33 @@
1616
*/
1717
class ResourceChain implements CollectionInterface, ResourceReaderInterface
1818
{
19-
public function __construct(
20-
/**
21-
* @var IdentifierInterface[]
22-
*/
23-
private readonly array $items,
24-
) {
19+
/** @var IdentifierInterface[] */
20+
private readonly array $items;
21+
22+
/**
23+
* @param IdentifierInterface[] $items
24+
*/
25+
public function __construct(array $items)
26+
{
27+
/** @var IdentifierInterface[] $values */
28+
$values = array_values(array_filter($items, fn ($item) => $item instanceof MetadataInterface));
29+
$keys = array_map(fn ($item) => $item->getUri(), $values);
30+
$this->items = array_combine($keys, $values);
2531
}
2632

2733
public function getMetadata(): array
2834
{
29-
return array_filter($this->items, fn ($item) => $item instanceof MetadataInterface);
35+
return array_values($this->items);
3036
}
3137

3238
public function read(ResourceRead $input): ResourceReadResult
3339
{
34-
foreach ($this->items as $item) {
35-
if ($item instanceof ResourceReaderInterface && $input->uri === $item->getUri()) {
36-
try {
37-
return $item->read($input);
38-
} catch (\Throwable $e) {
39-
throw new ResourceReadException($input, $e);
40-
}
40+
$item = $this->items[$input->uri] ?? null;
41+
if (!empty($item) && $item instanceof ResourceReaderInterface) {
42+
try {
43+
return $item->read($input);
44+
} catch (\Throwable $e) {
45+
throw new ResourceReadException($input, $e);
4146
}
4247
}
4348

src/Capability/ToolChain.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,33 @@
1616
*/
1717
class ToolChain implements ToolExecutorInterface, CollectionInterface
1818
{
19-
public function __construct(
20-
/**
21-
* @var IdentifierInterface[] $items
22-
*/
23-
private readonly array $items,
24-
) {
19+
/** @var IdentifierInterface[] */
20+
private readonly array $items;
21+
22+
/**
23+
* @param IdentifierInterface[] $items
24+
*/
25+
public function __construct(array $items)
26+
{
27+
/** @var IdentifierInterface[] $values */
28+
$values = array_values(array_filter($items, fn ($item) => $item instanceof MetadataInterface));
29+
$keys = array_map(fn ($item) => $item->getName(), $values);
30+
$this->items = array_combine($keys, $values);
2531
}
2632

2733
public function getMetadata(): array
2834
{
29-
return array_filter($this->items, fn ($item) => $item instanceof MetadataInterface);
35+
return array_values($this->items);
3036
}
3137

3238
public function call(ToolCall $input): ToolCallResult
3339
{
34-
foreach ($this->items as $item) {
35-
if ($item instanceof ToolExecutorInterface && $input->name === $item->getName()) {
36-
try {
37-
return $item->call($input);
38-
} catch (\Throwable $e) {
39-
throw new ToolExecutionException($input, $e);
40-
}
40+
$item = $this->items[$input->name] ?? null;
41+
if (!empty($item) && $item instanceof ToolExecutorInterface) {
42+
try {
43+
return $item->call($input);
44+
} catch (\Throwable $e) {
45+
throw new ToolExecutionException($input, $e);
4146
}
4247
}
4348

0 commit comments

Comments
 (0)