UNPKG

@coze/realtime-api

Version:

A powerful real-time communication SDK for voice interactions with Coze AI bots | 扣子官方实时通信 SDK,用于与 Coze AI bots 进行语音交互

143 lines (142 loc) 7.1 kB
import { type ScreenConfig, type AudioPropertiesConfig } from '@volcengine/rtc'; import { type GetToken } from '@coze/api'; import * as RealtimeUtils from './utils'; import { RealtimeEventHandler, EventNames } from './event-handler'; import { RealtimeAPIError, RealtimeError } from './error'; export interface VideoConfig { videoOnDefault?: boolean /** optional, Whether to turn on video by default, defaults to true */; renderDom?: string /** optional, The DOM element to render the video stream to */; videoInputDeviceId?: string /** optional, The device ID of the video input device to use */; screenConfig?: ScreenConfig; } export interface RealtimeClientConfig { accessToken: GetToken /** required, Access Token */; botId: string /** required, Bot Id */; voiceId?: string /** optional, Voice Id */; conversationId?: string /** optional, Conversation Id */; baseURL?: string /** optional, defaults to "https://api.coze.cn" */; debug?: boolean /** optional, defaults to false */; /** Whether Personal Access Tokens (PAT) are allowed in browser environments */ allowPersonalAccessTokenInBrowser?: boolean; /** Whether to mute by default, defaults to false * If set to true, audio streams will not be automatically published and subscribed */ audioMutedDefault?: boolean; connectorId: string /** required, Connector Id */; suppressStationaryNoise?: boolean /** optional, Suppress stationary noise, defaults to false */; suppressNonStationaryNoise?: boolean /** optional, Suppress non-stationary noise, defaults to false */; videoConfig?: VideoConfig /** optional, Video configuration */; isAutoSubscribeAudio?: boolean /** optional, Whether to automatically subscribe to bot reply audio streams, defaults to true */; } declare class RealtimeClient extends RealtimeEventHandler { private _config; private _client; isConnected: boolean; private _api; private _isTestEnv; _isSupportVideo: boolean; /** * Constructor for initializing a RealtimeClient instance. * * 构造函数,初始化RealtimeClient实例。 * * @param config * @param config.accessToken - Required, Access Token. | * 必填,Access Token。 * @param config.botId - Required, Bot Id. | * 必填,Bot Id。 * @param config.voiceId - Optional, Voice Id. | * 可选,音色Id。 * @param config.conversationId - Optional, Conversation Id. | * 可选,会话Id。 * @param config.baseURL - Optional, defaults to "https://api.coze.cn". | * 可选,默认值为 "https://api.coze.cn"。 * @param config.debug - Optional, defaults to false. * 可选,默认值为 false。 * @param config.allowPersonalAccessTokenInBrowser * - Optional, whether to allow personal access tokens in browser environment. | * 可选,是否允许在浏览器环境中使用个人访问令牌。 * @param config.audioMutedDefault - Optional, whether audio is muted by default, defaults to false. | * 可选,默认是否静音,默认值为 false。 * @param config.connectorId - Required, Connector Id. | * 必填,渠道 Id。 * @param config.suppressStationaryNoise - Optional, suppress stationary noise, defaults to false. | * 可选,默认是否抑制静态噪声,默认值为 false。 * @param config.suppressNonStationaryNoise - Optional, suppress non-stationary noise, defaults to false. | * 可选,默认是否抑制非静态噪声,默认值为 false。 * @param config.isAutoSubscribeAudio - Optional, whether to automatically subscribe to bot reply audio streams, defaults to true. | * @param config.videoConfig - Optional, Video configuration. | * 可选,视频配置。 * @param config.videoConfig.videoOnDefault - Optional, Whether to turn on video by default, defaults to true. | * 可选,默认是否开启视频,默认值为 true。 * @param config.videoConfig.renderDom - Optional, The DOM element to render the video stream to. | * 可选,渲染视频流的 DOM 元素。 * @param config.videoConfig.videoInputDeviceId - Optional, The device ID of the video input device to use. | * 可选,视频输入设备的设备 ID。 * @param config.videoConfig.screenConfig - Optional, Screen share configuration if videoInputDeviceId is 'screenShare' see https://www.volcengine.com/docs/6348/104481#screenconfig for more details. | * 可选,屏幕共享配置,如果 videoInputDeviceId 是 'screenShare',请参考 https://www.volcengine.com/docs/6348/104481#screenconfig 了解更多详情。 */ constructor(config: RealtimeClientConfig); /** * en: Establish a connection to the Coze API and join the room * * zh: 建立与 Coze API 的连接并加入房间 */ connect(): Promise<void>; /** * en: Interrupt the current conversation * * zh: 中断当前对话 */ interrupt(): Promise<void>; /** * en: Disconnect from the current session * * zh: 断开与当前会话的连接 */ disconnect(): Promise<void>; /** * en: Send a message to the bot * * zh: 发送消息给Bot */ sendMessage(message: Record<string, unknown>): Promise<void>; /** * en: Enable or disable audio * * zh: 启用或禁用音频 */ setAudioEnable(isEnable: boolean): Promise<void>; setVideoEnable(isEnable: boolean): Promise<void>; /** * en: Enable audio properties reporting (debug mode only) * * zh: 启用音频属性报告(仅限调试模式) */ enableAudioPropertiesReport(config?: AudioPropertiesConfig): boolean; /** * en: Start audio playback device test (debug mode only) * * zh: 开始音频播放设备测试(仅限调试模式) */ startAudioPlaybackDeviceTest(): Promise<void>; /** * en: Stop audio playback device test (debug mode only) * * zh: 停止音频播放设备测试(仅限调试模式) */ stopAudioPlaybackDeviceTest(): void; /** * en: Set the audio input device * * zh: 设置音频输入设备 */ setAudioInputDevice(deviceId: string): Promise<void>; /** * en: Set the audio output device * * zh: 设置音频输出设备 */ setAudioOutputDevice(deviceId: string): Promise<void>; setVideoInputDevice(deviceId: string): Promise<void>; } export { RealtimeUtils, RealtimeClient, RealtimeAPIError, RealtimeError, EventNames, };