Files
ars-backend/app/Services/Tool/ToolRegistry.php
ROOG 59d4831f00 main: 增强工具调用与消息流程
- 支持 tool.call 和 tool.result 消息类型处理
- 引入 Tool 调度与执行逻辑,支持超时与结果截断
- 增加 ToolRegistry 和 ToolExecutor 管理工具定义与执行
- 更新上下文构建与消息映射逻辑,适配工具闭环处理
- 扩展配置与环境变量,支持 Tool 调用相关选项
- 增强单元测试覆盖工具调用与执行情景
- 更新文档和 OpenAPI,新增工具相关说明与模型定义
2025-12-22 12:36:59 +08:00

65 lines
1.3 KiB
PHP

<?php
namespace App\Services\Tool;
use App\Services\Tool\Tools\GetTimeTool;
/**
* 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(),
];
$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());
}
}