13
13
use Prism \Prism \Enums \Provider ;
14
14
use Prism \Prism \Exceptions \PrismChunkDecodeException ;
15
15
use Prism \Prism \Exceptions \PrismException ;
16
- use Prism \Prism \Providers \Gemini \Concerns \ExtractSearchGroundings ;
17
- use Prism \Prism \Providers \Gemini \Concerns \ValidatesResponse ;
18
16
use Prism \Prism \Providers \Gemini \Maps \FinishReasonMap ;
19
17
use Prism \Prism \Providers \Gemini \Maps \MessageMap ;
20
18
use Prism \Prism \Providers \Gemini \Maps \ToolChoiceMap ;
29
27
30
28
class Stream
31
29
{
32
- use CallsTools, ExtractSearchGroundings, ValidatesResponse ;
30
+ use CallsTools;
33
31
34
32
public function __construct (
35
33
protected PendingRequest $ client ,
@@ -58,8 +56,6 @@ protected function processStream(Response $response, Request $request, int $dept
58
56
59
57
$ text = '' ;
60
58
$ toolCalls = [];
61
- $ groundingSupports = [];
62
- $ groundingChunks = [];
63
59
64
60
while (! $ response ->getBody ()->eof ()) {
65
61
$ data = $ this ->parseNextDataLine ($ response ->getBody ());
@@ -73,44 +69,24 @@ protected function processStream(Response $response, Request $request, int $dept
73
69
if ($ this ->hasToolCalls ($ data )) {
74
70
$ toolCalls = $ this ->extractToolCalls ($ data , $ toolCalls );
75
71
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
+ }
78
76
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 ;
83
78
}
84
79
85
80
// Handle content
86
81
$ 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 ;
101
83
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;
107
85
108
86
yield new Chunk (
109
- text: '' ,
110
- finishReason: $ finishReason,
87
+ text: $ content ,
88
+ finishReason: $ finishReason !== FinishReason::Unknown ? $ finishReason : null
111
89
);
112
-
113
- return ;
114
90
}
115
91
}
116
92
@@ -221,15 +197,6 @@ protected function hasToolCalls(array $data): bool
221
197
return false ;
222
198
}
223
199
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
-
233
200
/**
234
201
* @param array<string, mixed> $data
235
202
*/
0 commit comments