Spaces:
Sleeping
Sleeping
Design System Extractor v2 β Master Context File
Upload this file to refresh Claude's context when continuing work on this project.
π― Project Goal
Build a semi-automated, human-in-the-loop agentic system that:
- Reverse-engineers a design system from a live website
- Reconstructs and upgrades it into a modern, scalable design system
- Outputs production-ready JSON tokens
Philosophy: This is a design-aware co-pilot, NOT a magic button. Humans decide, agents propose.
ποΈ Architecture Overview
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β TECH STACK β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Frontend: Gradio (long-scroll, sectioned UI with live preview) β
β Orchestration: LangGraph (agent state management & workflow) β
β Models: HuggingFace Inference API (see model assignments below) β
β Hosting: Hugging Face Spaces β
β Storage: HF Spaces persistent storage β
β Output: Platform-agnostic JSON tokens β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
π§ Model Assignments
Stage 2: Multi-Agent Analysis (NEW!)
| Agent | Role | Model | Provider | Cost |
|---|---|---|---|---|
| LLM 1 | Design Analyst 1 | Qwen/Qwen2.5-72B-Instruct |
Novita | $0.29/M in, $0.59/M out |
| LLM 2 | Design Analyst 2 | meta-llama/Llama-3.3-70B-Instruct |
Novita | $0.59/M in, $0.79/M out |
| HEAD | Compiler | meta-llama/Llama-3.3-70B-Instruct |
Novita | $0.59/M in, $0.79/M out |
| Rules | Calculations | None (Rule-based) | β | FREE |
Architecture:
LLM 1 (Qwen) ββ
βββ> HEAD (Llama) ββ> Final Recommendations
LLM 2 (Llama) ββ
Rules ββββββββββ
LLM 1 and LLM 2 run in PARALLEL via LangGraph, then HEAD compiles results.
Other Agents
| Agent | Role | Model | Provider | Why |
|---|---|---|---|---|
| Agent 1 | Crawler & Extractor | None (Rule-based) | β | Pure CSS extraction, no LLM needed |
| Agent 2 | Normalizer | microsoft/Phi-3.5-mini-instruct |
Novita | Fast, great structured output |
| Agent 4 | Generator | mistralai/Codestral-22B-v0.1 |
Novita | Code specialist, JSON formatting |
Provider Configuration
Default provider: Novita (configurable in config/agents.yaml)
Available providers (via HuggingFace Inference Providers):
- novita - Default, good balance
- groq - Fastest
- cerebras - Ultra-fast
- sambanova - Good for Llama
- together - Wide model selection
Cost Tracking
Estimated cost per Stage 2 analysis: ~$0.05
- Free tier: $0.10/month
- Pro tier: $2.00/month ($9/mo subscription)
π€ Agent Personas
Agent 1: Website Crawler & Extractor
- Persona: Meticulous Design Archaeologist
- Tool: Playwright
- Job:
- Auto-discover 10+ pages from base URL
- Crawl Desktop (1440px) + Mobile (375px) separately
- Scroll to bottom + wait for network idle
- Extract: colors, typography, spacing, radius, shadows
- Output: Raw tokens with frequency, context, confidence
Agent 2: Token Normalizer & Structurer
- Persona: Design System Librarian
- Job:
- Clean noisy extraction, dedupe
- Infer naming patterns
- Tag tokens as:
detected|inferred|low-confidence
- Output: Structured token sets with metadata
Agent 3: Design System Best Practices Advisor
- Persona: Senior Staff Design Systems Architect
- Job:
- Research modern DS patterns (Material, Polaris, Carbon, etc.)
- Propose upgrade OPTIONS (not decisions)
- Suggest: type scales (3 options), spacing (8px), color ramps (AA compliant), naming conventions
- Output: Option sets with rationale
Agent 4: Plugin & JSON Generator
- Persona: Automation Engineer
- Job:
- Convert finalized tokens to Figma-compatible JSON
- Generate: typography, color (with tints/shades), spacing variables
- Maintain Desktop + Mobile + version metadata
- Output: Production-ready JSON
π₯οΈ UI Stages (3 Stages)
Stage 1: Extraction Review
- Purpose: Trust building
- Shows: Token tables, color swatches, font previews, confidence indicators
- Human Actions: Accept/reject tokens, flag anomalies, toggle DesktopβMobile
Stage 2: Upgrade Playground (MOST IMPORTANT)
- Purpose: Decision-making through live visuals
- Shows: Side-by-side option selector + live preview
- Human Actions: Select type scale A/B/C, spacing system, color ramps β preview updates instantly
Stage 3: Final Review & Export
- Purpose: Confidence before export
- Shows: Token preview, JSON tree, diff view (original vs final)
- Human Actions: Download JSON, save version, label version
π Project Structure
design-system-extractor/
βββ app.py # Gradio main entry point
βββ requirements.txt
βββ README.md
β
βββ config/
β βββ .env.example # Environment variables template
β βββ agents.yaml # Agent personas & configurations
β βββ settings.py # Application settings
β
βββ agents/
β βββ __init__.py
β βββ state.py # LangGraph state definitions
β βββ graph.py # LangGraph workflow orchestration
β βββ crawler.py # Agent 1: Website crawler
β βββ extractor.py # Agent 1: Token extraction
β βββ normalizer.py # Agent 2: Token normalization
β βββ advisor.py # Agent 3: Best practices
β βββ generator.py # Agent 4: JSON generator
β
βββ core/
β βββ __init__.py
β βββ browser.py # Playwright browser management
β βββ css_parser.py # CSS/computed style extraction
β βββ color_utils.py # Color analysis, contrast, ramps
β βββ typography_utils.py # Type scale detection & generation
β βββ spacing_utils.py # Spacing pattern detection
β βββ token_schema.py # Token data structures (Pydantic)
β
βββ ui/
β βββ __init__.py
β βββ components.py # Reusable Gradio components
β βββ stage1_extraction.py # Stage 1 UI
β βββ stage2_upgrade.py # Stage 2 UI
β βββ stage3_export.py # Stage 3 UI
β βββ preview_generator.py # HTML preview generation
β
βββ templates/
β βββ preview.html # Live preview base template
β βββ specimen.html # Design system specimen template
β
βββ storage/
β βββ persistence.py # HF Spaces storage management
β
βββ tests/
β βββ test_crawler.py
β βββ test_extractor.py
β βββ test_normalizer.py
β
βββ docs/
βββ CONTEXT.md # THIS FILE - upload for context refresh
βββ API.md # API documentation
π§ Key Technical Decisions
| Decision | Choice | Rationale |
|---|---|---|
| Viewports | Fixed 1440px + 375px | Simplicity, covers main use cases |
| Scrolling | Bottom + network idle | Captures lazy-loaded content |
| Infinite scroll | Skip | Avoid complexity |
| Modals | Manual trigger | User decides what to capture |
| Color ramps | 5-10 shades, AA compliant | Industry standard |
| Type scales | 3 options (1.25, 1.333, 1.414) | User selects |
| Spacing | 8px base system | Modern standard |
| ML models | Minimal, rule-based preferred | Simplicity, reliability |
| Versioning | HF Spaces persistent storage | Built-in, free |
| Preview | Gradio + iframe (best for dynamic) | Smooth updates |
π Token Schema (Core Data Structures)
class TokenSource(Enum):
DETECTED = "detected" # Directly found in CSS
INFERRED = "inferred" # Derived from patterns
UPGRADED = "upgraded" # User-selected improvement
class Confidence(Enum):
HIGH = "high" # 10+ occurrences
MEDIUM = "medium" # 3-9 occurrences
LOW = "low" # 1-2 occurrences
class Viewport(Enum):
DESKTOP = "desktop" # 1440px
MOBILE = "mobile" # 375px
Token Types:
- ColorToken: value, frequency, contexts, elements, contrast ratios
- TypographyToken: family, size, weight, line-height, elements
- SpacingToken: value, frequency, contexts, fits_base_8
- RadiusToken: value, frequency, elements
- ShadowToken: value, frequency, elements
π LangGraph Workflow
βββββββββββββββ
β START β
ββββββββ¬βββββββ
β
βΌ
βββββββββββββββ
β URL Input β
ββββββββ¬βββββββ
β
βΌ
ββββββββββββββββββββββββββ
β Agent 1: Discover β
β (find pages) β
βββββββββββββ¬βββββββββββββ
β
βΌ
ββββββββββββββββββββββββββ
β HUMAN: Confirm pages βββββ Checkpoint 1
βββββββββββββ¬βββββββββββββ
β
βΌ
ββββββββββββββββββββββββββ
β Agent 1: Extract β
β (crawl & extract) β
βββββββββββββ¬βββββββββββββ
β
βΌ
ββββββββββββββββββββββββββ
β Agent 2: Normalize β
βββββββββββββ¬βββββββββββββ
β
βΌ
ββββββββββββββββββββββββββ
β HUMAN: Review tokens βββββ Checkpoint 2 (Stage 1 UI)
βββββββββββββ¬βββββββββββββ
β
βββββββββββββββββ΄ββββββββββββββββ
β β
βΌ βΌ
ββββββββββββββββββββ ββββββββββββββββββββ
β Agent 3: Advise β β (parallel) β
β (best practices) β β β
ββββββββββ¬ββββββββββ ββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββββββββ
β HUMAN: Select options βββββ Checkpoint 3 (Stage 2 UI)
βββββββββββββ¬βββββββββββββ
β
βΌ
ββββββββββββββββββββββββββ
β Agent 4: Generate β
β (final JSON) β
βββββββββββββ¬βββββββββββββ
β
βΌ
ββββββββββββββββββββββββββ
β HUMAN: Export βββββ Checkpoint 4 (Stage 3 UI)
βββββββββββββ¬βββββββββββββ
β
βΌ
βββββββββββ
β END β
βββββββββββ
π¦ Human-in-the-Loop Rules
- No irreversible automation
- Agents propose β Humans decide
- Every auto action must be:
- Visible
- Reversible
- Previewed
π¦ Output JSON Format
{
"metadata": {
"source_url": "https://example.com",
"extracted_at": "2025-01-23T10:00:00Z",
"version": "v1-recovered",
"viewport": "desktop"
},
"colors": {
"primary": {
"50": { "value": "#e6f2ff", "source": "upgraded" },
"500": { "value": "#007bff", "source": "detected" },
"900": { "value": "#001a33", "source": "upgraded" }
}
},
"typography": {
"heading-xl": {
"fontFamily": "Inter",
"fontSize": "32px",
"fontWeight": 700,
"lineHeight": "1.2",
"source": "detected"
}
},
"spacing": {
"xs": { "value": "4px", "source": "upgraded" },
"sm": { "value": "8px", "source": "detected" },
"md": { "value": "16px", "source": "detected" }
}
}
π οΈ Implementation Phases
Phase 1 (Current)
- Project structure
- Configuration files
- Token schema (Pydantic models)
- Agent 1: Crawler
- Agent 1: Extractor
- Agent 2: Normalizer
- Stage 1 UI
- LangGraph basic workflow
Phase 2
- Agent 3: Advisor
- Stage 2 UI (Upgrade Playground)
- Live preview system
Phase 3
- Agent 4: Generator
- Stage 3 UI
- Export functionality
Phase 4
- Full LangGraph orchestration
- HF Spaces deployment
- Persistent storage
π Environment Variables
# Required
HF_TOKEN=your_huggingface_token
# Model Configuration (defaults shown β diverse providers)
AGENT2_MODEL=microsoft/Phi-3.5-mini-instruct # Microsoft - Fast naming
AGENT3_MODEL=meta-llama/Llama-3.1-70B-Instruct # Meta - Strong reasoning
AGENT4_MODEL=mistralai/Codestral-22B-v0.1 # Mistral - Code/JSON
# Optional
DEBUG=true
LOG_LEVEL=INFO
π Notes for Claude
When continuing this project:
- Check current phase in Implementation Phases section
- Review agent personas in agents.yaml for consistent behavior
- Follow token schema defined in core/token_schema.py
- Maintain LangGraph state consistency across agents
- Use Gradio components from ui/components.py for consistency
- Test with real websites before deployment
Last updated: 2025-01-23