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
2 changes: 1 addition & 1 deletion src/platform/src/Bridge/Anthropic/ResultConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function convert(RawHttpResult|RawResultInterface $result, array $options

if (429 === $response->getStatusCode()) {
$retryAfter = $response->getHeaders(false)['retry-after'][0] ?? null;
$retryAfterValue = $retryAfter ? (float) $retryAfter : null;
$retryAfterValue = $retryAfter ? (int) $retryAfter : null;
throw new RateLimitExceededException($retryAfterValue);
}

Expand Down
21 changes: 8 additions & 13 deletions src/platform/src/Bridge/OpenAi/Gpt/ResultConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,11 @@ public function convert(RawResultInterface|RawHttpResult $result, array $options

if (429 === $response->getStatusCode()) {
$headers = $response->getHeaders(false);
$resetTime = null;
$resetTime = $headers['x-ratelimit-reset-requests'][0]
?? $headers['x-ratelimit-reset-tokens'][0]
?? null;

if (isset($headers['x-ratelimit-reset-requests'][0])) {
$resetTime = self::parseResetTime($headers['x-ratelimit-reset-requests'][0]);
} elseif (isset($headers['x-ratelimit-reset-tokens'][0])) {
$resetTime = self::parseResetTime($headers['x-ratelimit-reset-tokens'][0]);
}

throw new RateLimitExceededException($resetTime);
throw new RateLimitExceededException($resetTime ? self::parseResetTime($resetTime) : null);
}

if ($options['stream'] ?? false) {
Expand Down Expand Up @@ -221,16 +217,15 @@ private function convertToolCall(array $toolCall): ToolCall
* - "6m0s"
* - "2m30s"
*/
private static function parseResetTime(string $resetTime): float
private static function parseResetTime(string $resetTime): ?int
{
$seconds = 0;

if (preg_match('/^(?:(\d+)m)?(?:(\d+)s)?$/', $resetTime, $matches)) {
$minutes = isset($matches[1]) ? (int) $matches[1] : 0;
$secs = isset($matches[2]) ? (int) $matches[2] : 0;
$seconds = ($minutes * 60) + $secs;

return ($minutes * 60) + $secs;
}

return (float) $seconds;
return null;
}
}
4 changes: 2 additions & 2 deletions src/platform/src/Exception/RateLimitExceededException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
final class RateLimitExceededException extends RuntimeException
{
public function __construct(
private readonly ?float $retryAfter = null,
private readonly ?int $retryAfter = null,
) {
parent::__construct('Rate limit exceeded.');
}

public function getRetryAfter(): ?float
public function getRetryAfter(): ?int
{
return $this->retryAfter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testRateLimitExceededThrowsException()
try {
$handler->convert(new RawHttpResult($httpResponse));
} catch (RateLimitExceededException $e) {
$this->assertSame(60.0, $e->getRetryAfter());
$this->assertSame(60, $e->getRetryAfter());
throw $e;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testRateLimitExceededWithRequestsResetTime()
try {
$handler->convert(new RawHttpResult($httpResponse));
} catch (RateLimitExceededException $e) {
$this->assertSame(20.0, $e->getRetryAfter());
$this->assertSame(20, $e->getRetryAfter());
throw $e;
}
}
Expand All @@ -69,7 +69,7 @@ public function testRateLimitExceededWithTokensResetTime()
try {
$handler->convert(new RawHttpResult($httpResponse));
} catch (RateLimitExceededException $e) {
$this->assertSame(150.0, $e->getRetryAfter());
$this->assertSame(150, $e->getRetryAfter());
throw $e;
}
}
Expand Down