@coze/realtime-api 1.1.1 → 1.2.0

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.
@@ -1,145 +1,3 @@
1
- export declare enum EventNames {
2
- /**
3
- * en: All events
4
- * zh: 所有事件
5
- */
6
- ALL = "realtime.event",
7
- /**
8
- * en: All client events
9
- * zh: 所有客户端事件
10
- */
11
- ALL_CLIENT = "client.*",
12
- /**
13
- * en: All server events
14
- * zh: 所有服务端事件
15
- */
16
- ALL_SERVER = "server.*",
17
- /**
18
- * en: Client connected
19
- * zh: 客户端连接
20
- */
21
- CONNECTED = "client.connected",
22
- /**
23
- * en: Client connecting
24
- * zh: 客户端连接中
25
- */
26
- CONNECTING = "client.connecting",
27
- /**
28
- * en: Client interrupted
29
- * zh: 客户端中断
30
- */
31
- INTERRUPTED = "client.interrupted",
32
- /**
33
- * en: Client disconnected
34
- * zh: 客户端断开
35
- */
36
- DISCONNECTED = "client.disconnected",
37
- /**
38
- * en: Client audio unmuted
39
- * zh: 客户端音频未静音
40
- */
41
- AUDIO_UNMUTED = "client.audio.unmuted",
42
- /**
43
- * en: Client audio muted
44
- * zh: 客户端音频静音
45
- */
46
- AUDIO_MUTED = "client.audio.muted",
47
- /**
48
- * en: Client video on
49
- * zh: 客户端视频开启
50
- */
51
- VIDEO_ON = "client.video.on",
52
- /**
53
- * en: Client video off
54
- * zh: 客户端视频关闭
55
- */
56
- VIDEO_OFF = "client.video.off",
57
- /**
58
- * en: Client video event
59
- * zh: 客户端视频事件
60
- */
61
- PLAYER_EVENT = "client.video.event",
62
- /**
63
- * en: Client error
64
- * zh: 客户端错误
65
- */
66
- ERROR = "client.error",
67
- /**
68
- * en: Audio noise reduction enabled
69
- * zh: 抑制平稳噪声
70
- */
71
- SUPPRESS_STATIONARY_NOISE = "client.suppress.stationary.noise",
72
- /**
73
- * en: Suppress non-stationary noise
74
- * zh: 抑制非平稳噪声
75
- */
76
- SUPPRESS_NON_STATIONARY_NOISE = "client.suppress.non.stationary.noise",
77
- /**
78
- * en: Audio input device changed
79
- * zh: 音频输入设备改变
80
- */
81
- AUDIO_INPUT_DEVICE_CHANGED = "client.input.device.changed",
82
- /**
83
- * en: Audio output device changed
84
- * zh: 音频输出设备改变
85
- */
86
- AUDIO_OUTPUT_DEVICE_CHANGED = "client.output.device.changed",
87
- /**
88
- * en: Video input device changed
89
- * zh: 视频输入设备改变
90
- */
91
- VIDEO_INPUT_DEVICE_CHANGED = "client.video.input.device.changed",
92
- /**
93
- * en: Network quality changed
94
- * zh: 网络质量改变
95
- */
96
- NETWORK_QUALITY = "client.network.quality",
97
- /**
98
- * en: Bot joined
99
- * zh: Bot 加入
100
- */
101
- BOT_JOIN = "server.bot.join",
102
- /**
103
- * en: Bot left
104
- * zh: Bot 离开
105
- */
106
- BOT_LEAVE = "server.bot.leave",
107
- /**
108
- * en: Audio speech started
109
- * zh: 开始说话
110
- */
111
- AUDIO_AGENT_SPEECH_STARTED = "server.audio.agent.speech_started",
112
- /**
113
- * en: Audio speech stopped
114
- * zh: 停止说话
115
- */
116
- AUDIO_AGENT_SPEECH_STOPPED = "server.audio.agent.speech_stopped",
117
- /**
118
- * en: Server error
119
- * zh: 服务端错误
120
- */
121
- SERVER_ERROR = "server.error",
122
- /**
123
- * en: User speech started
124
- * zh: 用户开始说话
125
- */
126
- AUDIO_USER_SPEECH_STARTED = "server.audio.user.speech_started",
127
- /**
128
- * en: User speech stopped
129
- * zh: 用户停止说话
130
- */
131
- AUDIO_USER_SPEECH_STOPPED = "server.audio.user.speech_stopped",
132
- /**
133
- * en: User successfully enters the room
134
- * zh: 用户成功进入房间后,会收到该事件
135
- */
136
- SESSION_CREATED = "server.session.created",
137
- /**
138
- * en: Session updated
139
- * zh: 会话更新
140
- */
141
- SESSION_UPDATE = "server.session.update"
142
- }
143
1
  export type EventCallback = (eventName: string, event: unknown) => void;
