orb-ui 0.1.0 → 0.2.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 CHANGED
@@ -2,11 +2,21 @@
2
2
 
3
3
  The simplest way to add voice UI to your React app. One install, one component, works with Vapi and ElevenLabs out of the box.
4
4
 
5
+ <p align="center">
6
+ <a href="https://orb-ui-demo.vercel.app">
7
+ <img src="assets/demo-circle.jpg" alt="orb-ui demo — circle theme" width="600" />
8
+ </a>
9
+ </p>
10
+
11
+ <p align="center">
12
+ <a href="https://orb-ui-demo.vercel.app">Live Demo</a> · <a href="https://www.npmjs.com/package/orb-ui">npm</a> · <a href="https://github.com/alexanderqchen/orb-ui">GitHub</a>
13
+ </p>
14
+
5
15
  ```jsx
6
- import { VoiceOrb } from 'orb-ui'
16
+ import { Orb } from 'orb-ui'
7
17
  import { createVapiAdapter } from 'orb-ui/adapters'
8
18
 
9
- <VoiceOrb adapter={createVapiAdapter(vapi, { assistantId: 'your-id' })} theme="circle" />
19
+ <Orb adapter={createVapiAdapter(vapi, { assistantId: 'your-id' })} theme="circle" />
10
20
  ```
11
21
 
12
22
  ## Install
@@ -17,20 +27,22 @@ npm install orb-ui
17
27
  yarn add orb-ui
18
28
  ```
19
29
 
30
+ > **Note:** Orb uses React hooks internally — in Next.js App Router, use it in a `'use client'` component.
31
+
20
32
  ## Quick Start
21
33
 
22
34
  ### With Vapi
23
35
 
24
36
  ```jsx
25
37
  import Vapi from '@vapi-ai/web'
26
- import { VoiceOrb } from 'orb-ui'
38
+ import { Orb } from 'orb-ui'
27
39
  import { createVapiAdapter } from 'orb-ui/adapters'
28
40
 
29
41
  const vapi = new Vapi('your-public-key')
30
42
  const adapter = createVapiAdapter(vapi, { assistantId: 'your-assistant-id' })
31
43
 
32
44
  function App() {
33
- return <VoiceOrb adapter={adapter} theme="circle" />
45
+ return <Orb adapter={adapter} theme="circle" />
34
46
  }
35
47
  ```
36
48
 
@@ -38,27 +50,27 @@ function App() {
38
50
 
39
51
  ```jsx
40
52
  import { Conversation } from '@elevenlabs/client'
41
- import { VoiceOrb } from 'orb-ui'
53
+ import { Orb } from 'orb-ui'
42
54
  import { createElevenLabsAdapter } from 'orb-ui/adapters'
43
55
 
44
56
  const adapter = createElevenLabsAdapter(Conversation, { agentId: 'your-agent-id' })
45
57
 
46
58
  function App() {
47
- return <VoiceOrb adapter={adapter} theme="circle" />
59
+ return <Orb adapter={adapter} theme="circle" />
48
60
  }
49
61
  ```
50
62
 
51
63
  ### Controlled mode (custom integration)
52
64
 
53
65
  ```jsx
54
- import { VoiceOrb } from 'orb-ui'
66
+ import { Orb } from 'orb-ui'
55
67
  import { useState } from 'react'
56
68
 
57
69
  function App() {
58
70
  const [state, setState] = useState('idle')
59
71
  const [volume, setVolume] = useState(0)
60
72
 
61
- return <VoiceOrb state={state} volume={volume} theme="circle" />
73
+ return <Orb state={state} volume={volume} theme="circle" />
62
74
  }
