Skip to content

Commit

Permalink
Add RequestHandled event
Browse files Browse the repository at this point in the history
  • Loading branch information
gehrisandro committed Dec 5, 2023
1 parent 5aeff0d commit 561c624
Show file tree
Hide file tree
Showing 27 changed files with 475 additions and 214 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"require-dev": {
"guzzlehttp/guzzle": "^7.8.0",
"guzzlehttp/psr7": "^2.6.1",
"illuminate/contracts": "^9.0|^10.0",
"laravel/pint": "^1.13.6",
"mockery/mockery": "^1.6.6",
"nunomaduro/collision": "^7.10.0",
Expand Down
28 changes: 13 additions & 15 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use OpenAI\Contracts\DispatcherContract;
use OpenAI\Contracts\Resources\ThreadsContract;
use OpenAI\Contracts\TransporterContract;
use OpenAI\Events\Dispatcher;
use OpenAI\Resources\Assistants;
use OpenAI\Resources\Audio;
use OpenAI\Resources\Chat;
Expand All @@ -31,8 +30,7 @@ final class Client implements ClientContract
public function __construct(
private readonly TransporterContract $transporter,
private readonly DispatcherContract $events,
)
{
) {
// ..
}

Expand All @@ -44,7 +42,7 @@ public function __construct(
*/
public function completions(): Completions
{
return new Completions($this->transporter);
return new Completions($this->transporter, $this->events);
}

/**
Expand All @@ -54,7 +52,7 @@ public function completions(): Completions
*/
public function chat(): Chat
{
return new Chat($this->transporter);
return new Chat($this->transporter, $this->events);
}

/**
Expand All @@ -64,7 +62,7 @@ public function chat(): Chat
*/
public function embeddings(): Embeddings
{
return new Embeddings($this->transporter);
return new Embeddings($this->transporter, $this->events);
}

/**
Expand All @@ -74,7 +72,7 @@ public function embeddings(): Embeddings
*/
public function audio(): Audio
{
return new Audio($this->transporter);
return new Audio($this->transporter, $this->events);
}

/**
Expand All @@ -84,7 +82,7 @@ public function audio(): Audio
*/
public function edits(): Edits
{
return new Edits($this->transporter);
return new Edits($this->transporter, $this->events);
}

/**
Expand All @@ -94,7 +92,7 @@ public function edits(): Edits
*/
public function files(): Files
{
return new Files($this->transporter);
return new Files($this->transporter, $this->events);
}

/**
Expand All @@ -104,7 +102,7 @@ public function files(): Files
*/
public function models(): Models
{
return new Models($this->transporter);
return new Models($this->transporter, $this->events);
}

/**
Expand All @@ -114,7 +112,7 @@ public function models(): Models
*/
public function fineTuning(): FineTuning
{
return new FineTuning($this->transporter);
return new FineTuning($this->transporter, $this->events);
}

/**
Expand All @@ -126,7 +124,7 @@ public function fineTuning(): FineTuning
*/
public function fineTunes(): FineTunes
{
return new FineTunes($this->transporter);
return new FineTunes($this->transporter, $this->events);
}

/**
Expand All @@ -136,7 +134,7 @@ public function fineTunes(): FineTunes
*/
public function moderations(): Moderations
{
return new Moderations($this->transporter);
return new Moderations($this->transporter, $this->events);
}

/**
Expand All @@ -146,7 +144,7 @@ public function moderations(): Moderations
*/
public function images(): Images
{
return new Images($this->transporter);
return new Images($this->transporter, $this->events);
}

/**
Expand All @@ -166,6 +164,6 @@ public function assistants(): Assistants
*/
public function threads(): ThreadsContract
{
return new Threads($this->transporter);
return new Threads($this->transporter, $this->events);
}
}
7 changes: 0 additions & 7 deletions src/Contracts/DispatcherContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@

namespace OpenAI\Contracts;

use OpenAI\Exceptions\ErrorException;
use OpenAI\Exceptions\TransporterException;
use OpenAI\Exceptions\UnserializableResponse;
use OpenAI\ValueObjects\Transporter\Payload;
use OpenAI\ValueObjects\Transporter\Response;
use Psr\Http\Message\ResponseInterface;

/**
* @internal
*/
Expand Down
3 changes: 1 addition & 2 deletions src/Events/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ class Dispatcher implements DispatcherContract
{
public function __construct(
private readonly LaravelDispatcher|EventDispatcherInterface|null $events
)
{
) {
}

public function dispatch(object $event): void
Expand Down
4 changes: 2 additions & 2 deletions src/Events/RequestHandled.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

class RequestHandled
{
// @phpstan-ignore-next-line
public function __construct(
public readonly Payload $payload,
public readonly ResponseContract|ResponseStreamContract|string $response,
)
{
) {
}
}
2 changes: 0 additions & 2 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ public function withQueryParam(string $name, string $value): self

/**
* Set the event dispatcher instance.
*
* @param LaravelDispatcher|EventDispatcherInterface $events
*/
public function withEventDispatcher(LaravelDispatcher|EventDispatcherInterface $events): self
{
Expand Down
48 changes: 32 additions & 16 deletions src/Resources/Assistants.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public function create(array $parameters): AssistantResponse
{
$payload = Payload::create('assistants', $parameters);

/** @var Response<array{id: string, object: string, created_at: int, name: ?string, description: ?string, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'retrieval'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, file_ids: array<int, string>, metadata: array<string, string>}> $response */
$response = $this->transporter->requestObject($payload);
/** @var Response<array{id: string, object: string, created_at: int, name: ?string, description: ?string, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'retrieval'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, file_ids: array<int, string>, metadata: array<string, string>}> $responseRaw */
$responseRaw = $this->transporter->requestObject($payload);

$response = AssistantResponse::from($response->data(), $response->meta());
$response = AssistantResponse::from($responseRaw->data(), $responseRaw->meta());

$this->event(new RequestHandled($payload, $response));

Expand All @@ -47,10 +47,14 @@ public function retrieve(string $id): AssistantResponse
{
$payload = Payload::retrieve('assistants', $id);

/** @var Response<array{id: string, object: string, created_at: int, name: ?string, description: ?string, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'retrieval'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, file_ids: array<int, string>, metadata: array<string, string>}> $response */
$response = $this->transporter->requestObject($payload);
/** @var Response<array{id: string, object: string, created_at: int, name: ?string, description: ?string, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'retrieval'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, file_ids: array<int, string>, metadata: array<string, string>}> $responseRaw */
$responseRaw = $this->transporter->requestObject($payload);

return AssistantResponse::from($response->data(), $response->meta());
$response = AssistantResponse::from($responseRaw->data(), $responseRaw->meta());

$this->event(new RequestHandled($payload, $response));

return $response;
}

/**
Expand All @@ -64,10 +68,14 @@ public function modify(string $id, array $parameters): AssistantResponse
{
$payload = Payload::modify('assistants', $id, $parameters);

/** @var Response<array{id: string, object: string, created_at: int, name: ?string, description: ?string, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'retrieval'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, file_ids: array<int, string>, metadata: array<string, string>}> $response */
$response = $this->transporter->requestObject($payload);
/** @var Response<array{id: string, object: string, created_at: int, name: ?string, description: ?string, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'retrieval'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, file_ids: array<int, string>, metadata: array<string, string>}> $responseRaw */
$responseRaw = $this->transporter->requestObject($payload);

return AssistantResponse::from($response->data(), $response->meta());
$response = AssistantResponse::from($responseRaw->data(), $responseRaw->meta());

$this->event(new RequestHandled($payload, $response));

return $response;
}

/**
Expand All @@ -79,10 +87,14 @@ public function delete(string $id): AssistantDeleteResponse
{
$payload = Payload::delete('assistants', $id);

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

return AssistantDeleteResponse::from($response->data(), $response->meta());
$response = AssistantDeleteResponse::from($responseRaw->data(), $responseRaw->meta());

$this->event(new RequestHandled($payload, $response));

return $response;
}

/**
Expand All @@ -96,10 +108,14 @@ public function list(array $parameters = []): AssistantListResponse
{
$payload = Payload::list('assistants', $parameters);

/** @var Response<array{object: string, data: array<int, array{id: string, object: string, created_at: int, name: ?string, description: ?string, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'retrieval'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, file_ids: array<int, string>, metadata: array<string, string>}>, first_id: ?string, last_id: ?string, has_more: bool}> $response */
$response = $this->transporter->requestObject($payload);
/** @var Response<array{object: string, data: array<int, array{id: string, object: string, created_at: int, name: ?string, description: ?string, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'retrieval'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, file_ids: array<int, string>, metadata: array<string, string>}>, first_id: ?string, last_id: ?string, has_more: bool}> $responseRaw */
$responseRaw = $this->transporter->requestObject($payload);

return AssistantListResponse::from($response->data(), $response->meta());
$response = AssistantListResponse::from($responseRaw->data(), $responseRaw->meta());

$this->event(new RequestHandled($payload, $response));

return $response;
}

/**
Expand All @@ -109,6 +125,6 @@ public function list(array $parameters = []): AssistantListResponse
*/
public function files(): AssistantsFilesContract
{
return new AssistantsFiles($this->transporter);
return new AssistantsFiles($this->transporter, $this->events);
}
}
41 changes: 29 additions & 12 deletions src/Resources/AssistantsFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace OpenAI\Resources;

use OpenAI\Contracts\Resources\AssistantsFilesContract;
use OpenAI\Events\RequestHandled;
use OpenAI\Responses\Assistants\Files\AssistantFileDeleteResponse;
use OpenAI\Responses\Assistants\Files\AssistantFileListResponse;
use OpenAI\Responses\Assistants\Files\AssistantFileResponse;
Expand All @@ -26,10 +27,14 @@ public function create(string $assistantId, array $parameters): AssistantFileRes
{
$payload = Payload::create("assistants/$assistantId/files", $parameters);

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

return AssistantFileResponse::from($response->data(), $response->meta());
$response = AssistantFileResponse::from($responseRaw->data(), $responseRaw->meta());

$this->event(new RequestHandled($payload, $response));

return $response;
}

/**
Expand All @@ -41,10 +46,14 @@ public function retrieve(string $assistantId, string $fileId): AssistantFileResp
{
$payload = Payload::retrieve("assistants/$assistantId/files", $fileId);

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

$response = AssistantFileResponse::from($responseRaw->data(), $responseRaw->meta());

$this->event(new RequestHandled($payload, $response));

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

/**
Expand All @@ -56,10 +65,14 @@ public function delete(string $assistantId, string $fileId): AssistantFileDelete
{
$payload = Payload::delete("assistants/$assistantId/files", $fileId);

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

return AssistantFileDeleteResponse::from($response->data(), $response->meta());
$response = AssistantFileDeleteResponse::from($responseRaw->data(), $responseRaw->meta());

$this->event(new RequestHandled($payload, $response));

return $response;
}

/**
Expand All @@ -73,9 +86,13 @@ public function list(string $assistantId, array $parameters = []): AssistantFile
{
$payload = Payload::list("assistants/$assistantId/files", $parameters);

/** @var Response<array{object: string, data: array<int, array{id: string, object: string, created_at: int, assistant_id: string}>, first_id: ?string, last_id: ?string, has_more: bool}> $response */
$response = $this->transporter->requestObject($payload);
/** @var Response<array{object: string, data: array<int, array{id: string, object: string, created_at: int, assistant_id: string}>, first_id: ?string, last_id: ?string, has_more: bool}> $responseRaw */
$responseRaw = $this->transporter->requestObject($payload);

$response = AssistantFileListResponse::from($responseRaw->data(), $responseRaw->meta());

$this->event(new RequestHandled($payload, $response));

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

0 comments on commit 561c624

Please sign in to comment.