@@ -52,15 +52,40 @@ class MessagePart implements WithJsonSchemaInterface
52
52
private ?FunctionResponse $ functionResponse = null ;
53
53
54
54
/**
55
- * Private constructor to enforce factory method usage .
55
+ * Constructor that accepts various content types and infers the message part type .
56
56
*
57
57
* @since n.e.x.t
58
58
*
59
- * @param MessagePartTypeEnum $type The type of this message part.
59
+ * @param mixed $content The content of this message part.
60
+ * @throws \InvalidArgumentException If an unsupported content type is provided.
60
61
*/
61
- private function __construct (MessagePartTypeEnum $ type )
62
+ public function __construct ($ content )
62
63
{
63
- $ this ->type = $ type ;
64
+ if (is_string ($ content )) {
65
+ $ this ->type = MessagePartTypeEnum::text ();
66
+ $ this ->text = $ content ;
67
+ } elseif ($ content instanceof InlineFile) {
68
+ $ this ->type = MessagePartTypeEnum::inlineFile ();
69
+ $ this ->inlineFile = $ content ;
70
+ } elseif ($ content instanceof RemoteFile) {
71
+ $ this ->type = MessagePartTypeEnum::remoteFile ();
72
+ $ this ->remoteFile = $ content ;
73
+ } elseif ($ content instanceof FunctionCall) {
74
+ $ this ->type = MessagePartTypeEnum::functionCall ();
75
+ $ this ->functionCall = $ content ;
76
+ } elseif ($ content instanceof FunctionResponse) {
77
+ $ this ->type = MessagePartTypeEnum::functionResponse ();
78
+ $ this ->functionResponse = $ content ;
79
+ } else {
80
+ $ type = is_object ($ content ) ? get_class ($ content ) : gettype ($ content );
81
+ throw new \InvalidArgumentException (
82
+ sprintf (
83
+ 'Unsupported content type %s. Expected string, InlineFile, RemoteFile, '
84
+ . 'FunctionCall, or FunctionResponse. ' ,
85
+ $ type
86
+ )
87
+ );
88
+ }
64
89
}
65
90
66
91
/**
@@ -143,47 +168,71 @@ public function getFunctionResponse(): ?FunctionResponse
143
168
public static function getJsonSchema (): array
144
169
{
145
170
return [
146
- 'type ' => 'object ' ,
147
- 'properties ' => [
148
- 'type ' => [
149
- 'type ' => 'string ' ,
150
- 'enum ' => ['text ' , 'inline_file ' , 'remote_file ' , 'function_call ' , 'function_response ' ],
151
- 'description ' => 'The type of this message part. ' ,
152
- ],
153
- 'text ' => [
154
- 'type ' => ['string ' , 'null ' ],
155
- 'description ' => 'Text content (when type is text). ' ,
171
+ 'oneOf ' => [
172
+ [
173
+ 'type ' => 'object ' ,
174
+ 'properties ' => [
175
+ 'type ' => [
176
+ 'type ' => 'string ' ,
177
+ 'const ' => MessagePartTypeEnum::text ()->value ,
178
+ ],
179
+ 'text ' => [
180
+ 'type ' => 'string ' ,
181
+ 'description ' => 'Text content. ' ,
182
+ ],
183
+ ],
184
+ 'required ' => ['type ' , 'text ' ],
185
+ 'additionalProperties ' => false ,
156
186
],
157
- 'inlineFile ' => [
158
- 'oneOf ' => [
159
- ['type ' => 'null ' ],
160
- InlineFile::getJsonSchema (),
187
+ [
188
+ 'type ' => 'object ' ,
189
+ 'properties ' => [
190
+ 'type ' => [
191
+ 'type ' => 'string ' ,
192
+ 'const ' => MessagePartTypeEnum::inlineFile ()->value ,
193
+ ],
194
+ 'inlineFile ' => InlineFile::getJsonSchema (),
161
195
],
162
- 'description ' => 'Inline file data (when type is inline_file). ' ,
196
+ 'required ' => ['type ' , 'inlineFile ' ],
197
+ 'additionalProperties ' => false ,
163
198
],
164
- 'remoteFile ' => [
165
- 'oneOf ' => [
166
- ['type ' => 'null ' ],
167
- RemoteFile::getJsonSchema (),
199
+ [
200
+ 'type ' => 'object ' ,
201
+ 'properties ' => [
202
+ 'type ' => [
203
+ 'type ' => 'string ' ,
204
+ 'const ' => MessagePartTypeEnum::remoteFile ()->value ,
205
+ ],
206
+ 'remoteFile ' => RemoteFile::getJsonSchema (),
168
207
],
169
- 'description ' => 'Remote file reference (when type is remote_file). ' ,
208
+ 'required ' => ['type ' , 'remoteFile ' ],
209
+ 'additionalProperties ' => false ,
170
210
],
171
- 'functionCall ' => [
172
- 'oneOf ' => [
173
- ['type ' => 'null ' ],
174
- FunctionCall::getJsonSchema (),
211
+ [
212
+ 'type ' => 'object ' ,
213
+ 'properties ' => [
214
+ 'type ' => [
215
+ 'type ' => 'string ' ,
216
+ 'const ' => MessagePartTypeEnum::functionCall ()->value ,
217
+ ],
218
+ 'functionCall ' => FunctionCall::getJsonSchema (),
175
219
],
176
- 'description ' => 'Function call request (when type is function_call). ' ,
220
+ 'required ' => ['type ' , 'functionCall ' ],
221
+ 'additionalProperties ' => false ,
177
222
],
178
- 'functionResponse ' => [
179
- 'oneOf ' => [
180
- ['type ' => 'null ' ],
181
- FunctionResponse::getJsonSchema (),
223
+ [
224
+ 'type ' => 'object ' ,
225
+ 'properties ' => [
226
+ 'type ' => [
227
+ 'type ' => 'string ' ,
228
+ 'const ' => MessagePartTypeEnum::functionResponse ()->value ,
229
+ ],
230
+ 'functionResponse ' => FunctionResponse::getJsonSchema (),
182
231
],
183
- 'description ' => 'Function response (when type is function_response). ' ,
232
+ 'required ' => ['type ' , 'functionResponse ' ],
233
+ 'additionalProperties ' => false ,
184
234
],
185
235
],
186
- 'required ' => ['type ' ],
187
236
];
188
237
}
189
238
}
0 commit comments