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
13 changes: 9 additions & 4 deletions src/Responses/Responses/Tool/FileSearchCompoundFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
/**
* @phpstan-import-type ComparisonFilterType from FileSearchComparisonFilter
*
* @phpstan-type CompoundFilterType array{filters: array<int, ComparisonFilterType>, type: 'and'|'or'}
* @phpstan-type CompoundFilterNodeType array{filters: array<int, ComparisonFilterType>, type: 'and'|'or'}
* @phpstan-type CompoundFilterType array{filters: array<int, ComparisonFilterType|CompoundFilterNodeType>, type: 'and'|'or'}
*
* @implements ResponseContract<CompoundFilterType>
*/
Expand All @@ -25,7 +26,7 @@ final class FileSearchCompoundFilter implements ResponseContract
use Fakeable;

/**
* @param array<int, FileSearchComparisonFilter> $filters
* @param array<int, FileSearchComparisonFilter|FileSearchCompoundFilter> $filters
* @param 'and'|'or' $type
*/
private function __construct(
Expand All @@ -39,7 +40,10 @@ private function __construct(
public static function from(array $attributes): self
{
$filters = array_map(
static fn (array $filter): FileSearchComparisonFilter => FileSearchComparisonFilter::from($filter),
static fn (array $filter): FileSearchComparisonFilter|FileSearchCompoundFilter => match ($filter['type']) {
'eq', 'ne', 'gt', 'gte', 'lt', 'lte' => FileSearchComparisonFilter::from($filter),
'and', 'or' => FileSearchCompoundFilter::from($filter),
},
$attributes['filters'],
);

Expand All @@ -54,9 +58,10 @@ public static function from(array $attributes): self
*/
public function toArray(): array
{
// @phpstan-ignore-next-line
return [
'filters' => array_map(
static fn (FileSearchComparisonFilter $filter): array => $filter->toArray(),
static fn (FileSearchComparisonFilter|FileSearchCompoundFilter $filter): array => $filter->toArray(),
$this->filters,
),
'type' => $this->type,
Expand Down
39 changes: 39 additions & 0 deletions tests/Fixtures/Responses.php
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,45 @@ function toolFileSearch(): array
];
}

/**
* @return array<string, mixed>
*/
function toolFileSearchNestedFilters(): array
{
return [
'type' => 'file_search',
'filters' => [
'type' => 'and',
'filters' => [
[
'type' => 'and',
'filters' => [
[
'type' => 'ne',
'key' => 'state',
'value' => 'ks',
],
[
'type' => 'ne',
'key' => 'state',
'value' => 'mo',
],
],
],
],
],
'max_num_results' => 5,
'ranking_options' => [
'ranker' => 'auto',
'score_threshold' => 0.1,
],
'vector_store_ids' => [
'vector_store_id_1',
'vector_store_id_2',
],
];
}

/**
* @return resource
*/
Expand Down
17 changes: 17 additions & 0 deletions tests/Responses/Responses/Tool/FileSearchTool.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use OpenAI\Responses\Responses\Tool\FileSearchComparisonFilter;
use OpenAI\Responses\Responses\Tool\FileSearchCompoundFilter;
use OpenAI\Responses\Responses\Tool\FileSearchRankingOption;
use OpenAI\Responses\Responses\Tool\FileSearchTool;

Expand Down Expand Up @@ -31,6 +32,22 @@
->filters->toBeNull();
});

test('from complex nested filters', function () {
$response = FileSearchTool::from(toolFileSearchNestedFilters());

expect($response)
->toBeInstanceOf(FileSearchTool::class)
->filters->toBeInstanceOf(FileSearchCompoundFilter::class)
->filters->filters->toBeArray()
->and($response->filters->filters[0])
->toBeInstanceOf(FileSearchCompoundFilter::class)
->filters->toBeArray()
->and($response->filters->filters[0]->filters[0])
->toBeInstanceOf(FileSearchComparisonFilter::class)
->and($response->filters->filters[0]->filters[1])
->toBeInstanceOf(FileSearchComparisonFilter::class);
});

test('from results', function () {
$response = FileSearchTool::from(toolFileSearch());

Expand Down