Skip to content

Commit 5b592a3

Browse files
committed
clean up the code
1 parent 0dc9a47 commit 5b592a3

File tree

1 file changed

+10
-43
lines changed

1 file changed

+10
-43
lines changed

src/Providers/Gemini/Handlers/Stream.php

+10-43
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
use Prism\Prism\Enums\Provider;
1414
use Prism\Prism\Exceptions\PrismChunkDecodeException;
1515
use Prism\Prism\Exceptions\PrismException;
16-
use Prism\Prism\Providers\Gemini\Concerns\ExtractSearchGroundings;
17-
use Prism\Prism\Providers\Gemini\Concerns\ValidatesResponse;
1816
use Prism\Prism\Providers\Gemini\Maps\FinishReasonMap;
1917
use Prism\Prism\Providers\Gemini\Maps\MessageMap;
2018
use Prism\Prism\Providers\Gemini\Maps\ToolChoiceMap;
@@ -29,7 +27,7 @@
2927

3028
class Stream
3129
{
32-
use CallsTools, ExtractSearchGroundings, ValidatesResponse;
30+
use CallsTools;
3331

3432
public function __construct(
3533
protected PendingRequest $client,
@@ -58,8 +56,6 @@ protected function processStream(Response $response, Request $request, int $dept
5856

5957
$text = '';
6058
$toolCalls = [];
61-
$groundingSupports = [];
62-
$groundingChunks = [];
6359

6460
while (! $response->getBody()->eof()) {
6561
$data = $this->parseNextDataLine($response->getBody());
@@ -73,44 +69,24 @@ protected function processStream(Response $response, Request $request, int $dept
7369
if ($this->hasToolCalls($data)) {
7470
$toolCalls = $this->extractToolCalls($data, $toolCalls);
7571

76-
continue;
77-
}
72+
// Check if this is the final part of the tool calls
73+
if ($this->mapFinishReason($data) === FinishReason::ToolCalls) {
74+
yield from $this->handleToolCalls($request, $text, $toolCalls, $depth);
75+
}
7876

79-
// Extract search grounding information if present
80-
if ($this->hasSearchGrounding($data)) {
81-
$groundingSupports = array_merge($groundingSupports, data_get($data, 'candidates.0.content.parts.0.text.groundingSupport', []));
82-
$groundingChunks = array_merge($groundingChunks, data_get($data, 'groundingChunks', []));
77+
continue;
8378
}
8479

8580
// Handle content
8681
$content = data_get($data, 'candidates.0.content.parts.0.text') ?? '';
87-
if (is_string($content)) {
88-
$text .= $content;
89-
90-
yield new Chunk(
91-
text: $content,
92-
finishReason: null,
93-
);
94-
}
95-
96-
// Handle finish reason
97-
$finishReason = $this->mapFinishReason($data);
98-
if ($finishReason === FinishReason::Unknown) {
99-
continue;
100-
}
82+
$text .= $content;
10183

102-
if ($finishReason === FinishReason::ToolCalls) {
103-
yield from $this->handleToolCalls($request, $text, $toolCalls, $depth);
104-
105-
return;
106-
}
84+
$finishReason = data_get($data, 'done', false) ? FinishReason::Stop : FinishReason::Unknown;
10785

10886
yield new Chunk(
109-
text: '',
110-
finishReason: $finishReason,
87+
text: $content,
88+
finishReason: $finishReason !== FinishReason::Unknown ? $finishReason : null
11189
);
112-
113-
return;
11490
}
11591
}
11692

@@ -221,15 +197,6 @@ protected function hasToolCalls(array $data): bool
221197
return false;
222198
}
223199

224-
/**
225-
* @param array<string, mixed> $data
226-
*/
227-
protected function hasSearchGrounding(array $data): bool
228-
{
229-
return ! empty(data_get($data, 'candidates.0.content.parts.0.text.groundingSupport'))
230-
&& ! empty(data_get($data, 'groundingChunks'));
231-
}
232-
233200
/**
234201
* @param array<string, mixed> $data
235202
*/

0 commit comments

Comments
 (0)