Skip to content

refactor: Improve code execution efficiency #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions src/Capability/PromptChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
33 changes: 19 additions & 14 deletions src/Capability/ResourceChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
33 changes: 19 additions & 14 deletions src/Capability/ToolChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Loading