diff --git a/src/Responses/Responses/Tool/FileSearchTool.php b/src/Responses/Responses/Tool/FileSearchTool.php index 72053147..56b4d081 100644 --- a/src/Responses/Responses/Tool/FileSearchTool.php +++ b/src/Responses/Responses/Tool/FileSearchTool.php @@ -13,7 +13,7 @@ * @phpstan-import-type ComparisonFilterType from FileSearchComparisonFilter * @phpstan-import-type CompoundFilterType from FileSearchCompoundFilter * - * @phpstan-type FileSearchToolType array{type: 'file_search', vector_store_ids: array, filters: ComparisonFilterType|CompoundFilterType|null, max_num_results: int, ranking_options: RankingOptionType} + * @phpstan-type FileSearchToolType array{type: 'file_search', vector_store_ids: array, filters: ComparisonFilterType|CompoundFilterType|null, max_num_results: int|null, ranking_options: RankingOptionType|null} * * @implements ResponseContract */ @@ -34,8 +34,8 @@ private function __construct( public readonly string $type, public readonly array $vectorStoreIds, public readonly FileSearchComparisonFilter|FileSearchCompoundFilter|null $filters, - public readonly int $maxNumResults, - public readonly FileSearchRankingOption $rankingOptions, + public readonly ?int $maxNumResults, + public readonly ?FileSearchRankingOption $rankingOptions, ) {} /** @@ -56,8 +56,8 @@ public static function from(array $attributes): self type: $attributes['type'], vectorStoreIds: $attributes['vector_store_ids'], filters: $filters, - maxNumResults: $attributes['max_num_results'], - rankingOptions: FileSearchRankingOption::from($attributes['ranking_options']), + maxNumResults: $attributes['max_num_results'] ?? null, + rankingOptions: isset($attributes['ranking_options']) ? FileSearchRankingOption::from($attributes['ranking_options']) : null, ); } @@ -71,7 +71,7 @@ public function toArray(): array 'vector_store_ids' => $this->vectorStoreIds, 'filters' => $this->filters?->toArray(), 'max_num_results' => $this->maxNumResults, - 'ranking_options' => $this->rankingOptions->toArray(), + 'ranking_options' => $this->rankingOptions?->toArray(), ]; } } diff --git a/tests/Responses/Responses/Tool/FileSearchTool.php b/tests/Responses/Responses/Tool/FileSearchTool.php index c551eb37..45d527cf 100644 --- a/tests/Responses/Responses/Tool/FileSearchTool.php +++ b/tests/Responses/Responses/Tool/FileSearchTool.php @@ -32,6 +32,45 @@ ->filters->toBeNull(); }); +test('from null ranking options', function () { + $payload = toolFileSearch(); + $payload['ranking_options'] = null; + $response = FileSearchTool::from($payload); + + expect($response) + ->toBeInstanceOf(FileSearchTool::class) + ->rankingOptions->toBeNull(); +}); + +test('from null max num results', function () { + $payload = toolFileSearch(); + $payload['max_num_results'] = null; + $response = FileSearchTool::from($payload); + + expect($response) + ->toBeInstanceOf(FileSearchTool::class) + ->maxNumResults->toBeNull(); +}); + +test('from without optional keys', function () { + $attributes = toolFileSearch(); + + unset($attributes['max_num_results'], $attributes['ranking_options']); + + set_error_handler(static fn (int $errno, string $errstr): bool => throw new ErrorException($errstr), E_WARNING); + + try { + $response = FileSearchTool::from($attributes); + } finally { + restore_error_handler(); + } + + expect($response) + ->toBeInstanceOf(FileSearchTool::class) + ->maxNumResults->toBeNull() + ->rankingOptions->toBeNull(); +}); + test('from complex nested filters', function () { $response = FileSearchTool::from(toolFileSearchNestedFilters());