Luca448 commited on
Commit
5fb3fb6
·
1 Parent(s): b873791

Savestate: Add design spec for Nano Banana 2 integration

Browse files
docs/superpowers/specs/2026-07-22-nano-banana-2-integration-design.md ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Nano Banana 2 (Gemini 3.1 Flash Image) Integration Design
2
+
3
+ ## 1. Goal
4
+ Integrate "Nano Banana 2" (Google's Gemini 3.1 Flash Image model) into SchoolMind AI to automatically generate and display high-quality images during chats when visual explanation provides significant added value. Add intuitive editing capabilities (conversational and touch-based inpainting) for these images.
5
+
6
+ ## 2. Architecture & Flow
7
+
8
+ ### 2.1 Intent Classification & Generation
9
+ 1. **Interceptor:** Before sending the user's prompt to the main LLM, an Intent-Classifier (e.g., Gemini 2.5 Flash) quickly analyzes the prompt: *"Does this require a visual image to explain properly, or did the user explicitly request an image?"*
10
+ 2. **Parallel Trigger:** If YES, the classifier returns a short image prompt (e.g., "A cross-section of a plant cell").
11
+ 3. **Backend Processing:** The backend immediately triggers a background request to the Nano Banana 2 API with this image prompt.
12
+ 4. **Prompt Injection:** The main LLM (regardless of which model the user selected) receives a system instruction: *"An image about '[Image Prompt]' is being generated. You must place the exact placeholder `[BILD: [Image Prompt]]` at the most appropriate semantic position in your response where this visual aid should appear. CRITICAL RULE: The image placeholder MUST NOT be placed at the very beginning of the text. Provide introductory text first."*
13
+
14
+ ### 2.2 Frontend Display (Streaming & Placeholders)
15
+ 1. **Parsing:** The `chat-view.js` markdown parser or streaming logic listens for the `[BILD: ...]` tag.
16
+ 2. **Skeleton Loader:** When the tag is encountered, it is replaced by an iOS-style Skeleton Loader (a pulsing placeholder rectangle matching the design language of the app).
17
+ 3. **Image Swap:** Once the Nano Banana 2 API returns the generated image URL (Base64 or server-hosted path), the backend streams an event to the frontend, which swaps the Skeleton Loader with the final, high-resolution image.
18
+
19
+ ### 2.3 Image Editing
20
+ The user can edit the generated image in two ways:
21
+ 1. **Conversational Editing:**
22
+ - The Intent-Classifier checks if a new user prompt is an edit request for the previous image (e.g., "make the sky bluer").
23
+ - If YES, the backend calls the Nano Banana 2 API with the previous image ID and the new prompt.
24
+ 2. **Visual Masking (Touch Inpainting):**
25
+ - Every generated image in the chat has an "Edit" (Bearbeiten) button overlay.
26
+ - Clicking it opens a fullscreen Canvas overlay (similar to Artifacts/Presentations).
27
+ - The user can paint a mask over a specific area (e.g., the cell nucleus) with their finger/mouse and type a modification prompt.
28
+ - The base image, alpha mask, and text prompt are sent to Nano Banana 2 for precise inpainting.
29
+
30
+ ## 3. Constraints & Edge Cases
31
+ - **No leading images:** As requested, images must always be preceded by some introductory text. The system prompt enforces this.
32
+ - **Model Agnostic:** The architecture ensures the feature works even if the user selects a non-Gemini model for text generation (e.g., DeepSeek, Ollama), because the Intent Classifier and Image Generator run independently via Google AI Studio keys.
33
+ - **Fail Gracefully:** If image generation fails (timeout, content filter), the skeleton loader fades out or shows a subtle error icon without breaking the text flow.
34
+
35
+ ## 4. Required Components to Modify/Create
36
+ - `server/ai-router.js`: Add Image Intent Classifier logic and Prompt Injection.
37
+ - `server.js` or new `server/image-service.js`: API integration for Nano Banana 2 (generate and edit/inpainting).
38
+ - `src/views/chat/chat-view.js`: Parsing for `[BILD: ...]` and rendering the Skeleton/Final image.
39
+ - `src/views/chat/image-editor.js` (NEW): Fullscreen Canvas UI for drawing masks and triggering edits.
40
+ - `src/styles/chat.css` or `learning.css`: Styles for the image container, edit button, and skeleton loader.