@huyooo/ai-chat-frontend-react 0.2.12 → 0.2.13

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@huyooo/ai-chat-frontend-react",
3
- "version": "0.2.12",
3
+ "version": "0.2.13",
4
4
  "description": "AI Chat Frontend - React components with adapter pattern",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -32,8 +32,8 @@
32
32
  "react-dom": ">=18.0.0"
33
33
  },
34
34
  "dependencies": {
35
- "@huyooo/ai-chat-bridge-electron": "^0.2.12",
36
- "@huyooo/ai-chat-shared": "^0.2.12",
35
+ "@huyooo/ai-chat-bridge-electron": "^0.2.13",
36
+ "@huyooo/ai-chat-shared": "^0.2.13",
37
37
  "@iconify/react": "^5.0.2"
38
38
  },
39
39
  "devDependencies": {
@@ -55,6 +55,9 @@ function resample(audioData: Float32Array, fromSampleRate: number, toSampleRate:
55
55
  return result;
56
56
  }
57
57
 
58
+ // 全局预热标志:确保只预热一次(避免多个组件实例重复预热)
59
+ let asrWarmupDone = false;
60
+
58
61
  async function setupAudioWorkletCapture(opts: {
59
62
  audioContext: AudioContext;
60
63
  source: MediaStreamAudioSourceNode;
@@ -156,6 +159,20 @@ registerProcessor('pcm-capture', PcmCaptureProcessor);
156
159
  export function useVoiceInput(adapter: ChatAdapter | undefined, config: VoiceInputConfig = {}): UseVoiceInputReturn {
157
160
  const { sampleRate = 16000, sendInterval = 200, enablePunc = true, enableItn = true } = config;
158
161
 
162
+ // 自动预热 ASR 连接(仅首次调用,延迟执行,避免阻塞初始化)
163
+ useEffect(() => {
164
+ if (adapter && !asrWarmupDone && typeof adapter.asrWarmup === 'function') {
165
+ asrWarmupDone = true;
166
+ // 延迟 800ms 预热,避免与首屏渲染竞争资源
167
+ const timer = setTimeout(() => {
168
+ adapter.asrWarmup?.().catch(() => {
169
+ // 静默失败,不影响功能
170
+ });
171
+ }, 800);
172
+ return () => clearTimeout(timer);
173
+ }
174
+ }, [adapter]);
175
+
159
176
  const [status, setStatus] = useState<VoiceInputStatus>('idle');
160
177
  const [currentText, setCurrentText] = useState('');
161
178
  const [finalText, setFinalText] = useState('');