Overview
The agent worker is a BullMQ-based job processor that runs headless Chromium browsers via Playwright to execute user prompts across AI providers (ChatGPT, Claude, Gemini, Perplexity, Google AI Overview). It extracts responses and sources, stores them in ClickHouse, and triggers analysis.Tech Stack
- Queue: BullMQ (Redis-backed)
- Browser Automation: Playwright (Chromium)
- Proxy Management: Custom rotating proxy pool with scoring
- Stealth: Custom CDP-based stealth configuration
- Language: TypeScript with ES modules
Architecture
Job Flow
Worker Implementation
Worker Initialization
Each provider gets its own BullMQ worker:apps/agent/src/worker.ts
apps/agent/src/worker.ts:12-55
Job Handler
The job handler processes jobs for a single provider:apps/agent/src/worker/jobHandler.ts
apps/agent/src/worker/jobHandler.ts:81-188
Playwright Browser Automation
CDP Browser Launch
The worker uses Chrome DevTools Protocol (CDP) for better control:apps/agent/src/lib/browser/launch.ts
apps/agent/src/lib/browser/launch.ts:44-134
Warm Browser Pool
To avoid repeated browser spawns, the agent maintains a warm pool:apps/agent/src/core/agentHandler.ts
apps/agent/src/core/agentHandler.ts:31-58
Stealth Configuration
The agent uses stealth techniques to avoid detection:Provider Integrations
Provider Configuration
Each provider has a dedicated configuration:apps/agent/src/core/providers/index.ts
apps/agent/src/core/providers/index.ts:19-25
Provider Config Structure
Each provider config defines:Example: ChatGPT Provider
apps/agent/src/core/providers/chatgpt/index.ts
Proxy Management
Proxy Pool
The agent maintains a scored proxy pool:apps/agent/src/lib/browser/proxy/pool.ts
Proxy Retry Logic
The agent retries with different proxies on failure:Job Processing Flow
1. Submit Job Group
The web app submits a job group with one job per enabled provider:packages/services/src/agent/jobs.ts
packages/services/src/agent/jobs.ts:18-72
2. Process Job
The worker picks up the job and runs the agent:3. Store Results
Results are stored in ClickHouse:Adding New Providers
To add a new AI provider:1. Create Provider Config
Createapps/agent/src/core/providers/myprovider/index.ts:
2. Register Provider
Add toapps/agent/src/core/providers/index.ts:
3. Add to Type Definitions
Updatepackages/types/src/provider.ts:
4. Update Database
Add the provider to default enabled providers in the schema:packages/db/src/schema/workspace.ts
Environment Variables
Development Commands
Graceful Shutdown
The worker handles SIGTERM/SIGINT gracefully:apps/agent/src/index.ts
apps/agent/src/index.ts:7-48
Related Documentation
- Services Layer - Agent service functions
- Database - ClickHouse schema for responses
- Web App - Job submission from web UI