fix(vertexai): update history getter to reflect google_generative_ai updates #13040
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem Description
There are two
ChatSession
classes: one in the Flutterfire Vertex AI package and one in the google_generative_ai package. Each of these classes maintains a private_history
list to store the content that has been sent to or received from the generative model. Both classes also provide ahistory
getter that returns this list to the clients. However, an issue arose because thesendMessage
method in the FlutterfireChatSession
class relies on thesendMessage
method from the google_generative_ai.ChatSession class to update the chat history. Consequently, only the_history
list in the google_generative_ai.ChatSession class was being updated, while the_history
list in the FlutterfireChatSession
class remained outdated. This discrepancy caused thehistory
getter in the FlutterfireChatSession
class to always return an empty list as the chat session history, leading to inconsistencies and potential errors in the application. Exactly the same issue can be observed with thesendMessageStream
methods defined in each class.The video below demonstrates this issue. In the example app, a
Text
widget has been added to the bottom of the screen to display the total length of the chat session history using thehistory
getter from the FlutterfireChatSession
class. As shown in the video, the count remains at zero, even when messages are present and visible in the chat session. This indicates that thehistory
getter in the FlutterfireChatSession
class is not being updated correctly with the latest messages, highlighting the need for the proposed fix.Simulator.Screen.Recording.-.iPhone.15.Pro.-.2024-07-04.at.12.57.58.mp4
Solution Description
To resolve this issue, the
ChatSession
class in the Flutterfire plugin was updated so that thehistory
getter returns the history from the google_generative_ai package, which is the one being actively updated when new messages are added to the chat. This change ensures that thehistory
returned to clients accurately reflects the current state of the chat session.Here are the specific changes made to the
ChatSession
class:_history
member and its initialization in the constructor were removed, as it was no longer necessary to maintain a separate history list in the FlutterfireChatSession
class.initialHistory
parameter, which is used to initialize the chat session in thestartChat
method call.history
getter was updated to fetch the history from the google_ai.ChatSession instance. This ensures that the history returned by the FlutterfireChatSession
class is always up-to-date and consistent with the state of the chat session.The video below demonstrates the proposed solution for the
ChatSession
class in the Flutterfire package. In the example app, aText
widget at the bottom of the screen displays the total length of the chat session history using the updatedhistory
getter from the Flutterfire ChatSession class. As new messages are added to the chat, theText
widget correctly updates to reflect the current length of the chat history. This confirms that thehistory
getter is now accurately reporting the chat history, validating the effectiveness of the proposed fix.Simulator.Screen.Recording.-.iPhone.15.Pro.-.2024-07-04.at.14.12.43.mp4
Alternate Solution Description
An alternate solution to the issue would involve updating the
sendMessage
andsendMessageStream
methods in the FlutterfireChatSession
class to ensure that the_history
list within the Flutterfire class is updated whenever these methods are called. This approach would maintain the original functionality of thehistory
getter in the FlutterfireChatSession
class and keep the class constructor unchanged. However, it would result in maintaining two versions of the same chat history: one in the FlutterfireChatSession
class and one in the google_generative_ai.ChatSession class.Related Issues
none found
Checklist
Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes (
[x]
).This will ensure a smooth and quick review process. Updating the
pubspec.yaml
and changelogs is not required.///
).melos run analyze
) does not report any problems on my PR.Breaking Change
Does your PR require plugin users to manually update their apps to accommodate your change?