Skip to main content

React Hooks

The Moss SDK exports React hooks for programmatic control of the assistant UI. Use these when you need more control than the declarative data-moss-trigger attribute provides.

useAgent

The primary hook for interacting with the Moss assistant. Must be used within an AgentProvider.

Return Value

useAgent() returns an AgentState object with the following properties and methods:

State Properties

Chat Control Methods

openChat(position?)

Opens the chat modal. No-op if already open.

closeChat()

Closes the chat modal. No-op if already closed.

toggleChat(position?)

Toggles the chat modal open/closed.

sendMessage(text)

Sends a message to the assistant programmatically.
Returns: Promise<void>

startNewChat()

Clears the current conversation and starts a new chat session.
Returns: Promise<void>

Guide Mode Methods

continueGuide()

Advances to the next step in guide mode. Equivalent to clicking the “Next” button.
Returns: Promise<void>

acceptGuideSuggestion()

Accepts the assistant’s suggestion to switch to guide mode. Equivalent to clicking “Start Guide”.
Returns: Promise<void>

Debug Methods

captureContext()

Manually captures the current page context (screenshot and DOM). Only useful for debugging.
Returns: Promise<{ screenshotDataUrl: string | null; pageText: string | null } | null>

Examples

Custom Help Button

Keyboard Shortcut

Message Counter Badge

Conditional Send with Loading State


Notes

useAgent() must be called within an AgentProvider. Calling it outside will throw an error.
The hook returns stable function references. You can safely include them in dependency arrays without causing unnecessary re-renders.