Masheev React SDK
Build fully custom chat UIs using React hooks. Two approaches:
@masheev/embed-sdk/react — Widget in an iframe, controlled via hooks
@masheev/embed-sdk/headless — No iframe, direct WebSocket connection, full UI control
When to Use Which
| Need |
Use |
| Default chat bubble with some customization |
useMasheev from /react |
| Embed widget in a specific area of the page |
useMasheev with mode: "embedded" |
| Fully custom chat UI (own message bubbles, layout) |
MasheevProvider + hooks from /headless |
| Chat on React Native |
/headless (no iframe available) |
Widget Hook (/react)
import { useMasheev } from "@masheev/embed-sdk/react";
function App() {
const {
open, close, toggle,
hide, show,
sendMessage,
setInputValue,
updateContext,
updateContact,
updateTools,
updateWorkflow,
setQuestions,
setListening,
resetConversation,
on, off,
containerRef, // For embedded mode — attach to a DOM element
isReady,
} = useMasheev({
inboxId: "YOUR_INBOX_ID",
mode: "chat-widget",
user: { name: "Jane", email: "jane@example.com" },
});
return (
<div>
<button disabled={!isReady}>Open Chat</button>
</div>
);
}
Singleton pattern: Multiple useMasheev() calls with the same inboxId share a single SDK instance and iframe. The iframe is destroyed only when the last consumer unmounts.
Headless Mode (/headless)
import {
MasheevProvider,
useWidgetSession,
useWidgetChat,
useWidgetSocket,
useReadAloud,
} from "@masheev/embed-sdk/headless";
function App() {
return (
<MasheevProvider config={{
inboxId: "YOUR_INBOX_ID",
apiBase: "https://api.masheev.com",
customerInfo: { name: "Jane", email: "jane@example.com" },
}}>
<CustomChat />
</MasheevProvider>
);
}
function CustomChat() {
const { conversationId, greeting, workflowRunState } = useWidgetSession();
const { messages, sendMessage, isStreaming } = useWidgetChat();
const { status: connectionStatus } = useWidgetSocket();
return (
<div>
<div className="messages">
{messages.map((msg) => (
<div key={msg.id} className={msg.role}>
{msg.content}
</div>
))}
{isStreaming && <div className="typing">AI is typing...</div>}
</div>
<input
=> {
if (e.key === "Enter") {
sendMessage(e.currentTarget.value);
e.currentTarget.value = "";
}
}}
/>
</div>
);
}
Provider Config
interface MasheevProviderConfig {
inboxId: string; // Required
apiBase?: string; // Default: "https://api.masheev.com"
turnstileSiteKey?: string; // Cloudflare Turnstile (anti-bot)
customerInfo?: {
name?: string;
email?: string;
phone?: string;
};
tools?: readonly ClientToolDefinition[]; // Client-side tools
instructions?: string; // Tool usage instructions for AI
workflow?: WorkflowConfig; // Conversational workflow
onStepComplete?: (payload: WorkflowStepCompletePayload) => void;
onWorkflowComplete?: (payload: WorkflowCompletePayload) => void;
}
Headless Hooks Reference
| Hook |
Returns |
Purpose |
useWidgetSession() |
{ conversationId, contactId, greeting, workflowRunState, status } |
Session lifecycle |
useWidgetChat() |
{ messages, sendMessage, resolve, isStreaming, history } |
Message operations |
useWidgetSocket() |
{ status, subscribe, unsubscribe } |
WebSocket connection state |
useReadAloud() |
{ play, pause, stop, isPlaying, currentMessageId } |
Text-to-speech controls |
Defining Tools with Zod
import { clientTool } from "@masheev/embed-sdk/headless";
import { z } from "zod";
const bookingTool = clientTool({
name: "check_availability",
description: "Check appointment availability for a given date",
parameters: z.object({
date: z.string().describe("ISO date string"),
service: z.string().describe("Service type"),
}),
execute: async (args, { onProgress }) => {
onProgress("Checking calendar...");
const slots = await fetchSlots(args.date, args.service);
return { success: true, data: { slots } };
},
});
// Pass to provider
<MasheevProvider config={{ inboxId: "...", tools: [bookingTool] }}>
Workflow Helpers
import { defineWorkflow, validateWorkflowConfig } from "@masheev/embed-sdk/headless";
const
id: "user_onboarding",
name: "New User Onboarding",
steps: [
{ id: "greeting", name: "Welcome", instructions: "Greet the user by name: {{context.name}}" },
{ id: "collect_info", name: "Collect Details", tools: ["check_availability"] },
{ id: "confirm", name: "Confirm Booking", instructions: "Summarize and confirm" },
],
context: { name: "Jane" },
});
// Validate before passing to provider
const result = validateWorkflowConfig(onboardingFlow);
if (!result.valid) console.error(result.errors);
See references/headless.md for advanced patterns (custom message components, streaming, reconnection).
1---2name: masheev-react-sdk3description: Use when building custom chat UIs with the Masheev React SDK instead of the default widget. Covers the useMasheev hook, MasheevProvider for headless mode, useWidgetSession, useWidgetChat, useWidgetSocket, useReadAloud, custom message rendering, event handling, and building chat interfaces from scratch. Use this skill when someone wants a "custom chat UI", "headless integration", "build their own chat component", or references @masheev/embed-sdk/react or @masheev/embed-sdk/headless.4license: Apache-2.05---67# Masheev React SDK89Build fully custom chat UIs using React hooks. Two approaches:10111. **`@masheev/embed-sdk/react`** — Widget in an iframe, controlled via hooks122. **`@masheev/embed-sdk/headless`** — No iframe, direct WebSocket connection, full UI control1314## When to Use Which1516| Need | Use |17|------|-----|18| Default chat bubble with some customization | `useMasheev` from `/react` |19| Embed widget in a specific area of the page | `useMasheev` with `mode: "embedded"` |20| Fully custom chat UI (own message bubbles, layout) | `MasheevProvider` + hooks from `/headless` |21| Chat on React Native | `/headless` (no iframe available) |2223## Widget Hook (`/react`)2425```tsx26import { useMasheev } from "@masheev/embed-sdk/react";2728function App() {29 const {30 open, close, toggle,31 hide, show,32 sendMessage,33 setInputValue,34 updateContext,35 updateContact,36 updateTools,37 updateWorkflow,38 setQuestions,39 setListening,40 resetConversation,41 on, off,42 containerRef, // For embedded mode — attach to a DOM element43 isReady,44 } = useMasheev({45 inboxId: "YOUR_INBOX_ID",46 mode: "chat-widget",47 user: { name: "Jane", email: "jane@example.com" },48 });4950 return (51 <div>52 <button onClick={open} disabled={!isReady}>Open Chat</button>53 </div>54 );55}56```5758**Singleton pattern**: Multiple `useMasheev()` calls with the same `inboxId` share a single SDK instance and iframe. The iframe is destroyed only when the last consumer unmounts.5960## Headless Mode (`/headless`)6162```tsx63import {64 MasheevProvider,65 useWidgetSession,66 useWidgetChat,67 useWidgetSocket,68 useReadAloud,69} from "@masheev/embed-sdk/headless";7071function App() {72 return (73 <MasheevProvider config={{74 inboxId: "YOUR_INBOX_ID",75 apiBase: "https://api.masheev.com",76 customerInfo: { name: "Jane", email: "jane@example.com" },77 }}>78 <CustomChat />79 </MasheevProvider>80 );81}8283function CustomChat() {84 const { conversationId, greeting, workflowRunState } = useWidgetSession();85 const { messages, sendMessage, isStreaming } = useWidgetChat();86 const { status: connectionStatus } = useWidgetSocket();8788 return (89 <div>90 <div className="messages">91 {messages.map((msg) => (92 <div key={msg.id} className={msg.role}>93 {msg.content}94 </div>95 ))}96 {isStreaming && <div className="typing">AI is typing...</div>}97 </div>98 <input99 onKeyDown={(e) => {100 if (e.key === "Enter") {101 sendMessage(e.currentTarget.value);102 e.currentTarget.value = "";103 }104 }}105 />106 </div>107 );108}109```110111## Provider Config112113```typescript114interface MasheevProviderConfig {115 inboxId: string; // Required116 apiBase?: string; // Default: "https://api.masheev.com"117 turnstileSiteKey?: string; // Cloudflare Turnstile (anti-bot)118 customerInfo?: {119 name?: string;120 email?: string;121 phone?: string;122 };123 tools?: readonly ClientToolDefinition[]; // Client-side tools124 instructions?: string; // Tool usage instructions for AI125 workflow?: WorkflowConfig; // Conversational workflow126 onStepComplete?: (payload: WorkflowStepCompletePayload) => void;127 onWorkflowComplete?: (payload: WorkflowCompletePayload) => void;128}129```130131## Headless Hooks Reference132133| Hook | Returns | Purpose |134|------|---------|---------|135| `useWidgetSession()` | `{ conversationId, contactId, greeting, workflowRunState, status }` | Session lifecycle |136| `useWidgetChat()` | `{ messages, sendMessage, resolve, isStreaming, history }` | Message operations |137| `useWidgetSocket()` | `{ status, subscribe, unsubscribe }` | WebSocket connection state |138| `useReadAloud()` | `{ play, pause, stop, isPlaying, currentMessageId }` | Text-to-speech controls |139140## Defining Tools with Zod141142```typescript143import { clientTool } from "@masheev/embed-sdk/headless";144import { z } from "zod";145146const bookingTool = clientTool({147 name: "check_availability",148 description: "Check appointment availability for a given date",149 parameters: z.object({150 date: z.string().describe("ISO date string"),151 service: z.string().describe("Service type"),152 }),153 execute: async (args, { onProgress }) => {154 onProgress("Checking calendar...");155 const slots = await fetchSlots(args.date, args.service);156 return { success: true, data: { slots } };157 },158});159160// Pass to provider161<MasheevProvider config={{ inboxId: "...", tools: [bookingTool] }}>162```163164## Workflow Helpers165166```typescript167import { defineWorkflow, validateWorkflowConfig } from "@masheev/embed-sdk/headless";168169const onboardingFlow = defineWorkflow({170 id: "user_onboarding",171 name: "New User Onboarding",172 steps: [173 { id: "greeting", name: "Welcome", instructions: "Greet the user by name: {{context.name}}" },174 { id: "collect_info", name: "Collect Details", tools: ["check_availability"] },175 { id: "confirm", name: "Confirm Booking", instructions: "Summarize and confirm" },176 ],177 context: { name: "Jane" },178});179180// Validate before passing to provider181const result = validateWorkflowConfig(onboardingFlow);182if (!result.valid) console.error(result.errors);183```184185See [references/headless.md](./references/headless.md) for advanced patterns (custom message components, streaming, reconnection).