Skip to content

Conversation

@kamja44
Copy link
Contributor

@kamja44 kamja44 commented Jul 21, 2025

Refactors part-related helper functions into a new partUtils.ts module and adds comprehensive unit tests.

Extracted partToString and getResponseText from summarizer.ts into packages/core/src/utils/partUtils.ts. This improves code organization and reusability.

Added a new test file partUtils.test.ts with thorough unit tests for both functions, covering various cases including invalid inputs and edge cases.

Renamed the module from partHelpers.ts to partUtils.ts to align with the project's naming conventions.

TLDR
This PR extracts partToString and getResponseText into a new partUtils.ts module and adds dedicated unit tests for these utility functions. This improves code organization, reusability, and test coverage.

Dive Deeper
Previously, partToString and getResponseText were co-located within summarizer.ts. While functional, this placement was not ideal for reusability and clear separation of concerns. This PR moves these general-purpose utility functions into their own dedicated module, partUtils.ts, under packages/core/src/utils/.

Furthermore, comprehensive unit tests have been added in partUtils.test.ts to ensure the correctness and robustness of these functions. The tests cover various scenarios, including:

Handling null and undefined inputs for partToString.

Correctly concatenating string and object parts.

Extracting text from GenerateContentResponse objects, including cases with no candidates, no content, or mixed part types.

Adherence to the project's Array linting rule for array types in the test mock.

The module was initially named partHelpers.ts but was renamed to partUtils.ts to maintain consistency with existing utility file naming conventions within the project.

Testing Matrix

🍏 🪟 🐧
npm run
npx
Docker
Podman - -
Seatbelt - -

Linked issues / bugs

Refactors part-related helper functions into a new  module and adds comprehensive unit tests.
- Extracted  and  from  into . This improves code organization and reusability.
- Added a new test file  with thorough unit tests for both functions, covering various cases including invalid inputs and edge cases.
- Renamed the module from  to  to align with the project's naming conventions.
@kamja44 kamja44 requested a review from a team as a code owner July 21, 2025 04:55
Copy link
Contributor

@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.

Summary of Changes

Hello @kamja44, 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 significantly improves the codebase's organization and testability by refactoring common utility functions related to content parts into their own module. This move not only enhances code reusability but also ensures the reliability of these critical functionalities through the addition of dedicated and thorough unit tests.

Highlights

  • Code Organization & Refactoring: I've extracted the partToString and getResponseText helper functions from summarizer.ts into a new, dedicated module: packages/core/src/utils/partUtils.ts. This improves separation of concerns and enhances reusability of these core utility functions.
  • Improved Test Coverage: Comprehensive unit tests have been added for both partToString and getResponseText in a new file, packages/core/src/utils/partUtils.test.ts. These tests cover various scenarios, including invalid inputs, edge cases, and different content part types, ensuring the robustness of these utilities.
  • Naming Convention Alignment: The new utility module was initially named partHelpers.ts but was renamed to partUtils.ts to maintain consistency with existing naming conventions within the project.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

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 issue 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 is currently in preview and 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 to provide feedback.

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
Contributor

@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 effectively refactors part-related utility functions into a new partUtils.ts module, accompanied by a solid set of unit tests. This is a great improvement for code organization and reusability.

My main feedback is on the implementation of getResponseText, where I've identified a correctness issue related to how text parts are filtered and a behavioral inconsistency in its return values. I've provided a suggestion to make the function more robust and predictable.

Overall, this is a valuable change, and with the suggested fix, it will be even better.

@gemini-cli gemini-cli bot added kind/bug priority/p1 Important and should be addressed in the near term. area/platform Issues related to Build infra, Release mgmt, Testing, Eval infra, Capacity, Quota mgmt priority/p3 Backlog - a good idea but not currently a priority. area/core Issues related to User Interface, OS Support, Core Functionality and removed priority/p1 Important and should be addressed in the near term. labels Jul 21, 2025

import { GenerateContentResponse, PartListUnion } from '@google/genai';

