5.8 KiB
Instructions for coding assistants
Project Overview
This is a monorepo with npm workspaces containing two main packages:
- Core Library (
packages/page-agent/) - Pure JavaScript/TypeScript AI agent library for browser DOM automation, published aspage-agenton npm - Website (
packages/website/) - React documentation and landing page. Also as demo and test page for the core lib. private package@page-agent/website
Development Commands
Core Commands
npm start # Start website dev server
npm run dev # Same as start
npm run build # Build all packages
npm run build:lib # Build page-agent library only
npm run lint # ESLint with TypeScript strict rules
Package-specific Commands
# Core library
npm run build --workspace=page-agent
npm run build:watch --workspace=page-agent
# Website
npm run dev --workspace=@page-agent/website
npm run build --workspace=@page-agent/website
Architecture & Critical Patterns
Monorepo Structure
packages/
├── page-agent/ # npm: "page-agent"
│ ├── src/ # Core library source
│ ├── vite.config.js # Library build (ES + UMD)
│ └── package.json
└── website/ # npm: "@page-agent/website" (private)
├── src/ # Website source (formerly pages/)
├── index.html
├── vite.config.js # Website build
└── package.json
Module Boundaries (Critical)
- Core library (
packages/page-agent/): NEVER import from website - must remain pure JavaScript - Website (
packages/website/): CAN import frompage-agentfor demos. Alias@/→website/src/
DOM Pipeline
- DOM Extraction: Convert live DOM to
FlatDomTreeviasrc/dom/dom_tree/ - Dehydration: DOM tree → simplified text for LLM processing
- LLM Processing: AI model returns action plans
- Indexed Operations: Map LLM responses back to specific DOM elements
Event Bus Communication
Use src/utils/bus.ts for decoupled PageAgent ↔ UI communication:
// Emit from PageAgent
getEventBus().emit('panel:show')
getEventBus().emit('panel:update', { status: 'thinking' })
// Listen in UI components
getEventBus().on('panel:show', () => panel.show())
Hash Routing Requirement
Uses wouter with useHashLocation for static hosting:
<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 in src/entry.ts.
File Organization
Core Library (packages/page-agent/src/)
entry.ts- CDN/UMD entry point with auto-initializationPageAgent.ts- Main AI agent class orchestrating DOM operationstools/- Agent tool implementations for web actionsui/- UI components (Panel, SimulatorMask) with CSS modulesutils/bus.ts- Type-safe event bus for decoupled communicationpatches/- Framework-specific optimizations (React, Antd compatibility)llms/- LLM integration and communication layerdom/- HTML serialization and page analysis utilitiesconfig/- Configuration constants and settings
Website (packages/website/src/)
main.tsx- Site entry 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
Adding New Features
New Documentation Page
- Create
packages/website/src/docs/<section>/<slug>/page.tsx - Add route to
packages/website/src/router.tsxwith<Header /> + <DocsLayout>wrapper - Add navigation item to
DocsLayout.tsx
New Agent Tool
- Implement under
packages/page-agent/src/tools/ - Export via
packages/page-agent/src/tools/index.ts - Wire into
PageAgent.tsif needed
New UI Component
- Create in
packages/page-agent/src/ui/with colocated CSS modules - Use event bus for PageAgent communication
Code Standards
TypeScript
- Explicit typing for exported/public APIs
- ESLint relaxes some unsafe rules for rapid iteration
CSS & Styling
- Prefer Tailwind CSS over custom CSS
- Custom CSS variables for theme gradients in
src/index.css - Dark mode support via
dark:classes - CSS modules for component-specific styles
Import Organization
- External libraries first
- Internal modules (
@/,@pages/) - Relative imports last
- Blank lines between groups
Critical Files to Understand
-
packages/page-agent/src/PageAgent.ts- Core AI agent class with DOM manipulation -
packages/page-agent/src/dom/dom_tree/index.js- DOM extraction engine -
packages/page-agent/src/utils/bus.ts- Type-safe event bus system -
packages/page-agent/src/entry.ts- Library entry point for CDN usage -
packages/page-agent/vite.config.js- Library build configuration -
packages/website/src/router.tsx- Central routing definition (manual registration required) -
packages/website/src/components/DocsLayout.tsx- Navigation structure -
packages/website/vite.config.js- Website build configuration
Debugging Common Issues
Blank Documentation Pages
- Verify route exists in
packages/website/src/router.tsx - Check component import path
- Verify CSS isn't hiding content (check dark mode classes)
- Test with minimal component first
Library Integration Issues
- Check
packages/page-agent/dist/lib/page-agent.umd.jsbuilds correctly - Test CDN injection with query params
- Verify event bus communications are properly typed
- Use
packages/website/src/test-pages/for isolated testing