Skip to main content

Overview

Agent providers allow you to integrate AI assistants and coding agents into React Grab. When configured, users can enter prompt mode to send element context to your agent and receive responses through a streaming interface.

AgentProvider Interface

The AgentProvider interface defines how React Grab communicates with your AI agent:
(context: AgentContext, signal: AbortSignal) => AsyncIterable<string>
required
Send element context to the agent and return a stream of response chunks.Returns an async iterable that yields response chunks as strings.
(sessionId: string, signal: AbortSignal, storage: AgentSessionStorage) => AsyncIterable<string>
Resume a previous session (e.g., after page reload). Only called if supportsResume is true.
(sessionId: string) => Promise<void>
Abort an ongoing agent session
boolean
Whether the provider supports resuming sessions
boolean
Whether the provider supports follow-up questions in the same session
string
Custom text for the dismiss button (default: “Dismiss”)
() => Promise<boolean>
Check if the agent service is available/connected
() => string | undefined
Return a custom completion message shown when the agent finishes
() => Promise<void>
Undo the last agent action
() => boolean
Whether undo is currently available
() => Promise<void>
Redo a previously undone action
() => boolean
Whether redo is currently available

AgentContext Interface

Context information passed to the agent:
string[]
Array of content strings for each selected element. Typically includes HTML source code and component information.
string
The user’s prompt or question
T | undefined
Custom options provided by the getOptions function
string | undefined
Session ID for tracking conversations and follow-ups

AgentOptions Interface

Configuration options for agent integration:
AgentProvider<T>
The agent provider instance
AgentSessionStorage | null
Storage interface for persisting session data. Set to null to disable storage.
() => T
Function that returns custom options passed to the provider
(session: AgentSession, elements: Element[]) => void
Called when an agent session starts
(status: string, session: AgentSession) => void
Called when the agent sends a status update
(session: AgentSession, elements: Element[]) => AgentCompleteResult | void | Promise<AgentCompleteResult | void>
Called when the agent completes successfully
(error: Error, session: AgentSession) => void
Called when an error occurs
(session: AgentSession) => void
Called when resuming a session
(session: AgentSession, elements: Element[]) => void
Called when the user aborts the session
(session: AgentSession, elements: Element[]) => void
Called when the user undoes an action
(session: AgentSession, elements: Element[]) => void
Called when the user dismisses the session

Creating a Basic Agent Provider

Here’s a simple agent provider that sends requests to an API:

Advanced Agent Provider with All Features

A complete implementation with session management, undo/redo, and connection checking:

Registering an Agent Provider

Register an agent provider through an action:

AgentSession Interface

Session information tracked during agent interactions:

Best Practices

  1. Error Handling: Always handle network errors and API failures gracefully
  2. Abort Support: Implement proper cancellation using the AbortSignal
  3. Streaming: Yield chunks as soon as they’re available for responsive UX
  4. Session Storage: Use AgentSessionStorage to persist sessions across page reloads
  5. Type Safety: Use generics to type your custom options: AgentProvider<MyOptions>
  6. Status Updates: Use the onStatus callback to show progress to users
  7. Connection Checks: Implement checkConnection to verify service availability
  8. Follow-ups: Set supportsFollowUp: true to enable conversation continuity

Example: OpenAI Integration

See Also