@vox-ai/react 0.2.0 → 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 +117 -2
- package/dist/hooks/index.d.ts +0 -0
- package/dist/hooks/useVoxAI.d.ts +17 -22
- package/dist/index.d.ts +1 -1
- package/dist/lib.cjs +1 -1
- package/dist/lib.cjs.map +1 -1
- package/dist/lib.modern.js +1 -1
- package/dist/lib.modern.js.map +1 -1
- package/dist/lib.module.js +1 -1
- package/dist/lib.module.js.map +1 -1
- package/dist/lib.umd.js +1 -1
- package/dist/lib.umd.js.map +1 -1
- package/dist/utils/constants.d.ts +2 -0
- package/package.json +6 -6
- package/dist/contexts/VoxContext.d.ts +0 -14
- package/dist/hooks/useVoxAudioWaveform.d.ts +0 -10
package/README.md
CHANGED
|
@@ -28,7 +28,16 @@ Vox.ai 플랫폼과의 음성 AI 상호작용을 관리하는 React 훅입니다
|
|
|
28
28
|
import { useVoxAI } from "@vox-ai/react";
|
|
29
29
|
|
|
30
30
|
function VoiceComponent() {
|
|
31
|
-
const {
|
|
31
|
+
const {
|
|
32
|
+
connect,
|
|
33
|
+
disconnect,
|
|
34
|
+
state,
|
|
35
|
+
messages,
|
|
36
|
+
send,
|
|
37
|
+
audioWaveform,
|
|
38
|
+
toggleMic,
|
|
39
|
+
setVolume,
|
|
40
|
+
} = useVoxAI({
|
|
32
41
|
onConnect: () => console.log("Vox.ai에 연결됨"),
|
|
33
42
|
onDisconnect: () => console.log("Vox.ai 연결 해제됨"),
|
|
34
43
|
onError: (error) => console.error("오류:", error),
|
|
@@ -62,6 +71,11 @@ connect({
|
|
|
62
71
|
userName: "홍길동",
|
|
63
72
|
context: "고객-지원",
|
|
64
73
|
},
|
|
74
|
+
metadata: {
|
|
75
|
+
// 통화에 대한 메타데이터를 프론트엔드에게 전달
|
|
76
|
+
callerId: "customer-123",
|
|
77
|
+
departmentId: "support",
|
|
78
|
+
},
|
|
65
79
|
});
|
|
66
80
|
```
|
|
67
81
|
|
|
@@ -73,6 +87,55 @@ connect({
|
|
|
73
87
|
disconnect();
|
|
74
88
|
```
|
|
75
89
|
|
|
90
|
+
##### send
|
|
91
|
+
|
|
92
|
+
텍스트 메시지나 DTMF 톤을 에이전트에 전송하는 메서드입니다.
|
|
93
|
+
|
|
94
|
+
```tsx
|
|
95
|
+
// 텍스트 메시지 전송
|
|
96
|
+
send({ message: "안녕하세요, 도움이 필요합니다." });
|
|
97
|
+
|
|
98
|
+
// DTMF 톤 전송 (0-9, *, #)
|
|
99
|
+
send({ digit: 1 });
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
##### audioWaveform
|
|
103
|
+
|
|
104
|
+
에이전트나 사용자의 오디오 웨이브폼 데이터를 반환하는 메서드입니다.
|
|
105
|
+
|
|
106
|
+
```tsx
|
|
107
|
+
// 에이전트 오디오 웨이브폼 데이터 가져오기 (기본값)
|
|
108
|
+
const agentWaveform = audioWaveform({
|
|
109
|
+
speaker: "agent", // "agent" 또는 "user"
|
|
110
|
+
barCount: 20, // 반환할 웨이브폼 바의 수
|
|
111
|
+
updateInterval: 50, // 업데이트 간격 (ms)
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
// 사용자 오디오 웨이브폼 데이터 가져오기
|
|
115
|
+
const userWaveform = audioWaveform({ speaker: "user" });
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
##### toggleMic
|
|
119
|
+
|
|
120
|
+
사용자의 마이크를 활성화/비활성화하는 메서드입니다.
|
|
121
|
+
|
|
122
|
+
```tsx
|
|
123
|
+
// 마이크 활성화
|
|
124
|
+
toggleMic(true);
|
|
125
|
+
|
|
126
|
+
// 마이크 비활성화
|
|
127
|
+
toggleMic(false);
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
##### setVolume
|
|
131
|
+
|
|
132
|
+
에이전트의 볼륨을 설정하는 메서드입니다. 값은 0(음소거)부터 1(최대 볼륨)까지입니다.
|
|
133
|
+
|
|
134
|
+
```tsx
|
|
135
|
+
// 볼륨 50%로 설정
|
|
136
|
+
setVolume(0.5);
|
|
137
|
+
```
|
|
138
|
+
|
|
76
139
|
#### 상태 및 데이터
|
|
77
140
|
|
|
78
141
|
##### state
|
|
@@ -104,18 +167,21 @@ type VoxMessage = {
|
|
|
104
167
|
message?: string;
|
|
105
168
|
timestamp: number;
|
|
106
169
|
isFinal?: boolean;
|
|
170
|
+
tool?: FunctionToolsExecuted; // 에이전트가 실행한 함수 도구
|
|
107
171
|
};
|
|
108
172
|
```
|
|
109
173
|
|
|
110
174
|
## 예제
|
|
111
175
|
|
|
176
|
+
### 기본 사용법
|
|
177
|
+
|
|
112
178
|
```tsx
|
|
113
179
|
import React, { useState } from "react";
|
|
114
180
|
import { useVoxAI } from "@vox-ai/react";
|
|
115
181
|
|
|
116
182
|
function VoiceAssistant() {
|
|
117
183
|
const [isConnected, setIsConnected] = useState(false);
|
|
118
|
-
const { connect, disconnect, state, messages } = useVoxAI({
|
|
184
|
+
const { connect, disconnect, state, messages, send } = useVoxAI({
|
|
119
185
|
onConnect: () => setIsConnected(true),
|
|
120
186
|
onDisconnect: () => setIsConnected(false),
|
|
121
187
|
onError: (error) => console.error("오류:", error),
|
|
@@ -128,6 +194,10 @@ function VoiceAssistant() {
|
|
|
128
194
|
});
|
|
129
195
|
};
|
|
130
196
|
|
|
197
|
+
const handleSendMessage = () => {
|
|
198
|
+
send({ message: "안녕하세요, 도움이 필요합니다." });
|
|
199
|
+
};
|
|
200
|
+
|
|
131
201
|
return (
|
|
132
202
|
<div>
|
|
133
203
|
<h1>Vox.ai 음성 비서</h1>
|
|
@@ -139,6 +209,9 @@ function VoiceAssistant() {
|
|
|
139
209
|
<button onClick={disconnect} disabled={!isConnected}>
|
|
140
210
|
연결 해제
|
|
141
211
|
</button>
|
|
212
|
+
<button onClick={handleSendMessage} disabled={!isConnected}>
|
|
213
|
+
메시지 전송
|
|
214
|
+
</button>
|
|
142
215
|
</div>
|
|
143
216
|
|
|
144
217
|
<div>
|
|
@@ -160,6 +233,48 @@ function VoiceAssistant() {
|
|
|
160
233
|
}
|
|
161
234
|
```
|
|
162
235
|
|
|
236
|
+
### 오디오 웨이브폼 시각화 예제
|
|
237
|
+
|
|
238
|
+
```tsx
|
|
239
|
+
import React, { useState, useEffect } from "react";
|
|
240
|
+
import { useVoxAI } from "@vox-ai/react";
|
|
241
|
+
|
|
242
|
+
function WaveformVisualizer() {
|
|
243
|
+
const { audioWaveform, state } = useVoxAI();
|
|
244
|
+
const [waveformData, setWaveformData] = useState([]);
|
|
245
|
+
|
|
246
|
+
// 웨이브폼 데이터를 정기적으로 업데이트
|
|
247
|
+
useEffect(() => {
|
|
248
|
+
if (state === "disconnected") return;
|
|
249
|
+
|
|
250
|
+
const intervalId = setInterval(() => {
|
|
251
|
+
// 에이전트 오디오 웨이브폼 데이터 가져오기
|
|
252
|
+
const data = audioWaveform({ speaker: "agent", barCount: 30 });
|
|
253
|
+
setWaveformData(data);
|
|
254
|
+
}, 50);
|
|
255
|
+
|
|
256
|
+
return () => clearInterval(intervalId);
|
|
257
|
+
}, [audioWaveform, state]);
|
|
258
|
+
|
|
259
|
+
return (
|
|
260
|
+
<div className="waveform-container">
|
|
261
|
+
{waveformData.map((value, index) => (
|
|
262
|
+
<div
|
|
263
|
+
key={index}
|
|
264
|
+
className="waveform-bar"
|
|
265
|
+
style={{
|
|
266
|
+
height: `${value * 100}%`,
|
|
267
|
+
width: "10px",
|
|
268
|
+
backgroundColor: "#3498db",
|
|
269
|
+
margin: "0 2px",
|
|
270
|
+
}}
|
|
271
|
+
/>
|
|
272
|
+
))}
|
|
273
|
+
</div>
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
```
|
|
277
|
+
|
|
163
278
|
## 기여하기
|
|
164
279
|
|
|
165
280
|
변경 사항을 제안하기 전에 먼저 이슈를 생성해 주세요. 모든 기여를 환영합니다!
|
package/dist/hooks/index.d.ts
CHANGED
|
File without changes
|
package/dist/hooks/useVoxAI.d.ts
CHANGED
|
@@ -6,29 +6,25 @@ export type VoxAgentState = "disconnected" | "connecting" | "initializing" | "li
|
|
|
6
6
|
/**
|
|
7
7
|
* Function call related types
|
|
8
8
|
*/
|
|
9
|
+
export interface FunctionToolsExecuted {
|
|
10
|
+
type: "function_tools_executed";
|
|
11
|
+
function_calls: FunctionCallInfo[];
|
|
12
|
+
function_call_outputs: FunctionCallResult[];
|
|
13
|
+
}
|
|
9
14
|
export interface FunctionCallInfo {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
};
|
|
16
|
-
raw_arguments: string;
|
|
17
|
-
arguments: Record<string, any>;
|
|
15
|
+
id: string;
|
|
16
|
+
type: string;
|
|
17
|
+
call_id: string;
|
|
18
|
+
arguments: string;
|
|
19
|
+
name: string;
|
|
18
20
|
}
|
|
19
21
|
export interface FunctionCallResult {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
};
|
|
27
|
-
raw_arguments: string;
|
|
28
|
-
arguments: Record<string, any>;
|
|
29
|
-
};
|
|
30
|
-
result: Record<string, any> | null;
|
|
31
|
-
exception: string | null;
|
|
22
|
+
id: string;
|
|
23
|
+
name: string;
|
|
24
|
+
type: string;
|
|
25
|
+
call_id: string;
|
|
26
|
+
output: string;
|
|
27
|
+
is_error: boolean;
|
|
32
28
|
}
|
|
33
29
|
/**
|
|
34
30
|
* VoxMessage
|
|
@@ -40,8 +36,7 @@ export type VoxMessage = {
|
|
|
40
36
|
message?: string;
|
|
41
37
|
timestamp: number;
|
|
42
38
|
isFinal?: boolean;
|
|
43
|
-
|
|
44
|
-
toolCallResults?: FunctionCallResult[];
|
|
39
|
+
tool?: FunctionToolsExecuted;
|
|
45
40
|
};
|
|
46
41
|
/**
|
|
47
42
|
* VoxAIOptions
|
package/dist/index.d.ts
CHANGED
|
@@ -3,4 +3,4 @@
|
|
|
3
3
|
* React UI component library for vox.ai
|
|
4
4
|
*/
|
|
5
5
|
export { useVoxAI } from "./hooks";
|
|
6
|
-
export type { VoxAgentState, VoxMessage, VoxAIOptions, ConnectParams, } from "./hooks";
|
|
6
|
+
export type { VoxAgentState, VoxMessage, VoxAIOptions, ConnectParams, FunctionToolsExecuted, FunctionCallInfo, FunctionCallResult, } from "./hooks";
|
package/dist/lib.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var e=require("react/jsx-runtime"),t=require("@livekit/components-react"),n=require("livekit-client"),r=require("react"),
|
|
1
|
+
var e=require("react/jsx-runtime"),t=require("@livekit/components-react"),n=require("livekit-client"),r=require("react"),o=require("react-dom/client");function a(e){let{port:o,initialConfig:a}=e;const{agent:s,state:c}=t.useVoiceAssistant(),{send:i}=t.useChat(),[u,l]=r.useState({speaker:a.speaker||"agent",barCount:a.barCount,updateInterval:a.updateInterval}),p=t.useParticipantTracks([n.Track.Source.Microphone],null==s?void 0:s.identity)[0],d=t.useTrackTranscription(p),m=t.useAudioWaveform(p,{barCount:"agent"===u.speaker?u.barCount:120,updateInterval:"agent"===u.speaker?u.updateInterval:20}),f=t.useLocalParticipant(),g=t.useTrackTranscription({publication:f.microphoneTrack,source:n.Track.Source.Microphone,participant:f.localParticipant}),v=t.useParticipantTracks([n.Track.Source.Microphone],f.localParticipant.identity)[0],y=t.useAudioWaveform(v,{barCount:"user"===u.speaker?u.barCount:120,updateInterval:"user"===u.speaker?u.updateInterval:20});return r.useEffect(()=>{o&&m&&m.bars&&o.postMessage({type:"waveform_update",waveformData:m.bars,speaker:"agent"})},[o,m]),r.useEffect(()=>{o&&y&&y.bars&&o.postMessage({type:"waveform_update",waveformData:y.bars,speaker:"user"})},[o,y]),r.useEffect(()=>{if(!o)return;const e=e=>{const t=e.data;if("waveform_config"===t.type&&t.config)"number"==typeof t.config.barCount&&"number"==typeof t.config.updateInterval&&l(t.config);else if("send_text"===t.type)i?i(t.text):console.error("sendChat function is not available");else if("send_dtmf"===t.type)f.localParticipant?f.localParticipant.publishDtmf(101,t.digit):console.error("Local participant is not available for DTMF");else if("toggle_mic"===t.type&&"boolean"==typeof t.enabled)f.localParticipant?f.localParticipant.setMicrophoneEnabled(t.enabled).catch(e=>{console.error("Failed to toggle microphone:",e)}):console.error("Local participant is not available for mic toggle");else if("set_volume"===t.type&&"number"==typeof t.volume)if(s)try{s.setVolume(t.volume),console.log("Set agent volume to "+t.volume)}catch(e){console.error("Failed to set agent volume:",e)}else console.error("Agent is not available for volume control")};return o.start(),o.addEventListener("message",e),()=>{o.removeEventListener("message",e)}},[o,i,f,s]),r.useEffect(()=>{o&&o.postMessage({type:"state_update",state:c})},[c,o]),r.useEffect(()=>{if(o&&d.segments.length>0){const e=d.segments.map(e=>({id:e.id,text:e.text,isFinal:e.final,timestamp:Date.now(),speaker:"agent"}));o.postMessage({type:"transcription_update",transcriptions:e})}},[d.segments,o]),r.useEffect(()=>{if(o&&g.segments.length>0){const e=g.segments.map(e=>({id:e.id,text:e.text,isFinal:e.final,timestamp:Date.now(),speaker:"user"}));o.postMessage({type:"transcription_update",transcriptions:e})}},[g.segments,o]),t.useDataChannel("function_tools_executed",e=>{if(!o)return;const t=new TextDecoder,n=e.payload instanceof Uint8Array?t.decode(e.payload):String(e.payload);let r;try{r=JSON.parse(n),o.postMessage({type:"function_tools_executed",tool:r})}catch(e){console.error("Failed to parse function call log:",e)}}),null}exports.useVoxAI=function(n){void 0===n&&(n={});const[s,c]=r.useState(null),[i,u]=r.useState("disconnected"),l=r.useRef(Date.now()),[p,d]=r.useState(new Map),[m,f]=r.useState([]),g=r.useRef(""),v=r.useRef(new Set),y=r.useRef(null),b=r.useRef(null),h=r.useRef(null),k=r.useRef(null),[w,C]=r.useState({agent:[],user:[]}),M=r.useRef(null),[E,_]=r.useState(!0);r.useEffect(()=>{const e=Array.from(p.values()).sort((e,t)=>e.timestamp-t.timestamp),t=JSON.stringify(e);t!==g.current&&(g.current=t,f(e),n.onMessage&&e.filter(e=>e.isFinal&&e.id&&!v.current.has(e.id)).forEach(e=>{e.id&&(v.current.add(e.id),null==n.onMessage||n.onMessage(e))}))},[p,n.onMessage]),r.useEffect(()=>{const e=new MessageChannel;return e.port1.onmessage=e=>{const t=e.data;if("state_update"===t.type)u(t.state);else if("transcription_update"===t.type)x(t.transcriptions);else if("waveform_update"===t.type&&t.speaker)C(e=>({...e,[t.speaker]:t.waveformData}));else if("function_tools_executed"===t.type&&t.tool){const e="function-calls-"+Date.now();d(n=>{const r=new Map(n);return r.set(e,{id:e,name:"tool",tool:t.tool,timestamp:Date.now(),isFinal:!0}),r})}},e.port1.start(),h.current=e,()=>{var e,t;null==(e=h.current)||e.port1.close(),null==(t=h.current)||t.port2.close(),h.current=null}},[]);const x=r.useCallback(e=>{d(t=>{const n=new Map(t);return e.forEach(e=>{var r;if(e.timestamp<l.current)return;const o="agent"===e.speaker?"agent":"user",a=(null==(r=t.get(e.id))?void 0:r.timestamp)||e.timestamp;n.set(e.id,{id:e.id,name:o,message:e.text,timestamp:a,isFinal:e.isFinal})}),n})},[]);r.useEffect(()=>{const e=document.createElement("div");return e.style.display="none",document.body.appendChild(e),y.current=e,b.current=o.createRoot(e),()=>{b.current&&b.current.unmount(),y.current&&document.body.removeChild(y.current)}},[]);const D=r.useCallback(function(e){let{agentId:t,apiKey:r,dynamicVariables:o,metadata:a}=e;try{return Promise.resolve(function(e,s){try{var p=function(){if("disconnected"!==i){const e="Connection attempt rejected: Already in a connection state ("+i+")";return console.warn(e),n.onError&&n.onError(new Error(e)),Promise.reject(new Error(e))}return l.current=Date.now(),u("connecting"),Promise.resolve(fetch("https://www.tryvox.co/api/agent/sdk",{method:"POST",headers:{Authorization:"Bearer "+r,"Content-Type":"application/json"},body:JSON.stringify({agent_id:t,metadata:{runtime_context:{source:{type:"react-sdk",version:"0.3.0"}},call_web:{dynamic_variables:o||{},metadata:a||{}}}})})).then(function(e){function t(t){return Promise.resolve(e.json()).then(function(e){c(e),n.onConnect&&n.onConnect()})}const r=function(){if(!e.ok)return Promise.resolve(e.text()).then(function(t){throw new Error("Connection failed ("+e.status+"): "+t)})}();return r&&r.then?r.then(t):t()})}()}catch(e){return s(e)}return p&&p.then?p.then(void 0,s):p}(0,function(e){c(null),d(new Map),f([]),u("disconnected");const t=e instanceof Error?e:new Error(String(e));n.onError&&n.onError(t)}))}catch(e){return Promise.reject(e)}},[n,i]),S=r.useCallback(()=>{l.current=Date.now(),c(null),d(new Map),f([]),u("disconnected"),n.onDisconnect&&n.onDisconnect()},[n]),P=r.useCallback(e=>{let{message:t,digit:n}=e;if("disconnected"!==i){if(t){const e="user-text-"+Date.now();d(n=>{const r=new Map(n);return r.set(e,{id:e,name:"user",message:t,timestamp:Date.now(),isFinal:!0}),r}),h.current?h.current.port1.postMessage({type:"send_text",text:t}):console.error("No message channel available to send message")}void 0!==n&&(h.current?h.current.port1.postMessage({type:"send_dtmf",digit:n}):console.error("No message channel available to send DTMF"))}else console.warn("Cannot send message: Not connected to a conversation")},[i]),T=r.useCallback(e=>{let{speaker:t="agent",barCount:n=10,updateInterval:r=20}=e;M.current={speaker:t,barCount:n,updateInterval:r},h.current&&h.current.port1.postMessage({type:"waveform_config",config:{speaker:t,barCount:n,updateInterval:r}});const o=w[t]||[];return o.length>0?o.slice(0,n):Array(n).fill(0)},[w]),F=r.useCallback(e=>{_(e),h.current?h.current.port1.postMessage({type:"toggle_mic",enabled:e}):console.error("No message channel available to toggle microphone")},[]),I=r.useCallback(e=>{const t=Math.min(Math.max(e,0),1);h.current?h.current.port1.postMessage({type:"set_volume",volume:t}):console.error("No message channel available to set volume")},[]);return r.useEffect(()=>{b.current&&(s?(k.current||(h.current&&h.current.port2.start(),k.current=e.jsxs(t.LiveKitRoom,{serverUrl:s.serverUrl,token:s.participantToken,audio:!0,video:!1,connect:!0,onDisconnected:S,onError:e=>{console.error("LiveKit connection error:",e),S(),n.onError&&n.onError(new Error("LiveKit connection error: "+e.message))},children:[e.jsx(t.RoomAudioRenderer,{}),h.current&&e.jsx(a,{port:h.current.port2,initialConfig:M.current||{barCount:10,updateInterval:20}})]})),b.current.render(k.current)):(k.current=null,b.current.render(e.jsx(e.Fragment,{}))))},[s,S,n.onError]),{connect:D,disconnect:S,state:i,messages:m,send:P,audioWaveform:T,toggleMic:F,setVolume:I}};
|
|
2
2
|
//# sourceMappingURL=lib.cjs.map
|
package/dist/lib.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lib.cjs","sources":["../src/hooks/useVoxAI.tsx"],"sourcesContent":["import {\n LiveKitRoom,\n RoomAudioRenderer,\n useAudioWaveform,\n useChat,\n useDataChannel,\n useLocalParticipant,\n useParticipantTracks,\n useTrackTranscription,\n useVoiceAssistant,\n} from \"@livekit/components-react\";\nimport { Track } from \"livekit-client\";\nimport { useCallback, useEffect, useRef, useState } from \"react\";\nimport { createRoot, Root } from \"react-dom/client\";\n\ntype VoxConnectionDetail = {\n serverUrl: string;\n roomName: string;\n participantName: string;\n participantToken: string;\n};\n\n/**\n * VoxAgentState\n * @description The state of the agent\n */\nexport type VoxAgentState =\n | \"disconnected\"\n | \"connecting\"\n | \"initializing\"\n | \"listening\"\n | \"thinking\"\n | \"speaking\";\n\n// API endpoint\nconst HTTPS_API_ORIGIN = \"https://www.tryvox.co/api/agent/sdk\";\n// const HTTPS_API_ORIGIN = \"http://localhost:3000/api/agent/sdk\";\n\n/**\n * Function call related types\n */\nexport interface FunctionCallInfo {\n tool_call_id: string;\n function_info: {\n name: string;\n description: string;\n arguments: Record<string, any>;\n };\n raw_arguments: string;\n arguments: Record<string, any>;\n}\n\nexport interface FunctionCallResult {\n call_info: {\n tool_call_id: string;\n function_info: {\n name: string;\n description: string;\n arguments: Record<string, any>;\n };\n raw_arguments: string;\n arguments: Record<string, any>;\n };\n result: Record<string, any> | null;\n exception: string | null;\n}\n\n/**\n * VoxMessage\n * @description The message type between the agent and the user\n */\nexport type VoxMessage = {\n id?: string;\n name: \"agent\" | \"user\" | \"tool\";\n message?: string;\n timestamp: number;\n isFinal?: boolean;\n toolCalls?: FunctionCallInfo[];\n toolCallResults?: FunctionCallResult[];\n};\n\n/**\n * VoxAIOptions\n * @description The callback functions for the useVoxAI hook\n */\nexport interface VoxAIOptions {\n onConnect?: () => void;\n onDisconnect?: () => void;\n onError?: (error: Error) => void;\n onMessage?: (message: VoxMessage) => void;\n}\n\n// Message channel event types\ntype MessageChannelEvent =\n | { type: \"state_update\"; state: VoxAgentState }\n | { type: \"transcription_update\"; transcriptions: TranscriptionSegment[] }\n | {\n type: \"waveform_update\";\n waveformData: number[];\n speaker: \"agent\" | \"user\";\n }\n | { type: \"function_calls_collected\"; functionCalls: FunctionCallInfo[] }\n | { type: \"function_calls_finished\"; functionResults: FunctionCallResult[] };\n\ntype TranscriptionSegment = {\n id: string;\n text: string;\n isFinal: boolean;\n timestamp: number;\n speaker: \"agent\" | \"user\";\n};\n\n/**\n * ConnectParams\n * @param agentId - The agent ID\n * @param apiKey - The API key\n * @param dynamicVariables - The dynamic variables\n * @param metadata - 이 메타데이터는 아웃바운드 웹훅, 통화 기록에 포함됩니다.\n */\nexport interface ConnectParams {\n agentId: string;\n apiKey: string;\n dynamicVariables?: Record<string, any>;\n metadata?: Record<string, any>;\n}\n\n/**\n * useVoxAI\n * @description The hook for integrating with vox.ai voice assistant\n * @param options - The options for the useVoxAI hook\n * @returns The useVoxAI hook\n * @example\n * const { connect, disconnect, state, messages, send } = useVoxAI({\n * onConnect: () => console.log(\"Connected\"),\n * onDisconnect: () => console.log(\"Disconnected\"),\n * onError: (error) => console.error(\"Error:\", error),\n * onMessage: (message) => console.log(\"Message:\", message),\n * });\n */\nexport function useVoxAI(options: VoxAIOptions = {}) {\n // Connection state\n const [connectionDetail, setConnectionDetail] =\n useState<VoxConnectionDetail | null>(null);\n const [state, setState] = useState<VoxAgentState>(\"disconnected\");\n\n // Message handling\n const [transcriptMap, setTranscriptMap] = useState<Map<string, VoxMessage>>(\n new Map()\n );\n const [messages, setMessages] = useState<VoxMessage[]>([]);\n const prevMessagesRef = useRef<string>(\"\");\n\n // Track which messages we've already sent to the onMessage callback\n const processedMessageIdsRef = useRef<Set<string>>(new Set());\n\n // DOM manipulation for LiveKit portal\n const portalRootRef = useRef<HTMLDivElement | null>(null);\n const rootRef = useRef<Root | null>(null);\n\n // Communication channel\n const channelRef = useRef<MessageChannel | null>(null);\n\n // Add this near the start of your useVoxAI hook\n const livekitComponentRef = useRef<React.ReactNode>(null);\n\n // Replace the single waveform state with a map for multiple speakers\n const [waveformDataMap, setWaveformDataMap] = useState<\n Record<string, number[]>\n >({\n agent: [],\n user: [],\n });\n\n // Add back the waveform config reference\n const waveformConfigRef = useRef<{\n speaker?: \"agent\" | \"user\";\n barCount: number;\n updateInterval: number;\n } | null>(null);\n\n // Add a new state to track microphone status\n const [isMicEnabled, setIsMicEnabled] = useState<boolean>(true);\n\n // Update messages whenever transcriptMap changes\n useEffect(() => {\n const allMessages = Array.from(transcriptMap.values()).sort(\n (a, b) => a.timestamp - b.timestamp\n );\n\n // Only update if the messages have actually changed\n const messagesString = JSON.stringify(allMessages);\n if (messagesString !== prevMessagesRef.current) {\n prevMessagesRef.current = messagesString;\n setMessages(allMessages);\n\n // Only trigger onMessage for new final messages that haven't been processed yet\n if (options.onMessage) {\n allMessages\n .filter(\n (msg) =>\n msg.isFinal &&\n msg.id &&\n !processedMessageIdsRef.current.has(msg.id)\n )\n .forEach((msg) => {\n if (msg.id) {\n // Mark this message as processed\n processedMessageIdsRef.current.add(msg.id);\n // Call the callback\n options.onMessage?.(msg);\n }\n });\n }\n }\n }, [transcriptMap, options.onMessage]);\n\n // Initialize message channel - ensure ports are properly connected\n useEffect(() => {\n const channel = new MessageChannel();\n\n channel.port1.onmessage = (e) => {\n const data = e.data as MessageChannelEvent;\n\n if (data.type === \"state_update\") {\n setState(data.state);\n } else if (data.type === \"transcription_update\") {\n handleTranscriptionUpdate(data.transcriptions);\n } else if (data.type === \"waveform_update\" && data.speaker) {\n // Store the waveform data for the specific speaker\n setWaveformDataMap((prevMap) => ({\n ...prevMap,\n [data.speaker]: data.waveformData,\n }));\n } else if (\n data.type === \"function_calls_collected\" &&\n data.functionCalls\n ) {\n // Handle function calls\n const functionCallsId = `function-calls-${Date.now()}`;\n setTranscriptMap((prevMap) => {\n const newMap = new Map(prevMap);\n newMap.set(functionCallsId, {\n id: functionCallsId,\n name: \"tool\",\n toolCalls: data.functionCalls,\n timestamp: Date.now(),\n isFinal: true,\n });\n return newMap;\n });\n } else if (\n data.type === \"function_calls_finished\" &&\n data.functionResults\n ) {\n // Handle function results\n const functionResultsId = `function-results-${Date.now()}`;\n setTranscriptMap((prevMap) => {\n const newMap = new Map(prevMap);\n newMap.set(functionResultsId, {\n id: functionResultsId,\n name: \"tool\",\n toolCallResults: data.functionResults,\n timestamp: Date.now(),\n isFinal: true,\n });\n return newMap;\n });\n }\n };\n\n // Start the port\n channel.port1.start();\n\n // Store the channel reference\n channelRef.current = channel;\n\n return () => {\n channelRef.current?.port1.close();\n channelRef.current?.port2.close();\n channelRef.current = null;\n };\n }, []);\n\n // Process incoming transcriptions\n const handleTranscriptionUpdate = useCallback(\n (transcriptions: TranscriptionSegment[]) => {\n setTranscriptMap((prevMap) => {\n const newMap = new Map(prevMap);\n\n transcriptions.forEach((t) => {\n const messageType = t.speaker === \"agent\" ? \"agent\" : \"user\";\n // Use existing timestamp if we already have this segment\n const existingTimestamp = prevMap.get(t.id)?.timestamp || t.timestamp;\n\n newMap.set(t.id, {\n id: t.id,\n name: messageType,\n message: t.text,\n timestamp: existingTimestamp,\n isFinal: t.isFinal,\n });\n });\n\n return newMap;\n });\n },\n []\n );\n\n // Set up DOM portal for LiveKit\n useEffect(() => {\n const div = document.createElement(\"div\");\n div.style.display = \"none\";\n document.body.appendChild(div);\n portalRootRef.current = div;\n rootRef.current = createRoot(div);\n\n return () => {\n if (rootRef.current) {\n rootRef.current.unmount();\n }\n if (portalRootRef.current) {\n document.body.removeChild(portalRootRef.current);\n }\n };\n }, []);\n\n // Connect to VoxAI service - updated to include dynamicVariables\n /**\n * Initiates a connection to the VoxAI service\n * @param {ConnectParams} options - Connection parameters\n * @returns {Promise} - Resolves when the connection is successful, rejects if:\n * 1. The connection is already in progress (state is not \"disconnected\")\n * 2. The server returns an error\n * 3. Any other error occurs during the connection process\n */\n const connect = useCallback(\n async ({ agentId, apiKey, dynamicVariables, metadata }: ConnectParams) => {\n try {\n // Prevent connecting if already in a connection state\n if (state !== \"disconnected\") {\n const errorMessage = `Connection attempt rejected: Already in a connection state (${state})`;\n console.warn(errorMessage);\n\n // Call the onError callback if provided\n if (options.onError) {\n options.onError(new Error(errorMessage));\n }\n\n // Return a rejected promise\n return Promise.reject(new Error(errorMessage));\n }\n\n setState(\"connecting\");\n\n const response = await fetch(HTTPS_API_ORIGIN, {\n method: \"POST\",\n headers: {\n Authorization: `Bearer ${apiKey}`,\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({\n agent_id: agentId,\n metadata: {\n call_web: {\n dynamic_variables: dynamicVariables || {},\n metadata: metadata || {},\n },\n },\n }),\n });\n\n if (!response.ok) {\n const errorText = await response.text();\n throw new Error(\n `Connection failed (${response.status}): ${errorText}`\n );\n }\n\n const data = await response.json();\n setConnectionDetail(data);\n\n if (options.onConnect) {\n options.onConnect();\n }\n } catch (err) {\n // Reset state on error\n setConnectionDetail(null);\n setTranscriptMap(new Map());\n setMessages([]);\n setState(\"disconnected\");\n\n const error = err instanceof Error ? err : new Error(String(err));\n\n if (options.onError) {\n options.onError(error);\n }\n }\n },\n [options]\n );\n\n // Disconnect from VoxAI service\n const disconnect = useCallback(() => {\n setConnectionDetail(null);\n setTranscriptMap(new Map());\n setMessages([]);\n setState(\"disconnected\");\n\n if (options.onDisconnect) {\n options.onDisconnect();\n }\n }, [options]);\n\n // Update the send function with debugging and error checking\n const send = useCallback(\n ({ message, digit }: { message?: string; digit?: number }) => {\n if (state === \"disconnected\") {\n console.warn(\"Cannot send message: Not connected to a conversation\");\n return;\n }\n\n if (message) {\n // Add the message to our local transcript map for immediate feedback\n const messageId = `user-text-${Date.now()}`;\n setTranscriptMap((prevMap) => {\n const newMap = new Map(prevMap);\n newMap.set(messageId, {\n id: messageId,\n name: \"user\",\n message: message,\n timestamp: Date.now(),\n isFinal: true,\n });\n return newMap;\n });\n\n // Send message through the message channel to StateMonitor\n if (channelRef.current) {\n channelRef.current.port1.postMessage({\n type: \"send_text\",\n text: message,\n });\n } else {\n console.error(\"No message channel available to send message\");\n }\n }\n\n if (digit !== undefined) {\n // Send DTMF through the message channel to StateMonitor\n if (channelRef.current) {\n channelRef.current.port1.postMessage({\n type: \"send_dtmf\",\n digit: digit,\n });\n } else {\n console.error(\"No message channel available to send DTMF\");\n }\n }\n },\n [state]\n );\n\n // Update the audioWaveform function to return data for the requested speaker\n const audioWaveform = useCallback(\n ({\n speaker = \"agent\",\n barCount = 10,\n updateInterval = 20,\n }: {\n speaker?: \"agent\" | \"user\";\n barCount?: number;\n updateInterval?: number;\n }): number[] => {\n // Store the waveform configuration for StateMonitor to use\n waveformConfigRef.current = { speaker, barCount, updateInterval };\n\n // Send the configuration to StateMonitor if channel is available\n if (channelRef.current) {\n channelRef.current.port1.postMessage({\n type: \"waveform_config\",\n config: { speaker, barCount, updateInterval },\n });\n }\n\n // Get the waveform data for the requested speaker\n const speakerData = waveformDataMap[speaker] || [];\n\n // Return the current waveform data, or a default array if no data yet\n return speakerData.length > 0\n ? speakerData.slice(0, barCount) // Ensure we return only barCount items\n : Array(barCount).fill(0);\n },\n [waveformDataMap]\n );\n\n // Add toggleMic function that will be exposed in the hook's return value\n const toggleMic = useCallback(\n (value: boolean) => {\n setIsMicEnabled(value);\n\n // Send the command to the StateMonitor through the message channel\n if (channelRef.current) {\n channelRef.current.port1.postMessage({\n type: \"toggle_mic\",\n enabled: value,\n });\n } else {\n console.error(\"No message channel available to toggle microphone\");\n }\n },\n [options]\n );\n\n // Add setVolume function that will be exposed in the hook's return value\n const setVolume = useCallback((volume: number) => {\n // Validate volume (0-1 range)\n const validVolume = Math.min(Math.max(volume, 0), 1);\n\n // Send the command to the StateMonitor through the message channel\n if (channelRef.current) {\n channelRef.current.port1.postMessage({\n type: \"set_volume\",\n volume: validVolume,\n });\n } else {\n console.error(\"No message channel available to set volume\");\n }\n }, []);\n\n // Modify the useEffect hook that renders the LiveKit component\n useEffect(() => {\n if (!rootRef.current) return;\n\n if (connectionDetail) {\n // Only create a new LiveKit component if we don't have one or connection details changed\n if (!livekitComponentRef.current) {\n if (channelRef.current) {\n // Start port2 before passing it to StateMonitor\n channelRef.current.port2.start();\n }\n\n livekitComponentRef.current = (\n <LiveKitRoom\n serverUrl={connectionDetail.serverUrl}\n token={connectionDetail.participantToken}\n audio={true}\n video={false}\n connect={true}\n onDisconnected={disconnect}\n onError={(error) => {\n console.error(\"LiveKit connection error:\", error);\n disconnect();\n if (options.onError) {\n options.onError(\n new Error(`LiveKit connection error: ${error.message}`)\n );\n }\n }}\n >\n <RoomAudioRenderer />\n {channelRef.current && (\n <StateMonitor\n port={channelRef.current.port2}\n initialConfig={\n waveformConfigRef.current || {\n barCount: 10,\n updateInterval: 20,\n }\n }\n />\n )}\n </LiveKitRoom>\n );\n }\n\n rootRef.current.render(livekitComponentRef.current);\n } else {\n livekitComponentRef.current = null;\n rootRef.current.render(<></>);\n }\n }, [connectionDetail, disconnect, options.onError]);\n\n /**\n * Returns the VoxAI interface for controlling the conversation\n * @returns {Object} VoxAI interface\n * @property {Function} connect - Initiates a connection to the VoxAI service. Will reject with an error if already connected or in the process of connecting.\n * @property {Function} disconnect - Terminates the connection to the VoxAI service.\n * @property {VoxAgentState} state - The current state of the agent.\n * @property {VoxMessage[]} messages - An array of messages exchanged in the conversation.\n * @property {Function} send - Sends a message or DTMF digit to the agent.\n * @property {Function} audioWaveform - Returns audio waveform data for UI visualization.\n * @property {Function} toggleMic - Toggles the microphone on/off.\n * @property {Function} setVolume - Sets the volume of the agent's audio (0-1).\n */\n return {\n connect,\n disconnect,\n state,\n messages,\n send,\n audioWaveform,\n toggleMic,\n setVolume,\n };\n}\n\n/**\n * Component that monitors LiveKit state and communicates back to the main hook\n */\nfunction StateMonitor({\n port,\n initialConfig,\n}: {\n port: MessagePort | undefined;\n initialConfig: {\n speaker?: \"agent\" | \"user\";\n barCount: number;\n updateInterval: number;\n };\n}) {\n const { agent, state } = useVoiceAssistant();\n const { send: sendChat } = useChat();\n\n // Initialize waveform config with the passed initial values, defaulting to \"agent\" if not specified\n const [waveformConfig, setWaveformConfig] = useState({\n speaker: initialConfig.speaker || \"agent\",\n barCount: initialConfig.barCount,\n updateInterval: initialConfig.updateInterval,\n });\n\n // Agent transcriptions\n const agentAudioTrack = useParticipantTracks(\n [Track.Source.Microphone],\n agent?.identity\n )[0];\n const agentTranscription = useTrackTranscription(agentAudioTrack);\n\n // Use the current config for the waveform, applying different settings based on speaker\n const agentAudioWaveform = useAudioWaveform(agentAudioTrack, {\n barCount:\n waveformConfig.speaker === \"agent\" ? waveformConfig.barCount : 120, // default if not the selected speaker\n updateInterval:\n waveformConfig.speaker === \"agent\" ? waveformConfig.updateInterval : 20,\n });\n\n // User transcriptions\n const localParticipant = useLocalParticipant();\n const localMessages = useTrackTranscription({\n publication: localParticipant.microphoneTrack,\n source: Track.Source.Microphone,\n participant: localParticipant.localParticipant,\n });\n const localAudioTrack = useParticipantTracks(\n [Track.Source.Microphone],\n localParticipant.localParticipant.identity\n )[0];\n const userAudioWaveform = useAudioWaveform(localAudioTrack, {\n barCount: waveformConfig.speaker === \"user\" ? waveformConfig.barCount : 120, // default if not the selected speaker\n updateInterval:\n waveformConfig.speaker === \"user\" ? waveformConfig.updateInterval : 20,\n });\n\n // Add separate effects to send agent and user waveform data\n useEffect(() => {\n if (!port || !agentAudioWaveform || !agentAudioWaveform.bars) return;\n\n // Send the agent waveform data\n port.postMessage({\n type: \"waveform_update\",\n waveformData: agentAudioWaveform.bars,\n speaker: \"agent\",\n });\n }, [port, agentAudioWaveform]);\n\n useEffect(() => {\n if (!port || !userAudioWaveform || !userAudioWaveform.bars) return;\n\n // Send the user waveform data\n port.postMessage({\n type: \"waveform_update\",\n waveformData: userAudioWaveform.bars,\n speaker: \"user\",\n });\n }, [port, userAudioWaveform]);\n\n // Listen for messages including config updates and mic toggle commands\n useEffect(() => {\n if (!port) return;\n\n const handleMessage = (event: MessageEvent) => {\n const data = event.data;\n\n if (data.type === \"waveform_config\" && data.config) {\n // Verify we have both required properties before updating\n if (\n typeof data.config.barCount === \"number\" &&\n typeof data.config.updateInterval === \"number\"\n ) {\n setWaveformConfig(data.config);\n }\n } else if (data.type === \"send_text\") {\n if (sendChat) {\n sendChat(data.text);\n } else {\n console.error(\"sendChat function is not available\");\n }\n } else if (data.type === \"send_dtmf\") {\n if (localParticipant.localParticipant) {\n // Use standard DTMF code (RFC 4733)\n const standardDtmfCode = 101; // Standard DTMF payload type\n localParticipant.localParticipant.publishDtmf(\n standardDtmfCode,\n data.digit\n );\n } else {\n console.error(\"Local participant is not available for DTMF\");\n }\n } else if (\n data.type === \"toggle_mic\" &&\n typeof data.enabled === \"boolean\"\n ) {\n // Handle microphone toggle\n if (localParticipant.localParticipant) {\n localParticipant.localParticipant\n .setMicrophoneEnabled(data.enabled)\n .catch((error) => {\n console.error(\"Failed to toggle microphone:\", error);\n });\n } else {\n console.error(\"Local participant is not available for mic toggle\");\n }\n } else if (\n data.type === \"set_volume\" &&\n typeof data.volume === \"number\"\n ) {\n // Handle volume control\n if (agent) {\n // The agent is a RemoteParticipant, so we can call setVolume directly\n try {\n agent.setVolume(data.volume);\n console.log(`Set agent volume to ${data.volume}`);\n } catch (error) {\n console.error(\"Failed to set agent volume:\", error);\n }\n } else {\n console.error(\"Agent is not available for volume control\");\n }\n }\n };\n\n // Make sure we start the port\n port.start();\n\n port.addEventListener(\"message\", handleMessage);\n\n return () => {\n port.removeEventListener(\"message\", handleMessage);\n };\n }, [port, sendChat, localParticipant, agent]);\n\n // Send agent state updates\n useEffect(() => {\n if (port) {\n port.postMessage({ type: \"state_update\", state });\n }\n }, [state, port]);\n\n // Send agent transcriptions\n useEffect(() => {\n if (port && agentTranscription.segments.length > 0) {\n const transcriptions = agentTranscription.segments.map((segment) => ({\n id: segment.id,\n text: segment.text,\n isFinal: segment.final,\n timestamp: Date.now(),\n speaker: \"agent\" as const,\n }));\n\n port.postMessage({\n type: \"transcription_update\",\n transcriptions,\n });\n }\n }, [agentTranscription.segments, port]);\n\n // Send user transcriptions\n useEffect(() => {\n if (port && localMessages.segments.length > 0) {\n const transcriptions = localMessages.segments.map((segment) => ({\n id: segment.id,\n text: segment.text,\n isFinal: segment.final,\n timestamp: Date.now(),\n speaker: \"user\" as const,\n }));\n\n port.postMessage({\n type: \"transcription_update\",\n transcriptions,\n });\n }\n }, [localMessages.segments, port]);\n\n // Add data channel hook for function calls\n const { message: functionCallsCollected } = useDataChannel(\n \"function_calls_collected\",\n (msg) => {\n if (!port) return;\n\n const textDecoder = new TextDecoder();\n const messageString =\n msg.payload instanceof Uint8Array\n ? textDecoder.decode(msg.payload)\n : String(msg.payload);\n\n let functionCallInfo: FunctionCallInfo[];\n try {\n functionCallInfo = JSON.parse(messageString);\n\n // Send function calls to main hook via the port\n port.postMessage({\n type: \"function_calls_collected\",\n functionCalls: functionCallInfo,\n });\n } catch (e) {\n console.error(\"Failed to parse function call log:\", e);\n }\n }\n );\n\n // Add data channel hook for function call results\n const { message: functionCallsFinished } = useDataChannel(\n \"function_calls_finished\",\n (msg) => {\n if (!port) return;\n\n const textDecoder = new TextDecoder();\n const messageString =\n msg.payload instanceof Uint8Array\n ? textDecoder.decode(msg.payload)\n : String(msg.payload);\n\n let functionCallResult: FunctionCallResult[];\n try {\n functionCallResult = JSON.parse(messageString);\n\n // Send function results to main hook via the port\n port.postMessage({\n type: \"function_calls_finished\",\n functionResults: functionCallResult,\n });\n } catch (e) {\n console.error(\"Failed to parse function call result:\", e);\n }\n }\n );\n\n return null;\n}\n"],"names":["StateMonitor","_ref4","port","initialConfig","agent","state","useVoiceAssistant","send","sendChat","useChat","waveformConfig","setWaveformConfig","useState","speaker","barCount","updateInterval","agentAudioTrack","useParticipantTracks","Track","Source","Microphone","identity","agentTranscription","useTrackTranscription","agentAudioWaveform","useAudioWaveform","localParticipant","useLocalParticipant","localMessages","publication","microphoneTrack","source","participant","localAudioTrack","userAudioWaveform","useEffect","bars","postMessage","type","waveformData","handleMessage","event","data","config","text","console","error","publishDtmf","digit","enabled","setMicrophoneEnabled","catch","volume","setVolume","log","start","addEventListener","removeEventListener","segments","length","transcriptions","map","segment","id","isFinal","final","timestamp","Date","now","useDataChannel","msg","textDecoder","TextDecoder","messageString","payload","Uint8Array","decode","String","functionCallInfo","JSON","parse","functionCalls","e","functionCallResult","functionResults","options","connectionDetail","setConnectionDetail","setState","transcriptMap","setTranscriptMap","Map","messages","setMessages","prevMessagesRef","useRef","processedMessageIdsRef","Set","portalRootRef","rootRef","channelRef","livekitComponentRef","waveformDataMap","setWaveformDataMap","user","waveformConfigRef","isMicEnabled","setIsMicEnabled","allMessages","Array","from","values","sort","a","b","messagesString","stringify","current","onMessage","filter","has","forEach","add","channel","MessageChannel","port1","onmessage","handleTranscriptionUpdate","prevMap","functionCallsId","newMap","set","name","toolCalls","functionResultsId","toolCallResults","_channelRef$current","_channelRef$current2","close","port2","useCallback","t","_prevMap$get","messageType","existingTimestamp","get","message","div","document","createElement","style","display","body","appendChild","createRoot","unmount","removeChild","connect","_ref","agentId","apiKey","dynamicVariables","metadata","Promise","resolve","recover","result","errorMessage","warn","onError","Error","reject","fetch","method","headers","Authorization","agent_id","call_web","dynamic_variables","then","response","_temp2","_result2","_exit","json","onConnect","_temp","ok","errorText","status","_catch","err","disconnect","onDisconnect","_ref2","messageId","undefined","audioWaveform","_ref3","speakerData","slice","fill","toggleMic","value","validVolume","Math","min","max","_jsxs","LiveKitRoom","serverUrl","token","participantToken","audio","video","onDisconnected","children","_jsx","RoomAudioRenderer","render","_Fragment","Fragment"],"mappings":"uJAkmBA,SAASA,EAAYC,GAAC,IAAAC,KACpBA,E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AAOF,EAAAA,IAAAG,EAAAC,SAAA,MACzB,EACC,CAACvI,EAAkB0G,EAAY3G,EAAQoF,UAcnC,CACLZ,UACAmC,aACA3L,QACAuF,WACArF,OACA8L,gBACAK,YACArJ,YAEJ"}
|
|
1
|
+
{"version":3,"file":"lib.cjs","sources":["../src/hooks/useVoxAI.tsx","../src/utils/constants.ts"],"sourcesContent":["import {\n LiveKitRoom,\n RoomAudioRenderer,\n useAudioWaveform,\n useChat,\n useDataChannel,\n useLocalParticipant,\n useParticipantTracks,\n useTrackTranscription,\n useVoiceAssistant,\n} from \"@livekit/components-react\";\nimport { Track } from \"livekit-client\";\nimport { useCallback, useEffect, useRef, useState } from \"react\";\nimport { createRoot, Root } from \"react-dom/client\";\nimport { HTTPS_API_ORIGIN, SDK_VERSION } from \"../utils/constants\";\n\ntype VoxConnectionDetail = {\n serverUrl: string;\n roomName: string;\n participantName: string;\n participantToken: string;\n};\n\n/**\n * VoxAgentState\n * @description The state of the agent\n */\nexport type VoxAgentState =\n | \"disconnected\"\n | \"connecting\"\n | \"initializing\"\n | \"listening\"\n | \"thinking\"\n | \"speaking\";\n\n/**\n * Function call related types\n */\nexport interface FunctionToolsExecuted {\n type: \"function_tools_executed\";\n function_calls: FunctionCallInfo[];\n function_call_outputs: FunctionCallResult[];\n}\n\nexport interface FunctionCallInfo {\n id: string;\n type: string;\n call_id: string;\n arguments: string;\n name: string;\n}\n\nexport interface FunctionCallResult {\n id: string;\n name: string;\n type: string;\n call_id: string;\n output: string;\n is_error: boolean;\n}\n\n/**\n * VoxMessage\n * @description The message type between the agent and the user\n */\nexport type VoxMessage = {\n id?: string;\n name: \"agent\" | \"user\" | \"tool\";\n message?: string;\n timestamp: number;\n isFinal?: boolean;\n tool?: FunctionToolsExecuted;\n};\n\n/**\n * VoxAIOptions\n * @description The callback functions for the useVoxAI hook\n */\nexport interface VoxAIOptions {\n onConnect?: () => void;\n onDisconnect?: () => void;\n onError?: (error: Error) => void;\n onMessage?: (message: VoxMessage) => void;\n}\n\n// Message channel event types\ntype MessageChannelEvent =\n | { type: \"state_update\"; state: VoxAgentState }\n | { type: \"transcription_update\"; transcriptions: TranscriptionSegment[] }\n | {\n type: \"waveform_update\";\n waveformData: number[];\n speaker: \"agent\" | \"user\";\n }\n | { type: \"function_tools_executed\"; tool: FunctionToolsExecuted };\n\ntype TranscriptionSegment = {\n id: string;\n text: string;\n isFinal: boolean;\n timestamp: number;\n speaker: \"agent\" | \"user\";\n};\n\n/**\n * ConnectParams\n * @param agentId - The agent ID\n * @param apiKey - The API key\n * @param dynamicVariables - The dynamic variables\n * @param metadata - 이 메타데이터는 아웃바운드 웹훅, 통화 기록에 포함됩니다.\n */\nexport interface ConnectParams {\n agentId: string;\n apiKey: string;\n dynamicVariables?: Record<string, any>;\n metadata?: Record<string, any>;\n}\n\n/**\n * useVoxAI\n * @description The hook for integrating with vox.ai voice assistant\n * @param options - The options for the useVoxAI hook\n * @returns The useVoxAI hook\n * @example\n * const { connect, disconnect, state, messages, send } = useVoxAI({\n * onConnect: () => console.log(\"Connected\"),\n * onDisconnect: () => console.log(\"Disconnected\"),\n * onError: (error) => console.error(\"Error:\", error),\n * onMessage: (message) => console.log(\"Message:\", message),\n * });\n */\nexport function useVoxAI(options: VoxAIOptions = {}) {\n // Connection state\n const [connectionDetail, setConnectionDetail] =\n useState<VoxConnectionDetail | null>(null);\n const [state, setState] = useState<VoxAgentState>(\"disconnected\");\n\n // Session timestamp to filter out stale asynchronous events\n const sessionTimestampRef = useRef<number>(Date.now());\n\n // Message handling\n const [transcriptMap, setTranscriptMap] = useState<Map<string, VoxMessage>>(\n new Map()\n );\n const [messages, setMessages] = useState<VoxMessage[]>([]);\n const prevMessagesRef = useRef<string>(\"\");\n\n // Track which messages we've already sent to the onMessage callback\n const processedMessageIdsRef = useRef<Set<string>>(new Set());\n\n // DOM manipulation for LiveKit portal\n const portalRootRef = useRef<HTMLDivElement | null>(null);\n const rootRef = useRef<Root | null>(null);\n\n // Communication channel\n const channelRef = useRef<MessageChannel | null>(null);\n\n // Add this near the start of your useVoxAI hook\n const livekitComponentRef = useRef<React.ReactNode>(null);\n\n // Replace the single waveform state with a map for multiple speakers\n const [waveformDataMap, setWaveformDataMap] = useState<\n Record<string, number[]>\n >({\n agent: [],\n user: [],\n });\n\n // Add back the waveform config reference\n const waveformConfigRef = useRef<{\n speaker?: \"agent\" | \"user\";\n barCount: number;\n updateInterval: number;\n } | null>(null);\n\n // Add a new state to track microphone status\n const [isMicEnabled, setIsMicEnabled] = useState<boolean>(true);\n\n // Update messages whenever transcriptMap changes\n useEffect(() => {\n const allMessages = Array.from(transcriptMap.values()).sort(\n (a, b) => a.timestamp - b.timestamp\n );\n\n // Only update if the messages have actually changed\n const messagesString = JSON.stringify(allMessages);\n if (messagesString !== prevMessagesRef.current) {\n prevMessagesRef.current = messagesString;\n setMessages(allMessages);\n\n // Only trigger onMessage for new final messages that haven't been processed yet\n if (options.onMessage) {\n allMessages\n .filter(\n (msg) =>\n msg.isFinal &&\n msg.id &&\n !processedMessageIdsRef.current.has(msg.id)\n )\n .forEach((msg) => {\n if (msg.id) {\n // Mark this message as processed\n processedMessageIdsRef.current.add(msg.id);\n // Call the callback\n options.onMessage?.(msg);\n }\n });\n }\n }\n }, [transcriptMap, options.onMessage]);\n\n // Initialize message channel - ensure ports are properly connected\n useEffect(() => {\n const channel = new MessageChannel();\n\n channel.port1.onmessage = (e) => {\n const data = e.data as MessageChannelEvent;\n\n if (data.type === \"state_update\") {\n setState(data.state);\n } else if (data.type === \"transcription_update\") {\n handleTranscriptionUpdate(data.transcriptions);\n } else if (data.type === \"waveform_update\" && data.speaker) {\n // Store the waveform data for the specific speaker\n setWaveformDataMap((prevMap) => ({\n ...prevMap,\n [data.speaker]: data.waveformData,\n }));\n } else if (data.type === \"function_tools_executed\" && data.tool) {\n // Handle function calls\n const functionCallsId = `function-calls-${Date.now()}`;\n setTranscriptMap((prevMap) => {\n const newMap = new Map(prevMap);\n newMap.set(functionCallsId, {\n id: functionCallsId,\n name: \"tool\",\n tool: data.tool,\n timestamp: Date.now(),\n isFinal: true,\n });\n return newMap;\n });\n }\n };\n\n // Start the port\n channel.port1.start();\n\n // Store the channel reference\n channelRef.current = channel;\n\n return () => {\n channelRef.current?.port1.close();\n channelRef.current?.port2.close();\n channelRef.current = null;\n };\n }, []);\n\n // Process incoming transcriptions and filter out stale events\n const handleTranscriptionUpdate = useCallback(\n (transcriptions: TranscriptionSegment[]) => {\n setTranscriptMap((prevMap) => {\n const newMap = new Map(prevMap);\n\n transcriptions.forEach((t) => {\n // Only process transcriptions generated after the current session timestamp\n if (t.timestamp < sessionTimestampRef.current) {\n return;\n }\n const messageType = t.speaker === \"agent\" ? \"agent\" : \"user\";\n // Use existing timestamp if we already have this segment\n const existingTimestamp = prevMap.get(t.id)?.timestamp || t.timestamp;\n\n newMap.set(t.id, {\n id: t.id,\n name: messageType,\n message: t.text,\n timestamp: existingTimestamp,\n isFinal: t.isFinal,\n });\n });\n\n return newMap;\n });\n },\n []\n );\n\n // Set up DOM portal for LiveKit\n useEffect(() => {\n const div = document.createElement(\"div\");\n div.style.display = \"none\";\n document.body.appendChild(div);\n portalRootRef.current = div;\n rootRef.current = createRoot(div);\n\n return () => {\n if (rootRef.current) {\n rootRef.current.unmount();\n }\n if (portalRootRef.current) {\n document.body.removeChild(portalRootRef.current);\n }\n };\n }, []);\n\n // Connect to VoxAI service - updated to include dynamicVariables\n const connect = useCallback(\n async ({ agentId, apiKey, dynamicVariables, metadata }: ConnectParams) => {\n try {\n // Prevent connecting if already in a connection state\n if (state !== \"disconnected\") {\n const errorMessage = `Connection attempt rejected: Already in a connection state (${state})`;\n console.warn(errorMessage);\n\n if (options.onError) {\n options.onError(new Error(errorMessage));\n }\n return Promise.reject(new Error(errorMessage));\n }\n\n // Update session timestamp for new connection\n sessionTimestampRef.current = Date.now();\n setState(\"connecting\");\n\n const response = await fetch(HTTPS_API_ORIGIN, {\n method: \"POST\",\n headers: {\n Authorization: `Bearer ${apiKey}`,\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({\n agent_id: agentId,\n metadata: {\n runtime_context: {\n source: {\n type: \"react-sdk\",\n version: SDK_VERSION,\n },\n },\n call_web: {\n dynamic_variables: dynamicVariables || {},\n metadata: metadata || {},\n },\n },\n }),\n });\n\n if (!response.ok) {\n const errorText = await response.text();\n throw new Error(\n `Connection failed (${response.status}): ${errorText}`\n );\n }\n\n const data = await response.json();\n setConnectionDetail(data);\n\n if (options.onConnect) {\n options.onConnect();\n }\n } catch (err) {\n setConnectionDetail(null);\n setTranscriptMap(new Map());\n setMessages([]);\n setState(\"disconnected\");\n\n const error = err instanceof Error ? err : new Error(String(err));\n\n if (options.onError) {\n options.onError(error);\n }\n }\n },\n [options, state]\n );\n\n // Disconnect from VoxAI service, updating the session timestamp to ignore stale events\n const disconnect = useCallback(() => {\n // Update session timestamp on disconnect\n sessionTimestampRef.current = Date.now();\n setConnectionDetail(null);\n setTranscriptMap(new Map());\n setMessages([]);\n setState(\"disconnected\");\n\n if (options.onDisconnect) {\n options.onDisconnect();\n }\n }, [options]);\n\n // Update the send function with debugging and error checking\n const send = useCallback(\n ({ message, digit }: { message?: string; digit?: number }) => {\n if (state === \"disconnected\") {\n console.warn(\"Cannot send message: Not connected to a conversation\");\n return;\n }\n\n if (message) {\n const messageId = `user-text-${Date.now()}`;\n setTranscriptMap((prevMap) => {\n const newMap = new Map(prevMap);\n newMap.set(messageId, {\n id: messageId,\n name: \"user\",\n message: message,\n timestamp: Date.now(),\n isFinal: true,\n });\n return newMap;\n });\n\n if (channelRef.current) {\n channelRef.current.port1.postMessage({\n type: \"send_text\",\n text: message,\n });\n } else {\n console.error(\"No message channel available to send message\");\n }\n }\n\n if (digit !== undefined) {\n if (channelRef.current) {\n channelRef.current.port1.postMessage({\n type: \"send_dtmf\",\n digit: digit,\n });\n } else {\n console.error(\"No message channel available to send DTMF\");\n }\n }\n },\n [state]\n );\n\n // Update the audioWaveform function to return data for the requested speaker\n const audioWaveform = useCallback(\n ({\n speaker = \"agent\",\n barCount = 10,\n updateInterval = 20,\n }: {\n speaker?: \"agent\" | \"user\";\n barCount?: number;\n updateInterval?: number;\n }): number[] => {\n waveformConfigRef.current = { speaker, barCount, updateInterval };\n\n if (channelRef.current) {\n channelRef.current.port1.postMessage({\n type: \"waveform_config\",\n config: { speaker, barCount, updateInterval },\n });\n }\n\n const speakerData = waveformDataMap[speaker] || [];\n return speakerData.length > 0\n ? speakerData.slice(0, barCount)\n : Array(barCount).fill(0);\n },\n [waveformDataMap]\n );\n\n // Add toggleMic function that will be exposed in the hook's return value\n const toggleMic = useCallback((value: boolean) => {\n setIsMicEnabled(value);\n if (channelRef.current) {\n channelRef.current.port1.postMessage({\n type: \"toggle_mic\",\n enabled: value,\n });\n } else {\n console.error(\"No message channel available to toggle microphone\");\n }\n }, []);\n\n // Add setVolume function that will be exposed in the hook's return value\n const setVolume = useCallback((volume: number) => {\n const validVolume = Math.min(Math.max(volume, 0), 1);\n if (channelRef.current) {\n channelRef.current.port1.postMessage({\n type: \"set_volume\",\n volume: validVolume,\n });\n } else {\n console.error(\"No message channel available to set volume\");\n }\n }, []);\n\n // Modify the useEffect hook that renders the LiveKit component\n useEffect(() => {\n if (!rootRef.current) return;\n\n if (connectionDetail) {\n if (!livekitComponentRef.current) {\n if (channelRef.current) {\n channelRef.current.port2.start();\n }\n\n livekitComponentRef.current = (\n <LiveKitRoom\n serverUrl={connectionDetail.serverUrl}\n token={connectionDetail.participantToken}\n audio={true}\n video={false}\n connect={true}\n onDisconnected={disconnect}\n onError={(error) => {\n console.error(\"LiveKit connection error:\", error);\n disconnect();\n if (options.onError) {\n options.onError(\n new Error(`LiveKit connection error: ${error.message}`)\n );\n }\n }}\n >\n <RoomAudioRenderer />\n {channelRef.current && (\n <StateMonitor\n port={channelRef.current.port2}\n initialConfig={\n waveformConfigRef.current || {\n barCount: 10,\n updateInterval: 20,\n }\n }\n />\n )}\n </LiveKitRoom>\n );\n }\n rootRef.current.render(livekitComponentRef.current);\n } else {\n livekitComponentRef.current = null;\n rootRef.current.render(<></>);\n }\n }, [connectionDetail, disconnect, options.onError]);\n\n return {\n connect,\n disconnect,\n state,\n messages,\n send,\n audioWaveform,\n toggleMic,\n setVolume,\n };\n}\n\n/**\n * Component that monitors LiveKit state and communicates back to the main hook\n */\nfunction StateMonitor({\n port,\n initialConfig,\n}: {\n port: MessagePort | undefined;\n initialConfig: {\n speaker?: \"agent\" | \"user\";\n barCount: number;\n updateInterval: number;\n };\n}) {\n const { agent, state } = useVoiceAssistant();\n const { send: sendChat } = useChat();\n\n // Initialize waveform config with the passed initial values, defaulting to \"agent\" if not specified\n const [waveformConfig, setWaveformConfig] = useState({\n speaker: initialConfig.speaker || \"agent\",\n barCount: initialConfig.barCount,\n updateInterval: initialConfig.updateInterval,\n });\n\n // Agent transcriptions\n const agentAudioTrack = useParticipantTracks(\n [Track.Source.Microphone],\n agent?.identity\n )[0];\n const agentTranscription = useTrackTranscription(agentAudioTrack);\n\n // Use the current config for the waveform, applying different settings based on speaker\n const agentAudioWaveform = useAudioWaveform(agentAudioTrack, {\n barCount:\n waveformConfig.speaker === \"agent\" ? waveformConfig.barCount : 120, // default if not the selected speaker\n updateInterval:\n waveformConfig.speaker === \"agent\" ? waveformConfig.updateInterval : 20,\n });\n\n // User transcriptions\n const localParticipant = useLocalParticipant();\n const localMessages = useTrackTranscription({\n publication: localParticipant.microphoneTrack,\n source: Track.Source.Microphone,\n participant: localParticipant.localParticipant,\n });\n const localAudioTrack = useParticipantTracks(\n [Track.Source.Microphone],\n localParticipant.localParticipant.identity\n )[0];\n const userAudioWaveform = useAudioWaveform(localAudioTrack, {\n barCount: waveformConfig.speaker === \"user\" ? waveformConfig.barCount : 120, // default if not the selected speaker\n updateInterval:\n waveformConfig.speaker === \"user\" ? waveformConfig.updateInterval : 20,\n });\n\n // Add separate effects to send agent and user waveform data\n useEffect(() => {\n if (!port || !agentAudioWaveform || !agentAudioWaveform.bars) return;\n\n // Send the agent waveform data\n port.postMessage({\n type: \"waveform_update\",\n waveformData: agentAudioWaveform.bars,\n speaker: \"agent\",\n });\n }, [port, agentAudioWaveform]);\n\n useEffect(() => {\n if (!port || !userAudioWaveform || !userAudioWaveform.bars) return;\n\n // Send the user waveform data\n port.postMessage({\n type: \"waveform_update\",\n waveformData: userAudioWaveform.bars,\n speaker: \"user\",\n });\n }, [port, userAudioWaveform]);\n\n // Listen for messages including config updates and mic toggle commands\n useEffect(() => {\n if (!port) return;\n\n const handleMessage = (event: MessageEvent) => {\n const data = event.data;\n\n if (data.type === \"waveform_config\" && data.config) {\n // Verify we have both required properties before updating\n if (\n typeof data.config.barCount === \"number\" &&\n typeof data.config.updateInterval === \"number\"\n ) {\n setWaveformConfig(data.config);\n }\n } else if (data.type === \"send_text\") {\n if (sendChat) {\n sendChat(data.text);\n } else {\n console.error(\"sendChat function is not available\");\n }\n } else if (data.type === \"send_dtmf\") {\n if (localParticipant.localParticipant) {\n // Use standard DTMF code (RFC 4733)\n const standardDtmfCode = 101; // Standard DTMF payload type\n localParticipant.localParticipant.publishDtmf(\n standardDtmfCode,\n data.digit\n );\n } else {\n console.error(\"Local participant is not available for DTMF\");\n }\n } else if (\n data.type === \"toggle_mic\" &&\n typeof data.enabled === \"boolean\"\n ) {\n // Handle microphone toggle\n if (localParticipant.localParticipant) {\n localParticipant.localParticipant\n .setMicrophoneEnabled(data.enabled)\n .catch((error) => {\n console.error(\"Failed to toggle microphone:\", error);\n });\n } else {\n console.error(\"Local participant is not available for mic toggle\");\n }\n } else if (\n data.type === \"set_volume\" &&\n typeof data.volume === \"number\"\n ) {\n // Handle volume control\n if (agent) {\n // The agent is a RemoteParticipant, so we can call setVolume directly\n try {\n agent.setVolume(data.volume);\n console.log(`Set agent volume to ${data.volume}`);\n } catch (error) {\n console.error(\"Failed to set agent volume:\", error);\n }\n } else {\n console.error(\"Agent is not available for volume control\");\n }\n }\n };\n\n // Make sure we start the port\n port.start();\n\n port.addEventListener(\"message\", handleMessage);\n\n return () => {\n port.removeEventListener(\"message\", handleMessage);\n };\n }, [port, sendChat, localParticipant, agent]);\n\n // Send agent state updates\n useEffect(() => {\n if (port) {\n port.postMessage({ type: \"state_update\", state });\n }\n }, [state, port]);\n\n // Send agent transcriptions\n useEffect(() => {\n if (port && agentTranscription.segments.length > 0) {\n const transcriptions = agentTranscription.segments.map((segment) => ({\n id: segment.id,\n text: segment.text,\n isFinal: segment.final,\n timestamp: Date.now(),\n speaker: \"agent\" as const,\n }));\n\n port.postMessage({\n type: \"transcription_update\",\n transcriptions,\n });\n }\n }, [agentTranscription.segments, port]);\n\n // Send user transcriptions\n useEffect(() => {\n if (port && localMessages.segments.length > 0) {\n const transcriptions = localMessages.segments.map((segment) => ({\n id: segment.id,\n text: segment.text,\n isFinal: segment.final,\n timestamp: Date.now(),\n speaker: \"user\" as const,\n }));\n\n port.postMessage({\n type: \"transcription_update\",\n transcriptions,\n });\n }\n }, [localMessages.segments, port]);\n\n // Add data channel hook for function calls\n const { message: functionToolsExecuted } = useDataChannel(\n \"function_tools_executed\",\n (msg) => {\n if (!port) return;\n\n const textDecoder = new TextDecoder();\n const messageString =\n msg.payload instanceof Uint8Array\n ? textDecoder.decode(msg.payload)\n : String(msg.payload);\n\n let tool: FunctionToolsExecuted;\n try {\n tool = JSON.parse(messageString);\n\n // Send function calls to main hook via the port\n port.postMessage({\n type: \"function_tools_executed\",\n tool: tool,\n });\n } catch (e) {\n console.error(\"Failed to parse function call log:\", e);\n }\n }\n );\n\n return null;\n}\n","// export const HTTPS_API_ORIGIN = \"https://frontend-dev.tryvox.co/api/agent/sdk\";\nexport const HTTPS_API_ORIGIN = \"https://www.tryvox.co/api/agent/sdk\";\n// export const HTTPS_API_ORIGIN = \"http://localhost:3000/api/agent/sdk\";\nexport const SDK_VERSION = \"0.3.0\";\n"],"names":["StateMonitor","_ref4","port","initialConfig","agent","state","useVoiceAssistant","send","sendChat","useChat","waveformConfig","setWaveformConfig","useState","speaker","barCount","updateInterval","agentAudioTrack","useParticipantTracks","Track","Source","Microphone","identity","agentTranscription","useTrackTranscription","agentAudioWaveform","useAudioWaveform","localParticipant","useLocalParticipant","localMessages","publication","microphoneTrack","source","participant","localAudioTrack","userAudioWaveform","useEffect","bars","postMessage","type","waveformData","handleMessage","event","data","config","text","console","error","publishDtmf","digit","enabled","setMicrophoneEnabled","catch","volume","setVolume","log","start","addEventListener","removeEventListener","segments","length","transcriptions","map","segment","id","isFinal","final","timestamp","Date","now","useDataChannel","msg","textDecoder","TextDecoder","messageString","payload","Uint8Array","decode","String","tool","JSON","parse","e","options","connectionDetail","setConnectionDetail","setState","sessionTimestampRef","useRef","transcriptMap","setTranscriptMap","Map","messages","setMessages","prevMessagesRef","processedMessageIdsRef","Set","portalRootRef","rootRef","channelRef","livekitComponentRef","waveformDataMap","setWaveformDataMap","user","waveformConfigRef","isMicEnabled","setIsMicEnabled","allMessages","Array","from","values","sort","a","b","messagesString","stringify","current","onMessage","filter","has","forEach","add","channel","MessageChannel","port1","onmessage","handleTranscriptionUpdate","prevMap","functionCallsId","newMap","set","name","_channelRef$current","_channelRef$current2","close","port2","useCallback","t","_prevMap$get","messageType","existingTimestamp","get","message","div","document","createElement","style","display","body","appendChild","createRoot","unmount","removeChild","connect","_ref","agentId","apiKey","dynamicVariables","metadata","Promise","resolve","errorMessage","warn","onError","Error","reject","fetch","method","headers","Authorization","agent_id","runtime_context","version","call_web","dynamic_variables","then","response","_temp2","_result2","_exit","json","onConnect","_temp","ok","errorText","status","_catch","err","disconnect","onDisconnect","_ref2","messageId","undefined","audioWaveform","_ref3","speakerData","slice","fill","toggleMic","value","validVolume","Math","min","max","_jsxs","LiveKitRoom","serverUrl","token","participantToken","audio","video","onDisconnected","children","_jsx","RoomAudioRenderer","render","_Fragment"],"mappings":"uJA4iBA,SAASA,EAAYC,GAAC,IAAAC,KACpBA,EAAIC,cACJA,GAQDF,EACC,MAAMG,MAAEA,EAAKC,MAAEA,GAAUC,EAAAA,qBACjBC,KAAMC,GAAa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,UAAU,KACH8D,EAAQkB,UAEThC,GACGgB,EAAoBgB,UACnBjB,EAAWiB,SACbjB,EAAWiB,QAAQmB,MAAM/E,QAG3B4C,EAAoBgB,QAClBwF,EAAAA,KAACC,EAAWA,aACVC,UAAW1H,EAAiB0H,UAC5BC,MAAO3H,EAAiB4H,iBACxBC,OAAO,EACPC,OAAO,EACPzD,SAAS,EACT0D,eAAgBvB,EAChBzB,QAAUpH,IACRD,QAAQC,MAAM,4BAA6BA,GAC3C6I,IACIzG,EAAQgF,SACVhF,EAAQgF,QACN,IAAIC,MAAmCrH,6BAAAA,EAAM+F,SAEjD,EACDsE,SAAA,CAEDC,EAAAA,IAACC,EAAiBA,kBAAG,IACpBnH,EAAWiB,SACViG,EAAAA,IAACpN,EAAY,CACXE,KAAMgG,EAAWiB,QAAQmB,MACzBnI,cACEoG,EAAkBY,SAAW,CAC3BrG,SAAU,GACVC,eAAgB,UAQ9BkF,EAAQkB,QAAQmG,OAAOnH,EAAoBgB,WAE3ChB,EAAoBgB,QAAU,KAC9BlB,EAAQkB,QAAQmG,OAAOF,MAAAG,EAAAA,SAAA,CAAA,KACzB,EACC,CAACpI,EAAkBwG,EAAYzG,EAAQgF,UAEnC,CACLV,UACAmC,aACAtL,QACAsF,WACApF,OACAyL,gBACAK,YACAhJ,YAEJ"}
|
package/dist/lib.modern.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{jsxs as e,jsx as t,Fragment as n}from"react/jsx-runtime";import{LiveKitRoom as r,RoomAudioRenderer as o,useVoiceAssistant as a,useChat as s,useParticipantTracks as i,useTrackTranscription as c,useAudioWaveform as l,useLocalParticipant as u,useDataChannel as p}from"@livekit/components-react";import{Track as d}from"livekit-client";import{useState as m,useRef as
|
|
1
|
+
import{jsxs as e,jsx as t,Fragment as n}from"react/jsx-runtime";import{LiveKitRoom as r,RoomAudioRenderer as o,useVoiceAssistant as a,useChat as s,useParticipantTracks as i,useTrackTranscription as c,useAudioWaveform as l,useLocalParticipant as u,useDataChannel as p}from"@livekit/components-react";import{Track as d}from"livekit-client";import{useState as m,useRef as g,useEffect as f,useCallback as v}from"react";import{createRoot as y}from"react-dom/client";function b(){return b=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)({}).hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},b.apply(null,arguments)}function w(a={}){const[s,i]=m(null),[c,l]=m("disconnected"),u=g(Date.now()),[p,d]=m(new Map),[w,M]=m([]),_=g(""),k=g(new Set),C=g(null),x=g(null),E=g(null),D=g(null),[F,I]=m({agent:[],user:[]}),S=g(null),[P,N]=m(!0);f(()=>{const e=Array.from(p.values()).sort((e,t)=>e.timestamp-t.timestamp),t=JSON.stringify(e);t!==_.current&&(_.current=t,M(e),a.onMessage&&e.filter(e=>e.isFinal&&e.id&&!k.current.has(e.id)).forEach(e=>{e.id&&(k.current.add(e.id),null==a.onMessage||a.onMessage(e))}))},[p,a.onMessage]),f(()=>{const e=new MessageChannel;return e.port1.onmessage=e=>{const t=e.data;if("state_update"===t.type)l(t.state);else if("transcription_update"===t.type)$(t.transcriptions);else if("waveform_update"===t.type&&t.speaker)I(e=>b({},e,{[t.speaker]:t.waveformData}));else if("function_tools_executed"===t.type&&t.tool){const e=`function-calls-${Date.now()}`;d(n=>{const r=new Map(n);return r.set(e,{id:e,name:"tool",tool:t.tool,timestamp:Date.now(),isFinal:!0}),r})}},e.port1.start(),E.current=e,()=>{var e,t;null==(e=E.current)||e.port1.close(),null==(t=E.current)||t.port2.close(),E.current=null}},[]);const $=v(e=>{d(t=>{const n=new Map(t);return e.forEach(e=>{var r;if(e.timestamp<u.current)return;const o="agent"===e.speaker?"agent":"user",a=(null==(r=t.get(e.id))?void 0:r.timestamp)||e.timestamp;n.set(e.id,{id:e.id,name:o,message:e.text,timestamp:a,isFinal:e.isFinal})}),n})},[]);f(()=>{const e=document.createElement("div");return e.style.display="none",document.body.appendChild(e),C.current=e,x.current=y(e),()=>{x.current&&x.current.unmount(),C.current&&document.body.removeChild(C.current)}},[]);const j=v(async({agentId:e,apiKey:t,dynamicVariables:n,metadata:r})=>{try{if("disconnected"!==c){const e=`Connection attempt rejected: Already in a connection state (${c})`;return console.warn(e),a.onError&&a.onError(new Error(e)),Promise.reject(new Error(e))}u.current=Date.now(),l("connecting");const o=await fetch("https://www.tryvox.co/api/agent/sdk",{method:"POST",headers:{Authorization:`Bearer ${t}`,"Content-Type":"application/json"},body:JSON.stringify({agent_id:e,metadata:{runtime_context:{source:{type:"react-sdk",version:"0.3.0"}},call_web:{dynamic_variables:n||{},metadata:r||{}}}})});if(!o.ok){const e=await o.text();throw new Error(`Connection failed (${o.status}): ${e}`)}const s=await o.json();i(s),a.onConnect&&a.onConnect()}catch(e){i(null),d(new Map),M([]),l("disconnected");const t=e instanceof Error?e:new Error(String(e));a.onError&&a.onError(t)}},[a,c]),O=v(()=>{u.current=Date.now(),i(null),d(new Map),M([]),l("disconnected"),a.onDisconnect&&a.onDisconnect()},[a]),T=v(({message:e,digit:t})=>{if("disconnected"!==c){if(e){const t=`user-text-${Date.now()}`;d(n=>{const r=new Map(n);return r.set(t,{id:t,name:"user",message:e,timestamp:Date.now(),isFinal:!0}),r}),E.current?E.current.port1.postMessage({type:"send_text",text:e}):console.error("No message channel available to send message")}void 0!==t&&(E.current?E.current.port1.postMessage({type:"send_dtmf",digit:t}):console.error("No message channel available to send DTMF"))}else console.warn("Cannot send message: Not connected to a conversation")},[c]),A=v(({speaker:e="agent",barCount:t=10,updateInterval:n=20})=>{S.current={speaker:e,barCount:t,updateInterval:n},E.current&&E.current.port1.postMessage({type:"waveform_config",config:{speaker:e,barCount:t,updateInterval:n}});const r=F[e]||[];return r.length>0?r.slice(0,t):Array(t).fill(0)},[F]),L=v(e=>{N(e),E.current?E.current.port1.postMessage({type:"toggle_mic",enabled:e}):console.error("No message channel available to toggle microphone")},[]),J=v(e=>{const t=Math.min(Math.max(e,0),1);E.current?E.current.port1.postMessage({type:"set_volume",volume:t}):console.error("No message channel available to set volume")},[]);return f(()=>{x.current&&(s?(D.current||(E.current&&E.current.port2.start(),D.current=e(r,{serverUrl:s.serverUrl,token:s.participantToken,audio:!0,video:!1,connect:!0,onDisconnected:O,onError:e=>{console.error("LiveKit connection error:",e),O(),a.onError&&a.onError(new Error(`LiveKit connection error: ${e.message}`))},children:[t(o,{}),E.current&&t(h,{port:E.current.port2,initialConfig:S.current||{barCount:10,updateInterval:20}})]})),x.current.render(D.current)):(D.current=null,x.current.render(t(n,{}))))},[s,O,a.onError]),{connect:j,disconnect:O,state:c,messages:w,send:T,audioWaveform:A,toggleMic:L,setVolume:J}}function h({port:e,initialConfig:t}){const{agent:n,state:r}=a(),{send:o}=s(),[g,v]=m({speaker:t.speaker||"agent",barCount:t.barCount,updateInterval:t.updateInterval}),y=i([d.Source.Microphone],null==n?void 0:n.identity)[0],b=c(y),w=l(y,{barCount:"agent"===g.speaker?g.barCount:120,updateInterval:"agent"===g.speaker?g.updateInterval:20}),h=u(),M=c({publication:h.microphoneTrack,source:d.Source.Microphone,participant:h.localParticipant}),_=i([d.Source.Microphone],h.localParticipant.identity)[0],k=l(_,{barCount:"user"===g.speaker?g.barCount:120,updateInterval:"user"===g.speaker?g.updateInterval:20});return f(()=>{e&&w&&w.bars&&e.postMessage({type:"waveform_update",waveformData:w.bars,speaker:"agent"})},[e,w]),f(()=>{e&&k&&k.bars&&e.postMessage({type:"waveform_update",waveformData:k.bars,speaker:"user"})},[e,k]),f(()=>{if(!e)return;const t=e=>{const t=e.data;if("waveform_config"===t.type&&t.config)"number"==typeof t.config.barCount&&"number"==typeof t.config.updateInterval&&v(t.config);else if("send_text"===t.type)o?o(t.text):console.error("sendChat function is not available");else if("send_dtmf"===t.type)h.localParticipant?h.localParticipant.publishDtmf(101,t.digit):console.error("Local participant is not available for DTMF");else if("toggle_mic"===t.type&&"boolean"==typeof t.enabled)h.localParticipant?h.localParticipant.setMicrophoneEnabled(t.enabled).catch(e=>{console.error("Failed to toggle microphone:",e)}):console.error("Local participant is not available for mic toggle");else if("set_volume"===t.type&&"number"==typeof t.volume)if(n)try{n.setVolume(t.volume),console.log(`Set agent volume to ${t.volume}`)}catch(e){console.error("Failed to set agent volume:",e)}else console.error("Agent is not available for volume control")};return e.start(),e.addEventListener("message",t),()=>{e.removeEventListener("message",t)}},[e,o,h,n]),f(()=>{e&&e.postMessage({type:"state_update",state:r})},[r,e]),f(()=>{if(e&&b.segments.length>0){const t=b.segments.map(e=>({id:e.id,text:e.text,isFinal:e.final,timestamp:Date.now(),speaker:"agent"}));e.postMessage({type:"transcription_update",transcriptions:t})}},[b.segments,e]),f(()=>{if(e&&M.segments.length>0){const t=M.segments.map(e=>({id:e.id,text:e.text,isFinal:e.final,timestamp:Date.now(),speaker:"user"}));e.postMessage({type:"transcription_update",transcriptions:t})}},[M.segments,e]),p("function_tools_executed",t=>{if(!e)return;const n=new TextDecoder,r=t.payload instanceof Uint8Array?n.decode(t.payload):String(t.payload);let o;try{o=JSON.parse(r),e.postMessage({type:"function_tools_executed",tool:o})}catch(e){console.error("Failed to parse function call log:",e)}}),null}export{w as useVoxAI};
|
|
2
2
|
//# sourceMappingURL=lib.modern.js.map
|