@@ -117,7 +117,7 @@ async function getResponsePromise(
117
117
* GenerateContentResponse in each iteration.
118
118
* @ignore
119
119
*/
120
- export function getResponseStream (
120
+ function getResponseStream (
121
121
inputStream : ReadableStream < string >
122
122
) : ReadableStream < unknown > {
123
123
const reader = inputStream . getReader ( ) ;
@@ -170,8 +170,9 @@ export function getResponseStream(
170
170
* Aggregates an array of `GenerateContentResponse`s into a single
171
171
* GenerateContentResponse.
172
172
* @ignore
173
+ * @VisibleForTesting
173
174
*/
174
- function aggregateResponses (
175
+ export function aggregateResponses (
175
176
responses : GenerateContentResponse [ ]
176
177
) : GenerateContentResponse {
177
178
const lastResponse = responses [ responses . length - 1 ] ;
@@ -216,12 +217,19 @@ function aggregateResponses(
216
217
] . citationMetadata ! . citationSources . concat ( existingMetadata ) ;
217
218
}
218
219
}
219
- aggregatedResponse . candidates [ i ] . finishReason =
220
- response . candidates [ i ] . finishReason ;
221
- aggregatedResponse . candidates [ i ] . finishMessage =
222
- response . candidates [ i ] . finishMessage ;
223
- aggregatedResponse . candidates [ i ] . safetyRatings =
224
- response . candidates [ i ] . safetyRatings ;
220
+ const finishResonOfChunk = response . candidates [ i ] . finishReason ;
221
+ if ( finishResonOfChunk ) {
222
+ aggregatedResponse . candidates [ i ] . finishReason =
223
+ response . candidates [ i ] . finishReason ;
224
+ }
225
+ const finishMessageOfChunk = response . candidates [ i ] . finishMessage ;
226
+ if ( finishMessageOfChunk ) {
227
+ aggregatedResponse . candidates [ i ] . finishMessage = finishMessageOfChunk ;
228
+ }
229
+ const safetyRatingsOfChunk = response . candidates [ i ] . safetyRatings ;
230
+ if ( safetyRatingsOfChunk ) {
231
+ aggregatedResponse . candidates [ i ] . safetyRatings = safetyRatingsOfChunk ;
232
+ }
225
233
if ( 'parts' in response . candidates [ i ] . content ) {
226
234
for ( const part of response . candidates [ i ] . content . parts ) {
227
235
if ( part . text ) {
0 commit comments