Skip to content

CATROID-1664, CATROID-1666 First AI tutor integration steps#5218

Open
spipau wants to merge 41 commits into
dev-ai-tutorfrom
explore-ai-tutor-integration
Open

CATROID-1664, CATROID-1666 First AI tutor integration steps#5218
spipau wants to merge 41 commits into
dev-ai-tutorfrom
explore-ai-tutor-integration

Conversation

@spipau

@spipau spipau commented Jun 3, 2026

Copy link
Copy Markdown

Summary

First integration of the Catrobat AI Tutor into Catroid. This PR enables Jetpack Compose, wires in the AI Tutor SDK, and adds the AI Assist overlay in SpriteActivity together with the supporting serialization, validation, error handling, and a
brick-level diff screen for reviewing AI-suggested sprite changes. It also carries the OkHttp/Okio and Lifecycle API updates that the SDK requires.

Base branch: dev-ai-tutor
Jira: CATROID-XXXX

Build and dependencies

  • Apply the Kotlin Compose compiler plugin and enable the Compose build feature.
  • Add Compose UI, runtime, foundation, material3, and UI tooling (preview) dependencies (v1.7.5).
  • Add the Kotzilla Maven repository (for Koin-Embedded) and the AI Tutor dependency.
  • Enable FEATURE_AI_ASSIST_ENABLED for the all and debug build variants.

API updates required by the AI Tutor SDK

  • Migrate OkHttp/Okio usages to Kotlin properties and extension functions (for example Response.body() to Response.body).
  • Switch CameraManager to LifecycleRegistry (rename lifecycle to lifecycleRegistry), update the LiveData extension, and update all references.

Serialization and validation

  • Add Sprite to/from XML string methods in XstreamSerializer (getXmlAsStringFromSprite, getSpriteFromXmlString), using the existing loadSaveLock for thread safety and handling parse errors.
  • Add AiTutorSpriteValidator to validate AI-generated sprite XML before it is applied.

AI Assist overlay

  • Add a Compose-based AI Assist overlay hosted full-screen in activity_sprite.xml (FrameLayout root, compose_ai_overlay), managed by AiAssistOverlayHelper with AiAssistOverlayCallbacks.
  • Handle overlay show/hide, the hardware back button, and disposal of Compose compositions on hide in SpriteActivity.
  • Hide the AI Assist button in the Looks and Sounds tabs when the feature flag is enabled.
  • Integrate AiTutorSpriteValidator in ScriptFragment so AI-generated sprites are validated as renderable before being applied.

AI Tutor diff screen

  • Add AiTutorDiffScreen with brick-level diffing via an LCS algorithm (ADDED, REMOVED, MODIFIED, UNCHANGED), implemented in SpriteDiffer with styling in DiffStatusStyle.
  • Add BrickDiffDialog showing native brick prototype views (via getPrototypeView in an AndroidView) for before/after inspection.
  • Add Compose previews for the "Changes" and "No changes" states, plus the ic_minus and ic_edit_pencil drawables.

Error handling and structure

  • Add AiTutorErrorDialog for surfacing AI response errors.
  • Organize AI Assist code into overlay, validation, error, and diff subpackages.

Note for reviewers (building locally)

This branch depends on the AI Tutor library (org.catrobat:aitutor), which is resolved from your Maven Local repository. To build, you first need to publish it locally:

  1. Clone the AI Tutor repo and check out the integration/pocket-code branch: https://github.com/Catrobat/catrobat-ai-tutor/tree/integration/pocket-code
  2. Publish it to Maven Local: ./gradlew publishToMavenLocal
  3. Build

@harissabil - ping me on Slack if the local install gives you any trouble and I'll walk you through it.

Your checklist for this pull request

Please review the contributing guidelines and wiki pages of this repository.

  • Include the name of the Jira ticket in the PR’s title
  • Include a summary of the changes plus the relevant context
  • Choose the proper base branch (develop)
  • Confirm that the changes follow the project’s coding guidelines
  • Verify that the changes generate no compiler or linter warnings
  • Perform a self-review of the changes
  • Verify to commit no other files than the intentionally changed ones
  • Include reasonable and readable tests verifying the added or changed behavior
  • Confirm that new and existing unit tests pass locally
  • Check that the commits’ message style matches the project’s guideline
  • Stick to the project’s gitflow workflow
  • Verify that your changes do not have any conflicts with the base branch
  • After the PR, verify that all CI checks have passed
  • Post a message in the catroid-stage or catroid-ide Slack channel and ask for a code reviewer

