Skip to content

feat(ai): Implement session resumption and context window compression for live api#9795

Open
hsubox76 wants to merge 5 commits intomainfrom
ch-session-resumption
Open

feat(ai): Implement session resumption and context window compression for live api#9795
hsubox76 wants to merge 5 commits intomainfrom
ch-session-resumption

Conversation

@hsubox76
Copy link
Copy Markdown
Contributor

Based on #9624

Moved connection initiation logic from LiveGenerativeModel.connect() into the class so that the LiveSession.resumeSession() method can more easily access it. Since the LiveGenerativeModel.connect() method needs to wait for the connection to be opened successfully, and a constructor can't be async, the class provides a connectionPromise property for external code to tell when the connection has finished opening.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Mar 31, 2026

🦋 Changeset detected

Latest commit: d397d01

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

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 adds support for session resumption and context window compression in Live sessions. Key changes include the implementation of LiveSession.resumeSession(), the addition of SessionResumptionConfig and ContextWindowCompressionConfig interfaces, and updated server message handling for resumption updates. The review feedback identifies a logic error in the session connection handshake where the wrong setup message was transmitted and private state was incorrectly mutated due to a shallow copy. A leftover debugging log was also identified for removal.

Comment thread packages/ai/src/methods/live-session.ts Outdated
Comment thread packages/ai/src/methods/live-session.ts
@hsubox76 hsubox76 marked this pull request as ready for review April 21, 2026 20:23
@hsubox76 hsubox76 requested review from a team as code owners April 21, 2026 20:23
}

// @beta
export interface SlidingWindow {
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.

I had to lookup what a Sliding Window is within this context.

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.

me too.

Could we possibly provide a brief description of sliding window here? (or wherever you think it makes the most sense so that folks see the description when they need to understand this concept)

I found this description: https://ai.google.dev/api/live#slidingwindow. But let's not link to here, just pull the important info out of the description.

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.

Added.

}

// @beta
export interface SlidingWindow {
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.

me too.

Could we possibly provide a brief description of sliding window here? (or wherever you think it makes the most sense so that folks see the description when they need to understand this concept)

I found this description: https://ai.google.dev/api/live#slidingwindow. But let's not link to here, just pull the important info out of the description.

Comment thread docs-devsite/ai.livesession.md Outdated
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
>

Allows external code to await the opening of the websocket connection.
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 - let's be consistent throughout with the capitalization of "Websocket". It's an official name of a protocol, so WDYT about capitalizing it throughout?

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, hopefully caught them all.

Comment thread docs-devsite/ai.slidingwindow.md Outdated

| Property | Type | Description |
| --- | --- | --- |
| [targetTokens](./ai.slidingwindow.md#slidingwindowtargettokens) | number | <b><i>(Public Preview)</i></b> The session reduction target, i.e., how many tokens we should keep. |
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.

for this instance of i.e. -- do you mean "for example" or "that is"?

We avoid latin abbreviations like e.g. and i.e. b/c it's unclear which is meant. Could this instance (and any others throughout) pls be changed to standard English phrases?

  • e.g., => for example, OR like OR such as OR for instance,
  • i.e., => specifically OR that is

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.

I went through and think I got all the public facing ones.

Comment thread docs-devsite/ai.slidingwindow.md Outdated

| Property | Type | Description |
| --- | --- | --- |
| [targetTokens](./ai.slidingwindow.md#slidingwindowtargettokens) | number | <b><i>(Public Preview)</i></b> The session reduction target, i.e., how many tokens we should keep. |
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.

"we" -- who is "we" referring to here?

Can we say something like "the model" here?

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.

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