export function partToString(part: PartListUnion): string {
Copy link
Contributor

Choose a reason for hiding this comment

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

there is a somewhat similar helper method in
geminiRequest.ts

export function partListUnionToString(value: PartListUnion): string {

Would it be feasible to unify these two similar util methods?
Ideally geminiRequest.ts could just call the partToString method from partsUtil.ts

Copy link
Contributor

Choose a reason for hiding this comment

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

That's an excellent observation, @jacob314! Unifying similar utility methods across the codebase, especially by leveraging the newly introduced partToString in partUtils.ts, would indeed further enhance code reusability and maintainability.

@kamja44, this is a valuable suggestion for a follow-up improvement. If it's a straightforward change, you might consider incorporating it into this PR, or creating a separate issue to track this refactoring in geminiRequest.ts.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @jacob314 ,

Thank you for your valuable feedback and for suggesting the unification of the partToString and partListUnionToString methods. I've implemented the changes accordingly, and I believe this results in a cleaner and more robust solution.

Here's a summary of the changes:

  1. Unified partToString Function:
    The partToString function in packages/core/src/utils/partUtils.ts has been enhanced to support both pure text extraction and detailed summary generation. It now accepts an optional options parameter with a verbose flag:

    • When verbose: true is passed, the function returns human-readable summary strings (e.g., [Video Metadata], [Function Call: myFunction]), integrating the behavior previously found in partListUnionToString.
    • When verbose is false or omitted, it preserves the original behavior of extracting only the plain text.
  2. geminiRequest.ts Wrapper:
    The partListUnionToString function in packages/core/src/core/geminiRequest.ts now acts as a thin wrapper that simply delegates to partToString(value, { verbose: true }), preserving existing behavior and API compatibility.

  3. Consolidated Tests:
    All unit tests related to part string conversion have been consolidated into partUtils.test.ts, covering both default and verbose modes. The redundant geminiRequest.test.ts file has been removed.

This approach centralizes the logic, improves reusability, and maintains compatibility with existing interfaces—all while aligning with project conventions.

Please let me know if you have any questions or further suggestions.

Thanks again!

kamja44 and others added 3 commits July 22, 2025 15:19
Consolidates part-related string conversion logic into a single partToString function in partUtils.ts, introducing a verbose option for detailed output.

Unified Conversion Logic: The partToString function in packages/core/src/utils/partUtils.ts now handles both pure text extraction (default) and detailed summary generation (when verbose: true is passed). This integrates the logic previously found in geminiRequest.ts's partListUnionToString.

Wrapper in geminiRequest.ts: packages/core/src/core/geminiRequest.ts's partListUnionToString is now a thin wrapper that calls partUtils.partToString with verbose: true, maintaining its original API and behavior.

Consolidated Tests: All tests for part string conversion, including those previously in geminiRequest.test.ts, have been moved and expanded within packages/core/src/utils/partUtils.test.ts. This file now thoroughly tests partToString in both its default and verbose modes.

Removed Redundant Test File: packages/core/src/core/geminiRequest.test.ts has been deleted as its tests are now covered by partUtils.test.ts.
Copy link
Contributor

@jacob314 jacob314 left a comment

Choose a reason for hiding this comment

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

lgtm

Good idea adding a verbose option to enable supporting this as a refactor that didn't change any logic!

@jacob314 jacob314 added this pull request to the merge queue Jul 22, 2025
Merged via the queue into google-gemini:main with commit a00f1bb Jul 23, 2025
8 checks passed
thacio added a commit to thacio/auditaria that referenced this pull request Jul 23, 2025
skeshive pushed a commit that referenced this pull request Jul 23, 2025
Co-authored-by: Jacob Richman <jacob314@gmail.com>
Aisha630 pushed a commit to Aisha630/gemini-cli that referenced this pull request Jul 24, 2025
Co-authored-by: Jacob Richman <jacob314@gmail.com>
jkcinouye pushed a commit that referenced this pull request Jul 25, 2025
Co-authored-by: Jacob Richman <jacob314@gmail.com>
JunYang-tes pushed a commit to JunYang-tes/gemini-cli.nvim that referenced this pull request Aug 9, 2025
Co-authored-by: Jacob Richman <jacob314@gmail.com>
involvex pushed a commit to involvex/gemini-cli that referenced this pull request Sep 11, 2025
Co-authored-by: Jacob Richman <jacob314@gmail.com>
reconsumeralization pushed a commit to reconsumeralization/gemini-cli that referenced this pull request Sep 19, 2025
Co-authored-by: Jacob Richman <jacob314@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Issues related to User Interface, OS Support, Core Functionality area/platform Issues related to Build infra, Release mgmt, Testing, Eval infra, Capacity, Quota mgmt priority/p3 Backlog - a good idea but not currently a priority.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor: Extract Part Utilities and Add Unit Tests

2 participants