Skip to content

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.

Comment on lines 208 to +216
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.")
text.stripPrefix("```html").stripSuffix("```")
else text
}
.getOrElse("")

Choose a reason for hiding this comment

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

high

The current logic for stripping html` and from the response is not robust for a streaming context. It incorrectly assumes that the start and end markers will be contained within the same text chunk from the stream. This is unlikely to be true for a streaming response and can lead to markdown artifacts (like a dangling ````) being sent to the client.

A simpler and more robust approach that works per-chunk is to replace these markers wherever they appear.

                                geminiResp.candidates.headOption
                                    .flatMap(_.content.parts.flatMap(_.headOption))
                                    .map(_.text.replace("```html", "").replace("```", ""))
                                    .getOrElse("")

Comment on lines +226 to +228
.drain ++ Stream.emit(
SummaryEvent.Error(s"API error: ${response.status.reason}")
)

Choose a reason for hiding this comment

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

medium

In this error path for a non-successful API response, a SummaryEvent.Error is emitted, but not a SummaryEvent.Done. While the caller streamSummary currently appends a Done event, this makes summarizeStream less self-contained and reliant on its call site for correct termination signaling. To improve robustness and encapsulation, summarizeStream should be responsible for emitting a Done event after an Error in all its error paths.

                            .drain ++ Stream.emits(Seq(SummaryEvent.Error(s"API error: ${response.status.reason}"), SummaryEvent.Done))

Stream
.eval(logger.error(error)(s"Error summarizing text: ${error.getMessage}"))
.drain ++
Stream.emit(SummaryEvent.Error(errorMessage))

Choose a reason for hiding this comment

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

medium

Similar to the error handling for non-2xx responses, this handleErrorWith block should also emit a SummaryEvent.Done after the SummaryEvent.Error. This makes the function more self-contained and ensures that clients always receive a termination event, even when errors occur within this stream.

                    Stream.emits(Seq(SummaryEvent.Error(errorMessage), SummaryEvent.Done))

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