Skip to content
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
4 changes: 3 additions & 1 deletion src/Responses/Responses/CreateStreamedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use OpenAI\Responses\Responses\Streaming\OutputTextAnnotationAdded;
use OpenAI\Responses\Responses\Streaming\OutputTextDelta;
use OpenAI\Responses\Responses\Streaming\OutputTextDone;
use OpenAI\Responses\Responses\Streaming\RateLimits;
use OpenAI\Responses\Responses\Streaming\ReasoningSummaryPart;
use OpenAI\Responses\Responses\Streaming\ReasoningSummaryTextDelta;
use OpenAI\Responses\Responses\Streaming\ReasoningSummaryTextDone;
Expand Down Expand Up @@ -53,7 +54,7 @@ final class CreateStreamedResponse implements ResponseContract

private function __construct(
public readonly string $event,
public readonly Response|OutputItem|ContentPart|OutputTextDelta|OutputTextAnnotationAdded|OutputTextDone|RefusalDelta|RefusalDone|FunctionCallArgumentsDelta|FunctionCallArgumentsDone|FileSearchCall|WebSearchCall|CodeInterpreterCall|CodeInterpreterCodeDelta|CodeInterpreterCodeDone|ReasoningSummaryPart|ReasoningSummaryTextDelta|ReasoningSummaryTextDone|ReasoningTextDelta|ReasoningTextDone|McpListTools|McpListToolsInProgress|McpCall|McpCallArgumentsDelta|McpCallArgumentsDone|ImageGenerationPart|ImageGenerationPartialImage|Error $response,
public readonly Response|OutputItem|ContentPart|OutputTextDelta|OutputTextAnnotationAdded|OutputTextDone|RefusalDelta|RefusalDone|FunctionCallArgumentsDelta|FunctionCallArgumentsDone|FileSearchCall|WebSearchCall|CodeInterpreterCall|CodeInterpreterCodeDelta|CodeInterpreterCodeDone|ReasoningSummaryPart|ReasoningSummaryTextDelta|ReasoningSummaryTextDone|ReasoningTextDelta|ReasoningTextDone|McpListTools|McpListToolsInProgress|McpCall|McpCallArgumentsDelta|McpCallArgumentsDone|ImageGenerationPart|ImageGenerationPartialImage|RateLimits|Error $response,
) {}

/**
Expand Down Expand Up @@ -115,6 +116,7 @@ public static function from(array $attributes): self
'response.image_generation_call.generating',
'response.image_generation_call.in_progress' => ImageGenerationPart::from($attributes, $meta), // @phpstan-ignore-line
'response.image_generation_call.partial_image' => ImageGenerationPartialImage::from($attributes, $meta), // @phpstan-ignore-line
'response.rate_limits.updated' => RateLimits::from($attributes, $meta), // @phpstan-ignore-line
'error' => Error::from($attributes, $meta), // @phpstan-ignore-line
default => throw new UnknownEventException('Unknown Responses streaming event: '.$event),
};
Expand Down
52 changes: 52 additions & 0 deletions src/Responses/Responses/Streaming/RateLimits.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace OpenAI\Responses\Responses\Streaming;

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;

/**
* @phpstan-type RateLimitsType array{type: string}
*
* @implements ResponseContract<RateLimitsType>
*/
final class RateLimits implements ResponseContract, ResponseHasMetaInformationContract
{
/**
* @use ArrayAccessible<RateLimitsType>
*/
use ArrayAccessible;

use Fakeable;
use HasMetaInformation;

private function __construct(
private readonly MetaInformation $meta,
) {}

/**
* @param RateLimitsType $attributes
*/
public static function from(array $attributes, MetaInformation $meta): self
{
return new self(
meta: $meta,
);
}

/**
* {@inheritDoc}
*/
public function toArray(): array
{
return [
'type' => 'response.rate_limits.updated',
];
}
}
5 changes: 5 additions & 0 deletions tests/Fixtures/Responses.php
Original file line number Diff line number Diff line change
Expand Up @@ -944,3 +944,8 @@ function responseReasoningTextDoneEvent()
{
return fopen(__DIR__.'/Streams/ResponseReasoningTextDone.txt', 'r');
}

function responseRateLimitsUpdatedEvent()
{
return fopen(__DIR__.'/Streams/ResponseRateLimitsUpdated.txt', 'r');
}
1 change: 1 addition & 0 deletions tests/Fixtures/Streams/ResponseRateLimitsUpdated.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data: {"type":"response.rate_limits.updated"}
13 changes: 13 additions & 0 deletions tests/Responses/Responses/CreateStreamedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use OpenAI\Responses\Responses\CreateResponse;
use OpenAI\Responses\Responses\CreateStreamedResponse;
use OpenAI\Responses\Responses\Streaming\RateLimits;
use OpenAI\Responses\Responses\Streaming\ReasoningTextDelta;
use OpenAI\Responses\Responses\Streaming\ReasoningTextDone;
use OpenAI\Responses\Responses\Streaming\Response;
Expand Down Expand Up @@ -55,3 +56,15 @@
->response->contentIndex->toBe(0)
->response->sequenceNumber->toBe(10);
});

test('rate limits updated event', function () {
$response = CreateStreamedResponse::fake(responseRateLimitsUpdatedEvent());

expect($response->getIterator()->current())
->toBeInstanceOf(CreateStreamedResponse::class)
->event->toBe('response.rate_limits.updated')
->response->toBeInstanceOf(RateLimits::class)
->response->toArray()->toBe([
'type' => 'response.rate_limits.updated',
]);
});