Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/Responses/Responses/Tool/FileSearchTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, string>, filters: ComparisonFilterType|CompoundFilterType|null, max_num_results: int, ranking_options: RankingOptionType}
* @phpstan-type FileSearchToolType array{type: 'file_search', vector_store_ids: array<int, string>, filters: ComparisonFilterType|CompoundFilterType|null, max_num_results: int|null, ranking_options: RankingOptionType|null}
*
* @implements ResponseContract<FileSearchToolType>
*/
Expand All @@ -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,
) {}

/**
Expand All @@ -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,
);
}

Expand All @@ -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(),
];
}
}
39 changes: 39 additions & 0 deletions tests/Responses/Responses/Tool/FileSearchTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down