React Hooks
The Moss SDK exports React hooks for programmatic control of the assistant UI. Use these when you need more control than the declarativedata-moss-trigger attribute provides.
useAgent
The primary hook for interacting with the Moss assistant. Must be used within anAgentProvider.
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.
Promise<void>
Guide Mode Methods
continueGuide()
Advances to the next step in guide mode. Equivalent to clicking the “Next” button.
Promise<void>
acceptGuideSuggestion()
Accepts the assistant’s suggestion to switch to guide mode. Equivalent to clicking “Start Guide”.
Promise<void>
Debug Methods
captureContext()
Manually captures the current page context (screenshot and DOM). Only useful for debugging.
Promise<{ screenshotDataUrl: string | null; pageText: string | null } | null>
Examples
Custom Help Button
Keyboard Shortcut
Message Counter Badge
Conditional Send with Loading State
Notes
The hook returns stable function references. You can safely include them in dependency arrays without causing unnecessary re-renders.