Skip to content

Commit eb386cf

Browse files
committed
refactor: improves type inferrence and json schema
1 parent 7ca0389 commit eb386cf

File tree

1 file changed

+84
-35
lines changed

1 file changed

+84
-35
lines changed

src/Messages/DTO/MessagePart.php

Lines changed: 84 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,40 @@ class MessagePart implements WithJsonSchemaInterface
5252
private ?FunctionResponse $functionResponse = null;
5353

5454
/**
55-
* Private constructor to enforce factory method usage.
55+
* Constructor that accepts various content types and infers the message part type.
5656
*
5757
* @since n.e.x.t
5858
*
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.
6061
*/
61-
private function __construct(MessagePartTypeEnum $type)
62+
public function __construct($content)
6263
{
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+
}
6489
}
6590

6691
/**
@@ -143,47 +168,71 @@ public function getFunctionResponse(): ?FunctionResponse
143168
public static function getJsonSchema(): array
144169
{
145170
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,
156186
],
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(),
161195
],
162-
'description' => 'Inline file data (when type is inline_file).',
196+
'required' => ['type', 'inlineFile'],
197+
'additionalProperties' => false,
163198
],
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(),
168207
],
169-
'description' => 'Remote file reference (when type is remote_file).',
208+
'required' => ['type', 'remoteFile'],
209+
'additionalProperties' => false,
170210
],
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(),
175219
],
176-
'description' => 'Function call request (when type is function_call).',
220+
'required' => ['type', 'functionCall'],
221+
'additionalProperties' => false,
177222
],
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(),
182231
],
183-
'description' => 'Function response (when type is function_response).',
232+
'required' => ['type', 'functionResponse'],
233+
'additionalProperties' => false,
184234
],
185235
],
186-
'required' => ['type'],
187236
];
188237
}
189238
}

0 commit comments

Comments
 (0)