Skip to content

feat(ai): Chat history and auto function calling for server templates#9763

Merged
hsubox76 merged 16 commits intomainfrom
ch-server-fc
Apr 8, 2026
Merged

feat(ai): Chat history and auto function calling for server templates#9763
hsubox76 merged 16 commits intomainfrom
ch-server-fc

Conversation

@hsubox76
Copy link
Copy Markdown
Contributor

@hsubox76 hsubox76 commented Mar 25, 2026

Based on initial work in #9742

API design doc (internal): https://docs.google.com/document/d/1ecqEdvMMkx1QSInhPVmqF9yOXSJaCgGlnxOc9GshcEY/edit?resourcekey=0-t1kgF5b2FKpRexQGQVc58Q&tab=t.qqsqzzhfm8vt

  • Created a new TemplateChatSession.
  • To avoid code duplication, extracted most of the functionality of ChatSession to a ChatSessionBase abstract class and allowed ChatSession and TemplateChatSession to extend it and add a few methods and types specific to their use cases.
  • Did not add integration test yet as the prod server won't be available until release, but tested against staging server manually.

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 25, 2026

🦋 Changeset detected

Latest commit: a0b3839

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@firebase/ai Minor
firebase Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 enhances the AI SDK by introducing robust chat history management and automatic function calling capabilities specifically for server-side templates. By refactoring core chat logic into a shared base class and providing a dedicated TemplateChatSession, it streamlines the development of interactive AI experiences that leverage predefined templates and external tools, making AI interactions more dynamic and stateful.

Highlights

  • Template Chat Sessions: Introduced TemplateChatSession to manage chat interactions with server-side AI templates, allowing for persistent chat history.
  • Automatic Function Calling for Templates: Enabled automatic function calling within TemplateChatSession, allowing the model to invoke predefined functions based on chat context.
  • Refactored Chat Logic: Created a new abstract base class, ChatSessionBase, to centralize common chat session logic, including history management and automatic function calling, which ChatSession and TemplateChatSession now extend.
  • New startChat() Method: Added a startChat() method to TemplateGenerativeModel for easily initiating a new TemplateChatSession.
  • Typo Correction: Corrected a typo from maxSequentalFunctionCalls to maxSequentialFunctionCalls in RequestOptions.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

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.

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
Copy Markdown
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 introduces TemplateChatSession and ChatSessionBase to enable template-based chat functionality and centralize common chat session logic. The TemplateGenerativeModel now supports starting a chat session via startChat(). The ChatSession class has been refactored to extend ChatSessionBase. New types like StartTemplateChatParams and TemplateGenerateContentRequest are added to support this feature, and a typo in maxSequentalFunctionCalls was corrected to maxSequentialFunctionCalls. Review feedback highlights a potential TypeError crash in ChatSessionBase when handling response.candidates[0].content if it's null or undefined. Additionally, ChatSessionBase should be exported for better extensibility, and public interfaces like StartTemplateChatParams and TemplateGenerateContentRequest require TSDoc comments for their properties. The _formatRequest method in TemplateChatSession can be made more type-safe, and the [key: string]: unknown; index signature in TemplateGenerateContentRequest should be reconsidered for a more type-safe approach.

Comment thread packages/ai/src/methods/chat-session-base.ts
Comment thread common/api-review/ai.api.md Outdated
Comment thread common/api-review/ai.api.md Outdated
Comment thread packages/ai/src/methods/template-chat-session.ts
Comment thread packages/ai/src/types/requests.ts
Comment thread packages/ai/src/types/requests.ts Outdated
@hsubox76 hsubox76 marked this pull request as ready for review March 30, 2026 16:43
@hsubox76 hsubox76 requested review from a team as code owners March 30, 2026 16:43
},
{
maxSequentalFunctionCalls: 0
maxSequentialFunctionCalls: 0
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍

Comment thread common/api-review/ai.api.md
Comment thread common/api-review/ai.api.md

| Method | Modifiers | Description |
| --- | --- | --- |
| [getHistory()](./ai.chatsession.md#chatsessiongethistory) | | Gets the chat history so far. Blocked prompts are not added to history. Neither blocked candidates nor the prompts that generated them are added to history. |
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Are there any changes that a dev needs to do within their code for regular chat (like non-server prompt template chat) to accommodate this change?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is a result of doing something that makes the code work better and makes the documentation messier - making ChatSession and TemplateChatSession extend a base class, ChatSessionBase, in order to avoid duplicate code. Unfortunately that means a method shared by both extended classes (getHistory) is only going to be found by clicking through to ChatSessionBase, which is linked above.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

But no changes for the developer in their actual source code for their app, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Nope, just harder to read the documentation.

Comment thread docs-devsite/ai.chatsessionbase.md Outdated
{% endcomment %}

# ChatSessionBase class
Base class for ChatSession that enables sending chat messages and stores history of sent and received messages so far.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit - ChatSession should be tagged as code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, also rewrote to reflect this is a base class for multiple ChatSession variants and not just literally ChatSession (it's only two right now but it could be more in the future).

Comment thread docs-devsite/ai.chatsessionbase.md Outdated
Comment thread docs-devsite/ai.chatsessionbase.md Outdated
Comment thread docs-devsite/ai.md Outdated
Comment thread docs-devsite/ai.md Outdated
Comment thread docs-devsite/ai.templatechatsession.md Outdated
Comment thread docs-devsite/ai.templategenerativemodel.md Outdated
@hsubox76 hsubox76 merged commit 9c8e864 into main Apr 8, 2026
29 checks passed
@hsubox76 hsubox76 deleted the ch-server-fc branch April 8, 2026 20:14
@google-oss-bot google-oss-bot mentioned this pull request Apr 8, 2026
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.

4 participants