Skip to content

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

Draft
hsubox76 wants to merge 2 commits intomainfrom
ch-session-resumption
Draft

feat(ai): Implement session resumption and context window compression for live api#9795
hsubox76 wants to merge 2 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: bbdcd1c

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

@github-actions
Copy link
Copy Markdown
Contributor

Vertex AI Mock Responses Check ⚠️

A newer major version of the mock responses for Vertex AI unit tests is available. update_vertexai_responses.sh should be updated to clone the latest version of the responses: v16.2

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 on lines +105 to +109
const setupMessage = { ...this._setupMessage };
if (sessionResumption) {
setupMessage.setup.sessionResumption = sessionResumption;
}
this._webSocketHandler.send(JSON.stringify(this._setupMessage));
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.

high

The current implementation prepares a local setupMessage variable but then sends this._setupMessage. Furthermore, the mutation at line 107 (setupMessage.setup.sessionResumption = sessionResumption) actually mutates the private this._setupMessage property because the spread at line 105 is only a shallow copy. It is better to avoid mutating the stored setup message and ensure the correctly prepared object is sent to the handler.

Suggested change
const setupMessage = { ...this._setupMessage };
if (sessionResumption) {
setupMessage.setup.sessionResumption = sessionResumption;
}
this._webSocketHandler.send(JSON.stringify(this._setupMessage));
const setupMessage: _LiveClientSetup = {
...this._setupMessage,
setup: {
...this._setupMessage.setup,
...(sessionResumption ? { sessionResumption } : {})
}
};
this._webSocketHandler.send(JSON.stringify(setupMessage));

Comment on lines +153 to +155
await this.close();
console.log('closed');
await this._connectSession(sessionResumption);
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.

medium

Remove the console.log statement used for debugging.

Suggested change
await this.close();
console.log('closed');
await this._connectSession(sessionResumption);
await this.close();
await this._connectSession(sessionResumption);

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.

2 participants