Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add thread messages delete #418

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,23 @@ $response->metadata; // ['name' => 'My new message name']
$response->toArray(); // ['id' => 'msg_SKYwvF3zcigxthfn6F4hnpdU', ...]
```

#### `delete`

Deletes a message.

```php
$response = $client->threads()->messages()->delete(
threadId: 'thread_tKFLqzRN9n7MnyKKvc1Q7868',
messageId: 'msg_SKYwvF3zcigxthfn6F4hnpdU'
);

$response->id; // 'msg_SKYwvF3zcigxthfn6F4hnpdU'
$response->object; // 'thread.message.deleted'
$response->deleted; // true

$response->toArray(); // ['id' => 'msg_SKYwvF3zcigxthfn6F4hnpdU', ...]
```

#### `list`

Returns a list of messages for a given thread.
Expand Down
8 changes: 8 additions & 0 deletions src/Contracts/Resources/ThreadsMessagesContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OpenAI\Contracts\Resources;

use OpenAI\Responses\Threads\Messages\ThreadMessageDeleteResponse;
use OpenAI\Responses\Threads\Messages\ThreadMessageListResponse;
use OpenAI\Responses\Threads\Messages\ThreadMessageResponse;

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

/**
* Deletes a message.
*
* @see https://platform.openai.com/docs/api-reference/messages/deleteMessage
*/
public function delete(string $threadId, string $messageId): ThreadMessageDeleteResponse;

/**
* Returns a list of messages for a given thread.
*
Expand Down
16 changes: 16 additions & 0 deletions src/Resources/ThreadsMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace OpenAI\Resources;

use OpenAI\Contracts\Resources\ThreadsMessagesContract;
use OpenAI\Responses\Threads\Messages\ThreadMessageDeleteResponse;
use OpenAI\Responses\Threads\Messages\ThreadMessageListResponse;
use OpenAI\Responses\Threads\Messages\ThreadMessageResponse;
use OpenAI\ValueObjects\Transporter\Payload;
Expand Down Expand Up @@ -63,6 +64,21 @@ public function modify(string $threadId, string $messageId, array $parameters):
return ThreadMessageResponse::from($response->data(), $response->meta());
}

/**
* Deletes a message.
*
* @see https://platform.openai.com/docs/api-reference/messages/deleteMessage
*/
public function delete(string $threadId, string $messageId): ThreadMessageDeleteResponse
{
$payload = Payload::delete("threads/$threadId/messages", $messageId);

/** @var Response<array{id: string, object: string, deleted: bool}> $response */
$response = $this->transporter->requestObject($payload);

return ThreadMessageDeleteResponse::from($response->data(), $response->meta());
}

/**
* Returns a list of messages for a given thread.
*
Expand Down
61 changes: 61 additions & 0 deletions src/Responses/Threads/Messages/ThreadMessageDeleteResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace OpenAI\Responses\Threads\Messages;

use OpenAI\Contracts\ResponseContract;
use OpenAI\Contracts\ResponseHasMetaInformationContract;
use OpenAI\Responses\Concerns\ArrayAccessible;
use OpenAI\Responses\Concerns\HasMetaInformation;
use OpenAI\Responses\Meta\MetaInformation;
use OpenAI\Testing\Responses\Concerns\Fakeable;

/**
* @implements ResponseContract<array{id: string, object: string, deleted: bool}>
*/
final class ThreadMessageDeleteResponse implements ResponseContract, ResponseHasMetaInformationContract
{
/**
* @use ArrayAccessible<array{id: string, object: string, deleted: bool}>
*/
use ArrayAccessible;

use Fakeable;
use HasMetaInformation;

private function __construct(
public readonly string $id,
public readonly string $object,
public readonly bool $deleted,
private readonly MetaInformation $meta,
) {
}

/**
* Acts as static factory, and returns a new Response instance.
*
* @param array{id: string, object: string, deleted: bool} $attributes
*/
public static function from(array $attributes, MetaInformation $meta): self
{
return new self(
$attributes['id'],
$attributes['object'],
$attributes['deleted'],
$meta,
);
}

/**
* {@inheritDoc}
*/
public function toArray(): array
{
return [
'id' => $this->id,
'object' => $this->object,
'deleted' => $this->deleted,
];
}
}
6 changes: 6 additions & 0 deletions src/Testing/Resources/ThreadsMessagesTestResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use OpenAI\Contracts\Resources\ThreadsMessagesContract;
use OpenAI\Resources\ThreadsMessages;
use OpenAI\Responses\Threads\Messages\ThreadMessageDeleteResponse;
use OpenAI\Responses\Threads\Messages\ThreadMessageListResponse;
use OpenAI\Responses\Threads\Messages\ThreadMessageResponse;
use OpenAI\Testing\Resources\Concerns\Testable;
Expand Down Expand Up @@ -32,6 +33,11 @@ public function modify(string $threadId, string $messageId, array $parameters):
return $this->record(__FUNCTION__, func_get_args());
}

public function delete(string $threadId, string $messageId): ThreadMessageDeleteResponse
{
return $this->record(__FUNCTION__, func_get_args());
}

public function list(string $threadId, array $parameters = []): ThreadMessageListResponse
{
return $this->record(__FUNCTION__, func_get_args());
Expand Down
12 changes: 12 additions & 0 deletions tests/Fixtures/ThreadMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,15 @@ function threadMessageListResource(): array
'has_more' => false,
];
}

/**
* @return array<string, mixed>
*/
function threadMessageDeleteResource(): array
{
return [
'id' => 'msg_KNsDDwE41BUAHhcPNpDkdHWZ',
'object' => 'thread.message.deleted',
'deleted' => true,
];
}
16 changes: 16 additions & 0 deletions tests/Resources/ThreadsMessages.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use OpenAI\Responses\Meta\MetaInformation;
use OpenAI\Responses\Threads\Messages\ThreadMessageDeleteResponse;
use OpenAI\Responses\Threads\Messages\ThreadMessageListResponse;
use OpenAI\Responses\Threads\Messages\ThreadMessageResponse;
use OpenAI\Responses\Threads\Messages\ThreadMessageResponseAttachment;
Expand Down Expand Up @@ -132,3 +133,18 @@
expect($result->meta())
->toBeInstanceOf(MetaInformation::class);
});

test('delete', function () {
$client = mockClient('DELETE', 'threads/thread_agvtHUGezjTCt4SKgQg0NJ2Y/messages/msg_KNsDDwE41BUAHhcPNpDkdHWZ', [], Response::from(threadMessageDeleteResource(), metaHeaders()));

$result = $client->threads()->messages()->delete('thread_agvtHUGezjTCt4SKgQg0NJ2Y', 'msg_KNsDDwE41BUAHhcPNpDkdHWZ');

expect($result)
->toBeInstanceOf(ThreadMessageDeleteResponse::class)
->id->toBe('msg_KNsDDwE41BUAHhcPNpDkdHWZ')
->object->toBe('thread.message.deleted')
->deleted->toBe(true);

expect($result->meta())
->toBeInstanceOf(MetaInformation::class);
});
47 changes: 47 additions & 0 deletions tests/Responses/Threads/Messages/ThreadMessageDeleteResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

use OpenAI\Responses\Meta\MetaInformation;
use OpenAI\Responses\Threads\Messages\ThreadMessageDeleteResponse;

test('from', function () {
$result = ThreadMessageDeleteResponse::from(threadMessageDeleteResource(), meta());

expect($result)
->id->toBe('msg_KNsDDwE41BUAHhcPNpDkdHWZ')
->object->toBe('thread.message.deleted')
->deleted->toBe(true)
->meta()->toBeInstanceOf(MetaInformation::class);
});

test('as array accessible', function () {
$result = ThreadMessageDeleteResponse::from(threadMessageDeleteResource(), meta());

expect($result['id'])
->toBe('msg_KNsDDwE41BUAHhcPNpDkdHWZ');
});

test('to array', function () {
$result = ThreadMessageDeleteResponse::from(threadMessageDeleteResource(), meta());

expect($result->toArray())
->toBe(threadMessageDeleteResource());
});

test('fake', function () {
$response = ThreadMessageDeleteResponse::fake();

expect($response)
->id->toBe('msg_KNsDDwE41BUAHhcPNpDkdHWZ')
->deleted->toBe(true);
});

test('fake with override', function () {
$response = ThreadMessageDeleteResponse::fake([
'id' => 'msg_1234',
'deleted' => false,
]);

expect($response)
->id->toBe('msg_1234')
->deleted->toBe(false);
});
14 changes: 14 additions & 0 deletions tests/Testing/Resources/ThreadsMessagesTestResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@
});
});

it('records a thread message delete request', function () {
$fake = new ClientFake([
\OpenAI\Responses\Threads\Messages\ThreadMessageDeleteResponse::fake(),
]);

$fake->threads()->messages()->delete('thread_tKFLqzRN9n7MnyKKvc1Q7868', 'msg_KNsDDwE41BUAHhcPNpDkdHWZ');

$fake->assertSent(ThreadsMessages::class, function ($method, $threadId, $messageId) {
return $method === 'delete' &&
$threadId === 'thread_tKFLqzRN9n7MnyKKvc1Q7868' &&
$messageId === 'msg_KNsDDwE41BUAHhcPNpDkdHWZ';
});
});

it('records a thread message list request', function () {
$fake = new ClientFake([
ThreadMessageListResponse::fake(),
Expand Down
Loading