|
1 | 1 | # Layercode React SDK |
2 | 2 |
|
3 | | -A React SDK for integrating Layercode audio agents into React applications. |
| 3 | +Build polished Layercode agent experiences in React with a single hook and a few UI helpers. |
4 | 4 |
|
5 | 5 | ## Installation |
6 | 6 |
|
7 | 7 | ```bash |
8 | 8 | npm install @layercode/react-sdk |
9 | 9 | ``` |
10 | 10 |
|
11 | | -## Usage |
| 11 | +> When developing in this repo we consume the package via a local file dependency (`layercode-react-sdk`). When publishing, the package name remains `@layercode/react-sdk`. |
12 | 12 |
|
13 | | -Read the docs: [https://docs.layercode.com/sdk-reference/react-sdk](https://docs.layercode.com/sdk-reference/react-sdk) |
| 13 | +## Quick start |
| 14 | + |
| 15 | +```tsx |
| 16 | +import { MicrophoneSelect, useLayercodeAgent } from '@layercode/react-sdk'; |
| 17 | + |
| 18 | +export function VoiceAgent() { |
| 19 | + const agent = useLayercodeAgent({ |
| 20 | + agentId: process.env.NEXT_PUBLIC_LAYERCODE_AGENT_ID!, |
| 21 | + authorizeSessionEndpoint: '/api/authorize', |
| 22 | + onMessage: (event) => { |
| 23 | + if (event.type === 'response.text') { |
| 24 | + console.log('Agent says:', event.content); |
| 25 | + } |
| 26 | + }, |
| 27 | + }); |
| 28 | + |
| 29 | + return ( |
| 30 | + <div> |
| 31 | + <button onClick={() => agent.connect()}>Connect</button> |
| 32 | + <MicrophoneSelect agent={agent} helperText="Pick an input before connecting." /> |
| 33 | + <p>User speaking? {agent.userSpeaking ? 'yes' : 'no'}</p> |
| 34 | + <p>Agent speaking? {agent.agentSpeaking ? 'yes' : 'no'}</p> |
| 35 | + </div> |
| 36 | + ); |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +## Microphone selection |
| 41 | + |
| 42 | +`useLayercodeAgent` now exposes microphone state and helpers: |
| 43 | + |
| 44 | +- `availableInputDevices`: list of normalized `LayercodeAudioInputDevice`s. |
| 45 | +- `activeInputDeviceId` / `preferredInputDeviceId`: reflect what is currently in use or what the user picked. |
| 46 | +- `refreshInputDevices()`: manually re-enumerate mics (permissions are requested automatically the first time). |
| 47 | +- `selectInputDevice(deviceId: string | null)`: persist the preferred device, even before connecting. |
| 48 | + |
| 49 | +Pair those fields with the ready-made `<MicrophoneSelect />` component to ship a drop-in selector with loading/error states. The component accepts plain `select` props plus a few extras (`helperText`, `containerClassName`, `autoRefresh`). (There is also a `LayercodeMicrophoneSelect` alias for backwards compatibility.) |
| 50 | + |
| 51 | +All React-only logic (state, refs, UI) lives in this package; device enumeration, watchers, and permission handling are implemented in `@layercode/js-sdk`, so they can also be used outside React. |
| 52 | + |
| 53 | +## Speaking state |
| 54 | + |
| 55 | +The hook surfaces `userSpeaking` and `agentSpeaking` booleans. They flip to `true` whenever VAD detects speech (user) or when the agent begins streaming audio (agent). This makes it trivial to render pulse indicators, avatar animations, or “agent typing” affordances without manually parsing websocket messages. |
| 56 | + |
| 57 | +## Documentation |
| 58 | + |
| 59 | +Full API reference lives in the docs: [https://docs.layercode.com/sdk-reference/react-sdk](https://docs.layercode.com/sdk-reference/react-sdk) |
0 commit comments