Skip to content

Commit 853d7ab

Browse files
committed
Annotations treated as arrays on message object
1 parent 4502386 commit 853d7ab

File tree

3 files changed

+27
-32
lines changed

3 files changed

+27
-32
lines changed

src/Responses/Chat/CreateResponseChoiceAnnotations.php

+12-19
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,30 @@
44

55
final class CreateResponseChoiceAnnotations
66
{
7-
/**
8-
* @param array<int, CreateResponseChoiceAnnotationsUrlCitations> $urlCitations
9-
*/
107
public function __construct(
11-
public readonly array $urlCitations
8+
public readonly string $type,
9+
public readonly CreateResponseChoiceAnnotationsUrlCitations $urlCitations
1210
) {}
1311

1412
/**
15-
* @param array<int, array{type: 'url_citation', url_citation: array{end_index: int, start_index: int, title: string, url: string}}> $attributes
13+
* @param array{type: 'url_citation', url_citation: array{end_index: int, start_index: int, title: string, url: string}} $attributes
1614
*/
1715
public static function from(array $attributes): self
1816
{
19-
$urlCitations = array_map(
20-
fn (array $citation) => CreateResponseChoiceAnnotationsUrlCitations::from($citation['url_citation']),
21-
$attributes
22-
);
23-
24-
return new self($urlCitations);
17+
return new self(
18+
$attributes['type'],
19+
CreateResponseChoiceAnnotationsUrlCitations::from($attributes['url_citation'])
20+
);
2521
}
2622

2723
/**
28-
* @return array<int, array{type: 'url_citation', url_citation: array{end_index: int, start_index: int, title: string, url: string}}>
24+
* @return array{type: 'url_citation', url_citation: array{end_index: int, start_index: int, title: string, url: string}}
2925
*/
3026
public function toArray(): array
3127
{
32-
return array_map(
33-
fn (CreateResponseChoiceAnnotationsUrlCitations $citation) => [
34-
'type' => 'url_citation',
35-
'url_citation' => $citation->toArray(),
36-
],
37-
$this->urlCitations
38-
);
28+
return [
29+
'type' => $this->type,
30+
'url_citation' => $this->urlCitations->toArray()
31+
];
3932
}
4033
}

src/Responses/Chat/CreateResponseMessage.php

+10-6
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,35 @@ final class CreateResponseMessage
1212
private function __construct(
1313
public readonly string $role,
1414
public readonly ?string $content,
15-
public readonly ?CreateResponseChoiceAnnotations $annotations,
15+
public readonly array $annotations,
1616
public readonly array $toolCalls,
1717
public readonly ?CreateResponseFunctionCall $functionCall,
1818
) {}
1919

2020
/**
21-
* @param array{role: string, content: ?string, annotations?: ?array<int, array{type: 'url_citation', url_citation: array{start_index: int, end_index: int, title: string, url: string}}>, function_call: ?array{name: string, arguments: string}, tool_calls: ?array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>} $attributes
21+
* @param array{role: string, content: ?string, annotations?: ?array{type: 'url_citation', url_citation: array{start_index: int, end_index: int, title: string, url: string}}, function_call: ?array{name: string, arguments: string}, tool_calls: ?array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>} $attributes
2222
*/
2323
public static function from(array $attributes): self
2424
{
2525
$toolCalls = array_map(fn (array $result): CreateResponseToolCall => CreateResponseToolCall::from(
2626
$result
2727
), $attributes['tool_calls'] ?? []);
2828

29+
$annotations = array_map(fn (array $result): CreateResponseChoiceAnnotations => CreateResponseChoiceAnnotations::from(
30+
$result,
31+
), $attributes['annotations'] ?? []);
32+
2933
return new self(
3034
$attributes['role'],
3135
$attributes['content'] ?? null,
32-
isset($attributes['annotations']) ? CreateResponseChoiceAnnotations::from($attributes['annotations']) : null,
36+
$annotations,
3337
$toolCalls,
3438
isset($attributes['function_call']) ? CreateResponseFunctionCall::from($attributes['function_call']) : null,
3539
);
3640
}
3741

3842
/**
39-
* @return array{role: string, content: string|null, annotations?: ?array<int, array{type: 'url_citation', url_citation: array{start_index: int, end_index: int, title: string, url: string}}>, function_call?: array{name: string, arguments: string}, tool_calls?: array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>}
43+
* @return array{role: string, content: string|null, annotations?: ?array{type: 'url_citation', url_citation: array{start_index: int, end_index: int, title: string, url: string}}, function_call?: array{name: string, arguments: string}, tool_calls?: array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>}
4044
*/
4145
public function toArray(): array
4246
{
@@ -45,8 +49,8 @@ public function toArray(): array
4549
'content' => $this->content,
4650
];
4751

48-
if ($this->annotations) {
49-
$data['annotations'] = $this->annotations->toArray();
52+
if ($this->annotations !== []) {
53+
$data['annotations'] = array_map(fn (CreateResponseChoiceAnnotations $annotations): array => $annotations->toArray(), $this->annotations);
5054
}
5155

5256
if ($this->functionCall instanceof CreateResponseFunctionCall) {

tests/Responses/Chat/CreateResponseChoiceAnnotations.php

+5-7
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,16 @@
44
use OpenAI\Responses\Chat\CreateResponseChoiceAnnotationsUrlCitations;
55

66
it('from url_citation annotation', function () {
7-
$result = CreateResponseChoiceAnnotations::from(chatCompletionWithAnnotations()['choices'][0]['message']['annotations']);
7+
$result = CreateResponseChoiceAnnotations::from(chatCompletionWithAnnotations()['choices'][0]['message']['annotations'][0]);
88

99
expect($result)
10-
->toBeInstanceOf(CreateResponseChoiceAnnotations::class)
11-
->urlCitations->toBeArray()
12-
->urlCitations->toHaveLength(1)
13-
->urlCitations->each->toBeInstanceOf(CreateResponseChoiceAnnotationsUrlCitations::class);
10+
->type->toBe('url_citation')
11+
->urlCitations->toBeInstanceOf(CreateResponseChoiceAnnotationsUrlCitations::class);
1412
});
1513

1614
test('to array', function () {
17-
$result = CreateResponseChoiceAnnotations::from(chatCompletionWithAnnotations()['choices'][0]['message']['annotations']);
15+
$result = CreateResponseChoiceAnnotations::from(chatCompletionWithAnnotations()['choices'][0]['message']['annotations'][0]);
1816

1917
expect($result->toArray())
20-
->toBe(chatCompletionWithAnnotations()['choices'][0]['message']['annotations']);
18+
->toBe(chatCompletionWithAnnotations()['choices'][0]['message']['annotations'][0]);
2119
});

0 commit comments

Comments
 (0)