- 添加 `FileReadTool`,支持文件内容读取与安全验证 - 引入 `hasToolMessages` 逻辑,优化工具历史上下文处理 - 修改工具选项逻辑,支持禁用工具时的动态调整 - 增加消息序列化逻辑,优化 Redis 序列管理与数据同步 - 扩展测试覆盖,验证序列化与工具调用场景 - 增强 Docker Compose 脚本,支持应用重置与日志清理 - 调整工具调用超时设置,提升运行时用户体验
71 lines
1.5 KiB
PHP
71 lines
1.5 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;
|
|
use App\Services\Tool\Tools\FileReadTool;
|
|
|
|
/**
|
|
* 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(),
|
|
new FileReadTool(),
|
|
];
|
|
|
|
$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());
|
|
}
|
|
}
|