Skip to content

Commit a00d775

Browse files
authored
refactor(meta): unify response types into Actions (#671)
* refactor(meta): unify response types into Actions * chore: unify types on output text for less duplication * chore: unify types on tool choice for less duplication * chore: unify types on tools for less duplication * chore: unify types on output for less duplication * chore: restore longer type for proper ide usage * chore: remove unused type
1 parent 78320df commit a00d775

File tree

9 files changed

+272
-182
lines changed

9 files changed

+272
-182
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenAI\Actions\Responses;
6+
7+
use OpenAI\Responses\Responses\Input\ComputerToolCallOutput;
8+
use OpenAI\Responses\Responses\Input\FunctionToolCallOutput;
9+
use OpenAI\Responses\Responses\Input\InputMessage;
10+
use OpenAI\Responses\Responses\Output\OutputCodeInterpreterToolCall;
11+
use OpenAI\Responses\Responses\Output\OutputComputerToolCall;
12+
use OpenAI\Responses\Responses\Output\OutputFileSearchToolCall;
13+
use OpenAI\Responses\Responses\Output\OutputFunctionToolCall;
14+
use OpenAI\Responses\Responses\Output\OutputImageGenerationToolCall;
15+
use OpenAI\Responses\Responses\Output\OutputMcpApprovalRequest;
16+
use OpenAI\Responses\Responses\Output\OutputMcpCall;
17+
use OpenAI\Responses\Responses\Output\OutputMcpListTools;
18+
use OpenAI\Responses\Responses\Output\OutputMessage;
19+
use OpenAI\Responses\Responses\Output\OutputReasoning;
20+
use OpenAI\Responses\Responses\Output\OutputWebSearchToolCall;
21+
22+
/**
23+
* @phpstan-import-type InputMessageType from InputMessage
24+
* @phpstan-import-type ComputerToolCallOutputType from ComputerToolCallOutput
25+
* @phpstan-import-type FunctionToolCallOutputType from FunctionToolCallOutput
26+
* @phpstan-import-type OutputComputerToolCallType from OutputComputerToolCall
27+
* @phpstan-import-type OutputFileSearchToolCallType from OutputFileSearchToolCall
28+
* @phpstan-import-type OutputFunctionToolCallType from OutputFunctionToolCall
29+
* @phpstan-import-type OutputMessageType from OutputMessage
30+
* @phpstan-import-type OutputReasoningType from OutputReasoning
31+
* @phpstan-import-type OutputWebSearchToolCallType from OutputWebSearchToolCall
32+
* @phpstan-import-type OutputMcpListToolsType from OutputMcpListTools
33+
* @phpstan-import-type OutputMcpApprovalRequestType from OutputMcpApprovalRequest
34+
* @phpstan-import-type OutputMcpCallType from OutputMcpCall
35+
* @phpstan-import-type OutputImageGenerationToolCallType from OutputImageGenerationToolCall
36+
* @phpstan-import-type OutputCodeInterpreterToolCallType from OutputCodeInterpreterToolCall
37+
*
38+
* @phpstan-type ResponseItemObjectTypes array<int, InputMessageType|ComputerToolCallOutputType|FunctionToolCallOutputType|OutputComputerToolCallType|OutputFileSearchToolCallType|OutputFunctionToolCallType|OutputMessageType|OutputReasoningType|OutputWebSearchToolCallType|OutputMcpListToolsType|OutputMcpApprovalRequestType|OutputMcpCallType|OutputImageGenerationToolCallType|OutputCodeInterpreterToolCallType>
39+
* @phpstan-type ResponseItemObjectReturnType array<int, InputMessage|ComputerToolCallOutput|FunctionToolCallOutput|OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall>
40+
*/
41+
final class ItemObjects
42+
{
43+
/**
44+
* @param ResponseItemObjectTypes $outputItems
45+
* @return ResponseItemObjectReturnType
46+
*/
47+
public static function parse(array $outputItems): array
48+
{
49+
return array_map(
50+
fn (array $item): InputMessage|ComputerToolCallOutput|FunctionToolCallOutput|OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall => match ($item['type']) {
51+
'message' => $item['role'] === 'assistant' ? OutputMessage::from($item) : InputMessage::from($item),
52+
'file_search_call' => OutputFileSearchToolCall::from($item),
53+
'function_call' => OutputFunctionToolCall::from($item),
54+
'function_call_output' => FunctionToolCallOutput::from($item),
55+
'web_search_call' => OutputWebSearchToolCall::from($item),
56+
'computer_call' => OutputComputerToolCall::from($item),
57+
'computer_call_output' => ComputerToolCallOutput::from($item),
58+
'reasoning' => OutputReasoning::from($item),
59+
'mcp_list_tools' => OutputMcpListTools::from($item),
60+
'mcp_approval_request' => OutputMcpApprovalRequest::from($item),
61+
'mcp_call' => OutputMcpCall::from($item),
62+
'image_generation_call' => OutputImageGenerationToolCall::from($item),
63+
'code_interpreter_call' => OutputCodeInterpreterToolCall::from($item),
64+
},
65+
$outputItems,
66+
);
67+
}
68+
}
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\Actions\Responses;
6+
7+
use OpenAI\Responses\Responses\Output\OutputCodeInterpreterToolCall;
8+
use OpenAI\Responses\Responses\Output\OutputComputerToolCall;
9+
use OpenAI\Responses\Responses\Output\OutputFileSearchToolCall;
10+
use OpenAI\Responses\Responses\Output\OutputFunctionToolCall;
11+
use OpenAI\Responses\Responses\Output\OutputImageGenerationToolCall;
12+
use OpenAI\Responses\Responses\Output\OutputMcpApprovalRequest;
13+
use OpenAI\Responses\Responses\Output\OutputMcpCall;
14+
use OpenAI\Responses\Responses\Output\OutputMcpListTools;
15+
use OpenAI\Responses\Responses\Output\OutputMessage;
16+
use OpenAI\Responses\Responses\Output\OutputReasoning;
17+
use OpenAI\Responses\Responses\Output\OutputWebSearchToolCall;
18+
19+
/**
20+
* @phpstan-import-type OutputComputerToolCallType from OutputComputerToolCall
21+
* @phpstan-import-type OutputFileSearchToolCallType from OutputFileSearchToolCall
22+
* @phpstan-import-type OutputFunctionToolCallType from OutputFunctionToolCall
23+
* @phpstan-import-type OutputMessageType from OutputMessage
24+
* @phpstan-import-type OutputReasoningType from OutputReasoning
25+
* @phpstan-import-type OutputWebSearchToolCallType from OutputWebSearchToolCall
26+
* @phpstan-import-type OutputMcpListToolsType from OutputMcpListTools
27+
* @phpstan-import-type OutputMcpApprovalRequestType from OutputMcpApprovalRequest
28+
* @phpstan-import-type OutputMcpCallType from OutputMcpCall
29+
* @phpstan-import-type OutputImageGenerationToolCallType from OutputImageGenerationToolCall
30+
* @phpstan-import-type OutputCodeInterpreterToolCallType from OutputCodeInterpreterToolCall
31+
*
32+
* @phpstan-type ResponseOutputObjectTypes array<int, OutputComputerToolCallType|OutputFileSearchToolCallType|OutputFunctionToolCallType|OutputMessageType|OutputReasoningType|OutputWebSearchToolCallType|OutputMcpListToolsType|OutputMcpApprovalRequestType|OutputMcpCallType|OutputImageGenerationToolCallType|OutputCodeInterpreterToolCallType>
33+
* @phpstan-type ResponseOutputObjectReturnType array<int, OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall>
34+
*/
35+
final class OutputObjects
36+
{
37+
/**
38+
* @param ResponseOutputObjectTypes $outputItems
39+
* @return ResponseOutputObjectReturnType
40+
*/
41+
public static function parse(array $outputItems): array
42+
{
43+
return array_map(
44+
fn (array $item): OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall => match ($item['type']) {
45+
'message' => OutputMessage::from($item),
46+
'file_search_call' => OutputFileSearchToolCall::from($item),
47+
'function_call' => OutputFunctionToolCall::from($item),
48+
'web_search_call' => OutputWebSearchToolCall::from($item),
49+
'computer_call' => OutputComputerToolCall::from($item),
50+
'reasoning' => OutputReasoning::from($item),
51+
'mcp_list_tools' => OutputMcpListTools::from($item),
52+
'mcp_approval_request' => OutputMcpApprovalRequest::from($item),
53+
'mcp_call' => OutputMcpCall::from($item),
54+
'image_generation_call' => OutputImageGenerationToolCall::from($item),
55+
'code_interpreter_call' => OutputCodeInterpreterToolCall::from($item),
56+
},
57+
$outputItems,
58+
);
59+
}
60+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenAI\Actions\Responses;
6+
7+
use OpenAI\Responses\Responses\Output\OutputMessage;
8+
use OpenAI\Responses\Responses\Output\OutputMessageContentOutputText;
9+
10+
/**
11+
* An SDK-only property (output_text) that concatenates all text content from output messages.
12+
*
13+
* @phpstan-import-type ResponseOutputObjectReturnType from OutputObjects
14+
*/
15+
final class OutputText
16+
{
17+
/**
18+
* @param ResponseOutputObjectReturnType $outputItems
19+
*/
20+
public static function parse(array $outputItems): ?string
21+
{
22+
$texts = [];
23+
foreach ($outputItems as $item) {
24+
if ($item instanceof OutputMessage) {
25+
foreach ($item->content as $content) {
26+
if ($content instanceof OutputMessageContentOutputText) {
27+
$texts[] = $content->text;
28+
}
29+
}
30+
}
31+
}
32+
33+
return empty($texts) ? null : implode(' ', $texts);
34+
}
35+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenAI\Actions\Responses;
6+
7+
use OpenAI\Responses\Responses\ToolChoice\FunctionToolChoice;
8+
use OpenAI\Responses\Responses\ToolChoice\HostedToolChoice;
9+
10+
/**
11+
* @phpstan-import-type FunctionToolChoiceType from FunctionToolChoice
12+
* @phpstan-import-type HostedToolChoiceType from HostedToolChoice
13+
*
14+
* @phpstan-type ResponseToolChoiceTypes 'none'|'auto'|'required'|FunctionToolChoiceType|HostedToolChoiceType
15+
* @phpstan-type ResponseToolChoiceReturnType 'none'|'auto'|'required'|FunctionToolChoice|HostedToolChoice
16+
*/
17+
final class ToolChoiceObjects
18+
{
19+
/**
20+
* @param ResponseToolChoiceTypes $toolChoice
21+
* @return ResponseToolChoiceReturnType
22+
*/
23+
public static function parse(array|string $toolChoice): string|FunctionToolChoice|HostedToolChoice
24+
{
25+
return is_array($toolChoice)
26+
? match ($toolChoice['type']) {
27+
'file_search', 'web_search', 'web_search_preview', 'computer_use_preview' => HostedToolChoice::from($toolChoice),
28+
'function' => FunctionToolChoice::from($toolChoice),
29+
}
30+
: $toolChoice;
31+
}
32+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenAI\Actions\Responses;
6+
7+
use OpenAI\Responses\Responses\Tool\CodeInterpreterTool;
8+
use OpenAI\Responses\Responses\Tool\ComputerUseTool;
9+
use OpenAI\Responses\Responses\Tool\FileSearchTool;
10+
use OpenAI\Responses\Responses\Tool\FunctionTool;
11+
use OpenAI\Responses\Responses\Tool\ImageGenerationTool;
12+
use OpenAI\Responses\Responses\Tool\RemoteMcpTool;
13+
use OpenAI\Responses\Responses\Tool\WebSearchTool;
14+
15+
/**
16+
* @phpstan-import-type ComputerUseToolType from ComputerUseTool
17+
* @phpstan-import-type FileSearchToolType from FileSearchTool
18+
* @phpstan-import-type ImageGenerationToolType from ImageGenerationTool
19+
* @phpstan-import-type RemoteMcpToolType from RemoteMcpTool
20+
* @phpstan-import-type FunctionToolType from FunctionTool
21+
* @phpstan-import-type WebSearchToolType from WebSearchTool
22+
* @phpstan-import-type CodeInterpreterToolType from CodeInterpreterTool
23+
*
24+
* @phpstan-type ResponseToolObjectTypes array<int, ComputerUseToolType|FileSearchToolType|FunctionToolType|WebSearchToolType|ImageGenerationToolType|RemoteMcpToolType|CodeInterpreterToolType>
25+
* @phpstan-type ResponseToolObjectReturnType array<int, ComputerUseTool|FileSearchTool|FunctionTool|WebSearchTool|ImageGenerationTool|RemoteMcpTool|CodeInterpreterTool>
26+
*/
27+
final class ToolObjects
28+
{
29+
/**
30+
* @param ResponseToolObjectTypes $toolItems
31+
* @return ResponseToolObjectReturnType
32+
*/
33+
public static function parse(array $toolItems): array
34+
{
35+
return array_map(
36+
fn (array $tool): ComputerUseTool|FileSearchTool|FunctionTool|WebSearchTool|ImageGenerationTool|RemoteMcpTool|CodeInterpreterTool => match ($tool['type']) {
37+
'file_search' => FileSearchTool::from($tool),
38+
'web_search', 'web_search_preview', 'web_search_preview_2025_03_11' => WebSearchTool::from($tool),
39+
'function' => FunctionTool::from($tool),
40+
'computer_use_preview' => ComputerUseTool::from($tool),
41+
'image_generation' => ImageGenerationTool::from($tool),
42+
'mcp' => RemoteMcpTool::from($tool),
43+
'code_interpreter' => CodeInterpreterTool::from($tool),
44+
},
45+
$toolItems,
46+
);
47+
}
48+
}

0 commit comments

Comments
 (0)