# GPT Puppeteer

A Cloudflare Worker that uses Puppeteer to automate image generation with ChatGPT.

## Features

- HTTP API for submitting image generation requests
- Queue-based processing for reliability
- Support for uploading images or providing image URLs
- Webhook notifications when images are ready
- Local development with a real Chrome browser

## Setup

### Prerequisites

- Node.js 18+
- pnpm or npm
- Cloudflare account with Workers and R2 enabled

### Installation

```bash
# Install dependencies
pnpm install
```

### Local Development

1. Create a `cookies.json` file with your ChatGPT cookies (see `cookies.json.example`)
2. Start the local development server:

```bash
pnpm dev:worker
```

This will launch a local server at http://localhost:8787 and use your local Chrome browser for processing.

### Production Setup

1. Set up the required Cloudflare resources:

```bash
# Create R2 buckets and queue
pnpm setup:all

# Or individually:
pnpm setup:buckets
pnpm setup:queue
```

2. Upload your cookies to R2:

```bash
pnpm cookies:upload
```

3. Deploy the worker:

```bash
pnpm publish
```

## Usage

### Submitting a Job

```bash
curl -X POST https://your-worker.workers.dev \
  -F "prompt=A beautiful sunset over mountains" \
  -F "webhook_url=https://your-webhook.com/callback"
```

You can also include an image:

```bash
curl -X POST https://your-worker.workers.dev \
  -F "prompt=Make this image more vibrant" \
  -F "image=@local-image.jpg"
```

Or an image URL:

```bash
curl -X POST https://your-worker.workers.dev \
  -F "prompt=Make this image more vibrant" \
  -F "image_url=https://example.com/image.jpg"
```

### Checking Job Status

```bash
curl https://your-worker.workers.dev/JOB_ID
```

## Cookie Management

### Downloading Cookies from Production

```bash
pnpm cookies:download
```

### Uploading Cookies to Production

```bash
pnpm cookies:upload
```

### Local Cookie Configuration

You can either:
1. Create a `cookies.json` file in the project root
2. Add your cookies to the `.env` file as `CHATGPT_COOKIES`

## Development Commands

- `pnpm build`: Build the TypeScript code
- `pnpm dev:worker`: Run the worker locally with a real Chrome browser
- `pnpm dev:worker:remote`: Run the worker on Cloudflare's edge (temporary deployment)
- `pnpm deploy`: Deploy the worker to production
- `pnpm publish`: Build and deploy the worker to production
- `pnpm cookies:upload`: Upload local cookies.json to R2
- `pnpm cookies:download`: Download cookies.json from R2
- `pnpm setup:buckets`: Create R2 buckets
- `pnpm setup:queue`: Create Cloudflare Queue
- `pnpm setup:all`: Set up all required Cloudflare resources
