Provider Architecture
Relevant source files
The following files were used as context for generating this wiki page:
The Provider Architecture in React Grab is a modular, pluggable system designed to connect the core context-gathering functionality with various external AI coding agents. Its primary purpose is to enable a seamless workflow where a developer can select a UI element in the browser, provide a prompt, and have an agent directly modify the corresponding source code without manual copy-pasting or context switching.
This architecture is built on a client-server model. A small client-side script, specific to each provider, runs in the browser to capture user interactions and communicate with a local server. This local server acts as a bridge, invoking the respective agent's Command Line Interface (CLI) or SDK with the context provided by React Grab. This design makes the system tool-agnostic and extensible, allowing for the integration of any coding agent with a CLI or API.
Sources: packages/website/app/blog/agent/page.tsx:290-319, CONTRIBUTING.md:20-29
High-Level Architecture
The data flow begins with a user action in the browser and ends with the application hot-reloading after an agent modifies the source code. The provider acts as the intermediary, connecting the browser's context with the local development environment's file system access.
The following diagram illustrates the end-to-end process:
This diagram shows how the client-side provider script sends captured context to a local server, which in turn executes the coding agent to modify files, triggering a hot reload.
Sources: packages/website/app/blog/agent/page.tsx:290-319, packages/provider-cursor/src/server.ts, packages/provider-cursor/src/client.ts
CLI and Automated Setup
React Grab provides a CLI (@react-grab/cli) to automate the installation and configuration of the core library and its agent providers. The init and add commands are central to this process.
Sources: packages/cli/src/commands/init.ts, packages/cli/src/commands/add.ts
init Command
The npx grab@latest init command is the primary entry point for new users. It performs a series of steps to detect the project environment and apply the necessary code transformations.
The workflow for the init command is as follows:
This flow shows the logic for both initial installation and reconfiguration of an existing setup, including framework detection, package installation, and file modification.
Sources: packages/cli/src/commands/init.ts:145-590
Key actions performed by the CLI include:
- Project Detection: Identifies the framework (Next.js, Vite, etc.), package manager, and any currently installed React Grab agents.
- File Transformation: Modifies project files (e.g.,
layout.tsx,index.html) to inject the necessary script tags forreact-graband the selected agent provider. package.jsonModification: Adds a script to thepackage.jsonfile to run the agent's local server alongside the development server.
Sources: packages/cli/src/commands/init.ts:162-163, 501-507, 520-526, packages/cli/test/transform.test.ts:221-232
add Command
The npx grab@latest add <agent> command is used to add a new agent integration to an existing React Grab installation. It allows users to add or replace agents, automatically handling package installation and updates to package.json and layout files.
Sources: packages/cli/src/commands/add.ts:70-298
Provider Structure
Each agent integration is a separate package within the packages/ directory, typically following the naming convention provider-<agent-name>. A provider consists of two main parts: a client-side script and a server-side component.
| Package Name | Agent Name | Default Port |
|---|---|---|
@react-grab/claude-code | Claude Code | 4567 |
@react-grab/cursor | Cursor | 5567 |
@react-grab/opencode | OpenCode | 6567 |
@react-grab/codex | Codex | 7567 |
@react-grab/gemini | Gemini | 8567 |
@react-grab/amp | Amp | 9567 |
@react-grab/droid | Factory Droid | 10567 |
@react-grab/ami | Ami | - |
@react-grab/visual-edit | Visual Edit | - |
Sources: CONTRIBUTING.md:22-29, packages/website/app/blog/agent/page.tsx:200-540, packages/cli/src/commands/init.ts:98-107
Client-Side Logic (client.ts)
The client script is responsible for integrating with the react-grab core library in the browser. Its main functions are:
- Registering the Provider: It calls
window.__REACT_GRAB_UI__.registerProvider()to make itself known to the core UI. - Creating UI: It renders the UI for user input, such as a prompt box that appears after an element is selected.
- Communication: It establishes a WebSocket or HTTP connection to the local provider server to send the user's prompt and the captured element context. It also receives and displays status updates from the server.
Sources: packages/provider-cursor/src/client.ts:14-25, 100-111, packages/agent-playground/app/main.tsx:4-12
Server-Side Logic (server.ts)
The server is a lightweight local process (often an Express server with WebSockets) that acts as a bridge between the browser and the agent's CLI.
- Listens for Requests: It opens a port and listens for incoming connections from the client script.
- Spawns Agent Process: Upon receiving a prompt and context, it spawns the agent's CLI tool as a child process (e.g.,
cursor-agent). - Streams I/O: It pipes the agent's
stdoutandstderrback to the client over the WebSocket, allowing the user to see real-time progress.
Sources: packages/provider-cursor/src/server.ts:25-33, 51-68
The following sequence diagram details the interaction between these components for the Cursor provider.
This diagram illustrates the full loop from user input to code modification and UI update, mediated by the client and server components of the provider.
Sources: packages/provider-cursor/src/client.ts:100-111, packages/provider-cursor/src/server.ts:51-68
Context Gathering
Before the provider is invoked, the core react-grab library is responsible for gathering context about the selected element. This is the crucial information that makes the agent's job easier. The getElementContext function is central to this process.
It works by:
- Getting the React Fiber node from the DOM element.
- Walking up the Fiber tree to get the owner stack, which includes component names and source locations (file, line, column).
- Filtering out internal or irrelevant components using heuristics (e.g., checking for capitalization, blacklisted names).
- Generating a preview of the HTML structure around the selected element.
- Combining the HTML preview and the component stack into a single string to be sent to the agent.
Sources: packages/react-grab/src/context.ts:68-121
Conclusion
The Provider Architecture is a key element of React Grab's design, enabling its core value proposition: connecting visual UI context directly to code-editing agents. By abstracting the communication layer into distinct, pluggable providers, the system remains extensible and independent of any single AI tool. The combination of automated CLI setup, a clear client-server model, and a standardized provider structure allows for easy integration of new agents and a powerful, streamlined workflow for frontend development.
Sources: packages/website/app/blog/agent/page.tsx:290-319, CONTRIBUTING.md:20-29