@@ -33,46 +33,28 @@ class Tool implements WithJsonSchemaInterface
33
33
private ?WebSearch $ webSearch = null ;
34
34
35
35
/**
36
- * Private constructor to enforce factory method usage .
36
+ * Constructor .
37
37
*
38
38
* @since n.e.x.t
39
39
*
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.
41
42
*/
42
- private function __construct (ToolTypeEnum $ type )
43
+ public function __construct ($ content )
43
44
{
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
+ }
45
56
}
46
57
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
- }
76
58
77
59
/**
78
60
* Gets the tool type.
@@ -118,27 +100,36 @@ public function getWebSearch(): ?WebSearch
118
100
public static function getJsonSchema (): array
119
101
{
120
102
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 ' ],
132
119
],
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 (),
137
129
],
138
- 'description ' => ' Web search configuration (when type is web_search). ' ,
130
+ 'required ' => [ ' type ' , ' webSearch ' ] ,
139
131
],
140
132
],
141
- 'required ' => ['type ' ],
142
133
];
143
134
}
144
135
}
0 commit comments