<?php
namespace App\AI\SymfonyAiExtend\Toolbox; use App\AI\Contexts\ContextManager; use Symfony\AI\Agent\Toolbox\Exception\ToolException; use Symfony\AI\Agent\Toolbox\ToolFactoryInterface; use Symfony\AI\Platform\Tool\ExecutionReference; use Symfony\AI\Platform\Tool\Tool; class ContextManagerToolFactory implements ToolFactoryInterface { public function __construct( private readonly array $contextsCfg, ) { } public function getTool(object|string $reference): iterable { if (!($reference instanceof ContextManager) && $reference !== ContextManager::class) { throw ToolException::invalidReference(\is_object($reference) ? $reference::class : $reference); } $enumValues = []; $descriptions = []; foreach ($this->contextsCfg as $contextFunctions) { foreach ($contextFunctions['functions'] ?? [] as $functionDef) { $enumValues[] = $functionDef['name']; $descriptions[] = '`' . $functionDef['name'] . '`: ' . $functionDef['description']; } } $toolDescription = 'Fetch additional context for the user request. Available contexts: ' . implode('; ', $descriptions); $parameters = [ 'type' => 'object', 'additionalProperties' => false, 'required' => ['add_contexts'], 'properties' => [ 'add_contexts' => [ 'type' => 'array', 'description' => 'List of context tool names to enable.', 'minItems' => 1, 'items' => [ 'type' => 'string', 'enum' => $enumValues, ], ], ], ]; yield new Tool( new ExecutionReference(ContextManager::class, 'additional_available_contexts'), 'additional_available_contexts', $toolDescription, $parameters, ); } }