Skip to content

Commit a80601e

Browse files
committed
refactor: cleans up Tool to work like MessagePart
1 parent 0540ea8 commit a80601e

File tree

1 file changed

+41
-50
lines changed

1 file changed

+41
-50
lines changed

src/Tools/DTO/Tool.php

Lines changed: 41 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -33,46 +33,28 @@ class Tool implements WithJsonSchemaInterface
3333
private ?WebSearch $webSearch = null;
3434

3535
/**
36-
* Private constructor to enforce factory method usage.
36+
* Constructor.
3737
*
3838
* @since n.e.x.t
3939
*
40-
* @param ToolTypeEnum $type The type of tool.
40+
* @param FunctionDeclaration[]|WebSearch $content The tool content.
41+
* @throws \InvalidArgumentException If content type is not supported.
4142
*/
42-
private function __construct(ToolTypeEnum $type)
43+
public function __construct($content)
4344
{
44-
$this->type = $type;
45+
if (is_array($content)) {
46+
$this->type = ToolTypeEnum::functionDeclarations();
47+
$this->functionDeclarations = $content;
48+
} elseif ($content instanceof WebSearch) {
49+
$this->type = ToolTypeEnum::webSearch();
50+
$this->webSearch = $content;
51+
} else {
52+
throw new \InvalidArgumentException(
53+
'Tool content must be an array of FunctionDeclaration instances or a WebSearch instance'
54+
);
55+
}
4556
}
4657

47-
/**
48-
* Creates a function declarations tool.
49-
*
50-
* @since n.e.x.t
51-
*
52-
* @param FunctionDeclaration[] $declarations The function declarations.
53-
* @return self
54-
*/
55-
public static function functionDeclarations(array $declarations): self
56-
{
57-
$tool = new self(ToolTypeEnum::functionDeclarations());
58-
$tool->functionDeclarations = $declarations;
59-
return $tool;
60-
}
61-
62-
/**
63-
* Creates a web search tool.
64-
*
65-
* @since n.e.x.t
66-
*
67-
* @param WebSearch $webSearch The web search configuration.
68-
* @return self
69-
*/
70-
public static function webSearch(WebSearch $webSearch): self
71-
{
72-
$tool = new self(ToolTypeEnum::webSearch());
73-
$tool->webSearch = $webSearch;
74-
return $tool;
75-
}
7658

7759
/**
7860
* Gets the tool type.
@@ -118,27 +100,36 @@ public function getWebSearch(): ?WebSearch
118100
public static function getJsonSchema(): array
119101
{
120102
return [
121-
'type' => 'object',
122-
'properties' => [
123-
'type' => [
124-
'type' => 'string',
125-
'enum' => ['function_declarations', 'web_search'],
126-
'description' => 'The type of tool.',
127-
],
128-
'functionDeclarations' => [
129-
'type' => ['array', 'null'],
130-
'items' => FunctionDeclaration::getJsonSchema(),
131-
'description' => 'Function declarations (when type is function_declarations).',
103+
'oneOf' => [
104+
[
105+
'type' => 'object',
106+
'properties' => [
107+
'type' => [
108+
'type' => 'string',
109+
'const' => ToolTypeEnum::functionDeclarations()->value,
110+
'description' => 'The type of tool.',
111+
],
112+
'functionDeclarations' => [
113+
'type' => 'array',
114+
'items' => FunctionDeclaration::getJsonSchema(),
115+
'description' => 'Function declarations.',
116+
],
117+
],
118+
'required' => ['type', 'functionDeclarations'],
132119
],
133-
'webSearch' => [
134-
'oneOf' => [
135-
['type' => 'null'],
136-
WebSearch::getJsonSchema(),
120+
[
121+
'type' => 'object',
122+
'properties' => [
123+
'type' => [
124+
'type' => 'string',
125+
'const' => ToolTypeEnum::webSearch()->value,
126+
'description' => 'The type of tool.',
127+
],
128+
'webSearch' => WebSearch::getJsonSchema(),
137129
],
138-
'description' => 'Web search configuration (when type is web_search).',
130+
'required' => ['type', 'webSearch'],
139131
],
140132
],
141-
'required' => ['type'],
142133
];
143134
}
144135
}

0 commit comments

Comments
 (0)