Skip to content

Commit 728a944

Browse files
committed
Add thread messages delete endpoint
1 parent 18c9eea commit 728a944

File tree

9 files changed

+197
-0
lines changed

9 files changed

+197
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,23 @@ $response->metadata; // ['name' => 'My new message name']
12801280
$response->toArray(); // ['id' => 'msg_SKYwvF3zcigxthfn6F4hnpdU', ...]
12811281
```
12821282

1283+
#### `delete`
1284+
1285+
Deletes a message.
1286+
1287+
```php
1288+
$response = $client->threads()->messages()->delete(
1289+
threadId: 'thread_tKFLqzRN9n7MnyKKvc1Q7868',
1290+
messageId: 'msg_SKYwvF3zcigxthfn6F4hnpdU'
1291+
);
1292+
1293+
$response->id; // 'msg_SKYwvF3zcigxthfn6F4hnpdU'
1294+
$response->object; // 'thread.message.deleted'
1295+
$response->deleted; // true
1296+
1297+
$response->toArray(); // ['id' => 'msg_SKYwvF3zcigxthfn6F4hnpdU', ...]
1298+
```
1299+
12831300
#### `list`
12841301

12851302
Returns a list of messages for a given thread.

src/Contracts/Resources/ThreadsMessagesContract.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace OpenAI\Contracts\Resources;
44

5+
use OpenAI\Responses\Threads\Messages\ThreadMessageDeleteResponse;
56
use OpenAI\Responses\Threads\Messages\ThreadMessageListResponse;
67
use OpenAI\Responses\Threads\Messages\ThreadMessageResponse;
78

@@ -32,6 +33,13 @@ public function retrieve(string $threadId, string $messageId): ThreadMessageResp
3233
*/
3334
public function modify(string $threadId, string $messageId, array $parameters): ThreadMessageResponse;
3435

36+
/**
37+
* Deletes a message.
38+
*
39+
* @see https://platform.openai.com/docs/api-reference/messages/deleteMessage
40+
*/
41+
public function delete(string $threadId, string $messageId): ThreadMessageDeleteResponse;
42+
3543
/**
3644
* Returns a list of messages for a given thread.
3745
*

src/Resources/ThreadsMessages.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace OpenAI\Resources;
66

77
use OpenAI\Contracts\Resources\ThreadsMessagesContract;
8+
use OpenAI\Responses\Threads\Messages\ThreadMessageDeleteResponse;
89
use OpenAI\Responses\Threads\Messages\ThreadMessageListResponse;
910
use OpenAI\Responses\Threads\Messages\ThreadMessageResponse;
1011
use OpenAI\ValueObjects\Transporter\Payload;
@@ -63,6 +64,21 @@ public function modify(string $threadId, string $messageId, array $parameters):
6364
return ThreadMessageResponse::from($response->data(), $response->meta());
6465
}
6566

67+
/**
68+
* Deletes a message.
69+
*
70+
* @see https://platform.openai.com/docs/api-reference/messages/deleteMessage
71+
*/
72+
public function delete(string $threadId, string $messageId): ThreadMessageDeleteResponse
73+
{
74+
$payload = Payload::delete("threads/$threadId/messages", $messageId);
75+
76+
/** @var Response<array{id: string, object: string, deleted: bool}> $response */
77+
$response = $this->transporter->requestObject($payload);
78+
79+
return ThreadMessageDeleteResponse::from($response->data(), $response->meta());
80+
}
81+
6682
/**
6783
* Returns a list of messages for a given thread.
6884
*
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenAI\Responses\Threads\Messages;
6+
7+
use OpenAI\Contracts\ResponseContract;
8+
use OpenAI\Contracts\ResponseHasMetaInformationContract;
9+
use OpenAI\Responses\Concerns\ArrayAccessible;
10+
use OpenAI\Responses\Concerns\HasMetaInformation;
11+
use OpenAI\Responses\Meta\MetaInformation;
12+
use OpenAI\Testing\Responses\Concerns\Fakeable;
13+
14+
/**
15+
* @implements ResponseContract<array{id: string, object: string, deleted: bool}>
16+
*/
17+
final class ThreadMessageDeleteResponse implements ResponseContract, ResponseHasMetaInformationContract
18+
{
19+
/**
20+
* @use ArrayAccessible<array{id: string, object: string, deleted: bool}>
21+
*/
22+
use ArrayAccessible;
23+
24+
use Fakeable;
25+
use HasMetaInformation;
26+
27+
private function __construct(
28+
public readonly string $id,
29+
public readonly string $object,
30+
public readonly bool $deleted,
31+
private readonly MetaInformation $meta,
32+
) {
33+
}
34+
35+
/**
36+
* Acts as static factory, and returns a new Response instance.
37+
*
38+
* @param array{id: string, object: string, deleted: bool} $attributes
39+
*/
40+
public static function from(array $attributes, MetaInformation $meta): self
41+
{
42+
return new self(
43+
$attributes['id'],
44+
$attributes['object'],
45+
$attributes['deleted'],
46+
$meta,
47+
);
48+
}
49+
50+
/**
51+
* {@inheritDoc}
52+
*/
53+
public function toArray(): array
54+
{
55+
return [
56+
'id' => $this->id,
57+
'object' => $this->object,
58+
'deleted' => $this->deleted,
59+
];
60+
}
61+
}

src/Testing/Resources/ThreadsMessagesTestResource.php

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

55
use OpenAI\Contracts\Resources\ThreadsMessagesContract;
66
use OpenAI\Resources\ThreadsMessages;
7+
use OpenAI\Responses\Threads\Messages\ThreadMessageDeleteResponse;
78
use OpenAI\Responses\Threads\Messages\ThreadMessageListResponse;
89
use OpenAI\Responses\Threads\Messages\ThreadMessageResponse;
910
use OpenAI\Testing\Resources\Concerns\Testable;
@@ -32,6 +33,11 @@ public function modify(string $threadId, string $messageId, array $parameters):
3233
return $this->record(__FUNCTION__, func_get_args());
3334
}
3435

36+
public function delete(string $threadId, string $messageId): ThreadMessageDeleteResponse
37+
{
38+
return $this->record(__FUNCTION__, func_get_args());
39+
}
40+
3541
public function list(string $threadId, array $parameters = []): ThreadMessageListResponse
3642
{
3743
return $this->record(__FUNCTION__, func_get_args());

tests/Fixtures/ThreadMessage.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,15 @@ function threadMessageListResource(): array
8181
'has_more' => false,
8282
];
8383
}
84+
85+
/**
86+
* @return array<string, mixed>
87+
*/
88+
function threadMessageDeleteResource(): array
89+
{
90+
return [
91+
'id' => 'msg_KNsDDwE41BUAHhcPNpDkdHWZ',
92+
'object' => 'thread.message.deleted',
93+
'deleted' => true,
94+
];
95+
}

tests/Resources/ThreadsMessages.php

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

33
use OpenAI\Responses\Meta\MetaInformation;
4+
use OpenAI\Responses\Threads\Messages\ThreadMessageDeleteResponse;
45
use OpenAI\Responses\Threads\Messages\ThreadMessageListResponse;
56
use OpenAI\Responses\Threads\Messages\ThreadMessageResponse;
67
use OpenAI\Responses\Threads\Messages\ThreadMessageResponseAttachment;
@@ -132,3 +133,18 @@
132133
expect($result->meta())
133134
->toBeInstanceOf(MetaInformation::class);
134135
});
136+
137+
test('delete', function () {
138+
$client = mockClient('DELETE', 'threads/thread_agvtHUGezjTCt4SKgQg0NJ2Y/messages/msg_KNsDDwE41BUAHhcPNpDkdHWZ', [], Response::from(threadMessageDeleteResource(), metaHeaders()));
139+
140+
$result = $client->threads()->messages()->delete('thread_agvtHUGezjTCt4SKgQg0NJ2Y', 'msg_KNsDDwE41BUAHhcPNpDkdHWZ');
141+
142+
expect($result)
143+
->toBeInstanceOf(ThreadMessageDeleteResponse::class)
144+
->id->toBe('msg_KNsDDwE41BUAHhcPNpDkdHWZ')
145+
->object->toBe('thread.message.deleted')
146+
->deleted->toBe(true);
147+
148+
expect($result->meta())
149+
->toBeInstanceOf(MetaInformation::class);
150+
});
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
use OpenAI\Responses\Meta\MetaInformation;
4+
use OpenAI\Responses\Threads\Messages\ThreadMessageDeleteResponse;
5+
6+
test('from', function () {
7+
$result = ThreadMessageDeleteResponse::from(threadMessageDeleteResource(), meta());
8+
9+
expect($result)
10+
->id->toBe('msg_KNsDDwE41BUAHhcPNpDkdHWZ')
11+
->object->toBe('thread.message.deleted')
12+
->deleted->toBe(true)
13+
->meta()->toBeInstanceOf(MetaInformation::class);
14+
});
15+
16+
test('as array accessible', function () {
17+
$result = ThreadMessageDeleteResponse::from(threadMessageDeleteResource(), meta());
18+
19+
expect($result['id'])
20+
->toBe('msg_KNsDDwE41BUAHhcPNpDkdHWZ');
21+
});
22+
23+
test('to array', function () {
24+
$result = ThreadMessageDeleteResponse::from(threadMessageDeleteResource(), meta());
25+
26+
expect($result->toArray())
27+
->toBe(threadMessageDeleteResource());
28+
});
29+
30+
test('fake', function () {
31+
$response = ThreadMessageDeleteResponse::fake();
32+
33+
expect($response)
34+
->id->toBe('msg_KNsDDwE41BUAHhcPNpDkdHWZ')
35+
->deleted->toBe(true);
36+
});
37+
38+
test('fake with override', function () {
39+
$response = ThreadMessageDeleteResponse::fake([
40+
'id' => 'msg_1234',
41+
'deleted' => false,
42+
]);
43+
44+
expect($response)
45+
->id->toBe('msg_1234')
46+
->deleted->toBe(false);
47+
});

tests/Testing/Resources/ThreadsMessagesTestResource.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@
6565
});
6666
});
6767

68+
it('records a thread message delete request', function () {
69+
$fake = new ClientFake([
70+
\OpenAI\Responses\Threads\Messages\ThreadMessageDeleteResponse::fake(),
71+
]);
72+
73+
$fake->threads()->messages()->delete('thread_tKFLqzRN9n7MnyKKvc1Q7868', 'msg_KNsDDwE41BUAHhcPNpDkdHWZ');
74+
75+
$fake->assertSent(ThreadsMessages::class, function ($method, $threadId, $messageId) {
76+
return $method === 'delete' &&
77+
$threadId === 'thread_tKFLqzRN9n7MnyKKvc1Q7868' &&
78+
$messageId === 'msg_KNsDDwE41BUAHhcPNpDkdHWZ';
79+
});
80+
});
81+
6882
it('records a thread message list request', function () {
6983
$fake = new ClientFake([
7084
ThreadMessageListResponse::fake(),

0 commit comments

Comments
 (0)