harissabil added 26 commits May 18, 2026 18:57
* Implement `getFormulaString` in `Formula` to generate a visual tree structure of the formula
* Add `getFlattenedAllListsString` to retrieve a trimmed extern formula string with flattened user lists
* Add `getLeftChild` and `getRightChild` getters to `FormulaElement`
* Add `getAllList` to generate a tree-like string representation of the project structure (Scenes, Sprites, Scripts, Bricks, and Formulas)
* Add `getAllListAsJson` and `getAllListAsJsonString` to provide a structured JSON representation of the project
* Include debug logging for the generated project export strings
… as strings and JSON

* Add `getAllListForSprite` and `getAllListForScene` to generate tree-like text representations of scripts and bricks
* Add `getAllListAsJsonStringForSprite` and `getAllListAsJsonStringForScene` for JSON-formatted component hierarchies
* Add `getProjectSummary` to provide a high-level overview of project statistics, including scene counts, sprite counts, and global variables/lists
* Introduce `getXmlAsStringFromSprite(Sprite)` to allow serializing individual Sprite objects to XML strings.
* Ensure thread safety by using the existing `loadSaveLock`.
* Add strings for AI Assist format options including Tree, JSON, XML, and Summary
* Implement `handleAiAssistButton` in `SpriteActivity` and `ProjectActivity` to display a format selection dialog
* Serialize project, scene, or sprite data based on user selection using `ProjectManager` and `XstreamSerializer`
* Navigate to `AiAssistFragment` with the generated structure string
* Hide options menu items in `SpriteActivity` when `AiAssistFragment` is active
* Add Kotzilla Maven repository for Koin-Embedded
* Apply Kotlin Compose compiler plugin
* Enable Compose build feature in the catroid module
* Add Catrobat AI Tutor dependency
* Add Compose UI and Runtime dependencies (v1.7.5)
- Update `fragment_ai_assist.xml` to include a `ComposeView` and wrap existing content in a `FrameLayout`
- Initialize `AiTutorView` within `AiAssistFragment` using Jetpack Compose
- Pass the project structure context from fragment arguments to the AI Tutor component
- Configure `ViewCompositionStrategy` to manage the ComposeView lifecycle correctly
- Implement `Formula.getFormulaInlineString()` to generate human-readable mathematical expressions from formula trees
- Enhance project summaries to include script triggers (e.g., broadcast messages, conditions, start events)
- Include variable names and broadcast messages in brick descriptions within tree and JSON outputs
- Handle duplicate script and brick types in JSON serialization by appending index suffixes (e.g., `Formula_2`)
- Update `GsonBuilder` to disable HTML escaping for better readability of project exports
- Add helper methods in `ProjectManager` to extract metadata from scripts and bricks for debugging purposes
- Remove `AiAssistFragment` and its associated layout `fragment_ai_assist.xml`
- Delete `handleAiAssistButton` and project serialization logic from `ProjectActivity`
- Replace the local `compose_ai_tutor_preview` in `fragment_script.xml` with a global `compose_ai_overlay` in `activity_sprite.xml`
- Update `activity_sprite.xml` to use a `FrameLayout` root to host the AI Tutor flow as a full-screen overlay
- Simplify `fragment_script.xml` by removing redundant nested layouts and the deleted Compose view components
- Replace basic script-level diffing with a granular brick-level comparison using a Longest Common Subsequence (LCS) algorithm
- Implement `buildDiffRows` to align original and AI-generated bricks, supporting ADDED, REMOVED, MODIFIED, and UNCHANGED statuses
- Flatten scripts into ordered lists to include script headers, nested bricks, and markers in the comparison view
- Reconcile adjacent remove/add operations of the same brick type into a single "MODIFIED" state for better readability
- Update `AiTutorDiffScreen` UI with a summary header, color-coded legend, and humanized brick names and field labels
- Improve visual styling using project-specific theme colors, rounded corners, and formatted subtitles for formula changes
- Add defensive error handling and logging for script flattening and formula string extraction
- Remove the now-obsolete `ScriptDiff` data class and `AiTutorPreviewHelper` object
- Replace `AiAssistFragment` with a Compose-based overlay managed by `AiAssistOverlayHelper` in `SpriteActivity`
- Update `SpriteActivity` to handle overlay visibility and hardware back button behavior
- Remove deprecated AI Tutor preview logic and fragment result listeners from `ScriptFragment`
- Integrate `AiTutorSpriteValidator` in `ScriptFragment` to ensure AI-generated sprites are renderable before application
- Clean up options menu handling logic in `SpriteActivity` previously required for the AI Assist fragment
- Dispose of Compose compositions when hiding the AI overlay to manage resources correctly

@github-advanced-security github-advanced-security AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

detekt found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

@github-advanced-security github-advanced-security AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Checkstyle found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

