This repository contains a Proof of Concept (POC) for a long-running AI supervisor that oversees an e-commerce order from creation to completion.
The system relies on Durable Execution to allow an AI agent to run, make decisions, sleep for extended periods, and wake up securely without losing its memory or state if the server restarts.
- Temporal (Docker): Handles the durable execution. It manages the long-running workflows, timers, and signals. If an agent goes to sleep for 3 days waiting for a package to be delivered, Temporal guarantees it will wake up on time.
- FastAPI (Python): The backend API that bridges the frontend to Temporal and the PostgreSQL database.
- Next.js (React): The frontend user interface for creating supervisor templates, monitoring live order runs, and injecting events.
- Supabase (PostgreSQL): Stores the supervisor templates, run metadata, and the activity logs generated by the AI agent.
- Groq / Gemini (LLM): The intelligence layer. The agent is fed the order context, its past memory, available tools, and recent events to autonomously decide what action to take next.
- Initialization: When a new order run is created via the UI, FastAPI inserts a record into Supabase and starts a new Temporal Workflow.
- First Run: The AI agent is immediately invoked. It reviews the new order, logs its initial reasoning, updates its compact memory, and decides how long to sleep (e.g., 60 minutes) to wait for an update.
- Event Interruption: Real-world events (like
payment_failedorshipment_delayed) can be injected via the API/UI. These are sent as Signals to the Temporal workflow. - Agent Wake-Up: The workflow instantly interrupts the agent's sleep. The agent is given the new event, its past memory, and the order context.
- Tool Usage: The AI analyzes the event and uses tools (e.g.,
message_payments_team,message_customer) to handle the issue. It updates its reasoning and goes back to sleep. - Termination & Summary: Once the order lifecycle is complete (or manually terminated), a final Temporal activity runs to summarize the entire history, generating a structured report of actions taken, learnings, and recommendations.
- Docker & Docker Compose (for Temporal)
- Python 3.10+
- Node.js 18+
- A Supabase Project (PostgreSQL)
- Groq / Gemini API Keys
Create a .env file in the root directory:
# Supabase
SUPABASE_URL=https://your-project.supabase.co
SUPABASE_KEY=your-anon-key
DATABASE_URL=postgresql://postgres:password@db.your-project.supabase.co:5432/postgres
# Temporal
TEMPORAL_HOST=localhost:7233
TEMPORAL_NAMESPACE=default
TEMPORAL_TASK_QUEUE=order-supervisor
# LLM Keys
GROQ_API_KEY=your-groq-key
GEMINI_API_KEY=your-gemini-key
# App
NEXT_PUBLIC_API_URL=http://localhost:8001Create a .env.local in the frontend/ directory:
NEXT_PUBLIC_API_URL=http://localhost:8001Run the contents of schema.sql in your Supabase SQL Editor. Make sure to disable Row Level Security (RLS) on the tables (supervisors, runs, activity_log) or provide appropriate policies.
docker-compose up -dThe Temporal UI will be available at http://localhost:8233
cd backend
python -m venv venv
source venv/Scripts/activate # Or venv/bin/activate on Mac/Linux
pip install -r requirements.txt
uvicorn main:app --reload --port 8001In a new terminal window:
cd backend
source venv/Scripts/activate
python worker.pyIn a new terminal window:
cd frontend
npm install
npm run devYou can test the full capabilities of the autonomous supervisor via the Next.js frontend (http://localhost:3000).
- Navigate to the Supervisors page.
- Create a new supervisor (e.g., "Strict Payment Supervisor").
- Set the base instruction (e.g., "Monitor orders. If a payment fails, escalate immediately.") and choose the Wake Aggressiveness.
- Go to the Order Runs page and click + New Run.
- Enter an Order ID (e.g.,
ORD-12345) and select your new supervisor. - Click Start Run.
- Click on the newly created run to open its Timeline.
- You will see the agent's initial thoughts:
- 🧠 Reasoning: Acknowledging the new order.
- 😴 Sleep: Setting a timer to check back later.
- The Memory Summary box will reflect the current state.
- Use the Inject Event panel on the right.
- Select an event like
payment_failedand click Send Event. - Watch the Timeline update live:
- 📡 The Event is received.
- ⚡ The AI takes Action (e.g., messaging the payments team).
- 🧠 The AI logs its updated Reasoning.
- 😴 The AI sets a new Sleep timer based on the urgency of the issue.
- Use the Add Instructions panel.
- Type a manual command: "The customer is angry, cancel the order immediately."
- The AI will instantly wake up, read your instruction, and adjust its behavior accordingly.
- Click the red ⏹ Terminate button at the top right.
- Scroll to the bottom of the page to see the Final Summary. This is an AI-generated retrospective of the entire order lifecycle, detailing actions taken and future recommendations.