Agent: Gemini Integration
Relevant source files
The following files were used as context for generating this wiki page:
The Gemini integration for React Grab is an agent provider that connects the React Grab context selection tool directly to the Gemini CLI. This allows developers to select a UI element in their browser during development, write a prompt, and have the Gemini agent make changes to the codebase directly. This workflow eliminates the need for manual copy-pasting of context, streamlining UI iteration and development.
This provider is one of several available agents, including Claude Code, Cursor, and OpenCode. It consists of a client-side script to capture user intent, a local server to act as a bridge, and a CLI wrapper to interface with the actual Gemini CLI tool. The integration is designed to be added to a project using the grab@latest init or grab@latest add commands.
Sources: packages/website/app/blog/agent/page.tsx:400-427, README.md:110-112, packages/cli/src/commands/add.ts:25-29
Architecture
The Gemini integration operates through a client-server architecture running on the developer's local machine. The core components are the client script, a local Node.js server, and a CLI wrapper that executes the Gemini CLI.
- Client-Side: The
@react-grab/geminiclient script is loaded in the browser. It works with the corereact-grablibrary to capture the user's prompt and the context of the selected element (component stack, file paths, etc.). - Local Server: A lightweight Node.js server (
server.ts) runs on a dedicated port (8567). It listens forPOSTrequests from the client script. - CLI Wrapper: Upon receiving a request, the local server spawns the
cli.tsscript. This script acts as a wrapper, formatting the received context and prompt into a command for the Gemini CLI. - Gemini CLI Execution: The wrapper script executes the Gemini CLI as a child process, passing the formatted prompt.
- Streaming Response: The wrapper captures the
stdoutandstderrfrom the Gemini CLI and streams the output back to the local server, which in turn streams it to the client-side UI, providing real-time status updates.
The following diagram illustrates the data flow from user action to code modification.
This diagram shows the flow of a visual edit request using the Gemini agent integration.
Sources: packages/provider-gemini/src/server.ts:1-74, packages/provider-gemini/src/cli.ts:1-55, packages/website/app/blog/agent/page.tsx:400-427
Setup and Installation
The Gemini agent can be added to a project either during the initial setup of React Grab or later using the add command.
Sources: packages/cli/src/commands/init.ts:1-10, packages/cli/src/commands/add.ts:1-10
Automated Setup
The recommended way to install the Gemini integration is via the React Grab CLI.
- Initialization: If React Grab is not yet installed, run
npx grab@latest initand select "Gemini" from the list of agent integrations. - Adding to Existing Setup: If React Grab is already installed, run
npx grab@latest add gemini.
The CLI automatically modifies the package.json and the root layout file (e.g., app/layout.tsx for Next.js) to configure the server and client components.
Sources: packages/cli/src/commands/init.ts:422-441, packages/cli/src/commands/add.ts:32-38
Manual Configuration
Manual setup involves two main steps: configuring the server to run alongside your development server and adding the client-side script to your application's HTML head. A prerequisite is that the Gemini CLI must be installed on the system.
Sources: README.md:270-272, packages/website/app/blog/agent/page.tsx:400-402
Server Setup
The agent provider includes a server that interfaces with the Gemini CLI. To run it, you modify the dev script in your package.json. The server runs on port 8567 by default.
{
"scripts": {
"dev": "npx @react-grab/gemini@latest && next dev"
}
}
Sources: README.md:275-281, packages/website/app/blog/agent/page.tsx:408-414, packages/provider-gemini/src/server.ts:10
Client Setup
You must include the core react-grab script and the Gemini-specific client script.
Using plain HTML:
<script src="//unpkg.com/react-grab/dist/index.global.js"></script>
<script src="//unpkg.com/@react-grab/gemini/dist/client.global.js"></script>
Sources: README.md:285-288, packages/website/app/blog/agent/page.tsx:420-422
Using Next.js Script component in app/layout.tsx:
import Script from "next/script";
export default function RootLayout({ children }) {
return (
<html>
<head>
{process.env.NODE_ENV === "development" && (
<>
<Script
src="//unpkg.com/react-grab/dist/index.global.js"
strategy="beforeInteractive"
/>
<Script
src="//unpkg.com/@react-grab/gemini/dist/client.global.js"
strategy="lazyOnload"
/>
</>
)}
</head>
<body>{children}</body>
</html>
);
}
Sources: README.md:290-308, packages/website/app/blog/agent/page.tsx:424-442
Core Components
Local Server (server.ts)
The local server is a simple Node.js http server responsible for handling communication between the browser client and the local Gemini CLI.
| Feature | Description |
|---|---|
| Port | Listens on port 8567. |
| Endpoint | A single POST / endpoint accepts the agent request. |
| CORS | Configured to allow requests from any origin (*) to support various local development environments. |
| Process Spawning | Uses child_process.spawn to execute the cli.ts wrapper script. |
| Streaming | Pipes stdout and stderr from the child process directly to the HTTP response, enabling real-time feedback in the browser. |
Sources: packages/provider-gemini/src/server.ts:1-74
The following sequence diagram shows the server's request handling logic.
This diagram shows the sequence of events when a user submits a prompt to the Gemini agent.
Sources: packages/provider-gemini/src/server.ts:31-69, packages/provider-gemini/src/cli.ts:24-54
CLI Wrapper (cli.ts)
This script is the direct interface to the Gemini CLI tool. It is not intended to be run manually but is executed by server.ts.
Its primary responsibilities are:
- Argument Parsing: It receives the raw context string and the user's prompt as command-line arguments.
- Prompt Formatting: It constructs a single, coherent prompt for the Gemini CLI by combining the user's request with the file context provided by React Grab.
- Execution: It spawns the
geminicommand as a child process. - Output Handling: It listens for
data,error, andcloseevents on the child process'sstdoutandstderrstreams and writes the data to its own process streams, which are then captured by the server.