diff --git a/src/Contracts/Resources/ThreadsMessagesContract.php b/src/Contracts/Resources/ThreadsMessagesContract.php index 8e632cdd..bbb6a482 100644 --- a/src/Contracts/Resources/ThreadsMessagesContract.php +++ b/src/Contracts/Resources/ThreadsMessagesContract.php @@ -51,4 +51,11 @@ public function delete(string $threadId, string $messageId): ThreadMessageDelete * @param array $parameters */ public function list(string $threadId, array $parameters = []): ThreadMessageListResponse; + + /** + * Manage files attached to a thred message. + * + * @see https://platform.openai.com/docs/api-reference/messages/file-object + */ + public function files(): ThreadsMessagesFilesContract; } diff --git a/src/Contracts/Resources/ThreadsMessagesFilesContract.php b/src/Contracts/Resources/ThreadsMessagesFilesContract.php new file mode 100644 index 00000000..c8218c35 --- /dev/null +++ b/src/Contracts/Resources/ThreadsMessagesFilesContract.php @@ -0,0 +1,28 @@ + $parameters + */ + public function list(string $threadId, string $messageId, array $parameters = []): ThreadMessageFileListResponse; +} diff --git a/src/Resources/ThreadsMessages.php b/src/Resources/ThreadsMessages.php index 2b92d500..b815695c 100644 --- a/src/Resources/ThreadsMessages.php +++ b/src/Resources/ThreadsMessages.php @@ -7,6 +7,7 @@ use OpenAI\Contracts\Resources\ListAssistantsResponse; use OpenAI\Contracts\Resources\ThreadsContract; use OpenAI\Contracts\Resources\ThreadsMessagesContract; +use OpenAI\Contracts\Resources\ThreadsMessagesFilesContract; use OpenAI\Responses\Threads\Messages\ThreadMessageDeleteResponse; use OpenAI\Responses\Threads\Messages\ThreadMessageListResponse; use OpenAI\Responses\Threads\Messages\ThreadMessageResponse; @@ -100,4 +101,14 @@ public function list(string $threadId, array $parameters = []): ThreadMessageLis return ThreadMessageListResponse::from($response->data(), $response->meta()); } + + /** + * Manage files attached to a thred message. + * + * @see https://platform.openai.com/docs/api-reference/messages/file-object + */ + public function files(): ThreadsMessagesFilesContract + { + return new ThreadsMessagesFiles($this->transporter); + } } diff --git a/src/Resources/ThreadsMessagesFiles.php b/src/Resources/ThreadsMessagesFiles.php new file mode 100644 index 00000000..7d593e52 --- /dev/null +++ b/src/Resources/ThreadsMessagesFiles.php @@ -0,0 +1,54 @@ +}> $response */ + $response = $this->transporter->requestObject($payload); + + return ThreadMessageFileResponse::from($response->data(), $response->meta()); + } + + /** + * Returns a list of message files. + * + * @see https://platform.openai.com/docs/api-reference/messages/listMessageFiles + * + * @param array $parameters + */ + public function list(string $threadId, string $messageId, array $parameters = []): ThreadMessageFileListResponse + { + $payload = Payload::list("threads/$threadId/messages/$messageId/files", $parameters); + + /** @var Response}>}> $response */ + $response = $this->transporter->requestObject($payload); + + return ThreadMessageFileListResponse::from($response->data(), $response->meta()); + } +} diff --git a/src/Responses/Threads/Messages/Files/ThreadMessageFileListResponse.php b/src/Responses/Threads/Messages/Files/ThreadMessageFileListResponse.php new file mode 100644 index 00000000..5a062af2 --- /dev/null +++ b/src/Responses/Threads/Messages/Files/ThreadMessageFileListResponse.php @@ -0,0 +1,78 @@ +, status: string, validation_file: ?string, training_file: string, trained_tokens: ?int}>, has_more: bool}> + */ +final class ThreadMessageFileListResponse implements ResponseContract, ResponseHasMetaInformationContract +{ + /** + * @use ArrayAccessible, status: string, validation_file: ?string, training_file: string, trained_tokens: ?int}>, has_more: bool}> + */ + use ArrayAccessible; + + use Fakeable; + use HasMetaInformation; + + /** + * @param array $data + */ + private function __construct( + public readonly string $object, + public readonly array $data, + public readonly ?string $firstId, + public readonly ?string $lastId, + public readonly bool $hasMore, + private readonly MetaInformation $meta, + ) { + } + + /** + * Acts as static factory, and returns a new Response instance. + * + * @param array{object: string, data: array, status: string, validation_file: ?string, training_file: string, trained_tokens: ?int}>, has_more: bool} $attributes + */ + public static function from(array $attributes, MetaInformation $meta): self + { + $data = array_map(fn (array $result): ThreadMessageFileResponse => ThreadMessageFileResponse::from( + $result, + $meta, + ), $attributes['data']); + + return new self( + $attributes['object'], + $data, + $attributes['first_id'], + $attributes['last_id'], + $attributes['has_more'], + $meta, + ); + } + + /** + * {@inheritDoc} + */ + public function toArray(): array + { + return [ + 'object' => $this->object, + 'data' => array_map( + static fn (ThreadMessageFileResponse $response): array => $response->toArray(), + $this->data, + ), + 'first_id' => $this->firstId, + 'last_id' => $this->lastId, + 'has_more' => $this->hasMore, + ]; + } +} diff --git a/src/Responses/Threads/Messages/Files/ThreadMessageFileResponse.php b/src/Responses/Threads/Messages/Files/ThreadMessageFileResponse.php new file mode 100644 index 00000000..4d69f871 --- /dev/null +++ b/src/Responses/Threads/Messages/Files/ThreadMessageFileResponse.php @@ -0,0 +1,68 @@ +, temperature: float, avg_logprob: float, compression_ratio: float, no_speech_prob: float, transient: bool}>, text: string}> + */ +final class ThreadMessageFileResponse implements ResponseContract, ResponseHasMetaInformationContract +{ + /** + * @use ArrayAccessible, temperature: float, avg_logprob: float, compression_ratio: float, no_speech_prob: float, transient: bool}>, text: string}> + */ + use ArrayAccessible; + + use Fakeable; + use HasMetaInformation; + + /** + * @param array $content + */ + private function __construct( + public string $id, + public string $object, + public int $createdAt, + public string $messageId, + private readonly MetaInformation $meta, + ) + { + } + + /** + * Acts as static factory, and returns a new Response instance. + * + * @param array{task: ?string, language: ?string, duration: ?float, segments: array, temperature: float, avg_logprob: float, compression_ratio: float, no_speech_prob: float, transient: bool}>, text: string}|string $attributes + */ + public static function from(array|string $attributes, MetaInformation $meta): self + { + return new self( + $attributes['id'], + $attributes['object'], + $attributes['created_at'], + $attributes['message_id'], + $meta, + ); + } + + /** + * {@inheritDoc} + */ + public function toArray(): array + { + return [ + 'id' => $this->id, + 'object' => $this->object, + 'created_at' => $this->createdAt, + 'message_id' => $this->messageId, + ]; + } +}