- Organize AI Assist classes into specialized sub-packages: `overlay`, `validation`, `error`, and `diff`
- Update `AiAssistOverlay` to use `getSpriteFromXmlString` for clipboard processing and update related imports
- Enhance `AiTutorErrorDialog` by applying a `RoundedCornerShape` to the `AlertDialog`
- Relocate `AiTutorSpriteValidator`, `AiAssistOverlay`, `AiAssistOverlayCallbacks`, and `AiTutorErrorDialog` to their respective new packages
- Move `AiTutorDiffScreen` to a dedicated `.diff` subpackage and decompose logic into specialized helper files
- Implement a GitHub-style diff layout using status stripes, leading icons, and row tinting for better change visualization
- Add `BrickDiffDialog` to allow detailed inspection of "Before" and "After" states for individual bricks
- Introduce `DiffToken` logic to highlight specific changed values within brick subtitles using `AnnotatedString` and bold styling
- Improve script header rendering by resolving real string resources via `BrickBaseType` instead of class name humanization
- Update `brickSignature` to include variable and list selections, ensuring data-only changes are correctly identified as MODIFIED
- Extract diffing algorithms to `SpriteDiffer.kt` and styling constants to `DiffStatusStyle.kt` for better maintainability
- Replace the dual-column comparison with a more natural single-column flow for MODIFIED rows, showing a clear "Old → New" value transition
…nges

- Implement a responsive `ModifiedValues` composable that switches from a horizontal to a vertical stacked layout for long text changes
- Use `TextMeasurer` and `onSizeChanged` to dynamically calculate text width and determine the optimal layout for modified brick values
- Replace the static legend `Row` with a `FlowRow` to better handle screen width constraints
- Update `LegendChip` to include status-specific icons and improved styling for the unchanged state
- Add visual borders and refine padding for unchanged brick rows to improve visual distinction
- Add vertical spacing and a `Spacer` before script headers for better grouping in the diff list
- Introduce Compose previews for "Changes" and "No changes" states using sample sprite data
… parameter naming

- Add `onClick` support to `ScriptHeaderRow` to allow selecting script headers in the diff view
- Refactor `AiTutorDiffScreen` and its sub-composables to use named arguments for improved readability
- Update `ScriptHeaderRow` to include a `clickable` modifier and standardized styling
- Remove unused `context` parameter from the `BrickDiffDialog` call
- Standardize parameter usage across `StatusContainer`, `BrickContent`, and `SummaryAndLegend` components
- Replace manual brick field rendering in `BrickDiffDialog` with an `AndroidView` that leverages existing `getPrototypeView` logic for a more accurate representation
- Simplify `DialogSection` by removing redundant parameters such as `oldForDiff`, `accent`, and `context`
- Add error handling and logging for prototype view generation to provide a fallback `TextView` if the view cannot be created
- Refine UI layout and spacing by adjusting modifiers, padding, and alignment within the dialog components
- Remove the dependency on `brickValueTokens` within the dialog, favoring the native brick view instead
@spipau spipau changed the title First AI tutor integration steps [WIP] First AI tutor integration steps Jun 17, 2026
@reichli reichli marked this pull request as draft June 17, 2026 15:22
- Remove logic in `onResume` that checks for the existence of the undo XML file to update the undo menu item visibility in `SpriteActivity`
- Wrap the technical reason display logic in `AiTutorErrorDialog` with a `BuildConfig.DEBUG` check to prevent showing internal error details in production environments.
- Improve readability in `SpriteDiffer` by renaming LCS variables and adding comments explaining the alignment algorithm
- Remove redundant error logging and defensive `try-catch` block from `SpriteDiffer.flatten`
- Reorganize `BrickTextFormatter` into logical sections (Titles, Value Text, Helpers) for better maintainability
- Add KDoc documentation to internal utility functions to clarify their purpose within the diffing process
- Replace manual subtitle generation with a view-traversal approach to reconstruct the full brick editor phrase in its natural UI order
- Update `DiffToken` to distinguish between static labels and dynamic values (formulas and spinner selections)
- Implement specialized styling in the diff screen to highlight modified dynamic values with bold and underline attributes
- Enhance data extraction using reflection to automatically include names from any `Nameable` fields, such as looks, sounds, or scenes
- Update `BrickDiffDialog` to display the full reconstructed editor phrase as the dialog title
- Add a fallback layout inflation mechanism to `BrickTextFormatter` to ensure phrases can be generated even if full view population fails
@sonarqubecloud

Copy link
Copy Markdown

@harissabil harissabil changed the title [WIP] First AI tutor integration steps CATROID-1664, CATROID-1666 First AI tutor integration steps Jun 30, 2026
@harissabil harissabil requested a review from reichli June 30, 2026 08:41
@harissabil harissabil marked this pull request as ready for review June 30, 2026 08:41
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.

3 participants