1import{j as n}from"./ui-D6nG3JCa.js";import{r as o}from"./vendor-TIPSVta7.js";import{bO as V,bP as M,u as B,f as K,c as P,B as U}from"./index-BCab1SPF.js";function I(){return I=Object.assign?Object.assign.bind():function(p){for(var a=1;a<arguments.length;a++){var i=arguments[a];for(var u in i)({}).hasOwnProperty.call(i,u)&&(p[u]=i[u])}return p},I.apply(null,arguments)}const q=["micMuted","volume"];function A(p={}){const{micMuted:a,volume:i}=p,u=function(e,r){if(e==null)return{};var m={};for(var x in e)if({}.hasOwnProperty.call(e,x)){if(r.includes(x))continue;m[x]=e[x]}return m}(p,q),s=o.useRef(null),f=o.useRef(null),[h,b]=o.useState("disconnected"),[w,c]=o.useState(!1),[g,v]=o.useState("listening");return o.useEffect(()=>{var e;a!==void 0&&(s==null||(e=s.current)==null||e.setMicMuted(a))},[a]),o.useEffect(()=>{var e;i!==void 0&&(s==null||(e=s.current)==null||e.setVolume({volume:i}))},[i]),o.useEffect(()=>()=>{var e;(e=s.current)==null||e.endSession()},[]),{startSession:async e=>{var r;if((r=s.current)!=null&&r.isOpen())return s.current.getId();if(f.current)return(await f.current).getId();try{return f.current=V.startSession(I({},u??{},e??{},{onModeChange:({mode:m})=>{v(m)},onStatusChange:({status:m})=>{b(m)},onCanSendFeedbackChange:({canSendFeedback:m})=>{c(m)}})),s.current=await f.current,a!==void 0&&s.current.setMicMuted(a),i!==void 0&&s.current.setVolume({volume:i}),s.current.getId()}finally{f.current=null}},endSession:async()=>{const e=s.current;s.current=null,await(e==null?void 0:e.endSession())},setVolume:({volume:e})=>{var r;(r=s.current)==null||r.setVolume({volume:e})},getInputByteFrequencyData:()=>{var e;return(e=s.current)==null?void 0:e.getInputByteFrequencyData()},getOutputByteFrequencyData:()=>{var e;return(e=s.current)==null?void 0:e.getOutputByteFrequencyData()},getInputVolume:()=>{var e,r;return(e=(r=s.current)==null?void 0:r.getInputVolume())!=null?e:0},getOutputVolume:()=>{var e,r;return(e=(r=s.current)==null?void 0:r.getOutputVolume())!=null?e:0},sendFeedback:e=>{var r;(r=s.current)==null||r.sendFeedback(e)},getId:()=>{var e;return(e=s.current)==null?void 0:e.getId()},sendContextualUpdate:e=>{var r;(r=s.current)==null||r.sendContextualUpdate(e)},status:h,canSendFeedback:w,micMuted:a,isSpeaking:g==="speaking"}}function Q({biographyId:p=70,chapterId:a="test-react-sdk",sectionId:i,initialQuestion:u="What is an important chapter or event in your life that you would like to share with me today?",onEvent:s,className:f=""}){const[h,b]=o.useState(""),[w,c]=o.useState("C5VQxDbkGBC7ox1TeRBC"),[g]=o.useState(`sdk-widget-${M()}`),[v,e]=o.useState([]),[r,m]=o.useState(""),x=o.useRef(null),[k,$]=o.useState(!1),d=A({onConnect:t=>{console.log("Connected to ElevenLabs conversation:",t.conversationId),y("system","Connected to ElevenLabs conversation"),u&&setTimeout(()=>{C(u)},500)},onDisconnect:t=>{console.log("Disconnected from ElevenLabs conversation:",t),y("system",`Disconnected: ${t.reason||"Unknown reason"}`)},onMessage:t=>{t.source==="ai"&&y("assistant",t.message)},onError:(t,l)=>{console.error("ElevenLabs conversation error:",t,l),y("system",`Error: ${t}`),E({type:"error",text:t,timestamp:Date.now(),conversationId:g})}});o.useEffect(()=>{(async()=>{try{const l=await fetch("/api/csrf-token");if(!l.ok)throw new Error("Failed to get CSRF token");const{csrfToken:S}=await l.json();if(!S)throw new Error("Invalid CSRF token response");const j=await fetch("/api/elevenlabs/get-api-key",{method:"POST",headers:{"Content-Type":"application/json","CSRF-Token":S}});if(!j.ok)throw new Error("Failed to get API key");const L=await j.json(),D=await fetch("/api/elevenlabs/agent-id",{headers:{"CSRF-Token":S}});if(D.ok){const R=await D.json();R.agentId&&c(R.agentId)}b(L.apiKey)}catch(l){console.error("Error fetching credentials:",l),y("system",`Error: ${l instanceof Error?l.message:"Unknown error"}`),E({type:"error",text:l instanceof Error?l.message:"Unknown error",timestamp:Date.now(),conversationId:g})}})()},[g]),o.useEffect(()=>(h&&!k&&T(),()=>{k&&d.endSession()}),[h,k]),o.useEffect(()=>{var t;(t=x.current)==null||t.scrollIntoView({behavior:"smooth"})},[v]);const T=async()=>{try{if(!h)throw new Error("API key not available");console.log("Starting ElevenLabs conversation session with agent:",w),await d.startSession({authorization:h,agentId:w}),$(!0)}catch(t){console.error("Failed to start conversation session:",t),y("system",`Failed to start conversation: ${t instanceof Error?t.message:"Unknown error"}`)}},E=t=>{s&&s(t)},y=(t,l)=>{const S={id:`msg-${M()}`,role:t,content:l,timestamp:new Date};e(j=>[...j,S]),(t==="user"||t==="assistant")&&E({type:`${t}-message`,text:l,timestamp:Date.now(),conversationId:g,messageId:S.id})},C=t=>{t.trim()&&(y("user",t),d.sendContextualUpdate(t),m(""))},O=t=>{t.preventDefault(),C(r)},N=t=>{switch(t){case"connected":return"bg-green-500";case"connecting":return"bg-amber-500 animate-pulse";case"disconnecting":case"disconnected":default:return"bg-red-500"}},F=t=>{switch(t){case"connected":return"Connected";case"connecting":return"Connecting...";case"disconnecting":return"Disconnecting...";case"disconnected":default:return"Disconnected"}};return h?n.jsxs("div",{className:`flex flex-col h-full bg-white rounded-md overflow-hidden border ${f}`,children:[n.jsxs("div",{className:"p-3 bg-white border-b flex justify-between items-center",children:[n.jsxs("div",{className:"flex items-center gap-2",children:[n.jsx("h3",{className:"text-lg font-medium",children:"Interview Assistant"}),n.jsx("div",{className:`h-2.5 w-2.5 rounded-full ${N(d.status)}`}),n.jsx("span",{className:"text-xs text-gray-600",children:F(d.status)})]}),n.jsxs("div",{className:"flex items-center gap-2",children:[d.isSpeaking&&n.jsx("span",{className:"text-xs bg-blue-100 text-blue-800 px-2 py-0.5 rounded-full font-medium",children:"Speaking"}),d.micMuted?n.jsx("button",{onClick:()=>d.setVolume({volume:1}),className:"text-xs bg-red-100 text-red-800 px-2 py-0.5 rounded-full font-medium",children:"Mic Muted"}):null]})]}),n.jsxs("div",{className:"flex-1 overflow-y-auto p-4 space-y-4",children:[v.map(t=>n.jsx("div",{className:`flex ${t.role==="user"?"justify-end":"justify-start"}`,children:n.jsx("div",{className:`max-w-[80%] p-3 rounded-lg ${t.role==="user"?"bg-blue-500 text-white rounded-br-none":t.role==="assistant"?"bg-gray-100 text-gray-800 rounded-bl-none":"bg-gray-200 text-gray-800 w-full text-center text-sm italic"}`,children:t.content})},t.id)),n.jsx("div",{ref:x})]}),n.jsxs("form",{onSubmit:O,className:"p-3 border-t flex items-center gap-2",children:[n.jsx("input",{type:"text",value:r,onChange:t=>m(t.target.value),placeholder:"Type your message...",className:"flex-1 p-2 border rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500",disabled:d.status!=="connected"}),n.jsx("button",{type:"submit",className:"bg-blue-500 text-white p-2 rounded-md hover:bg-blue-600 focus:outline-none focus:ring-2 focus:ring-blue-500 disabled:bg-gray-300 disabled:cursor-not-allowed",disabled:!r.trim()||d.status!=="connected",children:"Send"})]}),n.jsxs("div",{className:"px-3 py-1 bg-gray-50 border-t text-xs text-gray-500 flex justify-between items-center",children:[n.jsxs("div",{className:"flex items-center gap-1.5",children:[n.jsx("div",{className:`h-2 w-2 rounded-full ${N(d.status)}`}),n.jsxs("span",{children:["WebSocket: ",F(d.status)]})]}),n.jsx("span",{children:g?`Session: ${g.substring(0,8)}...`:""})]})]}):n.jsx("div",{className:`flex flex-col h-full bg-white rounded-md overflow-hidden border ${f}`,children:n.jsx("div",{className:"p-4 text-center",children:n.jsx("p",{children:"Loading conversation..."})})})}function H(){const{id:p,chapterId:a}=B(),i=parseInt(p),{toast:u}=K(),[s,f]=o.useState(""),{isLoading:h,error:b}=P({queryKey:["/api/elevenlabs/get-initial-prompt",i,a],queryFn:async()=>{try{const c=await fetch("/api/csrf-token");if(!c.ok)throw new Error("Failed to get CSRF token");const{csrfToken:g}=await c.json(),v=await fetch("/api/elevenlabs/get-initial-prompt",{method:"POST",headers:{"Content-Type":"application/json","CSRF-Token":g},body:JSON.stringify({biographyId:i,chapterId:a,mode:"qa"})});if(!v.ok)throw new Error("Failed to get initial prompt");const e=await v.json();return e.initialQuestion&&f(e.initialQuestion),e}catch(c){throw console.error("Error fetching initial prompt:",c),u({title:"Error",description:`Failed to get initial prompt: ${c instanceof Error?c.message:"Unknown error"}`,variant:"destructive"}),c}},enabled:!!(i&&a)}),w=c=>{console.log("Event received:",c),c.type==="error"&&u({title:"Conversation Error",description:c.text,variant:"destructive"})};return o.useEffect(()=>{b&&f("What is an important chapter or event in your life that you would like to share with me today?")},[b]),n.jsx(U,{children:n.jsxs("div",{className:"container mx-auto py-6 px-4",children:[n.jsx("h1",{className:"text-2xl font-bold mb-4",children:"AI Interview (SDK Version)"}),n.jsx("p",{className:"text-gray-600 mb-6",children:"Have a conversation with our AI interviewer to build your biography content. This version uses the official ElevenLabs React SDK."}),n.jsx("div",{className:"bg-white rounded-lg shadow-md p-1 h-[600px]",children:n.jsx(Q,{biographyId:i,chapterId:a,initialQuestion:s||void 0,onEvent:w,className:"h-full"})}),n.jsxs("div",{className:"mt-6 text-sm text-gray-500",children:[n.jsx("p",{children:"This conversation uses the ElevenLabs React SDK for enhanced reliability and features."}),n.jsx("p",{children:"Your conversation is being recorded and will be used to generate content for your biography."})]})]})})}export{H as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.