diff --git a/src/Actions/Responses/OutputObjects.php b/src/Actions/Responses/OutputObjects.php index fca94ba6..de7955d5 100644 --- a/src/Actions/Responses/OutputObjects.php +++ b/src/Actions/Responses/OutputObjects.php @@ -5,6 +5,7 @@ namespace OpenAI\Actions\Responses; use OpenAI\Responses\Responses\Output\OutputCodeInterpreterToolCall; +use OpenAI\Responses\Responses\Output\OutputCompaction; use OpenAI\Responses\Responses\Output\OutputComputerToolCall; use OpenAI\Responses\Responses\Output\OutputCustomToolCall; use OpenAI\Responses\Responses\Output\OutputFileSearchToolCall; @@ -32,13 +33,14 @@ * @phpstan-import-type OutputMcpCallType from OutputMcpCall * @phpstan-import-type OutputImageGenerationToolCallType from OutputImageGenerationToolCall * @phpstan-import-type OutputCodeInterpreterToolCallType from OutputCodeInterpreterToolCall + * @phpstan-import-type OutputCompactionType from OutputCompaction * @phpstan-import-type OutputLocalShellCallType from OutputLocalShellCall * @phpstan-import-type OutputCustomToolCallType from OutputCustomToolCall * @phpstan-import-type OutputToolSearchCallType from OutputToolSearchCall * @phpstan-import-type OutputToolSearchOutputType from OutputToolSearchOutput * - * @phpstan-type ResponseOutputObjectTypes array - * @phpstan-type ResponseOutputObjectReturnType array + * @phpstan-type ResponseOutputObjectTypes array + * @phpstan-type ResponseOutputObjectReturnType array */ final class OutputObjects { @@ -49,7 +51,7 @@ final class OutputObjects public static function parse(array $outputItems): array { return array_map( - fn (array $item): OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall|OutputLocalShellCall|OutputCustomToolCall|OutputToolSearchCall|OutputToolSearchOutput => match ($item['type']) { + fn (array $item): OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall|OutputLocalShellCall|OutputCustomToolCall|OutputToolSearchCall|OutputToolSearchOutput|OutputCompaction => match ($item['type']) { 'message' => OutputMessage::from($item), 'file_search_call' => OutputFileSearchToolCall::from($item), 'function_call' => OutputFunctionToolCall::from($item), @@ -65,6 +67,7 @@ public static function parse(array $outputItems): array 'custom_tool_call' => OutputCustomToolCall::from($item), 'tool_search_call' => OutputToolSearchCall::from($item), 'tool_search_output' => OutputToolSearchOutput::from($item), + 'compaction' => OutputCompaction::from($item), default => throw new \UnexpectedValueException('Uh oh! We do not recognize this type. Please submit a bug to openai-php/client on GitHub!'), }, $outputItems, diff --git a/src/Responses/Responses/CreateResponse.php b/src/Responses/Responses/CreateResponse.php index 6c4a7852..9078a76f 100644 --- a/src/Responses/Responses/CreateResponse.php +++ b/src/Responses/Responses/CreateResponse.php @@ -14,6 +14,7 @@ use OpenAI\Responses\Concerns\HasMetaInformation; use OpenAI\Responses\Meta\MetaInformation; use OpenAI\Responses\Responses\Output\OutputCodeInterpreterToolCall; +use OpenAI\Responses\Responses\Output\OutputCompaction; use OpenAI\Responses\Responses\Output\OutputComputerToolCall; use OpenAI\Responses\Responses\Output\OutputCustomToolCall; use OpenAI\Responses\Responses\Output\OutputFileSearchToolCall; @@ -195,7 +196,7 @@ public function toArray(): array 'metadata' => $this->metadata ?? [], 'model' => $this->model, 'output' => array_map( - fn (OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall|OutputLocalShellCall|OutputCustomToolCall|OutputToolSearchCall|OutputToolSearchOutput $output): array => $output->toArray(), + fn (OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall|OutputLocalShellCall|OutputCustomToolCall|OutputToolSearchCall|OutputToolSearchOutput|OutputCompaction $output): array => $output->toArray(), $this->output ), 'parallel_tool_calls' => $this->parallelToolCalls, diff --git a/src/Responses/Responses/Output/OutputCompaction.php b/src/Responses/Responses/Output/OutputCompaction.php new file mode 100644 index 00000000..cfb1b47b --- /dev/null +++ b/src/Responses/Responses/Output/OutputCompaction.php @@ -0,0 +1,60 @@ + + */ +final class OutputCompaction implements ResponseContract +{ + /** + * @use ArrayAccessible + */ + use ArrayAccessible; + + use Fakeable; + + /** + * @param 'compaction' $type + */ + private function __construct( + public readonly string $id, + public readonly string $encryptedContent, + public readonly string $type, + public readonly ?string $createdBy, + ) {} + + /** + * @param OutputCompactionType $attributes + */ + public static function from(array $attributes): self + { + return new self( + id: $attributes['id'], + encryptedContent: $attributes['encrypted_content'], + type: $attributes['type'], + createdBy: $attributes['created_by'] ?? null, + ); + } + + /** + * {@inheritDoc} + */ + public function toArray(): array + { + return [ + 'id' => $this->id, + 'encrypted_content' => $this->encryptedContent, + 'type' => $this->type, + 'created_by' => $this->createdBy, + ]; + } +} diff --git a/src/Responses/Responses/RetrieveResponse.php b/src/Responses/Responses/RetrieveResponse.php index 2c1d9909..aab18991 100644 --- a/src/Responses/Responses/RetrieveResponse.php +++ b/src/Responses/Responses/RetrieveResponse.php @@ -14,6 +14,7 @@ use OpenAI\Responses\Concerns\HasMetaInformation; use OpenAI\Responses\Meta\MetaInformation; use OpenAI\Responses\Responses\Output\OutputCodeInterpreterToolCall; +use OpenAI\Responses\Responses\Output\OutputCompaction; use OpenAI\Responses\Responses\Output\OutputComputerToolCall; use OpenAI\Responses\Responses\Output\OutputCustomToolCall; use OpenAI\Responses\Responses\Output\OutputFileSearchToolCall; @@ -195,7 +196,7 @@ public function toArray(): array 'metadata' => $this->metadata ?? [], 'model' => $this->model, 'output' => array_map( - fn (OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall|OutputLocalShellCall|OutputCustomToolCall|OutputToolSearchCall|OutputToolSearchOutput $output): array => $output->toArray(), + fn (OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall|OutputLocalShellCall|OutputCustomToolCall|OutputToolSearchCall|OutputToolSearchOutput|OutputCompaction $output): array => $output->toArray(), $this->output ), 'output_text' => $this->outputText, diff --git a/tests/Fixtures/Responses.php b/tests/Fixtures/Responses.php index 5dfb701e..36fd6a2a 100644 --- a/tests/Fixtures/Responses.php +++ b/tests/Fixtures/Responses.php @@ -983,6 +983,19 @@ function outputToolSearchOutput(): array ]; } +/** + * @return array + */ +function outputCompaction(): array +{ + return [ + 'id' => 'cmp_67ccf18f64008190a39b619f4c8455ef087bb177ab789d5c', + 'encrypted_content' => 'encrypted_string_value', + 'type' => 'compaction', + 'created_by' => 'user_123', + ]; +} + /** * @return resource */ diff --git a/tests/Responses/Responses/Output/OutputCompaction.php b/tests/Responses/Responses/Output/OutputCompaction.php new file mode 100644 index 00000000..b75ab96f --- /dev/null +++ b/tests/Responses/Responses/Output/OutputCompaction.php @@ -0,0 +1,39 @@ +toBeInstanceOf(OutputCompaction::class) + ->id->toBe('cmp_67ccf18f64008190a39b619f4c8455ef087bb177ab789d5c') + ->encryptedContent->toBe('encrypted_string_value') + ->type->toBe('compaction') + ->createdBy->toBe('user_123'); +}); + +test('from without created_by', function () { + $attributes = outputCompaction(); + unset($attributes['created_by']); + + $response = OutputCompaction::from($attributes); + + expect($response) + ->toBeInstanceOf(OutputCompaction::class) + ->createdBy->toBeNull(); +}); + +test('as array accessible', function () { + $response = OutputCompaction::from(outputCompaction()); + + expect($response['id'])->toBe('cmp_67ccf18f64008190a39b619f4c8455ef087bb177ab789d5c'); +}); + +test('to array', function () { + $response = OutputCompaction::from(outputCompaction()); + + expect($response->toArray()) + ->toBeArray() + ->toBe(outputCompaction()); +});