doomiaichat 7.3.3 → 7.3.4
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/dist/index.esm.js +1 -1
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/types/corzbot.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/index.esm.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{EventEmitter as e}from"events";import t from"openai";import{CozeAPI as i,COZE_CN_BASE_URL as s,ChatStatus as n,RoleType as r,ChatEventType as o,WorkflowEventType as a,WebsocketsEventType as c,COZE_CN_BASE_WS_URL as u,getJWTToken as l}from"@coze/api";export{WebsocketsEventType as COZE_SOCKET_EVENT_TYPE}from"@coze/api";import{parse as d}from"querystring";import h,{mkdirSync as p,createWriteStream as m}from"fs";import g,{join as _,resolve as f}from"path";import T from"ws";import{randomUUID as y}from"crypto";import E from"axios";import{OpenAIClient as v,AzureKeyCredential as C}from"@azure/openai";import{Readable as O,EventEmitter as A}from"stream";function S(e,t,i,s){return new(i||(i=Promise))(function(n,r){function o(e){try{c(s.next(e))}catch(e){r(e)}}function a(e){try{c(s.throw(e))}catch(e){r(e)}}function c(e){var t;e.done?n(e.value):(t=e.value,t instanceof i?t:new i(function(e){e(t)})).then(o,a)}c((s=s.apply(e,t||[])).next())})}function b(e){var t="function"==typeof Symbol&&Symbol.iterator,i=t&&e[t],s=0;if(i)return i.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&s>=e.length&&(e=void 0),{value:e&&e[s++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function k(e){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var t,i=e[Symbol.asyncIterator];return i?i.call(e):(e=b(e),t={},s("next"),s("throw"),s("return"),t[Symbol.asyncIterator]=function(){return this},t);function s(i){t[i]=e[i]&&function(t){return new Promise(function(s,n){(function(e,t,i,s){Promise.resolve(s).then(function(t){e({value:t,done:i})},t)})(s,n,(t=e[i](t)).done,t.value)})}}}"function"==typeof SuppressedError&&SuppressedError;class N extends e{constructor(){super()}getTextEmbedding(e,t){return null}createCoversation(e){return S(this,void 0,void 0,function*(){return null})}setVariables(e){return S(this,void 0,void 0,function*(){return{successed:!0}})}getVariables(e){return S(this,void 0,void 0,function*(){return{successed:!1}})}chatRequestInStream(e,t,i,s){return null}}var I=Object.freeze({__proto__:null,default:N});class R extends N{constructor(e,t={}){super(),this.apiKey=e,this.chatModel=t.model||"gpt-3.5-turbo",this.maxtoken=t.maxtoken||2048,this.top_p=t.top_p||.95,this.temperature=t.temperature||.9,this.presence_penalty=t.presence_penalty||0,this.frequency_penalty=t.frequency_penalty||0,this.embeddingmodel=t.embedding||"text-embedding-ada-002"}}class w extends R{createOpenAI(e){return new t({apiKey:e})}getTextEmbedding(e,t){return S(this,void 0,void 0,function*(){if(!e)return{successed:!1,error:{errcode:2,errmsg:"content required"}};this.aiApi||(this.aiApi=this.createOpenAI(this.apiKey));try{return{successed:!0,embedding:(yield this.aiApi.embeddings.create({model:this.embeddingmodel,input:e},t)).data.data}}catch(e){return{successed:!1,error:e}}})}chatRequest(e,t){return S(this,arguments,void 0,function*(e,t,i={}){if(!e)return{successed:!1,error:{errcode:2,errmsg:"缺失聊天的内容"}};this.aiApi||(this.aiApi=this.createOpenAI(this.apiKey));let s="string"==typeof e?[{role:"user",content:e}]:e;try{const e=yield this.aiApi.chat.completions.create({model:(null==t?void 0:t.model)||this.chatModel,messages:s,temperature:Number((null==t?void 0:t.temperature)||this.temperature),max_tokens:Number((null==t?void 0:t.maxtoken)||this.maxtoken),top_p:Number((null==t?void 0:t.top_p)||this.top_p),presence_penalty:Number((null==t?void 0:t.presence_penalty)||this.presence_penalty),frequency_penalty:Number((null==t?void 0:t.frequency_penalty)||this.frequency_penalty),n:Number((null==t?void 0:t.replyCounts)||1)||1},i);return{successed:!0,message:e.choices,usage:e.usage}}catch(e){return console.log("result is error ",e),{successed:!1,error:e}}})}chatRequestInStream(e,t,i,s){return S(this,void 0,void 0,function*(){var n,r,o,a;e||this.emit("chaterror",{successed:!1,error:"no text in chat"}),this.aiApi||(this.aiApi=this.createOpenAI(this.apiKey));let c="string"==typeof e?[{role:"user",content:e}]:e;s=Object.assign({},s||{timeout:6e4});let u=Math.ceil(Math.random()*((new Date).getTime()*Math.random())/1e3);try{const e=yield this.aiApi.chat.completions.create({model:(null==t?void 0:t.model)||this.chatModel,messages:c,temperature:Number((null==t?void 0:t.temperature)||this.temperature),max_tokens:Number((null==t?void 0:t.maxtoken)||this.maxtoken),top_p:Number((null==t?void 0:t.top_p)||this.top_p),presence_penalty:Number((null==t?void 0:t.presence_penalty)||this.presence_penalty),frequency_penalty:Number((null==t?void 0:t.frequency_penalty)||this.frequency_penalty),n:Number((null==t?void 0:t.replyCounts)||1)||1,tools:1===(null==t?void 0:t.enableToolCall)&&(null==t?void 0:t.tools)?t.tools:void 0,tool_choice:1===(null==t?void 0:t.enableToolCall)?"auto":void 0,stream:!0},s);let p,m=[],g=0,_=-1,f=[];try{for(var l,d=!0,h=k(e);!(n=(l=yield h.next()).done);d=!0){a=l.value,d=!1;const e=a,[t]=e.choices,{finish_reason:s,index:n,usage:r}=t,{content:o,tool_calls:c}=t.delta;c&&c.length?(p=c[0].index,g=1,p!==_?(f.push({id:c[0].id,type:"function",function:{name:c[0].function.name,arguments:c[0].function.arguments}}),_=p):f[_].function.arguments+=c[0].function.arguments):m.push(o);let h={successed:!0,requestid:u,segment:o,text:m.join(""),finish_reason:s,index:n,usage:r,has_tool_calls:g,tool_calls:f};i&&(h=Object.assign({},h,i)),this.emit(s?"chatdone":"chattext",h)}}catch(e){r={error:e}}finally{try{d||n||!(o=h.return)||(yield o.call(h))}finally{if(r)throw r.error}}return{successed:!0,requestid:u}}catch(e){return this.emit("requesterror",{successed:!1,requestid:u,error:"call axios faied "+e}),{successed:!1,requestid:u}}})}}const x={thinking:{start:"<xiaolu-think>",end:"</xiaolu-think>",tag:"xiaolu-think"},reasoning:{start:"<xiaolu-reason>",end:"</xiaolu-reason>",tag:"xiaolu-reason"},card:{start:"<xiaolu-card>",end:"</xiaolu-card>",tag:"xiaolu-card"},preresponse:{start:"<xiaolu-preresponse>",end:"</xiaolu-preresponse>",tag:"xiaolu-preresponse"}};var D;!function(e){e[e.None=0]="None",e[e.Thinking=1]="Thinking",e[e.ThinkingOver=2]="ThinkingOver"}(D||(D={}));class P extends N{constructor(e,t={}){if(super(),this.authorizationProvider=e,this.setting=t,this.botid=null,this.workflowid=null,this.talkflowid=null,this.debugDir="",this.debug=!1,this.setting.workflowid)this.workflowid=this.setting.workflowid;else if(this.setting.talkflowid)this.talkflowid=this.setting.talkflowid;else{if(!this.setting.botid&&!this.setting.botID)throw new Error("no botid or talkflowid or workflowid setting for coze");this.botid=this.setting.botid||this.setting.botID}!0===t.debug&&(this.debug=!0,this.debugDir=_(t.logPath||f(process.cwd(),"./cozedebug"),this.setting.model||"chat"),p(this.debugDir,{recursive:!0})),this.apiKey=this.setting.apiKey}createClient(){return S(this,void 0,void 0,function*(){if(this.apiKey)return new i({baseURL:s,token:this.apiKey});const e=yield this.authorizationProvider.getAccessToken();if(!e)throw new Error("get access token failed");return new i({baseURL:s,token:e})})}cloneVoice(e){return S(this,arguments,void 0,function*(e,t={}){const i=yield this.createClient();try{return{successed:!0,voice_id:(yield i.audio.voices.clone(e,t)).voice_id}}catch(e){return{successed:!1,error:e}}})}createSpeech(e,t){return S(this,arguments,void 0,function*(e,t,i="mp3",s={}){const n=yield this.createClient();try{return{successed:!0,data:yield n.audio.speech.create({input:e,voice_id:t,response_format:i},s)}}catch(e){return{successed:!1,error:e}}})}createCoversation(e){return S(this,void 0,void 0,function*(){try{const t=null!=e?e:yield this.createClient(),i=this.botid?{bot_id:this.botid}:{};return(yield t.conversations.create(i)).id}catch(e){return console.error("createCoversation error in coze api"),null}})}setVariables(){return S(this,arguments,void 0,function*(e={}){const t=yield this.createClient();!e.type&&(e.type="app");const i={data:e.data};"app"===e.type&&(i.app_id=this.authorizationProvider.appid),"bot"===e.type&&this.botid&&(i.bot_id=this.botid),e.user_id&&(i.connector_uid=e.user_id);const s=yield t.variables.update(i,{debug:!0});return console.log("setVariables result",s),{successed:!0}})}getVariables(e){return S(this,void 0,void 0,function*(){const t=yield this.createClient(),i=Object.assign({},{bot_id:this.botid},e),s=yield t.variables.retrieve(i);return s?Object.assign({successed:!0},s):{successed:!1,error:"get variables failed"}})}queryWorkflowExecuteResult(e,t){return S(this,arguments,void 0,function*(e,t,i={}){if(!e||!t)return{successed:!1,error:"workflowid or executeid is null"};const s=yield this.createClient();try{return{successed:!0,history:yield s.workflows.runs.history(e,t,i)}}catch(e){return{successed:!1,error:e}}})}getRequestStream(e,t){return S(this,arguments,void 0,function*(e,t,i={}){var s,n;if(this.botid){const n={bot_id:this.botid,additional_messages:t,user_id:i.userid||i.cozeUserID,conversation_id:null!==(s=i.session_id)&&void 0!==s?s:yield this.createCoversation(e),parameters:Object.assign({request_src:1},i.parameters||{})};return n.custom_variables=Object.assign({},this.setting.customVariables||{},i.customVariables||{}),n}if(this.workflowid){const e={ext:i.ext,workflow_id:this.workflowid,is_async:i.is_async||!1,parameters:Object.assign({request_src:1},i.parameters||{},{input:t})};return(i.userid||i.cozeUserID)&&(e.ext=Object.assign({},e.ext||{},{user_id:i.userid||i.cozeUserID})),e}const r={additional_messages:t,workflow_id:this.talkflowid,conversation_id:null!==(n=i.session_id)&&void 0!==n?n:yield this.createCoversation(e),parameters:Object.assign({request_src:1},i.parameters||{})};return(i.userid||i.cozeUserID)&&(r.ext=Object.assign({},r.ext||{},{user_id:i.userid||i.cozeUserID})),r})}getChatDetail(e,t,i){return S(this,void 0,void 0,function*(){const s=Date.now();for(;Date.now()-s<12e4;){const s=yield e.chat.retrieve(t,i);if(s.status===n.COMPLETED)return{usage:s.usage,messages:yield e.chat.messages.list(t,i)};yield new Promise(e=>setTimeout(e,1500))}return null})}chatRequest(e){return S(this,arguments,void 0,function*(e,t={},i={}){e||this.emit("chaterror",{successed:!1,error:"no message in chat"}),"string"==typeof e&&(e=[{role:r.User,content_type:"text",content:e}]);const s=yield this.createClient(),n=this.debug?m(_(this.debugDir,`sync_${this.formatDateWithMs()}.log`),{flags:"a"}):null,o=yield this.getRequestStream(s,e,t);n&&(n.write(`callChatOption:${JSON.stringify(t)}\n`),n.write(`parameters:${JSON.stringify(o)}\n`));const a=this.botid?yield s.chat.create(o,i):yield s.workflows.runs.create(o,i);if(this.workflowid){const e=a;if(o.is_async)return{successed:!0,workflow_id:this.workflowid,execute_id:e.execute_id};if("Success"===e.msg||!e.msg&&e.data){const t=e.data;return n&&n.end(t),{successed:!0,message:[{role:"assistant",type:"answer",content:t}]}}n&&n.end("workflow run failed"),console.error("workflow run failed",e.msg,e)}if(!this.workflowid&&a.conversation_id&&a.id){const e=a,t=yield this.getChatDetail(s,e.conversation_id,e.id);if(t){return{successed:!0,message:t.messages.filter(e=>"answer"===e.type).map(e=>({role:e.role,type:e.type,content:e.content})),usage:t.usage,session_id:e.conversation_id}}}return n&&n.end("聊天未完成"),{successed:!1,error:{message:"聊天未完成"}}})}extractXmlContent(e,t){const i=new RegExp(`<\\s*${t}\\s*>([\\s\\S]*?)<\\s*\\/\\s*${t}\\s*>`,"i"),s=e.match(i);return s&&s[1]?s[1].trim():""}parseDeepThinkingJson(e){const t=[e.indexOf(x.thinking.start),e.indexOf(x.reasoning.start),e.indexOf(x.card.start),e.indexOf(x.preresponse.start)];let i=1e4,s=-1;t.forEach((e,t)=>{e>=0&&e<i&&(i=e,s=t)});const n=s>=0?i:-1;if(n<0)return{content:e,status:D.ThinkingOver};const r=[x.thinking,x.reasoning,x.card,x.preresponse][s],o=e.indexOf(r.end,n),a=o>=0?o:e.indexOf("</",n),c=this.extractXmlContent(e.substring(n,a>=0?a:void 0)+r.end,r.tag);return o>=0&&(e=e.substring(o+r.end.length)),{thinking:{action:r,text:c,completed:o>=0},content:e}}formatDateWithMs(e=new Date){return`${e.getFullYear()}${String(e.getMonth()+1).padStart(2,"0")}${String(e.getDate()).padStart(2,"0")}${String(e.getHours()).padStart(2,"0")}${String(e.getMinutes()).padStart(2,"0")}${String(e.getSeconds()).padStart(2,"0")}${String(e.getMilliseconds()).padStart(3,"0")}`}chatRequestInStream(e){return S(this,arguments,void 0,function*(e,t={},i,s){var n,c,u,l,h,p,g;e||this.emit("chaterror",{successed:!1,error:"no message in chat"}),"string"==typeof e&&(e=[{role:r.User,content_type:"text",content:e}]);const f=yield this.createClient();let T=Math.ceil(Math.random()*((new Date).getTime()*Math.random())/1e3),y=0;const E=this.debug?m(_(this.debugDir,`${this.formatDateWithMs()}.log`),{flags:"a"}):null,v=yield this.getRequestStream(f,e,t);E&&(E.write(`callChatOption:${JSON.stringify(t)}\n`),E.write(`attach:${i?JSON.stringify(i):"null"}\n`),E.write(`parameters:${JSON.stringify(v)}\n`));const C=this.botid?f.chat.stream(v):this.workflowid?f.workflows.runs.stream(v,s):f.workflows.chat.stream(v,s);let O=0,A=[],S=[],b=[],N=[],I="",R=D.None,w="",P="";try{for(var U,V=!0,B=k(C);!(n=(U=yield B.next()).done);V=!0){l=U.value,V=!1;const e=l;if(e.event===o.ERROR)return E&&E.write("chat error:"),this.emit("chaterror",{successed:!1,error:"call failed"});if(e.event===o.CONVERSATION_MESSAGE_DELTA||e.event===a.MESSAGE){let{conversation_id:t,content_type:s,type:n="answer",role:r="assistant",content:o,reasoning_content:a}=e.data;if(E&&E.write(o),"Default"===o)continue;if(!o&&a){this.emit("chatthinking",{text:a,completed:!1,action:"deep-thinking"});continue}if(0===O&&o.startsWith("<xiaolu-")&&(R=D.Thinking,O++),R===D.Thinking){I+=o;const e=this.parseDeepThinkingJson(I),a=e.thinking;if(a)if(I=e.content,a.action===x.card){if(w+=e.thinking.text,e.thinking.completed){const e=w.replace(/[\x00-\x1F\x7F]/g,"").split("|");w="";for(const t of e){const e=d(t);if(e.type&&e.tag&&"string"==typeof e.tag)if("follow_up"===e.type)S.push(e.tag);else{const t=e.tag.split(",");for(const i of t)N.push({type:e.type,tag:i})}}this.emit("chatcard",N)}}else if(a.action===x.preresponse){const o=e.thinking.text.slice(P.length);P=e.thinking.text,A.push(o);let a={successed:!0,type:n,content_type:s,role:r,requestid:T,segment:o,text:A.join(""),index:y++,session_id:t,preresponse:!0,preresponseover:e.thinking.completed};i&&(a=Object.assign({},a,i)),this.emit("chattext",a)}else this.emit("chatthinking",{text:e.thinking.text,completed:e.thinking.completed,action:null===(h=a.action)||void 0===h?void 0:h.tag});if(e.status!=D.ThinkingOver)continue;R=D.ThinkingOver,o=I}A.push(o);let c={successed:!0,type:n,content_type:s,role:r,requestid:T,segment:o,text:A.join(""),index:y++,session_id:t};i&&(c=Object.assign({},c,i)),this.emit("chattext",c)}if(e.event===o.CONVERSATION_MESSAGE_COMPLETED){const{type:t,content:i}=null!==(p=e.data)&&void 0!==p?p:{};"follow_up"===t&&(E&&E.write("followup:"+i),b.push(i))}if(e.event===o.CONVERSATION_CHAT_COMPLETED||e.event===a.DONE){const{conversation_id:t,content:s,usage:n}=null!==(g=e.data)&&void 0!==g?g:{};let o={successed:!0,usage:n,cards:N.length?N:null,cardfollowup:S.filter(e=>"string"==typeof e&&e.length>0),followup:b,type:"answer",content_type:"text",role:r.Assistant,requestid:T,segment:null,text:null!=s?s:A.join(""),index:y++,session_id:t};i&&(o=Object.assign({},o,i)),E&&E.end("chat completed"),this.emit("chatdone",o)}}}catch(e){c={error:e}}finally{try{V||n||!(u=B.return)||(yield u.call(B))}finally{if(c)throw c.error}}return{successed:!0,requestid:T}})}}class U extends P{constructor(){super(...arguments),this.socket=null,this.traceId=y(),this.chatVoiceBuffer=[],this.chatContent=[],this.serverVad=!0,this.combineTTS=!0,this.currentAgentStatus=0,this.waitingWarmVoice=[],this.waitingInterval=10,this.willSendWarmVoice=!1,this.maxWaitingTimes=0,this.waitingTimes=0,this.waitingTimer=null}getChatVoiceFile(){return this.chatVoiceBuffer.length?Buffer.concat([...this.chatVoiceBuffer]):Buffer.alloc(0)}clearWaitingTimer(){this.waitingTimer&&clearTimeout(this.waitingTimer),this.waitingTimer=null}startWaitingUserSpeaking(e=0){this.willSendWarmVoice&&(console.log(`第${this.waitingTimes+1}等待用户说话,等待${this.waitingInterval+e} 秒`),this.clearWaitingTimer(),this.waitingTimer=setTimeout(()=>{const e=this.waitingWarmVoice.length,t=this.waitingWarmVoice[this.waitingTimes++%e]+"";this.submitWarmVoice(t),this.maxWaitingTimes>0&&this.waitingTimes>=this.maxWaitingTimes?setTimeout(()=>this.emit("chat.end",{reason:"wait_timeout"}),t.length/4*1e3):this.startWaitingUserSpeaking()},1e3*(this.waitingInterval+e)))}getChatContent(){return this.chatContent}submitActionResult(e){const t={id:this.traceId,event_type:c.CONVERSATION_CHAT_SUBMIT_TOOL_OUTPUTS,data:e};this.sendCommand(t)}submitWarmVoice(e){if(!e)return;console.log("发送暖场语音:",e);const t={id:this.traceId,event_type:"input_text.generate_audio",data:{mode:"text",text:e}};this.sendCommand(t)}sendVoice(e,t){if(!e)return;t=t||e;const i=this.serverVad?t.toString("base64"):e.toString("base64"),s={id:this.traceId,event_type:c.INPUT_AUDIO_BUFFER_APPEND,data:{delta:i}};this.sendCommand(s),this.chatVoiceBuffer.push(e)}sendChatMessage(e,t=r.User){const i={id:this.traceId,event_type:c.CONVERSATION_MESSAGE_CREATE,data:{role:t,content_type:"text",content:e}};this.sendCommand(i)}close(){this.breakAnswer(),this.currentAgentStatus=0,this.socket&&this.socket.close()}submitVoiceBuffer(){const e={id:this.traceId,event_type:c.INPUT_AUDIO_BUFFER_COMPLETE};this.sendCommand(e)}clearVoiceBuffer(){const e={id:this.traceId,event_type:c.INPUT_AUDIO_BUFFER_CLEAR};this.sendCommand(e)}breakAnswer(){if(1===this.currentAgentStatus){const e={id:this.traceId,event_type:c.CONVERSATION_CHAT_CANCEL};this.sendCommand(e)}}startVoiceChat(){return S(this,arguments,void 0,function*(e={}){if(!this.botid)return{successed:!1,code:-1,error:"语音对话仅支持智能体模式"};if(!e.voiceId)return{successed:!1,code:-2,error:"请提供语音音色"};const t=yield this.authorizationProvider.getAccessToken();if(!t)return{successed:!1,error:"获取token失败"};const i=`${u}/v1/chat?bot_id=${this.botid}&authorization=Bearer ${t}`;this.socket=new T(i);let s=!0;return this.socket.on("open",()=>console.log("Connected to bot websocket ")),this.socket.on("message",t=>{const i=JSON.parse(t.toString());switch(i.event_type){case c.CHAT_CREATED:this.ChatCreated(e);break;case c.CHAT_UPDATED:this.emit(c.CHAT_UPDATED,i);break;case c.CONVERSATION_CHAT_CREATED:this.currentAgentStatus=1,this.emit(c.CONVERSATION_CHAT_CREATED,i);break;case c.CONVERSATION_CHAT_IN_PROGRESS:this.emit(c.CONVERSATION_CHAT_IN_PROGRESS,i);break;case c.CONVERSATION_MESSAGE_DELTA:this.emit(c.CONVERSATION_MESSAGE_DELTA,i);break;case c.CONVERSATION_AUDIO_SENTENCE_START:if(s&&e.playPrologue){this.chatContent.push({role:r.Assistant,content:i.data.text});const e=i.data.text.length;this.startWaitingUserSpeaking(e/4),s=!1}this.emit(c.CONVERSATION_AUDIO_SENTENCE_START,i);break;case c.CONVERSATION_AUDIO_DELTA:this.emit(c.CONVERSATION_AUDIO_DELTA,i),this.combineTTS&&i.data.content&&this.chatVoiceBuffer.push(Buffer.from(i.data.content,"base64"));break;case c.CONVERSATION_MESSAGE_COMPLETED:if(this.emit(c.CONVERSATION_MESSAGE_COMPLETED,i),["question","answer"].includes(i.data.type)&&(this.chatContent.push({role:i.data.role,content:i.data.content}),"answer"===i.data.type)){const e=i.data.content.length;this.startWaitingUserSpeaking(e/4)}break;case c.CONVERSATION_AUDIO_COMPLETED:this.emit(c.CONVERSATION_AUDIO_COMPLETED,i);break;case c.CONVERSATION_CHAT_COMPLETED:this.currentAgentStatus=0,this.emit(c.CONVERSATION_CHAT_COMPLETED,i);break;case c.CONVERSATION_CHAT_CANCELED:this.currentAgentStatus=0,this.emit(c.CONVERSATION_CHAT_CANCELED,i),this.startWaitingUserSpeaking();break;case c.CONVERSATION_CHAT_FAILED:this.currentAgentStatus=0,this.emit(c.CONVERSATION_CHAT_FAILED,i);break;case c.INPUT_AUDIO_BUFFER_COMPLETED:this.emit(c.INPUT_AUDIO_BUFFER_COMPLETED,i);break;case c.INPUT_AUDIO_BUFFER_CLEAR:this.emit(c.INPUT_AUDIO_BUFFER_CLEAR,i);break;case c.ERROR:this.emit(c.ERROR,i);break;case c.INPUT_AUDIO_BUFFER_SPEECH_STARTED:this.emit(c.INPUT_AUDIO_BUFFER_SPEECH_STARTED,i);break;case c.INPUT_AUDIO_BUFFER_SPEECH_STOPPED:this.emit(c.INPUT_AUDIO_BUFFER_SPEECH_STOPPED,i);break;case c.CONVERSATION_AUDIO_TRANSCRIPT_UPDATE:this.emit(c.CONVERSATION_AUDIO_TRANSCRIPT_UPDATE,i),this.waitingTimer&&this.clearWaitingTimer();break;case c.CONVERSATION_AUDIO_TRANSCRIPT_COMPLETED:this.emit(c.CONVERSATION_AUDIO_TRANSCRIPT_COMPLETED,i);break;case c.CONVERSATION_CHAT_REQUIRES_ACTION:this.emit(c.CONVERSATION_CHAT_REQUIRES_ACTION,i)}}),{successed:!0}})}ChatCreated(e={}){this.emit(c.CHAT_CREATED),this.serverVad=null==e.serverVad||null==e.serverVad||e.serverVad,this.combineTTS=!this.serverVad||this.serverVad&&(e.combinetts||!1),this.waitingWarmVoice=e.waitingWarmVoice?"string"==typeof e.waitingWarmVoice?e.waitingWarmVoice.split("|"):Array.isArray(e.waitingWarmVoice)?e.waitingWarmVoice:[]:[],this.willSendWarmVoice=this.waitingWarmVoice.length>0,this.maxWaitingTimes=e.maxWaitingTimes||0,this.waitingInterval=e.waitingInterval||10;const t={id:this.traceId,event_type:c.CHAT_UPDATE,data:{chat_config:{auto_save_history:!0,conversation_id:e.conversationId,user_id:e.userid||e.cozeUserID,meta_data:e.meta_data||{},custom_variables:e.customVariables||{},extra_params:{},parameters:e.parameters},input_audio:{format:"pcm",codec:"pcm",sample_rate:e.sample_rate||16e3},output_audio:{codec:"pcm",pcm_config:{sample_rate:e.sample_rate||16e3,frame_size_ms:e.output_frame_size_ms||100},speech_rate:e.speed||0,voice_id:e.voiceId},turn_detection:{type:this.serverVad?"server_vad":"client_interrupt",silence_duration_ms:e.vadSilence||1e3},asr_config:{context:e.asrContext,user_language:e.asrLanguage||"common",hot_words:e.hotWords,enable_ddc:null==e.enable_ddc||null==e.enable_ddc||e.enable_ddc,enable_itn:null==e.enable_itn||null==e.enable_itn||e.enable_itn,enable_nostream:!1},need_play_prologue:e.playPrologue||!1,prologue_content:e.playPrologue&&e.prologueContent||null}};this.sendCommand(t)}sendCommand(e){this.socket&&this.socket.send("string"==typeof e?e:JSON.stringify(e))}}const V=["[AUTHORIZATION NEEDED]","[AUTHORIZATION ERROR]","[BODY ERROR]","[REQUEST ERROR]"];class B extends w{constructor(e,t,i={}){super(e,i),this.proxySetting=t}chatRequest(e,t,i){return S(this,void 0,void 0,function*(){const s=Object.assign({headers:{"Content-Type":"application/json",authorization:`Bearer ${this.apiKey}`},method:"post",url:this.proxySetting.serviceurl,data:{chatText:e,option:t}},i);return(yield E(s)).data})}chatRequestInStream(e,t,i,s){return S(this,void 0,void 0,function*(){let n,r=Math.ceil(Math.random()*((new Date).getTime()*Math.random())/1e3);const o={headers:{"Content-Type":"application/json",authorization:`Bearer ${this.apiKey}`},method:"post",url:this.proxySetting.serviceurl+"/stream",data:{messages:e,option:t,axiosOption:s},responseType:"stream"};let a="";E(o).then(e=>{e.data.on("data",e=>{if(n=e.toString(),n){if(V.includes(n))return this.emit("requesterror",{successed:!1,requestid:r,error:"Request Remote OpenAI Error : "+n});const e=(a+n).split("*&$");a="";for(const t of e)if(t)try{if(!t.endsWith("}")){a=t;break}const e=Object.assign(JSON.parse(t),i);this.emit(e.finish_reason?"chatdone":"chattext",e)}catch(e){break}}})}).catch(e=>{this.emit("requesterror",{successed:!1,requestid:r,error:"Axios Error : "+e})})})}}class M extends R{constructor(e,t,i={}){super(e,i),this.azureSetting=t,this.azureSetting.endpoint.toLowerCase().startsWith("https://")||(this.azureSetting.endpoint="https://"+this.azureSetting.endpoint)}createOpenAI(e){return new v(this.azureSetting.endpoint,new C(e))}get EmbeddingUrl(){return`${this.azureSetting.endpoint}/openai/deployments/${this.embeddingmodel||"openai-embedding-ada-002"}/embeddings?api-version=2022-12-01`}getTextEmbedding(e){return S(this,arguments,void 0,function*(e,t={}){if(!e)return{successed:!1,error:{errcode:2,errmsg:"content required"}};this.aiApi||(this.aiApi=this.createOpenAI(this.apiKey));try{return{successed:!0,embedding:(yield this.aiApi.getEmbeddings(this.embeddingmodel||"openai-embedding-ada-002","string"==typeof e?[e]:e,t)).data}}catch(e){return{successed:!1,error:e}}})}chatRequest(e,t){return S(this,arguments,void 0,function*(e,t,i={}){if(!e)return{successed:!1,error:{errcode:2,errmsg:"缺失聊天的内容"}};this.aiApi||(this.aiApi=this.createOpenAI(this.apiKey));let s="string"==typeof e?[{role:"user",content:e}]:e;try{const e=yield this.aiApi.getChatCompletions((null==t?void 0:t.model)||this.chatModel,s,{temperature:Number((null==t?void 0:t.temperature)||this.temperature),maxTokens:Number((null==t?void 0:t.maxtoken)||this.maxtoken),topP:Number((null==t?void 0:t.top_p)||this.top_p),presencePenalty:Number((null==t?void 0:t.presence_penalty)||this.presence_penalty),frequencyPenalty:Number((null==t?void 0:t.frequency_penalty)||this.frequency_penalty),n:Number((null==t?void 0:t.replyCounts)||1)||1,tools:1===(null==t?void 0:t.enableToolCall)&&(null==t?void 0:t.tools)?t.tools:void 0,toolChoice:1===(null==t?void 0:t.enableToolCall)?"auto":void 0}),{promptTokens:i,completionTokens:n,totalTokens:r}=e.usage;let o=[];for(const t of e.choices){const{index:e,finishReason:i,message:s}=t;o.push({index:e,finish_reason:i,message:s})}return{successed:!0,message:o,usage:{prompt_tokens:i,completion_tokens:n,total_tokens:r}}}catch(e){return console.log("result is error ",e),{successed:!1,error:e}}})}chatRequestInStream(e,t,i,s){return S(this,void 0,void 0,function*(){var n,r,o,a,c,u,l;e||this.emit("chaterror",{successed:!1,error:"no text in chat"}),this.aiApi||(this.aiApi=this.createOpenAI(this.apiKey));let d="string"==typeof e?[{role:"user",content:e}]:e;s=Object.assign({},s||{timeout:6e4});let h,p=Math.ceil(Math.random()*((new Date).getTime()*Math.random())/1e3),m=0,g=-1,_=[];try{const e=yield this.aiApi.streamChatCompletions((null==t?void 0:t.model)||this.chatModel,d,{temperature:Number((null==t?void 0:t.temperature)||this.temperature),maxTokens:Number((null==t?void 0:t.maxtoken)||this.maxtoken),topP:Number((null==t?void 0:t.top_p)||this.top_p),presencePenalty:Number((null==t?void 0:t.presence_penalty)||this.presence_penalty),frequencyPenalty:Number((null==t?void 0:t.frequency_penalty)||this.frequency_penalty),n:Number((null==t?void 0:t.replyCounts)||1)||1,tools:1===(null==t?void 0:t.enableToolCall)&&(null==t?void 0:t.tools)?t.tools:void 0,toolChoice:1===(null==t?void 0:t.enableToolCall)?"auto":void 0});let s=[];try{for(var f,T=!0,y=k(e);!(n=(f=yield y.next()).done);T=!0){a=f.value,T=!1;const e=a;for(const t of e.choices){const{finishReason:e,index:n}=t,r=null===(c=t.delta)||void 0===c?void 0:c.toolCalls;if(r&&r.length)h=r[0].index,m=1,h!==g?(_.push({id:r[0].id,type:"function",function:{name:r[0].function.name,arguments:r[0].function.arguments}}),g=h):_[g].function.arguments+=r[0].function.arguments;else{const e=null===(u=t.delta)||void 0===u?void 0:u.content;s.push(e)}let o={successed:!0,requestid:p,segment:null===(l=t.delta)||void 0===l?void 0:l.content,text:s.join(""),finish_reason:e,index:n,has_tool_calls:m,tool_calls:_};i&&(o=Object.assign({},o,i)),this.emit(e?"chatdone":"chattext",o)}}}catch(e){r={error:e}}finally{try{T||n||!(o=y.return)||(yield o.call(y))}finally{if(r)throw r.error}}return{successed:!0,requestid:p}}catch(e){return this.emit("requesterror",{successed:!1,requestid:p,error:"call axios faied "+e}),{successed:!1,requestid:p}}})}}function q(){return S(this,arguments,void 0,function*(e={}){e.data||(e.data=e.body);try{return{successed:!0,data:(yield E(e)).data}}catch(e){return{successed:!1,error:e,data:e}}})}function L(e){return new Promise(t=>setTimeout(t,e))}class F extends N{constructor(e,t){super(),this.apikey=e,this.agent=t}chatRequest(e,t){return S(this,arguments,void 0,function*(e,t,i={}){var s;if(!e)return{successed:!1,error:"no text in chat"};const n="object"==typeof e?null!==(s=e.text)&&void 0!==s?s:e.content:e;i=Object.assign({},i||{timeout:6e4});const r=Object.assign({headers:{"Content-Type":"application/json",authorization:`Bearer ${this.apikey}`},method:"post",url:`${this.agent.endpoint}/api/v1/agents/${this.agent.agentid}/completions`,data:{question:n,session_id:t.session_id,optional:t.optional,stream:!1}},i),o=yield q(r);if(!o.successed||o.data.code)return{successed:!1,error:"failed"};const{answer:a,session_id:c}=o.data.data;return{successed:!0,message:[{message:{content:a}}],session_id:c}})}chatRequestInStream(e,t,i,s){return S(this,void 0,void 0,function*(){var n,r,o,a,c;e||this.emit("chaterror",{successed:!1,error:"no text in chat"});const u="object"==typeof e?null!==(c=e.text)&&void 0!==c?c:e.content:e;s=Object.assign({},s||{timeout:6e4});let l=Math.ceil(Math.random()*((new Date).getTime()*Math.random())/1e3);try{const e=Object.assign({headers:{"Content-Type":"application/json",authorization:`Bearer ${this.apikey}`},method:"post",url:`${this.agent.endpoint}/api/v1/agents/${this.agent.agentid}/completions`,data:{question:u,session_id:t.session_id,stream:!0,optional:t.optional},responseType:"stream"},s),c=yield E(e),m=O.from(c.data);let g,_=0,f="",T=[],y=[];try{for(var d,h=!0,p=k(m);!(n=(d=yield p.next()).done);h=!0){a=d.value,h=!1;const e=a;let t=null,s="";try{s=e.toString().split("data:"),s.length&&(s=s[s.length-1]+""),t=JSON.parse(T.join("")+s)}catch(e){T.push(s);continue}T=[];const{answer:n,running_status:r,reference:o}=t.data;if(!0===r)continue;const c=n?n.replace(f,""):"";f=null!=n?n:f,g||(g=t.data.session_id),o&&o.chunks&&o.chunks.length&&(y=y.concat(o.chunks));const u=!0===t.data;let p={successed:!0,requestid:l,segment:c,chunks:y,text:f,finish_reason:u?"stop":null,index:_++,session_id:g};i&&(p=Object.assign({},p,i)),this.emit(u?"chatdone":"chattext",p)}}catch(e){r={error:e}}finally{try{h||n||!(o=p.return)||(yield o.call(p))}finally{if(r)throw r.error}}return{successed:!0,requestid:l}}catch(e){}})}}class j extends N{constructor(e,t={}){super(),this.apiOption={},this.apiKey=e,this.apiOption=t}getTextEmbedding(e){return S(this,arguments,void 0,function*(e,t={}){if(!e)return{successed:!1,error:{errcode:2,errmsg:"content required"}};if(Array.isArray(e)&&0===e.length)return{successed:!1,error:{errcode:2,errmsg:"content required"}};const i=Array.isArray(e)?e:[e],s={method:"post",headers:{"Content-Type":"application/json",authorization:`Bearer ${this.apiKey}`},data:{model:t.model||this.apiOption.embedding,input:i},url:"https://ark.cn-beijing.volces.com/api/v3/embeddings"};try{return{successed:!0,embedding:(yield q(s)).data}}catch(e){return{successed:!1,error:e}}})}chatRequest(e,t){return S(this,arguments,void 0,function*(e,t,i={}){if(!e)return{successed:!1,error:{errcode:2,errmsg:"缺失聊天的内容"}};const s=this.assembleApiParams(e,!1,t,i);try{const e=yield q(s);return e.successed&&!e.data.code?{successed:!0,message:e.data.choices,usage:e.data.usage}:Object.assign({successed:!1},e.data)}catch(e){return console.log("result is error ",e),{successed:!1,error:e}}})}assembleApiParams(e,t=!1,i,s={}){let n="string"==typeof e?[{role:"user",content:e}]:e,r={};((null==i?void 0:i.temperature)||this.apiOption.temperature)&&(r.temperature=Number((null==i?void 0:i.temperature)||this.apiOption.temperature)),r.max_tokens=Number((null==i?void 0:i.maxtoken)||this.apiOption.maxtoken),((null==i?void 0:i.top_p)||this.apiOption.top_p)&&(r.top_p=Number((null==i?void 0:i.top_p)||this.apiOption.top_p)),((null==i?void 0:i.presence_penalty)||this.apiOption.presence_penalty)&&(r.presence_penalty=Number((null==i?void 0:i.presence_penalty)||this.apiOption.presence_penalty)),((null==i?void 0:i.frequency_penalty)||this.apiOption.frequency_penalty)&&(r.frequency_penalty=Number((null==i?void 0:i.frequency_penalty)||this.apiOption.frequency_penalty)),((null==i?void 0:i.top_logprobs)||this.apiOption.top_logprobs)&&(r.logprobs=!0,r.top_logprobs=Number((null==i?void 0:i.top_logprobs)||this.apiOption.top_logprobs)),r.tools=1===(null==i?void 0:i.enableToolCall)&&(null==i?void 0:i.tools)?i.tools:void 0,r.tool_choice=1===(null==i?void 0:i.enableToolCall)?"auto":void 0;const o=Object.assign(Object.assign({},s),{method:"post",headers:{"Content-Type":"application/json",authorization:`Bearer ${this.apiKey}`},data:Object.assign(Object.assign({model:(null==i?void 0:i.model)||this.apiOption.model},r),{messages:n,stream:t}),url:"https://ark.cn-beijing.volces.com/api/v3/chat/completions"});return t&&(o.responseType="stream"),o}chatRequestInStream(e,t,i,s){return S(this,void 0,void 0,function*(){e||this.emit("chaterror",{successed:!1,error:"no text in chat"}),s=Object.assign({},s||{timeout:1e4});const n=this.assembleApiParams(e,!0,t,s);let r=Math.ceil(Math.random()*((new Date).getTime()*Math.random())/1e3),o=[];try{return function(e={},t){e.data||(e.data=e.body),E(e).then(e=>{e.data.on("data",e=>{t(e)})})}(n,e=>{let t=e.toString().replace("[DONE]","").replace(/[\r\n]+/gm,"");this.processChunkData(t.split(/data: /),r,o,i)}),{successed:!0,requestid:r}}catch(e){return this.emit("requesterror",{successed:!1,requestid:r,error:"call axios faied "+e}),{successed:!1,requestid:r}}})}processChunkData(e,t,i,s){let n,r=0,o=-1,a=[];for(const c of e){if(!c)continue;const e=JSON.parse(c),[u]=e.choices,{finish_reason:l,index:d,usage:h}=u,{content:p,tool_calls:m}=u.delta;m&&m.length?(n=m[0].index,r=1,n!==o?(a.push({id:m[0].id,type:"function",function:{name:m[0].function.name,arguments:m[0].function.arguments}}),o=n):a[o].function.arguments+=m[0].function.arguments):i.push(p);let g={successed:!0,requestid:t,segment:p,text:i.join(""),finish_reason:l,index:d,usage:h,has_tool_calls:r,tool_calls:a};s&&(g=Object.assign({},g,s)),this.emit(l?"chatdone":"chattext",g)}}}class W{constructor(e,t){this.appid=e,this.expiresAt=0,this.secret=t.keyid,this.pemfile=t.pemfile||"private.pem",this.sessionName=t.sessionName||"default"}getAccessToken(){return S(this,void 0,void 0,function*(){if(this.accessToken&&Date.now()<this.expiresAt)return this.accessToken;const e=yield this.doAuthorize();return e.successed?(this.accessToken=e.accessToken,this.expiresAt=1e3*e.expires_in,this.accessToken):null})}doAuthorize(){return S(this,void 0,void 0,function*(){try{const e="api.coze.cn",t=g.join(process.cwd(),this.pemfile);if(!h.existsSync(t))return{successed:!1,error:`${t} not existed`};const i=h.readFileSync(t).toString(),n=yield l({baseURL:s,appId:this.appid,aud:e,keyid:this.secret,privateKey:i,sessionName:this.sessionName});return{successed:!0,accessToken:n.access_token,expires_in:n.expires_in}}catch(e){return{successed:!0,error:e}}})}}const $=new Map,z={OPENAI:"openai",OPENAIPROXY:"openaiproxy",MICROSOFT:"microsoft",AIMP:"aimp",COZE:"coze",DOUBAO:"doubao"};var H=Object.freeze({__proto__:null,GptProviderEnum:z,createGpt:function(e,t,i){let{model:s,agentid:n,maxtoken:r,temperature:o,serviceurl:a,endpoint:c,engine:u,version:l,embedding:d,top_p:h,presence_penalty:p,frequency_penalty:m}=i||{};switch(e){case z.OPENAI:return new w(t+"",{model:s,maxtoken:r,temperature:o,embedding:d,top_p:h,presence_penalty:p,frequency_penalty:m});case z.OPENAIPROXY:return new B(t+"",{serviceurl:a},{model:s,maxtoken:r,temperature:o,embedding:d,top_p:h,presence_penalty:p,frequency_penalty:m});case z.MICROSOFT:return new M(t+"",{endpoint:c,engine:u,version:l},{model:s,maxtoken:r,temperature:o,embedding:d,top_p:h,presence_penalty:p,frequency_penalty:m});case z.AIMP:return new F(t+"",{endpoint:c,agentid:n});case z.COZE:let e=$.get(t+"");return e||(e=new W(t+"",i),$.set(t+"",e)),!0===i.voicebot?new U(e,i):new P(e,i);case z.DOUBAO:return new j(t+"",{model:s,maxtoken:r,temperature:o,top_p:h,presence_penalty:p,frequency_penalty:m,embedding:d});default:return null}}});const G=["。","!","!","?","?",";",";",":",":","\n"];class K extends A{constructor(e,t){super(),this.agentBot=e,this.chatconfig=t,this.asrSocket=null,this.ttsSocket=null,this.chatVoiceBuffer=[],this.chatContent=[],this.apiToken=null,this.waitingTimes=0,this.waitingTimer=null,this.traceId=y(),this.userTalkBuffer=[],this.userTalkStart=0,this.agentTalkBuffer=[],this.agentTalkStart=0,this.currentTTSConent=[],this.lastGetAsrChangedTime=0,this.breaked=!1,this.connectors={asr:!1,tts:!1}}get AgentBot(){return this.agentBot}get ChatConfig(){return this.chatconfig}getChatVoiceFile(){return this.chatVoiceBuffer.length?Buffer.concat([...this.chatVoiceBuffer]):Buffer.alloc(0)}clearWaitingTimer(){this.waitingTimer&&clearTimeout(this.waitingTimer),this.waitingTimer=null}startWaitingUserSpeaking(e=0){this.chatconfig.timeout_config&&this.chatconfig.timeout_config.interval&&this.chatconfig.timeout_config.messages&&this.chatconfig.timeout_config.messages.length&&(console.log(`第${this.waitingTimes+1}等待用户说话,等待${this.chatconfig.timeout_config.interval+e} 秒`),this.clearWaitingTimer(),this.waitingTimer=setTimeout(()=>{const e=this.chatconfig.timeout_config.messages.length,t=this.chatconfig.timeout_config.messages[this.waitingTimes++%e]+"";this.createTtsConnector(t),this.chatconfig.timeout_config.maxtimes&&this.waitingTimes>=this.chatconfig.timeout_config.maxtimes?setTimeout(()=>this.emit("voicechat.end",{reason:"wait_timeout"}),t.length/4*1e3):this.startWaitingUserSpeaking()},1e3*(this.chatconfig.timeout_config.interval+e)))}getChatContent(){return this.chatContent}sendText2Tts(e,t=!1){if(e){const t={id:this.traceId,event_type:c.INPUT_TEXT_BUFFER_APPEND,data:{delta:e}};this.sendCommand("tts",t),this.currentTTSConent.push(e),this.emit(c.CONVERSATION_AUDIO_SENTENCE_START,{event_type:c.CONVERSATION_AUDIO_SENTENCE_START,data:{text:this.currentTTSConent.join("")}})}if(t){const e={id:this.traceId,event_type:c.INPUT_TEXT_BUFFER_COMPLETE};this.sendCommand("tts",e)}}sendVoice(e){var t;if(!e)return;this.userTalkStart||(this.userTalkStart=(new Date).getTime());const i=e.toString("base64"),s={id:this.traceId,event_type:c.INPUT_AUDIO_BUFFER_APPEND,data:{delta:i}};this.sendCommand("asr",s),this.userTalkBuffer.push(e),this.lastGetAsrChangedTime&&(new Date).getTime()-this.lastGetAsrChangedTime>((null===(t=this.chatconfig.vad_config)||void 0===t?void 0:t.silence_duration_ms)||500)&&(this.lastGetAsrChangedTime=0,this.emit(c.INPUT_AUDIO_BUFFER_SPEECH_STOPPED,{event_type:c.INPUT_AUDIO_BUFFER_SPEECH_STOPPED}),this.submitVoiceBuffer())}sendChatMessage(e,t={}){this.agentBot.chatRequestInStream(e,t)}submitVoiceBuffer(){const e={id:this.traceId,event_type:c.INPUT_AUDIO_BUFFER_COMPLETE};this.sendCommand("asr",e)}clearVoiceBuffer(){const e={id:this.traceId,event_type:c.INPUT_AUDIO_BUFFER_CLEAR};this.sendCommand("asr",e)}breakAnswer(e=!1){this.breaked=!0;const t={id:this.traceId,event_type:c.INPUT_TEXT_BUFFER_COMPLETE};this.sendCommand("tts",t),this.emit(c.CONVERSATION_CHAT_CANCELED),e||this.startWaitingUserSpeaking()}configurationConnector(e,t){"tts"===e&&t&&this.chatconfig.emotionSupport&&(t.emotionContext&&(this.chatconfig.output_audio_config.context_texts=t.emotionContext),this.chatconfig.output_audio_config.emotion_config={emotion:t.emotion,emotion_scale:t.emotionScale||4});const i="tts"===e?{id:this.traceId,event_type:c.SPEECH_UPDATE,data:{output_audio:this.chatconfig.output_audio_config}}:{id:this.traceId,event_type:c.TRANSCRIPTIONS_UPDATE,data:{input_audio:this.chatconfig.input_audio_config,asr_config:this.chatconfig.asr_config}};this.sendCommand(e,i)}decorateUserMessage(e){return S(this,void 0,void 0,function*(){const t={question:e,answerByAgent:!0,chatparams:this.chatconfig.chat_config};if(this.chatconfig.emotionSupport){const e=yield this.getEmotion(this.chatContent);t.emotionInfo=e}return t})}getEmotion(e){return S(this,void 0,void 0,function*(){return{emotion:"neutral",emotionScale:4}})}getLastPunctuationInfo(e){if(!e||e.length<=5)return{index:-1};const t=new Set(["。","!","!","?","?",";",";",":",":","\n"]);for(let i=e.length-1;i>=5;i--){const s=e[i];if(t.has(s))return{index:i,text:e.slice(0,i+1),remain:e.slice(i+1)}}return{index:-1}}registerAgentEvent(){return S(this,void 0,void 0,function*(){let e=[];this.agentBot&&(this.agentBot.on("chattext",t=>{if(this.breaked||!t.segment)return;e.push(t.segment);const i=this.getLastPunctuationInfo(e.join(""));i.index>0&&(this.sendText2Tts(i.text,!1),e=i.remain?[i.remain]:[])}),this.agentBot.on("chatdone",t=>this.sendText2Tts(e.join(""),!0)))})}processAsrResult(e){return S(this,arguments,void 0,function*(e,t=!1){return e})}processChatResult(e,t,i,s){return S(this,arguments,void 0,function*(e,t,i,s,n="pcm"){const o=(new Date).getTime();return{role:e,content:t,start:s,end:o,duration:(o-s)/1e3,type:e===r.User?"question":"answer"}})}registerAsrEvent(e){return S(this,void 0,void 0,function*(){let t="";e.on("open",()=>console.log("Connected to asr server.")),e.on("message",e=>S(this,void 0,void 0,function*(){const i=JSON.parse(e.toString());switch(i.event_type){case c.TRANSCRIPTIONS_CREATED:this.configurationConnector("asr"),this.emit(c.TRANSCRIPTIONS_CREATED,i);break;case c.TRANSCRIPTIONS_UPDATED:this.connectors.asr=!0;break;case c.INPUT_AUDIO_BUFFER_COMPLETED:this.lastGetAsrChangedTime=0,this.emit(c.INPUT_AUDIO_BUFFER_COMPLETED,i);break;case c.TRANSCRIPTIONS_MESSAGE_UPDATE:this.lastGetAsrChangedTime=(new Date).getTime(),t=yield this.processAsrResult(i.data.content),this.emit(c.TRANSCRIPTIONS_MESSAGE_UPDATE,{content:t});break;case c.TRANSCRIPTIONS_MESSAGE_COMPLETED:t=yield this.processAsrResult(t,!0);const e=yield this.processChatResult(r.User,t,Buffer.concat([...this.userTalkBuffer]),this.userTalkStart);this.chatContent.push(e),this.emit(c.CONVERSATION_MESSAGE_COMPLETED,{event_type:c.CONVERSATION_MESSAGE_COMPLETED,data:e}),this.chatVoiceBuffer.push(Buffer.concat([...this.userTalkBuffer])),this.userTalkBuffer=[],this.userTalkStart=0,this.emit(c.TRANSCRIPTIONS_MESSAGE_COMPLETED,e);const s=yield this.decorateUserMessage(t);t="",s.answerByAgent?(this.breaked=!1,this.createTtsConnector(null,s.emotionInfo),this.agentBot.chatRequestInStream(s.question,s.chatparams)):s.answerContent&&this.streamOutputContent(s.answerContent,s.chatparams);break;case c.INPUT_AUDIO_BUFFER_CLEARED:this.emit(c.INPUT_AUDIO_BUFFER_CLEARED,i);break;case c.ERROR:this.close(),this.emit(c.ERROR,{type:"asr",error:i.data})}}))})}streamOutputContent(e,t){this.createTtsConnector(null,t);const i=function(e){const t=new RegExp("(["+G.join("")+"])","g");return t.test(e)?e.split(t).reduce((e,t,i,s)=>(i%2==0&&e.push(t+(s[i+1]||"")),e),[]).filter(e=>e.trim().length>0):[e]}(e),s=this;!function e(t=0){setTimeout(()=>{const n=i[t];s.sendText2Tts(n,t===i.length-1),t<i.length-1&&e(t+1)},200)}()}close(){return S(this,arguments,void 0,function*(e="all"){["all","asr"].includes(e.toLocaleLowerCase())&&this.closeAsr(),["all","tts"].includes(e.toLocaleLowerCase())&&this.closeTts()})}closeAsr(){this.asrSocket&&(this.asrSocket.close(),this.asrSocket.removeAllListeners(),this.asrSocket=null)}closeTts(){this.ttsSocket&&(this.ttsSocket.close(),this.ttsSocket.removeAllListeners(),this.ttsSocket=null)}registerTtsEvent(e,t,i){return S(this,void 0,void 0,function*(){e.on("open",()=>console.log("Connected to tts server.")),e.on("message",e=>S(this,void 0,void 0,function*(){const s=JSON.parse(e.toString());switch(s.event_type){case c.SPEECH_CREATED:this.configurationConnector("tts",i),this.emit(c.SPEECH_CREATED,s);break;case c.SPEECH_UPDATED:this.connectors.tts=!0,t&&this.sendText2Tts(t,!0);break;case c.INPUT_TEXT_BUFFER_COMPLETED:this.emit(c.INPUT_TEXT_BUFFER_COMPLETED,s);break;case c.SPEECH_AUDIO_UPDATE:if(!s.data.delta||this.breaked)return;this.agentTalkStart||(this.agentTalkStart=(new Date).getTime());const e=Buffer.from(s.data.delta,"base64");this.agentTalkBuffer.push(e),this.emit(c.CONVERSATION_AUDIO_DELTA,e);break;case c.SPEECH_AUDIO_COMPLETED:const n=yield this.processChatResult(r.Assistant,this.currentTTSConent.join(""),Buffer.concat([...this.agentTalkBuffer]),this.agentTalkStart);this.emit(c.CONVERSATION_MESSAGE_COMPLETED,{event_type:c.CONVERSATION_MESSAGE_COMPLETED,data:n}),this.emit(c.CONVERSATION_AUDIO_COMPLETED,n),this.chatVoiceBuffer.push(Buffer.concat([...this.agentTalkBuffer])),this.chatContent.push(n),this.currentTTSConent=[],this.agentTalkBuffer=[],this.agentTalkStart=0,n.content.length>0&&this.startWaitingUserSpeaking(n.content.length/4),this.closeTts();break;case c.ERROR:this.emit(c.ERROR,{type:"tts",error:s.data}),this.close()}}))})}startVoiceChat(e){return S(this,void 0,void 0,function*(){if(!this.agentBot)return{successed:!1,code:-1,error:"缺少对话机器人"};if(!this.chatconfig.output_audio_config.voice_id)return{successed:!1,code:-2,error:"请提供语音音色"};const t=new W(this.chatconfig.auth_config.appid,this.chatconfig.auth_config.setting);return this.apiToken=yield t.getAccessToken(),this.apiToken?(this.chatconfig.playPrologue&&this.chatconfig.prologueContent?this.createTtsConnector(this.chatconfig.prologueContent):this.connectors.tts=!0,e?this.connectors.asr=!0:this.createAsrConnector(),this.registerAgentEvent(),new Promise((e,t)=>S(this,void 0,void 0,function*(){for(;;){if(this.connectors.asr&&this.connectors.tts){const t=this.ChatConfig.playPrologue&&this.ChatConfig.prologueContent;return this.emit(c.CHAT_UPDATED,{status:t?"playvoice":"startrecord"}),e({successed:!0})}yield L(50)}}))):{successed:!1,error:"获取token失败"}})}createAsrConnector(){const e=`${u}/v1/audio/transcriptions?authorization=Bearer ${this.apiToken}`;this.asrSocket=new T(e),this.registerAsrEvent(this.asrSocket)}createTtsConnector(e,t){if(this.ttsSocket)return;const i=`${u}/v1/audio/speech?authorization=Bearer ${this.apiToken}`;this.ttsSocket=new T(i),this.registerTtsEvent(this.ttsSocket,e,t)}sendAsrCommand(e){this.asrSocket&&this.asrSocket.readyState===T.OPEN&&this.asrSocket.send("string"==typeof e?e:JSON.stringify(e))}sendTtsCommand(e){this.ttsSocket&&this.ttsSocket.readyState===T.OPEN&&this.ttsSocket.send("string"==typeof e?e:JSON.stringify(e))}sendCommand(e,t){return"asr"===e.toLocaleLowerCase()?this.sendAsrCommand(t):"tts"===e.toLocaleLowerCase()?this.sendTtsCommand(t):void 0}}export{K as DualVoiceAgent,I as GptBase,H as GptFactory};//# sourceMappingURL=index.esm.js.map
|
|
1
|
+
import{EventEmitter as e}from"events";import t from"openai";import{CozeAPI as i,COZE_CN_BASE_URL as s,ChatStatus as n,RoleType as r,ChatEventType as o,WorkflowEventType as a,WebsocketsEventType as c,COZE_CN_BASE_WS_URL as u,getJWTToken as d}from"@coze/api";export{WebsocketsEventType as COZE_SOCKET_EVENT_TYPE}from"@coze/api";import{parse as h}from"querystring";import l,{mkdirSync as p,createWriteStream as m}from"fs";import g,{join as _,resolve as f}from"path";import T from"ws";import{randomUUID as y}from"crypto";import E from"axios";import{OpenAIClient as v,AzureKeyCredential as C}from"@azure/openai";import{Readable as O,EventEmitter as A}from"stream";function S(e,t,i,s){return new(i||(i=Promise))(function(n,r){function o(e){try{c(s.next(e))}catch(e){r(e)}}function a(e){try{c(s.throw(e))}catch(e){r(e)}}function c(e){var t;e.done?n(e.value):(t=e.value,t instanceof i?t:new i(function(e){e(t)})).then(o,a)}c((s=s.apply(e,t||[])).next())})}function b(e){var t="function"==typeof Symbol&&Symbol.iterator,i=t&&e[t],s=0;if(i)return i.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&s>=e.length&&(e=void 0),{value:e&&e[s++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function k(e){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var t,i=e[Symbol.asyncIterator];return i?i.call(e):(e=b(e),t={},s("next"),s("throw"),s("return"),t[Symbol.asyncIterator]=function(){return this},t);function s(i){t[i]=e[i]&&function(t){return new Promise(function(s,n){(function(e,t,i,s){Promise.resolve(s).then(function(t){e({value:t,done:i})},t)})(s,n,(t=e[i](t)).done,t.value)})}}}"function"==typeof SuppressedError&&SuppressedError;class N extends e{constructor(){super()}getTextEmbedding(e,t){return null}createCoversation(e){return S(this,void 0,void 0,function*(){return null})}setVariables(e){return S(this,void 0,void 0,function*(){return{successed:!0}})}getVariables(e){return S(this,void 0,void 0,function*(){return{successed:!1}})}chatRequestInStream(e,t,i,s){return null}}var I=Object.freeze({__proto__:null,default:N});class R extends N{constructor(e,t={}){super(),this.apiKey=e,this.chatModel=t.model||"gpt-3.5-turbo",this.maxtoken=t.maxtoken||2048,this.top_p=t.top_p||.95,this.temperature=t.temperature||.9,this.presence_penalty=t.presence_penalty||0,this.frequency_penalty=t.frequency_penalty||0,this.embeddingmodel=t.embedding||"text-embedding-ada-002"}}class w extends R{createOpenAI(e){return new t({apiKey:e})}getTextEmbedding(e,t){return S(this,void 0,void 0,function*(){if(!e)return{successed:!1,error:{errcode:2,errmsg:"content required"}};this.aiApi||(this.aiApi=this.createOpenAI(this.apiKey));try{return{successed:!0,embedding:(yield this.aiApi.embeddings.create({model:this.embeddingmodel,input:e},t)).data.data}}catch(e){return{successed:!1,error:e}}})}chatRequest(e,t){return S(this,arguments,void 0,function*(e,t,i={}){if(!e)return{successed:!1,error:{errcode:2,errmsg:"缺失聊天的内容"}};this.aiApi||(this.aiApi=this.createOpenAI(this.apiKey));let s="string"==typeof e?[{role:"user",content:e}]:e;try{const e=yield this.aiApi.chat.completions.create({model:(null==t?void 0:t.model)||this.chatModel,messages:s,temperature:Number((null==t?void 0:t.temperature)||this.temperature),max_tokens:Number((null==t?void 0:t.maxtoken)||this.maxtoken),top_p:Number((null==t?void 0:t.top_p)||this.top_p),presence_penalty:Number((null==t?void 0:t.presence_penalty)||this.presence_penalty),frequency_penalty:Number((null==t?void 0:t.frequency_penalty)||this.frequency_penalty),n:Number((null==t?void 0:t.replyCounts)||1)||1},i);return{successed:!0,message:e.choices,usage:e.usage}}catch(e){return console.log("result is error ",e),{successed:!1,error:e}}})}chatRequestInStream(e,t,i,s){return S(this,void 0,void 0,function*(){var n,r,o,a;e||this.emit("chaterror",{successed:!1,error:"no text in chat"}),this.aiApi||(this.aiApi=this.createOpenAI(this.apiKey));let c="string"==typeof e?[{role:"user",content:e}]:e;s=Object.assign({},s||{timeout:6e4});let u=Math.ceil(Math.random()*((new Date).getTime()*Math.random())/1e3);try{const e=yield this.aiApi.chat.completions.create({model:(null==t?void 0:t.model)||this.chatModel,messages:c,temperature:Number((null==t?void 0:t.temperature)||this.temperature),max_tokens:Number((null==t?void 0:t.maxtoken)||this.maxtoken),top_p:Number((null==t?void 0:t.top_p)||this.top_p),presence_penalty:Number((null==t?void 0:t.presence_penalty)||this.presence_penalty),frequency_penalty:Number((null==t?void 0:t.frequency_penalty)||this.frequency_penalty),n:Number((null==t?void 0:t.replyCounts)||1)||1,tools:1===(null==t?void 0:t.enableToolCall)&&(null==t?void 0:t.tools)?t.tools:void 0,tool_choice:1===(null==t?void 0:t.enableToolCall)?"auto":void 0,stream:!0},s);let p,m=[],g=0,_=-1,f=[];try{for(var d,h=!0,l=k(e);!(n=(d=yield l.next()).done);h=!0){a=d.value,h=!1;const e=a,[t]=e.choices,{finish_reason:s,index:n,usage:r}=t,{content:o,tool_calls:c}=t.delta;c&&c.length?(p=c[0].index,g=1,p!==_?(f.push({id:c[0].id,type:"function",function:{name:c[0].function.name,arguments:c[0].function.arguments}}),_=p):f[_].function.arguments+=c[0].function.arguments):m.push(o);let l={successed:!0,requestid:u,segment:o,text:m.join(""),finish_reason:s,index:n,usage:r,has_tool_calls:g,tool_calls:f};i&&(l=Object.assign({},l,i)),this.emit(s?"chatdone":"chattext",l)}}catch(e){r={error:e}}finally{try{h||n||!(o=l.return)||(yield o.call(l))}finally{if(r)throw r.error}}return{successed:!0,requestid:u}}catch(e){return this.emit("requesterror",{successed:!1,requestid:u,error:"call axios faied "+e}),{successed:!1,requestid:u}}})}}const x={thinking:{start:"<xiaolu-think>",end:"</xiaolu-think>",tag:"xiaolu-think"},reasoning:{start:"<xiaolu-reason>",end:"</xiaolu-reason>",tag:"xiaolu-reason"},card:{start:"<xiaolu-card>",end:"</xiaolu-card>",tag:"xiaolu-card"},preresponse:{start:"<xiaolu-preresponse>",end:"</xiaolu-preresponse>",tag:"xiaolu-preresponse"}};var D;!function(e){e[e.None=0]="None",e[e.Thinking=1]="Thinking",e[e.ThinkingOver=2]="ThinkingOver"}(D||(D={}));class P extends N{constructor(e,t={}){if(super(),this.authorizationProvider=e,this.setting=t,this.botid=null,this.workflowid=null,this.talkflowid=null,this.debugDir="",this.debug=!1,this.setting.workflowid)this.workflowid=this.setting.workflowid;else if(this.setting.talkflowid)this.talkflowid=this.setting.talkflowid;else{if(!this.setting.botid&&!this.setting.botID)throw new Error("no botid or talkflowid or workflowid setting for coze");this.botid=this.setting.botid||this.setting.botID}!0===t.debug&&(this.debug=!0,this.debugDir=_(t.logPath||f(process.cwd(),"./cozedebug"),this.setting.model||"chat"),p(this.debugDir,{recursive:!0})),this.apiKey=this.setting.apiKey}createClient(){return S(this,void 0,void 0,function*(){if(this.apiKey)return new i({baseURL:s,token:this.apiKey});const e=yield this.authorizationProvider.getAccessToken();if(!e)throw new Error("get access token failed");return new i({baseURL:s,token:e})})}cloneVoice(e){return S(this,arguments,void 0,function*(e,t={}){const i=yield this.createClient();try{return{successed:!0,voice_id:(yield i.audio.voices.clone(e,t)).voice_id}}catch(e){return{successed:!1,error:e}}})}createSpeech(e,t){return S(this,arguments,void 0,function*(e,t,i="mp3",s={}){const n=yield this.createClient();try{return{successed:!0,data:yield n.audio.speech.create({input:e,voice_id:t,response_format:i},s)}}catch(e){return{successed:!1,error:e}}})}createCoversation(e){return S(this,void 0,void 0,function*(){try{const t=null!=e?e:yield this.createClient(),i=this.botid?{bot_id:this.botid}:{};return(yield t.conversations.create(i)).id}catch(e){return console.error("createCoversation error in coze api"),null}})}setVariables(){return S(this,arguments,void 0,function*(e={}){const t=yield this.createClient();!e.type&&(e.type="app");const i={data:e.data};return"app"===e.type&&(i.app_id=this.authorizationProvider.appid),"bot"===e.type&&this.botid&&(i.bot_id=this.botid),e.user_id&&(i.connector_uid=e.user_id),yield t.variables.update(i,{debug:!0}),{successed:!0}})}getVariables(e){return S(this,void 0,void 0,function*(){const t=yield this.createClient(),i=Object.assign({},{bot_id:this.botid},e),s=yield t.variables.retrieve(i);return s?Object.assign({successed:!0},s):{successed:!1,error:"get variables failed"}})}queryWorkflowExecuteResult(e,t){return S(this,arguments,void 0,function*(e,t,i={}){if(!e||!t)return{successed:!1,error:"workflowid or executeid is null"};const s=yield this.createClient();try{return{successed:!0,history:yield s.workflows.runs.history(e,t,i)}}catch(e){return{successed:!1,error:e}}})}getRequestStream(e,t){return S(this,arguments,void 0,function*(e,t,i={}){var s,n;if(this.botid){const n={bot_id:this.botid,additional_messages:t,user_id:i.userid||i.cozeUserID,conversation_id:null!==(s=i.session_id)&&void 0!==s?s:yield this.createCoversation(e),parameters:Object.assign({request_src:1},i.parameters||{})};return n.custom_variables=Object.assign({},this.setting.customVariables||{},i.customVariables||{}),n}if(this.workflowid){const e={ext:i.ext,workflow_id:this.workflowid,is_async:i.is_async||!1,parameters:Object.assign({request_src:1},i.parameters||{},{input:t})};return(i.userid||i.cozeUserID)&&(e.ext=Object.assign({},e.ext||{},{user_id:i.userid||i.cozeUserID})),e}const r={additional_messages:t,workflow_id:this.talkflowid,conversation_id:null!==(n=i.session_id)&&void 0!==n?n:yield this.createCoversation(e),parameters:Object.assign({request_src:1},i.parameters||{})};return(i.userid||i.cozeUserID)&&(r.ext=Object.assign({},r.ext||{},{user_id:i.userid||i.cozeUserID})),r})}getChatDetail(e,t,i){return S(this,void 0,void 0,function*(){const s=Date.now();for(;Date.now()-s<12e4;){const s=yield e.chat.retrieve(t,i);if(s.status===n.COMPLETED)return{usage:s.usage,messages:yield e.chat.messages.list(t,i)};yield new Promise(e=>setTimeout(e,1500))}return null})}chatRequest(e){return S(this,arguments,void 0,function*(e,t={},i={}){e||this.emit("chaterror",{successed:!1,error:"no message in chat"}),"string"==typeof e&&(e=[{role:r.User,content_type:"text",content:e}]);const s=yield this.createClient(),n=this.debug?m(_(this.debugDir,`sync_${this.formatDateWithMs()}.log`),{flags:"a"}):null,o=yield this.getRequestStream(s,e,t);n&&(n.write(`callChatOption:${JSON.stringify(t)}\n`),n.write(`parameters:${JSON.stringify(o)}\n`));const a=this.botid?yield s.chat.create(o,i):yield s.workflows.runs.create(o,i);if(this.workflowid){const e=a;if(o.is_async)return{successed:!0,workflow_id:this.workflowid,execute_id:e.execute_id};if("Success"===e.msg||!e.msg&&e.data){const t=e.data,i=e.usage;return n&&n.end(t),{successed:!0,message:[{role:"assistant",type:"answer",content:t,usage:i}]}}n&&n.end("workflow run failed"),console.error("workflow run failed",e.msg,e)}if(!this.workflowid&&a.conversation_id&&a.id){const e=a,t=yield this.getChatDetail(s,e.conversation_id,e.id);if(t){return{successed:!0,message:t.messages.filter(e=>"answer"===e.type).map(e=>({role:e.role,type:e.type,content:e.content})),usage:t.usage,session_id:e.conversation_id}}}return n&&n.end("聊天未完成"),{successed:!1,error:{message:"聊天未完成"}}})}extractXmlContent(e,t){const i=new RegExp(`<\\s*${t}\\s*>([\\s\\S]*?)<\\s*\\/\\s*${t}\\s*>`,"i"),s=e.match(i);return s&&s[1]?s[1].trim():""}parseDeepThinkingJson(e){const t=[e.indexOf(x.thinking.start),e.indexOf(x.reasoning.start),e.indexOf(x.card.start),e.indexOf(x.preresponse.start)];let i=1e4,s=-1;t.forEach((e,t)=>{e>=0&&e<i&&(i=e,s=t)});const n=s>=0?i:-1;if(n<0)return{content:e,status:D.ThinkingOver};const r=[x.thinking,x.reasoning,x.card,x.preresponse][s],o=e.indexOf(r.end,n),a=o>=0?o:e.indexOf("</",n),c=this.extractXmlContent(e.substring(n,a>=0?a:void 0)+r.end,r.tag);return o>=0&&(e=e.substring(o+r.end.length)),{thinking:{action:r,text:c,completed:o>=0},content:e}}formatDateWithMs(e=new Date){return`${e.getFullYear()}${String(e.getMonth()+1).padStart(2,"0")}${String(e.getDate()).padStart(2,"0")}${String(e.getHours()).padStart(2,"0")}${String(e.getMinutes()).padStart(2,"0")}${String(e.getSeconds()).padStart(2,"0")}${String(e.getMilliseconds()).padStart(3,"0")}`}chatRequestInStream(e){return S(this,arguments,void 0,function*(e,t={},i,s){var n,c,u,d,l,p,g;e||this.emit("chaterror",{successed:!1,error:"no message in chat"}),"string"==typeof e&&(e=[{role:r.User,content_type:"text",content:e}]);const f=yield this.createClient();let T=Math.ceil(Math.random()*((new Date).getTime()*Math.random())/1e3),y=0;const E=this.debug?m(_(this.debugDir,`${this.formatDateWithMs()}.log`),{flags:"a"}):null,v=yield this.getRequestStream(f,e,t);E&&(E.write(`callChatOption:${JSON.stringify(t)}\n`),E.write(`attach:${i?JSON.stringify(i):"null"}\n`),E.write(`parameters:${JSON.stringify(v)}\n`));const C=this.botid?f.chat.stream(v):this.workflowid?f.workflows.runs.stream(v,s):f.workflows.chat.stream(v,s);let O=[],A=[],S=[],b=[],N="",I=D.None,R="",w="";try{for(var P,U=!0,V=k(C);!(n=(P=yield V.next()).done);U=!0){d=P.value,U=!1;const e=d;if(e.event===o.ERROR)return E&&E.write("chat error:"),this.emit("chaterror",{successed:!1,error:"call failed"});if(e.event===o.CONVERSATION_MESSAGE_DELTA||e.event===a.MESSAGE){let{conversation_id:t,content_type:s,type:n="answer",role:r="assistant",content:o,reasoning_content:a}=e.data;if(E&&E.write(o),"Default"===o)continue;if(!o&&a){this.emit("chatthinking",{text:a,completed:!1,action:"deep-thinking"});continue}if(I!==D.Thinking&&o.startsWith("<xiaolu-")&&(I=D.Thinking),I===D.Thinking){N+=o;const e=this.parseDeepThinkingJson(N),a=e.thinking;if(a)if(N=e.content,a.action===x.card){if(R+=e.thinking.text,e.thinking.completed){const e=R.replace(/[\x00-\x1F\x7F]/g,"").split("|");R="";for(const t of e){const e=h(t);if(e.type&&e.tag&&"string"==typeof e.tag)if("follow_up"===e.type)A.push(e.tag);else{const t=e.tag.split(",");for(const i of t)b.push({type:e.type,tag:i})}}this.emit("chatcard",b)}}else if(a.action===x.preresponse){const o=e.thinking.text.slice(w.length);w=e.thinking.text,O.push(o);let a={successed:!0,type:n,content_type:s,role:r,requestid:T,segment:o,text:O.join(""),index:y++,session_id:t,preresponse:!0,preresponseover:e.thinking.completed};i&&(a=Object.assign({},a,i)),this.emit("chattext",a)}else this.emit("chatthinking",{text:e.thinking.text,completed:e.thinking.completed,action:null===(l=a.action)||void 0===l?void 0:l.tag});if(e.status!=D.ThinkingOver)continue;I=D.ThinkingOver,o=N}O.push(o);let c={successed:!0,type:n,content_type:s,role:r,requestid:T,segment:o,text:O.join(""),index:y++,session_id:t};i&&(c=Object.assign({},c,i)),this.emit("chattext",c)}if(e.event===o.CONVERSATION_MESSAGE_COMPLETED){const{type:t,content:i}=null!==(p=e.data)&&void 0!==p?p:{};"follow_up"===t&&(E&&E.write("followup:"+i),S.push(i))}if(e.event===o.CONVERSATION_CHAT_COMPLETED||e.event===a.DONE){const{conversation_id:t,content:s,usage:n}=null!==(g=e.data)&&void 0!==g?g:{};let o={successed:!0,usage:n,cards:b.length?b:null,cardfollowup:A.filter(e=>"string"==typeof e&&e.length>0),followup:S,type:"answer",content_type:"text",role:r.Assistant,requestid:T,segment:null,text:null!=s?s:O.join(""),index:y++,session_id:t};i&&(o=Object.assign({},o,i)),E&&E.end("chat completed"),this.emit("chatdone",o)}}}catch(e){c={error:e}}finally{try{U||n||!(u=V.return)||(yield u.call(V))}finally{if(c)throw c.error}}return{successed:!0,requestid:T}})}}class U extends P{constructor(){super(...arguments),this.socket=null,this.traceId=y(),this.chatVoiceBuffer=[],this.chatContent=[],this.serverVad=!0,this.combineTTS=!0,this.currentAgentStatus=0,this.waitingWarmVoice=[],this.waitingInterval=10,this.willSendWarmVoice=!1,this.maxWaitingTimes=0,this.waitingTimes=0,this.waitingTimer=null}getChatVoiceFile(){return this.chatVoiceBuffer.length?Buffer.concat([...this.chatVoiceBuffer]):Buffer.alloc(0)}clearWaitingTimer(){this.waitingTimer&&clearTimeout(this.waitingTimer),this.waitingTimer=null}startWaitingUserSpeaking(e=0){this.willSendWarmVoice&&(console.log(`第${this.waitingTimes+1}等待用户说话,等待${this.waitingInterval+e} 秒`),this.clearWaitingTimer(),this.waitingTimer=setTimeout(()=>{const e=this.waitingWarmVoice.length,t=this.waitingWarmVoice[this.waitingTimes++%e]+"";this.submitWarmVoice(t),this.maxWaitingTimes>0&&this.waitingTimes>=this.maxWaitingTimes?setTimeout(()=>this.emit("chat.end",{reason:"wait_timeout"}),t.length/4*1e3):this.startWaitingUserSpeaking()},1e3*(this.waitingInterval+e)))}getChatContent(){return this.chatContent}submitActionResult(e){const t={id:this.traceId,event_type:c.CONVERSATION_CHAT_SUBMIT_TOOL_OUTPUTS,data:e};this.sendCommand(t)}submitWarmVoice(e){if(!e)return;console.log("发送暖场语音:",e);const t={id:this.traceId,event_type:"input_text.generate_audio",data:{mode:"text",text:e}};this.sendCommand(t)}sendVoice(e,t){if(!e)return;t=t||e;const i=this.serverVad?t.toString("base64"):e.toString("base64"),s={id:this.traceId,event_type:c.INPUT_AUDIO_BUFFER_APPEND,data:{delta:i}};this.sendCommand(s),this.chatVoiceBuffer.push(e)}sendChatMessage(e,t=r.User){const i={id:this.traceId,event_type:c.CONVERSATION_MESSAGE_CREATE,data:{role:t,content_type:"text",content:e}};this.sendCommand(i)}close(){this.breakAnswer(),this.currentAgentStatus=0,this.socket&&this.socket.close()}submitVoiceBuffer(){const e={id:this.traceId,event_type:c.INPUT_AUDIO_BUFFER_COMPLETE};this.sendCommand(e)}clearVoiceBuffer(){const e={id:this.traceId,event_type:c.INPUT_AUDIO_BUFFER_CLEAR};this.sendCommand(e)}breakAnswer(){if(1===this.currentAgentStatus){const e={id:this.traceId,event_type:c.CONVERSATION_CHAT_CANCEL};this.sendCommand(e)}}startVoiceChat(){return S(this,arguments,void 0,function*(e={}){if(!this.botid)return{successed:!1,code:-1,error:"语音对话仅支持智能体模式"};if(!e.voiceId)return{successed:!1,code:-2,error:"请提供语音音色"};const t=yield this.authorizationProvider.getAccessToken();if(!t)return{successed:!1,error:"获取token失败"};const i=`${u}/v1/chat?bot_id=${this.botid}&authorization=Bearer ${t}`;this.socket=new T(i);let s=!0;return this.socket.on("open",()=>console.log("Connected to bot websocket ")),this.socket.on("message",t=>{const i=JSON.parse(t.toString());switch(i.event_type){case c.CHAT_CREATED:this.ChatCreated(e);break;case c.CHAT_UPDATED:this.emit(c.CHAT_UPDATED,i);break;case c.CONVERSATION_CHAT_CREATED:this.currentAgentStatus=1,this.emit(c.CONVERSATION_CHAT_CREATED,i);break;case c.CONVERSATION_CHAT_IN_PROGRESS:this.emit(c.CONVERSATION_CHAT_IN_PROGRESS,i);break;case c.CONVERSATION_MESSAGE_DELTA:this.emit(c.CONVERSATION_MESSAGE_DELTA,i);break;case c.CONVERSATION_AUDIO_SENTENCE_START:if(s&&e.playPrologue){this.chatContent.push({role:r.Assistant,content:i.data.text});const e=i.data.text.length;this.startWaitingUserSpeaking(e/4),s=!1}this.emit(c.CONVERSATION_AUDIO_SENTENCE_START,i);break;case c.CONVERSATION_AUDIO_DELTA:this.emit(c.CONVERSATION_AUDIO_DELTA,i),this.combineTTS&&i.data.content&&this.chatVoiceBuffer.push(Buffer.from(i.data.content,"base64"));break;case c.CONVERSATION_MESSAGE_COMPLETED:if(this.emit(c.CONVERSATION_MESSAGE_COMPLETED,i),["question","answer"].includes(i.data.type)&&(this.chatContent.push({role:i.data.role,content:i.data.content}),"answer"===i.data.type)){const e=i.data.content.length;this.startWaitingUserSpeaking(e/4)}break;case c.CONVERSATION_AUDIO_COMPLETED:this.emit(c.CONVERSATION_AUDIO_COMPLETED,i);break;case c.CONVERSATION_CHAT_COMPLETED:this.currentAgentStatus=0,this.emit(c.CONVERSATION_CHAT_COMPLETED,i);break;case c.CONVERSATION_CHAT_CANCELED:this.currentAgentStatus=0,this.emit(c.CONVERSATION_CHAT_CANCELED,i),this.startWaitingUserSpeaking();break;case c.CONVERSATION_CHAT_FAILED:this.currentAgentStatus=0,this.emit(c.CONVERSATION_CHAT_FAILED,i);break;case c.INPUT_AUDIO_BUFFER_COMPLETED:this.emit(c.INPUT_AUDIO_BUFFER_COMPLETED,i);break;case c.INPUT_AUDIO_BUFFER_CLEAR:this.emit(c.INPUT_AUDIO_BUFFER_CLEAR,i);break;case c.ERROR:this.emit(c.ERROR,i);break;case c.INPUT_AUDIO_BUFFER_SPEECH_STARTED:this.emit(c.INPUT_AUDIO_BUFFER_SPEECH_STARTED,i);break;case c.INPUT_AUDIO_BUFFER_SPEECH_STOPPED:this.emit(c.INPUT_AUDIO_BUFFER_SPEECH_STOPPED,i);break;case c.CONVERSATION_AUDIO_TRANSCRIPT_UPDATE:this.emit(c.CONVERSATION_AUDIO_TRANSCRIPT_UPDATE,i),this.waitingTimer&&this.clearWaitingTimer();break;case c.CONVERSATION_AUDIO_TRANSCRIPT_COMPLETED:this.emit(c.CONVERSATION_AUDIO_TRANSCRIPT_COMPLETED,i);break;case c.CONVERSATION_CHAT_REQUIRES_ACTION:this.emit(c.CONVERSATION_CHAT_REQUIRES_ACTION,i)}}),{successed:!0}})}ChatCreated(e={}){this.emit(c.CHAT_CREATED),this.serverVad=null==e.serverVad||null==e.serverVad||e.serverVad,this.combineTTS=!this.serverVad||this.serverVad&&(e.combinetts||!1),this.waitingWarmVoice=e.waitingWarmVoice?"string"==typeof e.waitingWarmVoice?e.waitingWarmVoice.split("|"):Array.isArray(e.waitingWarmVoice)?e.waitingWarmVoice:[]:[],this.willSendWarmVoice=this.waitingWarmVoice.length>0,this.maxWaitingTimes=e.maxWaitingTimes||0,this.waitingInterval=e.waitingInterval||10;const t={id:this.traceId,event_type:c.CHAT_UPDATE,data:{chat_config:{auto_save_history:!0,conversation_id:e.conversationId,user_id:e.userid||e.cozeUserID,meta_data:e.meta_data||{},custom_variables:e.customVariables||{},extra_params:{},parameters:e.parameters},input_audio:{format:"pcm",codec:"pcm",sample_rate:e.sample_rate||16e3},output_audio:{codec:"pcm",pcm_config:{sample_rate:e.sample_rate||16e3,frame_size_ms:e.output_frame_size_ms||100},speech_rate:e.speed||0,voice_id:e.voiceId},turn_detection:{type:this.serverVad?"server_vad":"client_interrupt",silence_duration_ms:e.vadSilence||1e3},asr_config:{context:e.asrContext,user_language:e.asrLanguage||"common",hot_words:e.hotWords,enable_ddc:null==e.enable_ddc||null==e.enable_ddc||e.enable_ddc,enable_itn:null==e.enable_itn||null==e.enable_itn||e.enable_itn,enable_nostream:!1},need_play_prologue:e.playPrologue||!1,prologue_content:e.playPrologue&&e.prologueContent||null}};this.sendCommand(t)}sendCommand(e){this.socket&&this.socket.send("string"==typeof e?e:JSON.stringify(e))}}const V=["[AUTHORIZATION NEEDED]","[AUTHORIZATION ERROR]","[BODY ERROR]","[REQUEST ERROR]"];class B extends w{constructor(e,t,i={}){super(e,i),this.proxySetting=t}chatRequest(e,t,i){return S(this,void 0,void 0,function*(){const s=Object.assign({headers:{"Content-Type":"application/json",authorization:`Bearer ${this.apiKey}`},method:"post",url:this.proxySetting.serviceurl,data:{chatText:e,option:t}},i);return(yield E(s)).data})}chatRequestInStream(e,t,i,s){return S(this,void 0,void 0,function*(){let n,r=Math.ceil(Math.random()*((new Date).getTime()*Math.random())/1e3);const o={headers:{"Content-Type":"application/json",authorization:`Bearer ${this.apiKey}`},method:"post",url:this.proxySetting.serviceurl+"/stream",data:{messages:e,option:t,axiosOption:s},responseType:"stream"};let a="";E(o).then(e=>{e.data.on("data",e=>{if(n=e.toString(),n){if(V.includes(n))return this.emit("requesterror",{successed:!1,requestid:r,error:"Request Remote OpenAI Error : "+n});const e=(a+n).split("*&$");a="";for(const t of e)if(t)try{if(!t.endsWith("}")){a=t;break}const e=Object.assign(JSON.parse(t),i);this.emit(e.finish_reason?"chatdone":"chattext",e)}catch(e){break}}})}).catch(e=>{this.emit("requesterror",{successed:!1,requestid:r,error:"Axios Error : "+e})})})}}class M extends R{constructor(e,t,i={}){super(e,i),this.azureSetting=t,this.azureSetting.endpoint.toLowerCase().startsWith("https://")||(this.azureSetting.endpoint="https://"+this.azureSetting.endpoint)}createOpenAI(e){return new v(this.azureSetting.endpoint,new C(e))}get EmbeddingUrl(){return`${this.azureSetting.endpoint}/openai/deployments/${this.embeddingmodel||"openai-embedding-ada-002"}/embeddings?api-version=2022-12-01`}getTextEmbedding(e){return S(this,arguments,void 0,function*(e,t={}){if(!e)return{successed:!1,error:{errcode:2,errmsg:"content required"}};this.aiApi||(this.aiApi=this.createOpenAI(this.apiKey));try{return{successed:!0,embedding:(yield this.aiApi.getEmbeddings(this.embeddingmodel||"openai-embedding-ada-002","string"==typeof e?[e]:e,t)).data}}catch(e){return{successed:!1,error:e}}})}chatRequest(e,t){return S(this,arguments,void 0,function*(e,t,i={}){if(!e)return{successed:!1,error:{errcode:2,errmsg:"缺失聊天的内容"}};this.aiApi||(this.aiApi=this.createOpenAI(this.apiKey));let s="string"==typeof e?[{role:"user",content:e}]:e;try{const e=yield this.aiApi.getChatCompletions((null==t?void 0:t.model)||this.chatModel,s,{temperature:Number((null==t?void 0:t.temperature)||this.temperature),maxTokens:Number((null==t?void 0:t.maxtoken)||this.maxtoken),topP:Number((null==t?void 0:t.top_p)||this.top_p),presencePenalty:Number((null==t?void 0:t.presence_penalty)||this.presence_penalty),frequencyPenalty:Number((null==t?void 0:t.frequency_penalty)||this.frequency_penalty),n:Number((null==t?void 0:t.replyCounts)||1)||1,tools:1===(null==t?void 0:t.enableToolCall)&&(null==t?void 0:t.tools)?t.tools:void 0,toolChoice:1===(null==t?void 0:t.enableToolCall)?"auto":void 0}),{promptTokens:i,completionTokens:n,totalTokens:r}=e.usage;let o=[];for(const t of e.choices){const{index:e,finishReason:i,message:s}=t;o.push({index:e,finish_reason:i,message:s})}return{successed:!0,message:o,usage:{prompt_tokens:i,completion_tokens:n,total_tokens:r}}}catch(e){return console.log("result is error ",e),{successed:!1,error:e}}})}chatRequestInStream(e,t,i,s){return S(this,void 0,void 0,function*(){var n,r,o,a,c,u,d;e||this.emit("chaterror",{successed:!1,error:"no text in chat"}),this.aiApi||(this.aiApi=this.createOpenAI(this.apiKey));let h="string"==typeof e?[{role:"user",content:e}]:e;s=Object.assign({},s||{timeout:6e4});let l,p=Math.ceil(Math.random()*((new Date).getTime()*Math.random())/1e3),m=0,g=-1,_=[];try{const e=yield this.aiApi.streamChatCompletions((null==t?void 0:t.model)||this.chatModel,h,{temperature:Number((null==t?void 0:t.temperature)||this.temperature),maxTokens:Number((null==t?void 0:t.maxtoken)||this.maxtoken),topP:Number((null==t?void 0:t.top_p)||this.top_p),presencePenalty:Number((null==t?void 0:t.presence_penalty)||this.presence_penalty),frequencyPenalty:Number((null==t?void 0:t.frequency_penalty)||this.frequency_penalty),n:Number((null==t?void 0:t.replyCounts)||1)||1,tools:1===(null==t?void 0:t.enableToolCall)&&(null==t?void 0:t.tools)?t.tools:void 0,toolChoice:1===(null==t?void 0:t.enableToolCall)?"auto":void 0});let s=[];try{for(var f,T=!0,y=k(e);!(n=(f=yield y.next()).done);T=!0){a=f.value,T=!1;const e=a;for(const t of e.choices){const{finishReason:e,index:n}=t,r=null===(c=t.delta)||void 0===c?void 0:c.toolCalls;if(r&&r.length)l=r[0].index,m=1,l!==g?(_.push({id:r[0].id,type:"function",function:{name:r[0].function.name,arguments:r[0].function.arguments}}),g=l):_[g].function.arguments+=r[0].function.arguments;else{const e=null===(u=t.delta)||void 0===u?void 0:u.content;s.push(e)}let o={successed:!0,requestid:p,segment:null===(d=t.delta)||void 0===d?void 0:d.content,text:s.join(""),finish_reason:e,index:n,has_tool_calls:m,tool_calls:_};i&&(o=Object.assign({},o,i)),this.emit(e?"chatdone":"chattext",o)}}}catch(e){r={error:e}}finally{try{T||n||!(o=y.return)||(yield o.call(y))}finally{if(r)throw r.error}}return{successed:!0,requestid:p}}catch(e){return this.emit("requesterror",{successed:!1,requestid:p,error:"call axios faied "+e}),{successed:!1,requestid:p}}})}}function q(){return S(this,arguments,void 0,function*(e={}){e.data||(e.data=e.body);try{return{successed:!0,data:(yield E(e)).data}}catch(e){return{successed:!1,error:e,data:e}}})}function L(e){return new Promise(t=>setTimeout(t,e))}class F extends N{constructor(e,t){super(),this.apikey=e,this.agent=t}chatRequest(e,t){return S(this,arguments,void 0,function*(e,t,i={}){var s;if(!e)return{successed:!1,error:"no text in chat"};const n="object"==typeof e?null!==(s=e.text)&&void 0!==s?s:e.content:e;i=Object.assign({},i||{timeout:6e4});const r=Object.assign({headers:{"Content-Type":"application/json",authorization:`Bearer ${this.apikey}`},method:"post",url:`${this.agent.endpoint}/api/v1/agents/${this.agent.agentid}/completions`,data:{question:n,session_id:t.session_id,optional:t.optional,stream:!1}},i),o=yield q(r);if(!o.successed||o.data.code)return{successed:!1,error:"failed"};const{answer:a,session_id:c}=o.data.data;return{successed:!0,message:[{message:{content:a}}],session_id:c}})}chatRequestInStream(e,t,i,s){return S(this,void 0,void 0,function*(){var n,r,o,a,c;e||this.emit("chaterror",{successed:!1,error:"no text in chat"});const u="object"==typeof e?null!==(c=e.text)&&void 0!==c?c:e.content:e;s=Object.assign({},s||{timeout:6e4});let d=Math.ceil(Math.random()*((new Date).getTime()*Math.random())/1e3);try{const e=Object.assign({headers:{"Content-Type":"application/json",authorization:`Bearer ${this.apikey}`},method:"post",url:`${this.agent.endpoint}/api/v1/agents/${this.agent.agentid}/completions`,data:{question:u,session_id:t.session_id,stream:!0,optional:t.optional},responseType:"stream"},s),c=yield E(e),m=O.from(c.data);let g,_=0,f="",T=[],y=[];try{for(var h,l=!0,p=k(m);!(n=(h=yield p.next()).done);l=!0){a=h.value,l=!1;const e=a;let t=null,s="";try{s=e.toString().split("data:"),s.length&&(s=s[s.length-1]+""),t=JSON.parse(T.join("")+s)}catch(e){T.push(s);continue}T=[];const{answer:n,running_status:r,reference:o}=t.data;if(!0===r)continue;const c=n?n.replace(f,""):"";f=null!=n?n:f,g||(g=t.data.session_id),o&&o.chunks&&o.chunks.length&&(y=y.concat(o.chunks));const u=!0===t.data;let p={successed:!0,requestid:d,segment:c,chunks:y,text:f,finish_reason:u?"stop":null,index:_++,session_id:g};i&&(p=Object.assign({},p,i)),this.emit(u?"chatdone":"chattext",p)}}catch(e){r={error:e}}finally{try{l||n||!(o=p.return)||(yield o.call(p))}finally{if(r)throw r.error}}return{successed:!0,requestid:d}}catch(e){}})}}class j extends N{constructor(e,t={}){super(),this.apiOption={},this.apiKey=e,this.apiOption=t}getTextEmbedding(e){return S(this,arguments,void 0,function*(e,t={}){if(!e)return{successed:!1,error:{errcode:2,errmsg:"content required"}};if(Array.isArray(e)&&0===e.length)return{successed:!1,error:{errcode:2,errmsg:"content required"}};const i=Array.isArray(e)?e:[e],s={method:"post",headers:{"Content-Type":"application/json",authorization:`Bearer ${this.apiKey}`},data:{model:t.model||this.apiOption.embedding,input:i},url:"https://ark.cn-beijing.volces.com/api/v3/embeddings"};try{return{successed:!0,embedding:(yield q(s)).data}}catch(e){return{successed:!1,error:e}}})}chatRequest(e,t){return S(this,arguments,void 0,function*(e,t,i={}){if(!e)return{successed:!1,error:{errcode:2,errmsg:"缺失聊天的内容"}};const s=this.assembleApiParams(e,!1,t,i);try{const e=yield q(s);return e.successed&&!e.data.code?{successed:!0,message:e.data.choices,usage:e.data.usage}:Object.assign({successed:!1},e.data)}catch(e){return console.log("result is error ",e),{successed:!1,error:e}}})}assembleApiParams(e,t=!1,i,s={}){let n="string"==typeof e?[{role:"user",content:e}]:e,r={};((null==i?void 0:i.temperature)||this.apiOption.temperature)&&(r.temperature=Number((null==i?void 0:i.temperature)||this.apiOption.temperature)),r.max_tokens=Number((null==i?void 0:i.maxtoken)||this.apiOption.maxtoken),((null==i?void 0:i.top_p)||this.apiOption.top_p)&&(r.top_p=Number((null==i?void 0:i.top_p)||this.apiOption.top_p)),((null==i?void 0:i.presence_penalty)||this.apiOption.presence_penalty)&&(r.presence_penalty=Number((null==i?void 0:i.presence_penalty)||this.apiOption.presence_penalty)),((null==i?void 0:i.frequency_penalty)||this.apiOption.frequency_penalty)&&(r.frequency_penalty=Number((null==i?void 0:i.frequency_penalty)||this.apiOption.frequency_penalty)),((null==i?void 0:i.top_logprobs)||this.apiOption.top_logprobs)&&(r.logprobs=!0,r.top_logprobs=Number((null==i?void 0:i.top_logprobs)||this.apiOption.top_logprobs)),r.tools=1===(null==i?void 0:i.enableToolCall)&&(null==i?void 0:i.tools)?i.tools:void 0,r.tool_choice=1===(null==i?void 0:i.enableToolCall)?"auto":void 0;const o=Object.assign(Object.assign({},s),{method:"post",headers:{"Content-Type":"application/json",authorization:`Bearer ${this.apiKey}`},data:Object.assign(Object.assign({model:(null==i?void 0:i.model)||this.apiOption.model},r),{messages:n,stream:t}),url:"https://ark.cn-beijing.volces.com/api/v3/chat/completions"});return t&&(o.responseType="stream"),o}chatRequestInStream(e,t,i,s){return S(this,void 0,void 0,function*(){e||this.emit("chaterror",{successed:!1,error:"no text in chat"}),s=Object.assign({},s||{timeout:1e4});const n=this.assembleApiParams(e,!0,t,s);let r=Math.ceil(Math.random()*((new Date).getTime()*Math.random())/1e3),o=[];try{return function(e={},t){e.data||(e.data=e.body),E(e).then(e=>{e.data.on("data",e=>{t(e)})})}(n,e=>{let t=e.toString().replace("[DONE]","").replace(/[\r\n]+/gm,"");this.processChunkData(t.split(/data: /),r,o,i)}),{successed:!0,requestid:r}}catch(e){return this.emit("requesterror",{successed:!1,requestid:r,error:"call axios faied "+e}),{successed:!1,requestid:r}}})}processChunkData(e,t,i,s){let n,r=0,o=-1,a=[];for(const c of e){if(!c)continue;const e=JSON.parse(c),[u]=e.choices,{finish_reason:d,index:h,usage:l}=u,{content:p,tool_calls:m}=u.delta;m&&m.length?(n=m[0].index,r=1,n!==o?(a.push({id:m[0].id,type:"function",function:{name:m[0].function.name,arguments:m[0].function.arguments}}),o=n):a[o].function.arguments+=m[0].function.arguments):i.push(p);let g={successed:!0,requestid:t,segment:p,text:i.join(""),finish_reason:d,index:h,usage:l,has_tool_calls:r,tool_calls:a};s&&(g=Object.assign({},g,s)),this.emit(d?"chatdone":"chattext",g)}}}class W{constructor(e,t){this.appid=e,this.expiresAt=0,this.secret=t.keyid,this.pemfile=t.pemfile||"private.pem",this.sessionName=t.sessionName||"default"}getAccessToken(){return S(this,void 0,void 0,function*(){if(this.accessToken&&Date.now()<this.expiresAt)return this.accessToken;const e=yield this.doAuthorize();return e.successed?(this.accessToken=e.accessToken,this.expiresAt=1e3*e.expires_in,this.accessToken):null})}doAuthorize(){return S(this,void 0,void 0,function*(){try{const e="api.coze.cn",t=g.join(process.cwd(),this.pemfile);if(!l.existsSync(t))return{successed:!1,error:`${t} not existed`};const i=l.readFileSync(t).toString(),n=yield d({baseURL:s,appId:this.appid,aud:e,keyid:this.secret,privateKey:i,sessionName:this.sessionName});return{successed:!0,accessToken:n.access_token,expires_in:n.expires_in}}catch(e){return{successed:!0,error:e}}})}}const $=new Map,z={OPENAI:"openai",OPENAIPROXY:"openaiproxy",MICROSOFT:"microsoft",AIMP:"aimp",COZE:"coze",DOUBAO:"doubao"};var H=Object.freeze({__proto__:null,GptProviderEnum:z,createGpt:function(e,t,i){let{model:s,agentid:n,maxtoken:r,temperature:o,serviceurl:a,endpoint:c,engine:u,version:d,embedding:h,top_p:l,presence_penalty:p,frequency_penalty:m}=i||{};switch(e){case z.OPENAI:return new w(t+"",{model:s,maxtoken:r,temperature:o,embedding:h,top_p:l,presence_penalty:p,frequency_penalty:m});case z.OPENAIPROXY:return new B(t+"",{serviceurl:a},{model:s,maxtoken:r,temperature:o,embedding:h,top_p:l,presence_penalty:p,frequency_penalty:m});case z.MICROSOFT:return new M(t+"",{endpoint:c,engine:u,version:d},{model:s,maxtoken:r,temperature:o,embedding:h,top_p:l,presence_penalty:p,frequency_penalty:m});case z.AIMP:return new F(t+"",{endpoint:c,agentid:n});case z.COZE:let e=$.get(t+"");return e||(e=new W(t+"",i),$.set(t+"",e)),!0===i.voicebot?new U(e,i):new P(e,i);case z.DOUBAO:return new j(t+"",{model:s,maxtoken:r,temperature:o,top_p:l,presence_penalty:p,frequency_penalty:m,embedding:h});default:return null}}});const G=["。","!","!","?","?",";",";",":",":","\n"];class K extends A{constructor(e,t){super(),this.agentBot=e,this.chatconfig=t,this.asrSocket=null,this.ttsSocket=null,this.chatVoiceBuffer=[],this.chatContent=[],this.apiToken=null,this.waitingTimes=0,this.waitingTimer=null,this.traceId=y(),this.userTalkBuffer=[],this.userTalkStart=0,this.agentTalkBuffer=[],this.agentTalkStart=0,this.currentTTSConent=[],this.lastGetAsrChangedTime=0,this.breaked=!1,this.connectors={asr:!1,tts:!1}}get AgentBot(){return this.agentBot}get ChatConfig(){return this.chatconfig}getChatVoiceFile(){return this.chatVoiceBuffer.length?Buffer.concat([...this.chatVoiceBuffer]):Buffer.alloc(0)}clearWaitingTimer(){this.waitingTimer&&clearTimeout(this.waitingTimer),this.waitingTimer=null}startWaitingUserSpeaking(e=0){this.chatconfig.timeout_config&&this.chatconfig.timeout_config.interval&&this.chatconfig.timeout_config.messages&&this.chatconfig.timeout_config.messages.length&&(console.log(`第${this.waitingTimes+1}等待用户说话,等待${this.chatconfig.timeout_config.interval+e} 秒`),this.clearWaitingTimer(),this.waitingTimer=setTimeout(()=>{const e=this.chatconfig.timeout_config.messages.length,t=this.chatconfig.timeout_config.messages[this.waitingTimes++%e]+"";this.createTtsConnector(t),this.chatconfig.timeout_config.maxtimes&&this.waitingTimes>=this.chatconfig.timeout_config.maxtimes?setTimeout(()=>this.emit("voicechat.end",{reason:"wait_timeout"}),t.length/4*1e3):this.startWaitingUserSpeaking()},1e3*(this.chatconfig.timeout_config.interval+e)))}getChatContent(){return this.chatContent}sendText2Tts(e,t=!1){if(e){const t={id:this.traceId,event_type:c.INPUT_TEXT_BUFFER_APPEND,data:{delta:e}};this.sendCommand("tts",t),this.currentTTSConent.push(e),this.emit(c.CONVERSATION_AUDIO_SENTENCE_START,{event_type:c.CONVERSATION_AUDIO_SENTENCE_START,data:{text:this.currentTTSConent.join("")}})}if(t){const e={id:this.traceId,event_type:c.INPUT_TEXT_BUFFER_COMPLETE};this.sendCommand("tts",e)}}sendVoice(e){var t;if(!e)return;this.userTalkStart||(this.userTalkStart=(new Date).getTime());const i=e.toString("base64"),s={id:this.traceId,event_type:c.INPUT_AUDIO_BUFFER_APPEND,data:{delta:i}};this.sendCommand("asr",s),this.userTalkBuffer.push(e),this.lastGetAsrChangedTime&&(new Date).getTime()-this.lastGetAsrChangedTime>((null===(t=this.chatconfig.vad_config)||void 0===t?void 0:t.silence_duration_ms)||500)&&(this.lastGetAsrChangedTime=0,this.emit(c.INPUT_AUDIO_BUFFER_SPEECH_STOPPED,{event_type:c.INPUT_AUDIO_BUFFER_SPEECH_STOPPED}),this.submitVoiceBuffer())}sendChatMessage(e,t={}){this.agentBot.chatRequestInStream(e,t)}submitVoiceBuffer(){const e={id:this.traceId,event_type:c.INPUT_AUDIO_BUFFER_COMPLETE};this.sendCommand("asr",e)}clearVoiceBuffer(){const e={id:this.traceId,event_type:c.INPUT_AUDIO_BUFFER_CLEAR};this.sendCommand("asr",e)}breakAnswer(e=!1){this.breaked=!0;const t={id:this.traceId,event_type:c.INPUT_TEXT_BUFFER_COMPLETE};this.sendCommand("tts",t),this.emit(c.CONVERSATION_CHAT_CANCELED),e||this.startWaitingUserSpeaking()}configurationConnector(e,t){"tts"===e&&t&&this.chatconfig.emotionSupport&&(t.emotionContext&&(this.chatconfig.output_audio_config.context_texts=t.emotionContext),this.chatconfig.output_audio_config.emotion_config={emotion:t.emotion,emotion_scale:t.emotionScale||4});const i="tts"===e?{id:this.traceId,event_type:c.SPEECH_UPDATE,data:{output_audio:this.chatconfig.output_audio_config}}:{id:this.traceId,event_type:c.TRANSCRIPTIONS_UPDATE,data:{input_audio:this.chatconfig.input_audio_config,asr_config:this.chatconfig.asr_config}};this.sendCommand(e,i)}decorateUserMessage(e){return S(this,void 0,void 0,function*(){const t={question:e,answerByAgent:!0,chatparams:this.chatconfig.chat_config};if(this.chatconfig.emotionSupport){const e=yield this.getEmotion(this.chatContent);t.emotionInfo=e}return t})}getEmotion(e){return S(this,void 0,void 0,function*(){return{emotion:"neutral",emotionScale:4}})}getLastPunctuationInfo(e){if(!e||e.length<=5)return{index:-1};const t=new Set(["。","!","!","?","?",";",";",":",":","\n"]);for(let i=e.length-1;i>=5;i--){const s=e[i];if(t.has(s))return{index:i,text:e.slice(0,i+1),remain:e.slice(i+1)}}return{index:-1}}registerAgentEvent(){return S(this,void 0,void 0,function*(){let e=[];this.agentBot&&(this.agentBot.on("chattext",t=>{if(this.breaked||!t.segment)return;e.push(t.segment);const i=this.getLastPunctuationInfo(e.join(""));i.index>0&&(this.sendText2Tts(i.text,!1),e=i.remain?[i.remain]:[])}),this.agentBot.on("chatdone",t=>this.sendText2Tts(e.join(""),!0)))})}processAsrResult(e){return S(this,arguments,void 0,function*(e,t=!1){return e})}processChatResult(e,t,i,s){return S(this,arguments,void 0,function*(e,t,i,s,n="pcm"){const o=(new Date).getTime();return{role:e,content:t,start:s,end:o,duration:(o-s)/1e3,type:e===r.User?"question":"answer"}})}registerAsrEvent(e){return S(this,void 0,void 0,function*(){let t="";e.on("open",()=>console.log("Connected to asr server.")),e.on("message",e=>S(this,void 0,void 0,function*(){const i=JSON.parse(e.toString());switch(i.event_type){case c.TRANSCRIPTIONS_CREATED:this.configurationConnector("asr"),this.emit(c.TRANSCRIPTIONS_CREATED,i);break;case c.TRANSCRIPTIONS_UPDATED:this.connectors.asr=!0;break;case c.INPUT_AUDIO_BUFFER_COMPLETED:this.lastGetAsrChangedTime=0,this.emit(c.INPUT_AUDIO_BUFFER_COMPLETED,i);break;case c.TRANSCRIPTIONS_MESSAGE_UPDATE:this.lastGetAsrChangedTime=(new Date).getTime(),t=yield this.processAsrResult(i.data.content),this.emit(c.TRANSCRIPTIONS_MESSAGE_UPDATE,{content:t});break;case c.TRANSCRIPTIONS_MESSAGE_COMPLETED:t=yield this.processAsrResult(t,!0);const e=yield this.processChatResult(r.User,t,Buffer.concat([...this.userTalkBuffer]),this.userTalkStart);this.chatContent.push(e),this.emit(c.CONVERSATION_MESSAGE_COMPLETED,{event_type:c.CONVERSATION_MESSAGE_COMPLETED,data:e}),this.chatVoiceBuffer.push(Buffer.concat([...this.userTalkBuffer])),this.userTalkBuffer=[],this.userTalkStart=0,this.emit(c.TRANSCRIPTIONS_MESSAGE_COMPLETED,e);const s=yield this.decorateUserMessage(t);t="",s.answerByAgent?(this.breaked=!1,this.createTtsConnector(null,s.emotionInfo),this.agentBot.chatRequestInStream(s.question,s.chatparams)):s.answerContent&&this.streamOutputContent(s.answerContent,s.chatparams);break;case c.INPUT_AUDIO_BUFFER_CLEARED:this.emit(c.INPUT_AUDIO_BUFFER_CLEARED,i);break;case c.ERROR:this.close(),this.emit(c.ERROR,{type:"asr",error:i.data})}}))})}streamOutputContent(e,t){this.createTtsConnector(null,t);const i=function(e){const t=new RegExp("(["+G.join("")+"])","g");return t.test(e)?e.split(t).reduce((e,t,i,s)=>(i%2==0&&e.push(t+(s[i+1]||"")),e),[]).filter(e=>e.trim().length>0):[e]}(e),s=this;!function e(t=0){setTimeout(()=>{const n=i[t];s.sendText2Tts(n,t===i.length-1),t<i.length-1&&e(t+1)},200)}()}close(){return S(this,arguments,void 0,function*(e="all"){["all","asr"].includes(e.toLocaleLowerCase())&&this.closeAsr(),["all","tts"].includes(e.toLocaleLowerCase())&&this.closeTts()})}closeAsr(){this.asrSocket&&(this.asrSocket.close(),this.asrSocket.removeAllListeners(),this.asrSocket=null)}closeTts(){this.ttsSocket&&(this.ttsSocket.close(),this.ttsSocket.removeAllListeners(),this.ttsSocket=null)}registerTtsEvent(e,t,i){return S(this,void 0,void 0,function*(){e.on("open",()=>console.log("Connected to tts server.")),e.on("message",e=>S(this,void 0,void 0,function*(){const s=JSON.parse(e.toString());switch(s.event_type){case c.SPEECH_CREATED:this.configurationConnector("tts",i),this.emit(c.SPEECH_CREATED,s);break;case c.SPEECH_UPDATED:this.connectors.tts=!0,t&&this.sendText2Tts(t,!0);break;case c.INPUT_TEXT_BUFFER_COMPLETED:this.emit(c.INPUT_TEXT_BUFFER_COMPLETED,s);break;case c.SPEECH_AUDIO_UPDATE:if(!s.data.delta||this.breaked)return;this.agentTalkStart||(this.agentTalkStart=(new Date).getTime());const e=Buffer.from(s.data.delta,"base64");this.agentTalkBuffer.push(e),this.emit(c.CONVERSATION_AUDIO_DELTA,e);break;case c.SPEECH_AUDIO_COMPLETED:const n=yield this.processChatResult(r.Assistant,this.currentTTSConent.join(""),Buffer.concat([...this.agentTalkBuffer]),this.agentTalkStart);this.emit(c.CONVERSATION_MESSAGE_COMPLETED,{event_type:c.CONVERSATION_MESSAGE_COMPLETED,data:n}),this.emit(c.CONVERSATION_AUDIO_COMPLETED,n),this.chatVoiceBuffer.push(Buffer.concat([...this.agentTalkBuffer])),this.chatContent.push(n),this.currentTTSConent=[],this.agentTalkBuffer=[],this.agentTalkStart=0,n.content.length>0&&this.startWaitingUserSpeaking(n.content.length/4),this.closeTts();break;case c.ERROR:this.emit(c.ERROR,{type:"tts",error:s.data}),this.close()}}))})}startVoiceChat(e){return S(this,void 0,void 0,function*(){if(!this.agentBot)return{successed:!1,code:-1,error:"缺少对话机器人"};if(!this.chatconfig.output_audio_config.voice_id)return{successed:!1,code:-2,error:"请提供语音音色"};const t=new W(this.chatconfig.auth_config.appid,this.chatconfig.auth_config.setting);return this.apiToken=yield t.getAccessToken(),this.apiToken?(this.chatconfig.playPrologue&&this.chatconfig.prologueContent?this.createTtsConnector(this.chatconfig.prologueContent):this.connectors.tts=!0,e?this.connectors.asr=!0:this.createAsrConnector(),this.registerAgentEvent(),new Promise((e,t)=>S(this,void 0,void 0,function*(){for(;;){if(this.connectors.asr&&this.connectors.tts){const t=this.ChatConfig.playPrologue&&this.ChatConfig.prologueContent;return this.emit(c.CHAT_UPDATED,{status:t?"playvoice":"startrecord"}),e({successed:!0})}yield L(50)}}))):{successed:!1,error:"获取token失败"}})}createAsrConnector(){const e=`${u}/v1/audio/transcriptions?authorization=Bearer ${this.apiToken}`;this.asrSocket=new T(e),this.registerAsrEvent(this.asrSocket)}createTtsConnector(e,t){if(this.ttsSocket)return;const i=`${u}/v1/audio/speech?authorization=Bearer ${this.apiToken}`;this.ttsSocket=new T(i),this.registerTtsEvent(this.ttsSocket,e,t)}sendAsrCommand(e){this.asrSocket&&this.asrSocket.readyState===T.OPEN&&this.asrSocket.send("string"==typeof e?e:JSON.stringify(e))}sendTtsCommand(e){this.ttsSocket&&this.ttsSocket.readyState===T.OPEN&&this.ttsSocket.send("string"==typeof e?e:JSON.stringify(e))}sendCommand(e,t){return"asr"===e.toLocaleLowerCase()?this.sendAsrCommand(t):"tts"===e.toLocaleLowerCase()?this.sendTtsCommand(t):void 0}}export{K as DualVoiceAgent,I as GptBase,H as GptFactory};//# sourceMappingURL=index.esm.js.map
|