Skip to content

Commit f00469e

Browse files
authored
fix(OpenAI): add support for response 'compaction' (#778)
1 parent caaea7d commit f00469e

6 files changed

Lines changed: 122 additions & 5 deletions

File tree

src/Actions/Responses/OutputObjects.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace OpenAI\Actions\Responses;
66

77
use OpenAI\Responses\Responses\Output\OutputCodeInterpreterToolCall;
8+
use OpenAI\Responses\Responses\Output\OutputCompaction;
89
use OpenAI\Responses\Responses\Output\OutputComputerToolCall;
910
use OpenAI\Responses\Responses\Output\OutputCustomToolCall;
1011
use OpenAI\Responses\Responses\Output\OutputFileSearchToolCall;
@@ -32,13 +33,14 @@
3233
* @phpstan-import-type OutputMcpCallType from OutputMcpCall
3334
* @phpstan-import-type OutputImageGenerationToolCallType from OutputImageGenerationToolCall
3435
* @phpstan-import-type OutputCodeInterpreterToolCallType from OutputCodeInterpreterToolCall
36+
* @phpstan-import-type OutputCompactionType from OutputCompaction
3537
* @phpstan-import-type OutputLocalShellCallType from OutputLocalShellCall
3638
* @phpstan-import-type OutputCustomToolCallType from OutputCustomToolCall
3739
* @phpstan-import-type OutputToolSearchCallType from OutputToolSearchCall
3840
* @phpstan-import-type OutputToolSearchOutputType from OutputToolSearchOutput
3941
*
40-
* @phpstan-type ResponseOutputObjectTypes array<int, OutputComputerToolCallType|OutputFileSearchToolCallType|OutputFunctionToolCallType|OutputMessageType|OutputReasoningType|OutputWebSearchToolCallType|OutputMcpListToolsType|OutputMcpApprovalRequestType|OutputMcpCallType|OutputImageGenerationToolCallType|OutputCodeInterpreterToolCallType|OutputLocalShellCallType|OutputCustomToolCallType|OutputToolSearchCallType|OutputToolSearchOutputType>
41-
* @phpstan-type ResponseOutputObjectReturnType array<int, OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall|OutputLocalShellCall|OutputCustomToolCall|OutputToolSearchCall|OutputToolSearchOutput>
42+
* @phpstan-type ResponseOutputObjectTypes array<int, OutputComputerToolCallType|OutputFileSearchToolCallType|OutputFunctionToolCallType|OutputMessageType|OutputReasoningType|OutputWebSearchToolCallType|OutputMcpListToolsType|OutputMcpApprovalRequestType|OutputMcpCallType|OutputImageGenerationToolCallType|OutputCodeInterpreterToolCallType|OutputLocalShellCallType|OutputCustomToolCallType|OutputToolSearchCallType|OutputToolSearchOutputType|OutputCompactionType>
43+
* @phpstan-type ResponseOutputObjectReturnType array<int, OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall|OutputLocalShellCall|OutputCustomToolCall|OutputToolSearchCall|OutputToolSearchOutput|OutputCompaction>
4244
*/
4345
final class OutputObjects
4446
{
@@ -49,7 +51,7 @@ final class OutputObjects
4951
public static function parse(array $outputItems): array
5052
{
5153
return array_map(
52-
fn (array $item): OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall|OutputLocalShellCall|OutputCustomToolCall|OutputToolSearchCall|OutputToolSearchOutput => match ($item['type']) {
54+
fn (array $item): OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall|OutputLocalShellCall|OutputCustomToolCall|OutputToolSearchCall|OutputToolSearchOutput|OutputCompaction => match ($item['type']) {
5355
'message' => OutputMessage::from($item),
5456
'file_search_call' => OutputFileSearchToolCall::from($item),
5557
'function_call' => OutputFunctionToolCall::from($item),
@@ -65,6 +67,7 @@ public static function parse(array $outputItems): array
6567
'custom_tool_call' => OutputCustomToolCall::from($item),
6668
'tool_search_call' => OutputToolSearchCall::from($item),
6769
'tool_search_output' => OutputToolSearchOutput::from($item),
70+
'compaction' => OutputCompaction::from($item),
6871
default => throw new \UnexpectedValueException('Uh oh! We do not recognize this type. Please submit a bug to openai-php/client on GitHub!'),
6972
},
7073
$outputItems,

src/Responses/Responses/CreateResponse.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use OpenAI\Responses\Concerns\HasMetaInformation;
1515
use OpenAI\Responses\Meta\MetaInformation;
1616
use OpenAI\Responses\Responses\Output\OutputCodeInterpreterToolCall;
17+
use OpenAI\Responses\Responses\Output\OutputCompaction;
1718
use OpenAI\Responses\Responses\Output\OutputComputerToolCall;
1819
use OpenAI\Responses\Responses\Output\OutputCustomToolCall;
1920
use OpenAI\Responses\Responses\Output\OutputFileSearchToolCall;
@@ -195,7 +196,7 @@ public function toArray(): array
195196
'metadata' => $this->metadata ?? [],
196197
'model' => $this->model,
197198
'output' => array_map(
198-
fn (OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall|OutputLocalShellCall|OutputCustomToolCall|OutputToolSearchCall|OutputToolSearchOutput $output): array => $output->toArray(),
199+
fn (OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall|OutputLocalShellCall|OutputCustomToolCall|OutputToolSearchCall|OutputToolSearchOutput|OutputCompaction $output): array => $output->toArray(),
199200
$this->output
200201
),
201202
'parallel_tool_calls' => $this->parallelToolCalls,
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenAI\Responses\Responses\Output;
6+
7+
use OpenAI\Contracts\ResponseContract;
8+
use OpenAI\Responses\Concerns\ArrayAccessible;
9+
use OpenAI\Testing\Responses\Concerns\Fakeable;
10+
11+
/**
12+
* @phpstan-type OutputCompactionType array{id: string, encrypted_content: string, type: 'compaction', created_by?: string|null}
13+
*
14+
* @implements ResponseContract<OutputCompactionType>
15+
*/
16+
final class OutputCompaction implements ResponseContract
17+
{
18+
/**
19+
* @use ArrayAccessible<OutputCompactionType>
20+
*/
21+
use ArrayAccessible;
22+
23+
use Fakeable;
24+
25+
/**
26+
* @param 'compaction' $type
27+
*/
28+
private function __construct(
29+
public readonly string $id,
30+
public readonly string $encryptedContent,
31+
public readonly string $type,
32+
public readonly ?string $createdBy,
33+
) {}
34+
35+
/**
36+
* @param OutputCompactionType $attributes
37+
*/
38+
public static function from(array $attributes): self
39+
{
40+
return new self(
41+
id: $attributes['id'],
42+
encryptedContent: $attributes['encrypted_content'],
43+
type: $attributes['type'],
44+
createdBy: $attributes['created_by'] ?? null,
45+
);
46+
}
47+
48+
/**
49+
* {@inheritDoc}
50+
*/
51+
public function toArray(): array
52+
{
53+
return [
54+
'id' => $this->id,
55+
'encrypted_content' => $this->encryptedContent,
56+
'type' => $this->type,
57+
'created_by' => $this->createdBy,
58+
];
59+
}
60+
}

src/Responses/Responses/RetrieveResponse.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use OpenAI\Responses\Concerns\HasMetaInformation;
1515
use OpenAI\Responses\Meta\MetaInformation;
1616
use OpenAI\Responses\Responses\Output\OutputCodeInterpreterToolCall;
17+
use OpenAI\Responses\Responses\Output\OutputCompaction;
1718
use OpenAI\Responses\Responses\Output\OutputComputerToolCall;
1819
use OpenAI\Responses\Responses\Output\OutputCustomToolCall;
1920
use OpenAI\Responses\Responses\Output\OutputFileSearchToolCall;
@@ -195,7 +196,7 @@ public function toArray(): array
195196
'metadata' => $this->metadata ?? [],
196197
'model' => $this->model,
197198
'output' => array_map(
198-
fn (OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall|OutputLocalShellCall|OutputCustomToolCall|OutputToolSearchCall|OutputToolSearchOutput $output): array => $output->toArray(),
199+
fn (OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall|OutputLocalShellCall|OutputCustomToolCall|OutputToolSearchCall|OutputToolSearchOutput|OutputCompaction $output): array => $output->toArray(),
199200
$this->output
200201
),
201202
'output_text' => $this->outputText,

tests/Fixtures/Responses.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,19 @@ function outputToolSearchOutput(): array
983983
];
984984
}
985985

986+
/**
987+
* @return array<string, mixed>
988+
*/
989+
function outputCompaction(): array
990+
{
991+
return [
992+
'id' => 'cmp_67ccf18f64008190a39b619f4c8455ef087bb177ab789d5c',
993+
'encrypted_content' => 'encrypted_string_value',
994+
'type' => 'compaction',
995+
'created_by' => 'user_123',
996+
];
997+
}
998+
986999
/**
9871000
* @return resource
9881001
*/
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
use OpenAI\Responses\Responses\Output\OutputCompaction;
4+
5+
test('from', function () {
6+
$response = OutputCompaction::from(outputCompaction());
7+
8+
expect($response)
9+
->toBeInstanceOf(OutputCompaction::class)
10+
->id->toBe('cmp_67ccf18f64008190a39b619f4c8455ef087bb177ab789d5c')
11+
->encryptedContent->toBe('encrypted_string_value')
12+
->type->toBe('compaction')
13+
->createdBy->toBe('user_123');
14+
});
15+
16+
test('from without created_by', function () {
17+
$attributes = outputCompaction();
18+
unset($attributes['created_by']);
19+
20+
$response = OutputCompaction::from($attributes);
21+
22+
expect($response)
23+
->toBeInstanceOf(OutputCompaction::class)
24+
->createdBy->toBeNull();
25+
});
26+
27+
test('as array accessible', function () {
28+
$response = OutputCompaction::from(outputCompaction());
29+
30+
expect($response['id'])->toBe('cmp_67ccf18f64008190a39b619f4c8455ef087bb177ab789d5c');
31+
});
32+
33+
test('to array', function () {
34+
$response = OutputCompaction::from(outputCompaction());
35+
36+
expect($response->toArray())
37+
->toBeArray()
38+
->toBe(outputCompaction());
39+
});

0 commit comments

Comments
 (0)