Skip to content

Stream Gemini Summary Response#142

Merged
trett merged 8 commits intomainfrom
stream-reposponse
Feb 15, 2026
Merged

Stream Gemini Summary Response#142
trett merged 8 commits intomainfrom
stream-reposponse

Conversation

@trett
Copy link
Owner

@trett trett commented Feb 12, 2026

This PR introduces streaming summary generation using Gemini's API.

Key Changes:

  • Backend: Refactored to stream content using Gemini's streaming API.
  • API: Updated to return Server-Sent Events (SSE).
  • Frontend: Updated to consume the SSE stream and update the UI incrementally.
  • Dependencies: Added for streaming JSON parsing.

This change significantly improves the user experience by displaying the summary as it is generated, reducing the perceived waiting time.

@gemini-code-assist
Copy link

Summary of Changes

Hello @trett, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a fundamental shift in how AI summaries are delivered to the user, moving from a batch-processing model to a real-time streaming approach. By leveraging Gemini's streaming capabilities and Server-Sent Events, the system can now progressively render summary content, metadata, and fun facts, providing a more dynamic and responsive user interface. This enhancement aims to make the summary generation process feel faster and more interactive for the end-user.

Highlights

  • Streaming Summary Generation: The application now generates summaries using Gemini's streaming API, allowing for incremental display of content.
  • API Update to Server-Sent Events (SSE): The backend API endpoint for summaries has been updated to return Server-Sent Events, enabling real-time updates to the client.
  • Frontend Integration of SSE: The frontend has been modified to consume the SSE stream, updating the UI incrementally as summary content is received.
  • Dependency Addition: The circe-fs2 library was added to handle streaming JSON parsing on the server side.
  • Improved User Experience: This change significantly reduces perceived waiting time by displaying summaries as they are generated, enhancing the user experience.
Changelog
  • build.sbt
    • Updated the project version to 2.4.3.
    • Added io.circe:circe-fs2 dependency for streaming JSON parsing.
  • client/src/main/scala/client/Models.scala
    • Added Decoder instances for various SummaryEvent types (Content, Metadata, FunFact, Error, Done) to handle streamed data.
  • client/src/main/scala/client/NetworkUtils.scala
    • Imported SummaryEvent.
    • Introduced a new streamSummary function that establishes an EventSource connection to receive Server-Sent Events and emits them via an EventBus.
  • client/src/main/scala/client/SummaryPage.scala
    • Removed imports for SummaryResponse, SummarySuccess, SummaryError and Try.
    • Replaced the batch-fetching fetchSummaryBatch and batchObserver with startStreaming and cleanup functions.
    • Implemented logic to manage EventSource subscriptions and incrementally update the UI based on SummaryEvent types (Content, Metadata, FunFact, Error, Done).
  • scripts/local-docker/docker-compose.yml
    • Updated the server image version to server:2.4.3.
  • server/src/main/scala/ru/trett/rss/server/codecs/SummaryCodecs.scala
    • Imported SummaryEvent.
    • Added Encoder instances for SummaryEvent types (Content, Metadata, FunFact, Error, Done) to serialize them for SSE.
  • server/src/main/scala/ru/trett/rss/server/controllers/SummarizeController.scala
    • Replaced CirceEntityEncoder import with ServerSentEvent and io.circe.syntax.
    • Modified the /api/summarize endpoint to return a Stream[IO, ServerSentEvent] by mapping SummaryEvent instances to ServerSentEvent.
  • server/src/main/scala/ru/trett/rss/server/services/SummarizeService.scala
    • Imported fs2.Stream.
    • Updated imports to use SummaryEvent instead of SummaryResponse, SummaryResult, SummarySuccess, and SummaryError.
    • Modified getEndpoint to include a stream parameter, allowing selection between generateContent and streamGenerateContent.
    • Added a stream parameter to buildGeminiRequest to enable streaming requests.
    • Replaced the getSummary method with streamSummary, which now returns an fs2.Stream[IO, SummaryEvent].
    • Refactored the summary generation logic within streamSummary to emit SummaryEvent instances (Metadata, FunFact, Content, Error, Done) as a stream.
    • Changed summarize to summarizeStream, which now builds a streaming Gemini request and processes the response body as a stream of GeminiResponse objects, mapping them to SummaryEvent.Content.
  • shared/src/main/scala/ru/trett/rss/models/SummaryEvent.scala
    • Added a new file defining the SummaryEvent sealed trait and its case classes: Content, Metadata, FunFact, Error, and Done.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request is a significant improvement, refactoring the summary generation to use a streaming approach with Gemini's API and Server-Sent Events. The changes on the backend, API, and frontend are well-executed and will greatly enhance the user experience by providing incremental updates. I've identified a couple of areas for improvement in the backend service to increase robustness, particularly around error handling and response parsing in the new streaming logic.

@trett
Copy link
Owner Author

trett commented Feb 14, 2026

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request successfully refactors summary generation to a streaming model using Gemini's API and Server-Sent Events, significantly enhancing the user experience by displaying summaries incrementally. However, it introduces security vulnerabilities related to LLM input/output handling. Specifically, the direct concatenation of untrusted RSS feed content into prompts allows for prompt injection, and the unsafe rendering of LLM-generated HTML on the client side leads to Cross-Site Scripting (XSS). These security concerns should be addressed by implementing client-side HTML sanitization and more secure prompt construction techniques. Additionally, there are suggestions to improve error handling and response parsing on the backend.

Comment on lines 207 to 213
.map { geminiResp =>
geminiResp.candidates.headOption
.flatMap(_.content.parts.flatMap(_.headOption))
.map(_.text)
.map { text =>
if text.startsWith("```html") then
text.stripPrefix("```html").stripSuffix("```").trim
else text.trim
} match
case Some(html) if html.nonEmpty => SummarySuccess(html)
case _ =>
SummaryError("Could not extract summary from response.")
.map(_.text.replace("```html", "").replace("```", ""))
.getOrElse("")
}
.map(SummaryEvent.Content(_))

Choose a reason for hiding this comment

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

medium

Using .replace() to strip markdown fences is a bit too broad and could corrupt the generated HTML if the fence characters appear within the content itself. The previous, non-streaming implementation's use of stripPrefix and stripSuffix is safer as it only targets the beginning and end of the string.

Additionally, filtering out empty content chunks would be a good practice to avoid sending unnecessary events to the client.

                            .map { geminiResp =>
                                geminiResp.candidates.headOption
                                    .flatMap(_.content.parts.flatMap(_.headOption))
                                    .map(_.text)
                                    .getOrElse("")
                            }
                            .map { text =>
                                if (text.startsWith("```html")) {
                                    text.stripPrefix("```html").stripSuffix("```").trim
                                } else {
                                    text.trim
                                }
                            }
                            .filter(_.nonEmpty)
                            .map(SummaryEvent.Content(_))

@trett
Copy link
Owner Author

trett commented Feb 14, 2026

Applied the suggested fix for markdown stripping and empty chunk filtering.

@trett
Copy link
Owner Author

trett commented Feb 14, 2026

I have addressed the unresolved comment regarding markdown stripping and empty chunk filtering. The changes have been pushed to the branch.

@trett
Copy link
Owner Author

trett commented Feb 14, 2026

Ran scalafix and scalafmt. Refactored SummaryPage.scala to remove mutable state and improve stream lifecycle management.

…EventSource

- Replace onMountCallback with EventStream.merge to guarantee initial request in SummaryPage
- Use ListBuffer in NetworkUtils to properly track and close EventSource connections
- Update AGENTS.md to enforce scalafix/scalafmt usage
@trett trett force-pushed the stream-reposponse branch from fa9f38f to 13c48e4 Compare February 14, 2026 20:07
@trett trett merged commit c0eff9b into main Feb 15, 2026
1 check passed
@trett trett deleted the stream-reposponse branch February 15, 2026 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant