Skip to content

Conversation

jacobsimionato
Copy link
Collaborator

No description provided.

@jacobsimionato
Copy link
Collaborator Author

/gemini summarize

Copy link

@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 a significant refactoring by implementing SurfaceController to manage UI surfaces, which simplifies GenUiSurface into a stateless widget. This change also involves removing the SurfaceManager and the abstract AiClient, consolidating logic into GenUiManager and GeminiAiClient respectively. While the overall design is improved, I've found a few critical issues, including syntax errors and incorrect variable names in the example apps that will break them. Additionally, the removal of the retry logic for AI client network calls is a concern for application resilience, and one of the spikes is broken due to API changes.

Copy link

Summary of Changes

This pull request significantly refactors the UI management and AI client interaction within the flutter_genui package. The core change involves introducing a SurfaceController to manage the state and events of individual UI surfaces, which in turn allows the GenUiSurface widget to become a StatelessWidget. Concurrently, the responsibilities of the SurfaceManager have been absorbed directly into the GenUiManager, simplifying the overall architecture. The generic AiClient interface has been removed, with GeminiAiClient now being used directly and also simplified by removing its internal model switching and retry logic. These changes lead to a cleaner, more modular design, improving how UI state is managed and how AI models interact with and understand the application's user interface.

Highlights

  • Introduction of SurfaceController: A new SurfaceController class has been introduced to encapsulate the state and event handling for individual UI surfaces. This allows the GenUiSurface widget to be refactored into a StatelessWidget, promoting better separation of concerns and simplifying its implementation.
  • Consolidation of UI Management: The SurfaceManager class has been removed, and its responsibilities for managing UI surfaces and emitting updates have been fully integrated into the GenUiManager. This consolidation streamlines the UI management architecture within the flutter_genui package.
  • Simplification of AI Client Interaction: The generic AiClient interface has been removed, and the GeminiAiClient is now used directly. Furthermore, the GeminiAiClient itself has been simplified by removing internal model switching capabilities and retry logic, suggesting these concerns may be handled at a higher application level or are no longer needed within the client.
  • Enhanced AI-UI State Integration: An extension method has been added to GeminiAiClient that allows SurfaceController data to be converted into AI model Content. This deeper integration enables the AI to have a more direct understanding of the current UI state when generating responses.
  • Refactored UI Management Tools: The AddOrUpdateSurfaceTool and DeleteSurfaceTool now receive direct callbacks from GenUiManager for their operations, rather than depending on a SurfaceManager instance. This refactoring decouples the tools and aligns with the new consolidated UI management.
Changelog
  • examples/minimal_genui/lib/main.dart
    • Updated the call to _aiClient.generateText to explicitly pass the conversation using a named parameter.
  • examples/travel_app/integration_test/app_test.dart
    • Adjusted the aiClient variable name in the runApp call within integration tests.
  • examples/travel_app/lib/main.dart
    • Updated type hints from AiClient to GeminiAiClient for aiClient properties in TravelApp and TravelPlannerPage.
    • Modified the SurfaceAdded case to now include a controller parameter.
    • Refactored calls to generateContent and generateText to use named conversation parameters.
    • Removed the UI component for AI model selection from the AppBar actions.
  • examples/travel_app/test/main_test.dart
    • Removed the test case related to switching AI models, aligning with UI changes.
  • pkgs/flutter_genui/lib/flutter_genui.dart
    • Removed exports for the now-removed ai_client.dart and surface_manager.dart files.
  • pkgs/flutter_genui/lib/src/ai_client/ai_client.dart
    • This file has been removed.
  • pkgs/flutter_genui/lib/src/ai_client/gemini_ai_client.dart
    • Relocated AiModel and AiClientException definitions into this file.
    • Introduced a ContentExtension for SurfaceController to facilitate converting UI state into AI model content.
    • GeminiAiClient no longer implements the AiClient interface.
    • Simplified the GeminiAiClient constructor by removing parameters related to model management and retry logic.
    • Updated the signatures of generateContent and generateText to accept optional conversation and content parameters.
    • Removed internal retry logic, simplifying the _generate method.
  • pkgs/flutter_genui/lib/src/ai_client/gemini_content_converter.dart
    • Updated import statements to reflect the removal of ai_client.dart.
  • pkgs/flutter_genui/lib/src/core/genui_manager.dart
    • Absorbed the GenUiUpdate, SurfaceAdded, SurfaceUpdated, and SurfaceRemoved classes from the removed SurfaceManager.
    • Integrated the _surfaces map and _updates stream directly into GenUiManager.
    • Added a _controllers map to manage SurfaceController instances.
    • Refactored _addOrUpdateSurface to create and associate SurfaceController instances with new surfaces.
    • Modified getTools() to provide direct callbacks to AddOrUpdateSurfaceTool and DeleteSurfaceTool.
  • pkgs/flutter_genui/lib/src/core/surface_controller.dart
    • New file added, introducing the SurfaceController class to manage individual UI surface states and events.
  • pkgs/flutter_genui/lib/src/core/surface_manager.dart
    • This file has been removed, with its functionality migrated to GenUiManager.
  • pkgs/flutter_genui/lib/src/core/ui_tools.dart
    • Updated AddOrUpdateSurfaceTool and DeleteSurfaceTool constructors to accept direct callbacks and a Catalog instead of a SurfaceManager instance.
  • pkgs/flutter_genui/lib/src/facade/genui_surface.dart
    • Refactored from a StatefulWidget to a StatelessWidget.
    • Now directly accepts a SurfaceController instance, centralizing UI state and event handling.
    • Internal logic updated to leverage the SurfaceController for building widgets and dispatching events.
  • pkgs/flutter_genui/lib/src/facade/to_refactor/chat_widget.dart
    • Updated imports and modified the SurfaceAdded case to destructure the new controller parameter.
    • GenUiSurface now utilizes the SurfaceController for its functionality.
  • pkgs/flutter_genui/lib/src/facade/to_refactor/conversation_widget.dart
    • Updated imports and modified GenUiSurface to use the SurfaceController.
  • pkgs/flutter_genui/lib/test/fake_ai_client.dart
    • FakeAiClient now directly implements GeminiAiClient.
    • Added dummy implementations for properties removed from GeminiAiClient to maintain compatibility.
    • Updated generateContent and generateText signatures to match GeminiAiClient changes.
  • pkgs/flutter_genui/test/ai_client/ai_client_test.dart
    • Removed ai_client.dart import.
    • Updated generateContent calls to pass conversation as a named parameter.
    • Removed the test case for generateContent retries.
  • pkgs/flutter_genui/test/catalog/core_widgets_test.dart
    • Updated imports and test setup to use the new SurfaceController and tool invocation methods.
  • pkgs/flutter_genui/test/core/chat_controller_test.dart
    • Updated imports and test setup to use the new tool invocation methods for surface management.
  • pkgs/flutter_genui/test/core/conversation_widget_test.dart
    • Updated imports and test setup to use the new tool invocation methods for surface management.
  • pkgs/flutter_genui/test/core/genui_manager_test.dart
    • Updated imports and test setup to use the new tool invocation methods and assert on the presence of SurfaceController.
  • pkgs/flutter_genui/test/core/ui_tools_test.dart
    • New file added, containing unit tests for AddOrUpdateSurfaceTool and DeleteSurfaceTool's callback-based invocation.
  • pkgs/flutter_genui/test/dynamic_ui_test.dart
    • Updated test setup to use the new SurfaceController and tool invocation methods.
  • pkgs/spikes/catalog_gallery/lib/main.dart
    • Updated calls to _genUi.addOrUpdateSurface to reflect the consolidation of SurfaceManager functionality into GenUiManager.
Activity
  • jacobsimionato requested a summary of the pull request.

SurfaceController({required this.definitionNotifier, required this.catalog});

final ValueNotifier<UiDefinition?> definitionNotifier;
final Catalog catalog;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is the catalog meant to be per-surface? That might be hard to communicate to the generating LLM: I don't think we want to have to communicate the entire catalog with each request. Or is this just meant to be a reference to a common central catalog? I wonder if there's a way we could enforce that there's only one catalog per GenUIManager? Maybe have it owned by the GenUIManager instead, and pass one of those to the SurfaceController constructor?

try {
final response = await _aiClient.generateText(
final response = await _aiClient.generateText(conversation: _conversation);
List.of(_chatController.conversation.value),
Copy link
Collaborator

Choose a reason for hiding this comment

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

This looks like a syntax error?

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