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-24 17:16:41 +00:00
|
|
|
use Illuminate\Support\Str;
|
2024-07-23 17:35:03 +00:00
|
|
|
|
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-24 17:16:41 +00:00
|
|
|
$random_str = Str::random(config('settings.function_call.random_prefix_length'));
|
|
|
|
$this->name = $random_str . '_' . $this->data['name'];
|
2024-07-23 17:35:03 +00:00
|
|
|
$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
|
|
|
}
|
|
|
|
}
|