UNPKG

stt-sdk

Version:

基于 LLMs 的语音转文本 SDK

95 lines (84 loc) 2.38 kB
import { BaseSTTClient, BaseSTTClientHookType } from "../base/BaseSTTClient.js"; import { RealtimeTranscriber } from "assemblyai"; /** * DeepGram STT 客户端实现 * @author kaiven */ export class AssemblyAIClient extends BaseSTTClient<any> { /** * transcriber */ private rt: RealtimeTranscriber | null = null; /** * 连接初始化处理 */ public onConnectInit() { const token = this.getAccessToken()!; this.rt = new RealtimeTranscriber({ token, }); } /** * 连接错误处理 */ public onConnectError() { if (!this.rt) throw new Error("连接尚未初始化"); this.rt.on("error", (error) => { this.connectErrorHandler(error); }); } /** * 连接关闭处理 */ public onConnectClose() { if (!this.rt) throw new Error("连接尚未初始化"); this.rt.on("close", (_, reason) => { this.connectCloseHandler(reason); }); } /** * 连接打开事件处理 */ public onConnectOpen() { (async (context) => { if (!context.rt) throw new Error("连接尚未初始化"); await context.rt.connect(); context.connectOpenHandler(context.sliceSend); })(this); } /** * 翻译文本接收处理 */ public onTextReceive() { if (!this.rt) throw new Error("连接尚未初始化"); this.rt.on("transcript", (data) => { if (!data.text) { return; } this.textReceiveHandler(data.text); }); } /** * hook注册 */ public addHook<R>(type: BaseSTTClientHookType, func: (...arg: any[]) => Promise<R>): void { this.setHook(type, func); } /** * 关闭连接 */ public closeConnection(): void { if (!this.rt) throw new Error("连接尚未初始化"); this.rt.close(); } /** * 切片发送 */ private sliceSend(data: Blob): void { (async (context) => { if (!context.rt) throw new Error("连接尚未初始化"); const buffer = await data.arrayBuffer(); context.rt.sendAudio(buffer); })(this); } }