Skip to content

Adds RequestHandled event #286

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

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"psr/http-client": "^1.0.3",
"psr/http-client-implementation": "^1.0.1",
"psr/http-factory-implementation": "*",
"psr/http-message": "^1.1.0|^2.0.0"
"psr/http-message": "^1.1.0|^2.0.0",
"psr/event-dispatcher": "^1.0.0"
},
"require-dev": {
"guzzlehttp/guzzle": "^7.8.1",
Expand Down
2 changes: 2 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Rector\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
Expand All @@ -15,6 +16,7 @@

$rectorConfig->skip([
__DIR__.'/src/Testing/ClientFake.php' => FinalizeClassesWithoutChildrenRector::class,
__DIR__.'/src/Events/NullEventDispatcher.php' => AddVoidReturnTypeWhereNoReturnRector::class,
]);

$rectorConfig->rules([
Expand Down
33 changes: 18 additions & 15 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace OpenAI;

use OpenAI\Contracts\ClientContract;
use OpenAI\Contracts\DispatcherContract;
use OpenAI\Contracts\Resources\ThreadsContract;
use OpenAI\Contracts\TransporterContract;
use OpenAI\Resources\Assistants;
Expand All @@ -26,8 +27,10 @@ final class Client implements ClientContract
/**
* Creates a Client instance with the given API token.
*/
public function __construct(private readonly TransporterContract $transporter)
{
public function __construct(
private readonly TransporterContract $transporter,
private readonly DispatcherContract $events,
) {
// ..
}

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

/**
Expand All @@ -49,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 @@ -59,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 @@ -69,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 @@ -79,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 @@ -89,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 @@ -99,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 @@ -109,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 @@ -121,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 @@ -131,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 @@ -141,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 @@ -151,7 +154,7 @@ public function images(): Images
*/
public function assistants(): Assistants
{
return new Assistants($this->transporter);
return new Assistants($this->transporter, $this->events);
}

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

declare(strict_types=1);

namespace OpenAI\Contracts;

/**
* @internal
*/
interface DispatcherContract
{
/**
* Dispatch an event.
*/
public function dispatch(object $event): void;
}
19 changes: 19 additions & 0 deletions src/Events/Dispatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace OpenAI\Events;

use OpenAI\Contracts\DispatcherContract;
use Psr\EventDispatcher\EventDispatcherInterface;

class Dispatcher implements DispatcherContract
{
public function __construct(
private readonly EventDispatcherInterface $events
) {
}

public function dispatch(object $event): void
{
$this->events->dispatch($event);
}
}
13 changes: 13 additions & 0 deletions src/Events/NullEventDispatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace OpenAI\Events;

use Psr\EventDispatcher\EventDispatcherInterface;

class NullEventDispatcher implements EventDispatcherInterface
{
public function dispatch(object $event) // @pest-ignore-type
{
return $event;
}
}
17 changes: 17 additions & 0 deletions src/Events/RequestHandled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace OpenAI\Events;

use OpenAI\Contracts\ResponseContract;
use OpenAI\Contracts\ResponseStreamContract;
use OpenAI\ValueObjects\Transporter\Payload;

class RequestHandled
{
// @phpstan-ignore-next-line
public function __construct(
public readonly Payload $payload,
public readonly ResponseContract|ResponseStreamContract|string $response,
) {
}
}
19 changes: 18 additions & 1 deletion src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
use Exception;
use GuzzleHttp\Client as GuzzleClient;
use Http\Discovery\Psr18ClientDiscovery;
use OpenAI\Events\Dispatcher;
use OpenAI\Events\NullEventDispatcher;
use OpenAI\Transporters\HttpTransporter;
use OpenAI\ValueObjects\ApiKey;
use OpenAI\ValueObjects\Transporter\BaseUri;
use OpenAI\ValueObjects\Transporter\Headers;
use OpenAI\ValueObjects\Transporter\QueryParams;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -54,6 +57,8 @@ final class Factory

private ?Closure $streamHandler = null;

private ?EventDispatcherInterface $events = null;

/**
* Sets the API key for the requests.
*/
Expand Down Expand Up @@ -126,6 +131,16 @@ public function withQueryParam(string $name, string $value): self
return $this;
}

/**
* Set the event dispatcher instance.
*/
public function withEventDispatcher(EventDispatcherInterface $events): self
{
$this->events = $events;

return $this;
}

/**
* Creates a new Open AI Client.
*/
Expand Down Expand Up @@ -158,7 +173,9 @@ public function make(): Client

$transporter = new HttpTransporter($client, $baseUri, $headers, $queryParams, $sendAsync);

return new Client($transporter);
$dispatcher = new Dispatcher($this->events ?? new NullEventDispatcher);

return new Client($transporter, $dispatcher);
}

/**
Expand Down
Loading