Skip to content

Commit 662ffbe

Browse files
committed
WIP: add events
1 parent 759196c commit 662ffbe

File tree

9 files changed

+110
-10
lines changed

9 files changed

+110
-10
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"psr/http-client": "^1.0.3",
2020
"psr/http-client-implementation": "^1.0.1",
2121
"psr/http-factory-implementation": "*",
22-
"psr/http-message": "^1.1.0|^2.0.0"
22+
"psr/http-message": "^1.1.0|^2.0.0",
23+
"psr/event-dispatcher": "^1.0.0"
2324
},
2425
"require-dev": {
2526
"guzzlehttp/guzzle": "^7.8.0",

src/Client.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
namespace OpenAI;
66

77
use OpenAI\Contracts\ClientContract;
8+
use OpenAI\Contracts\DispatcherContract;
89
use OpenAI\Contracts\Resources\ThreadsContract;
910
use OpenAI\Contracts\TransporterContract;
11+
use OpenAI\Events\Dispatcher;
1012
use OpenAI\Resources\Assistants;
1113
use OpenAI\Resources\Audio;
1214
use OpenAI\Resources\Chat;
@@ -26,7 +28,10 @@ final class Client implements ClientContract
2628
/**
2729
* Creates a Client instance with the given API token.
2830
*/
29-
public function __construct(private readonly TransporterContract $transporter)
31+
public function __construct(
32+
private readonly TransporterContract $transporter,
33+
private readonly DispatcherContract $events,
34+
)
3035
{
3136
// ..
3237
}
@@ -151,7 +156,7 @@ public function images(): Images
151156
*/
152157
public function assistants(): Assistants
153158
{
154-
return new Assistants($this->transporter);
159+
return new Assistants($this->transporter, $this->events);
155160
}
156161

157162
/**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenAI\Contracts;
6+
7+
use OpenAI\Exceptions\ErrorException;
8+
use OpenAI\Exceptions\TransporterException;
9+
use OpenAI\Exceptions\UnserializableResponse;
10+
use OpenAI\ValueObjects\Transporter\Payload;
11+
use OpenAI\ValueObjects\Transporter\Response;
12+
use Psr\Http\Message\ResponseInterface;
13+
14+
/**
15+
* @internal
16+
*/
17+
interface DispatcherContract
18+
{
19+
/**
20+
* Dispatch an event.
21+
*/
22+
public function dispatch(object $event): void;
23+
}

src/Events/Dispatcher.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace OpenAI\Events;
4+
5+
use Illuminate\Contracts\Events\Dispatcher as LaravelDispatcher;
6+
use OpenAI\Contracts\DispatcherContract;
7+
use Psr\EventDispatcher\EventDispatcherInterface;
8+
9+
class Dispatcher implements DispatcherContract
10+
{
11+
public function __construct(
12+
private readonly LaravelDispatcher|EventDispatcherInterface|null $events
13+
)
14+
{
15+
}
16+
17+
public function dispatch(object $event): void
18+
{
19+
$this->events?->dispatch($event);
20+
}
21+
}

src/Events/RequestHandled.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace OpenAI\Events;
4+
5+
use OpenAI\Contracts\ResponseContract;
6+
use OpenAI\Contracts\ResponseStreamContract;
7+
use OpenAI\ValueObjects\Transporter\Payload;
8+
9+
class RequestHandled
10+
{
11+
public function __construct(
12+
public readonly Payload $payload,
13+
public readonly ResponseContract|ResponseStreamContract|string $response,
14+
)
15+
{
16+
}
17+
}

src/Factory.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
use Exception;
77
use GuzzleHttp\Client as GuzzleClient;
88
use Http\Discovery\Psr18ClientDiscovery;
9+
use Illuminate\Contracts\Events\Dispatcher as LaravelDispatcher;
10+
use OpenAI\Events\Dispatcher;
911
use OpenAI\Transporters\HttpTransporter;
1012
use OpenAI\ValueObjects\ApiKey;
1113
use OpenAI\ValueObjects\Transporter\BaseUri;
1214
use OpenAI\ValueObjects\Transporter\Headers;
1315
use OpenAI\ValueObjects\Transporter\QueryParams;
16+
use Psr\EventDispatcher\EventDispatcherInterface;
1417
use Psr\Http\Client\ClientInterface;
1518
use Psr\Http\Message\RequestInterface;
1619
use Psr\Http\Message\ResponseInterface;
@@ -54,6 +57,8 @@ final class Factory
5457

5558
private ?Closure $streamHandler = null;
5659

60+
private Dispatcher|EventDispatcherInterface|null $events = null;
61+
5762
/**
5863
* Sets the API key for the requests.
5964
*/
@@ -126,6 +131,18 @@ public function withQueryParam(string $name, string $value): self
126131
return $this;
127132
}
128133

134+
/**
135+
* Set the event dispatcher instance.
136+
*
137+
* @param LaravelDispatcher|EventDispatcherInterface $events
138+
*/
139+
public function withEventDispatcher(LaravelDispatcher|EventDispatcherInterface $events): self
140+
{
141+
$this->events = $events;
142+
143+
return $this;
144+
}
145+
129146
/**
130147
* Creates a new Open AI Client.
131148
*/
@@ -158,7 +175,9 @@ public function make(): Client
158175

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

161-
return new Client($transporter);
178+
$dispatcher = new Dispatcher($this->events);
179+
180+
return new Client($transporter, $dispatcher);
162181
}
163182

164183
/**

src/Resources/Assistants.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use OpenAI\Contracts\Resources\AssistantsContract;
88
use OpenAI\Contracts\Resources\AssistantsFilesContract;
9+
use OpenAI\Events\RequestHandled;
910
use OpenAI\Responses\Assistants\AssistantDeleteResponse;
1011
use OpenAI\Responses\Assistants\AssistantListResponse;
1112
use OpenAI\Responses\Assistants\AssistantResponse;
@@ -30,7 +31,11 @@ public function create(array $parameters): AssistantResponse
3031
/** @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 */
3132
$response = $this->transporter->requestObject($payload);
3233

33-
return AssistantResponse::from($response->data(), $response->meta());
34+
$response = AssistantResponse::from($response->data(), $response->meta());
35+
36+
$this->event(new RequestHandled($payload, $response));
37+
38+
return $response;
3439
}
3540

3641
/**

src/Resources/Concerns/Transportable.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,24 @@
44

55
namespace OpenAI\Resources\Concerns;
66

7+
use OpenAI\Contracts\DispatcherContract;
78
use OpenAI\Contracts\TransporterContract;
89

910
trait Transportable
1011
{
1112
/**
1213
* Creates a Client instance with the given API token.
1314
*/
14-
public function __construct(private readonly TransporterContract $transporter)
15+
public function __construct(
16+
private readonly TransporterContract $transporter,
17+
private readonly DispatcherContract $events,
18+
)
1519
{
1620
// ..
1721
}
22+
23+
public function event(object $event): void
24+
{
25+
$this->events->dispatch($event);
26+
}
1827
}

src/ValueObjects/Transporter/Payload.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ final class Payload
2424
* @param array<string, mixed> $parameters
2525
*/
2626
private function __construct(
27-
private readonly ContentType $contentType,
28-
private readonly Method $method,
29-
private readonly ResourceUri $uri,
30-
private readonly array $parameters = [],
27+
public readonly ContentType $contentType,
28+
public readonly Method $method,
29+
public readonly ResourceUri $uri,
30+
public readonly array $parameters = [],
3131
) {
3232
// ..
3333
}

0 commit comments

Comments
 (0)