bandit-runner/docs/development_documentation/DURABLE-OBJECT-SETUP.md
2025-10-09 22:03:37 -06:00

4.2 KiB

Durable Object Setup Issue & Solutions

The Problem

OpenNext builds for Cloudflare Workers don't easily support Durable Objects in local development because:

  1. The build system generates a bundled worker that doesn't export the DO
  2. LangGraph.js has many dependencies that complicate bundling
  3. Local DO bindings require the class to be exported from the worker

Current Status

  • UI Works Perfectly - All frontend components functional
  • API Routes Exist - /api/agent/[runId]/start etc. all created
  • Backend Code Complete - LangGraph agent, tools, everything ready
  • Durable Object Export - Not working in local dev with OpenNext

Solutions

The Durable Object will work perfectly when deployed to Cloudflare:

cd bandit-runner-app

# Deploy to Cloudflare
wrangler deploy

# Set secrets
wrangler secret put OPENROUTER_API_KEY
# Enter your key when prompted

# Test on:
# https://bandit-runner-app.YOUR-ACCOUNT.workers.dev

Why this works:

  • Cloudflare's production environment properly handles DO exports
  • OpenNext builds work correctly in production
  • All bindings are available

Option 2: Mock the Durable Object (For Local Dev)

Create a mock DO that returns simulated responses:

// In API routes, add:
if (process.env.NODE_ENV === 'development') {
  // Return mock response
  return NextResponse.json({
    success: true,
    runId,
    state: {
      status: 'running',
      currentLevel: 0,
      thoughts: ['[Mock] Agent would start here']
    }
  })
}

Option 3: Separate DO Worker

Create the DO in a separate wrangler project:

# Create new worker project
mkdir ../bandit-agent-do
cd ../bandit-agent-do

# Create wrangler.toml
cat > wrangler.toml << EOF
name = "bandit-agent-do"
main = "src/index.ts"
compatibility_date = "2025-01-01"

[[durable_objects.bindings]]
name = "BANDIT_AGENT"
class_name = "BanditAgentDO"
EOF

# Copy DO code
cp ../bandit-runner-app/src/lib/durable-objects/BanditAgentDO.ts src/index.ts

# Deploy
wrangler deploy

Then update main app's wrangler.jsonc to use remote DO.

Option 4: Use wrangler dev with custom build

Skip OpenNext for local dev:

# Build Next.js normally
pnpm build

# Run with wrangler directly (not via OpenNext)
wrangler dev --local

# Access at http://localhost:8787

For Development & Testing:

  1. UI already works - test all components locally
  2. Test SSH proxy integration separately
  3. Mock the agent responses for UI development

For Real Testing:

  1. Deploy to Cloudflare production
  2. Set secrets properly
  3. Test full integration with real LLMs

Current Working Features:

  • Beautiful retro terminal UI
  • Control panel with model selection
  • WebSocket client code
  • All API route handlers
  • LangGraph state machine
  • SSH tool wrappers
  • Error handling
  • Cost tracking

What Needs Production:

  • ⏸️ Actual Durable Object runtime
  • ⏸️ Real WebSocket connections
  • ⏸️ LangGraph execution

Quick Deploy Guide

cd bandit-runner-app

# 1. Deploy
wrangler deploy

# 2. Set API key
wrangler secret put OPENROUTER_API_KEY
# Paste: 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

# 3. Test
# Open: https://bandit-runner-app.YOUR-SUBDOMAIN.workers.dev
# Click START
# Watch it work! 🎉

Why Deploy is Better

  1. Zero Configuration - Works out of the box
  2. Real Environment - Actual Workers runtime
  3. Free Tier - Cloudflare Free plan includes DOs
  4. Fast - Edge deployment, global CDN
  5. No Build Complexity - OpenNext handles everything

Next Steps

Choose your path:

A) Want to see it working NOW? → Deploy to Cloudflare (5 minutes)

B) Want to develop UI locally? → Use pnpm dev and test components → Mock agent responses

C) Want full local development? → Create separate DO worker project → Use service bindings

D) Want production-ready? → Deploy to Cloudflare → Set up D1 database → Configure R2 storage → Add monitoring

I recommend Option A - deploy to production and test the full system there. Local development with OpenNext + DOs is complex, but production deployment is simple and everything works!