orb-ui 0.2.4 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -10
- package/dist/adapters/index.d.ts +2 -1
- package/dist/adapters/livekit/index.d.ts +129 -0
- package/dist/adapters/types.d.ts +4 -8
- package/dist/adapters/vapi/index.d.ts +2 -2
- package/dist/adapters.cjs +1 -1
- package/dist/adapters.js +425 -73
- package/dist/components/Orb/Orb.d.ts +1 -1
- package/dist/components/Orb/Orb.types.d.ts +31 -10
- package/dist/components/Orb/index.d.ts +1 -1
- package/dist/components/Orb/signals.d.ts +5 -0
- package/dist/index.d.ts +1 -1
- package/dist/orb-ui.cjs +2 -2
- package/dist/orb-ui.js +126 -147
- package/package.json +7 -4
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# orb-ui
|
|
2
2
|
|
|
3
|
-
React voice agent UI components for Vapi, ElevenLabs, and custom realtime voice AI apps. orb-ui gives you animated voice orbs, audio-reactive themes, accessible clickable controls, and provider adapters for building polished voice agent interfaces in React.
|
|
3
|
+
React voice agent UI components for Vapi, ElevenLabs, LiveKit, and custom realtime voice AI apps. orb-ui gives you animated voice orbs, audio-reactive themes, accessible clickable controls, and provider adapters for building polished voice agent interfaces in React.
|
|
4
4
|
|
|
5
5
|
<p align="center">
|
|
6
6
|
<a href="https://orb-ui.com">
|
|
@@ -41,13 +41,16 @@ npm install orb-ui @vapi-ai/web
|
|
|
41
41
|
|
|
42
42
|
# ElevenLabs Conversational AI
|
|
43
43
|
npm install orb-ui @elevenlabs/client
|
|
44
|
+
|
|
45
|
+
# LiveKit Agents
|
|
46
|
+
npm install orb-ui livekit-client
|
|
44
47
|
```
|
|
45
48
|
|
|
46
49
|
> **Note:** Orb uses React hooks internally — in Next.js App Router, use it in a `'use client'` component.
|
|
47
50
|
|
|
48
51
|
## Quick Start
|
|
49
52
|
|
|
50
|
-
Use orb-ui as a React voice AI component when you need a Vapi voice UI, an ElevenLabs voice UI, or a custom animated voice orb for another realtime voice agent stack.
|
|
53
|
+
Use orb-ui as a React voice AI component when you need a Vapi voice UI, an ElevenLabs voice UI, a LiveKit voice UI, or a custom animated voice orb for another realtime voice agent stack.
|
|
51
54
|
|
|
52
55
|
### With Vapi
|
|
53
56
|
|
|
@@ -78,6 +81,31 @@ function App() {
|
|
|
78
81
|
}
|
|
79
82
|
```
|
|
80
83
|
|
|
84
|
+
### With LiveKit
|
|
85
|
+
|
|
86
|
+
```jsx
|
|
87
|
+
import { Room, TokenSource, createAudioAnalyser } from 'livekit-client'
|
|
88
|
+
import { Orb } from 'orb-ui'
|
|
89
|
+
import { createLiveKitAdapter } from 'orb-ui/adapters'
|
|
90
|
+
|
|
91
|
+
const adapter = createLiveKitAdapter({
|
|
92
|
+
tokenSource: TokenSource.endpoint('/api/livekit-token'),
|
|
93
|
+
tokenOptions: {
|
|
94
|
+
agentName: 'your-agent-name',
|
|
95
|
+
roomName: () => `orb-${crypto.randomUUID()}`,
|
|
96
|
+
},
|
|
97
|
+
createAudioAnalyser,
|
|
98
|
+
RoomClass: Room,
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
function App() {
|
|
102
|
+
return <Orb adapter={adapter} theme="circle" aria-label="Start LiveKit assistant" />
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
The LiveKit adapter meters the local microphone while listening and the remote agent audio while
|
|
107
|
+
speaking, so every audio-reactive theme follows the active side of the conversation.
|
|
108
|
+
|
|
81
109
|
### Controlled mode (custom integration)
|
|
82
110
|
|
|
83
111
|
```jsx
|
|
@@ -92,6 +120,16 @@ function App() {
|
|
|
92
120
|
}
|
|
93
121
|
```
|
|
94
122
|
|
|
123
|
+
Use `signal` when your integration has separate input and output levels:
|
|
124
|
+
|
|
125
|
+
```jsx
|
|
126
|
+
import { Orb } from 'orb-ui'
|
|
127
|
+
|
|
128
|
+
function App() {
|
|
129
|
+
return <Orb signal={{ state: 'speaking', outputVolume: 0.7 }} theme="circle" />
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
95
133
|
## Themes
|
|
96
134
|
|
|
97
135
|
| Theme | Description |
|
|
@@ -107,9 +145,10 @@ When an adapter or `onStart`/`onStop` handler is provided, `circle` and `bars` r
|
|
|
107
145
|
| Prop | Type | Default | Description |
|
|
108
146
|
| ------------ | ------------------------------- | --------- | ----------------------------------------------------------- |
|
|
109
147
|
| `theme` | `'debug' \| 'circle' \| 'bars'` | `'debug'` | Visual theme |
|
|
148
|
+
| `signal` | `OrbSignal` | — | Rich controlled signal with state/input/output volume |
|
|
110
149
|
| `state` | `OrbState` | `'idle'` | Conversation state (controlled mode) |
|
|
111
|
-
| `volume` | `number` | `0` | Audio volume, 0–1
|
|
112
|
-
| `adapter` | `OrbAdapter` | — | Provider adapter (manages
|
|
150
|
+
| `volume` | `number` | `0` | Audio volume, 0–1. Overrides signal/adapter volume. |
|
|
151
|
+
| `adapter` | `OrbAdapter` | — | Provider adapter (manages signal updates automatically) |
|
|
113
152
|
| `size` | `number` | `200` | Size in pixels |
|
|
114
153
|
| `className` | `string` | — | Optional class name for the rendered theme |
|
|
115
154
|
| `style` | `React.CSSProperties` | — | Optional inline styles for the rendered theme |
|
|
@@ -120,15 +159,16 @@ When an adapter or `onStart`/`onStop` handler is provided, `circle` and `bars` r
|
|
|
120
159
|
|
|
121
160
|
## States
|
|
122
161
|
|
|
123
|
-
`idle` · `connecting` · `listening` · `speaking` · `error`
|
|
162
|
+
`idle` · `connecting` · `listening` · `thinking` · `speaking` · `error`
|
|
124
163
|
|
|
125
164
|
## Supported Providers
|
|
126
165
|
|
|
127
|
-
| Provider | Adapter
|
|
128
|
-
| ----------------------------------------------------- |
|
|
129
|
-
| [Vapi](https://vapi.ai) | `createVapiAdapter` from `orb-ui/adapters`
|
|
130
|
-
| [ElevenLabs](https://elevenlabs.io/conversational-ai) | `createElevenLabsAdapter` from `orb-ui/adapters`
|
|
131
|
-
|
|
|
166
|
+
| Provider | Adapter |
|
|
167
|
+
| ----------------------------------------------------- | --------------------------------------------------------------------- |
|
|
168
|
+
| [Vapi](https://vapi.ai) | `createVapiAdapter` from `orb-ui/adapters` |
|
|
169
|
+
| [ElevenLabs](https://elevenlabs.io/conversational-ai) | `createElevenLabsAdapter` from `orb-ui/adapters` |
|
|
170
|
+
| [LiveKit](https://livekit.io) | `createLiveKitAdapter` from `orb-ui/adapters` |
|
|
171
|
+
| Custom | Use controlled mode — pass `signal`, or `state` and `volume` directly |
|
|
132
172
|
|
|
133
173
|
## Development
|
|
134
174
|
|
package/dist/adapters/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { createVapiAdapter } from './vapi/index.js';
|
|
2
2
|
export { createElevenLabsAdapter, type ElevenLabsCallbacks, type ElevenLabsConfig, type ElevenLabsConnectionType, type ElevenLabsConversation, type ElevenLabsConversationClass, type ElevenLabsMode, type ElevenLabsOrbAdapter, type ElevenLabsStartSessionOptions, type ElevenLabsStatus, } from './elevenlabs/index.js';
|
|
3
|
-
export type
|
|
3
|
+
export { createLiveKitAdapter, type LiveKitAdapterConfig, type LiveKitConnectionDetails, type LiveKitOrbAdapter, type LiveKitResolvedTokenOptions, type LiveKitTokenOptions, type LiveKitTokenSource, } from './livekit/index.js';
|
|
4
|
+
export type { AdapterCallbacks, LegacyOrbAdapter, OrbAdapter, OrbSignal, OrbSignalListener, OrbState, } from './types.js';
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { OrbAdapter } from '../types.js';
|
|
2
|
+
interface LKAnalyserResult {
|
|
3
|
+
calculateVolume: () => number;
|
|
4
|
+
cleanup: () => Promise<void>;
|
|
5
|
+
}
|
|
6
|
+
interface LKAudioTrack {
|
|
7
|
+
mediaStreamTrack: MediaStreamTrack;
|
|
8
|
+
}
|
|
9
|
+
interface LKRemoteAudioTrack extends LKAudioTrack {
|
|
10
|
+
attach(): HTMLMediaElement;
|
|
11
|
+
detach(): HTMLMediaElement[];
|
|
12
|
+
}
|
|
13
|
+
interface LKTrackPublication<TTrack extends LKAudioTrack = LKRemoteAudioTrack> {
|
|
14
|
+
track?: TTrack;
|
|
15
|
+
source: string;
|
|
16
|
+
}
|
|
17
|
+
interface LKParticipant {
|
|
18
|
+
isLocal: boolean;
|
|
19
|
+
attributes?: Record<string, string>;
|
|
20
|
+
getTrackPublication(source: string): LKTrackPublication | undefined;
|
|
21
|
+
kind?: number;
|
|
22
|
+
}
|
|
23
|
+
interface LKLocalParticipant {
|
|
24
|
+
setMicrophoneEnabled(enabled: boolean): Promise<LKTrackPublication<LKAudioTrack> | undefined>;
|
|
25
|
+
getTrackPublication?(source: string): LKTrackPublication<LKAudioTrack> | undefined;
|
|
26
|
+
}
|
|
27
|
+
interface LKRoom {
|
|
28
|
+
connect(serverUrl: string, token: string, options?: Record<string, unknown>): Promise<void>;
|
|
29
|
+
disconnect(): void;
|
|
30
|
+
on: unknown;
|
|
31
|
+
off?: unknown;
|
|
32
|
+
removeListener?: unknown;
|
|
33
|
+
remoteParticipants: Map<string, LKParticipant>;
|
|
34
|
+
localParticipant: LKLocalParticipant;
|
|
35
|
+
state: string;
|
|
36
|
+
}
|
|
37
|
+
interface LKCreateAudioAnalyser<TTrack = unknown> {
|
|
38
|
+
(track: TTrack, options?: Record<string, unknown>): LKAnalyserResult;
|
|
39
|
+
}
|
|
40
|
+
type LiveKitTokenOptionValue = string | (() => string);
|
|
41
|
+
export interface LiveKitConnectionDetails {
|
|
42
|
+
/** LiveKit server URL, e.g. "wss://your-project.livekit.cloud". */
|
|
43
|
+
serverUrl: string;
|
|
44
|
+
/** Short-lived participant token generated by LiveKit or your token endpoint. */
|
|
45
|
+
participantToken: string;
|
|
46
|
+
}
|
|
47
|
+
export interface LiveKitResolvedTokenOptions {
|
|
48
|
+
roomName?: string;
|
|
49
|
+
participantName?: string;
|
|
50
|
+
participantIdentity?: string;
|
|
51
|
+
participantMetadata?: string;
|
|
52
|
+
participantAttributes?: Record<string, string>;
|
|
53
|
+
agentName?: string;
|
|
54
|
+
agentMetadata?: string;
|
|
55
|
+
deployment?: string;
|
|
56
|
+
}
|
|
57
|
+
export interface LiveKitTokenOptions {
|
|
58
|
+
roomName?: LiveKitTokenOptionValue;
|
|
59
|
+
participantName?: LiveKitTokenOptionValue;
|
|
60
|
+
participantIdentity?: LiveKitTokenOptionValue;
|
|
61
|
+
participantMetadata?: LiveKitTokenOptionValue;
|
|
62
|
+
participantAttributes?: Record<string, string> | (() => Record<string, string>);
|
|
63
|
+
agentName?: LiveKitTokenOptionValue;
|
|
64
|
+
agentMetadata?: LiveKitTokenOptionValue;
|
|
65
|
+
deployment?: LiveKitTokenOptionValue;
|
|
66
|
+
}
|
|
67
|
+
export interface LiveKitTokenSource {
|
|
68
|
+
fetch(options?: LiveKitResolvedTokenOptions, force?: boolean): Promise<LiveKitConnectionDetails>;
|
|
69
|
+
}
|
|
70
|
+
interface LiveKitManagedBaseConfig<TTrack = unknown> {
|
|
71
|
+
/** The createAudioAnalyser function from livekit-client. */
|
|
72
|
+
createAudioAnalyser: LKCreateAudioAnalyser<TTrack>;
|
|
73
|
+
/** The Room constructor from livekit-client. */
|
|
74
|
+
RoomClass: new (...args: never[]) => LKRoom;
|
|
75
|
+
/** LiveKit room.connect options passed through unchanged. */
|
|
76
|
+
connectOptions?: Record<string, unknown>;
|
|
77
|
+
/** Enable the local microphone after connecting. Defaults to true. */
|
|
78
|
+
enableMicrophone?: boolean;
|
|
79
|
+
/** Options passed to token sources or getConnectionDetails. Values may be resolved per start. */
|
|
80
|
+
tokenOptions?: LiveKitTokenOptions;
|
|
81
|
+
}
|
|
82
|
+
/** App-managed mode: subscribe to a pre-existing Room. */
|
|
83
|
+
interface LiveKitRoomConfig<TTrack = unknown> {
|
|
84
|
+
/** A connected Room instance from livekit-client. */
|
|
85
|
+
room: LKRoom;
|
|
86
|
+
/** The createAudioAnalyser function from livekit-client. */
|
|
87
|
+
createAudioAnalyser: LKCreateAudioAnalyser<TTrack>;
|
|
88
|
+
}
|
|
89
|
+
/** Recommended mode: fetch connection details when the orb starts. */
|
|
90
|
+
interface LiveKitConnectionDetailsConfig<TTrack = unknown> extends LiveKitManagedBaseConfig<TTrack> {
|
|
91
|
+
getConnectionDetails(options: LiveKitResolvedTokenOptions): Promise<LiveKitConnectionDetails>;
|
|
92
|
+
tokenSource?: never;
|
|
93
|
+
serverUrl?: never;
|
|
94
|
+
participantToken?: never;
|
|
95
|
+
}
|
|
96
|
+
/** Convenience mode: use a LiveKit TokenSource such as TokenSource.endpoint or sandboxTokenServer. */
|
|
97
|
+
interface LiveKitTokenSourceConfig<TTrack = unknown> extends LiveKitManagedBaseConfig<TTrack> {
|
|
98
|
+
tokenSource: LiveKitTokenSource;
|
|
99
|
+
getConnectionDetails?: never;
|
|
100
|
+
serverUrl?: never;
|
|
101
|
+
participantToken?: never;
|
|
102
|
+
}
|
|
103
|
+
/** Advanced mode: pass already-generated static credentials. */
|
|
104
|
+
interface LiveKitStaticConnectionConfig<TTrack = unknown> extends LiveKitManagedBaseConfig<TTrack> {
|
|
105
|
+
/** LiveKit server URL, e.g. "wss://your-project.livekit.cloud". */
|
|
106
|
+
serverUrl: string;
|
|
107
|
+
/** Short-lived participant token. Generate this on your server. */
|
|
108
|
+
participantToken: string;
|
|
109
|
+
getConnectionDetails?: never;
|
|
110
|
+
tokenSource?: never;
|
|
111
|
+
}
|
|
112
|
+
type LiveKitManagedConfig<TTrack = unknown> = LiveKitConnectionDetailsConfig<TTrack> | LiveKitTokenSourceConfig<TTrack> | LiveKitStaticConnectionConfig<TTrack>;
|
|
113
|
+
export type LiveKitAdapterConfig<TTrack = unknown> = LiveKitRoomConfig<TTrack> | LiveKitManagedConfig<TTrack>;
|
|
114
|
+
export interface LiveKitOrbAdapter extends OrbAdapter {
|
|
115
|
+
/** Connect to the LiveKit room and start tracking agent state. */
|
|
116
|
+
start(): Promise<void>;
|
|
117
|
+
/** Disconnect from the LiveKit room. */
|
|
118
|
+
stop(): Promise<void>;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Creates an OrbAdapter for LiveKit Agents.
|
|
122
|
+
*
|
|
123
|
+
* Managed modes create and own a Room. App-managed mode listens to a Room that
|
|
124
|
+
* your app already connected. The adapter emits signal-native orb-ui state,
|
|
125
|
+
* including LiveKit `thinking` as `OrbState` `"thinking"`, local microphone
|
|
126
|
+
* audio as `inputVolume`, and agent audio as `outputVolume`.
|
|
127
|
+
*/
|
|
128
|
+
export declare function createLiveKitAdapter<TTrack = unknown>(config: LiveKitAdapterConfig<TTrack>): LiveKitOrbAdapter | OrbAdapter;
|
|
129
|
+
export {};
|
package/dist/adapters/types.d.ts
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export type {
|
|
3
|
-
export interface AdapterCallbacks {
|
|
4
|
-
onStateChange: (state: OrbState) => void;
|
|
5
|
-
onVolumeChange: (volume: number) => void;
|
|
6
|
-
}
|
|
1
|
+
import { AdapterCallbacks, LegacyOrbAdapter, OrbAdapter, OrbSignal, OrbSignalListener, OrbState } from '../components/Orb/Orb.types.js';
|
|
2
|
+
export type { AdapterCallbacks, LegacyOrbAdapter, OrbAdapter, OrbSignal, OrbSignalListener, OrbState, };
|
|
7
3
|
/**
|
|
8
|
-
* Shared mic volume curve — rescale real-world mic range to 0–1 with pow shaping
|
|
9
|
-
*
|
|
4
|
+
* Shared mic volume curve — rescale real-world mic range to 0–1 with pow shaping
|
|
5
|
+
* for custom provider adapters that need browser-side input metering.
|
|
10
6
|
*/
|
|
11
7
|
export declare function normalizeMicVolume(v: number): number;
|
|
@@ -28,8 +28,8 @@ interface VapiMessage {
|
|
|
28
28
|
* call-end → 'idle'
|
|
29
29
|
* error → 'error'
|
|
30
30
|
*
|
|
31
|
-
* Volume: raw Vapi values are normalized (noise gate + EMA) before being
|
|
32
|
-
*
|
|
31
|
+
* Volume: raw Vapi values are normalized (noise gate + EMA) before being emitted
|
|
32
|
+
* as outputVolume while the assistant is speaking.
|
|
33
33
|
*
|
|
34
34
|
* @param client - A Vapi instance from @vapi-ai/web
|
|
35
35
|
* @param options - Optional config (e.g. assistantId to pass to vapi.start())
|
package/dist/adapters.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=.12;function t(e){let t=`idle`,n=null;function r(r){if(n&&=(clearTimeout(n),null),t===`speaking`&&r===`listening`){n=setTimeout(()=>{t=r,e(r),n=null},350);return}t=r,e(r)}function i(){n&&=(clearTimeout(n),null)}return{emitState:r,clearTimer:i}}function n(n,r){let i=
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=.12;function t(e){let t=`idle`,n=null;function r(r){if(n&&=(clearTimeout(n),null),t===`speaking`&&r===`listening`){n=setTimeout(()=>{t=r,e(r),n=null},350);return}t=r,e(r)}function i(){n&&=(clearTimeout(n),null)}return{emitState:r,clearTimer:i}}function n(n,r){let i=new Set,a=null;function o(){a||(a=n.start.bind(n),n.start=async(...e)=>(i.forEach(e=>e()),a(...e)))}function s(){i.size>0||!a||(n.start=a,a=null)}return{async start(){await n.start(r?.assistantId)},stop(){n.stop()},subscribe(r){let a={state:`idle`,volume:0,outputVolume:0},c=0;function l(t){let n=t<e?0:(t-e)/(1-e),r=n>c?.8:.5;return c+=(n-c)*r,c}function u(e){a=e,r(e)}function d(e){u({...a,...e,state:e.state??a.state})}let{emitState:f,clearTimer:p}=t(e=>d({state:e})),m=()=>f(`connecting`),h=`idle`,g=!1,_=()=>{g=!0,h=`listening`,f(`listening`),d({volume:0,outputVolume:0})},v=()=>{g=!1,h=`idle`,E(),f(`idle`),d({volume:0,outputVolume:0}),c=0},y=()=>{g&&(h=`speaking`,f(`speaking`),d({volume:0,outputVolume:0}),T())},b=()=>{g&&(E(),h=`listening`,d({volume:0,outputVolume:0}),c=0,f(`listening`))},x=0,S=0,C=0,w=()=>{h===`speaking`&&(S+=(x-S)*.1,d({volume:S,outputVolume:S})),C=requestAnimationFrame(w)},T=()=>{C||=requestAnimationFrame(w)},E=()=>{C&&=(cancelAnimationFrame(C),0),S=0,x=0},D=e=>{if(h===`speaking`){let t=l(e);x=t/(t+.3)}},O=()=>{},k=e=>{console.error(`[orb-ui/vapi] Error:`,e),g=!1,h=`error`,p(),E(),d({state:`error`,volume:0,outputVolume:0,error:e}),c=0};return n.on(`call-start`,_),n.on(`call-end`,v),n.on(`speech-start`,y),n.on(`speech-end`,b),n.on(`volume-level`,D),n.on(`message`,O),n.on(`error`,k),i.add(m),o(),()=>{p(),E(),c=0,n.removeListener(`call-start`,_),n.removeListener(`call-end`,v),n.removeListener(`speech-start`,y),n.removeListener(`speech-end`,b),n.removeListener(`volume-level`,D),n.removeListener(`message`,O),n.removeListener(`error`,k),i.delete(m),s()}}}}function r(e,t){let n=null,r=0,i=null,a=null,o={state:`idle`,volume:0,inputVolume:0,outputVolume:0},s=`idle`,c=new Set;function l(e){o=e,c.forEach(t=>t(e))}function u(e){o={...o,...e,state:e.state??o.state},l(o)}function d(e){s=e,u({state:e})}function f(){a||=setInterval(()=>{if(n){if(s===`listening`){let e=Math.min(n.getInputVolume()*2,1);u({volume:e,inputVolume:e})}else if(s===`speaking`){let e=Math.min(n.getOutputVolume()*2,1);u({volume:e,outputVolume:e})}}},33)}function p(){a&&=(clearInterval(a),null)}function m(){p(),u({volume:0,inputVolume:0,outputVolume:0})}function h(){s=`idle`,u({state:`idle`,volume:0,inputVolume:0,outputVolume:0})}function g(e){return e===r}function _(e){return{onStatusChange:({status:t})=>{g(e)&&t===`connecting`&&d(`connecting`)},onConnect:()=>{g(e)&&(d(`listening`),f())},onModeChange:({mode:t})=>{g(e)&&(t===`speaking`?(d(`speaking`),f()):(d(`listening`),f()))},onDisconnect:()=>{g(e)&&(p(),n=null,h())},onError:t=>{g(e)&&(console.error(`[orb-ui/elevenlabs] Error:`,t),p(),s=`error`,u({state:`error`,volume:0,inputVolume:0,outputVolume:0,error:t}),n=null)}}}return{subscribe(e){return c.add(e),e(o),n&&(s===`listening`||s===`speaking`)&&f(),()=>{c.delete(e),c.size===0&&m()}},async start(){if(n||i)return i??void 0;r+=1;let a=r;return i=(async()=>{try{let r=await e.startSession({...t,..._(a)});if(!g(a)){await r.endSession().catch(e=>{console.error(`[orb-ui/elevenlabs] stale session cleanup failed:`,e)});return}n=r}catch(e){if(!g(a))return;console.error(`[orb-ui/elevenlabs] startSession failed:`,e),d(`error`),u({volume:0,inputVolume:0,outputVolume:0,error:e})}finally{i=null}})(),i},async stop(){let e=n;if(!e){i&&(r+=1,p(),h(),await i);return}let t=r;p();try{await e.endSession()}catch(e){console.error(`[orb-ui/elevenlabs] endSession failed:`,e)}n===e&&g(t)&&(n=null,h())}}}function i(e){let t=Math.min(e/.5,1);return t**=1.3,t}var a=4,o=.03,s=1.5,c=.6,l=.2,u=.35;function d(e){switch(e){case`speaking`:return`speaking`;case`thinking`:return`thinking`;case`listening`:case`idle`:return`listening`;case`connecting`:case`pre-connect-buffering`:case`initializing`:return`connecting`;case`disconnected`:return`idle`;case`failed`:return`error`;default:return`idle`}}function f(e){return`RoomClass`in e}function p(e){return typeof e==`function`?e():e}function m(e){if(!e)return{};let t={},n=(e,n)=>{n!==void 0&&(t[e]=n)};return n(`roomName`,p(e.roomName)),n(`participantName`,p(e.participantName)),n(`participantIdentity`,p(e.participantIdentity)),n(`participantMetadata`,p(e.participantMetadata)),n(`participantAttributes`,typeof e.participantAttributes==`function`?e.participantAttributes():e.participantAttributes),n(`agentName`,p(e.agentName)),n(`agentMetadata`,p(e.agentMetadata)),n(`deployment`,p(e.deployment)),t}function h(e){if(!e.serverUrl||!e.participantToken)throw Error(`[orb-ui/livekit] connection details must include serverUrl and participantToken.`);return e}async function g(e){let t=m(e.tokenOptions);return typeof e.getConnectionDetails==`function`?h(await e.getConnectionDetails(t)):e.tokenSource?h(await e.tokenSource.fetch(t)):h({serverUrl:e.serverUrl,participantToken:e.participantToken})}function _(e){for(let[,t]of e.remoteParticipants)if(!t.isLocal&&(t.kind===a||t.attributes?.[`lk.agent.state`]!==void 0))return t;return null}function v(e,t,n){let r=e.off;if(r){r.call(e,t,n);return}e.removeListener?.call(e,t,n)}function y(e,t,n){e.on.call(e,t,n)}function b(e){let t=f(e),n=e.createAudioAnalyser,r={current:t?null:e.room},p=new Set,m={state:`idle`,volume:0,inputVolume:0,outputVolume:0},h=`idle`,b=null,x=null,S=null,C=null,w=null,T=null,E=null,D=0,O=null;function k(e){m=e,p.forEach(t=>t(e))}function A(e){k({...m,...e,state:e.state??m.state})}function j(e){let t=e<o?0:e,n=t>D?c:l;D+=(t-D)*n;let r=Math.min(D*s,1);return r/(r+u)}function M(){D=0,A({volume:0,outputVolume:0})}function N({emitZero:e=!0}={}){w&&=(clearInterval(w),null),C&&=(C.cleanup().catch(()=>{}),null),e&&A({volume:0,inputVolume:0})}function P(e){if(!(b===e&&C)){N({emitZero:!1}),b=e;try{C=n(e)}catch(e){console.warn(`[orb-ui/livekit] local createAudioAnalyser failed:`,e);return}w=setInterval(()=>{if(!C||h!==`listening`)return;let e=i(C.calculateVolume());A({volume:e,inputVolume:e,outputVolume:0})},33)}}function F({emitZero:e=!0}={}){E&&=(clearInterval(E),null),T&&=(T.cleanup().catch(()=>{}),null),D=0,e&&A({volume:0,outputVolume:0})}function I(e){F();try{T=n(e)}catch(e){console.warn(`[orb-ui/livekit] remote createAudioAnalyser failed:`,e);return}E=setInterval(()=>{if(!T||h!==`speaking`)return;let e=j(T.calculateVolume());A({volume:e,inputVolume:0,outputVolume:e})},33)}function L(e){h=e,A({state:e,volume:0,inputVolume:0,outputVolume:0}),e===`speaking`&&x?I(x):e!==`speaking`&&F({emitZero:!1})}function R(e){S||typeof document>`u`||(S=e.attach(),S.id=`orb-ui-livekit-agent-audio`,document.body.appendChild(S))}function z(){S&&=(S.remove(),null)}function B(e){x=e,R(e),h===`speaking`&&I(e)}function V(e){P(e)}function H(e){let t=e.getTrackPublication?.(`microphone`);t?.track&&V(t.track)}function U(e){let t=d(e);t!==h&&L(t)}function W(e){let t=e.attributes?.[`lk.agent.state`];t&&U(t);let n=e.getTrackPublication(`microphone`);n?.track&&B(n.track)}function G(e){if(O)return;let t=(e,t)=>{if(t.isLocal)return;let n=e[`lk.agent.state`];n&&U(n)},n=(e,t,n)=>{n.isLocal||!(n.kind===a||n.attributes?.[`lk.agent.state`]!==void 0)||e.mediaStreamTrack.kind!==`audio`||B(e)},r=e=>{x===e&&(x=null,z(),F())},i=e=>{e.source!==`microphone`||!e.track||V(e.track)},o=e=>{e.source===`microphone`&&(b=null,N())},s=()=>{b=null,x=null,z(),N({emitZero:!1}),F({emitZero:!1}),L(`idle`)};if(y(e,`participantAttributesChanged`,t),y(e,`trackSubscribed`,n),y(e,`trackUnsubscribed`,r),y(e,`localTrackPublished`,i),y(e,`localTrackUnpublished`,o),y(e,`disconnected`,s),O=()=>{v(e,`participantAttributesChanged`,t),v(e,`trackSubscribed`,n),v(e,`trackUnsubscribed`,r),v(e,`localTrackPublished`,i),v(e,`localTrackUnpublished`,o),v(e,`disconnected`,s)},H(e.localParticipant),e.state===`connected`){let t=_(e);t?W(t):L(`connecting`)}else L(`idle`)}function K(){O?.(),O=null,b=null,x=null,z(),N({emitZero:!1}),F({emitZero:!1}),A({volume:0,inputVolume:0,outputVolume:0})}let q=e=>{p.add(e),e(m);let t=r.current;return t&&G(t),()=>{p.delete(e),p.size===0&&K()}};return t?{subscribe:q,async start(){if(r.current)return;let t=e,n=new t.RoomClass;L(`connecting`);try{let e=await g(t);if(await n.connect(e.serverUrl,e.participantToken,t.connectOptions),r.current=n,G(n),t.enableMicrophone!==!1){let e=(await n.localParticipant.setMicrophoneEnabled(!0))?.track??n.localParticipant.getTrackPublication?.(`microphone`)?.track;e&&V(e)}}catch(e){throw console.error(`[orb-ui/livekit] connect failed:`,e),r.current===n&&K(),n.disconnect(),r.current=null,L(`error`),M(),A({error:e}),e}},async stop(){let e=r.current;if(!e){L(`idle`);return}K(),e.disconnect(),r.current=null,L(`idle`)}}:{subscribe:q}}exports.createElevenLabsAdapter=r,exports.createLiveKitAdapter=b,exports.createVapiAdapter=n;
|