2 lines
12 KiB
JavaScript
2 lines
12 KiB
JavaScript
import{r,e as T,d as N,f as b,j as e,g as re,h as $,u as ce,i as ie,H as ae,L as le,k as G}from"./.pnpm-BDdfG1pO.js";const ue=1,de=1e6;let W=0;function pe(){return W=(W+1)%Number.MAX_SAFE_INTEGER,W.toString()}const L=new Map,U=t=>{if(L.has(t))return;const s=setTimeout(()=>{L.delete(t),v({type:"REMOVE_TOAST",toastId:t})},de);L.set(t,s)},ge=(t,s)=>{switch(s.type){case"ADD_TOAST":return{...t,toasts:[s.toast,...t.toasts].slice(0,ue)};case"UPDATE_TOAST":return{...t,toasts:t.toasts.map(n=>n.id===s.toast.id?{...n,...s.toast}:n)};case"DISMISS_TOAST":{const{toastId:n}=s;return n?U(n):t.toasts.forEach(c=>{U(c.id)}),{...t,toasts:t.toasts.map(c=>c.id===n||n===void 0?{...c,open:!1}:c)}}case"REMOVE_TOAST":return s.toastId===void 0?{...t,toasts:[]}:{...t,toasts:t.toasts.filter(n=>n.id!==s.toastId)}}},O=[];let M={toasts:[]};function v(t){M=ge(M,t),O.forEach(s=>{s(M)})}function he({...t}){const s=pe(),n=i=>v({type:"UPDATE_TOAST",toast:{...i,id:s}}),c=()=>v({type:"DISMISS_TOAST",toastId:s});return v({type:"ADD_TOAST",toast:{...t,id:s,open:!0,onOpenChange:i=>{i||c()}}}),{id:s,dismiss:c,update:n}}function H(){const[t,s]=r.useState(M);return r.useEffect(()=>(O.push(s),()=>{const n=O.indexOf(s);n>-1&&O.splice(n,1)}),[t]),{...t,toast:he,dismiss:n=>v({type:"DISMISS_TOAST",toastId:n})}}const E=r.createContext({client:null,isConnecting:!1,isConnected:!1,audioEnabled:!0,isSupportVideo:!1,messageList:[],isAiTalking:!1,roomInfo:null,initClient:()=>{},handleConnect:()=>{},handleInterrupt:()=>{},handleDisconnect:()=>{},toggleMicrophone:()=>{},sendUserMessageWithText:()=>{},setInitMessage:()=>{}}),me=()=>{const t=r.useContext(E);if(t===void 0)throw new Error("useRealtimeClient必须在RealtimeClientProvider内部使用");return{...t}},_e=({children:t})=>{const s="pat_NhhZGW7sxkuyP4mJrPrVyZx20b3m6lymg0y2Ln9EyM0CV9q2f9t3rlGbtzppLQua",n="7456409430717480998",c="7426720361733144585",i="1024",o=r.useRef(null),[g,d]=r.useState([]),[h,x]=r.useState(!1),[R,C]=r.useState(!1),[u,m]=r.useState(!1),[I]=r.useState(!1),[w,S]=r.useState(!1),[k,z]=r.useState(""),[F,V]=r.useState(!1),[J,Y]=r.useState(null),{toast:Z}=H(),Q=async()=>{if((await ce.checkDevicePermission(!1)).audio){const a=new ie({accessToken:s,botId:n,voiceId:c,connectorId:i,allowPersonalAccessTokenInBrowser:!0});o.current=a,oe(a),V(!0)}else throw Z({title:"连接错误",description:"需要麦克风访问权限"}),new Error("需要麦克风访问权限")};r.useEffect(()=>{o.current&&(ne(o.current),k&&se(o.current))},[k,F]);const X=async()=>{var l;try{o.current||await Q(),await((l=o.current)==null?void 0:l.connect())}catch(a){if(console.error(a),a instanceof re)switch(a.code){case $.CREATE_ROOM_ERROR:console.error(`创建房间失败: ${a.message}`);break;case $.CONNECTION_ERROR:console.error(`加入房间失败: ${a.message}`);break;case $.DEVICE_ACCESS_ERROR:console.error(`获取设备失败: ${a.message}`);break;default:console.error(`连接错误: ${a.message}`)}else console.error("连接错误:"+a)}},K=()=>{var l;try{(l=o.current)==null||l.interrupt()}catch(a){console.error("打断失败:"+a)}},ee=()=>{var l,a;try{S(!1),V(!1),d([]),(l=o.current)==null||l.disconnect(),(a=o.current)==null||a.clearEventHandlers(),o.current=null,C(!1)}catch(y){console.error("断开失败:"+y)}},te=async()=>{var l;try{await((l=o.current)==null?void 0:l.setAudioEnable(!u)),m(!u)}catch(a){console.error("切换麦克风状态失败:"+a)}},se=l=>{l.on(T.ALL_SERVER,(a,y)=>{a==="server.session.created"&&B(k)})},ne=l=>{let a;l.on(T.ALL,(y,p)=>{if(p.event_type!==N.CONVERSATION_MESSAGE_DELTA&&p.event_type!==N.CONVERSATION_MESSAGE_COMPLETED&&p.event_type!=="conversation.created")return;const D=p.data.content;d(j=>(a==null?void 0:a.event_type)===N.CONVERSATION_MESSAGE_DELTA&&(p.data.type==="answer"||p.data.type==="question")?[...j.slice(0,-1),{content:j[j.length-1].content+D,role:j[j.length-1].role}]:k===""&&p.event_type==="conversation.created"?[...j,{content:p.data.prologue,role:b.Assistant}]:D!==""&&p.event_type===N.CONVERSATION_MESSAGE_DELTA||p.event_type===N.CONVERSATION_MESSAGE_COMPLETED&&(p.data.type==="answer"||p.data.type==="question")&&p.data.role!==b.Assistant?[...j,{content:D,role:p.data.role}]:j),a=p})},oe=r.useCallback(l=>{l.on(T.AUDIO_AGENT_SPEECH_STARTED,()=>{S(!0),m(!1)}),l.on(T.AUDIO_AGENT_SPEECH_STOPPED,()=>{S(!1),m(!0)}),l.on(T.CONNECTING,()=>{x(!0),C(!1)}),l.on(T.CONNECTED,(a,y)=>{Y(y),x(!1),C(!0)})},[o.current,k]),B=async l=>{var a;try{await((a=o.current)==null?void 0:a.sendMessage({id:"",event_type:"conversation.message.create",data:{role:"user",content_type:"text",content:l}}))}catch(y){console.error("发送消息失败:"+y)}};return e.jsx(E.Provider,{value:{client:o.current,isConnecting:h,isConnected:R,audioEnabled:u,isSupportVideo:I,messageList:g,isAiTalking:w,roomInfo:J,initClient:Q,handleConnect:X,handleInterrupt:K,handleDisconnect:ee,toggleMicrophone:te,sendUserMessageWithText:B,setInitMessage:z},children:t})};function fe(){const{messageList:t}=r.useContext(E),s=r.useRef(null),n=()=>{var c;(c=s.current)==null||c.scrollIntoView({behavior:"smooth"})};return r.useEffect(()=>{n()},[t]),e.jsx("div",{className:"flex-1 flex flex-col h-full",children:e.jsxs("div",{className:"flex-1 overflow-y-auto p-4 space-y-4",children:[t.map((c,i)=>e.jsx("div",{className:`flex ${c.role===b.Assistant?"justify-start":"justify-end"}`,children:e.jsx("div",{className:`max-w-3/4 p-3 rounded-lg ${c.role===b.Assistant?"bg-white text-black rounded-tl-none":"bg-blue-500 text-white rounded-tr-none"}`,children:c.content})},i)),e.jsx("div",{ref:s})]})})}const xe="_talkWrapper_5jmgu_23",Ee="_listenerDot_5jmgu_53",je="_isTalking_5jmgu_93",ye="_microphoneWrapper_5jmgu_157",A={talkWrapper:xe,listenerDot:Ee,isTalking:je,microphoneWrapper:ye},Ie="/icons/lockmicrophone.png",Te="/icons/microphone.png",Ce="/icons/handoff.png",Se=t=>{const s=r.useRef(null),{handleDisconnect:n}=me(),{toast:c}=H();return r.useEffect(()=>{if(!t.access_token||!t.roomId)return;const i=new ae().withServerTimeout(3e4).withAutomaticReconnect().withUrl(`https://api.v3.ycymedu.com/hubs/weminpro?access_token=${t.access_token}&roomId=${t.roomId}`).configureLogging(le.Information).build();return s.current=i,i.on("ForceOffline",function(o){c({variant:"destructive",title:"下线提醒",description:o}),n()}),i.on("SendWarn",function(o){console.warn(`下线提醒:${o}"`),c({variant:"destructive",title:"下线提醒",description:o})}),i.start().then(()=>{console.log("SignalR连接已建立"),setInterval(()=>{i.invoke("Ping")},5e3)}).catch(o=>{console.error("SignalR连接失败:",o)}),()=>{s.current&&s.current.stop().then(()=>{console.log("SignalR连接已关闭")}).catch(o=>{console.error("关闭SignalR连接失败:",o)})}},[t.access_token,t.roomId]),s.current};function ke({className:t,...s}){const{handleDisconnect:n,toggleMicrophone:c,audioEnabled:i,isAiTalking:o,handleInterrupt:g,roomInfo:d}=r.useContext(E),[h]=G();return Se({access_token:h.get("token")||"",roomId:(d==null?void 0:d.roomId)||""}),e.jsxs("div",{className:`${t} flex items-center justify-center bg-white pb-[20px] pt-[10px] gap-[10px]`,...s,children:[e.jsxs("div",{className:`${A.microphoneWrapper}`,onClick:n,children:[e.jsx("img",{src:Ce,alt:"handoff"}),e.jsx("div",{children:"挂断"})]}),e.jsxs("div",{className:`${A.microphoneWrapper}`,onClick:c,children:[e.jsx("img",{src:i?Te:Ie,alt:"lock"}),e.jsx("div",{children:i?"关麦":"开麦"})]}),e.jsxs("div",{className:`${A.talkWrapper}`,onClick:g,children:[e.jsxs("div",{className:`${o?A.isTalking:""} ${A.listenerDot}`,children:[e.jsx("span",{style:{"--d":"2"}}),e.jsx("span",{style:{"--d":"1"}}),e.jsx("span",{style:{"--d":"0"}}),e.jsx("span",{style:{"--d":"1"}}),e.jsx("span",{style:{"--d":"2"}})]}),e.jsx("div",{children:o?"点击打断":"正在听"})]})]})}function Ne(){return e.jsxs("div",{className:"flex flex-col h-full",children:[e.jsx(fe,{}),e.jsx(ke,{})]})}const Ae="/icons/hello.gif",ve="/icons/whatsThing.png",Re="/icons/circle.png",we="/icons/right.png",Oe="_headerWrapper_lrk2k_1",Me="_wrapper_lrk2k_15",be="_img_lrk2k_37",De="_text_lrk2k_81",$e="_main_lrk2k_101",We="_thing_lrk2k_135",Le="_circle_lrk2k_153",Pe="_rightIcon_lrk2k_179",Ge="_change_lrk2k_189",Ve="_tip_lrk2k_197",Qe="_rotating_lrk2k_223",_={headerWrapper:Oe,wrapper:Me,img:be,text:De,"main-wrapper":"_main-wrapper_lrk2k_101",main:$e,thing:We,circle:Le,rightIcon:Pe,change:Ge,tip:Ve,rotating:Qe},Be=t=>async(s,n,c={})=>{const i={method:t,...c};if(t!=="GET"&&n)i.body=JSON.stringify(n);else if(t==="GET"&&n){const d=[];for(const[h,x]of Object.entries(n))d.push(`${h}=${x}`);s+=`?${d.join("&")}`}const o=await fetch(`${s}`,i),g=await o.json();return o.ok?g:{error:g.message||"Request failed",code:o.status}},q=Be("GET"),Ue=async({options:t})=>{const s=await q("https://api.v3.ycymedu.com/api/zhiYuan/aigcquestionswords?",{},t);return s.code===200?{result:s.result}:{result:[],message:s.message}};function He({toRoom:t}){const[s,n]=r.useState(!1),[c,i]=r.useState([]),[o,g]=r.useState([]),{setInitMessage:d}=r.useContext(E),h=()=>{const u=Array.from(o),m=[],I=Math.min(4,u.length);for(let w=0;w<I;w++){const S=Math.floor(Math.random()*u.length);m.push(u.splice(S,1)[0])}return m},x=async({signal:u})=>{const{result:m,message:I}=await Ue({options:{signal:u}});I?console.log(I):g(m)};r.useEffect(()=>{i(h())},[o]),r.useEffect(()=>{const u=new AbortController,{signal:m}=u;x({signal:m})},[]);const R=()=>{n(!0),i(h()),setTimeout(()=>{n(!1)},1e3)},C=async u=>{d(u),await t()};return e.jsxs("div",{className:_.headerWrapper,children:[e.jsxs("div",{className:_.wrapper,children:[e.jsx("img",{className:_.img,src:Ae,alt:"hello"}),e.jsx("div",{className:_.text,children:"Hey,我是您的六纬AI小助手"})]}),e.jsxs("div",{className:_["main-wrapper"],children:[e.jsxs("div",{className:_.main,onClick:R,children:[e.jsx("img",{className:_.thing,src:ve,alt:"whatsThing"}),e.jsxs("div",{className:_.circle,children:[e.jsx("img",{src:Re,className:s?_.rotating:"",alt:"circle"}),e.jsx("div",{className:_.change,children:"换一批"})]})]}),c.map((u,m)=>e.jsxs("div",{className:_.tip,onClick:()=>C(u),children:[e.jsx("div",{children:u}),e.jsx("img",{src:we,alt:"right-icon",className:_.rightIcon})]},m))]})]})}const qe="_wrapper_e1j90_1",ze="_text_e1j90_75",Fe="_call_e1j90_87",P={wrapper:qe,text:ze,call:Fe},Je="/icons/call.png";function Ye(t){const{disable:s,loading:n,className:c,...i}=t,{isConnecting:o}=r.useContext(E);return e.jsxs("div",{className:`${P.wrapper} ${c}`,...i,children:[e.jsx("img",{className:P.call,src:Je,alt:"call"}),e.jsx("div",{className:P.text,children:s?"暂不可用":o?"连接中":"发起通话"})]})}const Ze="/icons/myInput.png",Xe="/icons/rightBlue.png",Ke="_scoreWrapper_1g8jt_1",et="_innerWrapper_1g8jt_13",tt="_detail_1g8jt_37",st="_right_1g8jt_53",nt="_imgIcon_1g8jt_65",ot="_rightBlue_1g8jt_75",f={scoreWrapper:Ke,innerWrapper:et,detail:tt,right:st,imgIcon:nt,rightBlue:ot};function rt({toRoom:t}){const[s]=G(),n=s.get("provinceName")||"山东省",c=s.get("subjectGroup")||"物/化/史",i=s.get("expectedScore")||500,{setInitMessage:o}=r.useContext(E),g=async()=>{t(),o(`我的高考地点在${n},我选择的科目是${c},我的高考分数为${i}分。我适合哪些学校和专业`)};return e.jsx("div",{className:f.scoreWrapper,children:e.jsxs("div",{className:f.innerWrapper,children:[e.jsxs("div",{className:f.left,children:[e.jsx("img",{src:Ze,className:f.imgIcon,alt:"input-ico"}),e.jsxs("div",{className:f.detail,children:[e.jsx("div",{className:f.city,children:n}),e.jsx("div",{className:f.subject,children:c.split(",").join("/")}),e.jsxs("div",{className:f.score,children:[i,"分"]})]})]}),e.jsxs("div",{className:f.right,onClick:g,children:[e.jsx("span",{children:"智能分析"}),e.jsx("img",{src:Xe,alt:"right",className:f.rightBlue})]})]})})}const ct=async({options:t})=>{const s=await q("https://api.v3.ycymedu.com/api/sysOnlineUser/hasitexpired",{},t);return s.code===200?{result:s.result}:{result:[],message:s.message}};function it(){const{handleConnect:t}=r.useContext(E),[s]=G(),[n,c]=r.useState(!0),i=s.get("token")||"",o=async({signal:d})=>{const{result:h,message:x}=await ct({options:{signal:d,headers:{Authorization:`Bearer ${i}`}}});x?console.log(x):c(!h.isExpired)};r.useEffect(()=>{const d=new AbortController,{signal:h}=d;o({signal:h})},[i]);const g=()=>{t()};return e.jsxs("div",{className:"flex flex-col items-center h-full",children:[e.jsx(He,{toRoom:g}),e.jsx(rt,{toRoom:g}),e.jsx(Ye,{disable:n,onClick:g})]})}function at(){const{isConnected:t}=r.useContext(E);return t?e.jsx(Ne,{}):e.jsx(it,{})}function ut(){return e.jsx(_e,{children:e.jsx(at,{})})}export{ut as default};
|