Skip to content

Commit 561c624

Browse files
committed
Add RequestHandled event
1 parent 5aeff0d commit 561c624

27 files changed

+475
-214
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"require-dev": {
2626
"guzzlehttp/guzzle": "^7.8.0",
2727
"guzzlehttp/psr7": "^2.6.1",
28+
"illuminate/contracts": "^9.0|^10.0",
2829
"laravel/pint": "^1.13.6",
2930
"mockery/mockery": "^1.6.6",
3031
"nunomaduro/collision": "^7.10.0",

src/Client.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use OpenAI\Contracts\DispatcherContract;
99
use OpenAI\Contracts\Resources\ThreadsContract;
1010
use OpenAI\Contracts\TransporterContract;
11-
use OpenAI\Events\Dispatcher;
1211
use OpenAI\Resources\Assistants;
1312
use OpenAI\Resources\Audio;
1413
use OpenAI\Resources\Chat;
@@ -31,8 +30,7 @@ final class Client implements ClientContract
3130
public function __construct(
3231
private readonly TransporterContract $transporter,
3332
private readonly DispatcherContract $events,
34-
)
35-
{
33+
) {
3634
// ..
3735
}
3836

@@ -44,7 +42,7 @@ public function __construct(
4442
*/
4543
public function completions(): Completions
4644
{
47-
return new Completions($this->transporter);
45+
return new Completions($this->transporter, $this->events);
4846
}
4947

5048
/**
@@ -54,7 +52,7 @@ public function completions(): Completions
5452
*/
5553
public function chat(): Chat
5654
{
57-
return new Chat($this->transporter);
55+
return new Chat($this->transporter, $this->events);
5856
}
5957

6058
/**
@@ -64,7 +62,7 @@ public function chat(): Chat
6462
*/
6563
public function embeddings(): Embeddings
6664
{
67-
return new Embeddings($this->transporter);
65+
return new Embeddings($this->transporter, $this->events);
6866
}
6967

7068
/**
@@ -74,7 +72,7 @@ public function embeddings(): Embeddings
7472
*/
7573
public function audio(): Audio
7674
{
77-
return new Audio($this->transporter);
75+
return new Audio($this->transporter, $this->events);
7876
}
7977

8078
/**
@@ -84,7 +82,7 @@ public function audio(): Audio
8482
*/
8583
public function edits(): Edits
8684
{
87-
return new Edits($this->transporter);
85+
return new Edits($this->transporter, $this->events);
8886
}
8987

9088
/**
@@ -94,7 +92,7 @@ public function edits(): Edits
9492
*/
9593
public function files(): Files
9694
{
97-
return new Files($this->transporter);
95+
return new Files($this->transporter, $this->events);
9896
}
9997

10098
/**
@@ -104,7 +102,7 @@ public function files(): Files
104102
*/
105103
public function models(): Models
106104
{
107-
return new Models($this->transporter);
105+
return new Models($this->transporter, $this->events);
108106
}
109107

110108
/**
@@ -114,7 +112,7 @@ public function models(): Models
114112
*/
115113
public function fineTuning(): FineTuning
116114
{
117-
return new FineTuning($this->transporter);
115+
return new FineTuning($this->transporter, $this->events);
118116
}
119117

120118
/**
@@ -126,7 +124,7 @@ public function fineTuning(): FineTuning
126124
*/
127125
public function fineTunes(): FineTunes
128126
{
129-
return new FineTunes($this->transporter);
127+
return new FineTunes($this->transporter, $this->events);
130128
}
131129

132130
/**
@@ -136,7 +134,7 @@ public function fineTunes(): FineTunes
136134
*/
137135
public function moderations(): Moderations
138136
{
139-
return new Moderations($this->transporter);
137+
return new Moderations($this->transporter, $this->events);
140138
}
141139

142140
/**
@@ -146,7 +144,7 @@ public function moderations(): Moderations
146144
*/
147145
public function images(): Images
148146
{
149-
return new Images($this->transporter);
147+
return new Images($this->transporter, $this->events);
150148
}
151149

152150
/**
@@ -166,6 +164,6 @@ public function assistants(): Assistants
166164
*/
167165
public function threads(): ThreadsContract
168166
{
169-
return new Threads($this->transporter);
167+
return new Threads($this->transporter, $this->events);
170168
}
171169
}

src/Contracts/DispatcherContract.php

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

55
namespace OpenAI\Contracts;
66

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-
147
/**
158
* @internal
169
*/

src/Events/Dispatcher.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ class Dispatcher implements DispatcherContract
1010
{
1111
public function __construct(
1212
private readonly LaravelDispatcher|EventDispatcherInterface|null $events
13-
)
14-
{
13+
) {
1514
}
1615

1716
public function dispatch(object $event): void

src/Events/RequestHandled.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
class RequestHandled
1010
{
11+
// @phpstan-ignore-next-line
1112
public function __construct(
1213
public readonly Payload $payload,
1314
public readonly ResponseContract|ResponseStreamContract|string $response,
14-
)
15-
{
15+
) {
1616
}
1717
}

src/Factory.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,6 @@ public function withQueryParam(string $name, string $value): self
133133

134134
/**
135135
* Set the event dispatcher instance.
136-
*
137-
* @param LaravelDispatcher|EventDispatcherInterface $events
138136
*/
139137
public function withEventDispatcher(LaravelDispatcher|EventDispatcherInterface $events): self
140138
{

src/Resources/Assistants.php

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ public function create(array $parameters): AssistantResponse
2828
{
2929
$payload = Payload::create('assistants', $parameters);
3030

31-
/** @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 */
32-
$response = $this->transporter->requestObject($payload);
31+
/** @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 */
32+
$responseRaw = $this->transporter->requestObject($payload);
3333

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

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

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

50-
/** @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 */
51-
$response = $this->transporter->requestObject($payload);
50+
/** @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 */
51+
$responseRaw = $this->transporter->requestObject($payload);
5252

53-
return AssistantResponse::from($response->data(), $response->meta());
53+
$response = AssistantResponse::from($responseRaw->data(), $responseRaw->meta());
54+
55+
$this->event(new RequestHandled($payload, $response));
56+
57+
return $response;
5458
}
5559

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

67-
/** @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 */
68-
$response = $this->transporter->requestObject($payload);
71+
/** @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 */
72+
$responseRaw = $this->transporter->requestObject($payload);
6973

70-
return AssistantResponse::from($response->data(), $response->meta());
74+
$response = AssistantResponse::from($responseRaw->data(), $responseRaw->meta());
75+
76+
$this->event(new RequestHandled($payload, $response));
77+
78+
return $response;
7179
}
7280

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

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

85-
return AssistantDeleteResponse::from($response->data(), $response->meta());
93+
$response = AssistantDeleteResponse::from($responseRaw->data(), $responseRaw->meta());
94+
95+
$this->event(new RequestHandled($payload, $response));
96+
97+
return $response;
8698
}
8799

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

99-
/** @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 */
100-
$response = $this->transporter->requestObject($payload);
111+
/** @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 */
112+
$responseRaw = $this->transporter->requestObject($payload);
101113

102-
return AssistantListResponse::from($response->data(), $response->meta());
114+
$response = AssistantListResponse::from($responseRaw->data(), $responseRaw->meta());
115+
116+
$this->event(new RequestHandled($payload, $response));
117+
118+
return $response;
103119
}
104120

