UNPKG

stt-sdk

Version:

基于 LLMs 的语音转文本 SDK

99 lines (83 loc) 2.63 kB
import { createClient, ListenLiveClient, LiveTranscriptionEvents, type LiveSchema } from "@deepgram/sdk"; import { BaseSTTClient, BaseSTTClientHookType } from "../base/BaseSTTClient.js"; export type DeepGramClientConfig = LiveSchema; /** * DeepGram STT 客户端实现 * @author kaiven */ export class DeepGramClient extends BaseSTTClient<DeepGramClientConfig> { /** * 连接对象 */ private connection: null | ListenLiveClient = null; /** * 连接初始化处理 */ public onConnectInit() { const token = this.getAccessToken()!; const dg = createClient({ accessToken: token }); const config = this.getClientConfig(); this.connection = dg.listen.live(config ?? this.getDefaultClientConfig()); } /** * 连接错误处理 */ public onConnectError() { if (!this.connection) throw new Error("连接尚未初始化"); this.connection.on(LiveTranscriptionEvents.Error, this.connectErrorHandler); } /** * 连接关闭处理 */ public onConnectClose() { if (!this.connection) throw new Error("连接尚未初始化"); this.connection.on(LiveTranscriptionEvents.Close, this.connectCloseHandler); } /** * 连接打开事件处理 */ public onConnectOpen() { if (!this.connection) throw new Error("连接尚未初始化"); this.connection.on(LiveTranscriptionEvents.Open, () => { this.connectOpenHandler((data) => this.sliceSend(data)); }); } /** * 翻译文本接收处理 */ public onTextReceive() { this.connection?.on(LiveTranscriptionEvents.Transcript, (data: any) => { const text = data?.channel?.alternatives?.[0]?.transcript || ""; if (!text) return; this.textReceiveHandler(text); }); } /** * hook注册 */ public addHook<R>(type: BaseSTTClientHookType, func: (...arg: any[]) => Promise<R>): void { this.setHook(type, func); } /** * 关闭连接 */ public closeConnection(): void { this.connection?.disconnect(); } /** * 客户端默认配置 */ private getDefaultClientConfig(): DeepGramClientConfig { return { model: "nova-3", language: "en-US", smart_format: true } } /** * 切片发送 */ private sliceSend(data: Blob): void { this.connection?.send(data); } }