@bdky/aaas-pilot-kit 1.0.7 → 1.0.9

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,6 +1,7 @@
1
1
  import { type IConversationBeanSnapshot } from './conversationBean';
2
2
  import { ClientConversationBean } from './clientConversationBean';
3
3
  import { AIWorkerConversationBean } from './aiWorkerConversationBean';
4
+ import { type IConversationUIState } from './types';
4
5
  import type { IResolvedOptions } from '../../../types/config';
5
6
  export type AnyConversation = ClientConversationBean | AIWorkerConversationBean;
6
7
  export type IConversationFormatter = (text: string) => string;
@@ -9,6 +10,11 @@ export declare class ConversationService {
9
10
  beans: AnyConversation[];
10
11
  currentConversation: AnyConversation | null;
11
12
  private aiWorkerConversationFormatters;
13
+ /**
14
+ * UI 渲染状态缓存
15
+ * 用于存储会话的 UI 层渲染数据(累加后的文本、UI ID 等)
16
+ */
17
+ private readonly uiStateCache;
12
18
  constructor(opts: IResolvedOptions);
13
19
  get last(): AnyConversation | null;
14
20
  get currentClientConversation(): ClientConversationBean | null;
@@ -20,6 +26,24 @@ export declare class ConversationService {
20
26
  setCurrentConversation: (conversation: AnyConversation | null) => void;
21
27
  clear: () => void;
22
28
  toJSON: () => IConversationBeanSnapshot[];
29
+ /**
30
+ * 获取会话的 UI 渲染状态缓存
31
+ * 返回深拷贝副本,防止多实例修改共享引用
32
+ */
33
+ getUIState: (conversationId: string) => IConversationUIState | undefined;
34
+ /**
35
+ * 设置会话的 UI 渲染状态缓存
36
+ * 深拷贝后再存储,防止外部修改影响缓存
37
+ */
38
+ setUIState: (conversationId: string, state: IConversationUIState) => void;
39
+ /**
40
+ * 清除指定会话的 UI 状态缓存
41
+ */
42
+ clearUIState: (conversationId: string) => void;
43
+ /**
44
+ * 从 AIWorkerConversationBean 的 contents 构建 UI 渲染快照
45
+ */
46
+ buildSnapshotUIState: (conversation: AIWorkerConversationBean) => IConversationUIState;
23
47
  dispose: () => void;
24
48
  /**
25
49
  * 注册aiWorkerConversation formatter
@@ -0,0 +1,37 @@
1
+ /**
2
+ * UI 层会话内容渲染状态
3
+ * 用于在 SDK 层缓存 UI 组件需要的渲染数据
4
+ */
5
+ export interface IConversationUIContent {
6
+ /**
7
+ * UI 层唯一标识,用于 React key
8
+ */
9
+ uiId: string;
10
+ /**
11
+ * 内容类型
12
+ */
13
+ mod: 'text' | 'image' | 'video';
14
+ /**
15
+ * 内容数据(文本内容或资源URL)
16
+ */
17
+ content: string;
18
+ /**
19
+ * 是否已完成渲染
20
+ */
21
+ completed: boolean;
22
+ }
23
+ /**
24
+ * 会话的 UI 渲染状态
25
+ */
26
+ export interface IConversationUIState {
27
+ /**
28
+ * UI 渲染内容列表
29
+ */
30
+ contents: IConversationUIContent[];
31
+ /**
32
+ * 渲染模式
33
+ * - stream: 流式渲染模式(增量更新)
34
+ * - snapshot: 快照模式(从 bean.contents 构建)
35
+ */
36
+ mode: 'stream' | 'snapshot';
37
+ }
@@ -37,6 +37,7 @@ export declare namespace NBaseAsrService {
37
37
  interface IMessageEventPayload extends IMessageProtocol, IEventWithSessionIdPayload {
38
38
  }
39
39
  type IDeviceCheckCompletedPayload = AudioDeviceCheckResult;
40
+ type IDeviceCheckResult = AudioDeviceCheckResult;
40
41
  interface IEmitter {
41
42
  start: IStartEventPayload;
42
43
  stop: IStopEventPayload;
@@ -102,6 +103,15 @@ export declare abstract class BaseAsrService<E extends NBaseAsrService.IEmitter
102
103
  checkAudioDevice: (options?: AudioDetectorOptions & {
103
104
  forceCheck?: boolean;
104
105
  }) => Promise<AudioDeviceCheckResult>;
106
+ /**
107
+ * 处理麦克风检测失败的策略
108
+ *
109
+ * @param strategy - 处理策略
110
+ * @param result - 检测结果
111
+ * @param callback - 用户自定义回调(仅 prompt 策略使用)
112
+ * @returns Promise<boolean> - true: 继续启动,false: 终止启动
113
+ */
114
+ protected readonly handleMicrophoneCheckFailure: (strategy: "error" | "warn" | "silent" | "prompt", result: AudioDeviceCheckResult, callback?: (result: AudioDeviceCheckResult) => Promise<boolean>) => Promise<boolean>;
105
115
  protected readonly _stop: () => Promise<void>;
106
116
  protected readonly _dispose: () => void;
107
117
  /**
@@ -46,8 +46,10 @@ export interface AudioDeviceCheckResult {
46
46
  available: boolean;
47
47
  /** 错误类型 (仅在 available = false 时存在) */
48
48
  error?: AudioDeviceError;
49
- /** 错误详细信息 */
49
+ /** 错误详细信息(技术性) */
50
50
  message?: string;
51
+ /** 用户友好的错误提示文案 */
52
+ userMessage?: string;
51
53
  /** 检测通过的层级 (1-4) */
52
54
  passedLevel?: number;
53
55
  /** 找到的音频设备列表 */
@@ -2,6 +2,7 @@ import type { Newable } from 'inversify';
2
2
  import type { AgentConfig } from './agent';
3
3
  import type { Env } from './common';
4
4
  import { BaseAgentService, NBaseAgentService } from '../lib/service/agent/baseAgentService';
5
+ import type { NBaseAsrService } from '../lib/service/rtc-asr/asr/baseAsrService';
5
6
  import { type ReplacementRule } from '../lib/utils/applyTextReplacements';
6
7
  import { NPictureClientDigitalHumanService } from '../lib/service/digital-human/PictureClientDigitalHumanService';
7
8
  import Emittery from 'emittery';
@@ -232,6 +233,57 @@ export interface IOptions<AS extends BaseAgentService = BaseAgentService> {
232
233
  * @default true
233
234
  */
234
235
  checkAudioDeviceBeforeStart?: boolean;
236
+ /**
237
+ * 🎙️【选填】麦克风检测失败时的处理策略
238
+ *
239
+ * 当 checkAudioDeviceBeforeStart=true 且麦克风检测失败时的行为:
240
+ *
241
+ * - 'error' (默认) → 抛出 AsrInitializationError,终止启动
242
+ * - 'warn' → 在控制台输出警告,禁用语音输入,继续启动(仅文本模式)
243
+ * - 'silent' → 静默降级为仅文本模式,不输出警告(仍会发出事件)
244
+ * - 'prompt' → 调用 onMicrophoneCheckFailed 回调,让开发者自定义交互逻辑
245
+ *
246
+ * 💡 使用场景:
247
+ * - 'error' → 严格要求语音功能的场景(如语音客服)
248
+ * - 'warn' → 语音为可选功能,允许文本输入兜底
249
+ * - 'silent' → 自动降级,不干扰用户体验
250
+ * - 'prompt' → 需要显示自定义对话框让用户确认
251
+ *
252
+ * ⚠️ 注意:
253
+ * - 降级后用户仍可通过 input(text) 方法进行文本输入
254
+ * - 系统会发出 device_check_completed 和 microphone_available 事件
255
+ * - ASR 服务会被自动禁用(controller.asrService.disabled = true)
256
+ *
257
+ * @default 'error'
258
+ */
259
+ microphoneFailureHandling?: 'error' | 'warn' | 'silent' | 'prompt';
260
+ /**
261
+ * 🔔【选填】麦克风检测失败时的自定义处理回调
262
+ *
263
+ * 仅当 microphoneFailureHandling='prompt' 时生效。
264
+ *
265
+ * 💡 典型用途:
266
+ * - 显示自定义确认对话框确认是否继续流程
267
+ * - 根据错误类型展示不同的提示信息
268
+ * - 记录用户选择到分析系统
269
+ *
270
+ * @param result - 设备检测结果,包含错误详情、设备列表、权限状态等
271
+ * @returns Promise<boolean> - true: 继续启动(文本模式),false: 终止启动
272
+ *
273
+ * @example
274
+ * ```typescript
275
+ * onMicrophoneCheckFailed: async (result) => {
276
+ * // result.userMessage 已包含用户友好的错误提示
277
+ * const userChoice = await showDialog({
278
+ * title: '麦克风不可用',
279
+ * message: `${result.userMessage}\n\n您可以选择继续使用文本输入。`,
280
+ * buttons: ['继续(仅文本)', '取消']
281
+ * });
282
+ * return userChoice === 'continue';
283
+ * }
284
+ * ```
285
+ */
286
+ onMicrophoneCheckFailed?: (result: NBaseAsrService.IDeviceCheckResult) => Promise<boolean>;
235
287
  /**
236
288
  * 🌐【选填】运行环境
237
289
  * - 'development' → 开发调试(日志全开)
@@ -336,6 +388,7 @@ export declare const DEFAULT_OPTIONS: {
336
388
  readonly getMountContainer: () => HTMLElement;
337
389
  readonly hotWordReplacementRules: ReplacementRule[];
338
390
  readonly checkAudioDeviceBeforeStart: true;
391
+ readonly microphoneFailureHandling: "error";
339
392
  readonly rtcID: "appreimunmd7utp";
340
393
  };
341
394
  type DefaultedOptionKeys = keyof typeof DEFAULT_OPTIONS;
@@ -754,7 +807,7 @@ export interface IAaaSPilotKitController<AS extends BaseAgentService = BaseAgent
754
807
  */
755
808
  activateManually(): Promise<void>;
756
809
  /**
757
- * 🕰️ 重置会话超时计时器 —— “我还在用,别退出!”
810
+ * 🕰️ 重置会话超时计时器 —— "我还在用,别退出!"
758
811
  *
759
812
  * 🔹 行为:
760
813
  * - 重新从 0 开始计时 opts.timeoutSec
@@ -765,4 +818,18 @@ export interface IAaaSPilotKitController<AS extends BaseAgentService = BaseAgent
765
818
  * @returns this(支持链式调用)
766
819
  */
767
820
  keepAlive(): IAaaSPilotKitController;
821
+ /**
822
+ * 🎤 播放开场白(Prologue)
823
+ *
824
+ * 🔹 行为:
825
+ * - 若 opts.prologue 有值 → 直接播放自定义开场白
826
+ * - 若未配置 → 调用 Agent 接口获取默认开场白
827
+ *
828
+ * 💡 适用场景:
829
+ * - mount 完成后自动播放欢迎语(一般在 ready 之后调用)
830
+ * - 手动激活(activateManually)后播放
831
+ *
832
+ * ⚠️ 注意:通常不需要手动调用,框架会在合适时机自动触发
833
+ */
834
+ playPrologue(): void;
768
835
  }
package/package.json CHANGED
@@ -1,7 +1,28 @@
1
1
  {
2
2
  "name": "@bdky/aaas-pilot-kit",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
+ "description": "百度数字员工基础套件 - AI智能体、语音识别、数字人渲染全链路SDK,事件驱动、框架无关",
5
+ "keywords": [
6
+ "digital-employee",
7
+ "digital-human",
8
+ "数字员工",
9
+ "数字人",
10
+ "ai-assistant",
11
+ "chatbot",
12
+ "conversational-ai",
13
+ "speech-recognition",
14
+ "asr",
15
+ "语音识别",
16
+ "tts",
17
+ "text-to-speech",
18
+ "语音合成",
19
+ "avatar",
20
+ "baidu",
21
+ "百度",
22
+ "客悦"
23
+ ],
4
24
  "license": "MIT",
25
+ "author": "Baidu Keyue Team <zhangwenxi@baidu.com>",
5
26
  "contributors": [
6
27
  {
7
28
  "name": "zhangwenxi",