Skip to content

Commit 03f2b76

Browse files
committed
add mic selection, upgrade js sdk
1 parent 4290370 commit 03f2b76

File tree

5 files changed

+312
-105
lines changed

5 files changed

+312
-105
lines changed

README.md

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,59 @@
11
# Layercode React SDK
22

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.
44

55
## Installation
66

77
```bash
88
npm install @layercode/react-sdk
99
```
1010

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`.
1212
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)

package-lock.json

Lines changed: 37 additions & 97 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"access": "public"
4040
},
4141
"dependencies": {
42-
"@layercode/js-sdk": "^2.5.0"
42+
"@layercode/js-sdk": "^2.8.0"
4343
},
4444
"devDependencies": {
4545
"@rollup/plugin-commonjs": "^25.0.8",

rollup.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import typescript from "@rollup/plugin-typescript";
55
export default [
66
// ESM build
77
{
8-
input: "src/index.ts",
8+
input: "src/index.tsx",
99
output: {
1010
file: "dist/index.js",
1111
format: "esm",

0 commit comments

Comments
 (0)