- 注册 `LsTool` 和 `BashTool` 工具,支持目录操作和命令执行 - 增强工具调用逻辑,添加日志记录以提升调试能力 - 增加 `ToolRegistry` 和 `RunLoop` 的增量累积与排序优化 - 完善单元测试覆盖新工具的执行与行为验证
69 lines
1.4 KiB
PHP
69 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Tool;
|
|
|
|
use App\Services\Tool\Tools\BashTool;
|
|
use App\Services\Tool\Tools\GetTimeTool;
|
|
use App\Services\Tool\Tools\LsTool;
|
|
|
|
/**
|
|
* Tool 注册表:管理已注册工具与 OpenAI 兼容的声明。
|
|
*/
|
|
class ToolRegistry
|
|
{
|
|
/**
|
|
* @var array<string, Tool>
|
|
*/
|
|
private array $tools;
|
|
|
|
/**
|
|
* @param array<int, Tool>|null $tools
|
|
*/
|
|
public function __construct(?array $tools = null)
|
|
{
|
|
$tools = $tools ?? [
|
|
new GetTimeTool(),
|
|
new LsTool(),
|
|
new BashTool(),
|
|
];
|
|
|
|
$this->tools = [];
|
|
|
|
foreach ($tools as $tool) {
|
|
$this->tools[$tool->name()] = $tool;
|
|
}
|
|
}
|
|
|
|
public function get(string $name): ?Tool
|
|
{
|
|
return $this->tools[$name] ?? null;
|
|
}
|
|
|
|
/**
|
|
* @return array<int, Tool>
|
|
*/
|
|
public function all(): array
|
|
{
|
|
return array_values($this->tools);
|
|
}
|
|
|
|
/**
|
|
* 返回 OpenAI-compatible tools 描述。
|
|
*
|
|
* @return array<int, array<string, mixed>>
|
|
*/
|
|
public function openAiToolsSpec(): array
|
|
{
|
|
return array_map(function (Tool $tool) {
|
|
return [
|
|
'type' => 'function',
|
|
'function' => [
|
|
'name' => $tool->name(),
|
|
'description' => $tool->description(),
|
|
'parameters' => $tool->parameters(),
|
|
],
|
|
];
|
|
}, $this->all());
|
|
}
|
|
}
|