Overview
The Pope Bot framework provides code workspace functionality through:- thepopebot/code - React UI component
- thepopebot/code/actions - Server actions for workspace management
- thepopebot/code/ws-proxy - WebSocket authentication proxy
thepopebot/code
React component for interactive Docker-based code workspaces.Import
Component
Full-page code workspace with terminal (xterm.js) and WebSocket connection
app/code/[id]/page.js:
Features
- In-browser terminal - xterm.js with WebSocket streaming
- Container lifecycle - Auto-start, restart, and recovery
- Session-based auth - WebSocket proxy validates session token
- Real-time sync - Bidirectional terminal I/O over WebSocket
thepopebot/code/actions
Server actions for workspace CRUD operations.Import
Functions
List all workspaces for current user
Create new Docker workspace container
Update workspace name
Toggle workspace star/favorite status
Delete workspace and stop container
Check container state and restart/recreate if needed
Container Recovery
ensureCodeWorkspaceContainer() handles container lifecycle:
- Inspects container state via Docker API
- Restarts if stopped/exited/paused
- Recreates if dead/missing
- Returns status for UI feedback
thepopebot/code/ws-proxy
WebSocket authentication proxy for terminal connections.Import
Route Handler
HTTP upgrade handler for WebSocket connections to code workspaces
app/api/ws/code/[id]/route.js:
Authentication Flow
- Browser connects to
wss://domain/api/ws/code/{id} - Proxy reads
authjs.session-tokencookie from upgrade request - Decodes JWT using
next-auth/jwtwithAUTH_SECRET - Validates user owns workspace (database lookup)
- Returns
401if no session,403if wrong user - Proxies WebSocket to
ws://{containerName}:7681/ws
Why Not Middleware?
Next.js middleware cannot intercept WebSocket upgrade requests. The proxy handles auth directly by decoding the session cookie.Container Architecture
Code workspaces runclaude-code-workspace Docker image:
- Base: Ubuntu with Node.js, Python, Git
- Terminal: ttyd on port 7681 (
/wsendpoint) - Shell: bash with full environment
- Persistence: Docker volumes (not ephemeral)
Container Naming
Docker API Access
Server actions communicate with Docker Engine via Unix socket:Environment Variables
OAuth token for Claude Code integration
Set to
true to enable code workspaces featureSecret for JWT session decoding (same as auth module)
Data Flow
- Create workspace:
createCodeWorkspace()→ Docker API creates container - Browser visits
/code/{id}:CodePagerenders terminal UI - Terminal connects: xterm.js opens WebSocket to
/api/ws/code/{id} - Proxy authenticates: Validates session token, checks workspace ownership
- Stream data: Bidirectional WebSocket proxy to
ttydin container - User types commands: Streamed to container shell, output streamed back
Security
- Session-based auth - All actions use
requireAuth()pattern - Ownership checks - Users can only access their own workspaces
- Timing-safe JWT decode - Prevents timing attacks on session validation
- Container isolation - Each workspace runs in separate Docker container
Related
- thepopebot/auth - Session authentication
- Docker documentation for container API