DUPATANE ← Back to hands-on labs

AI ASSISTANT · CLOUDFLARE · OPENAI

Building a guarded AI assistant for a static site

A reproducible implementation that connects a GitHub Pages portfolio to an OpenAI-backed assistant through a Cloudflare Worker, with official-source grounding, D1-backed quotas, secret management, and operational validation.

GitHub Pages Cloudflare Workers D1 OpenAI Responses API Wrangler CORS

Architecture

Visitor browser
      ↓ HTTPS fetch
GitHub Pages portfolio + Ask Groot widget
      ↓ POST /api/chat
Cloudflare Worker
  ↙          ↓             ↘
D1 quota   OpenAI API   Origin and input validation
             ↓
       official-documentation web search
             ↓
     short answer + source links + quota metadata

The static page owns the user interface. The Worker is the server-side boundary: it keeps the OpenAI key out of the browser, applies request controls, calls the model, and returns a small JSON contract.

What was implemented

01

Browser integration

The widget reads a public Worker base URL from data-groot-api-base, sends a short conversation over HTTPS, and renders safe answer text and validated links.

02

Guarded API boundary

The Worker handles CORS, body-size limits, message validation, bounded history, timeouts, no-store responses, and an explicit health endpoint.

03

Grounded answers

The Responses API uses web search restricted to approved official documentation domains; returned sources are checked again before they reach the browser.

04

Quota and operations

Cloudflare D1 stores a salted IP hash and rolling 24-hour count. Wrangler deploys the Worker, migrations, and secrets while the public page stays static.

Request lifecycle

OPTIONS /api/chat
        ↓
CORS preflight response

GET /api/health
        ↓
Worker + D1 binding check

POST /api/chat
        ↓
Validate origin, size, history and message
        ↓
Read rolling quota from D1
        ↓
Call OpenAI Responses API with web search
        ↓
Validate answer and official source URLs
        ↓
Return answer, sources and quota status
The app allows three accepted questions per IP in a rolling 24-hour window. The fourth request is rejected before another model call. This is an application safeguard, not a promise about provider billing.

Deployment workflow

wrangler login --device
wrangler d1 create groot-usage
wrangler d1 migrations apply groot-usage --remote
wrangler secret put OPENAI_API_KEY --name groot-api
wrangler secret put RATE_LIMIT_SALT --name groot-api
wrangler deploy

Deployment was verified with health and chat requests using the live portfolio origin. The Worker endpoint is https://groot-api.support-dupatane.workers.dev. The complete runbook, source, migration, troubleshooting, operations, and rollback notes are kept in the private companion repository groot-openai-api-chatbot.

Engineering outcomes