Skip to content

Commit 41ec95c

Browse files
committed
chore(OpenAI): support all data items + todos for input/output
1 parent ce766b1 commit 41ec95c

File tree

6 files changed

+103
-79
lines changed

6 files changed

+103
-79
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenAI\Actions\Conversations;
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\OutputReasoning;
19+
use OpenAI\Responses\Responses\Output\OutputWebSearchToolCall;
20+
21+
/**
22+
* @phpstan-import-type InputMessageType from InputMessage
23+
* @phpstan-import-type ComputerToolCallOutputType from ComputerToolCallOutput
24+
* @phpstan-import-type FunctionToolCallOutputType from FunctionToolCallOutput
25+
* @phpstan-import-type OutputComputerToolCallType from OutputComputerToolCall
26+
* @phpstan-import-type OutputFileSearchToolCallType from OutputFileSearchToolCall
27+
* @phpstan-import-type OutputFunctionToolCallType from OutputFunctionToolCall
28+
* @phpstan-import-type OutputReasoningType from OutputReasoning
29+
* @phpstan-import-type OutputWebSearchToolCallType from OutputWebSearchToolCall
30+
* @phpstan-import-type OutputMcpListToolsType from OutputMcpListTools
31+
* @phpstan-import-type OutputMcpApprovalRequestType from OutputMcpApprovalRequest
32+
* @phpstan-import-type OutputMcpCallType from OutputMcpCall
33+
* @phpstan-import-type OutputImageGenerationToolCallType from OutputImageGenerationToolCall
34+
* @phpstan-import-type OutputCodeInterpreterToolCallType from OutputCodeInterpreterToolCall
35+
*
36+
* @phpstan-type ConversationItemObjectTypes array<int, InputMessageType|ComputerToolCallOutputType|FunctionToolCallOutputType|OutputComputerToolCallType|OutputFileSearchToolCallType|OutputFunctionToolCallType|OutputReasoningType|OutputWebSearchToolCallType|OutputMcpListToolsType|OutputMcpApprovalRequestType|OutputMcpCallType|OutputImageGenerationToolCallType|OutputCodeInterpreterToolCallType>
37+
* @phpstan-type ConversationItemObjectReturnType array<int, InputMessage|ComputerToolCallOutput|FunctionToolCallOutput|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall>
38+
*/
39+
final class ItemObjects
40+
{
41+
/**
42+
* @param ConversationItemObjectTypes $outputItems
43+
* @return ConversationItemObjectReturnType
44+
*/
45+
public static function parse(array $outputItems): array
46+
{
47+
return array_map(
48+
fn (array $item): InputMessage|ComputerToolCallOutput|FunctionToolCallOutput|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall => match ($item['type']) {
49+
'message' => InputMessage::from($item),
50+
'file_search_call' => OutputFileSearchToolCall::from($item),
51+
'function_call' => OutputFunctionToolCall::from($item),
52+
'function_call_output' => FunctionToolCallOutput::from($item),
53+
'web_search_call' => OutputWebSearchToolCall::from($item),
54+
'computer_call' => OutputComputerToolCall::from($item),
55+
'computer_call_output' => ComputerToolCallOutput::from($item),
56+
'reasoning' => OutputReasoning::from($item),
57+
'mcp_list_tools' => OutputMcpListTools::from($item),
58+
'mcp_approval_request' => OutputMcpApprovalRequest::from($item),
59+
'mcp_call' => OutputMcpCall::from($item),
60+
'image_generation_call' => OutputImageGenerationToolCall::from($item),
61+
'code_interpreter_call' => OutputCodeInterpreterToolCall::from($item),
62+
// todo
63+
// local_shell_call
64+
// local_shell_call_output
65+
// mcp_approval_response
66+
// custom_tool_call
67+
// custom_tool_call_output
68+
},
69+
$outputItems,
70+
);
71+
}
72+
}

src/Actions/Responses/ItemObjects.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ public static function parse(array $outputItems): array
6161
'mcp_call' => OutputMcpCall::from($item),
6262
'image_generation_call' => OutputImageGenerationToolCall::from($item),
6363
'code_interpreter_call' => OutputCodeInterpreterToolCall::from($item),
64+
// todo
65+
// local_shell_call
66+
// local_shell_call_output
67+
// mcp_approval_response
6468
},
6569
$outputItems,
6670
);

src/Actions/Responses/OutputObjects.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ public static function parse(array $outputItems): array
5353
'mcp_call' => OutputMcpCall::from($item),
5454
'image_generation_call' => OutputImageGenerationToolCall::from($item),
5555
'code_interpreter_call' => OutputCodeInterpreterToolCall::from($item),
56+
// todo
57+
// local_shell_call
58+
// custom_tool_call
5659
},
5760
$outputItems,
5861
);

src/Responses/Conversations/ConversationItem.php

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/Responses/Conversations/ConversationItemList.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,31 @@
44

55
namespace OpenAI\Responses\Conversations;
66