105121
/**
@@ -109,6 +125,6 @@ public function list(array $parameters = []): AssistantListResponse
109125
*/
110126
public function files(): AssistantsFilesContract
111127
{
112-
return new AssistantsFiles($this->transporter);
128+
return new AssistantsFiles($this->transporter, $this->events);
113129
}
114130
}

src/Resources/AssistantsFiles.php

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

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

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

32-
return AssistantFileResponse::from($response->data(), $response->meta());
33+
$response = AssistantFileResponse::from($responseRaw->data(), $responseRaw->meta());
34+
35+
$this->event(new RequestHandled($payload, $response));
36+
37+
return $response;
3338
}
3439

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

44-
/** @var Response<array{id: string, object: string, created_at: int, assistant_id: string}> $response */
45-
$response = $this->transporter->requestObject($payload);
49+
/** @var Response<array{id: string, object: string, created_at: int, assistant_id: string}> $responseRaw */
50+
$responseRaw = $this->transporter->requestObject($payload);
51+
52+
$response = AssistantFileResponse::from($responseRaw->data(), $responseRaw->meta());
53+
54+
$this->event(new RequestHandled($payload, $response));
4655

47-
return AssistantFileResponse::from($response->data(), $response->meta());
56+
return $response;
4857
}
4958

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

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

62-
return AssistantFileDeleteResponse::from($response->data(), $response->meta());
71+
$response = AssistantFileDeleteResponse::from($responseRaw->data(), $responseRaw->meta());
72+
73+
$this->event(new RequestHandled($payload, $response));
74+
75+
return $response;
6376
}
6477

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

76-
/** @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 */
77-
$response = $this->transporter->requestObject($payload);
89+
/** @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 */
90+
$responseRaw = $this->transporter->requestObject($payload);
91+
92+
$response = AssistantFileListResponse::from($responseRaw->data(), $responseRaw->meta());
93+
94+
$this->event(new RequestHandled($payload, $response));
7895

79-
return AssistantFileListResponse::from($response->data(), $response->meta());
96+
return $response;
8097
}
8198
}

0 commit comments

Comments
 (0)