Skip to main content

@enclave-vm/react

React hooks and components for building interactive code execution UIs with the EnclaveJS streaming runtime.

Installation

Peer Dependencies:
  • react >= 18.0.0

Quick Start

EnclaveProvider

The provider sets up the EnclaveClient context for your application.
config takes exactly the options of EnclaveClient: baseUrl, headers, timeout, autoReconnect, maxReconnectAttempts, reconnectDelay, maxEventLength and fetch. Any other option, such as encryption, auth or serverUrl, throws when the provider renders.
The client does not encrypt. Requests and events travel as plain JSON over HTTP and are protected only by the transport: use an https: base URL, and send credentials with headers.

Configuration Changes

The provider builds the client from config:
  • When a render brings a configuration with other values, such as another baseUrl or a renewed token in headers, the provider builds a new client from it. The context then hands out that client together with that configuration.
  • A render with an equal configuration, such as an object literal written inline, keeps the client.
  • A session that was started before the change finishes on the client it was started with.

Using Your Own Client

Pass a client that you created yourself with the client prop. The provider then builds none; config is still required, and the context reports it as it is.

useEnclaveSession Hook

The main hook for executing code and managing session state.

Basic Usage

Session States

Options

onComplete is called with the result of every execution, whether it succeeded, failed or was cancelled. onError is called for non-fatal errors that the session reports while it runs, when the execution fails, and with the code CANCEL_FAILED when the broker does not confirm a cancellation. There is also a sessionId option. The ID is used for every execution of the hook; without it, each execution gets a generated ID.

Return Value

execute resolves with the SessionResult of the execution, also when it fails or is cancelled. result holds the same object: the returned value is result.value, and the client’s stats are result.stats. stdout, events, result and error are cleared when the next execution starts. reset() clears them too, but it does not cancel a running session.

useEnclaveClient Hook

Access the underlying EnclaveClient directly.

useEnclaveContext Hook

Access the full context value including client and configuration.

Examples

Code Editor with Output

With Tool Calls

Tools run on the broker. The hook is told about tool calls; it executes nothing.

With Cancellation

cancel() ends the session in the client, where the state becomes 'cancelled', and asks the broker to end it. The hook does not cancel a running session when the component unmounts.

Multiple Sessions

TypeScript Types