7.6 KiB
7.6 KiB
page-agent AI Coding Instructions
Project Overview
This project has TWO SEPARATE PARTS:
- Core Library (
src/) - Pure JavaScript AI agent library that can be injected into any webpage - Marketing Website (
pages/) - React web app for landing page and documentation
Architecture & Tech Stack
Core Library (src/) - Pure JavaScript AI Agent
- Technology: Vanilla JavaScript/TypeScript (no React dependency)
- Purpose: AI agent with DOM automation capabilities
- Output: UMD/ES modules via
vite.lib.config.ts - Key Features: DOM processing, LLM integration, type-safe event bus
Marketing Website (pages/) - React Web App
- Technology: React 19 + TypeScript + Wouter routing + Tailwind CSS v4
- Purpose: Landing page and documentation site
- Output: Static website via
vite.config.ts - Usage: Hosted documentation and demos
Critical Development Workflows
Dual Build System - Two Separate Projects
npm start # React website (pages/) development
npm run build # Build BOTH library AND website
npm run build:lib # Pure JS library only (src/ → dist/lib/)
npm run build:lib:watch # Library development with auto-rebuild
Library Development (Pure JavaScript)
Working on the core AI agent (src/):
- Edit pure JavaScript/TypeScript files in
src/ - Use
npm run build:lib:watchfor development - Test via CDN injection:
<script src="dist/lib/page-agent.umd.js"></script> - No React dependencies allowed in
src/
Website Development (React)
Working on marketing/docs (pages/):
- Use
npm startfor React development server - Can import from
src/via@/alias to demo library features - Create test pages in
pages/test-pages/to validate library integration
DOM Pipeline
- DOM Extraction:
src/dom/converts live DOM toFlatDomTree - Dehydration: DOM tree → simplified text representation for LLM
- LLM Processing: Send text to AI model for action planning
- Indexed Operations: Map LLM responses back to specific DOM elements via indices
Event Bus Communication
Use src/utils/bus.ts instead of prop drilling for PageAgent ↔ UI communication:
// Emit events from PageAgent
getEventBus().emit('panel:show', undefined)
getEventBus().emit('panel:update', { status: 'thinking', message: 'Processing...' })
// Listen in UI components
getEventBus().on('panel:show', () => panel.show())
Key Patterns & Conventions
Strict Module Boundaries
- Core library (
src/): Never import frompages/- must remain pure JavaScript - React website (
pages/): Can import fromsrc/via@/alias to demo features - Entry points:
src/entry.ts(CDN/UMD),pages/main.tsx(React website)
Manual Route Registration Pattern
Routes in pages/router.tsx require explicit definition:
<Route path="/docs/features/dom-operations">
<div className="min-h-screen bg-white dark:bg-gray-900">
<Header />
<DocsLayout>
<DomOperations />
</DocsLayout>
</div>
</Route>
Adding new docs pages: 1) Add route to router.tsx, 2) Add nav item to DocsLayout.tsx
Hash Routing Requirement
Uses wouter with useHashLocation for static hosting compatibility:
<Router hook={useHashLocation}> // Always hash-based routes
CDN Auto-Injection Pattern
Library auto-initializes when injected via script tag:
<script src="page-agent.js?model=gpt-4"></script>
Query params configure PageAgentConfig automatically.
CSS & Design System
- Prefer Tailwind CSS over custom CSS - Use utility classes for styling
- Custom CSS variables in
src/index.cssdefine theme gradients:--theme-color-1: rgb(88, 192, 252); --theme-color-2: rgb(189, 69, 251); - Design follows modern SaaS aesthetic with blue/purple gradients
- Accessibility: Ensure proper contrast ratios, semantic HTML, ARIA labels
- Dark mode support throughout via Tailwind
dark:classes - Responsive grid layouts for features and content sections
File Organization
Core Library (src/)
- PageAgent.ts: Core agent implementation and public API
- entry.ts: Library entry point for CDN/UMD usage
- config/: Configuration constants and settings management
- tools/: Agent tool implementations for web actions and page manipulation
- ui/: User interface components (Panel, SimulatorMask, etc.) for agent visualization
- llms/: LLM integration and communication layer
- dom/: HTML serialization, page analysis utilities, and DOM manipulation helpers
- utils/: Shared utilities including the type-safe event bus system
- patches/: Framework-specific optimizations and compatibility fixes (React, Antd, etc.)
- prompts/: System prompts and LLM instruction templates
- i18n/: Internationalization and language support
Documentation Site (pages/)
main.tsx- Site entry point with hash routing setuprouter.tsx- Manual route definitions (requires explicit registration)components/DocsLayout.tsx- Navigation structure (hardcoded nav items)docs/[section]/[topic]/page.tsx- Documentation pagestest-pages/- Library integration test pages
Development Workflow
Commands
npm start- Development server (Vite)npm run build- Production build (TypeScript check + Vite build)npm run lint- ESLint with strict TypeScript rules
Debugging Routing Issues
Common problem: Blank pages in docs sections. Debug steps:
- Verify route exists in
pages/router.tsx - Check component import path
- Test with minimal component first
- Verify CSS isn't hiding content (check dark mode classes)
TypeScript Configuration
- Strict mode enabled with
noUnusedLocalsandnoUnusedParameters - Path alias
@/*maps tosrc/* - ESLint uses strict TypeScript rules but disables some for pragmatism:
'@typescript-eslint/no-non-null-assertion': 'off' '@typescript-eslint/no-unsafe-assignment': 'off'
Content & Messaging
- Tone: Technical but approachable, targeting web developers
- Core value prop: "一行 CDN 引入,为任何网站添加智能 UI Agent"
- Differentiator: Page-embedded vs external automation (vs browser-use)
- Feature categories: DOM operations, security, data masking, knowledge injection, model integration
Code Quality Standards
- Use TypeScript strict mode
- Prefer functional components with hooks
- Prefer Tailwind CSS over custom CSS - Use utility classes for styling
- Consistent file naming: kebab-case for multi-word files
- Import organization: React imports first, then components, then styles
Critical Files to Understand
pages/router.tsx- Central routing definitionpages/components/DocsLayout.tsx- Navigation structurepages/page.tsx- Homepage showcasing product featuressrc/PageAgent.ts- Core library: AI agent class with DOM manipulationsrc/dom/dom_tree/index.js- DOM extraction engine (ported from Python)src/utils/bus.ts- Type-safe event bus for decoupled communicationsrc/entry.ts- CDN/UMD entry point with auto-initializationvite.config.tsandvite.lib.config.ts- Dual build configurationtsconfig.app.json- TypeScript strictness settings
Core Library Architecture
The core library (src/) is a standalone AI agent with these key components:
- PageAgent.ts: Main agent class managing DOM tree updates, element highlighting, and LLM communication
- DOM processing pipeline: Converts complex web pages into LLM-friendly text while preserving interactive element mappings for precise actions
- Event bus system: Type-safe communication between PageAgent and UI components