Skip to content

Repository files navigation

cf_ai_revops

An autonomous revenue operations agent that monitors business metrics, investigates anomalies, and recommends actions — built entirely on Cloudflare's AI and agent stack.

RevOps Agent dashboard showing investigation trail with root cause, confidence level, and evidence


What It Does

The agent monitors 8 revenue metrics on a cron schedule. When it detects an anomaly (z-score + threshold detection), it runs a 6-step Cloudflare Workflow: fetch metrics → detect anomalies → LLM root cause analysis → store results → update agent state. Users see the full evidence trail — not just a conclusion — and can chat with the agent to dig deeper.

  • Autonomous — cron-triggered investigations every 15 minutes, no user action needed
  • Persistent — Durable Object per company maintains health score and investigation memory across requests
  • Evidence-driven — anomaly detection runs deterministically first; the LLM only fires when a real anomaly is confirmed, and must reason from that evidence
  • Mock mode — full demo runs with zero API keys

Quick Start

git clone https://github.com/GOH8910/cf_ai_revops
cd cf_ai_revops
npm install

Create the local D1 database:

npx wrangler d1 create cf-ai-revops-db
# Copy the database_id output into wrangler.toml under [[d1_databases]]
npx wrangler d1 migrations apply cf-ai-revops-db --local

Start the dev server:

npm run dev
# Open http://localhost:8787

No API key needed — LLM_PROVIDER=mock is the default. To use OpenAI or Workers AI, copy .dev.vars.example to .dev.vars and set the relevant variables.


Demo Flow

On first load the app seeds 7 days of metric data with a pre-baked checkout latency incident.

  1. Dashboard loads → Revenue Health Score ~71 (amber), one active anomaly
  2. Click ▶ Run Investigation → investigation card appears with evidence trail
  3. Chat: "Why did the checkout rate drop?" → grounded answer from the investigation
  4. Click + Inject Incident → inject a new synthetic anomaly
  5. Click ↺ Reset Demo → returns to clean baseline

Suggested chat prompts: Why did revenue drop today? · What's the biggest risk right now? · What should I fix first?


Cloudflare Components

Component Role
Cloudflare Workers HTTP API, cron scheduler, request router
Durable Objects Per-company agent state (health score, last investigation ID), serialises concurrent mutations
Cloudflare Workflows 6-step retryable investigation pipeline with per-step checkpointing
Workers AI LLM inference — @cf/meta/llama-3.1-8b-instruct
D1 (SQLite) Metric snapshots, investigation history, chat messages
KV Health score cache (15-minute TTL matching cron interval)
Cron Triggers Autonomous scheduled investigations every 15 minutes

Static assets (public/) are vanilla JS served directly by the Worker.


Architecture

Browser (vanilla JS)  →  Cloudflare Worker (API + static assets)
                              │
                              ├── Durable Object (RevOpsAgent)
                              │       health score · last investigation ID
                              │       serialises concurrent Workflow + chat mutations
                              │
                              ├── Workflow (InvestigationWorkflow)
                              │       step 1: create investigation record
                              │       step 2: fetch metrics from D1
                              │       step 3: detect anomalies (z-score)
                              │       step 4: LLM root cause analysis
                              │       step 5: store investigation results
                              │       step 6: update Durable Object
                              │
                              ├── D1  — metric_snapshots · investigations · chat_messages
                              ├── KV  — health score cache (TTL 15m)
                              └── Workers AI / OpenAI

See docs/architecture.md for the full design rationale.


Project Structure

cf_ai_revops/
├── PROMPTS.md                        ← AI prompts used + design rationale
├── wrangler.toml
├── src/
│   ├── index.ts                      ← Worker entry: router + cron handler
│   ├── agent/
│   │   ├── RevOpsAgent.ts            ← Durable Object: chat, state, memory
│   │   ├── InvestigationWorkflow.ts  ← Cloudflare Workflow: 6-step pipeline
│   │   └── prompts.ts                ← LLM prompt templates
│   ├── services/
│   │   ├── anomalyDetection.ts       ← Z-score detection, health score
│   │   ├── mockData.ts               ← Mock data generator + seeds
│   │   ├── llmProvider.ts            ← Workers AI / OpenAI / Mock abstraction
│   │   └── storage.ts                ← D1 query helpers
│   └── db/migrations/0001_schema.sql
└── public/                           ← Vanilla JS frontend
    ├── index.html
    ├── styles.css
    └── app.js

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages