(
streamResponse: AsyncGenerator<GenerateContentResponse>,
inputContent: Content,
)
| 451 | } |
| 452 | |
| 453 | private async *processStreamResponse( |
| 454 | streamResponse: AsyncGenerator<GenerateContentResponse>, |
| 455 | inputContent: Content, |
| 456 | ) { |
| 457 | const outputContent: Content[] = []; |
| 458 | const chunks: GenerateContentResponse[] = []; |
| 459 | let errorOccurred = false; |
| 460 | |
| 461 | try { |
| 462 | for await (const chunk of streamResponse) { |
| 463 | if (isValidResponse(chunk)) { |
| 464 | chunks.push(chunk); |
| 465 | const content = chunk.candidates?.[0]?.content; |
| 466 | if (content !== undefined) { |
| 467 | if (this.isThoughtContent(content)) { |
| 468 | yield chunk; |
| 469 | continue; |
| 470 | } |
| 471 | outputContent.push(content); |
| 472 | } |
| 473 | } |
| 474 | yield chunk; |
| 475 | } |
| 476 | } catch (error) { |
| 477 | errorOccurred = true; |
| 478 | throw error; |
| 479 | } |
| 480 | |
| 481 | if (!errorOccurred) { |
| 482 | const allParts: Part[] = []; |
| 483 | for (const content of outputContent) { |
| 484 | if (content.parts) { |
| 485 | allParts.push(...content.parts); |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | this.recordHistory(inputContent, outputContent); |
| 490 | } |
| 491 | |
| 492 | private recordHistory( |
| 493 | userInput: Content, |
no test coverage detected