Skip to main content

Design Editor AI Image Generation Integration

Last updated: 2026-01-07

This document describes the AI image generation feature integration in the Design Editor storefront component.


Overview

The AI image generation feature allows customers to generate unique design base images using AI. The feature:

  • Requires customer authentication
  • Uses badge preferences (style, color, body type, etc.) to guide generation
  • Supports preview and commit modes
  • Integrates seamlessly with the existing design editor workflow

Architecture

Backend (jyt)

┌─────────────────────────────────────────────────────────────────┐
│ API Layer │
│ src/api/store/ai/imagegen/route.ts │
│ - POST /store/ai/imagegen │
│ - Requires customer authentication │
│ - Validates badges, reference images, mode │
└──────────────────────────┬──────────────────────────────────────┘


┌─────────────────────────────────────────────────────────────────┐
│ Medusa Application Workflow │
│ src/workflows/ai/generate-design-image.ts │
│ - generateDesignAiImageWorkflow │
│ - Steps: invokeMastra → uploadImage → updateDesign │
└──────────────────────────┬──────────────────────────────────────┘


┌─────────────────────────────────────────────────────────────────┐
│ Mastra AI Workflow │
│ src/mastra/workflows/imagegen/index.ts │
│ - imageGenerationWorkflow │
│ - Steps: buildPrompt → checkQuota → generateImage │
│ - Uses @ai-sdk/mistral (Pixtral model) │
└─────────────────────────────────────────────────────────────────┘

Storefront (jyt-storefront)

┌─────────────────────────────────────────────────────────────────┐
│ Data Layer │
│ src/lib/data/ai-imagegen.ts │
│ - generateAiImage() server action │
│ - convertBadgesToApiFormat() utility │
└──────────────────────────┬──────────────────────────────────────┘


┌─────────────────────────────────────────────────────────────────┐
│ Hook Module │
│ .../hooks/modules/use-ai-generation.ts │
│ - useAiGeneration() hook │
│ - Manages: loading, error, auth prompt, quota │
└──────────────────────────┬──────────────────────────────────────┘


┌─────────────────────────────────────────────────────────────────┐
│ Main Design Editor Hook │
│ .../hooks/use-design-editor.ts │
│ - Integrates useAiGeneration module │
│ - Exposes AI state and methods │
└──────────────────────────┬──────────────────────────────────────┘

┌──────────────┼──────────────┐
▼ ▼ ▼
┌───────────────┐ ┌───────────────┐ ┌───────────────┐
│ EditorSidebar │ │ EditorCanvas │ │ AiLoginPrompt │
│ - AI section │ │ - AI overlay │ │ - Auth modal │
└───────────────┘ └───────────────┘ └───────────────┘

Files Reference

Backend Files

FilePurpose
src/api/store/ai/imagegen/route.tsPOST endpoint for AI generation
src/api/store/ai/imagegen/validators.tsZod validation schemas
src/api/middlewares.tsAuth middleware registration
src/workflows/ai/generate-design-image.tsMedusa orchestration workflow
src/mastra/workflows/imagegen/index.tsMastra AI workflow (3 steps)
src/mastra/index.tsWorkflow registration

Storefront Files

FilePurpose
src/lib/data/ai-imagegen.tsServer action for API calls
.../hooks/modules/use-ai-generation.tsAI generation hook module
.../hooks/use-design-editor.tsMain hook integration
.../components/ai-login-prompt.tsxLogin prompt modal
.../components/editor-sidebar.tsxAI section in sidebar
.../components/editor-canvas.tsxAI generation overlay
.../index.tsxMain editor component

Authentication Flow

User clicks "Generate with AI"


┌─────────────────┐
│ Check customer │
│ prop from SSR │
└────────┬────────┘

┌───────┴───────┐
│ │
▼ ▼
customer=null customer exists
│ │
▼ ▼
Show Login Call API
Modal │
│ ▼
▼ Show Loading
Click Login Overlay
│ │
▼ ▼
Save draft On Success:
to localStorage Update canvas
│ │
▼ ▼
Redirect to Set generated
/account base URL


(User logs in)


Return to editor


Draft restored
automatically

Component Integration

EditorSidebar Props

// AI Generation props (optional, for backwards compatibility)
isGeneratingAi?: boolean
aiGenerationError?: string | null
quotaRemaining?: number | null
onGenerateAi?: () => void
onClearAiError?: () => void

EditorCanvas Props

// AI Generation
isGeneratingAi?: boolean

useAiGeneration Hook

type UseAiGenerationArgs = {
customer: CustomerInfo | null
countryCode?: string
badgePreferences: BadgePreferences
design: DesignState
setDesign: React.Dispatch<React.SetStateAction<DesignState>>
persistDraftSnapshot: () => void
onBaseImageGenerated?: (url: string) => void
}

type UseAiGenerationResult = {
// State
isGeneratingAi: boolean
aiGenerationError: string | null
showLoginPrompt: boolean
lastAiGeneration: AiGenerationMetadata | null
quotaRemaining: number | null

// Methods
generateAiBase: (mode?: "preview" | "commit") => Promise<void>
dismissLoginPrompt: () => void
handleLoginRedirect: () => void
clearAiError: () => void
}

API Request/Response

Request

POST /store/ai/imagegen
Content-Type: application/json
Authorization: Bearer <jwt_token>

{
"mode": "preview" | "commit",
"badges": {
"style": "minimal",
"color_family": "Earth, Pastels",
"body_type": "athletic",
"embellishment_level": "balanced",
"occasion": "Daily, Workwear"
},
"materials_prompt": "Silk with subtle texture",
"canvas_snapshot": {
"width": 1024,
"height": 1024,
"layers": [...]
}
}

Response

{
"generation": {
"mode": "preview",
"preview_url": "https://...",
"media_id": "media_123", // only in commit mode
"prompt_used": "A minimalist fashion design...",
"badges": {...},
"materials_prompt": "...",
"generated_at": "2026-01-07T...",
"quota_remaining": 45
}
}

Error Handling

ErrorCodeUser Message
Not authenticatedAUTH_REQUIREDShows login prompt modal
Quota exceededQUOTA_EXCEEDED"You've reached your daily AI generation limit"
Generation failedGeneric"Failed to generate AI image. Please try again."

UI Components

AI Generation Button (Sidebar)

  • Location: After "Canvas Tools" section
  • Styling: Purple/blue gradient
  • States: Default, Loading (spinner), Disabled
  • Shows quota remaining when available

AI Loading Overlay (Canvas)

  • Full canvas overlay with blur
  • Animated sparkle icon with orbiting dots
  • Gradient purple/blue theme
  • Message: "Generating with AI" / "Creating your unique design base..."

Login Prompt Modal

  • Shows benefits of signing in
  • "Sign in to continue" primary button
  • "Maybe later" secondary button
  • Note: "Your design will be saved and restored after signing in"

Mobile Support

  • AI tab in bottom toolbar (Sparkles icon)
  • Swipe-up sheet with generate button
  • Same functionality as desktop

Future Enhancements

  1. Actual Image Generation Service: Replace placeholder in generateImageStep with real service (Replicate SDXL/Flux, DALL-E, or FAL.ai)

  2. Reference Image Support: Allow users to upload reference images for style guidance

  3. Preview Comparison: Side-by-side comparison of existing vs AI-generated base

  4. Regenerate with Variations: Generate multiple variations to choose from

  5. Quota Management: Implement actual Redis/DB-based quota tracking per customer