Skip to content

Commit de2a822

Browse files
CopilotiBotPeaches
andauthored
fix: support actions array in output computer tool call parsing
Agent-Logs-Url: https://github.com/openai-php/client/sessions/18ddc838-e520-4048-879d-4a1218823f7a Co-authored-by: iBotPeaches <611784+iBotPeaches@users.noreply.github.com>
1 parent 329709c commit de2a822

2 files changed

Lines changed: 41 additions & 12 deletions

File tree

src/Responses/Responses/Output/OutputComputerToolCall.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
* @phpstan-import-type WaitType from Wait
3131
* @phpstan-import-type PendingSafetyCheckType from OutputComputerPendingSafetyCheck
3232
*
33-
* @phpstan-type OutputComputerToolCallType array{action: ClickType|DoubleClickType|DragType|KeyPressType|MoveType|ScreenshotType|ScrollType|TypeType|WaitType, call_id: string, id: string, pending_safety_checks: array<int, PendingSafetyCheckType>, status: 'in_progress'|'completed'|'incomplete', type: 'computer_call'}
33+
* @phpstan-type ActionType ClickType|DoubleClickType|DragType|KeyPressType|MoveType|ScreenshotType|ScrollType|TypeType|WaitType
34+
* @phpstan-type OutputComputerToolCallType array{action?: ActionType, actions?: array<int, ActionType>, call_id: string, id: string, pending_safety_checks?: array<int, PendingSafetyCheckType>, status: 'in_progress'|'completed'|'incomplete', type: 'computer_call'}
3435
*
3536
* @implements ResponseContract<OutputComputerToolCallType>
3637
*/
@@ -62,21 +63,23 @@ private function __construct(
6263
*/
6364
public static function from(array $attributes): self
6465
{
65-
$action = match ($attributes['action']['type']) {
66-
'click' => Click::from($attributes['action']),
67-
'double_click' => DoubleClick::from($attributes['action']),
68-
'drag' => Drag::from($attributes['action']),
69-
'keypress' => KeyPress::from($attributes['action']),
70-
'move' => Move::from($attributes['action']),
71-
'screenshot' => Screenshot::from($attributes['action']),
72-
'scroll' => Scroll::from($attributes['action']),
73-
'type' => Type::from($attributes['action']),
74-
'wait' => Wait::from($attributes['action']),
66+
$actionAttributes = $attributes['action'] ?? ($attributes['actions'][0] ?? []);
67+
68+
$action = match ($actionAttributes['type']) {
69+
'click' => Click::from($actionAttributes),
70+
'double_click' => DoubleClick::from($actionAttributes),
71+
'drag' => Drag::from($actionAttributes),
72+
'keypress' => KeyPress::from($actionAttributes),
73+
'move' => Move::from($actionAttributes),
74+
'screenshot' => Screenshot::from($actionAttributes),
75+
'scroll' => Scroll::from($actionAttributes),
76+
'type' => Type::from($actionAttributes),
77+
'wait' => Wait::from($actionAttributes),
7578
};
7679

7780
$pendingSafetyChecks = array_map(
7881
fn (array $safetyCheck): OutputComputerPendingSafetyCheck => OutputComputerPendingSafetyCheck::from($safetyCheck),
79-
$attributes['pending_safety_checks']
82+
$attributes['pending_safety_checks'] ?? []
8083
);
8184

8285
return new self(

tests/Responses/Responses/Output/OutputComputerToolCall.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use OpenAI\Responses\Responses\Output\ComputerAction\OutputComputerActionClick;
4+
use OpenAI\Responses\Responses\Output\ComputerAction\OutputComputerActionScreenshot;
45
use OpenAI\Responses\Responses\Output\OutputComputerToolCall;
56

67
test('from', function () {
@@ -28,3 +29,28 @@
2829
->toBeArray()
2930
->toBe(outputComputerToolCall());
3031
});
32+
33+
test('from with actions and without pending safety checks', function () {
34+
$payload = outputComputerToolCall();
35+
unset($payload['action'], $payload['pending_safety_checks']);
36+
$payload['actions'] = [
37+
['type' => 'screenshot'],
38+
];
39+
40+
$response = OutputComputerToolCall::from($payload);
41+
42+
expect($response)
43+
->toBeInstanceOf(OutputComputerToolCall::class)
44+
->action->toBeInstanceOf(OutputComputerActionScreenshot::class)
45+
->pendingSafetyChecks->toBeArray()->toHaveCount(0)
46+
->status->toBe('completed')
47+
->type->toBe('computer_call');
48+
49+
expect($response->toArray())
50+
->toBeArray()
51+
->toMatchArray([
52+
'action' => ['type' => 'screenshot'],
53+
'pending_safety_checks' => [],
54+
])
55+
->not->toHaveKey('actions');
56+
});

0 commit comments

Comments
 (0)