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.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("events"),t=require("openai"),s=require("@coze/api"),i=require("querystring"),n=require("fs"),o=require("path"),r=require("ws"),a=require("crypto"),c=require("axios"),u=require("@azure/openai"),l=require("stream");function d(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var h=d(t),p=d(n),m=d(o),g=d(r),_=d(c);function T(e,t,s,i){return new(s||(s=Promise))(function(n,o){function r(e){try{c(i.next(e))}catch(e){o(e)}}function a(e){try{c(i.throw(e))}catch(e){o(e)}}function c(e){var t;e.done?n(e.value):(t=e.value,t instanceof s?t:new s(function(e){e(t)})).then(r,a)}c((i=i.apply(e,t||[])).next())})}function f(e){var t="function"==typeof Symbol&&Symbol.iterator,s=t&&e[t],i=0;if(s)return s.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&i>=e.length&&(e=void 0),{value:e&&e[i++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function y(e){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var t,s=e[Symbol.asyncIterator];return s?s.call(e):(e=f(e),t={},i("next"),i("throw"),i("return"),t[Symbol.asyncIterator]=function(){return this},t);function i(s){t[s]=e[s]&&function(t){return new Promise(function(i,n){(function(e,t,s,i){Promise.resolve(i).then(function(t){e({value:t,done:s})},t)})(i,n,(t=e[s](t)).done,t.value)})}}}"function"==typeof SuppressedError&&SuppressedError;class E extends e.EventEmitter{constructor(){super()}getTextEmbedding(e,t){return null}createCoversation(e){return T(this,void 0,void 0,function*(){return null})}setVariables(e){return T(this,void 0,void 0,function*(){return{successed:!0}})}getVariables(e){return T(this,void 0,void 0,function*(){return{successed:!1}})}chatRequestInStream(e,t,s,i){return null}}var v=Object.freeze({__proto__:null,default:E});class b extends E{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 k extends b{createOpenAI(e){return new h.default({apiKey:e})}getTextEmbedding(e,t){return T(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 T(this,arguments,void 0,function*(e,t,s={}){if(!e)return{successed:!1,error:{errcode:2,errmsg:"缺失聊天的内容"}};this.aiApi||(this.aiApi=this.createOpenAI(this.apiKey));let i="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:i,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},s);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,s,i){return T(this,void 0,void 0,function*(){var n,o,r,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;i=Object.assign({},i||{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},i);let p,m=[],g=0,_=-1,T=[];try{for(var l,d=!0,h=y(e);!(n=(l=yield h.next()).done);d=!0){a=l.value,d=!1;const e=a,[t]=e.choices,{finish_reason:i,index:n,usage:o}=t,{content:r,tool_calls:c}=t.delta;c&&c.length?(p=c[0].index,g=1,p!==_?(T.push({id:c[0].id,type:"function",function:{name:c[0].function.name,arguments:c[0].function.arguments}}),_=p):T[_].function.arguments+=c[0].function.arguments):m.push(r);let h={successed:!0,requestid:u,segment:r,text:m.join(""),finish_reason:i,index:n,usage:o,has_tool_calls:g,tool_calls:T};s&&(h=Object.assign({},h,s)),this.emit(i?"chatdone":"chattext",h)}}catch(e){o={error:e}}finally{try{d||n||!(r=h.return)||(yield r.call(h))}finally{if(o)throw o.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 C={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 O;!function(e){e[e.None=0]="None",e[e.Thinking=1]="Thinking",e[e.ThinkingOver=2]="ThinkingOver"}(O||(O={}));class A extends E{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=o.join(t.logPath||o.resolve(process.cwd(),"./cozedebug"),this.setting.model||"chat"),n.mkdirSync(this.debugDir,{recursive:!0})),this.apiKey=this.setting.apiKey}createClient(){return T(this,void 0,void 0,function*(){if(this.apiKey)return new s.CozeAPI({baseURL:s.COZE_CN_BASE_URL,token:this.apiKey});const e=yield this.authorizationProvider.getAccessToken();if(!e)throw new Error("get access token failed");return new s.CozeAPI({baseURL:s.COZE_CN_BASE_URL,token:e})})}cloneVoice(e){return T(this,arguments,void 0,function*(e,t={}){const s=yield this.createClient();try{return{successed:!0,voice_id:(yield s.audio.voices.clone(e,t)).voice_id}}catch(e){return{successed:!1,error:e}}})}createSpeech(e,t){return T(this,arguments,void 0,function*(e,t,s="mp3",i={}){const n=yield this.createClient();try{return{successed:!0,data:yield n.audio.speech.create({input:e,voice_id:t,response_format:s},i)}}catch(e){return{successed:!1,error:e}}})}createCoversation(e){return T(this,void 0,void 0,function*(){try{const t=null!=e?e:yield this.createClient(),s=this.botid?{bot_id:this.botid}:{};return(yield t.conversations.create(s)).id}catch(e){return console.error("createCoversation error in coze api"),null}})}setVariables(){return T(this,arguments,void 0,function*(e={}){const t=yield this.createClient();!e.type&&(e.type="app");const s={data:e.data};"app"===e.type&&(s.app_id=this.authorizationProvider.appid),"bot"===e.type&&this.botid&&(s.bot_id=this.botid),e.user_id&&(s.connector_uid=e.user_id);const i=yield t.variables.update(s,{debug:!0});return console.log("setVariables result",i),{successed:!0}})}getVariables(e){return T(this,void 0,void 0,function*(){const t=yield this.createClient(),s=Object.assign({},{bot_id:this.botid},e),i=yield t.variables.retrieve(s);return i?Object.assign({successed:!0},i):{successed:!1,error:"get variables failed"}})}queryWorkflowExecuteResult(e,t){return T(this,arguments,void 0,function*(e,t,s={}){if(!e||!t)return{successed:!1,error:"workflowid or executeid is null"};const i=yield this.createClient();try{return{successed:!0,history:yield i.workflows.runs.history(e,t,s)}}catch(e){return{successed:!1,error:e}}})}getRequestStream(e,t){return T(this,arguments,void 0,function*(e,t,s={}){var i,n;if(this.botid){const n={bot_id:this.botid,additional_messages:t,user_id:s.userid||s.cozeUserID,conversation_id:null!==(i=s.session_id)&&void 0!==i?i:yield this.createCoversation(e),parameters:Object.assign({request_src:1},s.parameters||{})};return n.custom_variables=Object.assign({},this.setting.customVariables||{},s.customVariables||{}),n}if(this.workflowid){const e={ext:s.ext,workflow_id:this.workflowid,is_async:s.is_async||!1,parameters:Object.assign({request_src:1},s.parameters||{},{input:t})};return(s.userid||s.cozeUserID)&&(e.ext=Object.assign({},e.ext||{},{user_id:s.userid||s.cozeUserID})),e}const o={additional_messages:t,workflow_id:this.talkflowid,conversation_id:null!==(n=s.session_id)&&void 0!==n?n:yield this.createCoversation(e),parameters:Object.assign({request_src:1},s.parameters||{})};return(s.userid||s.cozeUserID)&&(o.ext=Object.assign({},o.ext||{},{user_id:s.userid||s.cozeUserID})),o})}getChatDetail(e,t,i){return T(this,void 0,void 0,function*(){const n=Date.now();for(;Date.now()-n<12e4;){const n=yield e.chat.retrieve(t,i);if(n.status===s.ChatStatus.COMPLETED)return{usage:n.usage,messages:yield e.chat.messages.list(t,i)};yield new Promise(e=>setTimeout(e,1500))}return null})}chatRequest(e){return T(this,arguments,void 0,function*(e,t={},i={}){e||this.emit("chaterror",{successed:!1,error:"no message in chat"}),"string"==typeof e&&(e=[{role:s.RoleType.User,content_type:"text",content:e}]);const r=yield this.createClient(),a=this.debug?n.createWriteStream(o.join(this.debugDir,`sync_${this.formatDateWithMs()}.log`),{flags:"a"}):null,c=yield this.getRequestStream(r,e,t);a&&(a.write(`callChatOption:${JSON.stringify(t)}\n`),a.write(`parameters:${JSON.stringify(c)}\n`));const u=this.botid?yield r.chat.create(c,i):yield r.workflows.runs.create(c,i);if(this.workflowid){const e=u;if(c.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 a&&a.end(t),{successed:!0,message:[{role:"assistant",type:"answer",content:t}]}}a&&a.end("workflow run failed"),console.error("workflow run failed",e.msg,e)}if(!this.workflowid&&u.conversation_id&&u.id){const e=u,t=yield this.getChatDetail(r,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 a&&a.end("聊天未完成"),{successed:!1,error:{message:"聊天未完成"}}})}extractXmlContent(e,t){const s=new RegExp(`<\\s*${t}\\s*>([\\s\\S]*?)<\\s*\\/\\s*${t}\\s*>`,"i"),i=e.match(s);return i&&i[1]?i[1].trim():""}parseDeepThinkingJson(e){const t=[e.indexOf(C.thinking.start),e.indexOf(C.reasoning.start),e.indexOf(C.card.start),e.indexOf(C.preresponse.start)];let s=1e4,i=-1;t.forEach((e,t)=>{e>=0&&e<s&&(s=e,i=t)});const n=i>=0?s:-1;if(n<0)return{content:e,status:O.ThinkingOver};const o=[C.thinking,C.reasoning,C.card,C.preresponse][i],r=e.indexOf(o.end,n),a=r>=0?r:e.indexOf("</",n),c=this.extractXmlContent(e.substring(n,a>=0?a:void 0)+o.end,o.tag);return r>=0&&(e=e.substring(r+o.end.length)),{thinking:{action:o,text:c,completed:r>=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 T(this,arguments,void 0,function*(e,t={},r,a){var c,u,l,d,h,p,m;e||this.emit("chaterror",{successed:!1,error:"no message in chat"}),"string"==typeof e&&(e=[{role:s.RoleType.User,content_type:"text",content:e}]);const g=yield this.createClient();let _=Math.ceil(Math.random()*((new Date).getTime()*Math.random())/1e3),T=0;const f=this.debug?n.createWriteStream(o.join(this.debugDir,`${this.formatDateWithMs()}.log`),{flags:"a"}):null,E=yield this.getRequestStream(g,e,t);f&&(f.write(`callChatOption:${JSON.stringify(t)}\n`),f.write(`attach:${r?JSON.stringify(r):"null"}\n`),f.write(`parameters:${JSON.stringify(E)}\n`));const v=this.botid?g.chat.stream(E):this.workflowid?g.workflows.runs.stream(E,a):g.workflows.chat.stream(E,a);let b=0,k=[],A=[],S=[],N=[],I="",R=O.None,w="",x="";try{for(var D,P=!0,U=y(v);!(c=(D=yield U.next()).done);P=!0){d=D.value,P=!1;const e=d;if(e.event===s.ChatEventType.ERROR)return f&&f.write("chat error:"),this.emit("chaterror",{successed:!1,error:"call failed"});if(e.event===s.ChatEventType.CONVERSATION_MESSAGE_DELTA||e.event===s.WorkflowEventType.MESSAGE){let{conversation_id:t,content_type:s,type:n="answer",role:o="assistant",content:a,reasoning_content:c}=e.data;if(f&&f.write(a),"Default"===a)continue;if(!a&&c){this.emit("chatthinking",{text:c,completed:!1,action:"deep-thinking"});continue}if(0===b&&a.startsWith("<xiaolu-")&&(R=O.Thinking,b++),R===O.Thinking){I+=a;const e=this.parseDeepThinkingJson(I),c=e.thinking;if(c)if(I=e.content,c.action===C.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=i.parse(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 s of t)N.push({type:e.type,tag:s})}}this.emit("chatcard",N)}}else if(c.action===C.preresponse){const i=e.thinking.text.slice(x.length);x=e.thinking.text,k.push(i);let a={successed:!0,type:n,content_type:s,role:o,requestid:_,segment:i,text:k.join(""),index:T++,session_id:t,preresponse:!0,preresponseover:e.thinking.completed};r&&(a=Object.assign({},a,r)),this.emit("chattext",a)}else this.emit("chatthinking",{text:e.thinking.text,completed:e.thinking.completed,action:null===(h=c.action)||void 0===h?void 0:h.tag});if(e.status!=O.ThinkingOver)continue;R=O.ThinkingOver,a=I}k.push(a);let u={successed:!0,type:n,content_type:s,role:o,requestid:_,segment:a,text:k.join(""),index:T++,session_id:t};r&&(u=Object.assign({},u,r)),this.emit("chattext",u)}if(e.event===s.ChatEventType.CONVERSATION_MESSAGE_COMPLETED){const{type:t,content:s}=null!==(p=e.data)&&void 0!==p?p:{};"follow_up"===t&&(f&&f.write("followup:"+s),S.push(s))}if(e.event===s.ChatEventType.CONVERSATION_CHAT_COMPLETED||e.event===s.WorkflowEventType.DONE){const{conversation_id:t,content:i,usage:n}=null!==(m=e.data)&&void 0!==m?m:{};let o={successed:!0,usage:n,cards:N.length?N:null,cardfollowup:A.filter(e=>"string"==typeof e&&e.length>0),followup:S,type:"answer",content_type:"text",role:s.RoleType.Assistant,requestid:_,segment:null,text:null!=i?i:k.join(""),index:T++,session_id:t};r&&(o=Object.assign({},o,r)),f&&f.end("chat completed"),this.emit("chatdone",o)}}}catch(e){u={error:e}}finally{try{P||c||!(l=U.return)||(yield l.call(U))}finally{if(u)throw u.error}}return{successed:!0,requestid:_}})}}class S extends A{constructor(){super(...arguments),this.socket=null,this.traceId=a.randomUUID(),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:s.WebsocketsEventType.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"),n={id:this.traceId,event_type:s.WebsocketsEventType.INPUT_AUDIO_BUFFER_APPEND,data:{delta:i}};this.sendCommand(n),this.chatVoiceBuffer.push(e)}sendChatMessage(e,t=s.RoleType.User){const i={id:this.traceId,event_type:s.WebsocketsEventType.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:s.WebsocketsEventType.INPUT_AUDIO_BUFFER_COMPLETE};this.sendCommand(e)}clearVoiceBuffer(){const e={id:this.traceId,event_type:s.WebsocketsEventType.INPUT_AUDIO_BUFFER_CLEAR};this.sendCommand(e)}breakAnswer(){if(1===this.currentAgentStatus){const e={id:this.traceId,event_type:s.WebsocketsEventType.CONVERSATION_CHAT_CANCEL};this.sendCommand(e)}}startVoiceChat(){return T(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=`${s.COZE_CN_BASE_WS_URL}/v1/chat?bot_id=${this.botid}&authorization=Bearer ${t}`;this.socket=new g.default(i);let n=!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 s.WebsocketsEventType.CHAT_CREATED:this.ChatCreated(e);break;case s.WebsocketsEventType.CHAT_UPDATED:this.emit(s.WebsocketsEventType.CHAT_UPDATED,i);break;case s.WebsocketsEventType.CONVERSATION_CHAT_CREATED:this.currentAgentStatus=1,this.emit(s.WebsocketsEventType.CONVERSATION_CHAT_CREATED,i);break;case s.WebsocketsEventType.CONVERSATION_CHAT_IN_PROGRESS:this.emit(s.WebsocketsEventType.CONVERSATION_CHAT_IN_PROGRESS,i);break;case s.WebsocketsEventType.CONVERSATION_MESSAGE_DELTA:this.emit(s.WebsocketsEventType.CONVERSATION_MESSAGE_DELTA,i);break;case s.WebsocketsEventType.CONVERSATION_AUDIO_SENTENCE_START:if(n&&e.playPrologue){this.chatContent.push({role:s.RoleType.Assistant,content:i.data.text});const e=i.data.text.length;this.startWaitingUserSpeaking(e/4),n=!1}this.emit(s.WebsocketsEventType.CONVERSATION_AUDIO_SENTENCE_START,i);break;case s.WebsocketsEventType.CONVERSATION_AUDIO_DELTA:this.emit(s.WebsocketsEventType.CONVERSATION_AUDIO_DELTA,i),this.combineTTS&&i.data.content&&this.chatVoiceBuffer.push(Buffer.from(i.data.content,"base64"));break;case s.WebsocketsEventType.CONVERSATION_MESSAGE_COMPLETED:if(this.emit(s.WebsocketsEventType.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 s.WebsocketsEventType.CONVERSATION_AUDIO_COMPLETED:this.emit(s.WebsocketsEventType.CONVERSATION_AUDIO_COMPLETED,i);break;case s.WebsocketsEventType.CONVERSATION_CHAT_COMPLETED:this.currentAgentStatus=0,this.emit(s.WebsocketsEventType.CONVERSATION_CHAT_COMPLETED,i);break;case s.WebsocketsEventType.CONVERSATION_CHAT_CANCELED:this.currentAgentStatus=0,this.emit(s.WebsocketsEventType.CONVERSATION_CHAT_CANCELED,i),this.startWaitingUserSpeaking();break;case s.WebsocketsEventType.CONVERSATION_CHAT_FAILED:this.currentAgentStatus=0,this.emit(s.WebsocketsEventType.CONVERSATION_CHAT_FAILED,i);break;case s.WebsocketsEventType.INPUT_AUDIO_BUFFER_COMPLETED:this.emit(s.WebsocketsEventType.INPUT_AUDIO_BUFFER_COMPLETED,i);break;case s.WebsocketsEventType.INPUT_AUDIO_BUFFER_CLEAR:this.emit(s.WebsocketsEventType.INPUT_AUDIO_BUFFER_CLEAR,i);break;case s.WebsocketsEventType.ERROR:this.emit(s.WebsocketsEventType.ERROR,i);break;case s.WebsocketsEventType.INPUT_AUDIO_BUFFER_SPEECH_STARTED:this.emit(s.WebsocketsEventType.INPUT_AUDIO_BUFFER_SPEECH_STARTED,i);break;case s.WebsocketsEventType.INPUT_AUDIO_BUFFER_SPEECH_STOPPED:this.emit(s.WebsocketsEventType.INPUT_AUDIO_BUFFER_SPEECH_STOPPED,i);break;case s.WebsocketsEventType.CONVERSATION_AUDIO_TRANSCRIPT_UPDATE:this.emit(s.WebsocketsEventType.CONVERSATION_AUDIO_TRANSCRIPT_UPDATE,i),this.waitingTimer&&this.clearWaitingTimer();break;case s.WebsocketsEventType.CONVERSATION_AUDIO_TRANSCRIPT_COMPLETED:this.emit(s.WebsocketsEventType.CONVERSATION_AUDIO_TRANSCRIPT_COMPLETED,i);break;case s.WebsocketsEventType.CONVERSATION_CHAT_REQUIRES_ACTION:this.emit(s.WebsocketsEventType.CONVERSATION_CHAT_REQUIRES_ACTION,i)}}),{successed:!0}})}ChatCreated(e={}){this.emit(s.WebsocketsEventType.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:s.WebsocketsEventType.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 N=["[AUTHORIZATION NEEDED]","[AUTHORIZATION ERROR]","[BODY ERROR]","[REQUEST ERROR]"];class I extends k{constructor(e,t,s={}){super(e,s),this.proxySetting=t}chatRequest(e,t,s){return T(this,void 0,void 0,function*(){const i=Object.assign({headers:{"Content-Type":"application/json",authorization:`Bearer ${this.apiKey}`},method:"post",url:this.proxySetting.serviceurl,data:{chatText:e,option:t}},s);return(yield _.default(i)).data})}chatRequestInStream(e,t,s,i){return T(this,void 0,void 0,function*(){let n,o=Math.ceil(Math.random()*((new Date).getTime()*Math.random())/1e3);const r={headers:{"Content-Type":"application/json",authorization:`Bearer ${this.apiKey}`},method:"post",url:this.proxySetting.serviceurl+"/stream",data:{messages:e,option:t,axiosOption:i},responseType:"stream"};let a="";_.default(r).then(e=>{e.data.on("data",e=>{if(n=e.toString(),n){if(N.includes(n))return this.emit("requesterror",{successed:!1,requestid:o,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),s);this.emit(e.finish_reason?"chatdone":"chattext",e)}catch(e){break}}})}).catch(e=>{this.emit("requesterror",{successed:!1,requestid:o,error:"Axios Error : "+e})})})}}class R extends b{constructor(e,t,s={}){super(e,s),this.azureSetting=t,this.azureSetting.endpoint.toLowerCase().startsWith("https://")||(this.azureSetting.endpoint="https://"+this.azureSetting.endpoint)}createOpenAI(e){return new u.OpenAIClient(this.azureSetting.endpoint,new u.AzureKeyCredential(e))}get EmbeddingUrl(){return`${this.azureSetting.endpoint}/openai/deployments/${this.embeddingmodel||"openai-embedding-ada-002"}/embeddings?api-version=2022-12-01`}getTextEmbedding(e){return T(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 T(this,arguments,void 0,function*(e,t,s={}){if(!e)return{successed:!1,error:{errcode:2,errmsg:"缺失聊天的内容"}};this.aiApi||(this.aiApi=this.createOpenAI(this.apiKey));let i="string"==typeof e?[{role:"user",content:e}]:e;try{const e=yield this.aiApi.getChatCompletions((null==t?void 0:t.model)||this.chatModel,i,{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:s,completionTokens:n,totalTokens:o}=e.usage;let r=[];for(const t of e.choices){const{index:e,finishReason:s,message:i}=t;r.push({index:e,finish_reason:s,message:i})}return{successed:!0,message:r,usage:{prompt_tokens:s,completion_tokens:n,total_tokens:o}}}catch(e){return console.log("result is error ",e),{successed:!1,error:e}}})}chatRequestInStream(e,t,s,i){return T(this,void 0,void 0,function*(){var n,o,r,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;i=Object.assign({},i||{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 i=[];try{for(var T,f=!0,E=y(e);!(n=(T=yield E.next()).done);f=!0){a=T.value,f=!1;const e=a;for(const t of e.choices){const{finishReason:e,index:n}=t,o=null===(c=t.delta)||void 0===c?void 0:c.toolCalls;if(o&&o.length)h=o[0].index,m=1,h!==g?(_.push({id:o[0].id,type:"function",function:{name:o[0].function.name,arguments:o[0].function.arguments}}),g=h):_[g].function.arguments+=o[0].function.arguments;else{const e=null===(u=t.delta)||void 0===u?void 0:u.content;i.push(e)}let r={successed:!0,requestid:p,segment:null===(l=t.delta)||void 0===l?void 0:l.content,text:i.join(""),finish_reason:e,index:n,has_tool_calls:m,tool_calls:_};s&&(r=Object.assign({},r,s)),this.emit(e?"chatdone":"chattext",r)}}}catch(e){o={error:e}}finally{try{f||n||!(r=E.return)||(yield r.call(E))}finally{if(o)throw o.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 w(){return T(this,arguments,void 0,function*(e={}){e.data||(e.data=e.body);try{return{successed:!0,data:(yield _.default(e)).data}}catch(e){return{successed:!1,error:e,data:e}}})}function x(e){return new Promise(t=>setTimeout(t,e))}class D extends E{constructor(e,t){super(),this.apikey=e,this.agent=t}chatRequest(e,t){return T(this,arguments,void 0,function*(e,t,s={}){var i;if(!e)return{successed:!1,error:"no text in chat"};const n="object"==typeof e?null!==(i=e.text)&&void 0!==i?i:e.content:e;s=Object.assign({},s||{timeout:6e4});const o=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}},s),r=yield w(o);if(!r.successed||r.data.code)return{successed:!1,error:"failed"};const{answer:a,session_id:c}=r.data.data;return{successed:!0,message:[{message:{content:a}}],session_id:c}})}chatRequestInStream(e,t,s,i){return T(this,void 0,void 0,function*(){var n,o,r,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;i=Object.assign({},i||{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"},i),c=yield _.default(e),g=l.Readable.from(c.data);let T,f=0,E="",v=[],b=[];try{for(var h,p=!0,m=y(g);!(n=(h=yield m.next()).done);p=!0){a=h.value,p=!1;const e=a;let t=null,i="";try{i=e.toString().split("data:"),i.length&&(i=i[i.length-1]+""),t=JSON.parse(v.join("")+i)}catch(e){v.push(i);continue}v=[];const{answer:n,running_status:o,reference:r}=t.data;if(!0===o)continue;const c=n?n.replace(E,""):"";E=null!=n?n:E,T||(T=t.data.session_id),r&&r.chunks&&r.chunks.length&&(b=b.concat(r.chunks));const u=!0===t.data;let l={successed:!0,requestid:d,segment:c,chunks:b,text:E,finish_reason:u?"stop":null,index:f++,session_id:T};s&&(l=Object.assign({},l,s)),this.emit(u?"chatdone":"chattext",l)}}catch(e){o={error:e}}finally{try{p||n||!(r=m.return)||(yield r.call(m))}finally{if(o)throw o.error}}return{successed:!0,requestid:d}}catch(e){}})}}class P extends E{constructor(e,t={}){super(),this.apiOption={},this.apiKey=e,this.apiOption=t}getTextEmbedding(e){return T(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 s=Array.isArray(e)?e:[e],i={method:"post",headers:{"Content-Type":"application/json",authorization:`Bearer ${this.apiKey}`},data:{model:t.model||this.apiOption.embedding,input:s},url:"https://ark.cn-beijing.volces.com/api/v3/embeddings"};try{return{successed:!0,embedding:(yield w(i)).data}}catch(e){return{successed:!1,error:e}}})}chatRequest(e,t){return T(this,arguments,void 0,function*(e,t,s={}){if(!e)return{successed:!1,error:{errcode:2,errmsg:"缺失聊天的内容"}};const i=this.assembleApiParams(e,!1,t,s);try{const e=yield w(i);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,s,i={}){let n="string"==typeof e?[{role:"user",content:e}]:e,o={};((null==s?void 0:s.temperature)||this.apiOption.temperature)&&(o.temperature=Number((null==s?void 0:s.temperature)||this.apiOption.temperature)),o.max_tokens=Number((null==s?void 0:s.maxtoken)||this.apiOption.maxtoken),((null==s?void 0:s.top_p)||this.apiOption.top_p)&&(o.top_p=Number((null==s?void 0:s.top_p)||this.apiOption.top_p)),((null==s?void 0:s.presence_penalty)||this.apiOption.presence_penalty)&&(o.presence_penalty=Number((null==s?void 0:s.presence_penalty)||this.apiOption.presence_penalty)),((null==s?void 0:s.frequency_penalty)||this.apiOption.frequency_penalty)&&(o.frequency_penalty=Number((null==s?void 0:s.frequency_penalty)||this.apiOption.frequency_penalty)),((null==s?void 0:s.top_logprobs)||this.apiOption.top_logprobs)&&(o.logprobs=!0,o.top_logprobs=Number((null==s?void 0:s.top_logprobs)||this.apiOption.top_logprobs)),o.tools=1===(null==s?void 0:s.enableToolCall)&&(null==s?void 0:s.tools)?s.tools:void 0,o.tool_choice=1===(null==s?void 0:s.enableToolCall)?"auto":void 0;const r=Object.assign(Object.assign({},i),{method:"post",headers:{"Content-Type":"application/json",authorization:`Bearer ${this.apiKey}`},data:Object.assign(Object.assign({model:(null==s?void 0:s.model)||this.apiOption.model},o),{messages:n,stream:t}),url:"https://ark.cn-beijing.volces.com/api/v3/chat/completions"});return t&&(r.responseType="stream"),r}chatRequestInStream(e,t,s,i){return T(this,void 0,void 0,function*(){e||this.emit("chaterror",{successed:!1,error:"no text in chat"}),i=Object.assign({},i||{timeout:1e4});const n=this.assembleApiParams(e,!0,t,i);let o=Math.ceil(Math.random()*((new Date).getTime()*Math.random())/1e3),r=[];try{return function(e={},t){e.data||(e.data=e.body),_.default(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: /),o,r,s)}),{successed:!0,requestid:o}}catch(e){return this.emit("requesterror",{successed:!1,requestid:o,error:"call axios faied "+e}),{successed:!1,requestid:o}}})}processChunkData(e,t,s,i){let n,o=0,r=-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,o=1,n!==r?(a.push({id:m[0].id,type:"function",function:{name:m[0].function.name,arguments:m[0].function.arguments}}),r=n):a[r].function.arguments+=m[0].function.arguments):s.push(p);let g={successed:!0,requestid:t,segment:p,text:s.join(""),finish_reason:l,index:d,usage:h,has_tool_calls:o,tool_calls:a};i&&(g=Object.assign({},g,i)),this.emit(l?"chatdone":"chattext",g)}}}class U{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 T(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 T(this,void 0,void 0,function*(){try{const e="api.coze.cn",t=m.default.join(process.cwd(),this.pemfile);if(!p.default.existsSync(t))return{successed:!1,error:`${t} not existed`};const i=p.default.readFileSync(t).toString(),n=yield s.getJWTToken({baseURL:s.COZE_CN_BASE_URL,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 W=new Map,V={OPENAI:"openai",OPENAIPROXY:"openaiproxy",MICROSOFT:"microsoft",AIMP:"aimp",COZE:"coze",DOUBAO:"doubao"};var q=Object.freeze({__proto__:null,GptProviderEnum:V,createGpt:function(e,t,s){let{model:i,agentid:n,maxtoken:o,temperature:r,serviceurl:a,endpoint:c,engine:u,version:l,embedding:d,top_p:h,presence_penalty:p,frequency_penalty:m}=s||{};switch(e){case V.OPENAI:return new k(t+"",{model:i,maxtoken:o,temperature:r,embedding:d,top_p:h,presence_penalty:p,frequency_penalty:m});case V.OPENAIPROXY:return new I(t+"",{serviceurl:a},{model:i,maxtoken:o,temperature:r,embedding:d,top_p:h,presence_penalty:p,frequency_penalty:m});case V.MICROSOFT:return new R(t+"",{endpoint:c,engine:u,version:l},{model:i,maxtoken:o,temperature:r,embedding:d,top_p:h,presence_penalty:p,frequency_penalty:m});case V.AIMP:return new D(t+"",{endpoint:c,agentid:n});case V.COZE:let e=W.get(t+"");return e||(e=new U(t+"",s),W.set(t+"",e)),!0===s.voicebot?new S(e,s):new A(e,s);case V.DOUBAO:return new P(t+"",{model:i,maxtoken:o,temperature:r,top_p:h,presence_penalty:p,frequency_penalty:m,embedding:d});default:return null}}});const B=["。","!","!","?","?",";",";",":",":","\n"];class M extends l.EventEmitter{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=a.randomUUID(),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:s.WebsocketsEventType.INPUT_TEXT_BUFFER_APPEND,data:{delta:e}};this.sendCommand("tts",t),this.currentTTSConent.push(e),this.emit(s.WebsocketsEventType.CONVERSATION_AUDIO_SENTENCE_START,{event_type:s.WebsocketsEventType.CONVERSATION_AUDIO_SENTENCE_START,data:{text:this.currentTTSConent.join("")}})}if(t){const e={id:this.traceId,event_type:s.WebsocketsEventType.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"),n={id:this.traceId,event_type:s.WebsocketsEventType.INPUT_AUDIO_BUFFER_APPEND,data:{delta:i}};this.sendCommand("asr",n),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(s.WebsocketsEventType.INPUT_AUDIO_BUFFER_SPEECH_STOPPED,{event_type:s.WebsocketsEventType.INPUT_AUDIO_BUFFER_SPEECH_STOPPED}),this.submitVoiceBuffer())}sendChatMessage(e,t={}){this.agentBot.chatRequestInStream(e,t)}submitVoiceBuffer(){const e={id:this.traceId,event_type:s.WebsocketsEventType.INPUT_AUDIO_BUFFER_COMPLETE};this.sendCommand("asr",e)}clearVoiceBuffer(){const e={id:this.traceId,event_type:s.WebsocketsEventType.INPUT_AUDIO_BUFFER_CLEAR};this.sendCommand("asr",e)}breakAnswer(e=!1){this.breaked=!0;const t={id:this.traceId,event_type:s.WebsocketsEventType.INPUT_TEXT_BUFFER_COMPLETE};this.sendCommand("tts",t),this.emit(s.WebsocketsEventType.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:s.WebsocketsEventType.SPEECH_UPDATE,data:{output_audio:this.chatconfig.output_audio_config}}:{id:this.traceId,event_type:s.WebsocketsEventType.TRANSCRIPTIONS_UPDATE,data:{input_audio:this.chatconfig.input_audio_config,asr_config:this.chatconfig.asr_config}};this.sendCommand(e,i)}decorateUserMessage(e){return T(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 T(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 s=e.length-1;s>=5;s--){const i=e[s];if(t.has(i))return{index:s,text:e.slice(0,s+1),remain:e.slice(s+1)}}return{index:-1}}registerAgentEvent(){return T(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 s=this.getLastPunctuationInfo(e.join(""));s.index>0&&(this.sendText2Tts(s.text,!1),e=s.remain?[s.remain]:[])}),this.agentBot.on("chatdone",t=>this.sendText2Tts(e.join(""),!0)))})}processAsrResult(e){return T(this,arguments,void 0,function*(e,t=!1){return e})}processChatResult(e,t,i,n){return T(this,arguments,void 0,function*(e,t,i,n,o="pcm"){const r=(new Date).getTime();return{role:e,content:t,start:n,end:r,duration:(r-n)/1e3,type:e===s.RoleType.User?"question":"answer"}})}registerAsrEvent(e){return T(this,void 0,void 0,function*(){let t="";e.on("open",()=>console.log("Connected to asr server.")),e.on("message",e=>T(this,void 0,void 0,function*(){const i=JSON.parse(e.toString());switch(i.event_type){case s.WebsocketsEventType.TRANSCRIPTIONS_CREATED:this.configurationConnector("asr"),this.emit(s.WebsocketsEventType.TRANSCRIPTIONS_CREATED,i);break;case s.WebsocketsEventType.TRANSCRIPTIONS_UPDATED:this.connectors.asr=!0;break;case s.WebsocketsEventType.INPUT_AUDIO_BUFFER_COMPLETED:this.lastGetAsrChangedTime=0,this.emit(s.WebsocketsEventType.INPUT_AUDIO_BUFFER_COMPLETED,i);break;case s.WebsocketsEventType.TRANSCRIPTIONS_MESSAGE_UPDATE:this.lastGetAsrChangedTime=(new Date).getTime(),t=yield this.processAsrResult(i.data.content),this.emit(s.WebsocketsEventType.TRANSCRIPTIONS_MESSAGE_UPDATE,{content:t});break;case s.WebsocketsEventType.TRANSCRIPTIONS_MESSAGE_COMPLETED:t=yield this.processAsrResult(t,!0);const e=yield this.processChatResult(s.RoleType.User,t,Buffer.concat([...this.userTalkBuffer]),this.userTalkStart);this.chatContent.push(e),this.emit(s.WebsocketsEventType.CONVERSATION_MESSAGE_COMPLETED,{event_type:s.WebsocketsEventType.CONVERSATION_MESSAGE_COMPLETED,data:e}),this.chatVoiceBuffer.push(Buffer.concat([...this.userTalkBuffer])),this.userTalkBuffer=[],this.userTalkStart=0,this.emit(s.WebsocketsEventType.TRANSCRIPTIONS_MESSAGE_COMPLETED,e);const n=yield this.decorateUserMessage(t);t="",n.answerByAgent?(this.breaked=!1,this.createTtsConnector(null,n.emotionInfo),this.agentBot.chatRequestInStream(n.question,n.chatparams)):n.answerContent&&this.streamOutputContent(n.answerContent,n.chatparams);break;case s.WebsocketsEventType.INPUT_AUDIO_BUFFER_CLEARED:this.emit(s.WebsocketsEventType.INPUT_AUDIO_BUFFER_CLEARED,i);break;case s.WebsocketsEventType.ERROR:this.close(),this.emit(s.WebsocketsEventType.ERROR,{type:"asr",error:i.data})}}))})}streamOutputContent(e,t){this.createTtsConnector(null,t);const s=function(e){const t=new RegExp("(["+B.join("")+"])","g");return t.test(e)?e.split(t).reduce((e,t,s,i)=>(s%2==0&&e.push(t+(i[s+1]||"")),e),[]).filter(e=>e.trim().length>0):[e]}(e),i=this;!function e(t=0){setTimeout(()=>{const n=s[t];i.sendText2Tts(n,t===s.length-1),t<s.length-1&&e(t+1)},200)}()}close(){return T(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 T(this,void 0,void 0,function*(){e.on("open",()=>console.log("Connected to tts server.")),e.on("message",e=>T(this,void 0,void 0,function*(){const n=JSON.parse(e.toString());switch(n.event_type){case s.WebsocketsEventType.SPEECH_CREATED:this.configurationConnector("tts",i),this.emit(s.WebsocketsEventType.SPEECH_CREATED,n);break;case s.WebsocketsEventType.SPEECH_UPDATED:this.connectors.tts=!0,t&&this.sendText2Tts(t,!0);break;case s.WebsocketsEventType.INPUT_TEXT_BUFFER_COMPLETED:this.emit(s.WebsocketsEventType.INPUT_TEXT_BUFFER_COMPLETED,n);break;case s.WebsocketsEventType.SPEECH_AUDIO_UPDATE:if(!n.data.delta||this.breaked)return;this.agentTalkStart||(this.agentTalkStart=(new Date).getTime());const e=Buffer.from(n.data.delta,"base64");this.agentTalkBuffer.push(e),this.emit(s.WebsocketsEventType.CONVERSATION_AUDIO_DELTA,e);break;case s.WebsocketsEventType.SPEECH_AUDIO_COMPLETED:const o=yield this.processChatResult(s.RoleType.Assistant,this.currentTTSConent.join(""),Buffer.concat([...this.agentTalkBuffer]),this.agentTalkStart);this.emit(s.WebsocketsEventType.CONVERSATION_MESSAGE_COMPLETED,{event_type:s.WebsocketsEventType.CONVERSATION_MESSAGE_COMPLETED,data:o}),this.emit(s.WebsocketsEventType.CONVERSATION_AUDIO_COMPLETED,o),this.chatVoiceBuffer.push(Buffer.concat([...this.agentTalkBuffer])),this.chatContent.push(o),this.currentTTSConent=[],this.agentTalkBuffer=[],this.agentTalkStart=0,o.content.length>0&&this.startWaitingUserSpeaking(o.content.length/4),this.closeTts();break;case s.WebsocketsEventType.ERROR:this.emit(s.WebsocketsEventType.ERROR,{type:"tts",error:n.data}),this.close()}}))})}startVoiceChat(e){return T(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 U(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)=>T(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(s.WebsocketsEventType.CHAT_UPDATED,{status:t?"playvoice":"startrecord"}),e({successed:!0})}yield x(50)}}))):{successed:!1,error:"获取token失败"}})}createAsrConnector(){const e=`${s.COZE_CN_BASE_WS_URL}/v1/audio/transcriptions?authorization=Bearer ${this.apiToken}`;this.asrSocket=new g.default(e),this.registerAsrEvent(this.asrSocket)}createTtsConnector(e,t){if(this.ttsSocket)return;const i=`${s.COZE_CN_BASE_WS_URL}/v1/audio/speech?authorization=Bearer ${this.apiToken}`;this.ttsSocket=new g.default(i),this.registerTtsEvent(this.ttsSocket,e,t)}sendAsrCommand(e){this.asrSocket&&this.asrSocket.readyState===g.default.OPEN&&this.asrSocket.send("string"==typeof e?e:JSON.stringify(e))}sendTtsCommand(e){this.ttsSocket&&this.ttsSocket.readyState===g.default.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}}Object.defineProperty(exports,"COZE_SOCKET_EVENT_TYPE",{enumerable:!0,get:function(){return s.WebsocketsEventType}}),exports.DualVoiceAgent=M,exports.GptBase=v,exports.GptFactory=q;//# sourceMappingURL=index.js.map
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("events"),t=require("openai"),s=require("@coze/api"),i=require("querystring"),n=require("fs"),o=require("path"),r=require("ws"),a=require("crypto"),c=require("axios"),u=require("@azure/openai"),l=require("stream");function d(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var h=d(t),p=d(n),m=d(o),g=d(r),_=d(c);function T(e,t,s,i){return new(s||(s=Promise))(function(n,o){function r(e){try{c(i.next(e))}catch(e){o(e)}}function a(e){try{c(i.throw(e))}catch(e){o(e)}}function c(e){var t;e.done?n(e.value):(t=e.value,t instanceof s?t:new s(function(e){e(t)})).then(r,a)}c((i=i.apply(e,t||[])).next())})}function f(e){var t="function"==typeof Symbol&&Symbol.iterator,s=t&&e[t],i=0;if(s)return s.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&i>=e.length&&(e=void 0),{value:e&&e[i++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function y(e){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var t,s=e[Symbol.asyncIterator];return s?s.call(e):(e=f(e),t={},i("next"),i("throw"),i("return"),t[Symbol.asyncIterator]=function(){return this},t);function i(s){t[s]=e[s]&&function(t){return new Promise(function(i,n){(function(e,t,s,i){Promise.resolve(i).then(function(t){e({value:t,done:s})},t)})(i,n,(t=e[s](t)).done,t.value)})}}}"function"==typeof SuppressedError&&SuppressedError;class E extends e.EventEmitter{constructor(){super()}getTextEmbedding(e,t){return null}createCoversation(e){return T(this,void 0,void 0,function*(){return null})}setVariables(e){return T(this,void 0,void 0,function*(){return{successed:!0}})}getVariables(e){return T(this,void 0,void 0,function*(){return{successed:!1}})}chatRequestInStream(e,t,s,i){return null}}var v=Object.freeze({__proto__:null,default:E});class b extends E{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 k extends b{createOpenAI(e){return new h.default({apiKey:e})}getTextEmbedding(e,t){return T(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 T(this,arguments,void 0,function*(e,t,s={}){if(!e)return{successed:!1,error:{errcode:2,errmsg:"缺失聊天的内容"}};this.aiApi||(this.aiApi=this.createOpenAI(this.apiKey));let i="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:i,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},s);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,s,i){return T(this,void 0,void 0,function*(){var n,o,r,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;i=Object.assign({},i||{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},i);let p,m=[],g=0,_=-1,T=[];try{for(var l,d=!0,h=y(e);!(n=(l=yield h.next()).done);d=!0){a=l.value,d=!1;const e=a,[t]=e.choices,{finish_reason:i,index:n,usage:o}=t,{content:r,tool_calls:c}=t.delta;c&&c.length?(p=c[0].index,g=1,p!==_?(T.push({id:c[0].id,type:"function",function:{name:c[0].function.name,arguments:c[0].function.arguments}}),_=p):T[_].function.arguments+=c[0].function.arguments):m.push(r);let h={successed:!0,requestid:u,segment:r,text:m.join(""),finish_reason:i,index:n,usage:o,has_tool_calls:g,tool_calls:T};s&&(h=Object.assign({},h,s)),this.emit(i?"chatdone":"chattext",h)}}catch(e){o={error:e}}finally{try{d||n||!(r=h.return)||(yield r.call(h))}finally{if(o)throw o.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 C={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 O;!function(e){e[e.None=0]="None",e[e.Thinking=1]="Thinking",e[e.ThinkingOver=2]="ThinkingOver"}(O||(O={}));class A extends E{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=o.join(t.logPath||o.resolve(process.cwd(),"./cozedebug"),this.setting.model||"chat"),n.mkdirSync(this.debugDir,{recursive:!0})),this.apiKey=this.setting.apiKey}createClient(){return T(this,void 0,void 0,function*(){if(this.apiKey)return new s.CozeAPI({baseURL:s.COZE_CN_BASE_URL,token:this.apiKey});const e=yield this.authorizationProvider.getAccessToken();if(!e)throw new Error("get access token failed");return new s.CozeAPI({baseURL:s.COZE_CN_BASE_URL,token:e})})}cloneVoice(e){return T(this,arguments,void 0,function*(e,t={}){const s=yield this.createClient();try{return{successed:!0,voice_id:(yield s.audio.voices.clone(e,t)).voice_id}}catch(e){return{successed:!1,error:e}}})}createSpeech(e,t){return T(this,arguments,void 0,function*(e,t,s="mp3",i={}){const n=yield this.createClient();try{return{successed:!0,data:yield n.audio.speech.create({input:e,voice_id:t,response_format:s},i)}}catch(e){return{successed:!1,error:e}}})}createCoversation(e){return T(this,void 0,void 0,function*(){try{const t=null!=e?e:yield this.createClient(),s=this.botid?{bot_id:this.botid}:{};return(yield t.conversations.create(s)).id}catch(e){return console.error("createCoversation error in coze api"),null}})}setVariables(){return T(this,arguments,void 0,function*(e={}){const t=yield this.createClient();!e.type&&(e.type="app");const s={data:e.data};return"app"===e.type&&(s.app_id=this.authorizationProvider.appid),"bot"===e.type&&this.botid&&(s.bot_id=this.botid),e.user_id&&(s.connector_uid=e.user_id),yield t.variables.update(s,{debug:!0}),{successed:!0}})}getVariables(e){return T(this,void 0,void 0,function*(){const t=yield this.createClient(),s=Object.assign({},{bot_id:this.botid},e),i=yield t.variables.retrieve(s);return i?Object.assign({successed:!0},i):{successed:!1,error:"get variables failed"}})}queryWorkflowExecuteResult(e,t){return T(this,arguments,void 0,function*(e,t,s={}){if(!e||!t)return{successed:!1,error:"workflowid or executeid is null"};const i=yield this.createClient();try{return{successed:!0,history:yield i.workflows.runs.history(e,t,s)}}catch(e){return{successed:!1,error:e}}})}getRequestStream(e,t){return T(this,arguments,void 0,function*(e,t,s={}){var i,n;if(this.botid){const n={bot_id:this.botid,additional_messages:t,user_id:s.userid||s.cozeUserID,conversation_id:null!==(i=s.session_id)&&void 0!==i?i:yield this.createCoversation(e),parameters:Object.assign({request_src:1},s.parameters||{})};return n.custom_variables=Object.assign({},this.setting.customVariables||{},s.customVariables||{}),n}if(this.workflowid){const e={ext:s.ext,workflow_id:this.workflowid,is_async:s.is_async||!1,parameters:Object.assign({request_src:1},s.parameters||{},{input:t})};return(s.userid||s.cozeUserID)&&(e.ext=Object.assign({},e.ext||{},{user_id:s.userid||s.cozeUserID})),e}const o={additional_messages:t,workflow_id:this.talkflowid,conversation_id:null!==(n=s.session_id)&&void 0!==n?n:yield this.createCoversation(e),parameters:Object.assign({request_src:1},s.parameters||{})};return(s.userid||s.cozeUserID)&&(o.ext=Object.assign({},o.ext||{},{user_id:s.userid||s.cozeUserID})),o})}getChatDetail(e,t,i){return T(this,void 0,void 0,function*(){const n=Date.now();for(;Date.now()-n<12e4;){const n=yield e.chat.retrieve(t,i);if(n.status===s.ChatStatus.COMPLETED)return{usage:n.usage,messages:yield e.chat.messages.list(t,i)};yield new Promise(e=>setTimeout(e,1500))}return null})}chatRequest(e){return T(this,arguments,void 0,function*(e,t={},i={}){e||this.emit("chaterror",{successed:!1,error:"no message in chat"}),"string"==typeof e&&(e=[{role:s.RoleType.User,content_type:"text",content:e}]);const r=yield this.createClient(),a=this.debug?n.createWriteStream(o.join(this.debugDir,`sync_${this.formatDateWithMs()}.log`),{flags:"a"}):null,c=yield this.getRequestStream(r,e,t);a&&(a.write(`callChatOption:${JSON.stringify(t)}\n`),a.write(`parameters:${JSON.stringify(c)}\n`));const u=this.botid?yield r.chat.create(c,i):yield r.workflows.runs.create(c,i);if(this.workflowid){const e=u;if(c.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,s=e.usage;return a&&a.end(t),{successed:!0,message:[{role:"assistant",type:"answer",content:t,usage:s}]}}a&&a.end("workflow run failed"),console.error("workflow run failed",e.msg,e)}if(!this.workflowid&&u.conversation_id&&u.id){const e=u,t=yield this.getChatDetail(r,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 a&&a.end("聊天未完成"),{successed:!1,error:{message:"聊天未完成"}}})}extractXmlContent(e,t){const s=new RegExp(`<\\s*${t}\\s*>([\\s\\S]*?)<\\s*\\/\\s*${t}\\s*>`,"i"),i=e.match(s);return i&&i[1]?i[1].trim():""}parseDeepThinkingJson(e){const t=[e.indexOf(C.thinking.start),e.indexOf(C.reasoning.start),e.indexOf(C.card.start),e.indexOf(C.preresponse.start)];let s=1e4,i=-1;t.forEach((e,t)=>{e>=0&&e<s&&(s=e,i=t)});const n=i>=0?s:-1;if(n<0)return{content:e,status:O.ThinkingOver};const o=[C.thinking,C.reasoning,C.card,C.preresponse][i],r=e.indexOf(o.end,n),a=r>=0?r:e.indexOf("</",n),c=this.extractXmlContent(e.substring(n,a>=0?a:void 0)+o.end,o.tag);return r>=0&&(e=e.substring(r+o.end.length)),{thinking:{action:o,text:c,completed:r>=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 T(this,arguments,void 0,function*(e,t={},r,a){var c,u,l,d,h,p,m;e||this.emit("chaterror",{successed:!1,error:"no message in chat"}),"string"==typeof e&&(e=[{role:s.RoleType.User,content_type:"text",content:e}]);const g=yield this.createClient();let _=Math.ceil(Math.random()*((new Date).getTime()*Math.random())/1e3),T=0;const f=this.debug?n.createWriteStream(o.join(this.debugDir,`${this.formatDateWithMs()}.log`),{flags:"a"}):null,E=yield this.getRequestStream(g,e,t);f&&(f.write(`callChatOption:${JSON.stringify(t)}\n`),f.write(`attach:${r?JSON.stringify(r):"null"}\n`),f.write(`parameters:${JSON.stringify(E)}\n`));const v=this.botid?g.chat.stream(E):this.workflowid?g.workflows.runs.stream(E,a):g.workflows.chat.stream(E,a);let b=[],k=[],A=[],S=[],N="",I=O.None,R="",w="";try{for(var x,D=!0,P=y(v);!(c=(x=yield P.next()).done);D=!0){d=x.value,D=!1;const e=d;if(e.event===s.ChatEventType.ERROR)return f&&f.write("chat error:"),this.emit("chaterror",{successed:!1,error:"call failed"});if(e.event===s.ChatEventType.CONVERSATION_MESSAGE_DELTA||e.event===s.WorkflowEventType.MESSAGE){let{conversation_id:t,content_type:s,type:n="answer",role:o="assistant",content:a,reasoning_content:c}=e.data;if(f&&f.write(a),"Default"===a)continue;if(!a&&c){this.emit("chatthinking",{text:c,completed:!1,action:"deep-thinking"});continue}if(I!==O.Thinking&&a.startsWith("<xiaolu-")&&(I=O.Thinking),I===O.Thinking){N+=a;const e=this.parseDeepThinkingJson(N),c=e.thinking;if(c)if(N=e.content,c.action===C.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=i.parse(t);if(e.type&&e.tag&&"string"==typeof e.tag)if("follow_up"===e.type)k.push(e.tag);else{const t=e.tag.split(",");for(const s of t)S.push({type:e.type,tag:s})}}this.emit("chatcard",S)}}else if(c.action===C.preresponse){const i=e.thinking.text.slice(w.length);w=e.thinking.text,b.push(i);let a={successed:!0,type:n,content_type:s,role:o,requestid:_,segment:i,text:b.join(""),index:T++,session_id:t,preresponse:!0,preresponseover:e.thinking.completed};r&&(a=Object.assign({},a,r)),this.emit("chattext",a)}else this.emit("chatthinking",{text:e.thinking.text,completed:e.thinking.completed,action:null===(h=c.action)||void 0===h?void 0:h.tag});if(e.status!=O.ThinkingOver)continue;I=O.ThinkingOver,a=N}b.push(a);let u={successed:!0,type:n,content_type:s,role:o,requestid:_,segment:a,text:b.join(""),index:T++,session_id:t};r&&(u=Object.assign({},u,r)),this.emit("chattext",u)}if(e.event===s.ChatEventType.CONVERSATION_MESSAGE_COMPLETED){const{type:t,content:s}=null!==(p=e.data)&&void 0!==p?p:{};"follow_up"===t&&(f&&f.write("followup:"+s),A.push(s))}if(e.event===s.ChatEventType.CONVERSATION_CHAT_COMPLETED||e.event===s.WorkflowEventType.DONE){const{conversation_id:t,content:i,usage:n}=null!==(m=e.data)&&void 0!==m?m:{};let o={successed:!0,usage:n,cards:S.length?S:null,cardfollowup:k.filter(e=>"string"==typeof e&&e.length>0),followup:A,type:"answer",content_type:"text",role:s.RoleType.Assistant,requestid:_,segment:null,text:null!=i?i:b.join(""),index:T++,session_id:t};r&&(o=Object.assign({},o,r)),f&&f.end("chat completed"),this.emit("chatdone",o)}}}catch(e){u={error:e}}finally{try{D||c||!(l=P.return)||(yield l.call(P))}finally{if(u)throw u.error}}return{successed:!0,requestid:_}})}}class S extends A{constructor(){super(...arguments),this.socket=null,this.traceId=a.randomUUID(),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:s.WebsocketsEventType.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"),n={id:this.traceId,event_type:s.WebsocketsEventType.INPUT_AUDIO_BUFFER_APPEND,data:{delta:i}};this.sendCommand(n),this.chatVoiceBuffer.push(e)}sendChatMessage(e,t=s.RoleType.User){const i={id:this.traceId,event_type:s.WebsocketsEventType.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:s.WebsocketsEventType.INPUT_AUDIO_BUFFER_COMPLETE};this.sendCommand(e)}clearVoiceBuffer(){const e={id:this.traceId,event_type:s.WebsocketsEventType.INPUT_AUDIO_BUFFER_CLEAR};this.sendCommand(e)}breakAnswer(){if(1===this.currentAgentStatus){const e={id:this.traceId,event_type:s.WebsocketsEventType.CONVERSATION_CHAT_CANCEL};this.sendCommand(e)}}startVoiceChat(){return T(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=`${s.COZE_CN_BASE_WS_URL}/v1/chat?bot_id=${this.botid}&authorization=Bearer ${t}`;this.socket=new g.default(i);let n=!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 s.WebsocketsEventType.CHAT_CREATED:this.ChatCreated(e);break;case s.WebsocketsEventType.CHAT_UPDATED:this.emit(s.WebsocketsEventType.CHAT_UPDATED,i);break;case s.WebsocketsEventType.CONVERSATION_CHAT_CREATED:this.currentAgentStatus=1,this.emit(s.WebsocketsEventType.CONVERSATION_CHAT_CREATED,i);break;case s.WebsocketsEventType.CONVERSATION_CHAT_IN_PROGRESS:this.emit(s.WebsocketsEventType.CONVERSATION_CHAT_IN_PROGRESS,i);break;case s.WebsocketsEventType.CONVERSATION_MESSAGE_DELTA:this.emit(s.WebsocketsEventType.CONVERSATION_MESSAGE_DELTA,i);break;case s.WebsocketsEventType.CONVERSATION_AUDIO_SENTENCE_START:if(n&&e.playPrologue){this.chatContent.push({role:s.RoleType.Assistant,content:i.data.text});const e=i.data.text.length;this.startWaitingUserSpeaking(e/4),n=!1}this.emit(s.WebsocketsEventType.CONVERSATION_AUDIO_SENTENCE_START,i);break;case s.WebsocketsEventType.CONVERSATION_AUDIO_DELTA:this.emit(s.WebsocketsEventType.CONVERSATION_AUDIO_DELTA,i),this.combineTTS&&i.data.content&&this.chatVoiceBuffer.push(Buffer.from(i.data.content,"base64"));break;case s.WebsocketsEventType.CONVERSATION_MESSAGE_COMPLETED:if(this.emit(s.WebsocketsEventType.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 s.WebsocketsEventType.CONVERSATION_AUDIO_COMPLETED:this.emit(s.WebsocketsEventType.CONVERSATION_AUDIO_COMPLETED,i);break;case s.WebsocketsEventType.CONVERSATION_CHAT_COMPLETED:this.currentAgentStatus=0,this.emit(s.WebsocketsEventType.CONVERSATION_CHAT_COMPLETED,i);break;case s.WebsocketsEventType.CONVERSATION_CHAT_CANCELED:this.currentAgentStatus=0,this.emit(s.WebsocketsEventType.CONVERSATION_CHAT_CANCELED,i),this.startWaitingUserSpeaking();break;case s.WebsocketsEventType.CONVERSATION_CHAT_FAILED:this.currentAgentStatus=0,this.emit(s.WebsocketsEventType.CONVERSATION_CHAT_FAILED,i);break;case s.WebsocketsEventType.INPUT_AUDIO_BUFFER_COMPLETED:this.emit(s.WebsocketsEventType.INPUT_AUDIO_BUFFER_COMPLETED,i);break;case s.WebsocketsEventType.INPUT_AUDIO_BUFFER_CLEAR:this.emit(s.WebsocketsEventType.INPUT_AUDIO_BUFFER_CLEAR,i);break;case s.WebsocketsEventType.ERROR:this.emit(s.WebsocketsEventType.ERROR,i);break;case s.WebsocketsEventType.INPUT_AUDIO_BUFFER_SPEECH_STARTED:this.emit(s.WebsocketsEventType.INPUT_AUDIO_BUFFER_SPEECH_STARTED,i);break;case s.WebsocketsEventType.INPUT_AUDIO_BUFFER_SPEECH_STOPPED:this.emit(s.WebsocketsEventType.INPUT_AUDIO_BUFFER_SPEECH_STOPPED,i);break;case s.WebsocketsEventType.CONVERSATION_AUDIO_TRANSCRIPT_UPDATE:this.emit(s.WebsocketsEventType.CONVERSATION_AUDIO_TRANSCRIPT_UPDATE,i),this.waitingTimer&&this.clearWaitingTimer();break;case s.WebsocketsEventType.CONVERSATION_AUDIO_TRANSCRIPT_COMPLETED:this.emit(s.WebsocketsEventType.CONVERSATION_AUDIO_TRANSCRIPT_COMPLETED,i);break;case s.WebsocketsEventType.CONVERSATION_CHAT_REQUIRES_ACTION:this.emit(s.WebsocketsEventType.CONVERSATION_CHAT_REQUIRES_ACTION,i)}}),{successed:!0}})}ChatCreated(e={}){this.emit(s.WebsocketsEventType.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:s.WebsocketsEventType.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 N=["[AUTHORIZATION NEEDED]","[AUTHORIZATION ERROR]","[BODY ERROR]","[REQUEST ERROR]"];class I extends k{constructor(e,t,s={}){super(e,s),this.proxySetting=t}chatRequest(e,t,s){return T(this,void 0,void 0,function*(){const i=Object.assign({headers:{"Content-Type":"application/json",authorization:`Bearer ${this.apiKey}`},method:"post",url:this.proxySetting.serviceurl,data:{chatText:e,option:t}},s);return(yield _.default(i)).data})}chatRequestInStream(e,t,s,i){return T(this,void 0,void 0,function*(){let n,o=Math.ceil(Math.random()*((new Date).getTime()*Math.random())/1e3);const r={headers:{"Content-Type":"application/json",authorization:`Bearer ${this.apiKey}`},method:"post",url:this.proxySetting.serviceurl+"/stream",data:{messages:e,option:t,axiosOption:i},responseType:"stream"};let a="";_.default(r).then(e=>{e.data.on("data",e=>{if(n=e.toString(),n){if(N.includes(n))return this.emit("requesterror",{successed:!1,requestid:o,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),s);this.emit(e.finish_reason?"chatdone":"chattext",e)}catch(e){break}}})}).catch(e=>{this.emit("requesterror",{successed:!1,requestid:o,error:"Axios Error : "+e})})})}}class R extends b{constructor(e,t,s={}){super(e,s),this.azureSetting=t,this.azureSetting.endpoint.toLowerCase().startsWith("https://")||(this.azureSetting.endpoint="https://"+this.azureSetting.endpoint)}createOpenAI(e){return new u.OpenAIClient(this.azureSetting.endpoint,new u.AzureKeyCredential(e))}get EmbeddingUrl(){return`${this.azureSetting.endpoint}/openai/deployments/${this.embeddingmodel||"openai-embedding-ada-002"}/embeddings?api-version=2022-12-01`}getTextEmbedding(e){return T(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 T(this,arguments,void 0,function*(e,t,s={}){if(!e)return{successed:!1,error:{errcode:2,errmsg:"缺失聊天的内容"}};this.aiApi||(this.aiApi=this.createOpenAI(this.apiKey));let i="string"==typeof e?[{role:"user",content:e}]:e;try{const e=yield this.aiApi.getChatCompletions((null==t?void 0:t.model)||this.chatModel,i,{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:s,completionTokens:n,totalTokens:o}=e.usage;let r=[];for(const t of e.choices){const{index:e,finishReason:s,message:i}=t;r.push({index:e,finish_reason:s,message:i})}return{successed:!0,message:r,usage:{prompt_tokens:s,completion_tokens:n,total_tokens:o}}}catch(e){return console.log("result is error ",e),{successed:!1,error:e}}})}chatRequestInStream(e,t,s,i){return T(this,void 0,void 0,function*(){var n,o,r,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;i=Object.assign({},i||{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 i=[];try{for(var T,f=!0,E=y(e);!(n=(T=yield E.next()).done);f=!0){a=T.value,f=!1;const e=a;for(const t of e.choices){const{finishReason:e,index:n}=t,o=null===(c=t.delta)||void 0===c?void 0:c.toolCalls;if(o&&o.length)h=o[0].index,m=1,h!==g?(_.push({id:o[0].id,type:"function",function:{name:o[0].function.name,arguments:o[0].function.arguments}}),g=h):_[g].function.arguments+=o[0].function.arguments;else{const e=null===(u=t.delta)||void 0===u?void 0:u.content;i.push(e)}let r={successed:!0,requestid:p,segment:null===(l=t.delta)||void 0===l?void 0:l.content,text:i.join(""),finish_reason:e,index:n,has_tool_calls:m,tool_calls:_};s&&(r=Object.assign({},r,s)),this.emit(e?"chatdone":"chattext",r)}}}catch(e){o={error:e}}finally{try{f||n||!(r=E.return)||(yield r.call(E))}finally{if(o)throw o.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 w(){return T(this,arguments,void 0,function*(e={}){e.data||(e.data=e.body);try{return{successed:!0,data:(yield _.default(e)).data}}catch(e){return{successed:!1,error:e,data:e}}})}function x(e){return new Promise(t=>setTimeout(t,e))}class D extends E{constructor(e,t){super(),this.apikey=e,this.agent=t}chatRequest(e,t){return T(this,arguments,void 0,function*(e,t,s={}){var i;if(!e)return{successed:!1,error:"no text in chat"};const n="object"==typeof e?null!==(i=e.text)&&void 0!==i?i:e.content:e;s=Object.assign({},s||{timeout:6e4});const o=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}},s),r=yield w(o);if(!r.successed||r.data.code)return{successed:!1,error:"failed"};const{answer:a,session_id:c}=r.data.data;return{successed:!0,message:[{message:{content:a}}],session_id:c}})}chatRequestInStream(e,t,s,i){return T(this,void 0,void 0,function*(){var n,o,r,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;i=Object.assign({},i||{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"},i),c=yield _.default(e),g=l.Readable.from(c.data);let T,f=0,E="",v=[],b=[];try{for(var h,p=!0,m=y(g);!(n=(h=yield m.next()).done);p=!0){a=h.value,p=!1;const e=a;let t=null,i="";try{i=e.toString().split("data:"),i.length&&(i=i[i.length-1]+""),t=JSON.parse(v.join("")+i)}catch(e){v.push(i);continue}v=[];const{answer:n,running_status:o,reference:r}=t.data;if(!0===o)continue;const c=n?n.replace(E,""):"";E=null!=n?n:E,T||(T=t.data.session_id),r&&r.chunks&&r.chunks.length&&(b=b.concat(r.chunks));const u=!0===t.data;let l={successed:!0,requestid:d,segment:c,chunks:b,text:E,finish_reason:u?"stop":null,index:f++,session_id:T};s&&(l=Object.assign({},l,s)),this.emit(u?"chatdone":"chattext",l)}}catch(e){o={error:e}}finally{try{p||n||!(r=m.return)||(yield r.call(m))}finally{if(o)throw o.error}}return{successed:!0,requestid:d}}catch(e){}})}}class P extends E{constructor(e,t={}){super(),this.apiOption={},this.apiKey=e,this.apiOption=t}getTextEmbedding(e){return T(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 s=Array.isArray(e)?e:[e],i={method:"post",headers:{"Content-Type":"application/json",authorization:`Bearer ${this.apiKey}`},data:{model:t.model||this.apiOption.embedding,input:s},url:"https://ark.cn-beijing.volces.com/api/v3/embeddings"};try{return{successed:!0,embedding:(yield w(i)).data}}catch(e){return{successed:!1,error:e}}})}chatRequest(e,t){return T(this,arguments,void 0,function*(e,t,s={}){if(!e)return{successed:!1,error:{errcode:2,errmsg:"缺失聊天的内容"}};const i=this.assembleApiParams(e,!1,t,s);try{const e=yield w(i);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,s,i={}){let n="string"==typeof e?[{role:"user",content:e}]:e,o={};((null==s?void 0:s.temperature)||this.apiOption.temperature)&&(o.temperature=Number((null==s?void 0:s.temperature)||this.apiOption.temperature)),o.max_tokens=Number((null==s?void 0:s.maxtoken)||this.apiOption.maxtoken),((null==s?void 0:s.top_p)||this.apiOption.top_p)&&(o.top_p=Number((null==s?void 0:s.top_p)||this.apiOption.top_p)),((null==s?void 0:s.presence_penalty)||this.apiOption.presence_penalty)&&(o.presence_penalty=Number((null==s?void 0:s.presence_penalty)||this.apiOption.presence_penalty)),((null==s?void 0:s.frequency_penalty)||this.apiOption.frequency_penalty)&&(o.frequency_penalty=Number((null==s?void 0:s.frequency_penalty)||this.apiOption.frequency_penalty)),((null==s?void 0:s.top_logprobs)||this.apiOption.top_logprobs)&&(o.logprobs=!0,o.top_logprobs=Number((null==s?void 0:s.top_logprobs)||this.apiOption.top_logprobs)),o.tools=1===(null==s?void 0:s.enableToolCall)&&(null==s?void 0:s.tools)?s.tools:void 0,o.tool_choice=1===(null==s?void 0:s.enableToolCall)?"auto":void 0;const r=Object.assign(Object.assign({},i),{method:"post",headers:{"Content-Type":"application/json",authorization:`Bearer ${this.apiKey}`},data:Object.assign(Object.assign({model:(null==s?void 0:s.model)||this.apiOption.model},o),{messages:n,stream:t}),url:"https://ark.cn-beijing.volces.com/api/v3/chat/completions"});return t&&(r.responseType="stream"),r}chatRequestInStream(e,t,s,i){return T(this,void 0,void 0,function*(){e||this.emit("chaterror",{successed:!1,error:"no text in chat"}),i=Object.assign({},i||{timeout:1e4});const n=this.assembleApiParams(e,!0,t,i);let o=Math.ceil(Math.random()*((new Date).getTime()*Math.random())/1e3),r=[];try{return function(e={},t){e.data||(e.data=e.body),_.default(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: /),o,r,s)}),{successed:!0,requestid:o}}catch(e){return this.emit("requesterror",{successed:!1,requestid:o,error:"call axios faied "+e}),{successed:!1,requestid:o}}})}processChunkData(e,t,s,i){let n,o=0,r=-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,o=1,n!==r?(a.push({id:m[0].id,type:"function",function:{name:m[0].function.name,arguments:m[0].function.arguments}}),r=n):a[r].function.arguments+=m[0].function.arguments):s.push(p);let g={successed:!0,requestid:t,segment:p,text:s.join(""),finish_reason:l,index:d,usage:h,has_tool_calls:o,tool_calls:a};i&&(g=Object.assign({},g,i)),this.emit(l?"chatdone":"chattext",g)}}}class U{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 T(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 T(this,void 0,void 0,function*(){try{const e="api.coze.cn",t=m.default.join(process.cwd(),this.pemfile);if(!p.default.existsSync(t))return{successed:!1,error:`${t} not existed`};const i=p.default.readFileSync(t).toString(),n=yield s.getJWTToken({baseURL:s.COZE_CN_BASE_URL,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 W=new Map,V={OPENAI:"openai",OPENAIPROXY:"openaiproxy",MICROSOFT:"microsoft",AIMP:"aimp",COZE:"coze",DOUBAO:"doubao"};var q=Object.freeze({__proto__:null,GptProviderEnum:V,createGpt:function(e,t,s){let{model:i,agentid:n,maxtoken:o,temperature:r,serviceurl:a,endpoint:c,engine:u,version:l,embedding:d,top_p:h,presence_penalty:p,frequency_penalty:m}=s||{};switch(e){case V.OPENAI:return new k(t+"",{model:i,maxtoken:o,temperature:r,embedding:d,top_p:h,presence_penalty:p,frequency_penalty:m});case V.OPENAIPROXY:return new I(t+"",{serviceurl:a},{model:i,maxtoken:o,temperature:r,embedding:d,top_p:h,presence_penalty:p,frequency_penalty:m});case V.MICROSOFT:return new R(t+"",{endpoint:c,engine:u,version:l},{model:i,maxtoken:o,temperature:r,embedding:d,top_p:h,presence_penalty:p,frequency_penalty:m});case V.AIMP:return new D(t+"",{endpoint:c,agentid:n});case V.COZE:let e=W.get(t+"");return e||(e=new U(t+"",s),W.set(t+"",e)),!0===s.voicebot?new S(e,s):new A(e,s);case V.DOUBAO:return new P(t+"",{model:i,maxtoken:o,temperature:r,top_p:h,presence_penalty:p,frequency_penalty:m,embedding:d});default:return null}}});const B=["。","!","!","?","?",";",";",":",":","\n"];class M extends l.EventEmitter{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=a.randomUUID(),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:s.WebsocketsEventType.INPUT_TEXT_BUFFER_APPEND,data:{delta:e}};this.sendCommand("tts",t),this.currentTTSConent.push(e),this.emit(s.WebsocketsEventType.CONVERSATION_AUDIO_SENTENCE_START,{event_type:s.WebsocketsEventType.CONVERSATION_AUDIO_SENTENCE_START,data:{text:this.currentTTSConent.join("")}})}if(t){const e={id:this.traceId,event_type:s.WebsocketsEventType.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"),n={id:this.traceId,event_type:s.WebsocketsEventType.INPUT_AUDIO_BUFFER_APPEND,data:{delta:i}};this.sendCommand("asr",n),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(s.WebsocketsEventType.INPUT_AUDIO_BUFFER_SPEECH_STOPPED,{event_type:s.WebsocketsEventType.INPUT_AUDIO_BUFFER_SPEECH_STOPPED}),this.submitVoiceBuffer())}sendChatMessage(e,t={}){this.agentBot.chatRequestInStream(e,t)}submitVoiceBuffer(){const e={id:this.traceId,event_type:s.WebsocketsEventType.INPUT_AUDIO_BUFFER_COMPLETE};this.sendCommand("asr",e)}clearVoiceBuffer(){const e={id:this.traceId,event_type:s.WebsocketsEventType.INPUT_AUDIO_BUFFER_CLEAR};this.sendCommand("asr",e)}breakAnswer(e=!1){this.breaked=!0;const t={id:this.traceId,event_type:s.WebsocketsEventType.INPUT_TEXT_BUFFER_COMPLETE};this.sendCommand("tts",t),this.emit(s.WebsocketsEventType.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:s.WebsocketsEventType.SPEECH_UPDATE,data:{output_audio:this.chatconfig.output_audio_config}}:{id:this.traceId,event_type:s.WebsocketsEventType.TRANSCRIPTIONS_UPDATE,data:{input_audio:this.chatconfig.input_audio_config,asr_config:this.chatconfig.asr_config}};this.sendCommand(e,i)}decorateUserMessage(e){return T(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 T(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 s=e.length-1;s>=5;s--){const i=e[s];if(t.has(i))return{index:s,text:e.slice(0,s+1),remain:e.slice(s+1)}}return{index:-1}}registerAgentEvent(){return T(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 s=this.getLastPunctuationInfo(e.join(""));s.index>0&&(this.sendText2Tts(s.text,!1),e=s.remain?[s.remain]:[])}),this.agentBot.on("chatdone",t=>this.sendText2Tts(e.join(""),!0)))})}processAsrResult(e){return T(this,arguments,void 0,function*(e,t=!1){return e})}processChatResult(e,t,i,n){return T(this,arguments,void 0,function*(e,t,i,n,o="pcm"){const r=(new Date).getTime();return{role:e,content:t,start:n,end:r,duration:(r-n)/1e3,type:e===s.RoleType.User?"question":"answer"}})}registerAsrEvent(e){return T(this,void 0,void 0,function*(){let t="";e.on("open",()=>console.log("Connected to asr server.")),e.on("message",e=>T(this,void 0,void 0,function*(){const i=JSON.parse(e.toString());switch(i.event_type){case s.WebsocketsEventType.TRANSCRIPTIONS_CREATED:this.configurationConnector("asr"),this.emit(s.WebsocketsEventType.TRANSCRIPTIONS_CREATED,i);break;case s.WebsocketsEventType.TRANSCRIPTIONS_UPDATED:this.connectors.asr=!0;break;case s.WebsocketsEventType.INPUT_AUDIO_BUFFER_COMPLETED:this.lastGetAsrChangedTime=0,this.emit(s.WebsocketsEventType.INPUT_AUDIO_BUFFER_COMPLETED,i);break;case s.WebsocketsEventType.TRANSCRIPTIONS_MESSAGE_UPDATE:this.lastGetAsrChangedTime=(new Date).getTime(),t=yield this.processAsrResult(i.data.content),this.emit(s.WebsocketsEventType.TRANSCRIPTIONS_MESSAGE_UPDATE,{content:t});break;case s.WebsocketsEventType.TRANSCRIPTIONS_MESSAGE_COMPLETED:t=yield this.processAsrResult(t,!0);const e=yield this.processChatResult(s.RoleType.User,t,Buffer.concat([...this.userTalkBuffer]),this.userTalkStart);this.chatContent.push(e),this.emit(s.WebsocketsEventType.CONVERSATION_MESSAGE_COMPLETED,{event_type:s.WebsocketsEventType.CONVERSATION_MESSAGE_COMPLETED,data:e}),this.chatVoiceBuffer.push(Buffer.concat([...this.userTalkBuffer])),this.userTalkBuffer=[],this.userTalkStart=0,this.emit(s.WebsocketsEventType.TRANSCRIPTIONS_MESSAGE_COMPLETED,e);const n=yield this.decorateUserMessage(t);t="",n.answerByAgent?(this.breaked=!1,this.createTtsConnector(null,n.emotionInfo),this.agentBot.chatRequestInStream(n.question,n.chatparams)):n.answerContent&&this.streamOutputContent(n.answerContent,n.chatparams);break;case s.WebsocketsEventType.INPUT_AUDIO_BUFFER_CLEARED:this.emit(s.WebsocketsEventType.INPUT_AUDIO_BUFFER_CLEARED,i);break;case s.WebsocketsEventType.ERROR:this.close(),this.emit(s.WebsocketsEventType.ERROR,{type:"asr",error:i.data})}}))})}streamOutputContent(e,t){this.createTtsConnector(null,t);const s=function(e){const t=new RegExp("(["+B.join("")+"])","g");return t.test(e)?e.split(t).reduce((e,t,s,i)=>(s%2==0&&e.push(t+(i[s+1]||"")),e),[]).filter(e=>e.trim().length>0):[e]}(e),i=this;!function e(t=0){setTimeout(()=>{const n=s[t];i.sendText2Tts(n,t===s.length-1),t<s.length-1&&e(t+1)},200)}()}close(){return T(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 T(this,void 0,void 0,function*(){e.on("open",()=>console.log("Connected to tts server.")),e.on("message",e=>T(this,void 0,void 0,function*(){const n=JSON.parse(e.toString());switch(n.event_type){case s.WebsocketsEventType.SPEECH_CREATED:this.configurationConnector("tts",i),this.emit(s.WebsocketsEventType.SPEECH_CREATED,n);break;case s.WebsocketsEventType.SPEECH_UPDATED:this.connectors.tts=!0,t&&this.sendText2Tts(t,!0);break;case s.WebsocketsEventType.INPUT_TEXT_BUFFER_COMPLETED:this.emit(s.WebsocketsEventType.INPUT_TEXT_BUFFER_COMPLETED,n);break;case s.WebsocketsEventType.SPEECH_AUDIO_UPDATE:if(!n.data.delta||this.breaked)return;this.agentTalkStart||(this.agentTalkStart=(new Date).getTime());const e=Buffer.from(n.data.delta,"base64");this.agentTalkBuffer.push(e),this.emit(s.WebsocketsEventType.CONVERSATION_AUDIO_DELTA,e);break;case s.WebsocketsEventType.SPEECH_AUDIO_COMPLETED:const o=yield this.processChatResult(s.RoleType.Assistant,this.currentTTSConent.join(""),Buffer.concat([...this.agentTalkBuffer]),this.agentTalkStart);this.emit(s.WebsocketsEventType.CONVERSATION_MESSAGE_COMPLETED,{event_type:s.WebsocketsEventType.CONVERSATION_MESSAGE_COMPLETED,data:o}),this.emit(s.WebsocketsEventType.CONVERSATION_AUDIO_COMPLETED,o),this.chatVoiceBuffer.push(Buffer.concat([...this.agentTalkBuffer])),this.chatContent.push(o),this.currentTTSConent=[],this.agentTalkBuffer=[],this.agentTalkStart=0,o.content.length>0&&this.startWaitingUserSpeaking(o.content.length/4),this.closeTts();break;case s.WebsocketsEventType.ERROR:this.emit(s.WebsocketsEventType.ERROR,{type:"tts",error:n.data}),this.close()}}))})}startVoiceChat(e){return T(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 U(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)=>T(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(s.WebsocketsEventType.CHAT_UPDATED,{status:t?"playvoice":"startrecord"}),e({successed:!0})}yield x(50)}}))):{successed:!1,error:"获取token失败"}})}createAsrConnector(){const e=`${s.COZE_CN_BASE_WS_URL}/v1/audio/transcriptions?authorization=Bearer ${this.apiToken}`;this.asrSocket=new g.default(e),this.registerAsrEvent(this.asrSocket)}createTtsConnector(e,t){if(this.ttsSocket)return;const i=`${s.COZE_CN_BASE_WS_URL}/v1/audio/speech?authorization=Bearer ${this.apiToken}`;this.ttsSocket=new g.default(i),this.registerTtsEvent(this.ttsSocket,e,t)}sendAsrCommand(e){this.asrSocket&&this.asrSocket.readyState===g.default.OPEN&&this.asrSocket.send("string"==typeof e?e:JSON.stringify(e))}sendTtsCommand(e){this.ttsSocket&&this.ttsSocket.readyState===g.default.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}}Object.defineProperty(exports,"COZE_SOCKET_EVENT_TYPE",{enumerable:!0,get:function(){return s.WebsocketsEventType}}),exports.DualVoiceAgent=M,exports.GptBase=v,exports.GptFactory=q;//# sourceMappingURL=index.js.map
|