@bytexbyte/nxtlinq-ai-agent-sdk 1.6.16 → 1.6.18

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,16 +1,15 @@
1
- import { useEffect, useRef, useState } from 'react';
1
+ import { useCallback, useEffect, useRef, useState } from 'react';
2
2
  import { startSpeechToTextFromMic, stopRecognition } from './helper';
3
3
  export function useSpeechToTextFromMic({ apiKey, apiSecret, autoClearTranscript = true, }) {
4
- const [isRecording, setIsRecording] = useState(false);
4
+ const [isMicEnabled, setIsMicEnabled] = useState(false);
5
5
  const [transcriptArray, setTranscriptArray] = useState([]);
6
6
  const [partialTranscript, setPartialTranscript] = useState('');
7
7
  const [recognizer, setRecognizer] = useState();
8
8
  const wakelock = useRef();
9
9
  const historyRef = useRef([]);
10
10
  const indexRef = useRef(0);
11
- const canWakeLock = () => 'wakeLock' in navigator;
12
- async function lockWakeState() {
13
- if (!canWakeLock())
11
+ const lockWakeState = useCallback(async () => {
12
+ if (typeof navigator === 'undefined' || !('wakeLock' in navigator))
14
13
  return;
15
14
  try {
16
15
  wakelock.current = await navigator.wakeLock.request('screen');
@@ -21,26 +20,34 @@ export function useSpeechToTextFromMic({ apiKey, apiSecret, autoClearTranscript
21
20
  catch (err) {
22
21
  console.error('Wake lock error:', err);
23
22
  }
24
- }
25
- const start = async () => {
23
+ }, []);
24
+ const clear = useCallback(() => {
25
+ historyRef.current = [];
26
+ indexRef.current = 0;
27
+ setTranscriptArray([]);
28
+ setPartialTranscript('');
29
+ }, [setPartialTranscript, setTranscriptArray]);
30
+ const start = useCallback(async () => {
26
31
  clear();
27
32
  await lockWakeState();
28
- setIsRecording(true);
29
- const recognizerInstance = await startSpeechToTextFromMic(setTranscriptArray, { apiKey, apiSecret }, historyRef, indexRef, setPartialTranscript);
30
- setRecognizer(recognizerInstance);
31
- };
32
- const stop = () => {
33
+ try {
34
+ const recognizerInstance = await startSpeechToTextFromMic(setTranscriptArray, { apiKey, apiSecret }, historyRef, indexRef, setPartialTranscript);
35
+ setRecognizer(recognizerInstance);
36
+ setIsMicEnabled(true);
37
+ }
38
+ catch (error) {
39
+ wakelock.current?.release();
40
+ setIsMicEnabled(false);
41
+ console.error('Failed to start speech recognition:', error);
42
+ throw error;
43
+ }
44
+ }, [apiKey, apiSecret, clear, lockWakeState, setPartialTranscript, setTranscriptArray]);
45
+ const stop = useCallback(() => {
33
46
  wakelock.current?.release();
34
47
  stopRecognition(recognizer);
35
- setIsRecording(false);
48
+ setIsMicEnabled(false);
36
49
  setRecognizer(undefined);
37
- };
38
- const clear = () => {
39
- historyRef.current = [];
40
- indexRef.current = 0;
41
- setTranscriptArray([]);
42
- setPartialTranscript('');
43
- };
50
+ }, [recognizer, setRecognizer]);
44
51
  useEffect(() => {
45
52
  if (autoClearTranscript && transcriptArray.length > 0) {
46
53
  const timer = setTimeout(() => {
@@ -54,7 +61,7 @@ export function useSpeechToTextFromMic({ apiKey, apiSecret, autoClearTranscript
54
61
  start,
55
62
  stop,
56
63
  clear,
57
- isRecording,
64
+ isMicEnabled,
58
65
  transcript,
59
66
  partialTranscript,
60
67
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bytexbyte/nxtlinq-ai-agent-sdk",
3
- "version": "1.6.16",
3
+ "version": "1.6.18",
4
4
  "description": "Nxtlinq AI Agent SDK - Proprietary Software with enhanced async operation handling",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",