Skip to content

Commit 9b84990

Browse files
authored
feat(OpenAI): add support for apply_patch tool in Responses API (#794)
* Support streaming compaction output * Add apply_patch tool support * Add apply_patch fake fixtures * Add apply patch stream events
1 parent e6ba99f commit 9b84990

41 files changed

Lines changed: 1012 additions & 30 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/Actions/Conversations/ItemObjects.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
namespace OpenAI\Actions\Conversations;
66

77
use OpenAI\Responses\Conversations\Objects\Message;
8+
use OpenAI\Responses\Responses\Input\ApplyPatchToolCallOutput;
89
use OpenAI\Responses\Responses\Input\ComputerToolCallOutput;
910
use OpenAI\Responses\Responses\Input\CustomToolCallOutput;
1011
use OpenAI\Responses\Responses\Input\FunctionToolCallOutput;
1112
use OpenAI\Responses\Responses\Input\LocalShellCallOutput;
1213
use OpenAI\Responses\Responses\Input\McpApprovalResponse;
14+
use OpenAI\Responses\Responses\Output\OutputApplyPatchToolCall;
1315
use OpenAI\Responses\Responses\Output\OutputCodeInterpreterToolCall;
1416
use OpenAI\Responses\Responses\Output\OutputComputerToolCall;
1517
use OpenAI\Responses\Responses\Output\OutputCustomToolCall;
@@ -26,13 +28,15 @@
2628
use OpenAI\Responses\Responses\Output\OutputWebSearchToolCall;
2729

2830
/**
31+
* @phpstan-import-type ApplyPatchToolCallOutputType from ApplyPatchToolCallOutput
2932
* @phpstan-import-type MessageType from Message
3033
* @phpstan-import-type ComputerToolCallOutputType from ComputerToolCallOutput
3134
* @phpstan-import-type FunctionToolCallOutputType from FunctionToolCallOutput
3235
* @phpstan-import-type LocalShellCallOutputType from LocalShellCallOutput
3336
* @phpstan-import-type McpApprovalResponseType from McpApprovalResponse
3437
* @phpstan-import-type CustomToolCallOutputType from CustomToolCallOutput
3538
* @phpstan-import-type OutputComputerToolCallType from OutputComputerToolCall
39+
* @phpstan-import-type OutputApplyPatchToolCallType from OutputApplyPatchToolCall
3640
* @phpstan-import-type OutputFileSearchToolCallType from OutputFileSearchToolCall
3741
* @phpstan-import-type OutputFunctionToolCallType from OutputFunctionToolCall
3842
* @phpstan-import-type OutputReasoningType from OutputReasoning
@@ -47,9 +51,9 @@
4751
* @phpstan-import-type OutputProgramType from OutputProgram
4852
* @phpstan-import-type OutputProgramOutputType from OutputProgramOutput
4953
*
50-
* @phpstan-type ItemObjectTypes MessageType|ComputerToolCallOutputType|FunctionToolCallOutputType|LocalShellCallOutputType|McpApprovalResponseType|CustomToolCallOutputType|OutputComputerToolCallType|OutputFileSearchToolCallType|OutputFunctionToolCallType|OutputReasoningType|OutputWebSearchToolCallType|OutputMcpListToolsType|OutputMcpApprovalRequestType|OutputMcpCallType|OutputImageGenerationToolCallType|OutputCodeInterpreterToolCallType|OutputLocalShellCallType|OutputCustomToolCallType|OutputProgramType|OutputProgramOutputType
54+
* @phpstan-type ItemObjectTypes ApplyPatchToolCallOutputType|MessageType|ComputerToolCallOutputType|FunctionToolCallOutputType|LocalShellCallOutputType|McpApprovalResponseType|CustomToolCallOutputType|OutputApplyPatchToolCallType|OutputComputerToolCallType|OutputFileSearchToolCallType|OutputFunctionToolCallType|OutputReasoningType|OutputWebSearchToolCallType|OutputMcpListToolsType|OutputMcpApprovalRequestType|OutputMcpCallType|OutputImageGenerationToolCallType|OutputCodeInterpreterToolCallType|OutputLocalShellCallType|OutputCustomToolCallType|OutputProgramType|OutputProgramOutputType
5155
* @phpstan-type ConversationItemObjectTypes array<int, ItemObjectTypes>
52-
* @phpstan-type ConversationItemObjectReturnType array<int, Message|ComputerToolCallOutput|FunctionToolCallOutput|LocalShellCallOutput|McpApprovalResponse|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall|OutputLocalShellCall|OutputCustomToolCall|CustomToolCallOutput|OutputProgram|OutputProgramOutput>
56+
* @phpstan-type ConversationItemObjectReturnType array<int, ApplyPatchToolCallOutput|Message|ComputerToolCallOutput|FunctionToolCallOutput|LocalShellCallOutput|McpApprovalResponse|OutputApplyPatchToolCall|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall|OutputLocalShellCall|OutputCustomToolCall|CustomToolCallOutput|OutputProgram|OutputProgramOutput>
5357
*/
5458
final class ItemObjects
5559
{
@@ -60,7 +64,7 @@ final class ItemObjects
6064
public static function parse(array $outputItems): array
6165
{
6266
return array_map(
63-
fn (array $item): Message|ComputerToolCallOutput|FunctionToolCallOutput|LocalShellCallOutput|McpApprovalResponse|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall|OutputLocalShellCall|OutputCustomToolCall|CustomToolCallOutput|OutputProgram|OutputProgramOutput => match ($item['type']) {
67+
fn (array $item): ApplyPatchToolCallOutput|Message|ComputerToolCallOutput|FunctionToolCallOutput|LocalShellCallOutput|McpApprovalResponse|OutputApplyPatchToolCall|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall|OutputLocalShellCall|OutputCustomToolCall|CustomToolCallOutput|OutputProgram|OutputProgramOutput => match ($item['type']) {
6468
'message' => Message::from($item),
6569
'file_search_call' => OutputFileSearchToolCall::from($item),
6670
'function_call' => OutputFunctionToolCall::from($item),
@@ -81,6 +85,8 @@ public static function parse(array $outputItems): array
8185
'mcp_approval_response' => McpApprovalResponse::from($item),
8286
'program' => OutputProgram::from($item),
8387
'program_output' => OutputProgramOutput::from($item),
88+
'apply_patch_call' => OutputApplyPatchToolCall::from($item),
89+
'apply_patch_call_output' => ApplyPatchToolCallOutput::from($item),
8490
},
8591
$outputItems,
8692
);

src/Actions/Responses/ItemObjects.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
namespace OpenAI\Actions\Responses;
66

7+
use OpenAI\Responses\Responses\Input\ApplyPatchToolCallOutput;
78
use OpenAI\Responses\Responses\Input\ComputerToolCallOutput;
89
use OpenAI\Responses\Responses\Input\CustomToolCallOutput;
910
use OpenAI\Responses\Responses\Input\FunctionToolCallOutput;
1011
use OpenAI\Responses\Responses\Input\InputMessage;
1112
use OpenAI\Responses\Responses\Input\LocalShellCallOutput;
1213
use OpenAI\Responses\Responses\Input\McpApprovalResponse;
14+
use OpenAI\Responses\Responses\Output\OutputApplyPatchToolCall;
1315
use OpenAI\Responses\Responses\Output\OutputCodeInterpreterToolCall;
1416
use OpenAI\Responses\Responses\Output\OutputComputerToolCall;
1517
use OpenAI\Responses\Responses\Output\OutputCustomToolCall;
@@ -27,13 +29,15 @@
2729
use OpenAI\Responses\Responses\Output\OutputWebSearchToolCall;
2830

2931
/**
32+
* @phpstan-import-type ApplyPatchToolCallOutputType from ApplyPatchToolCallOutput
3033
* @phpstan-import-type InputMessageType from InputMessage
3134
* @phpstan-import-type ComputerToolCallOutputType from ComputerToolCallOutput
3235
* @phpstan-import-type FunctionToolCallOutputType from FunctionToolCallOutput
3336
* @phpstan-import-type LocalShellCallOutputType from LocalShellCallOutput
3437
* @phpstan-import-type McpApprovalResponseType from McpApprovalResponse
3538
* @phpstan-import-type CustomToolCallOutputType from CustomToolCallOutput
3639
* @phpstan-import-type OutputComputerToolCallType from OutputComputerToolCall
40+
* @phpstan-import-type OutputApplyPatchToolCallType from OutputApplyPatchToolCall
3741
* @phpstan-import-type OutputFileSearchToolCallType from OutputFileSearchToolCall
3842
* @phpstan-import-type OutputFunctionToolCallType from OutputFunctionToolCall
3943
* @phpstan-import-type OutputMessageType from OutputMessage
@@ -49,8 +53,8 @@
4953
* @phpstan-import-type OutputProgramType from OutputProgram
5054
* @phpstan-import-type OutputProgramOutputType from OutputProgramOutput
5155
*
52-
* @phpstan-type ResponseItemObjectTypes array<int, InputMessageType|ComputerToolCallOutputType|FunctionToolCallOutputType|LocalShellCallOutputType|McpApprovalResponseType|CustomToolCallOutputType|OutputComputerToolCallType|OutputFileSearchToolCallType|OutputFunctionToolCallType|OutputMessageType|OutputReasoningType|OutputWebSearchToolCallType|OutputMcpListToolsType|OutputMcpApprovalRequestType|OutputMcpCallType|OutputImageGenerationToolCallType|OutputCodeInterpreterToolCallType|OutputLocalShellCallType|OutputCustomToolCallType|OutputProgramType|OutputProgramOutputType>
53-
* @phpstan-type ResponseItemObjectReturnType array<int, InputMessage|ComputerToolCallOutput|FunctionToolCallOutput|LocalShellCallOutput|McpApprovalResponse|CustomToolCallOutput|OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall|OutputLocalShellCall|OutputCustomToolCall|OutputProgram|OutputProgramOutput>
56+
* @phpstan-type ResponseItemObjectTypes array<int, ApplyPatchToolCallOutputType|InputMessageType|ComputerToolCallOutputType|FunctionToolCallOutputType|LocalShellCallOutputType|McpApprovalResponseType|CustomToolCallOutputType|OutputApplyPatchToolCallType|OutputComputerToolCallType|OutputFileSearchToolCallType|OutputFunctionToolCallType|OutputMessageType|OutputReasoningType|OutputWebSearchToolCallType|OutputMcpListToolsType|OutputMcpApprovalRequestType|OutputMcpCallType|OutputImageGenerationToolCallType|OutputCodeInterpreterToolCallType|OutputLocalShellCallType|OutputCustomToolCallType|OutputProgramType|OutputProgramOutputType>
57+
* @phpstan-type ResponseItemObjectReturnType array<int, ApplyPatchToolCallOutput|InputMessage|ComputerToolCallOutput|FunctionToolCallOutput|LocalShellCallOutput|McpApprovalResponse|CustomToolCallOutput|OutputApplyPatchToolCall|OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall|OutputLocalShellCall|OutputCustomToolCall|OutputProgram|OutputProgramOutput>
5458
*/
5559
final class ItemObjects
5660
{
@@ -61,7 +65,7 @@ final class ItemObjects
6165
public static function parse(array $outputItems): array
6266
{
6367
return array_map(
64-
fn (array $item): InputMessage|ComputerToolCallOutput|FunctionToolCallOutput|LocalShellCallOutput|McpApprovalResponse|CustomToolCallOutput|OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall|OutputLocalShellCall|OutputCustomToolCall|OutputProgram|OutputProgramOutput => match ($item['type']) {
68+
fn (array $item): ApplyPatchToolCallOutput|InputMessage|ComputerToolCallOutput|FunctionToolCallOutput|LocalShellCallOutput|McpApprovalResponse|CustomToolCallOutput|OutputApplyPatchToolCall|OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall|OutputLocalShellCall|OutputCustomToolCall|OutputProgram|OutputProgramOutput => match ($item['type']) {
6569
'message' => $item['role'] === 'assistant' ? OutputMessage::from($item) : InputMessage::from($item),
6670
'file_search_call' => OutputFileSearchToolCall::from($item),
6771
'function_call' => OutputFunctionToolCall::from($item),
@@ -82,6 +86,8 @@ public static function parse(array $outputItems): array
8286
'mcp_approval_response' => McpApprovalResponse::from($item),
8387
'program' => OutputProgram::from($item),
8488
'program_output' => OutputProgramOutput::from($item),
89+
'apply_patch_call' => OutputApplyPatchToolCall::from($item),
90+
'apply_patch_call_output' => ApplyPatchToolCallOutput::from($item),
8591
},
8692
$outputItems,
8793
);

src/Actions/Responses/OutputObjects.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace OpenAI\Actions\Responses;
66

7+
use OpenAI\Responses\Responses\Output\OutputApplyPatchToolCall;
78
use OpenAI\Responses\Responses\Output\OutputCodeInterpreterToolCall;
89
use OpenAI\Responses\Responses\Output\OutputCompaction;
910
use OpenAI\Responses\Responses\Output\OutputComputerToolCall;
@@ -24,6 +25,7 @@
2425
use OpenAI\Responses\Responses\Output\OutputWebSearchToolCall;
2526

2627
/**
28+
* @phpstan-import-type OutputApplyPatchToolCallType from OutputApplyPatchToolCall
2729
* @phpstan-import-type OutputComputerToolCallType from OutputComputerToolCall
2830
* @phpstan-import-type OutputFileSearchToolCallType from OutputFileSearchToolCall
2931
* @phpstan-import-type OutputFunctionToolCallType from OutputFunctionToolCall
@@ -43,8 +45,8 @@
4345
* @phpstan-import-type OutputToolSearchCallType from OutputToolSearchCall
4446
* @phpstan-import-type OutputToolSearchOutputType from OutputToolSearchOutput
4547
*
46-
* @phpstan-type ResponseOutputObjectTypes array<int, OutputComputerToolCallType|OutputFileSearchToolCallType|OutputFunctionToolCallType|OutputMessageType|OutputProgramType|OutputProgramOutputType|OutputReasoningType|OutputWebSearchToolCallType|OutputMcpListToolsType|OutputMcpApprovalRequestType|OutputMcpCallType|OutputImageGenerationToolCallType|OutputCodeInterpreterToolCallType|OutputLocalShellCallType|OutputCustomToolCallType|OutputToolSearchCallType|OutputToolSearchOutputType|OutputCompactionType>
47-
* @phpstan-type ResponseOutputObjectReturnType array<int, OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputProgram|OutputProgramOutput|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall|OutputLocalShellCall|OutputCustomToolCall|OutputToolSearchCall|OutputToolSearchOutput|OutputCompaction>
48+
* @phpstan-type ResponseOutputObjectTypes array<int, OutputApplyPatchToolCallType|OutputComputerToolCallType|OutputFileSearchToolCallType|OutputFunctionToolCallType|OutputMessageType|OutputProgramType|OutputProgramOutputType|OutputReasoningType|OutputWebSearchToolCallType|OutputMcpListToolsType|OutputMcpApprovalRequestType|OutputMcpCallType|OutputImageGenerationToolCallType|OutputCodeInterpreterToolCallType|OutputLocalShellCallType|OutputCustomToolCallType|OutputToolSearchCallType|OutputToolSearchOutputType|OutputCompactionType>
49+
* @phpstan-type ResponseOutputObjectReturnType array<int, OutputApplyPatchToolCall|OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputProgram|OutputProgramOutput|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall|OutputLocalShellCall|OutputCustomToolCall|OutputToolSearchCall|OutputToolSearchOutput|OutputCompaction>
4850
*/
4951
final class OutputObjects
5052
{
@@ -55,7 +57,7 @@ final class OutputObjects
5557
public static function parse(array $outputItems): array
5658
{
5759
return array_map(
58-
fn (array $item): OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputProgram|OutputProgramOutput|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall|OutputLocalShellCall|OutputCustomToolCall|OutputToolSearchCall|OutputToolSearchOutput|OutputCompaction => match ($item['type']) {
60+
fn (array $item): OutputApplyPatchToolCall|OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputProgram|OutputProgramOutput|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall|OutputLocalShellCall|OutputCustomToolCall|OutputToolSearchCall|OutputToolSearchOutput|OutputCompaction => match ($item['type']) {
5961
'message' => OutputMessage::from($item),
6062
'file_search_call' => OutputFileSearchToolCall::from($item),
6163
'function_call' => OutputFunctionToolCall::from($item),
@@ -74,6 +76,7 @@ public static function parse(array $outputItems): array
7476
'tool_search_call' => OutputToolSearchCall::from($item),
7577
'tool_search_output' => OutputToolSearchOutput::from($item),
7678
'compaction' => OutputCompaction::from($item),
79+
'apply_patch_call' => OutputApplyPatchToolCall::from($item),
7780
default => throw new \UnexpectedValueException('Uh oh! We do not recognize this type. Please submit a bug to openai-php/client on GitHub!'),
7881
},
7982
$outputItems,

src/Actions/Responses/ToolChoiceObjects.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static function parse(array|string $toolChoice): string|FunctionToolChoic
2424
{
2525
return is_array($toolChoice)
2626
? match ($toolChoice['type']) {
27-
'file_search', 'web_search', 'web_search_preview', 'computer_use_preview', 'programmatic_tool_calling' => HostedToolChoice::from($toolChoice),
27+
'apply_patch', 'file_search', 'web_search', 'web_search_preview', 'computer_use_preview', 'programmatic_tool_calling' => HostedToolChoice::from($toolChoice),
2828
'function' => FunctionToolChoice::from($toolChoice),
2929
}
3030
: $toolChoice;

0 commit comments

Comments
 (0)