7+
use OpenAI\Actions\Conversations\ItemObjects;
78
use OpenAI\Contracts\ResponseContract;
89
use OpenAI\Contracts\ResponseHasMetaInformationContract;
910
use OpenAI\Responses\Concerns\ArrayAccessible;
1011
use OpenAI\Responses\Concerns\HasMetaInformation;
1112
use OpenAI\Responses\Meta\MetaInformation;
13+
use OpenAI\Responses\Responses\Input\ComputerToolCallOutput;
14+
use OpenAI\Responses\Responses\Input\FunctionToolCallOutput;
15+
use OpenAI\Responses\Responses\Input\InputMessage;
16+
use OpenAI\Responses\Responses\Output\OutputCodeInterpreterToolCall;
17+
use OpenAI\Responses\Responses\Output\OutputComputerToolCall;
18+
use OpenAI\Responses\Responses\Output\OutputFileSearchToolCall;
19+
use OpenAI\Responses\Responses\Output\OutputFunctionToolCall;
20+
use OpenAI\Responses\Responses\Output\OutputImageGenerationToolCall;
21+
use OpenAI\Responses\Responses\Output\OutputMcpApprovalRequest;
22+
use OpenAI\Responses\Responses\Output\OutputMcpCall;
23+
use OpenAI\Responses\Responses\Output\OutputMcpListTools;
24+
use OpenAI\Responses\Responses\Output\OutputReasoning;
25+
use OpenAI\Responses\Responses\Output\OutputWebSearchToolCall;
1226
use OpenAI\Testing\Responses\Concerns\Fakeable;
1327

1428
/**
15-
* @phpstan-import-type ConversationItemType from ConversationItem
29+
* @phpstan-import-type ConversationItemObjectTypes from ItemObjects
1630
*
17-
* @phpstan-type ConversationItemListType array{object: 'list', data: array<int, ConversationItemType>, first_id: ?string, last_id: ?string, has_more: bool}
31+
* @phpstan-type ConversationItemListType array{object: 'list', data: ConversationItemObjectTypes, first_id: ?string, last_id: ?string, has_more: bool}
1832
*
1933
* @implements ResponseContract<ConversationItemListType>
2034
*/
@@ -30,7 +44,7 @@ final class ConversationItemList implements ResponseContract, ResponseHasMetaInf
3044

3145
/**
3246
* @param 'list' $object
33-
* @param array<int, ConversationItem> $data
47+
* @param array<int, InputMessage|OutputFileSearchToolCall|OutputComputerToolCall|ComputerToolCallOutput|OutputWebSearchToolCall|OutputFunctionToolCall|FunctionToolCallOutput|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall> $data
3448
*/
3549
private function __construct(
3650
public readonly string $object,
@@ -46,10 +60,7 @@ private function __construct(
4660
*/
4761
public static function from(array $attributes, MetaInformation $meta): self
4862
{
49-
$items = array_map(
50-
static fn (array $item): ConversationItem => ConversationItem::from($item),
51-
$attributes['data'],
52-
);
63+
$items = ItemObjects::parse($attributes['data']);
5364

5465
return new self(
5566
object: $attributes['object'],
@@ -65,7 +76,10 @@ public function toArray(): array
6576
{
6677
return [
6778
'object' => $this->object,
68-
'data' => array_map(static fn (ConversationItem $i): array => $i->toArray(), $this->data),
79+
'data' => array_map(
80+
fn (InputMessage|OutputFileSearchToolCall|OutputFunctionToolCall|FunctionToolCallOutput|OutputWebSearchToolCall|OutputComputerToolCall|ComputerToolCallOutput|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall $item): array => $item->toArray(),
81+
$this->data,
82+
),
6983
'first_id' => $this->firstId,
7084
'last_id' => $this->lastId,
7185
'has_more' => $this->hasMore,

src/Responses/Responses/Input/InputMessage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @phpstan-import-type ContentInputImageType from InputMessageContentInputImage
1414
* @phpstan-import-type ContentInputFileType from InputMessageContentInputFile
1515
*
16-
* @phpstan-type InputMessageType array{content: array<int, ContentInputTextType|ContentInputImageType|ContentInputFileType>, id: string, role: 'user'|'system'|'developer', status: 'in_progress'|'completed'|'incomplete', type: 'message'}
16+
* @phpstan-type InputMessageType array{content: array<int, ContentInputTextType|ContentInputImageType|ContentInputFileType>, id: string, role: 'unknown'|'user'|'assistant'|'system'|'critic'|'discriminator'|'developer'|'tool', status: 'in_progress'|'completed'|'incomplete', type: 'message'}
1717
*
1818
* @implements ResponseContract<InputMessageType>
1919
*/
@@ -28,7 +28,7 @@ final class InputMessage implements ResponseContract
2828

2929
/**
3030
* @param array<int, InputMessageContentInputText|InputMessageContentInputImage|InputMessageContentInputFile> $content
31-
* @param 'user'|'system'|'developer' $role
31+
* @param 'unknown'|'user'|'assistant'|'system'|'critic'|'discriminator'|'developer'|'tool' $role
3232
* @param 'in_progress'|'completed'|'incomplete' $status
3333
* @param 'message' $type
3434
*/

0 commit comments

Comments
 (0)