What You’ll Build
A code editor with:- Real-time syntax highlighting
- Live execution with streaming output
- Tool call visualization
- Error display
- Execution statistics
Prerequisites
- React 18+
- Node.js server running EnclaveJS broker
- Basic React/TypeScript knowledge
Project Setup
Architecture
Step 1: Configure EnclaveJS Client
https: base URL, and send your credentials with the headers option. The client has no other options than baseUrl, headers, timeout, autoReconnect, maxReconnectAttempts, reconnectDelay, maxEventLength and fetch; any other option makes the constructor throw.
An Editor on Another Origin Than the Broker
A browser asks before it sends a request with a JSON body or anAuthorization header to another origin (the CORS preflight). The broker’s session routes answer that question themselves: a page of an origin in the handler’s allowedOrigins (every origin by default) may send Content-Type, Accept, Last-Event-ID and Authorization. A credential in the Authorization header therefore works from an editor on another origin. Add it to the client of this step:
allowedHeaders. The preflight is an OPTIONS request without the credential: authentication in front of the routes has to let OPTIONS requests through to them.
Step 2: Create the Provider
Step 3: Create Execution Hook
stdout events, so output stays empty unless your server sends them.
Stopping has two parts. cancel() ends the session in the client at once: wait() resolves with the code CANCELLED and the hook is idle before the broker has answered. The request that asks the broker to end the session can still fail. The client then calls onError with the code CANCEL_FAILED, and the hook keeps the message in stopError: show it, because the script may still be running on the broker. The hook shows it only for the run that is still the current one. When the user has started another run before the answer arrives, the warning would appear on a run that was not stopped, so the hook drops it; an application that has to know about every unconfirmed stop keeps them in a list of its own, by session id.
Step 4: Build the Editor Component
Step 5: Build Output Panel
Step 6: Build Tool Calls Panel
Step 7: Build Stats Panel
Step 8: Assemble the Main Component
Step 9: Add Styles
Step 10: Wire It Up
Using the Official React Package
The@enclave-vm/react package provides the provider and the hook ready-made:
useEnclaveSession returns execute, cancel, reset, state, isRunning, sessionId, result, error, stdout and events. Its cancel() behaves like the one above: the session ends in the client at once, and a stop the broker did not confirm is reported to the hook’s onError option with the code CANCEL_FAILED. See @enclave-vm/react.
Best Practices
- Debounce execution - Don’t run on every keystroke
- Show loading states - Indicate when code is running
- Handle disconnections - The client reconnects by itself (
autoReconnect) and resumes after the last event it received - Limit output - Cap what you keep of the output and the events to prevent memory issues
- Sanitize display - Escape HTML in output
- Mobile support - Handle touch events for mobile editors
Related
- @enclave-vm/react - React SDK reference
- @enclave-vm/client - Client SDK reference
- Streaming UI Example - Complete example