$tools */ private function __construct(private ToolkitInterface $inner, private array $tools) {} /** * `$wrap`, when given, is handed each tool that is kept and the key its toolkit came under, * and what it returns is what the wrapper offers: how Chat\Pipeline::offered() puts every * tool behind Toolkit\Guarded. The name a tool is settled by is the one asked before the * wrap, so a wrap that renamed a tool could not move a call to another toolkit. * * @param array $toolkits in the order the agent is to be handed them * @param (\Closure(ToolInterface, array-key): ToolInterface)|null $wrap * @return list one per toolkit, in the same order */ public static function over(array $toolkits, ?\Closure $wrap = null): array { $taken = []; $out = []; foreach ($toolkits as $key => $toolkit) { $kept = []; $names = []; foreach ($toolkit->tools() as $tool) { if (!isset($taken[$tool->name()])) { $kept[] = $wrap === null ? $tool : $wrap($tool, $key); $names[$tool->name()] = true; } } $taken += $names; $out[] = new self($toolkit, $kept); } return $out; } /** @return list */ public function tools(): array { return $this->tools; } public function guidelines(): string { return $this->tools === [] ? '' : $this->inner->guidelines(); } }