63
75
  ```
64
76
 
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const b=.12;function C(t){let l="idle",e=null;function i(n){if(e&&(clearTimeout(e),e=null),l==="speaking"&&n==="listening"){e=setTimeout(()=>{l=n,t(n),e=null},350);return}l=n,t(n)}function s(){e&&(clearTimeout(e),e=null)}return{emitState:i,clearTimer:s}}function I(t,l){let e=0;function i(s){const n=s<b?0:(s-b)/(1-b),o=n>e?.8:.5;return e=e+(n-e)*o,e}return{async start(){await t.start(l==null?void 0:l.assistantId)},stop(){t.stop()},subscribe({onStateChange:s,onVolumeChange:n}){const{emitState:o,clearTimer:g}=C(s);let a="idle",c=!1;const r=()=>{c=!0,a="listening",o("listening")},u=()=>{c=!1,a="idle",p(),o("idle"),n(0),e=0},S=()=>{c&&(a="speaking",o("speaking"),T())},L=()=>{c&&(p(),a="listening",o("listening"))};let v=0,d=0,m=0;const E=()=>{a==="speaking"&&(d+=(v-d)*.1,n(d)),m=requestAnimationFrame(E)},T=()=>{m||(m=requestAnimationFrame(E))},p=()=>{m&&(cancelAnimationFrame(m),m=0),d=0,v=0},V=f=>{if(a==="speaking"){const A=i(f);v=A/(A+.3)}},h=f=>{},k=f=>{console.error("[orb-ui/vapi] Error:",f),p(),o("error"),n(0),e=0};t.on("call-start",r),t.on("call-end",u),t.on("speech-start",S),t.on("speech-end",L),t.on("volume-level",V),t.on("message",h),t.on("error",k);const y=t.start.bind(t);return t.start=async(...f)=>(o("connecting"),y(...f)),()=>{g(),t.removeListener("call-start",r),t.removeListener("call-end",u),t.removeListener("speech-start",S),t.removeListener("speech-end",L),t.removeListener("volume-level",V),t.removeListener("message",h),t.removeListener("error",k),t.start=y}}}}function O(t,l){let e=null,i=null;const s=new Set;function n(r){s.forEach(u=>u.onStateChange(r))}function o(r){s.forEach(u=>u.onVolumeChange(r))}function g(){i||(i=setInterval(()=>{if(!e)return;const r=e.getOutputVolume();o(Math.min(r*2,1))},33))}function a(){i&&(clearInterval(i),i=null),o(0)}const c={onStatusChange:({status:r})=>{r==="connecting"&&n("connecting")},onConnect:()=>{n("listening")},onModeChange:({mode:r})=>{r==="speaking"?(n("speaking"),g()):(n("listening"),a())},onDisconnect:()=>{a(),n("idle"),o(0),e=null},onError:r=>{console.error("[orb-ui/elevenlabs] Error:",r),a(),n("error"),o(0),e=null}};return{subscribe(r){return s.add(r),()=>{s.delete(r),s.size===0&&a()}},async start(){if(!e)try{e=await t.startSession({...l,...c})}catch(r){console.error("[orb-ui/elevenlabs] startSession failed:",r),n("error"),o(0)}},async stop(){if(e){a();try{await e.endSession()}catch(r){console.error("[orb-ui/elevenlabs] endSession failed:",r)}e=null}}}}exports.createElevenLabsAdapter=O;exports.createVapiAdapter=I;
@@ -8,14 +8,14 @@ export declare interface AdapterCallbacks {
8
8
  *
9
9
  * Unlike createVapiAdapter, this adapter owns the session lifecycle because
10
10
  * ElevenLabs requires callbacks to be injected at Conversation.startSession()
11
- * time. Use the returned start() and stop() methods with VoiceOrb's props.
11
+ * time. Use the returned start() and stop() methods with Orb's props.
12
12
  *
13
13
  * @param ConversationClass - The Conversation class from @elevenlabs/client
14
14
  * @param config - agentId / signedUrl + any other startSession options
15
15
  *
16
16
  * @example
17
17
  * import { Conversation } from '@elevenlabs/client'
18
- * import { VoiceOrb } from 'orb-ui'
18
+ * import { Orb } from 'orb-ui'
19
19
  * import { createElevenLabsAdapter } from 'orb-ui/adapters'
20
20
  *
21
21
  * const adapter = createElevenLabsAdapter(Conversation, {
@@ -24,7 +24,7 @@ export declare interface AdapterCallbacks {
24
24
  *
25
25
  * function App() {
26
26
  * return (
27
- * <VoiceOrb
27
+ * <Orb
28
28
  * adapter={adapter}
29
29
  * theme="circle"
30
30
  * onStart={() => adapter.start()}
@@ -75,9 +75,9 @@ declare interface ElevenLabsConversationClass {
75
75
  declare type ElevenLabsMode = 'speaking' | 'listening';
76
76
 
77
77
  export declare interface ElevenLabsOrbAdapter extends OrbAdapter {
78
- /** Call this from VoiceOrb's onStart prop to begin a conversation. */
78
+ /** Call this from Orb's onStart prop to begin a conversation. */
79
79
  start(): Promise<void>;
80
- /** Call this from VoiceOrb's onStop prop to end the current conversation. */
80
+ /** Call this from Orb's onStop prop to end the current conversation. */
81
81
  stop(): Promise<void>;
82
82
  }
83
83
 
@@ -92,13 +92,13 @@ export declare interface OrbAdapter {
92
92
  onStateChange: (state: OrbState) => void;
93
93
  onVolumeChange: (volume: number) => void;
94
94
  }): () => void;
95
- /** Start the voice session. Called internally by VoiceOrb on click. */
95
+ /** Start the voice session. Called internally by Orb on click. */
96
96
  start?: () => void | Promise<void>;
97
- /** Stop the voice session. Called internally by VoiceOrb on click. */
97
+ /** Stop the voice session. Called internally by Orb on click. */
98
98
  stop?: () => void | Promise<void>;
99
99
  }
100
100
 
101
- export declare type OrbState = 'idle' | 'connecting' | 'listening' | 'thinking' | 'speaking' | 'error' | 'disconnected';
101
+ export declare type OrbState = 'idle' | 'connecting' | 'listening' | 'speaking' | 'error';
102
102
 
103
103
  /**
104
104
  * Creates an OrbAdapter for Vapi voice agents.
@@ -106,10 +106,9 @@ export declare type OrbState = 'idle' | 'connecting' | 'listening' | 'thinking'
106
106
  * State mapping:
107
107
  * vapi.start() called (intercepted) → 'connecting'
108
108
  * call-start → 'listening'
109
- * message (final user transcript) → 'thinking'
110
109
  * speech-start → 'speaking'
111
110
  * speech-end → 'listening' (debounced 350 ms)
112
- * call-end → 'disconnected'
111
+ * call-end → 'idle'
113
112
  * error → 'error'
114
113
  *
115
114
  * Volume: raw Vapi values are normalized (noise gate + EMA) before being
package/dist/adapters.js CHANGED
@@ -1,94 +1,88 @@
1
- function h(e) {
2
- let l = "idle", t = null;
3
- function s(i) {
4
- if (t && (clearTimeout(t), t = null), l === "speaking" && i === "listening") {
5
- t = setTimeout(() => {
6
- l = i, e(i), t = null;
1
+ function F(t) {
2
+ let l = "idle", e = null;
3
+ function i(n) {
4
+ if (e && (clearTimeout(e), e = null), l === "speaking" && n === "listening") {
5
+ e = setTimeout(() => {
6
+ l = n, t(n), e = null;
7
7
  }, 350);
8
8
  return;
9
9
  }
10
- l = i, e(i);
10
+ l = n, t(n);
11
11
  }
12
- function c() {
13
- t && (clearTimeout(t), t = null);
12
+ function s() {
13
+ e && (clearTimeout(e), e = null);
14
14
  }
15
- return { emitState: s, clearTimer: c };
15
+ return { emitState: i, clearTimer: s };
16
16
  }
17
- function I(e, l) {
18
- var m;
19
- let t = 0;
20
- function s(a) {
21
- const n = a < 0.12 ? 0 : (a - 0.12) / 0.88, o = n > t ? 0.65 : 0.12;
22
- return t = t + (n - t) * o, t;
23
- }
24
- let c = null;
25
- if (typeof navigator < "u" && ((m = navigator.mediaDevices) != null && m.getUserMedia)) {
26
- const a = navigator.mediaDevices.getUserMedia.bind(navigator.mediaDevices);
27
- navigator.mediaDevices.getUserMedia = async (n) => {
28
- const o = await a(n);
29
- return n != null && n.audio && (c = o), o;
30
- };
31
- }
32
- function i() {
33
- c == null || c.getTracks().forEach((a) => {
34
- a.readyState === "live" && a.stop();
35
- }), c = null;
17
+ function y(t, l) {
18
+ let e = 0;
19
+ function i(s) {
20
+ const n = s < 0.12 ? 0 : (s - 0.12) / 0.88, o = n > e ? 0.8 : 0.5;
21
+ return e = e + (n - e) * o, e;
36
22
  }
37
23
  return {
38
24
  async start() {
39
- await e.start(l == null ? void 0 : l.assistantId);
25
+ await t.start(l == null ? void 0 : l.assistantId);
40
26
  },
41
27
  stop() {
42
- e.stop();
28
+ t.stop();
43
29
  },
44
- subscribe({ onStateChange: a, onVolumeChange: n }) {
45
- const { emitState: o, clearTimer: g } = h(a), d = () => o("listening"), v = () => {
46
- o("disconnected"), n(0), t = 0, i();
47
- }, r = () => o("speaking"), f = () => {
48
- o("listening"), n(0);
49
- }, E = (u) => {
50
- n(s(u));
51
- }, S = (u) => {
52
- u.type === "transcript" && u.transcriptType === "final" && u.role === "user" && (o("thinking"), n(0));
53
- }, O = (u) => {
54
- console.error("[orb-ui/vapi] Error:", u), o("error"), n(0), t = 0, i();
30
+ subscribe({ onStateChange: s, onVolumeChange: n }) {
31
+ const { emitState: o, clearTimer: d } = F(s);
32
+ let a = "idle", u = !1;
33
+ const r = () => {
34
+ u = !0, a = "listening", o("listening");
35
+ }, c = () => {
36
+ u = !1, a = "idle", p(), o("idle"), n(0), e = 0;
37
+ }, S = () => {
38
+ u && (a = "speaking", o("speaking"), I());
39
+ }, L = () => {
40
+ u && (p(), a = "listening", o("listening"));
55
41
  };
56
- e.on("call-start", d), e.on("call-end", v), e.on("speech-start", r), e.on("speech-end", f), e.on("volume-level", E), e.on("message", S), e.on("error", O);
57
- const b = e.start.bind(e);
58
- return e.start = async (...u) => (o("connecting"), b(...u)), () => {
59
- g(), e.removeListener("call-start", d), e.removeListener("call-end", v), e.removeListener("speech-start", r), e.removeListener("speech-end", f), e.removeListener("volume-level", E), e.removeListener("message", S), e.removeListener("error", O), e.start = b, i();
42
+ let v = 0, g = 0, m = 0;
43
+ const b = () => {
44
+ a === "speaking" && (g += (v - g) * 0.1, n(g)), m = requestAnimationFrame(b);
45
+ }, I = () => {
46
+ m || (m = requestAnimationFrame(b));
47
+ }, p = () => {
48
+ m && (cancelAnimationFrame(m), m = 0), g = 0, v = 0;
49
+ }, E = (f) => {
50
+ if (a === "speaking") {
51
+ const k = i(f);
52
+ v = k / (k + 0.3);
53
+ }
54
+ }, O = (f) => {
55
+ }, h = (f) => {
56
+ console.error("[orb-ui/vapi] Error:", f), p(), o("error"), n(0), e = 0;
57
+ };
58
+ t.on("call-start", r), t.on("call-end", c), t.on("speech-start", S), t.on("speech-end", L), t.on("volume-level", E), t.on("message", O), t.on("error", h);
59
+ const V = t.start.bind(t);
60
+ return t.start = async (...f) => (o("connecting"), V(...f)), () => {
61
+ d(), t.removeListener("call-start", r), t.removeListener("call-end", c), t.removeListener("speech-start", S), t.removeListener("speech-end", L), t.removeListener("volume-level", E), t.removeListener("message", O), t.removeListener("error", h), t.start = V;
60
62
  };
61
63
  }
62
64
  };
63
65
  }
64
- const y = 2.7, p = 0.05, M = 0.5, T = 0.15;
65
- function L() {
66
- let e = 0;
67
- return function(t) {
68
- const s = t > e ? M : T;
69
- return e = e + (t - e) * s, e;
70
- };
71
- }
72
- function _(e, l) {
73
- let t = null, s = null, c = "listening";
74
- const i = L(), m = L(), a = /* @__PURE__ */ new Set();
66
+ function A(t, l) {
67
+ let e = null, i = null;
68
+ const s = /* @__PURE__ */ new Set();
75
69
  function n(r) {
76
- a.forEach((f) => f.onStateChange(r));
70
+ s.forEach((c) => c.onStateChange(r));
77
71
  }
78
72
  function o(r) {
79
- a.forEach((f) => f.onVolumeChange(r));
73
+ s.forEach((c) => c.onVolumeChange(r));
80
74
  }
81
- function g() {
82
- s || (s = setInterval(() => {
83
- if (!t) return;
84
- const r = t.getOutputVolume();
85
- o(i(Math.min(1, r * y)));
75
+ function d() {
76
+ i || (i = setInterval(() => {
77
+ if (!e) return;
78
+ const r = e.getOutputVolume();
79
+ o(Math.min(r * 2, 1));
86
80
  }, 33));
87
81
  }
88
- function d() {
89
- s && (clearInterval(s), s = null), o(0);
82
+ function a() {
83
+ i && (clearInterval(i), i = null), o(0);
90
84
  }
91
- const v = {
85
+ const u = {
92
86
  onStatusChange: ({ status: r }) => {
93
87
  r === "connecting" && n("connecting");
94
88
  },
@@ -96,54 +90,48 @@ function _(e, l) {
96
90
  n("listening");
97
91
  },
98
92
  onModeChange: ({ mode: r }) => {
99
- c = r, r === "speaking" ? (n("speaking"), g()) : (n("listening"), d());
100
- },
101
- onVadScore: ({ vadScore: r }) => {
102
- if (c === "listening") {
103
- const f = r < p ? 0 : (r - p) / (1 - p);
104
- o(m(f));
105
- }
93
+ r === "speaking" ? (n("speaking"), d()) : (n("listening"), a());
106
94
  },
107
95
  onDisconnect: () => {
108
- d(), n("disconnected"), o(0), t = null;
96
+ a(), n("idle"), o(0), e = null;
109
97
  },
110
98
  onError: (r) => {
111
- console.error("[orb-ui/elevenlabs] Error:", r), d(), n("error"), o(0), t = null;
99
+ console.error("[orb-ui/elevenlabs] Error:", r), a(), n("error"), o(0), e = null;
112
100
  }
113
101
  };
114
102
  return {
115
103
  // ── OrbAdapter.subscribe ────────────────────────────────────────────────
116
104
  subscribe(r) {
117
- return a.add(r), () => {
118
- a.delete(r), a.size === 0 && d();
105
+ return s.add(r), () => {
106
+ s.delete(r), s.size === 0 && a();
119
107
  };
120
108
  },
121
109
  // ── Lifecycle ───────────────────────────────────────────────────────────
122
110
  async start() {
123
- if (!t)
111
+ if (!e)
124
112
  try {
125
- t = await e.startSession({
113
+ e = await t.startSession({
126
114
  ...l,
127
- ...v
115
+ ...u
128
116
  });
129
117
  } catch (r) {
130
118
  console.error("[orb-ui/elevenlabs] startSession failed:", r), n("error"), o(0);
131
119
  }
132
120
  },
133
121
  async stop() {
134
- if (t) {
135
- d();
122
+ if (e) {
123
+ a();
136
124
  try {
137
- await t.endSession();
125
+ await e.endSession();
138
126
  } catch (r) {
139
127
  console.error("[orb-ui/elevenlabs] endSession failed:", r);
140
128
  }
141
- t = null;
129
+ e = null;
142
130
  }
143
131
  }
144
132
  };
145
133
  }
146
134
  export {
147
- _ as createElevenLabsAdapter,
148
- I as createVapiAdapter
135
+ A as createElevenLabsAdapter,
136
+ y as createVapiAdapter
149
137
  };
@@ -0,0 +1,11 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("react/jsx-runtime"),t=require("react");function te(n){let i=Math.min(n/.5,1);return i=Math.pow(i,1.3),i}const re=["idle","connecting","listening","speaking","error"],U={idle:"#888",connecting:"#f0c040",listening:"#40c0f0",speaking:"#40f080",error:"#f04040"};function ce({state:n,volume:i,size:e,className:j,style:v,onStart:d,onStop:k}){return o.jsxs("div",{className:j,style:{width:e,fontFamily:"monospace",fontSize:12,background:"#111",color:"#ccc",border:"1px solid #333",borderRadius:8,padding:12,boxSizing:"border-box",userSelect:"none",...v},children:[o.jsx("div",{style:{color:"#555",marginBottom:10,fontSize:10,letterSpacing:1},children:"ORB DEBUG"}),o.jsxs("div",{style:{marginBottom:8},children:[o.jsx("span",{style:{color:"#555"},children:"state "}),o.jsx("span",{style:{color:U[n],fontWeight:"bold"},children:n})]}),o.jsxs("div",{style:{marginBottom:10},children:[o.jsx("span",{style:{color:"#555"},children:"volume "}),o.jsx("span",{style:{color:"#ccc"},children:i.toFixed(2)}),o.jsx("div",{style:{marginTop:4,height:4,background:"#222",borderRadius:2,overflow:"hidden"},children:o.jsx("div",{style:{height:"100%",width:`${i*100}%`,background:U[n],borderRadius:2,transition:"width 50ms linear"}})})]}),o.jsxs("div",{style:{marginBottom:10},children:[o.jsx("div",{style:{color:"#555",marginBottom:4,fontSize:10},children:"force state"}),o.jsx("div",{style:{display:"flex",flexWrap:"wrap",gap:4},children:re.map(r=>o.jsx("button",{style:{fontSize:10,padding:"2px 6px",background:n===r?U[r]:"#222",color:n===r?"#000":"#888",border:`1px solid ${n===r?U[r]:"#333"}`,borderRadius:3,cursor:"pointer"},onClick:()=>{console.warn(`[orb-ui debug] To force state '${r}', use controlled mode: <Orb state="${r}" />`)},children:r},r))})]}),o.jsxs("div",{style:{display:"flex",gap:6},children:[o.jsx("button",{onClick:d,style:{flex:1,padding:"4px 0",background:"#1a3a1a",color:"#40f080",border:"1px solid #40f080",borderRadius:4,cursor:"pointer",fontSize:11},children:"Start"}),o.jsx("button",{onClick:k,style:{flex:1,padding:"4px 0",background:"#3a1a1a",color:"#f04040",border:"1px solid #f04040",borderRadius:4,cursor:"pointer",fontSize:11},children:"Stop"})]})]})}function K(n){return[parseInt(n.slice(1,3),16),parseInt(n.slice(3,5),16),parseInt(n.slice(5,7),16)]}const _={idle:"#cccccc",connecting:"#cccccc",listening:"#999999",speaking:"#e8e8e8",error:"#f87171"},oe=`
2
+ @keyframes orb-circle-idle-pulse {
3
+ from { transform: scale(1); }
4
+ to { transform: scale(1.06); }
5
+ }
6
+ @keyframes orb-circle-connecting-pulse {
7
+ 0% { opacity: 1; transform: scale(1); }
8
+ 50% { opacity: 0.6; transform: scale(0.95); }
9
+ 100% { opacity: 1; transform: scale(1); }
10
+ }
11
+ `,se=.95,ie=.08,J=.82,X=.18,le=24,ue=0;function ae({state:n,volume:i,size:e,className:j,style:v,onClick:d}){const k=t.useRef(null),r=t.useRef(null),c=t.useRef(null),m=t.useRef(0),h=t.useRef(i);t.useLayoutEffect(()=>{h.current=i},[i]);const s=t.useRef(1),u=t.useRef(0),A=t.useRef(K(_.idle)),M=.06,S=t.useRef(J),g=t.useRef(X);t.useEffect(()=>{const f="orb-circle-keyframes";if(!document.getElementById(f)){const p=document.createElement("style");p.id=f,p.textContent=oe,document.head.appendChild(p)}},[]),t.useEffect(()=>{const f=k.current;if(f)if(n==="listening"||n==="speaking"){const p=n==="speaking"?se:J,I=n==="speaking"?ie:X,q=n==="speaking"?le:ue,C=()=>{const F=h.current;S.current+=(p-S.current)*M,g.current+=(I-g.current)*M;const l=S.current+F*g.current,E=F*q;n==="listening"?(s.current+=(l-s.current)*.45,u.current+=(E-u.current)*.45):(s.current=l,u.current=E);const L=K(_[n]),[D,O,P]=A.current;A.current=[D+(L[0]-D)*.05,O+(L[1]-O)*.05,P+(L[2]-P)*.05];const[N,b,a]=A.current.map(Math.round);f.style.transform=`scale(${s.current})`,f.style.background=`rgb(${N},${b},${a})`,f.style.boxShadow="none",f.style.animation="none";const R=r.current;if(R){const x=u.current;R.style.transform=`scale(${s.current})`,R.style.boxShadow=x>.5?`0 0 ${x}px ${x*.4}px rgb(${N},${b},${a})`:"none"}m.current=requestAnimationFrame(C)};return m.current=requestAnimationFrame(C),()=>{cancelAnimationFrame(m.current)}}else{cancelAnimationFrame(m.current),s.current=1,u.current=0,A.current=K(_[n]??_.idle),f.style.transform="",f.style.boxShadow="none",r.current&&(r.current.style.boxShadow="none");const p=_[n]??_.idle,[I,q,C]=K(p);f.style.background=p,n==="idle"?f.style.animation="orb-circle-idle-pulse 3s ease-in-out infinite alternate":n==="connecting"?f.style.animation="orb-circle-connecting-pulse 1.5s ease-in-out infinite":f.style.animation="none"}},[n]);const y=e*.55;return o.jsx("div",{className:j,style:{width:e,height:e,display:"flex",alignItems:"center",justifyContent:"center",position:"relative",...v},children:o.jsxs("div",{ref:c,onClick:d,onMouseEnter:()=>{c.current&&(c.current.style.transform="scale(1.06)",c.current.style.filter="brightness(1.12)")},onMouseLeave:()=>{c.current&&(c.current.style.transform="scale(1)",c.current.style.filter="brightness(1)")},onTouchEnd:()=>{setTimeout(()=>{c.current&&(c.current.style.transform="scale(1)",c.current.style.filter="brightness(1)")},200)},style:{transition:"transform 0.3s ease, filter 0.3s ease",cursor:d?"pointer":"default",borderRadius:"50%",lineHeight:0},children:[o.jsx("div",{ref:r,style:{position:"absolute",width:y,height:y,borderRadius:"50%",pointerEvents:"none"}}),o.jsx("div",{ref:k,style:{position:"relative",width:y,height:y,borderRadius:"50%",background:_[n]}})]})})}const T=5,fe=1.4,de=Math.PI*2/T,G={idle:"#cccccc",connecting:"#cccccc",listening:"#999999",speaking:"#e8e8e8",error:"#f87171"};function Z(n){return[parseInt(n.slice(1,3),16),parseInt(n.slice(3,5),16),parseInt(n.slice(5,7),16)]}function me({state:n,volume:i,size:e,className:j,style:v,onClick:d}){const k=t.useRef([]),r=t.useRef(null),c=t.useRef(0),m=t.useRef(new Array(T).fill(0)),h=t.useRef(i),s=t.useRef(!1),u=t.useRef(0),A=t.useRef(Z(G.idle)),M=300,S=t.useRef(null),g=t.useRef(new Array(T).fill(0)),y=t.useRef(n);t.useEffect(()=>{n!==y.current&&(g.current=[...m.current],S.current=Date.now(),y.current=n)},[n]),t.useEffect(()=>{h.current=i},[i]),t.useEffect(()=>{const F=e*.55,l=e*.06,E=G[n]??G.idle,L=e*.1,D=Array.from({length:T},(b,a)=>{const R=(T-1)/2;return 1-.7*(Math.abs(a-R)/R)}),O=()=>{const a=s.current?L:0;u.current+=(a-u.current)*.15},P=(b,a)=>{O();const R=Z(a),[x,w,H]=A.current;A.current=[x+(R[0]-x)*.08,w+(R[1]-w)*.08,H+(R[2]-H)*.08];const[$,Q,Y]=A.current.map(Math.round),V=`rgb(${$},${Q},${Y})`;for(let B=0;B<T;B++){const W=k.current[B];if(!W)continue;const ee=n==="idle"?D[B]:1,ne=u.current*ee;W.style.height=`${Math.min(b[B]+ne,F)}px`,W.style.background=V,W.style.animation="none"}};if(n==="listening"||n==="speaking"){const b=n==="speaking"?1:.4,a=()=>{const R=h.current,x=Date.now()/1e3;for(let w=0;w<T;w++){const H=.5+.15*Math.sin(x*fe*b*Math.PI*2+w*de);let $=l+(F-l)*R*H;if(S.current!==null){const Y=Date.now()-S.current,V=Math.min(Y/M,1),B=1-(1-V)*(1-V);$=g.current[w]+($-g.current[w])*B,V>=1&&(S.current=null)}const Q=n==="listening"?.45:1;m.current[w]+=($-m.current[w])*Q}P(m.current,E),c.current=requestAnimationFrame(a)};return c.current=requestAnimationFrame(a),()=>cancelAnimationFrame(c.current)}if(n==="connecting"){const b=Date.now(),a=()=>{const R=(Date.now()-b)/1e3;O();for(let x=0;x<T;x++){const w=(R*.6+x/T*.5)%1,H=w<.5?Math.sin(w/.5*Math.PI):0,$=l+(F*.4-l)*H;m.current[x]+=($-m.current[x])*.15}P(m.current,E),c.current=requestAnimationFrame(a)};return c.current=requestAnimationFrame(a),()=>cancelAnimationFrame(c.current)}cancelAnimationFrame(c.current);const N=()=>{O();for(let b=0;b<T;b++){const a=k.current[b];a&&(a.style.height=`${l+u.current*D[b]}px`,a.style.background=E,a.style.animation="none")}c.current=requestAnimationFrame(N)};return c.current=requestAnimationFrame(N),()=>cancelAnimationFrame(c.current)},[n,e]);const f=e*.055,p=e*.035,I=e*.03,q=e*.55,C=e*.06;return o.jsx("div",{className:j,style:{width:e,height:e,display:"flex",alignItems:"center",justifyContent:"center",position:"relative",...v},children:o.jsx("div",{ref:r,onClick:d,onMouseEnter:()=>{s.current=!0,r.current&&(r.current.style.filter="brightness(1.35)")},onMouseLeave:()=>{s.current=!1,r.current&&(r.current.style.filter="brightness(1)")},onTouchEnd:()=>{setTimeout(()=>{s.current=!1,r.current&&(r.current.style.filter="brightness(1)")},200)},style:{display:"flex",alignItems:"center",justifyContent:"center",gap:p,transition:"filter 0.3s ease",cursor:d?"pointer":"default"},children:Array.from({length:T},(F,l)=>o.jsx("div",{ref:E=>{k.current[l]=E},style:{width:f,minHeight:C,maxHeight:q,height:C,borderRadius:I,background:G[n]??G.idle}},l))})})}function ge(){var m;let n=null,i=null,e=null,j=0,v=0,d=null;if(typeof navigator<"u"&&((m=navigator.mediaDevices)!=null&&m.getUserMedia)){const h=navigator.mediaDevices.getUserMedia.bind(navigator.mediaDevices);navigator.mediaDevices.getUserMedia=async s=>{const u=await h(s);return s!=null&&s.audio&&(d=u),u}}function k(h){if(!(!d||i))try{n=new AudioContext,i=n.createAnalyser(),i.fftSize=256,e=n.createMediaStreamSource(d),e.connect(i);const s=new Uint8Array(i.frequencyBinCount),u=()=>{if(!i)return;i.getByteFrequencyData(s);let A=0;for(let g=0;g<s.length;g++)A+=s[g]*s[g];const M=Math.sqrt(A/s.length)/255,S=M>v?.7:.3;v+=(M-v)*S,h(te(v)),j=requestAnimationFrame(u)};j=requestAnimationFrame(u)}catch(s){console.warn("[orb-ui] Mic monitor failed:",s)}}function r(){cancelAnimationFrame(j),j=0,v=0,e==null||e.disconnect(),e=null,i=null}function c(){r(),n&&(n.close().catch(()=>{}),n=null),d==null||d.getTracks().forEach(h=>{h.readyState==="live"&&h.stop()}),d=null}return{start:k,stop:r,release:c}}function z({state:n,volume:i,adapter:e,theme:j="debug",size:v=200,className:d,style:k,onStart:r,onStop:c}){const[m,h]=t.useState("idle"),[s,u]=t.useState(0),[A,M]=t.useState(0),S=t.useRef(ge()),g=t.useRef(!1);t.useEffect(()=>(h("idle"),u(0),M(0),e?e.subscribe({onStateChange:h,onVolumeChange:u}):void 0),[e]);const y=n??m;t.useEffect(()=>{const l=S.current;if(y==="listening"&&e){const E=setTimeout(()=>{l.start(M),g.current=!0},500);return()=>{clearTimeout(E),l.stop(),g.current=!1,M(0)}}else l.stop(),g.current=!1,M(0)},[y,e]),t.useEffect(()=>()=>S.current.release(),[]);const f=i??(y==="listening"&&g.current?A:s),p=y!=="idle"&&y!=="error",I=t.useCallback(()=>{var l,E;p?c?c():(l=e==null?void 0:e.stop)==null||l.call(e):r?r():(E=e==null?void 0:e.start)==null||E.call(e)},[e,p,r,c]),q=!!(e!=null&&e.start||r||c),C={state:y,volume:f,size:v,className:d,style:k},F=q?I:void 0;switch(j){case"circle":return o.jsx(ae,{...C,onClick:F});case"bars":return o.jsx(me,{...C,onClick:F});case"debug":default:return o.jsx(ce,{...C,onStart:r??(()=>{var l;return(l=e==null?void 0:e.start)==null?void 0:l.call(e)}),onStop:c??(()=>{var l;return(l=e==null?void 0:e.stop)==null?void 0:l.call(e)})})}}exports.Orb=z;exports.VoiceOrb=z;
package/dist/orb-ui.d.ts CHANGED
@@ -1,5 +1,9 @@
1
1
  import { JSX as JSX_2 } from 'react/jsx-runtime';
2
2
 
3
+ declare function Orb({ state: stateProp, volume: volumeProp, adapter, theme, size, className, style, onStart, onStop, }: OrbProps): JSX_2.Element;
4
+ export { Orb }
5
+ export { Orb as VoiceOrb }
6
+
3
7
  export declare interface OrbAdapter {
4
8
  /**
5
9
  * Subscribe to state and volume changes from a voice provider.
@@ -9,19 +13,13 @@ export declare interface OrbAdapter {
9
13
  onStateChange: (state: OrbState) => void;
10
14
  onVolumeChange: (volume: number) => void;
11
15
  }): () => void;
12
- /** Start the voice session. Called internally by VoiceOrb on click. */
16
+ /** Start the voice session. Called internally by Orb on click. */
13
17
  start?: () => void | Promise<void>;
14
- /** Stop the voice session. Called internally by VoiceOrb on click. */
18
+ /** Stop the voice session. Called internally by Orb on click. */
15
19
  stop?: () => void | Promise<void>;
16
20
  }
17
21
 
18
- export declare type OrbState = 'idle' | 'connecting' | 'listening' | 'thinking' | 'speaking' | 'error' | 'disconnected';
19
-
20
- export declare type OrbTheme = 'debug' | 'circle' | 'bars';
21
-
22
- export declare function VoiceOrb({ state: stateProp, volume: volumeProp, adapter, theme, size, className, style, onStart, onStop, }: VoiceOrbProps): JSX_2.Element;
23
-
24
- export declare interface VoiceOrbProps {
22
+ declare interface OrbProps {
25
23
  /**
26
24
  * Current conversation state. Required in controlled mode (no adapter).
27
25
  * Overrides adapter state if both are provided.
@@ -48,5 +46,11 @@ export declare interface VoiceOrbProps {
48
46
  /** Called when the Stop button is clicked (debug theme only). */
49
47
  onStop?: () => void;
50
48
  }
49
+ export { OrbProps }
50
+ export { OrbProps as VoiceOrbProps }
51
+
52
+ export declare type OrbState = 'idle' | 'connecting' | 'listening' | 'speaking' | 'error';
53
+
54
+ export declare type OrbTheme = 'debug' | 'circle' | 'bars';
51
55
 
52
56
  export { }
package/dist/orb-ui.js CHANGED
@@ -1,37 +1,37 @@
1
- import { jsxs as C, jsx as i } from "react/jsx-runtime";
2
- import { useRef as y, useLayoutEffect as q, useEffect as T, useState as O, useCallback as G } from "react";
3
- const N = [
1
+ import { jsxs as N, jsx as l } from "react/jsx-runtime";
2
+ import { useRef as s, useLayoutEffect as ce, useEffect as B, useState as Z, useCallback as oe } from "react";
3
+ function ie(n) {
4
+ let o = Math.min(n / 0.5, 1);
5
+ return o = Math.pow(o, 1.3), o;
6
+ }
7
+ const se = [
4
8
  "idle",
5
9
  "connecting",
6
10
  "listening",
7
- "thinking",
8
11
  "speaking",
9
- "error",
10
- "disconnected"
11
- ], I = {
12
+ "error"
13
+ ], Q = {
12
14
  idle: "#888",
13
15
  connecting: "#f0c040",
14
16
  listening: "#40c0f0",
15
- thinking: "#c040f0",
16
17
  speaking: "#40f080",
17
- error: "#f04040",
18
- disconnected: "#555"
18
+ error: "#f04040"
19
19
  };
20
- function V({
21
- state: e,
22
- volume: u,
23
- size: n,
24
- className: x,
25
- style: S,
26
- onStart: g,
27
- onStop: a
20
+ function le({
21
+ state: n,
22
+ volume: o,
23
+ size: e,
24
+ className: k,
25
+ style: A,
26
+ onStart: d,
27
+ onStop: M
28
28
  }) {
29
- return /* @__PURE__ */ C(
29
+ return /* @__PURE__ */ N(
30
30
  "div",
31
31
  {
32
- className: x,
32
+ className: k,
33
33
  style: {
34
- width: n,
34
+ width: e,
35
35
  fontFamily: "monospace",
36
36
  fontSize: 12,
37
37
  background: "#111",
@@ -41,18 +41,18 @@ function V({
41
41
  padding: 12,
42
42
  boxSizing: "border-box",
43
43
  userSelect: "none",
44
- ...S
44
+ ...A
45
45
  },
46
46
  children: [
47
- /* @__PURE__ */ i("div", { style: { color: "#555", marginBottom: 10, fontSize: 10, letterSpacing: 1 }, children: "ORB DEBUG" }),
48
- /* @__PURE__ */ C("div", { style: { marginBottom: 8 }, children: [
49
- /* @__PURE__ */ i("span", { style: { color: "#555" }, children: "state " }),
50
- /* @__PURE__ */ i("span", { style: { color: I[e], fontWeight: "bold" }, children: e })
47
+ /* @__PURE__ */ l("div", { style: { color: "#555", marginBottom: 10, fontSize: 10, letterSpacing: 1 }, children: "ORB DEBUG" }),
48
+ /* @__PURE__ */ N("div", { style: { marginBottom: 8 }, children: [
49
+ /* @__PURE__ */ l("span", { style: { color: "#555" }, children: "state " }),
50
+ /* @__PURE__ */ l("span", { style: { color: Q[n], fontWeight: "bold" }, children: n })
51
51
  ] }),
52
- /* @__PURE__ */ C("div", { style: { marginBottom: 10 }, children: [
53
- /* @__PURE__ */ i("span", { style: { color: "#555" }, children: "volume " }),
54
- /* @__PURE__ */ i("span", { style: { color: "#ccc" }, children: u.toFixed(2) }),
55
- /* @__PURE__ */ i(
52
+ /* @__PURE__ */ N("div", { style: { marginBottom: 10 }, children: [
53
+ /* @__PURE__ */ l("span", { style: { color: "#555" }, children: "volume " }),
54
+ /* @__PURE__ */ l("span", { style: { color: "#ccc" }, children: o.toFixed(2) }),
55
+ /* @__PURE__ */ l(
56
56
  "div",
57
57
  {
58
58
  style: {
@@ -62,13 +62,13 @@ function V({
62
62
  borderRadius: 2,
63
63
  overflow: "hidden"
64
64
  },
65
- children: /* @__PURE__ */ i(
65
+ children: /* @__PURE__ */ l(
66
66
  "div",
67
67
  {
68
68
  style: {
69
69
  height: "100%",
70
- width: `${u * 100}%`,
71
- background: I[e],
70
+ width: `${o * 100}%`,
71
+ background: Q[n],
72
72
  borderRadius: 2,
73
73
  transition: "width 50ms linear"
74
74
  }
@@ -77,35 +77,35 @@ function V({
77
77
  }
78
78
  )
79
79
  ] }),
80
- /* @__PURE__ */ C("div", { style: { marginBottom: 10 }, children: [
81
- /* @__PURE__ */ i("div", { style: { color: "#555", marginBottom: 4, fontSize: 10 }, children: "force state" }),
82
- /* @__PURE__ */ i("div", { style: { display: "flex", flexWrap: "wrap", gap: 4 }, children: N.map((r) => /* @__PURE__ */ i(
80
+ /* @__PURE__ */ N("div", { style: { marginBottom: 10 }, children: [
81
+ /* @__PURE__ */ l("div", { style: { color: "#555", marginBottom: 4, fontSize: 10 }, children: "force state" }),
82
+ /* @__PURE__ */ l("div", { style: { display: "flex", flexWrap: "wrap", gap: 4 }, children: se.map((t) => /* @__PURE__ */ l(
83
83
  "button",
84
84
  {
85
85
  style: {
86
86
  fontSize: 10,
87
87
  padding: "2px 6px",
88
- background: e === r ? I[r] : "#222",
89
- color: e === r ? "#000" : "#888",
90
- border: `1px solid ${e === r ? I[r] : "#333"}`,
88
+ background: n === t ? Q[t] : "#222",
89
+ color: n === t ? "#000" : "#888",
90
+ border: `1px solid ${n === t ? Q[t] : "#333"}`,
91
91
  borderRadius: 3,
92
92
  cursor: "pointer"
93
93
  },
94
94
  onClick: () => {
95
95
  console.warn(
96
- `[orb-ui debug] To force state '${r}', use controlled mode: <VoiceOrb state="${r}" />`
96
+ `[orb-ui debug] To force state '${t}', use controlled mode: <Orb state="${t}" />`
97
97
  );
98
98
  },
99
- children: r
99
+ children: t
100
100
  },
101
- r
101
+ t
102
102
  )) })
103
103
  ] }),
104
- /* @__PURE__ */ C("div", { style: { display: "flex", gap: 6 }, children: [
105
- /* @__PURE__ */ i(
104
+ /* @__PURE__ */ N("div", { style: { display: "flex", gap: 6 }, children: [
105
+ /* @__PURE__ */ l(
106
106
  "button",
107
107
  {
108
- onClick: g,
108
+ onClick: d,
109
109
  style: {
110
110
  flex: 1,
111
111
  padding: "4px 0",
@@ -119,10 +119,10 @@ function V({
119
119
  children: "Start"
120
120
  }
121
121
  ),
122
- /* @__PURE__ */ i(
122
+ /* @__PURE__ */ l(
123
123
  "button",
124
124
  {
125
- onClick: a,
125
+ onClick: M,
126
126
  style: {
127
127
  flex: 1,
128
128
  padding: "4px 0",
@@ -141,257 +141,382 @@ function V({
141
141
  }
142
142
  );
143
143
  }
144
- function L(e) {
144
+ function Y(n) {
145
145
  return [
146
- parseInt(e.slice(1, 3), 16),
147
- parseInt(e.slice(3, 5), 16),
148
- parseInt(e.slice(5, 7), 16)
146
+ parseInt(n.slice(1, 3), 16),
147
+ parseInt(n.slice(3, 5), 16),
148
+ parseInt(n.slice(5, 7), 16)
149
149
  ];
150
150
  }
151
- const v = {
151
+ const H = {
152
152
  idle: "#cccccc",
153
153
  connecting: "#cccccc",
154
- listening: "#60a5fa",
155
- speaking: "#a3e635",
156
- thinking: "#fbbf24",
157
- error: "#f87171",
158
- disconnected: "#444444"
159
- }, W = `
154
+ listening: "#999999",
155
+ speaking: "#e8e8e8",
156
+ error: "#f87171"
157
+ }, ue = `
160
158
  @keyframes orb-circle-idle-pulse {
161
159
  from { transform: scale(1); }
162
160
  to { transform: scale(1.06); }
163
161
  }
164
162
  @keyframes orb-circle-connecting-pulse {
165
- from { transform: scale(1); }
166
- to { transform: scale(1.06); }
167
- }
168
- @keyframes orb-circle-thinking-spin {
169
- from { transform: rotate(0deg); }
170
- to { transform: rotate(360deg); }
163
+ 0% { opacity: 1; transform: scale(1); }
164
+ 50% { opacity: 0.6; transform: scale(0.95); }
165
+ 100% { opacity: 1; transform: scale(1); }
171
166
  }
172
- `, M = 0.88, K = 0.22, j = 0.9, D = 0.15, U = 16, Q = 10, H = 0.55;
173
- function Y({ state: e, volume: u, size: n, className: x, style: S }) {
174
- const g = y(null), a = y(0), r = y(u);
175
- q(() => {
176
- r.current = u;
177
- }, [u]);
178
- const f = y(1), p = y(0), A = y(L(v.idle));
179
- T(() => {
180
- const s = "orb-circle-keyframes";
181
- if (!document.getElementById(s)) {
182
- const m = document.createElement("style");
183
- m.id = s, m.textContent = W, document.head.appendChild(m);
167
+ `, ae = 0.95, fe = 0.08, z = 0.82, ee = 0.18, de = 24, me = 0;
168
+ function ge({ state: n, volume: o, size: e, className: k, style: A, onClick: d }) {
169
+ const M = s(null), t = s(null), r = s(null), m = s(0), h = s(o);
170
+ ce(() => {
171
+ h.current = o;
172
+ }, [o]);
173
+ const c = s(1), u = s(0), p = s(Y(H.idle)), T = 0.06, S = s(z), g = s(ee);
174
+ B(() => {
175
+ const f = "orb-circle-keyframes";
176
+ if (!document.getElementById(f)) {
177
+ const x = document.createElement("style");
178
+ x.id = f, x.textContent = ue, document.head.appendChild(x);
184
179
  }
185
- }, []), T(() => {
186
- const s = g.current;
187
- if (s)
188
- if (e === "listening" || e === "speaking") {
189
- const m = e === "speaking" ? M : j, $ = e === "speaking" ? K : D, h = e === "speaking" ? U : Q, c = () => {
190
- const b = r.current, R = m + b * $, l = b * h;
191
- f.current += (R - f.current) * H, p.current += (l - p.current) * H;
192
- const t = L(v[e]), [o, d, k] = A.current;
193
- A.current = [
194
- o + (t[0] - o) * 0.05,
195
- d + (t[1] - d) * 0.05,
196
- k + (t[2] - k) * 0.05
180
+ }, []), B(() => {
181
+ const f = M.current;
182
+ if (f)
183
+ if (n === "listening" || n === "speaking") {
184
+ const x = n === "speaking" ? ae : z, q = n === "speaking" ? fe : ee, L = n === "speaking" ? de : me, F = () => {
185
+ const $ = h.current;
186
+ S.current += (x - S.current) * T, g.current += (q - g.current) * T;
187
+ const i = S.current + $ * g.current, w = $ * L;
188
+ n === "listening" ? (c.current += (i - c.current) * 0.45, u.current += (w - u.current) * 0.45) : (c.current = i, u.current = w);
189
+ const P = Y(H[n]), [V, O, G] = p.current;
190
+ p.current = [
191
+ V + (P[0] - V) * 0.05,
192
+ O + (P[1] - O) * 0.05,
193
+ G + (P[2] - G) * 0.05
197
194
  ];
198
- const [_, F, B] = A.current.map(Math.round);
199
- s.style.transform = `scale(${f.current})`, s.style.background = `rgb(${_},${F},${B})`, s.style.boxShadow = `0 0 ${p.current}px ${p.current * 0.25}px rgb(${_},${F},${B})`, s.style.animation = "none", a.current = requestAnimationFrame(c);
195
+ const [W, b, a] = p.current.map(Math.round);
196
+ f.style.transform = `scale(${c.current})`, f.style.background = `rgb(${W},${b},${a})`, f.style.boxShadow = "none", f.style.animation = "none";
197
+ const v = t.current;
198
+ if (v) {
199
+ const R = u.current;
200
+ v.style.transform = `scale(${c.current})`, v.style.boxShadow = R > 0.5 ? `0 0 ${R}px ${R * 0.4}px rgb(${W},${b},${a})` : "none";
201
+ }
202
+ m.current = requestAnimationFrame(F);
200
203
  };
201
- return a.current = requestAnimationFrame(c), () => {
202
- cancelAnimationFrame(a.current);
204
+ return m.current = requestAnimationFrame(F), () => {
205
+ cancelAnimationFrame(m.current);
203
206
  };
204
- } else
205
- cancelAnimationFrame(a.current), f.current = 1, p.current = 0, A.current = L(v[e] ?? v.idle), s.style.transform = "", s.style.boxShadow = "", s.style.background = v[e] ?? v.idle, e === "idle" ? s.style.animation = "orb-circle-idle-pulse 3s ease-in-out infinite alternate" : e === "connecting" ? s.style.animation = "orb-circle-connecting-pulse 1.2s ease-in-out infinite alternate" : s.style.animation = "none";
206
- }, [e]);
207
- const w = n * 0.55;
208
- return /* @__PURE__ */ C(
207
+ } else {
208
+ cancelAnimationFrame(m.current), c.current = 1, u.current = 0, p.current = Y(H[n] ?? H.idle), f.style.transform = "", f.style.boxShadow = "none", t.current && (t.current.style.boxShadow = "none");
209
+ const x = H[n] ?? H.idle, [q, L, F] = Y(x);
210
+ f.style.background = x, n === "idle" ? f.style.animation = "orb-circle-idle-pulse 3s ease-in-out infinite alternate" : n === "connecting" ? f.style.animation = "orb-circle-connecting-pulse 1.5s ease-in-out infinite" : f.style.animation = "none";
211
+ }
212
+ }, [n]);
213
+ const y = e * 0.55;
214
+ return /* @__PURE__ */ l(
209
215
  "div",
210
216
  {
211
- className: x,
217
+ className: k,
212
218
  style: {
213
- width: n,
214
- height: n,
219
+ width: e,
220
+ height: e,
215
221
  display: "flex",
216
222
  alignItems: "center",
217
223
  justifyContent: "center",
218
224
  position: "relative",
219
- ...S
225
+ ...A
220
226
  },
221
- children: [
222
- /* @__PURE__ */ i(
223
- "div",
224
- {
225
- ref: g,
226
- style: {
227
- width: w,
228
- height: w,
229
- borderRadius: "50%",
230
- // Initial color — rAF overwrites this immediately on first frame
231
- background: v[e]
232
- }
233
- }
234
- ),
235
- e === "thinking" && /* @__PURE__ */ i("div", { style: {
236
- position: "absolute",
237
- width: n * 0.68,
238
- height: n * 0.68,
239
- border: "2px dashed #fbbf24",
240
- borderRadius: "50%",
241
- animation: "orb-circle-thinking-spin 1.5s linear infinite",
242
- pointerEvents: "none"
243
- } })
244
- ]
227
+ children: /* @__PURE__ */ N(
228
+ "div",
229
+ {
230
+ ref: r,
231
+ onClick: d,
232
+ onMouseEnter: () => {
233
+ r.current && (r.current.style.transform = "scale(1.06)", r.current.style.filter = "brightness(1.12)");
234
+ },
235
+ onMouseLeave: () => {
236
+ r.current && (r.current.style.transform = "scale(1)", r.current.style.filter = "brightness(1)");
237
+ },
238
+ onTouchEnd: () => {
239
+ setTimeout(() => {
240
+ r.current && (r.current.style.transform = "scale(1)", r.current.style.filter = "brightness(1)");
241
+ }, 200);
242
+ },
243
+ style: {
244
+ transition: "transform 0.3s ease, filter 0.3s ease",
245
+ cursor: d ? "pointer" : "default",
246
+ borderRadius: "50%",
247
+ lineHeight: 0
248
+ },
249
+ children: [
250
+ /* @__PURE__ */ l(
251
+ "div",
252
+ {
253
+ ref: t,
254
+ style: {
255
+ position: "absolute",
256
+ width: y,
257
+ height: y,
258
+ borderRadius: "50%",
259
+ pointerEvents: "none"
260
+ }
261
+ }
262
+ ),
263
+ /* @__PURE__ */ l(
264
+ "div",
265
+ {
266
+ ref: M,
267
+ style: {
268
+ position: "relative",
269
+ width: y,
270
+ height: y,
271
+ borderRadius: "50%",
272
+ background: H[n]
273
+ }
274
+ }
275
+ )
276
+ ]
277
+ }
278
+ )
245
279
  }
246
280
  );
247
281
  }
248
- const E = 5, J = 1.4, X = Math.PI * 2 / E, P = {
282
+ const C = 5, he = 1.4, be = Math.PI * 2 / C, j = {
249
283
  idle: "#cccccc",
250
284
  connecting: "#cccccc",
251
- listening: "#60a5fa",
252
- speaking: "#a3e635",
253
- thinking: "#fbbf24",
254
- error: "#f87171",
255
- disconnected: "#444444"
285
+ listening: "#999999",
286
+ speaking: "#e8e8e8",
287
+ error: "#f87171"
256
288
  };
257
- function Z(e) {
258
- const u = e * 0.55, n = e * 0.06;
259
- return `
260
- @keyframes orb-bars-wave {
261
- 0%, 100% { height: ${n}px; }
262
- 50% { height: ${u * 0.5}px; }
263
- }
264
- @keyframes orb-bars-wave-fast {
265
- 0%, 100% { height: ${n}px; }
266
- 50% { height: ${u * 0.5}px; }
267
- }
268
- `;
289
+ function ne(n) {
290
+ return [
291
+ parseInt(n.slice(1, 3), 16),
292
+ parseInt(n.slice(3, 5), 16),
293
+ parseInt(n.slice(5, 7), 16)
294
+ ];
269
295
  }
270
- function z({ state: e, volume: u, size: n, className: x, style: S }) {
271
- const g = y([]), a = y(0), r = y(new Array(E).fill(0)), f = y(null), p = y(u);
272
- T(() => {
273
- p.current = u;
274
- }, [u]), T(() => {
275
- const h = "orb-bars-keyframes";
276
- let c = document.getElementById(h);
277
- c || (c = document.createElement("style"), c.id = h, document.head.appendChild(c)), c.textContent = Z(n), f.current = c;
278
- }, [n]), T(() => {
279
- const h = n * 0.55, c = n * 0.06, b = P[e], R = (l, t) => {
280
- for (let o = 0; o < E; o++) {
281
- const d = g.current[o];
282
- d && (d.style.height = `${l[o]}px`, d.style.background = t, d.style.animation = "none");
296
+ function ye({ state: n, volume: o, size: e, className: k, style: A, onClick: d }) {
297
+ const M = s([]), t = s(null), r = s(0), m = s(new Array(C).fill(0)), h = s(o), c = s(!1), u = s(0), p = s(ne(j.idle)), T = 300, S = s(null), g = s(new Array(C).fill(0)), y = s(n);
298
+ B(() => {
299
+ n !== y.current && (g.current = [...m.current], S.current = Date.now(), y.current = n);
300
+ }, [n]), B(() => {
301
+ h.current = o;
302
+ }, [o]), B(() => {
303
+ const $ = e * 0.55, i = e * 0.06, w = j[n] ?? j.idle, P = e * 0.1, V = Array.from({ length: C }, (b, a) => {
304
+ const v = (C - 1) / 2;
305
+ return 1 - 0.7 * (Math.abs(a - v) / v);
306
+ }), O = () => {
307
+ const a = c.current ? P : 0;
308
+ u.current += (a - u.current) * 0.15;
309
+ }, G = (b, a) => {
310
+ O();
311
+ const v = ne(a), [R, E, D] = p.current;
312
+ p.current = [
313
+ R + (v[0] - R) * 0.08,
314
+ E + (v[1] - E) * 0.08,
315
+ D + (v[2] - D) * 0.08
316
+ ];
317
+ const [_, J, X] = p.current.map(Math.round), U = `rgb(${_},${J},${X})`;
318
+ for (let I = 0; I < C; I++) {
319
+ const K = M.current[I];
320
+ if (!K) continue;
321
+ const te = n === "idle" ? V[I] : 1, re = u.current * te;
322
+ K.style.height = `${Math.min(b[I] + re, $)}px`, K.style.background = U, K.style.animation = "none";
283
323
  }
284
324
  };
285
- if (e === "listening" || e === "speaking") {
286
- const l = e === "speaking" ? 1 : 0.4, t = () => {
287
- const o = p.current, d = Date.now() / 1e3;
288
- for (let k = 0; k < E; k++) {
289
- const _ = 0.5 + 0.15 * Math.sin(d * J * l * Math.PI * 2 + k * X), F = c + (h - c) * o * _, B = F > r.current[k] ? 0.3 : 0.2;
290
- r.current[k] += (F - r.current[k]) * B;
325
+ if (n === "listening" || n === "speaking") {
326
+ const b = n === "speaking" ? 1 : 0.4, a = () => {
327
+ const v = h.current, R = Date.now() / 1e3;
328
+ for (let E = 0; E < C; E++) {
329
+ const D = 0.5 + 0.15 * Math.sin(R * he * b * Math.PI * 2 + E * be);
330
+ let _ = i + ($ - i) * v * D;
331
+ if (S.current !== null) {
332
+ const X = Date.now() - S.current, U = Math.min(X / T, 1), I = 1 - (1 - U) * (1 - U);
333
+ _ = g.current[E] + (_ - g.current[E]) * I, U >= 1 && (S.current = null);
334
+ }
335
+ const J = n === "listening" ? 0.45 : 1;
336
+ m.current[E] += (_ - m.current[E]) * J;
291
337
  }
292
- R(r.current, b), a.current = requestAnimationFrame(t);
338
+ G(m.current, w), r.current = requestAnimationFrame(a);
293
339
  };
294
- return a.current = requestAnimationFrame(t), () => cancelAnimationFrame(a.current);
340
+ return r.current = requestAnimationFrame(a), () => cancelAnimationFrame(r.current);
295
341
  }
296
- if (e === "thinking") {
297
- const l = () => {
298
- const t = Date.now(), o = [];
299
- for (let d = 0; d < E; d++)
300
- o.push(c + (h - c) * 0.5 * (1 + Math.sin(t / 500 + d * 0.8)));
301
- R(o, b), a.current = requestAnimationFrame(l);
342
+ if (n === "connecting") {
343
+ const b = Date.now(), a = () => {
344
+ const v = (Date.now() - b) / 1e3;
345
+ O();
346
+ for (let R = 0; R < C; R++) {
347
+ const E = (v * 0.6 + R / C * 0.5) % 1, D = E < 0.5 ? Math.sin(E / 0.5 * Math.PI) : 0, _ = i + ($ * 0.4 - i) * D;
348
+ m.current[R] += (_ - m.current[R]) * 0.15;
349
+ }
350
+ G(m.current, w), r.current = requestAnimationFrame(a);
302
351
  };
303
- return a.current = requestAnimationFrame(l), () => cancelAnimationFrame(a.current);
352
+ return r.current = requestAnimationFrame(a), () => cancelAnimationFrame(r.current);
304
353
  }
305
- if (cancelAnimationFrame(a.current), e === "idle" || e === "connecting") {
306
- const l = e === "idle" ? "1.8s" : "1s";
307
- for (let t = 0; t < E; t++) {
308
- const o = g.current[t];
309
- if (!o) continue;
310
- o.style.background = b, o.style.height = "";
311
- const d = e === "idle" ? "orb-bars-wave" : "orb-bars-wave-fast";
312
- o.style.animation = `${d} ${l} ease-in-out ${t * 0.15}s infinite`;
354
+ cancelAnimationFrame(r.current);
355
+ const W = () => {
356
+ O();
357
+ for (let b = 0; b < C; b++) {
358
+ const a = M.current[b];
359
+ a && (a.style.height = `${i + u.current * V[b]}px`, a.style.background = w, a.style.animation = "none");
313
360
  }
314
- return;
315
- }
316
- for (let l = 0; l < E; l++) {
317
- const t = g.current[l];
318
- t && (t.style.height = `${c}px`, t.style.background = b, t.style.animation = "none");
319
- }
320
- }, [e, n]);
321
- const A = n * 0.055, w = n * 0.035, s = n * 0.03, m = n * 0.55, $ = n * 0.06;
322
- return /* @__PURE__ */ i(
361
+ r.current = requestAnimationFrame(W);
362
+ };
363
+ return r.current = requestAnimationFrame(W), () => cancelAnimationFrame(r.current);
364
+ }, [n, e]);
365
+ const f = e * 0.055, x = e * 0.035, q = e * 0.03, L = e * 0.55, F = e * 0.06;
366
+ return /* @__PURE__ */ l(
323
367
  "div",
324
368
  {
325
- className: x,
369
+ className: k,
326
370
  style: {
327
- width: n,
328
- height: n,
371
+ width: e,
372
+ height: e,
329
373
  display: "flex",
330
374
  alignItems: "center",
331
375
  justifyContent: "center",
332
- gap: w,
333
- ...S
376
+ position: "relative",
377
+ ...A
334
378
  },
335
- children: Array.from({ length: E }, (h, c) => /* @__PURE__ */ i(
379
+ children: /* @__PURE__ */ l(
336
380
  "div",
337
381
  {
338
- ref: (b) => {
339
- g.current[c] = b;
382
+ ref: t,
383
+ onClick: d,
384
+ onMouseEnter: () => {
385
+ c.current = !0, t.current && (t.current.style.filter = "brightness(1.35)");
386
+ },
387
+ onMouseLeave: () => {
388
+ c.current = !1, t.current && (t.current.style.filter = "brightness(1)");
389
+ },
390
+ onTouchEnd: () => {
391
+ setTimeout(() => {
392
+ c.current = !1, t.current && (t.current.style.filter = "brightness(1)");
393
+ }, 200);
340
394
  },
341
395
  style: {
342
- width: A,
343
- minHeight: $,
344
- maxHeight: m,
345
- height: $,
346
- borderRadius: s,
347
- background: P[e],
348
- transition: "background 0.3s ease"
349
- }
350
- },
351
- c
352
- ))
396
+ display: "flex",
397
+ alignItems: "center",
398
+ justifyContent: "center",
399
+ gap: x,
400
+ transition: "filter 0.3s ease",
401
+ cursor: d ? "pointer" : "default"
402
+ },
403
+ children: Array.from({ length: C }, ($, i) => /* @__PURE__ */ l(
404
+ "div",
405
+ {
406
+ ref: (w) => {
407
+ M.current[i] = w;
408
+ },
409
+ style: {
410
+ width: f,
411
+ minHeight: F,
412
+ maxHeight: L,
413
+ height: F,
414
+ borderRadius: q,
415
+ background: j[n] ?? j.idle
416
+ }
417
+ },
418
+ i
419
+ ))
420
+ }
421
+ )
353
422
  }
354
423
  );
355
424
  }
356
- function te({
357
- state: e,
358
- volume: u,
359
- adapter: n,
360
- theme: x = "debug",
361
- size: S = 200,
362
- className: g,
363
- style: a,
364
- onStart: r,
365
- onStop: f
425
+ function ve() {
426
+ var m;
427
+ let n = null, o = null, e = null, k = 0, A = 0, d = null;
428
+ if (typeof navigator < "u" && ((m = navigator.mediaDevices) != null && m.getUserMedia)) {
429
+ const h = navigator.mediaDevices.getUserMedia.bind(navigator.mediaDevices);
430
+ navigator.mediaDevices.getUserMedia = async (c) => {
431
+ const u = await h(c);
432
+ return c != null && c.audio && (d = u), u;
433
+ };
434
+ }
435
+ function M(h) {
436
+ if (!(!d || o))
437
+ try {
438
+ n = new AudioContext(), o = n.createAnalyser(), o.fftSize = 256, e = n.createMediaStreamSource(d), e.connect(o);
439
+ const c = new Uint8Array(o.frequencyBinCount), u = () => {
440
+ if (!o) return;
441
+ o.getByteFrequencyData(c);
442
+ let p = 0;
443
+ for (let g = 0; g < c.length; g++) p += c[g] * c[g];
444
+ const T = Math.sqrt(p / c.length) / 255, S = T > A ? 0.7 : 0.3;
445
+ A += (T - A) * S, h(ie(A)), k = requestAnimationFrame(u);
446
+ };
447
+ k = requestAnimationFrame(u);
448
+ } catch (c) {
449
+ console.warn("[orb-ui] Mic monitor failed:", c);
450
+ }
451
+ }
452
+ function t() {
453
+ cancelAnimationFrame(k), k = 0, A = 0, e == null || e.disconnect(), e = null, o = null;
454
+ }
455
+ function r() {
456
+ t(), n && (n.close().catch(() => {
457
+ }), n = null), d == null || d.getTracks().forEach((h) => {
458
+ h.readyState === "live" && h.stop();
459
+ }), d = null;
460
+ }
461
+ return { start: M, stop: t, release: r };
462
+ }
463
+ function pe({
464
+ state: n,
465
+ volume: o,
466
+ adapter: e,
467
+ theme: k = "debug",
468
+ size: A = 200,
469
+ className: d,
470
+ style: M,
471
+ onStart: t,
472
+ onStop: r
366
473
  }) {
367
- const [p, A] = O("idle"), [w, s] = O(0);
368
- T(() => n ? n.subscribe({
369
- onStateChange: A,
370
- onVolumeChange: s
371
- }) : void 0, [n]);
372
- const m = e ?? p, $ = u ?? w, h = m !== "idle" && m !== "disconnected" && m !== "error", c = G(() => {
373
- var t, o;
374
- h ? f ? f() : (t = n == null ? void 0 : n.stop) == null || t.call(n) : r ? r() : (o = n == null ? void 0 : n.start) == null || o.call(n);
375
- }, [n, h, r, f]), b = !!(n != null && n.start || r || f), R = { state: m, volume: $, size: S, className: g, style: a }, l = (() => {
376
- switch (x) {
377
- case "circle":
378
- return /* @__PURE__ */ i(Y, { ...R });
379
- case "bars":
380
- return /* @__PURE__ */ i(z, { ...R });
381
- case "debug":
382
- default:
383
- return /* @__PURE__ */ i(V, { ...R, onStart: r, onStop: f });
384
- }
385
- })();
386
- return b && x !== "debug" ? /* @__PURE__ */ i(
387
- "div",
388
- {
389
- onClick: c,
390
- style: { cursor: "pointer", display: "inline-flex" },
391
- children: l
392
- }
393
- ) : l;
474
+ const [m, h] = Z("idle"), [c, u] = Z(0), [p, T] = Z(0), S = s(ve()), g = s(!1);
475
+ B(() => (h("idle"), u(0), T(0), e ? e.subscribe({
476
+ onStateChange: h,
477
+ onVolumeChange: u
478
+ }) : void 0), [e]);
479
+ const y = n ?? m;
480
+ B(() => {
481
+ const i = S.current;
482
+ if (y === "listening" && e) {
483
+ const w = setTimeout(() => {
484
+ i.start(T), g.current = !0;
485
+ }, 500);
486
+ return () => {
487
+ clearTimeout(w), i.stop(), g.current = !1, T(0);
488
+ };
489
+ } else
490
+ i.stop(), g.current = !1, T(0);
491
+ }, [y, e]), B(() => () => S.current.release(), []);
492
+ const f = o ?? (y === "listening" && g.current ? p : c), x = y !== "idle" && y !== "error", q = oe(() => {
493
+ var i, w;
494
+ x ? r ? r() : (i = e == null ? void 0 : e.stop) == null || i.call(e) : t ? t() : (w = e == null ? void 0 : e.start) == null || w.call(e);
495
+ }, [e, x, t, r]), L = !!(e != null && e.start || t || r), F = { state: y, volume: f, size: A, className: d, style: M }, $ = L ? q : void 0;
496
+ switch (k) {
497
+ case "circle":
498
+ return /* @__PURE__ */ l(ge, { ...F, onClick: $ });
499
+ case "bars":
500
+ return /* @__PURE__ */ l(ye, { ...F, onClick: $ });
501
+ case "debug":
502
+ default:
503
+ return /* @__PURE__ */ l(
504
+ le,
505
+ {
506
+ ...F,
507
+ onStart: t ?? (() => {
508
+ var i;
509
+ return (i = e == null ? void 0 : e.start) == null ? void 0 : i.call(e);
510
+ }),
511
+ onStop: r ?? (() => {
512
+ var i;
513
+ return (i = e == null ? void 0 : e.stop) == null ? void 0 : i.call(e);
514
+ })
515
+ }
516
+ );
517
+ }
394
518
  }
395
519
  export {
396
- te as VoiceOrb
520
+ pe as Orb,
521
+ pe as VoiceOrb
397
522
  };
package/package.json CHANGED
@@ -1,18 +1,21 @@
1
1
  {
2
2
  "name": "orb-ui",
3
- "version": "0.1.0",
4
- "description": "The simplest voice AI UI component library for React. Works with Vapi and ElevenLabs.",
3
+ "version": "0.2.0",
4
+ "description": "The simplest way to add voice UI to your React app. One install, one component, works with Vapi and ElevenLabs out of the box.",
5
5
  "type": "module",
6
+ "main": "./dist/orb-ui.cjs",
6
7
  "module": "./dist/orb-ui.js",
7
- "types": "./dist/index.d.ts",
8
+ "types": "./dist/orb-ui.d.ts",
8
9
  "exports": {
9
10
  ".": {
10
11
  "import": "./dist/orb-ui.js",
11
- "types": "./dist/index.d.ts"
12
+ "require": "./dist/orb-ui.cjs",
13
+ "types": "./dist/orb-ui.d.ts"
12
14
  },
13
15
  "./adapters": {
14
16
  "import": "./dist/adapters.js",
15
- "types": "./dist/adapters/index.d.ts"
17
+ "require": "./dist/adapters.cjs",
18
+ "types": "./dist/adapters.d.ts"
16
19
  }
17
20
  },
18
21
  "files": [
@@ -32,7 +35,6 @@
32
35
  "devDependencies": {
33
36
  "@types/react": "^18.3.0",
34
37
  "@types/react-dom": "^18.3.0",
35
-
36
38
  "@typescript-eslint/eslint-plugin": "^7.0.0",
37
39
  "@typescript-eslint/parser": "^7.0.0",
38
40
  "@vitejs/plugin-react": "^4.3.0",
@@ -44,17 +46,29 @@
44
46
  },
45
47
  "keywords": [
46
48
  "voice",
49
+ "voice-ui",
50
+ "voice-agent",
47
51
  "ai",
48
52
  "react",
49
53
  "animation",
50
54
  "component",
55
+ "state-machine",
51
56
  "vapi",
52
57
  "elevenlabs"
53
58
  ],
54
59
  "license": "MIT",
60
+ "author": "Alexander Chen",
61
+ "homepage": "https://orb-ui-demo.vercel.app",
55
62
  "repository": {
56
63
  "type": "git",
57
64
  "url": "https://github.com/alexanderqchen/orb-ui.git"
58
65
  },
59
- "dependencies": {}
66
+ "dependencies": {},
67
+ "typesVersions": {
68
+ "*": {
69
+ "adapters": [
70
+ "./dist/adapters.d.ts"
71
+ ]
72
+ }
73
+ }
60
74
  }