amber-laravel/app/Repositories/Tool/ToolFunction.php

66 lines
1.6 KiB
PHP
Raw Normal View History

2024-07-23 16:40:56 +00:00
<?php
namespace App\Repositories\Tool;
2024-07-23 17:35:03 +00:00
use Exception;
2024-07-23 16:40:56 +00:00
class ToolFunction
{
2024-07-23 17:35:03 +00:00
public string $name;
2024-07-24 08:13:16 +00:00
2024-07-23 17:35:03 +00:00
public string $description;
2024-07-24 08:13:16 +00:00
2024-07-23 17:35:03 +00:00
public array $parameters;
2024-07-24 08:13:16 +00:00
2024-07-23 17:35:03 +00:00
protected array $data;
2024-07-24 08:13:16 +00:00
2024-07-23 17:35:03 +00:00
public array $required;
2024-07-23 16:40:56 +00:00
/**
* Create a new class instance.
2024-07-24 08:13:16 +00:00
*
2024-07-23 17:35:03 +00:00
* @throws Exception
*/
public function __construct(array $data)
{
$this->data = $data;
$this->parse();
}
/**
* @throws Exception
2024-07-23 16:40:56 +00:00
*/
2024-07-23 17:35:03 +00:00
private function parse(): void
2024-07-23 16:40:56 +00:00
{
2024-07-23 17:35:03 +00:00
$this->name = $this->data['name'];
$this->description = $this->data['description'];
// 如果 parameters 不为空,则验证
if (! empty($this->data['parameters'])) {
// 检查 type 是否为 object
if ($this->data['parameters']['type'] !== 'object') {
throw new Exception('parameters type must be object');
}
if (! isset($this->data['parameters']['properties'])) {
throw new Exception('parameters must have properties');
}
// 循环检查 properties 下的 key 的值,必须有 type 和 description
foreach ($this->data['parameters']['properties'] as $key => $value) {
if (! isset($value['type'])) {
throw new Exception('parameters properties must have type');
}
if (! isset($value['description'])) {
throw new Exception('parameters properties must have description');
}
}
}
$this->parameters = $this->data['parameters'];
$this->required = $this->data['required'] ?? [];
2024-07-23 16:40:56 +00:00
}
}