UNPKG

doomiaichat

Version:

Doomisoft OpenAI

220 lines 6.96 kB
import GptBase from "./gptbase"; import WebSocket from 'ws'; import { ApiResult, VoiceChatSetting, Voiceformat } from './declare'; import { RoleType } from '@coze/api'; import { EventEmitter } from "stream"; type emotionType = 'neutral' | 'sad' | 'angry' | 'surprised' | 'fear' | 'hate' | 'excited' | 'coldness' | 'neutral' | string; type connectorType = 'asr' | 'tts' | string; export declare class VoiceChat extends EventEmitter { private agentBot; private chatconfig; private asrSocket; private ttsSocket; private chatVoiceBuffer; private chatContent; private apiToken; private waitingTimes; private waitingTimer; private traceId; private userTalkBuffer; private userTalkStart; private agentTalkBuffer; private agentTalkStart; private currentTTSContent; private lastGetAsrChangedTime; private breaked; private connectors; /** * * @param agentBot : 智能体,无所谓用哪个产商的,只要实现了GptBase接口的都可以,支持流式输出内容 * @param asrSetting : 语音识别的配置(目前语音识别和语音合成都需要用流式的,所以必须都用coze) * @param ttsSetting : 语音合成的配置 */ constructor(agentBot: GptBase, chatconfig: VoiceChatSetting); /** * 当前绑定的智能体 */ get AgentBot(): GptBase; /** * 对话的配置 */ get ChatConfig(): VoiceChatSetting; /** * 返回整个对话的语音流(pcm) */ getChatVoiceFile(): Buffer; /** * 清除等待计时器 */ private clearWaitingTimer; /** * 开始等待用户说话 */ private startWaitingUserSpeaking; /** * 获取到整个对话过程内容 * @returns */ getChatContent(): any; /** * 持续发送提交文本生成语音 * @param text */ sendText2Tts(text: string | null | undefined, finished?: boolean): void; /** * 持续发送来自于客户端语音流 * @param originalVoice 原始的录音流 * @param cleanVoice 经过回声消除后的录音流 * 如果继承的子类传入的是第三方的语音识别(非COZE,则此处的方法也需要重写) */ sendVoice(originalVoice: Buffer): void; /** * 手动提交文本对话内容给智能体进行回复 * @param data */ sendChatMessage(content: string, params?: any): void; /** * 提交语音片段 (这个在非server_vad 模式下,如按停止录音按钮。主动提交,Server_vad模式下无效) * 其实就是给服务器一个截止的标记 * @param voiceBuffer */ private submitVoiceBuffer; /** * 手动清除缓冲区的音频 * 同上面submitVoiceBuffer 配套,Server_vad模式下无效 */ clearVoiceBuffer(): void; /** * 打断智能体回复中 */ breakAnswer(end?: boolean): void; /** * 初始化连接器的参数(ASR Or TTS) * @param param */ configurationConnector(type: connectorType, emotionInfo?: { emotion: emotionType; emotionScale: number; emotionContext?: string; }): void; /** * 放出一个事件给继承的子类实现,当用户问完一个问题时,可将问题进行二次处理后再发送给智能体 * @param content : 用户原始说话ASR出来的内容 * @returns { * content:string : 二次加工后给到智能体回答的内容, * agentanswer:是否需要智能体回答, * answercontent?:回答的内容 /// 如果直接返回了这个内容,并且agentanswer:false,说明已经找到答案,直接让TTS说出来就好了 * emotion:回答时应具备的情绪 * emotionScale:情绪的强度 * } */ decorateUserMessage(question: string): Promise<{ question: string; answerByAgent: boolean; chatparams?: any; answerContent?: string; emotionInfo?: { emotion: emotionType; emotionScale: number; emotionContext?: string; }; }>; /** * 根据对话的上下文分析智能体回复的下一句应该保持什么情绪 * @param context * emotion 情感值 * emotionScale 情感强度 * emotionContext 情感分析的上下文 * @returns */ getEmotion(_context: any): Promise<{ emotion: emotionType; emotionScale: number; emotionContext?: string; }>; /** * 流式的TTS也尽量每次送去合成的时候保持一段完整的句子,不能太短 * 获取到最后一个标点符号所在的位置 * @param str * @returns */ getLastPunctuationInfo(str: string): { index: number; text?: string; remain?: string; }; /** * 注册Agent的事件 */ registerAgentEvent(): Promise<void>; /** * 交给外部处理ASR输出的结果 * 如有需要,子类可重写这个方法 * @param content * @returns */ processAsrResult(content: string, _isfinal?: boolean): Promise<string>; /** * 处理添加到对话内容中的内容 * @param role * @param content * @param voice * @param start */ processChatResult(role: RoleType, content: string, _voice: any, start: number, _voiceBufferFormat?: Voiceformat): Promise<any>; /** * 注册Asr的下行事件 */ registerAsrEvent(binder: WebSocket): Promise<void>; /** * 流式的输出固定的文字,模拟智能体的流式输出 * @param content */ private streamOutputContent; /** * 关闭ASR或TTS的连接器 * @param type */ close(type?: connectorType): Promise<void>; private closeAsr; private closeTts; /** * 注册Tts的下行事件 */ registerTtsEvent(binder: WebSocket, speakText?: string | null, emotionInfo?: { emotion: emotionType; emotionScale: number; }): Promise<void>; /** * 监控状态是否设置完成 * @param countTimes */ /** * 开启语音对话(Coze支持双向语音对话流) * @param setting * asrConnector : ASR 设备可以传入第三方的ASR识别程序,但TTS目前只能使用COZE的流式合成 */ startVoiceChat(asrDevice?: any): Promise<ApiResult>; /** * 创建ASR连接器服务 * asr 可以使用其他平台的ASR,所以这个方法可以被重写 */ createAsrConnector(): void; /** * 创建TTS连接器服务 */ private createTtsConnector; /** * 上行指令Asr发送 * @param command */ sendAsrCommand(command: any): void; /** * 上行指令TTS发送 * @param command */ sendTtsCommand(command: any): void; sendCommand(commandType: connectorType, command: any): void; } export {}; //# sourceMappingURL=voicechat.d.ts.map