Skip to content
Open
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
31 changes: 12 additions & 19 deletions src/platform/src/Result/RawHttpResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,23 @@ public function getData(): array
public function getDataStream(): iterable
{
foreach ((new EventSourceHttpClient())->stream($this->response) as $chunk) {
if ($chunk->isFirst() || $chunk->isLast() || ($chunk instanceof ServerSentEvent && '[DONE]' === $chunk->getData())) {
// Handle only complete events (no need for handle DataChunk`s)
if (!$chunk instanceof ServerSentEvent) {
continue;
}

$jsonDelta = $chunk instanceof ServerSentEvent ? $chunk->getData() : $chunk->getContent();

// Remove leading/trailing brackets
if (str_starts_with($jsonDelta, '[') || str_starts_with($jsonDelta, ',')) {
$jsonDelta = substr($jsonDelta, 1);
}
if (str_ends_with($jsonDelta, ']')) {
$jsonDelta = substr($jsonDelta, 0, -1);
// Do not handle: Init, Terminate, Errors, Comments and openAI specific termination via DONE
if (
$chunk->isFirst()
|| $chunk->isLast()
|| null !== $chunk->getError()
|| str_starts_with($chunk->getContent(), ':')
|| '[DONE]' === $chunk->getData()
) {
continue;
}

// Split in case of multiple JSON objects
$deltas = explode(",\r\n", $jsonDelta);

foreach ($deltas as $delta) {
if ('' === trim($delta)) {
continue;
}

yield json_decode($delta, true, flags: \JSON_THROW_ON_ERROR);
}
Comment on lines -50 to -59
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the reason for removing this?
there is no replacement for this part or do i miss something?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

background for having this was gemini api streaming parts of a larger json document - which cannot be decoded right away - testing with examples/gemini/stream.php might help

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will check this. The "delta"split is part of the SSE integration and is handled via the symfony/http-client (I think so): https://github.com/symfony/http-client/blob/4f420c441088fc3364647565701f34ace5ba0d5f/EventSourceHttpClient.php#L136 Only complete SSE events create ServerSentEvent objects. But I will check the specification, symfony/http-client and gemini API again. I will share my information in the next days...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... and the json_decord part is included in the $chunk->getArrayData() call. I will check this too...

yield $chunk->getArrayData();
}
}

Expand Down
Loading