AI-Native RF Module Designer - Architecture¶
The PCB Stackup Generator uses a two-service architecture — a React frontend and a FastAPI backend communicating via REST/SSE — plus external managed services (Supabase, Gemini API):
┌─────────────────────────────────────────────────────────────┐
│ React Frontend (Vite/TypeScript) │
│ Port 5180 │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────────┐ │
│ │ Components │ │ Hooks │ │ Services │ │
│ │ (300+ UI) │ │ (State Mgmt)│ │ (Business Logic) │ │
│ └──────────────┘ └──────────────┘ └──────────────────┘ │
│ │ │
└────────────────────────────┼─────────────────────────────────┘
│ HTTP/REST + SSE
▼
┌─────────────────────┐ ┌─────────────────┐
│ FastAPI │ │ External │
│ Port 8000 │────▶│ Services │
│ │ │ │
│ • Gemini AI Chat │ │ • Supabase │
│ (agent loop, SSE) │ │ (auth + DB) │
│ • RF Projects │ │ • Gemini API │
│ • Components │ │ • Nexar API │
│ • Materials │ └─────────────────┘
│ • KiCad Export │
│ • ODB++/ADS/Gerber │
│ • IPC-2581/HFSS │
└─────────────────────┘
There is no separate Node.js backend — chat, knowledge/RAG, and all exports run inside the FastAPI service (see the note under Backend Architecture).
Frontend Architecture¶
Component Structure¶
frontend/src/
├── components/ # React UI components (300+ files)
│ ├── Chat/ # AI chat components
│ │ ├── EmptyState.tsx # Starter prompt cards (6 clickable, 2-col grid)
│ │ ├── ActionCard.tsx # Inline tool call result cards
│ │ └── MessageBubble.tsx # Chat messages with action card rendering
│ ├── Layout/
│ │ └── GeminiSidebar.tsx # Chat sidebar (auto-open, welcome)
│ └── ...
├── hooks/ # Custom React hooks (10+ files)
├── services/ # Business logic services (120+ files)
│ ├── chat/ # Chat services
│ │ ├── suggestionService.ts # Starter prompt suggestions
│ │ └── geminiToolCallHandler.ts # Tool call dispatch (incl. apply_design_template)
│ └── ...
├── contexts/ # React contexts (5 files)
├── utils/ # Utility functions (115+ files)
├── types/ # TypeScript definitions
└── constants/ # Application constants
Design Patterns¶
- Service Layer Pattern: Business logic extracted from components into services.
- Hook-Based State Management: Custom hooks encapsulate state logic.
- Context Splitting: Multiple focused contexts instead of one monolithic context.
- Barrel Exports: Centralized exports from index files.
Backend Architecture¶
FastAPI Backend (Port 8000)¶
backend/
├── main.py # FastAPI application entry point
├── routes/ # API route handlers (~33 modules; see CLAUDE.md for the domain grouping)
├── services/ # Business logic services (split into packages: solver/, compliance/, component_search/, rf_chain/, stackup_pdf/, surrogate/, test_coupon/, nexar/, pdf/, pptx_generator/)
├── tools/ # Agent-loop tool handlers (10 active tools + `run_solver` alias)
├── agents/ # AI selection agents (9 agents: 8 simple_*_agent + material_selection_agent, plus base_agent + scoring helpers)
├── agent_router_config.py # Component-type → agent routing table
├── utils/ # Utility modules (incl. `kicad/` package for symbol/schematic/pcb/stackup generators)
└── categorized_databases/ # Component JSON databases (~20 category dirs)
AI Chat (Gemini) is handled by the FastAPI backend at port 8000 — see
backend/services/gemini_chat_service.py, thegemini_chat_prompts/andagent_loop/packages (the loop itself isagent_loop/loop.py),agent_loop_sse.py, andtool_dispatcher.py. The agent-loop streams over SSE atPOST /api/chat/gemini/stream. There is no separate Node.js backend; legacypackage.jsonscripts referencingindex.jsare inactive.
Data Flow¶
RF Module Generation Flow¶
- User Input (Frontend)
- Component Selection (
frontend/src/services/rfChain/designService.ts+chainMapper/) - Power Flow Calculation (
frontend/src/services/rfChain/powerFlowCalculatorService.ts) - KiCad Project Generation (
backend/utils/kicad_export.py+backend/utils/kicad/) - ZIP Download (Frontend)
Stackup Generation Flow¶
- User Configuration (Frontend)
- Stackup Generation (stackupGenerator.ts)
- Validation (DfmValidator.ts)
- Export Options (KiCad, ADS, ODB++, Gerber, IPC-2581, HFSS, MagicON Internal, PDF)
AI Chat Onboarding Flow¶
- First visit → Chat sidebar auto-opens with welcome message
- User clicks starter prompt card or types question
- Gemini processes request with enhanced system prompt (design templates, phase context)
- Response rendered with inline ActionCards for tool calls
- Live agent: the AI-native shell (
/app/agent) streams a multi-turn tool-call loop (scripted demo mode removed; the public signed-out/demo/agentreplay is shipped —backend/routes/demo.py+frontend/src/components/Demo/, flagsFF_PUBLIC_DEMO/VITE_FF_PUBLIC_DEMO)
Performance Optimizations¶
- Request Deduplication: Prevents duplicate API calls.
- Database Preloading: Component database loads at server startup.
- Component Caching: Frontend and backend caching.
- Context Memoization: Split contexts reduce unnecessary re-renders.
Security¶
- HTTPS Enforcement: Production redirects to HTTPS.
- CORS Configuration: Environment-specific origins.
- Security Headers: HSTS, CSP, X-Frame-Options.
- Input Validation: Pydantic models for all API inputs.