144
2
  export declare class RealtimeEventHandler {
145
3
  private eventHandlers;
@@ -0,0 +1,48 @@
1
+ import { type AudioPropertiesConfig, type IRTCEngine, type NetworkQuality, type onUserJoinedEvent, type onUserLeaveEvent, type UserMessageEvent } from '@volcengine/rtc';
2
+ import { RealtimeEventHandler } from './event-handler';
3
+ import { type VideoConfig } from '.';
4
+ export declare class EngineClient extends RealtimeEventHandler {
5
+ private engine;
6
+ private joinUserId;
7
+ private _AIAnsExtension;
8
+ private _isSupportVideo;
9
+ private _videoConfig?;
10
+ private _streamIndex?;
11
+ private _roomUserId?;
12
+ constructor(appId: string, debug?: boolean, isTestEnv?: boolean, isSupportVideo?: boolean, videoConfig?: VideoConfig);
13
+ bindEngineEvents(): void;
14
+ removeEventListener(): void;
15
+ _parseMessage(event: UserMessageEvent): any;
16
+ handleMessage(event: UserMessageEvent): void;
17
+ handleEventError(e: unknown): void;
18
+ handleUserJoin(event: onUserJoinedEvent): void;
19
+ handleUserLeave(event: onUserLeaveEvent): void;
20
+ handlePlayerEvent(event: unknown): void;
21
+ handleNetworkQuality(uplinkNetworkQuality: NetworkQuality, downlinkNetworkQuality: NetworkQuality): void;
22
+ joinRoom(options: {
23
+ token: string;
24
+ roomId: string;
25
+ uid: string;
26
+ audioMutedDefault?: boolean;
27
+ videoOnDefault?: boolean;
28
+ isAutoSubscribeAudio?: boolean;
29
+ }): Promise<void>;
30
+ setAudioInputDevice(deviceId: string): Promise<void>;
31
+ setAudioOutputDevice(deviceId: string): Promise<void>;
32
+ setVideoInputDevice(deviceId: string, isAutoCapture?: boolean): Promise<void>;
33
+ createLocalStream(userId?: string, videoConfig?: VideoConfig): Promise<void>;
34
+ disconnect(): Promise<void>;
35
+ changeAudioState(isMicOn: boolean): Promise<void>;
36
+ changeVideoState(isVideoOn: boolean): Promise<void>;
37
+ stop(): Promise<void>;
38
+ sendMessage(message: Record<string, unknown>): Promise<void>;
39
+ enableAudioPropertiesReport(config?: AudioPropertiesConfig): void;
40
+ handleLocalAudioPropertiesReport(event: any): void;
41
+ handleRemoteAudioPropertiesReport(event: unknown): void;
42
+ enableAudioNoiseReduction(): Promise<void>;
43
+ initAIAnsExtension(): Promise<void>;
44
+ changeAIAnsExtension(enable: boolean): void;
45
+ startAudioPlaybackDeviceTest(): Promise<void>;
46
+ stopAudioPlaybackDeviceTest(): void;
47
+ getRtcEngine(): IRTCEngine;
48
+ }
@@ -0,0 +1,25 @@
1
+ export declare enum RealtimeError {
2
+ DEVICE_ACCESS_ERROR = "DEVICE_ACCESS_ERROR",
3
+ STREAM_CREATION_ERROR = "STREAM_CREATION_ERROR",
4
+ CONNECTION_ERROR = "CONNECTION_ERROR",
5
+ DISCONNECTION_ERROR = "DISCONNECTION_ERROR",
6
+ INTERRUPT_ERROR = "INTERRUPT_ERROR",
7
+ EVENT_HANDLER_ERROR = "EVENT_HANDLER_ERROR",
8
+ PERMISSION_DENIED = "PERMISSION_DENIED",
9
+ NETWORK_ERROR = "NETWORK_ERROR",
10
+ INVALID_STATE = "INVALID_STATE",
11
+ CREATE_ROOM_ERROR = "CREATE_ROOM_ERROR",
12
+ PARSE_MESSAGE_ERROR = "PARSE_MESSAGE_ERROR",
13
+ HANDLER_MESSAGE_ERROR = "HANDLER_MESSAGE_ERROR"
14
+ }
15
+ export declare const ErrorMessages: Record<RealtimeError, string>;
16
+ export declare class RealtimeAPIError extends Error {
17
+ code: RealtimeError;
18
+ error?: unknown;
19
+ /**
20
+ * @param code - Error code
21
+ * @param message - Error message
22
+ * @param error - Error object
23
+ */
24
+ constructor(code: RealtimeError, message: string, error?: unknown);
25
+ }
@@ -0,0 +1,12 @@
1
+ export type EventCallback = (eventName: string, event: unknown) => void;
2
+ export declare class RealtimeEventHandler {
3
+ private eventHandlers;
4
+ protected _debug: boolean;
5
+ constructor(debug?: boolean);
6
+ clearEventHandlers(): void;
7
+ on(eventName: string, callback: EventCallback): EventCallback;
8
+ off(eventName: string, callback: EventCallback): void;
9
+ private _dispatchToHandlers;
10
+ dispatch(eventName: string, event: unknown, consoleLog?: boolean): void;
11
+ _log(message: string, event?: unknown): void;
12
+ }
@@ -0,0 +1,143 @@
1
+ declare enum EventNames {
2
+ /**
3
+ * en: All events
4
+ * zh: 所有事件
5
+ */
6
+ ALL = "realtime.event",
7
+ /**
8
+ * en: All client events
9
+ * zh: 所有客户端事件
10
+ */
11
+ ALL_CLIENT = "client.*",
12
+ /**
13
+ * en: All server events
14
+ * zh: 所有服务端事件
15
+ */
16
+ ALL_SERVER = "server.*",
17
+ /**
18
+ * en: Client connected
19
+ * zh: 客户端连接
20
+ */
21
+ CONNECTED = "client.connected",
22
+ /**
23
+ * en: Client connecting
24
+ * zh: 客户端连接中
25
+ */
26
+ CONNECTING = "client.connecting",
27
+ /**
28
+ * en: Client interrupted
29
+ * zh: 客户端中断
30
+ */
31
+ INTERRUPTED = "client.interrupted",
32
+ /**
33
+ * en: Client disconnected
34
+ * zh: 客户端断开
35
+ */
36
+ DISCONNECTED = "client.disconnected",
37
+ /**
38
+ * en: Client audio unmuted
39
+ * zh: 客户端音频未静音
40
+ */
41
+ AUDIO_UNMUTED = "client.audio.unmuted",
42
+ /**
43
+ * en: Client audio muted
44
+ * zh: 客户端音频静音
45
+ */
46
+ AUDIO_MUTED = "client.audio.muted",
47
+ /**
48
+ * en: Client video on
49
+ * zh: 客户端视频开启
50
+ */
51
+ VIDEO_ON = "client.video.on",
52
+ /**
53
+ * en: Client video off
54
+ * zh: 客户端视频关闭
55
+ */
56
+ VIDEO_OFF = "client.video.off",
57
+ /**
58
+ * en: Client video event
59
+ * zh: 客户端视频事件
60
+ */
61
+ PLAYER_EVENT = "client.video.event",
62
+ /**
63
+ * en: Client error
64
+ * zh: 客户端错误
65
+ */
66
+ ERROR = "client.error",
67
+ /**
68
+ * en: Audio noise reduction enabled
69
+ * zh: 抑制平稳噪声
70
+ */
71
+ SUPPRESS_STATIONARY_NOISE = "client.suppress.stationary.noise",
72
+ /**
73
+ * en: Suppress non-stationary noise
74
+ * zh: 抑制非平稳噪声
75
+ */
76
+ SUPPRESS_NON_STATIONARY_NOISE = "client.suppress.non.stationary.noise",
77
+ /**
78
+ * en: Audio input device changed
79
+ * zh: 音频输入设备改变
80
+ */
81
+ AUDIO_INPUT_DEVICE_CHANGED = "client.input.device.changed",
82
+ /**
83
+ * en: Audio output device changed
84
+ * zh: 音频输出设备改变
85
+ */
86
+ AUDIO_OUTPUT_DEVICE_CHANGED = "client.output.device.changed",
87
+ /**
88
+ * en: Video input device changed
89
+ * zh: 视频输入设备改变
90
+ */
91
+ VIDEO_INPUT_DEVICE_CHANGED = "client.video.input.device.changed",
92
+ /**
93
+ * en: Network quality changed
94
+ * zh: 网络质量改变
95
+ */
96
+ NETWORK_QUALITY = "client.network.quality",
97
+ /**
98
+ * en: Bot joined
99
+ * zh: Bot 加入
100
+ */
101
+ BOT_JOIN = "server.bot.join",
102
+ /**
103
+ * en: Bot left
104
+ * zh: Bot 离开
105
+ */
106
+ BOT_LEAVE = "server.bot.leave",
107
+ /**
108
+ * en: Audio speech started
109
+ * zh: 开始说话
110
+ */
111
+ AUDIO_AGENT_SPEECH_STARTED = "server.audio.agent.speech_started",
112
+ /**
113
+ * en: Audio speech stopped
114
+ * zh: 停止说话
115
+ */
116
+ AUDIO_AGENT_SPEECH_STOPPED = "server.audio.agent.speech_stopped",
117
+ /**
118
+ * en: Server error
119
+ * zh: 服务端错误
120
+ */
121
+ SERVER_ERROR = "server.error",
122
+ /**
123
+ * en: User speech started
124
+ * zh: 用户开始说话
125
+ */
126
+ AUDIO_USER_SPEECH_STARTED = "server.audio.user.speech_started",
127
+ /**
128
+ * en: User speech stopped
129
+ * zh: 用户停止说话
130
+ */
131
+ AUDIO_USER_SPEECH_STOPPED = "server.audio.user.speech_stopped",
132
+ /**
133
+ * en: User successfully enters the room
134
+ * zh: 用户成功进入房间后,会收到该事件
135
+ */
136
+ SESSION_CREATED = "server.session.created",
137
+ /**
138
+ * en: Session updated
139
+ * zh: 会话更新
140
+ */
141
+ SESSION_UPDATE = "server.session.update"
142
+ }
143
+ export default EventNames;
@@ -0,0 +1,154 @@
1
+ import { type ScreenConfig, type AudioPropertiesConfig, type IRTCEngine } from '@volcengine/rtc';
2
+ import { type CreateRoomData, type GetToken } from '@coze/api';
3
+ import * as RealtimeUtils from './utils';
4
+ import EventNames from './event-names';
5
+ import { RealtimeEventHandler } from './event-handler';
6
+ import { RealtimeAPIError, RealtimeError } from './error';
7
+ export interface VideoConfig {
8
+ videoOnDefault?: boolean /** optional, Whether to turn on video by default, defaults to true */;
9
+ renderDom?: string /** optional, The DOM element to render the video stream to */;
10
+ videoInputDeviceId?: string /** optional, The device ID of the video input device to use */;
11
+ screenConfig?: ScreenConfig;
12
+ }
13
+ export interface RealtimeClientConfig {
14
+ accessToken: GetToken /** required, Access Token */;
15
+ botId: string /** required, Bot Id */;
16
+ voiceId?: string /** optional, Voice Id */;
17
+ conversationId?: string /** optional, Conversation Id */;
18
+ userId?: string /** optional, User Id */;
19
+ workflowId?: string /** optional, Workflow Id */;
20
+ baseURL?: string /** optional, defaults to "https://api.coze.cn" */;
21
+ debug?: boolean /** optional, defaults to false */;
22
+ getRoomInfo?: () => Promise<CreateRoomData> /** optional, Customize get room info */;
23
+ /** Whether Personal Access Tokens (PAT) are allowed in browser environments */
24
+ allowPersonalAccessTokenInBrowser?: boolean;
25
+ /** Whether to mute by default, defaults to false
26
+ * If set to true, audio streams will not be automatically published and subscribed */
27
+ audioMutedDefault?: boolean;
28
+ connectorId: string /** required, Connector Id */;
29
+ suppressStationaryNoise?: boolean /** optional, Suppress stationary noise, defaults to false */;
30
+ suppressNonStationaryNoise?: boolean /** optional, Suppress non-stationary noise, defaults to false */;
31
+ videoConfig?: VideoConfig /** optional, Video configuration */;
32
+ isAutoSubscribeAudio?: boolean /** optional, Whether to automatically subscribe to bot reply audio streams, defaults to true */;
33
+ }
34
+ declare class RealtimeClient extends RealtimeEventHandler {
35
+ _config: RealtimeClientConfig;
36
+ private _client;
37
+ isConnected: boolean;
38
+ private _api;
39
+ private _isTestEnv;
40
+ _isSupportVideo: boolean;
41
+ /**
42
+ * Constructor for initializing a RealtimeClient instance.
43
+ *
44
+ * 构造函数,初始化RealtimeClient实例。
45
+ *
46
+ * @param config
47
+ * @param config.accessToken - Required, Access Token. |
48
+ * 必填,Access Token。
49
+ * @param config.botId - Required, Bot Id. |
50
+ * 必填,Bot Id。
51
+ * @param config.voiceId - Optional, Voice Id. |
52
+ * 可选,音色Id。
53
+ * @param config.conversationId - Optional, Conversation Id. |
54
+ * 可选,会话Id。
55
+ * @param config.userId - Optional, User Id. |
56
+ * 可选,用户Id。
57
+ * @param config.baseURL - Optional, defaults to "https://api.coze.cn". |
58
+ * 可选,默认值为 "https://api.coze.cn"。
59
+ * @param config.debug - Optional, defaults to false.
60
+ * 可选,默认值为 false。
61
+ * @param config.allowPersonalAccessTokenInBrowser
62
+ * - Optional, whether to allow personal access tokens in browser environment. |
63
+ * 可选,是否允许在浏览器环境中使用个人访问令牌。
64
+ * @param config.audioMutedDefault - Optional, whether audio is muted by default, defaults to false. |
65
+ * 可选,默认是否静音,默认值为 false。
66
+ * @param config.connectorId - Required, Connector Id. |
67
+ * 必填,渠道 Id。
68
+ * @param config.suppressStationaryNoise - Optional, suppress stationary noise, defaults to false. |
69
+ * 可选,默认是否抑制静态噪声,默认值为 false。
70
+ * @param config.suppressNonStationaryNoise - Optional, suppress non-stationary noise, defaults to false. |
71
+ * 可选,默认是否抑制非静态噪声,默认值为 false。
72
+ * @param config.isAutoSubscribeAudio - Optional, whether to automatically subscribe to bot reply audio streams, defaults to true. |
73
+ * @param config.videoConfig - Optional, Video configuration. |
74
+ * 可选,视频配置。
75
+ * @param config.videoConfig.videoOnDefault - Optional, Whether to turn on video by default, defaults to true. |
76
+ * 可选,默认是否开启视频,默认值为 true。
77
+ * @param config.videoConfig.renderDom - Optional, The DOM element to render the video stream to. |
78
+ * 可选,渲染视频流的 DOM 元素。
79
+ * @param config.videoConfig.videoInputDeviceId - Optional, The device ID of the video input device to use. |
80
+ * 可选,视频输入设备的设备 ID。
81
+ * @param config.videoConfig.screenConfig - Optional, Screen share configuration if videoInputDeviceId is 'screenShare' see https://www.volcengine.com/docs/6348/104481#screenconfig for more details. |
82
+ * 可选,屏幕共享配置,如果 videoInputDeviceId 是 'screenShare',请参考 https://www.volcengine.com/docs/6348/104481#screenconfig 了解更多详情。
83
+ */
84
+ constructor(config: RealtimeClientConfig);
85
+ /**
86
+ * en: Establish a connection to the Coze API and join the room
87
+ *
88
+ * zh: 建立与 Coze API 的连接并加入房间
89
+ */
90
+ connect(): Promise<void>;
91
+ /**
92
+ * en: Interrupt the current conversation
93
+ *
94
+ * zh: 中断当前对话
95
+ */
96
+ interrupt(): Promise<void>;
97
+ /**
98
+ * en: Disconnect from the current session
99
+ *
100
+ * zh: 断开与当前会话的连接
101
+ */
102
+ disconnect(): Promise<void>;
103
+ /**
104
+ * en: Send a message to the bot
105
+ *
106
+ * zh: 发送消息给Bot
107
+ */
108
+ sendMessage(message: Record<string, unknown>): Promise<void>;
109
+ /**
110
+ * en: Enable or disable audio
111
+ *
112
+ * zh: 启用或禁用音频
113
+ */
114
+ setAudioEnable(isEnable: boolean): Promise<void>;
115
+ setVideoEnable(isEnable: boolean): Promise<void>;
116
+ /**
117
+ * en: Enable audio properties reporting (debug mode only)
118
+ *
119
+ * zh: 启用音频属性报告(仅限调试模式)
120
+ */
121
+ enableAudioPropertiesReport(config?: AudioPropertiesConfig): boolean;
122
+ /**
123
+ * en: Start audio playback device test (debug mode only)
124
+ *
125
+ * zh: 开始音频播放设备测试(仅限调试模式)
126
+ */
127
+ startAudioPlaybackDeviceTest(): Promise<void>;
128
+ /**
129
+ * en: Stop audio playback device test (debug mode only)
130
+ *
131
+ * zh: 停止音频播放设备测试(仅限调试模式)
132
+ */
133
+ stopAudioPlaybackDeviceTest(): void;
134
+ /**
135
+ * en: Set the audio input device
136
+ *
137
+ * zh: 设置音频输入设备
138
+ */
139
+ setAudioInputDevice(deviceId: string): Promise<void>;
140
+ /**
141
+ * en: Set the audio output device
142
+ *
143
+ * zh: 设置音频输出设备
144
+ */
145
+ setAudioOutputDevice(deviceId: string): Promise<void>;
146
+ setVideoInputDevice(deviceId: string): Promise<void>;
147
+ /**
148
+ * en: Get the RTC engine instance, for detail visit https://www.volcengine.com/docs/6348/104481
149
+ *
150
+ * zh: 获取 RTC 引擎实例,详情请访问 https://www.volcengine.com/docs/6348/104481
151
+ */
152
+ getRtcEngine(): IRTCEngine | undefined;
153
+ }
154
+ export { RealtimeUtils, RealtimeClient, RealtimeAPIError, RealtimeError, EventNames, };
@@ -0,0 +1,43 @@
1
+ /**
2
+ + * Delays execution for the specified duration
3
+ + * @param milliseconds The time to sleep in milliseconds
4
+ + * @throws {Error} If milliseconds is negative
5
+ + * @returns Promise that resolves after the specified duration
6
+ + */
7
+ export declare const sleep: (milliseconds: number) => Promise<void>;
8
+ /**
9
+ * @deprecated use checkDevicePermission instead
10
+ * Check microphone permission,return boolean
11
+ */
12
+ export declare const checkPermission: ({ audio, video, }?: {
13
+ audio?: boolean;
14
+ video?: boolean;
15
+ }) => Promise<boolean>;
16
+ /**
17
+ * Checks device permissions for audio and video
18
+ * @param checkVideo Whether to check video permissions (default: false)
19
+ * @returns Promise that resolves with the device permission status
20
+ */
21
+ export declare const checkDevicePermission: (checkVideo?: boolean) => Promise<{
22
+ video: boolean;
23
+ audio: boolean;
24
+ videoExceptionError?: DOMException | undefined;
25
+ audioExceptionError?: DOMException | undefined;
26
+ }>;
27
+ /**
28
+ * Get audio devices
29
+ * @returns Promise<AudioDevices> Object containing arrays of audio input and output devices
30
+ */
31
+ export declare const getAudioDevices: ({ video, }?: {
32
+ video?: boolean;
33
+ }) => Promise<{
34
+ audioInputs: MediaDeviceInfo[];
35
+ audioOutputs: MediaDeviceInfo[];
36
+ videoInputs: MediaDeviceInfo[];
37
+ }>;
38
+ export declare const isScreenShareDevice: (deviceId?: string) => boolean;
39
+ /**
40
+ * Check if browser supports screen sharing
41
+ * 检查浏览器是否支持屏幕共享
42
+ */
43
+ export declare function isScreenShareSupported(): boolean;